aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-02-06 17:07:41 +0300
committerGravatar Vitaly Takmazov2017-02-06 17:41:03 +0300
commitd0ca065097636c2e3313582849b0a88ad9b1bf16 (patch)
tree12b9c1ec10d9a89ae4799dc8cb0d17842d53f41d /juick-xmpp
parent2befd7c98f9932d1aab6d72477490d0224000d62 (diff)
juick-xmpp: broadcast bot presence
Diffstat (limited to 'juick-xmpp')
-rw-r--r--juick-xmpp/src/main/java/com/juick/components/s2s/JuickBot.java28
1 files changed, 27 insertions, 1 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 cf791ada..9a970855 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
@@ -3,6 +3,8 @@ package com.juick.components.s2s;
import com.juick.User;
import com.juick.components.XMPPServer;
import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Message;
import rocks.xmpp.core.stanza.model.Presence;
@@ -21,7 +23,9 @@ import java.util.regex.Pattern;
*
* @author ugnich
*/
-public class JuickBot implements StanzaListener {
+public class JuickBot implements StanzaListener, AutoCloseable {
+
+ private static final Logger logger = LoggerFactory.getLogger(JuickBot.class);
XMPPServer xmpp;
@@ -29,6 +33,7 @@ public class JuickBot implements StanzaListener {
public JuickBot(XMPPServer xmpp) {
this.xmpp = xmpp;
xmpp.addStanzaListener(this);
+ broadcastPresence(null);
}
private static final String HELPTEXT =
@@ -431,4 +436,25 @@ public class JuickBot implements StanzaListener {
}
}
}
+
+ void broadcastPresence(Presence.Type type) {
+ Presence presence = new Presence();
+ presence.setFrom(xmpp.getJid());
+ if (type != null) {
+ presence.setType(type);
+ }
+ xmpp.userService.getAllJIDs().forEach(j -> {
+ try {
+ presence.setTo(Jid.of(j));
+ xmpp.sendOut(ClientPresence.from(presence));
+ } catch (IllegalArgumentException ex) {
+ logger.warn("Invalid jid: {}", j, ex);
+ }
+ });
+ }
+
+ @Override
+ public void close() throws Exception {
+ broadcastPresence(Presence.Type.UNAVAILABLE);
+ }
}