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 --- .../main/java/com/juick/components/XMPPServer.java | 32 ++++++++----- .../configuration/XmppAppConfiguration.java | 5 +++ .../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 +++++ 8 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 juick-xmpp/src/main/java/com/juick/components/s2s/StanzaListener.java (limited to 'juick-xmpp') diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java index caa4c200..e057a9ea 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java @@ -33,7 +33,6 @@ public class XMPPServer implements AutoCloseable { public ExecutorService service; private ConnectionRouter router; - public JuickBot bot; public String HOSTNAME, componentName; public String keystore; @@ -44,6 +43,7 @@ public class XMPPServer implements AutoCloseable { private final List inConnections = Collections.synchronizedList(new ArrayList<>()); private final List outConnections = Collections.synchronizedList(new ArrayList<>()); private final List outCache = Collections.synchronizedList(new ArrayList<>()); + private final List stanzaListeners = Collections.synchronizedList(new ArrayList<>()); final public HashMap childParsers = new HashMap<>(); @@ -58,6 +58,7 @@ public class XMPPServer implements AutoCloseable { @Inject public SubscriptionService subscriptionService; + private JID jid; public XMPPServer(Environment env, ExecutorService service) { this.service = service; @@ -68,12 +69,11 @@ public class XMPPServer implements AutoCloseable { componentName = env.getProperty("componentname"); int componentPort = NumberUtils.toInt(env.getProperty("component_port"), 5347); int s2sPort = NumberUtils.toInt(env.getProperty("s2s_port"), 5269); - JID Jid = new JID(env.getProperty("xmppbot_jid")); keystore = env.getProperty("keystore"); keystorePassword = env.getProperty("keystore_password"); brokenSSLhosts = Arrays.asList(env.getProperty("broken_ssl_hosts", "").split(",")); bannedHosts = Arrays.asList(env.getProperty("banned_hosts", "").split(",")); - bot = new JuickBot(this, Jid); + jid = new JID(env.getProperty("xmppbot_jid")); boolean disabled = BooleanUtils.toBoolean(env.getProperty("xmpp_disabled", "false")); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); @@ -88,7 +88,7 @@ public class XMPPServer implements AutoCloseable { while (true) { try { Socket socket = listener.accept(); - ConnectionIn client = new ConnectionIn(this, bot, socket); + ConnectionIn client = new ConnectionIn(this, socket); addConnectionIn(client); service.submit(client); } catch (Exception e) { @@ -206,12 +206,7 @@ public class XMPPServer implements AutoCloseable { } } if (connOut != null) { - try { - connOut.sendStanza(xml); - } catch (IOException e) { - logger.warn("stream to {} {} error: {}", - connOut.to, connOut.streamID, e); - } + connOut.sendStanza(xml); return; } @@ -266,4 +261,21 @@ public class XMPPServer implements AutoCloseable { } } + public List getStanzaListeners() { + return stanzaListeners; + } + + public void addStanzaListener(StanzaListener listener) { + synchronized (stanzaListeners) { + stanzaListeners.add(listener); + } + } + + public void onStanzaReceived(String type, Stanza xmlValue) { + stanzaListeners.forEach(l -> l.stanzaReceived(type, xmlValue)); + } + + public JID getJid() { + return jid; + } } diff --git a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java index a10e6553..50bce7b9 100644 --- a/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java +++ b/juick-xmpp/src/main/java/com/juick/components/configuration/XmppAppConfiguration.java @@ -6,6 +6,7 @@ package com.juick.components.configuration; import com.juick.components.XMPPServer; import com.juick.components.s2s.CleaningUp; +import com.juick.components.s2s.JuickBot; import com.juick.configuration.DataConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -39,4 +40,8 @@ public class XmppAppConfiguration { public CleaningUp cleaningUp() { return new CleaningUp(); } + @Bean + public JuickBot bot() { + return new JuickBot(xmpp()); + } } 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