git » sdk » commit 68860e6

Outgoing invites are not invites

author Stephen Paul Weber
2026-11-10 03:44:05 UTC
committer Stephen Paul Weber
2026-11-10 03:53:35 UTC
parent a87561dda8f4ff34da82f5feba3e5ed85e54c056

Outgoing invites are not invites

And make sure we use bare jid for mediated ones coming in, in case there
is a fulljid in there.

borogove/Message.hx +3 -3
test/TestChatMessage.hx +39 -0

diff --git a/borogove/Message.hx b/borogove/Message.hx
index cda1f49..aa5b9ca 100644
--- a/borogove/Message.hx
+++ b/borogove/Message.hx
@@ -147,7 +147,7 @@ class Message {
 		msg.status = msg.direction == MessageReceived ? MessageDeliveredToDevice : MessageDeliveredToServer; // Delivered to us, a device
 
 		final mucDirectInvite = stanza.getChild("x", "jabber:x:conference");
-		if (mucDirectInvite != null) {
+		if (msg.direction == MessageReceived && mucDirectInvite != null) {
 			final mucJid = mucDirectInvite.attr.get("jid");
 			if (mucJid != null) {
 				return new Message(mucJid, from, mucDirectInvite.attr.get("thread"), MucInviteStanza(msg.serverId, msg.serverIdBy, mucDirectInvite.attr.get("reason"), mucDirectInvite.attr.get("password")), encryptionInfo);
@@ -156,12 +156,12 @@ class Message {
 
 		final mucUser = stanza.getChild("x", "http://jabber.org/protocol/muc#user");
 		final mucInvite = mucUser?.getChild("invite");
-		if (mucInvite != null) {
+		if (msg.direction == MessageReceived && mucInvite != null) {
 			final threadId = mucInvite.getChild("continue")?.attr?.get("thread");
 			final reason = mucInvite.getChildText("reason");
 			final password = mucInvite.getChildText("password");
 			// NOTE: this senderId is unverified and unverifiable
-			return new Message(from, mucInvite.attr.get("from") ?? from, threadId, MucInviteStanza(msg.serverId, msg.serverIdBy, reason, password), encryptionInfo);
+			return new Message(JID.parse(from).asBare().asString(), mucInvite.attr.get("from") ?? from, threadId, MucInviteStanza(msg.serverId, msg.serverIdBy, reason, password), encryptionInfo);
 		}
 
 		final recipients: Map<String, Bool> = [];
diff --git a/test/TestChatMessage.hx b/test/TestChatMessage.hx
index 321809d..36f864d 100644
--- a/test/TestChatMessage.hx
+++ b/test/TestChatMessage.hx
@@ -244,4 +244,43 @@ class TestChatMessage extends utest.Test {
 		// Reaction should skip the already-quoted line and only quote the first non-quoted line
 		Assert.equals("> actual text…\n\n❤️", body);
 	}
+
+	public function testMucInviteChatIdIsBare() {
+		final stanza = new Stanza("message");
+		stanza.attr.set("id", "invite-1");
+		stanza.attr.set("from", "alice@example.com/phone");
+		stanza.attr.set("to", "bob@example.com");
+		stanza.tag("x", { xmlns: "http://jabber.org/protocol/muc#user" })
+			.tag("invite", { from: "alice@example.com/phone" })
+			.textTag("reason", "join us")
+			.up()
+			.up();
+
+		final msg = Message.fromStanza(stanza, JID.parse("bob@example.com"));
+		Assert.equals("alice@example.com", msg.chatId);
+		switch (msg.parsed) {
+			case MucInviteStanza(_, _, reason, _): Assert.equals("join us", reason);
+			default: Assert.fail("Expected MucInviteStanza");
+		}
+	}
+
+	public function testSentCarbonInviteIsNotAnInvite() {
+		final inner = new Stanza("message", { from: "bob@example.com/phone", to: "alice@example.com", type: "chat" });
+		inner.tag("x", { xmlns: "http://jabber.org/protocol/muc#user" })
+			.tag("invite", { from: "bob@example.com/phone" })
+			.up()
+			.up();
+
+		final carbon = new Stanza("message", { from: "bob@example.com", to: "bob@example.com/phone" });
+		carbon.tag("sent", { xmlns: "urn:xmpp:carbons:2" })
+			.tag("forwarded", { xmlns: "urn:xmpp:forward:0" })
+			.addChild(inner);
+
+		final msg = Message.fromStanza(carbon, JID.parse("bob@example.com"));
+		Assert.equals("alice@example.com", msg.chatId);
+		switch (msg.parsed) {
+			case MucInviteStanza(_, _, _, _): Assert.fail("Sent carbon invite should not be processed as an invite");
+			default:
+		}
+	}
 }