diff options
Diffstat (limited to 'juick-xmpp-bot/src/main/java/com/juick')
-rw-r--r-- | juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java b/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java index 9f5d076a..234d04f8 100644 --- a/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java +++ b/juick-xmpp-bot/src/main/java/com/juick/components/XMPPBot.java @@ -1,10 +1,11 @@ package com.juick.components; -import com.fasterxml.jackson.core.JsonProcessingException; import com.juick.User; import com.juick.server.helpers.UserInfo; import com.juick.server.protocol.JuickProtocol; +import com.juick.server.protocol.ProtocolListener; import com.juick.server.protocol.ProtocolReply; +import com.juick.service.PMQueriesService; import com.juick.service.UserService; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -24,24 +25,30 @@ import javax.inject.Inject; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URL; +import java.util.List; /** * Created by vt on 12/11/2016. */ -public class XMPPBot implements AutoCloseable { +public class XMPPBot implements AutoCloseable, ProtocolListener { private static final Logger logger = LoggerFactory.getLogger(XMPPBot.class); @Inject UserService userService; @Inject + PMQueriesService pmQueriesService; + @Inject JuickProtocol juickProtocol; + Jid juickJid; + private ExternalComponent component; public XMPPBot(Environment env) { component = ExternalComponent.create(env.getProperty("component_name", "juick.com"), env.getProperty("component_password", "secret"), env.getProperty("component_host", "localhost"), NumberUtils.toInt(env.getProperty("component_port", "5347"), 5347)); - Jid juickJid = Jid.of(env.getProperty("xmppbot_jid", "juick@juick.com/Juick")); + juickJid = Jid.of(env.getProperty("xmppbot_jid", "juick@juick.com/Juick")); + juickProtocol.setListener(this); try { SoftwareVersionManager softwareVersionManager = component.getManager(SoftwareVersionManager.class); softwareVersionManager.setSoftwareVersion(new SoftwareVersion("Juick", "git", System.getProperty("os.name", "generic"))); @@ -79,7 +86,7 @@ public class XMPPBot implements AutoCloseable { replyMessage.setBody(reply.getResult()); replyMessage.setFrom(juickJid); component.send(replyMessage); - } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | JsonProcessingException ex) { + } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException ex) { logger.warn("unhandled error", ex); } } @@ -97,4 +104,37 @@ public class XMPPBot implements AutoCloseable { logger.info("ExternalComponent on xmpp-bot destroyed"); } + + @Override + public void privateMessage(User from, User to, String body) { + List<String> toJids = userService.getJIDsbyUID(to.getUid()); + toJids.forEach(jid -> { + Message mm = new Message(); + mm.setTo(Jid.of(jid)); + mm.setType(Message.Type.CHAT); + boolean haveInRoster = pmQueriesService.havePMinRoster(from.getUid(), jid); + if (haveInRoster) { + mm.setFrom(Jid.of(from.getName(), juickJid.getDomain(), "Juick")); + mm.setBody(body); + } else { + mm.setFrom(Jid.of("juick", juickJid.getDomain(), "Juick")); + mm.setBody("Private message from @" + from.getName() + ":\n" + body); + } + component.send(mm); + }); + } + + @Override + public void userSubscribed(User from, User to) { + String notification = String.format("%s subscribed to your blog", from.getName()); + List<String> toJids = userService.getJIDsbyUID(to.getUid()); + toJids.forEach(jid -> { + Message mm = new Message(); + mm.setTo(Jid.of(jid)); + mm.setType(Message.Type.CHAT); + mm.setFrom(juickJid); + mm.setBody(notification); + component.send(mm); + }); + } } |