aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java')
-rw-r--r--juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java52
1 files changed, 33 insertions, 19 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java b/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java
index 2ed3eaf5..304d365e 100644
--- a/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java
+++ b/juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java
@@ -5,9 +5,11 @@ import com.juick.components.XMPPServer;
import com.juick.xmpp.JID;
import com.juick.xmpp.Message;
import com.juick.xmpp.Presence;
+import com.juick.xmpp.Stanza;
import com.juick.xmpp.extensions.Error;
import com.juick.xmpp.extensions.JuickMessage;
+import javax.inject.Inject;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -16,15 +18,16 @@ import java.util.regex.Pattern;
*
* @author ugnich
*/
-public class JuickBot {
+public class JuickBot implements StanzaListener {
+
XMPPServer xmpp;
- public JuickBot(XMPPServer xmpp, JID jid) {
+ @Inject
+ public JuickBot(XMPPServer xmpp) {
this.xmpp = xmpp;
- this.jid = jid;
+ xmpp.addStanzaListener(this);
}
- private final JID jid;
private static final String HELPTEXT =
"@username text - Send private message\n"
+ "*tagname Blah-blah-blah - Post a message with tag 'tagname'\n"
@@ -59,7 +62,7 @@ public class JuickBot {
+ "\n"
+ "Read more: http://juick.com/help/";
- public boolean incomingPresence(Presence p) throws Exception {
+ public boolean incomingPresence(Presence p) {
final String username = p.to.Username.toLowerCase();
final boolean toJuick = username.equals("juick");
@@ -135,7 +138,7 @@ public class JuickBot {
return false;
}
- public boolean incomingMessage(Message msg) throws Exception {
+ public boolean incomingMessage(Message msg) {
if (msg.body == null || msg.body.isEmpty()) {
return false;
}
@@ -220,7 +223,7 @@ public class JuickBot {
}
private static Pattern regexPM = Pattern.compile("^\\@(\\S+)\\s+([\\s\\S]+)$");
- public boolean incomingMessageJuick(User user_from, Message msg) throws Exception {
+ public boolean incomingMessageJuick(User user_from, Message msg) {
String command = msg.body.trim();
int commandlen = command.length();
@@ -257,29 +260,29 @@ public class JuickBot {
return false;
}
- private void commandPing(Message m) throws Exception {
- Presence p = new Presence(jid, m.from);
+ private void commandPing(Message m) {
+ Presence p = new Presence(xmpp.getJid(), m.from);
p.priority = 10;
xmpp.sendOut(p);
- Message reply = new Message(jid, m.from, Message.Type.chat);
+ Message reply = new Message(xmpp.getJid(), m.from, Message.Type.chat);
reply.body = "PONG";
xmpp.sendOut(reply);
}
- private void commandHelp(Message m) throws Exception {
- Message reply = new Message(jid, m.from, Message.Type.chat);
+ private void commandHelp(Message m) {
+ Message reply = new Message(xmpp.getJid(), m.from, Message.Type.chat);
reply.body = HELPTEXT;
xmpp.sendOut(reply);
}
- private void commandLogin(Message m, User user_from) throws Exception {
- Message reply = new Message(jid, m.from, Message.Type.chat);
+ private void commandLogin(Message m, User user_from) {
+ Message reply = new Message(xmpp.getJid(), m.from, Message.Type.chat);
reply.body = "http://juick.com/login?" + xmpp.userService.getHashByUID(user_from.getUid());
xmpp.sendOut(reply);
}
- private void commandPM(Message m, User user_from, String user_to, String body) throws Exception {
+ private void commandPM(Message m, User user_from, String user_to, String body) {
int ret = 0;
int uid_to = 0;
@@ -347,7 +350,7 @@ public class JuickBot {
xmpp.sendOut(reply);
}
- private void commandBLShow(Message m, User user_from) throws Exception {
+ private void commandBLShow(Message m, User user_from) {
List<User> blusers = xmpp.userService.getUserBLUsers(user_from.getUid());
List<String> bltags = xmpp.tagService.getUserBLTags(user_from.getUid());
@@ -370,12 +373,23 @@ public class JuickBot {
txt = "You don't have any users or tags in your blacklist.";
}
- Message reply = new Message(jid, m.from, Message.Type.chat);
+ Message reply = new Message(xmpp.getJid(), m.from, Message.Type.chat);
reply.body = txt;
xmpp.sendOut(reply);
}
- public JID getJid() {
- return jid;
+ @Override
+ public void stanzaReceived(String name, Stanza xmlValue) {
+ if (xmlValue instanceof Presence) {
+ Presence p = (Presence) xmlValue;
+ if (p.type == null || !p.type.equals(Presence.Type.error)) {
+ incomingPresence(p);
+ }
+ } else if (xmlValue instanceof Message) {
+ Message msg = (Message) xmlValue;
+ if (!incomingMessage(msg)) {
+ xmpp.getRouter().sendStanza(msg.toString());
+ }
+ }
}
}