diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-03-19 00:43:11 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2023-03-19 00:43:11 +0300 |
commit | 42ed16bf9e9d72bf226045f1f291b9d58e2a6200 (patch) | |
tree | e01b63ea0f49afdab1e8463cd1b10138f5db7816 /telegram/utils.go | |
parent | 90807b2d9e0565629a913d3b28b09c5fc9d746e6 (diff) |
Simulate carbons
Diffstat (limited to 'telegram/utils.go')
-rw-r--r-- | telegram/utils.go | 74 |
1 files changed, 71 insertions, 3 deletions
diff --git a/telegram/utils.go b/telegram/utils.go index 68dd524..99492e1 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -109,6 +109,33 @@ func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *cli return chat, user, nil } +// IsPM checks if a chat is PM +func (c *Client) IsPM(id int64) (bool, error) { + if !c.Online() || id == 0 { + return false, errOffline + } + + var err error + + chat, ok := c.cache.GetChat(id) + if !ok { + chat, err = c.client.GetChat(&client.GetChatRequest{ + ChatId: id, + }) + if err != nil { + return false, err + } + + c.cache.SetChat(id, chat) + } + + chatType := chat.Type.ChatTypeType() + if chatType == client.TypeChatTypePrivate || chatType == client.TypeChatTypeSecret { + return true, nil + } + return false, nil +} + func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (string, string, string) { var show, textStatus, presenceType string @@ -782,6 +809,7 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File { // ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { var text, oob, auxText string + var err error reply, replyMsg := c.getMessageReply(message) @@ -847,12 +875,33 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) { MessageIds: []int64{message.Id}, ForceRead: true, }) + // forward message to XMPP sId := strconv.FormatInt(message.Id, 10) sChatId := strconv.FormatInt(chatId, 10) - gateway.SendMessageWithOOB(c.jid, sChatId, text, sId, c.xmpp, reply, oob) - if auxText != "" { - gateway.SendMessage(c.jid, sChatId, auxText, sId, c.xmpp, reply) + + var jids []string + var isPM bool + if gateway.MessageOutgoingPermission && c.Session.Carbons { + isPM, err = c.IsPM(chatId) + if err != nil { + log.Errorf("Could not determine if chat is PM: %v", err) + } + } + isOutgoing := isPM && message.IsOutgoing + if isOutgoing { + for resource := range c.resourcesRange() { + jids = append(jids, c.jid + "/" + resource) + } + } else { + jids = []string{c.jid} + } + + for _, jid := range jids { + gateway.SendMessageWithOOB(jid, sChatId, text, sId, c.xmpp, reply, oob, isOutgoing) + if auxText != "" { + gateway.SendMessage(jid, sChatId, auxText, sId, c.xmpp, reply, isOutgoing) + } } } @@ -1024,6 +1073,25 @@ func (c *Client) deleteResource(resource string) { } } +func (c *Client) resourcesRange() chan string { + c.locks.resourcesLock.Lock() + + resourceChan := make(chan string, 1) + + go func() { + defer func() { + c.locks.resourcesLock.Unlock() + close(resourceChan) + }() + + for resource := range c.resources { + resourceChan <- resource + } + }() + + return resourceChan +} + // resend statuses to (to another resource, for example) func (c *Client) roster(resource string) { if _, ok := c.resources[resource]; ok { |