From d0ca065097636c2e3313582849b0a88ad9b1bf16 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 6 Feb 2017 17:07:41 +0300 Subject: juick-xmpp: broadcast bot presence --- .../java/com/juick/components/s2s/JuickBot.java | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'juick-xmpp') 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); + } } -- cgit v1.2.3