From 2be5fc6e0397b53173aa21298142d5fa66877fa5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 6 Feb 2017 12:14:29 +0300 Subject: juick-xmpp: ConnectionRouter uses babbler now --- .../java/com/juick/components/s2s/JuickBot.java | 255 ++++++++++++--------- 1 file changed, 147 insertions(+), 108 deletions(-) (limited to 'juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java') 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 f309bd21..cf791ada 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 @@ -2,13 +2,15 @@ package com.juick.components.s2s; import com.juick.User; import com.juick.components.XMPPServer; -import com.juick.xmpp.Message; -import com.juick.xmpp.Presence; -import com.juick.xmpp.Stanza; -import com.juick.xmpp.extensions.XMPPError; -import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.lang3.StringUtils; import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.stanza.model.Message; +import rocks.xmpp.core.stanza.model.Presence; +import rocks.xmpp.core.stanza.model.Stanza; +import rocks.xmpp.core.stanza.model.StanzaError; +import rocks.xmpp.core.stanza.model.client.ClientMessage; +import rocks.xmpp.core.stanza.model.client.ClientPresence; +import rocks.xmpp.core.stanza.model.errors.Condition; import javax.inject.Inject; import java.util.List; @@ -64,17 +66,17 @@ public class JuickBot implements StanzaListener { + "Read more: http://juick.com/help/"; public boolean incomingPresence(Presence p) { - final String username = p.to.getLocal(); + final String username = p.getTo().getLocal(); final boolean toJuick = username.equals("juick"); - if (p.type == null) { + if (p.getType() == null) { Presence reply = new Presence(); - reply.from = p.to.asBareJid(); - reply.to = p.from.asBareJid(); - reply.type = Presence.Type.unsubscribe; - xmpp.sendOut(reply); + reply.setFrom(p.getTo().asBareJid()); + reply.setTo(p.getFrom().asBareJid()); + reply.setType(Presence.Type.UNSUBSCRIBE); + xmpp.sendOut(ClientPresence.from(reply)); return true; - } else if (p.type.equals(Presence.Type.probe)) { + } else if (p.getType().equals(Presence.Type.PROBE)) { int uid_to = 0; if (!toJuick) { uid_to = xmpp.userService.getUIDbyName(username); @@ -82,84 +84,100 @@ public class JuickBot implements StanzaListener { if (toJuick || uid_to > 0) { Presence reply = new Presence(); - reply.from = p.to.withResource("Juick"); - reply.to = p.from; - reply.priority = 10; - xmpp.sendOut(reply); + reply.setFrom(p.getTo().withResource("Juick")); + reply.setTo(p.getFrom()); + reply.setPriority((byte)10); + xmpp.sendOut(ClientPresence.from(reply)); } else { - Presence reply = new Presence(p.to, p.from, Presence.Type.error); - reply.id = p.id; - reply.addChild(new XMPPError(XMPPError.Type.cancel, "item-not-found")); - xmpp.sendOut(reply); + Presence reply = new Presence(); + reply.setFrom(p.getTo()); + reply.setTo(p.getFrom()); + reply.setType(Presence.Type.ERROR); + reply.setId(p.getId()); + StanzaError stanzaError = new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND); + reply.addExtension(stanzaError); + xmpp.sendOut(ClientPresence.from(reply)); return true; } return true; - } else if (p.type.equals(Presence.Type.subscribe)) { + } else if (p.getType().equals(Presence.Type.SUBSCRIBE)) { boolean canSubscribe = false; if (toJuick) { canSubscribe = true; } else { int uid_to = xmpp.userService.getUIDbyName(username); if (uid_to > 0) { - xmpp.pmQueriesService.addPMinRoster(uid_to, p.from.asBareJid().toEscapedString()); + xmpp.pmQueriesService.addPMinRoster(uid_to, p.getFrom().asBareJid().toEscapedString()); canSubscribe = true; } } if (canSubscribe) { - Presence reply = new Presence(p.to, p.from, Presence.Type.subscribed); - xmpp.sendOut(reply); + Presence reply = new Presence(); + reply.setFrom(p.getTo()); + reply.setTo(p.getFrom()); + reply.setType(Presence.Type.SUBSCRIBED); + xmpp.sendOut(ClientPresence.from(reply)); - reply.from = reply.from.withResource("Juick"); - reply.priority = 10; - reply.type = null; - xmpp.sendOut(reply); + reply.setFrom(reply.getFrom().withResource("Juick")); + reply.setPriority((byte) 10); + reply.setType(null); + xmpp.sendOut(ClientPresence.from(reply)); return true; } else { - Presence reply = new Presence(p.to, p.from, Presence.Type.error); - reply.id = p.id; - reply.addChild(new XMPPError(XMPPError.Type.cancel, "item-not-found")); - xmpp.sendOut(reply); + Presence reply = new Presence(); + reply.setFrom(p.getTo()); + reply.setTo(p.getFrom()); + reply.setType(Presence.Type.ERROR); + reply.setId(p.getId()); + reply.addExtension(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); + xmpp.sendOut(ClientPresence.from(reply)); return true; } - } else if (p.type.equals(Presence.Type.unsubscribe)) { + } else if (p.getType().equals(Presence.Type.UNSUBSCRIBE)) { if (!toJuick) { int uid_to = xmpp.userService.getUIDbyName(username); if (uid_to > 0) { - xmpp.pmQueriesService.removePMinRoster(uid_to, p.from.asBareJid().toEscapedString()); + xmpp.pmQueriesService.removePMinRoster(uid_to, p.getFrom().asBareJid().toEscapedString()); } } - Presence reply = new Presence(p.to, p.from, Presence.Type.unsubscribed); - xmpp.sendOut(reply); + Presence reply = new Presence(); + reply.setFrom(p.getTo()); + reply.setTo(p.getFrom()); + reply.setType(Presence.Type.UNSUBSCRIBED); + xmpp.sendOut(ClientPresence.from(reply)); } return false; } public boolean incomingMessage(Message msg) { - if (msg.body == null || msg.body.isEmpty()) { + if (StringUtils.isBlank(msg.getBody())) { return false; } - String username = msg.to.getLocal(); + String username = msg.getTo().getLocal(); User user_from; String signuphash = StringUtils.EMPTY; - user_from = xmpp.userService.getUserByJID(msg.from.asBareJid().toEscapedString()); + user_from = xmpp.userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString()); if (user_from == null) { - signuphash = xmpp.userService.getSignUpHashByJID(msg.from.asBareJid().toEscapedString()); + signuphash = xmpp.userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString()); } if (user_from == null) { - Message reply = new Message(msg.to, msg.from, Message.Type.chat); + Message reply = new Message(); + reply.setFrom(msg.getTo()); + reply.setTo(msg.getFrom()); + reply.setType(Message.Type.CHAT); if (username.equals("juick")) { - reply.body = "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."; + reply.setBody("Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."); } else { - reply.body = "Внимание, системное сообщение!\nВаш JabberID не обнаружен в списке доверенных. Для того, чтобы отправить сообщение пользователю " + username + "@juick.com, пожалуйста зарегистрируйте свой JabberID в системе: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nWarning, system message!\nYour JabberID is not found in our server's white list. To send a message to " + username + "@juick.com, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."; + reply.setBody("Внимание, системное сообщение!\nВаш JabberID не обнаружен в списке доверенных. Для того, чтобы отправить сообщение пользователю " + username + "@juick.com, пожалуйста зарегистрируйте свой JabberID в системе: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nWarning, system message!\nYour JabberID is not found in our server's white list. To send a message to " + username + "@juick.com, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."); } - xmpp.sendOut(reply); + xmpp.sendOut(ClientMessage.from(reply)); return true; } @@ -170,53 +188,59 @@ public class JuickBot implements StanzaListener { int uid_to = xmpp.userService.getUIDbyName(username); if (uid_to == 0) { - Message reply = new Message(msg.to, msg.from, Message.Type.error); - reply.id = msg.id; - reply.addChild(new XMPPError(XMPPError.Type.cancel, "item-not-found")); - xmpp.sendOut(reply); + Message reply = new Message(); + reply.setFrom(msg.getTo()); + reply.setTo(msg.getFrom()); + reply.setType(Message.Type.ERROR); + reply.setId(msg.getId()); + reply.addExtension(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); + xmpp.sendOut(ClientMessage.from(reply)); return true; } boolean success = false; if (!xmpp.userService.isInBLAny(uid_to, user_from.getUid())) { - success = xmpp.pmQueriesService.createPM(user_from.getUid(), uid_to, msg.body); + success = xmpp.pmQueriesService.createPM(user_from.getUid(), uid_to, msg.getBody()); } if (success) { Message m = new Message(); - m.from = Jid.of("juick@juick.com"); - m.to = Jid.of(Integer.toString(uid_to), "push.juick.com", null); - JuickMessage jmsg = new JuickMessage(); + m.setFrom(Jid.of("juick@juick.com")); + m.setTo(Jid.of(Integer.toString(uid_to), "push.juick.com", null)); + com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(user_from); - jmsg.setText(msg.body); - m.childs.add(jmsg); - xmpp.getRouter().sendStanza(m.toString()); + jmsg.setText(msg.getBody()); + m.addExtension(jmsg); + xmpp.getRouter().sendStanza(m); - m.to = Jid.of(Integer.toString(uid_to), "ws.juick.com", null); - xmpp.getRouter().sendStanza(m.toString()); + m.setTo(Jid.of(Integer.toString(uid_to), "ws.juick.com", null)); + xmpp.getRouter().sendStanza(m); List jids; boolean inroster = false; jids = xmpp.userService.getJIDsbyUID(uid_to); for (String jid : jids) { Message mm = new Message(); - mm.to = Jid.of(jid); - mm.type = Message.Type.chat; + mm.setTo(Jid.of(jid)); + mm.setType(Message.Type.CHAT); inroster = xmpp.pmQueriesService.havePMinRoster(user_from.getUid(), jid); if (inroster) { - mm.from = Jid.of(jmsg.getUser().getName(), "juick.com", "Juick"); - mm.body = msg.body; + mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick")); + mm.setBody(msg.getBody()); } else { - mm.from = Jid.of("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + jmsg.getUser().getName() + ":\n" + msg.body; + mm.setFrom(Jid.of("juick", "juick.com", "Juick")); + mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + msg.getBody()); } - xmpp.sendOut(mm); + xmpp.sendOut(ClientMessage.from(mm)); } } else { - Message reply = new Message(msg.to, msg.from, Message.Type.error); - reply.id = msg.id; - reply.addChild(new XMPPError(XMPPError.Type.cancel, "not-allowed")); - xmpp.sendOut(reply); + Message reply = new Message(); + reply.setFrom(msg.getTo()); + reply.setTo(msg.getFrom()); + reply.setType(Message.Type.ERROR); + reply.setId(msg.getId()); + reply.addExtension(new StanzaError(StanzaError.Type.CANCEL, Condition.NOT_ALLOWED)); + xmpp.sendOut(ClientMessage.from(reply)); } return false; @@ -224,7 +248,7 @@ public class JuickBot implements StanzaListener { private static Pattern regexPM = Pattern.compile("^\\@(\\S+)\\s+([\\s\\S]+)$"); public boolean incomingMessageJuick(User user_from, Message msg) { - String command = msg.body.trim(); + String command = msg.getBody().trim(); int commandlen = command.length(); // COMPATIBILITY @@ -261,25 +285,35 @@ public class JuickBot implements StanzaListener { } private void commandPing(Message m) { - Presence p = new Presence(xmpp.getJid(), m.from); - p.priority = 10; - xmpp.sendOut(p); - - Message reply = new Message(xmpp.getJid(), m.from, Message.Type.chat); - reply.body = "PONG"; - xmpp.sendOut(reply); + Presence p = new Presence(m.getFrom()); + p.setFrom(xmpp.getJid()); + p.setPriority((byte) 10); + xmpp.sendOut(ClientPresence.from(p)); + + Message reply = new Message(); + reply.setFrom(xmpp.getJid()); + reply.setTo(m.getFrom()); + reply.setType(Message.Type.CHAT); + reply.setBody("PONG"); + xmpp.sendOut(ClientMessage.from(reply)); } private void commandHelp(Message m) { - Message reply = new Message(xmpp.getJid(), m.from, Message.Type.chat); - reply.body = HELPTEXT; - xmpp.sendOut(reply); + Message reply = new Message(); + reply.setFrom(xmpp.getJid()); + reply.setTo(m.getFrom()); + reply.setType(Message.Type.CHAT); + reply.setBody(HELPTEXT); + xmpp.sendOut(ClientMessage.from(reply)); } 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); + Message reply = new Message(); + reply.setFrom(xmpp.getJid()); + reply.setTo(m.getFrom()); + reply.setType(Message.Type.CHAT); + reply.setBody("http://juick.com/login?" + xmpp.userService.getHashByUID(user_from.getUid())); + xmpp.sendOut(ClientMessage.from(reply)); } private void commandPM(Message m, User user_from, String user_to, String body) { @@ -312,42 +346,44 @@ public class JuickBot implements StanzaListener { if (ret == 200) { Message msg = new Message(); - msg.from = Jid.of("juick", "juick.com", null); - msg.to = Jid.of(Integer.toString(uid_to), "push.juick.com", null); - JuickMessage jmsg = new JuickMessage(); + msg.setFrom(Jid.of("juick", "juick.com", null)); + msg.setTo(Jid.of(Integer.toString(uid_to), "push.juick.com", null)); + com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(user_from); jmsg.setText(body); - msg.childs.add(jmsg); - xmpp.getRouter().sendStanza(msg.toString()); + msg.addExtension(jmsg); + xmpp.getRouter().sendStanza(msg); - msg.to = Jid.of(Integer.toString(uid_to), "ws.juick.com", null); - xmpp.getRouter().sendStanza(msg.toString()); + msg.setTo(Jid.of(Integer.toString(uid_to), "ws.juick.com", null)); + xmpp.getRouter().sendStanza(msg); for (String jid : jids_to) { Message mm = new Message(); - mm.to = Jid.of(jid); - mm.type = Message.Type.chat; + mm.setTo(Jid.of(jid)); + mm.setType(Message.Type.CHAT); haveInRoster = xmpp.pmQueriesService.havePMinRoster(user_from.getUid(), jid); if (haveInRoster) { - mm.from = Jid.of(user_from.getName(), "juick.com", "Juick"); - mm.body = body; + mm.setFrom(Jid.of(user_from.getName(), "juick.com", "Juick")); + mm.setBody(body); } else { - mm.from = Jid.of("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + user_from.getName() + ":\n" + body; + mm.setFrom(Jid.of("juick", "juick.com", "Juick")); + mm.setBody("Private message from @" + user_from.getName() + ":\n" + body); } - xmpp.sendOut(mm); + xmpp.sendOut(ClientMessage.from(mm)); } } - Message reply = new Message(m.to, m.from); + Message reply = new Message(); + reply.setFrom(m.getTo()); + reply.setTo(m.getFrom()); if (ret == 200) { - reply.type = m.type; - reply.body = "Private message sent"; + reply.setType(m.getType()); + reply.setBody("Private message sent"); } else { - reply.type = Message.Type.error; - reply.body = "Error " + ret; + reply.setType(Message.Type.ERROR); + reply.setBody("Error " + ret); } - xmpp.sendOut(reply); + xmpp.sendOut(ClientMessage.from(reply)); } private void commandBLShow(Message m, User user_from) { @@ -373,22 +409,25 @@ public class JuickBot implements StanzaListener { txt = "You don't have any users or tags in your blacklist."; } - Message reply = new Message(xmpp.getJid(), m.from, Message.Type.chat); - reply.body = txt; - xmpp.sendOut(reply); + Message reply = new Message(); + reply.setFrom(xmpp.getJid()); + reply.setTo(m.getFrom()); + reply.setType(Message.Type.CHAT); + reply.setBody(txt); + xmpp.sendOut(ClientMessage.from(reply)); } @Override - public void stanzaReceived(String name, Stanza xmlValue) { + public void stanzaReceived(Stanza xmlValue) { if (xmlValue instanceof Presence) { Presence p = (Presence) xmlValue; - if (p.type == null || !p.type.equals(Presence.Type.error)) { + if (p.getType() == null || !p.getType().equals(Presence.Type.ERROR)) { incomingPresence(p); } } else if (xmlValue instanceof Message) { Message msg = (Message) xmlValue; if (!incomingMessage(msg)) { - xmpp.getRouter().sendStanza(msg.toString()); + xmpp.getRouter().sendStanza(msg); } } } -- cgit v1.2.3