From eea7abf829ed9b011d34d6ac8131fe3057b1b6f6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 7 Dec 2016 14:28:31 +0300 Subject: juick-xmpp: JuickBot is now Bean --- .../java/com/juick/components/s2s/Connection.java | 11 +++-- .../com/juick/components/s2s/ConnectionIn.java | 24 +++------- .../com/juick/components/s2s/ConnectionOut.java | 7 +-- .../com/juick/components/s2s/ConnectionRouter.java | 6 +-- .../java/com/juick/components/s2s/JuickBot.java | 52 ++++++++++++++-------- .../com/juick/components/s2s/StanzaListener.java | 10 +++++ 6 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 juick-xmpp/src/main/java/com/juick/components/s2s/StanzaListener.java (limited to 'juick-xmpp/src/main/java/com/juick/components/s2s') diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java b/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java index b6052fc8..e54a1548 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java @@ -83,12 +83,17 @@ public class Connection { logger.trace(tag); } - public void sendStanza(String xml) throws IOException { + public void sendStanza(String xml) { if (streamID != null) { logger.trace("OUT: {}\n", xml); } - writer.write(xml); - writer.flush(); + try { + writer.write(xml); + writer.flush(); + } catch (IOException e) { + logger.error("send stanza failed", e); + } + tsLocalData = System.currentTimeMillis(); bytesLocal += xml.length(); packetsLocal++; diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java index a6d0549c..25f28ef6 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java @@ -26,11 +26,9 @@ public class ConnectionIn extends Connection implements Runnable { final public List from = new ArrayList<>(); public long tsRemoteData = 0; public long packetsRemote = 0; - JuickBot bot; - public ConnectionIn(XMPPServer xmpp, JuickBot bot, Socket socket) throws Exception { + public ConnectionIn(XMPPServer xmpp, Socket socket) throws Exception { super(xmpp); - this.bot = bot; this.socket = socket; streamID = UUID.randomUUID().toString(); restartParser(); @@ -101,17 +99,13 @@ public class ConnectionIn extends Connection implements Runnable { } } else if (tag.equals("presence") && checkFromTo(parser)) { Presence p = Presence.parse(parser, null); - if (p != null && (p.type == null || !p.type.equals(Presence.Type.error))) { - bot.incomingPresence(p); - } + xmpp.onStanzaReceived("presence", p); } else if (tag.equals("message") && checkFromTo(parser)) { updateTsRemoteData(); Message msg = Message.parse(parser, xmpp.childParsers); if (msg != null && (msg.type == null || !msg.type.equals(Message.Type.error))) { logger.info("stream {}: {}", streamID, msg); - if (!bot.incomingMessage(msg)) { - xmpp.getRouter().sendStanza(msg.toString()); - } + xmpp.onStanzaReceived("message", msg); } } else if (tag.equals("iq") && checkFromTo(parser)) { updateTsRemoteData(); @@ -180,14 +174,10 @@ public class ConnectionIn extends Connection implements Runnable { } public void sendDialbackResult(String sfrom, String type) { - try { - sendStanza(""); - if (type.equals("valid")) { - from.add(sfrom); - logger.info("stream from {} {} ready", sfrom, streamID); - } - } catch (IOException e) { - logger.warn("stream from {} {} error: {}", sfrom, streamID, e); + sendStanza(""); + if (type.equals("valid")) { + from.add(sfrom); + logger.info("stream from {} {} ready", sfrom, streamID); } } diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java index 0111601e..87e3cdbd 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java @@ -162,10 +162,7 @@ public class ConnectionOut extends Connection implements Runnable { } public void sendDialbackVerify(String sid, String key) { - try { - sendStanza("" + key + ""); - } catch (IOException e) { - logger.warn("stream to {} {} error {}", to, streamID, e); - } + sendStanza("" + + key + ""); } } diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java index 3517462b..b28298a2 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java @@ -163,7 +163,7 @@ public class ConnectionRouter extends Connection implements Runnable { nick.Nickname = "@" + jmsg.getUser().getName(); com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); - msg.from = xmpp.bot.getJid(); + msg.from = xmpp.getJid(); msg.body = txt; msg.type = Message.Type.chat; msg.thread = "juick-" + jmsg.getMid(); @@ -201,7 +201,7 @@ public class ConnectionRouter extends Connection implements Runnable { txt += jmsg.getText() + "\n\n" + "#" + jmsg.getMid() + "/" + jmsg.getRid() + " http://juick.com/" + jmsg.getMid() + "#" + jmsg.getRid(); com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); - msg.from = xmpp.bot.getJid(); + msg.from = xmpp.getJid(); msg.body = txt; msg.type = Message.Type.chat; msg.addChild(jmsg); @@ -241,7 +241,7 @@ public class ConnectionRouter extends Connection implements Runnable { nick.Nickname = "@" + jmsg.getUser().getName(); com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); - msg.from = xmpp.bot.getJid(); + msg.from = xmpp.getJid(); msg.body = txt; msg.type = Message.Type.chat; msg.thread = "juick-" + jmsg.getMid(); 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 blusers = xmpp.userService.getUserBLUsers(user_from.getUid()); List 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()); + } + } } } diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/StanzaListener.java b/juick-xmpp/src/main/java/com/juick/components/s2s/StanzaListener.java new file mode 100644 index 00000000..72ca72e8 --- /dev/null +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/StanzaListener.java @@ -0,0 +1,10 @@ +package com.juick.components.s2s; + +import com.juick.xmpp.Stanza; + +/** + * Created by vitalyster on 07.12.2016. + */ +public interface StanzaListener { + void stanzaReceived(String name, Stanza xmlValue); +} -- cgit v1.2.3