From 9ee13bf582666cb04d06ed6a417a519535a2a49f Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sun, 20 Jul 2025 14:46:58 -0400 Subject: [PATCH] Send service messages to connected full JIDs rather than one bare JID wherever possible --- telegram/handlers.go | 4 ++-- telegram/loginwizard.go | 4 +++- telegram/utils.go | 16 ++++++++++------ xmpp/handlers.go | 6 ++++-- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/telegram/handlers.go b/telegram/handlers.go index 15c1bd5..fe9da04 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -276,7 +276,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { if isMUC { _, jids = c.getMUCJoinedJIDs(update.ChatId, nil, true) } else { - jids = c.getCarbonFullJids(true, ignoredResource) + jids = c.GetCarbonFullJids(true, ignoredResource, true) } if len(jids) == 0 { log.Info("The only resource is ignored, aborting") @@ -419,7 +419,7 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { } } else { fromJid = gateway.CHATNODE(update.ChatId) - jids = c.getCarbonFullJids(true, "") + jids = c.GetCarbonFullJids(true, "", false) for _, jid := range jids { gateway.SendTextMessage(jid, fromJid, text, c.xmpp, isGroupchat) } diff --git a/telegram/loginwizard.go b/telegram/loginwizard.go index 3062f8c..695507e 100644 --- a/telegram/loginwizard.go +++ b/telegram/loginwizard.go @@ -103,7 +103,9 @@ func (c *Client) wizardStageOrPrompt(stage LoginStage, message string) { if c.loginWizard == nil { c.locks.loginWizardWriteLock.Unlock() if message != "" { - gateway.SendServiceMessage(c.jid, message, c.xmpp) + for _, jid := range c.GetCarbonFullJids(true, "", false) { + gateway.SendServiceMessage(jid, message, c.xmpp) + } } } else { if !c.loginWizard.chanBusy { diff --git a/telegram/utils.go b/telegram/utils.go index 8567ccc..525bcf9 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -1743,8 +1743,8 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { addMembers, _ := message.Content.(*client.MessageChatAddMembers) for _, memberId := range addMembers.MemberUserIds { if c.me != nil && c.me.Id == memberId { - for resource := range c.resourcesRange() { - gateway.InviteToMUC(chatId, c.jid+"/"+resource, c.xmpp) + for _, jid := range c.GetCarbonFullJids(true, "", false) { + gateway.InviteToMUC(chatId, jid, c.xmpp) } } c.mucOccupantRolePresence(chatId, memberId, ChatMemberStatusUnmuted, c.GetMUCNickname(memberId)) @@ -1753,8 +1753,8 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { deleteMember, _ := message.Content.(*client.MessageChatDeleteMember) c.mucOccupantRolePresence(chatId, deleteMember.UserId, ChatMemberStatusKicked, c.GetMUCNickname(deleteMember.UserId)) case client.TypeMessageBasicGroupChatCreate, client.TypeMessageSupergroupChatCreate: - for resource := range c.resourcesRange() { - gateway.InviteToMUC(chatId, c.jid+"/"+resource, c.xmpp) + for _, jid := range c.GetCarbonFullJids(true, "", false) { + gateway.InviteToMUC(chatId, jid, c.xmpp) } } @@ -1795,7 +1795,7 @@ func (c *Client) SendMessageToGateway(chatId int64, message *client.Message, id var originalFrom string if len(groupChatTos) == 0 { isCarbon = c.isCarbonsEnabled() && message.IsOutgoing - jids = c.getCarbonFullJids(isCarbon, "") + jids = c.GetCarbonFullJids(isCarbon, "", true) } else { isGroupchat = true jids = groupChatTos @@ -2555,7 +2555,8 @@ func (c *Client) getFromOutbox(xmppId string) string { return resource } -func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []string { +// GetCarbonFullJids builds a set of full jids or of one bare jid for outgoing stanzas +func (c *Client) GetCarbonFullJids(isOutgoing bool, ignoredResource string, forceFull bool) []string { var jids []string if isOutgoing { for resource := range c.resourcesRange() { @@ -2563,6 +2564,9 @@ func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []st jids = append(jids, c.jid+"/"+resource) } } + if len(jids) == 0 && !forceFull { + jids = []string{c.jid} + } } else { jids = []string{c.jid} } diff --git a/xmpp/handlers.go b/xmpp/handlers.go index b94dfd6..647f0b1 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -402,9 +402,11 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) { if msg.XMLName.Space == "jabber:component:accept" && msg.Error.Code == 401 { suffix := "@" + msg.From - for bare := range sessions { + for bare, session := range sessions { if strings.HasSuffix(bare, suffix) { - gateway.SendServiceMessage(bare, "Your server \""+msg.From+"\" does not allow to send carbons", component) + for _, jid := range session.GetCarbonFullJids(true, "", false) { + gateway.SendServiceMessage(jid, "Your server \""+msg.From+"\" does not allow to send carbons", component) + } } } }