[xmpp] Add action for sending a group message (#17938)

* implement group message sending

Signed-off-by: akallabeth <akallabeth@posteo.net>
This commit is contained in:
akallabeth 2024-12-31 20:36:07 +01:00 committed by GitHub
parent 05382279a9
commit d4cbc5f839
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 72 additions and 3 deletions

View File

@ -65,6 +65,23 @@ public class XMPPActions implements ThingActions {
connection.sendMessage(to, text);
}
@RuleAction(label = "publish a group message", description = "Publish a group message using XMPP.")
public void publishGroupXMPP(@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
@ActionInput(name = "text", label = "Text", description = "Message text") @Nullable String text) {
XMPPClientHandler clientHandler = handler;
if (clientHandler == null) {
logger.warn("XMPP ThingHandler is null");
return;
}
XMPPClient connection = clientHandler.getXMPPClient();
if (to == null || text == null) {
logger.warn("Skipping XMPP messaging to {} value {}", to, text);
return;
}
connection.sendGroupMessage(to, text);
}
@RuleAction(label = "publish an image by HTTP", description = "Publish an image by HTTP using XMPP.")
public void publishXMPPImageByHTTP(
@ActionInput(name = "to", label = "To", description = "Send to") @Nullable String to,
@ -87,6 +104,10 @@ public class XMPPActions implements ThingActions {
((XMPPActions) actions).publishXMPP(to, text);
}
public static void publishGroupXMPP(ThingActions actions, @Nullable String to, @Nullable String text) {
((XMPPActions) actions).publishGroupXMPP(to, text);
}
public static void publishXMPPImageByHTTP(ThingActions actions, @Nullable String to, @Nullable String filename) {
((XMPPActions) actions).publishXMPPImageByHTTP(to, filename);
}

View File

@ -37,8 +37,11 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.MultiUserChatManager;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException;
import org.openhab.binding.xmppclient.internal.handler.XMPPClientMessageSubscriber;
import org.slf4j.Logger;
@ -55,9 +58,11 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
private final Logger logger = LoggerFactory.getLogger(XMPPClient.class);
private @Nullable AbstractXMPPConnection connection;
private @Nullable ChatManager chatManager;
private @Nullable MultiUserChatManager multiUserChatManager;
private @Nullable HttpFileUploadManager httpFileUploadManager;
private Set<XMPPClientMessageSubscriber> subscribers = new HashSet<>();
private final XMPPClientEventlistener eventListener;
private String nickname = "";
public XMPPClient(XMPPClientEventlistener eventListener) {
this.eventListener = eventListener;
@ -73,7 +78,7 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
subscribers.remove(channel);
}
public void connect(String host, Integer port, String login, String domain, String password,
public void connect(String host, Integer port, String login, String nick, String domain, String password,
SecurityMode securityMode) throws XMPPClientConfigException, XMPPClientException {
disconnect();
String serverHost = domain;
@ -81,6 +86,12 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
serverHost = host;
}
if (!nick.isBlank()) {
nickname = nick;
} else {
nickname = login;
}
XMPPTCPConnectionConfiguration config;
try {
config = XMPPTCPConnectionConfiguration.builder() //
@ -115,6 +126,10 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
ChatManager chatManager = ChatManager.getInstanceFor(connection);
chatManager.addIncomingListener(this);
this.chatManager = chatManager;
MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);
multiUserChatManager.setAutoJoinOnReconnect(true);
this.multiUserChatManager = multiUserChatManager;
httpFileUploadManager = HttpFileUploadManager.getInstanceFor(connection);
}
@ -145,6 +160,33 @@ public class XMPPClient implements IncomingChatMessageListener, ConnectionListen
}
}
public void sendGroupMessage(String to, String message) {
if (connection == null) {
eventListener.onErrorEvent("XMPP connection is null");
return;
}
MultiUserChatManager chatManager = this.multiUserChatManager;
if (chatManager == null) {
eventListener.onErrorEvent("XMPP chatManager is null");
return;
}
try {
EntityBareJid jid = JidCreate.entityBareFrom(to);
MultiUserChat chat = multiUserChatManager.getMultiUserChat(jid);
if (!chat.isJoined()) {
chat.join(Resourcepart.from(nickname));
}
chat.sendMessage(message);
} catch (XmppStringprepException | SmackException.NotConnectedException | InterruptedException e) {
logger.warn("XMPP message sending error", e);
} catch (SmackException | XMPPException e) {
logger.warn("XMPP message group join error", e);
}
}
public void sendImageByHTTP(String to, String filename) {
if (connection == null) {
logger.warn("XMPP connection is null");

View File

@ -26,6 +26,7 @@ public class XMPPClientConfiguration {
public @Nullable String host;
public Integer port = 5222;
public String username = "";
public String nickname = "";
public String password = "";
public String domain = "";
public String securityMode = SecurityMode.required.toString();

View File

@ -95,8 +95,8 @@ public class XMPPClientHandler extends BaseBridgeHandler implements XMPPClientEv
}
try {
xmppClient.connect(Objects.requireNonNullElse(config.host, ""), config.port, config.username, config.domain,
config.password, SecurityMode.valueOf(config.securityMode));
xmppClient.connect(Objects.requireNonNullElse(config.host, ""), config.port, config.username,
config.nickname, config.domain, config.password, SecurityMode.valueOf(config.securityMode));
updateStatus(ThingStatus.ONLINE);
} catch (XMPPClientConfigException e) {
logger.debug("XMPP connection error", e);

View File

@ -12,6 +12,11 @@
<label>Username</label>
<description>The XMPP Username (the left side of JID, e.g. user for JID user@example.com)</description>
</parameter>
<parameter name="nickname" type="text" required="false">
<label>Nickname</label>
<description>The XMPP Nickname to use in multi user chats. (Defaults to Username)</description>
<advanced>true</advanced>
</parameter>
<parameter name="domain" type="text" required="true">
<label>Domain</label>
<description>The XMPP Domain (the right side of JID, e.g. example.com for JID user@example.com)</description>