diff --git a/telegram/client.go b/telegram/client.go index 52fb326..e9e1954 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -80,6 +80,41 @@ func (b *barrier) IsPending() bool { return b.open } +// NewId stores message ids and timestamps of their additions so old ones can be truncated to save memory +type newId struct { + Id int64 + Ts int64 + lock sync.Mutex + ownLock sync.Mutex + locked bool + fired bool +} + +func newNewId() *newId { + return &newId{Ts: time.Now().Unix()} +} + +func (i *newId) Lock() { + i.ownLock.Lock() + if i.fired { + i.ownLock.Unlock() + return + } + i.locked = true + i.ownLock.Unlock() + i.lock.Lock() +} + +func (i *newId) Unlock() { + i.ownLock.Lock() + if i.locked { + i.lock.Unlock() + i.locked = false + i.fired = true + } + i.ownLock.Unlock() +} + // Client stores the metadata for lazily invoked TDlib instance type Client struct { client *client.Client @@ -121,6 +156,9 @@ type Client struct { avatarHashes map[int64]*HashedAvatar avatarHashesLock sync.Mutex + MessageIdChanges map[int64]map[int64]*newId + MessageIdChangesLock sync.Mutex + locks clientLocks SendMessageLock sync.Mutex } @@ -211,6 +249,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component lastMsgIds: make(map[int64]string), XmppClientFeatures: make(map[string]*[]string), avatarHashes: make(map[int64]*HashedAvatar), + MessageIdChanges: make(map[int64]map[int64]*newId), locks: clientLocks{ chatMessageLocks: make(map[int64]*sync.Mutex), }, diff --git a/telegram/commands.go b/telegram/commands.go index 9878a07..5c905dc 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -51,26 +51,26 @@ var permissionsMember = client.ChatPermissions{ var permissionsReadonly = client.ChatPermissions{} var transportCommands = map[string]command{ - "help": command{0, []string{}, "help", false, nil}, - "login": command{1, []string{"phone"}, "sign in", false, nil}, - "logout": command{0, []string{}, "sign out", true, nil}, - "cleanup": command{0, []string{}, "unsubscribe from all known chats", false, nil}, - "cancelauth": command{0, []string{}, "quit the signin wizard", false, nil}, - "code": command{1, []string{"xxxxx"}, "check one-time code", false, nil}, - "password": command{1, []string{"********"}, "check 2fa password", false, nil}, - "setusername": command{0, []string{"@username"}, "update @username", true, nil}, - "setname": command{1, []string{"first", "last"}, "update name", true, nil}, - "setbio": command{0, []string{"Lorem ipsum"}, "update about", true, nil}, - "setpassword": command{0, []string{"old", "new"}, "set or remove password", true, nil}, - "config": command{0, []string{"param", "value"}, "view or update configuration options", false, nil}, - "report": command{2, []string{"chat", "comment"}, "report a chat by id or @username", true, nil}, - "add": command{1, []string{"@username"}, "add @username to your chat list", true, nil}, - "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil}, - "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil}, - "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil}, - "preset": command{1, []string{"modern|classic|pass"}, "apply a config preset", false, nil}, - "pass": command{0, []string{}, "proceed to next login stage", false, nil}, - "finish": command{0, []string{}, "skip post-login configuration", false, nil}, + "help": command{0, []string{}, "help", false, nil, false}, + "login": command{1, []string{"phone"}, "sign in", false, nil, false}, + "logout": command{0, []string{}, "sign out", true, nil, true}, + "cleanup": command{0, []string{}, "unsubscribe from all known chats", false, nil, false}, + "cancelauth": command{0, []string{}, "quit the signin wizard", false, nil, false}, + "code": command{1, []string{"xxxxx"}, "check one-time code", false, nil, false}, + "password": command{1, []string{"********"}, "check 2fa password", false, nil, false}, + "setusername": command{0, []string{"@username"}, "update @username", true, nil, true}, + "setname": command{1, []string{"first", "last"}, "update name", true, nil, false}, + "setbio": command{0, []string{"Lorem ipsum"}, "update about", true, nil, true}, + "setpassword": command{0, []string{"old", "new"}, "set or remove password", true, nil, true}, + "config": command{0, []string{"param", "value"}, "view or update configuration options", false, nil, false}, + "report": command{2, []string{"chat", "comment"}, "report a chat by id or @username", true, nil, true}, + "add": command{1, []string{"@username"}, "add @username to your chat list", true, nil, true}, + "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil, true}, + "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil, true}, + "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil, true}, + "preset": command{1, []string{"modern|classic|pass"}, "apply a config preset", false, nil, false}, + "pass": command{0, []string{}, "proceed to next login stage", false, nil, false}, + "finish": command{0, []string{}, "skip post-login configuration", false, nil, false}, } var notForGroups = []ChatType{ChatTypeBasicGroup, ChatTypeSupergroup, ChatTypeChannel} @@ -79,38 +79,38 @@ var notForPMAndBasic = []ChatType{ChatTypePrivate, ChatTypeSecret, ChatTypeBasic var onlyForSecret = []ChatType{ChatTypePrivate, ChatTypeBasicGroup, ChatTypeSupergroup, ChatTypeChannel} var chatCommands = map[string]command{ - "help": command{0, []string{}, "help", false, nil}, - "d": command{0, []string{"n"}, "delete your last message(s)", true, nil}, - "s": command{1, []string{"edited message"}, "edit your last message", true, nil}, - "silent": command{1, []string{"message"}, "send a message without sound", true, nil}, - "schedule": command{2, []string{"{online | 2006-01-02T15:04:05 | 15:04:05}", "message"}, "schedules a message either to timestamp or to whenever the user goes online", true, nil}, - "raw": command{1, []string{"message"}, "send a raw message not interpeted as a transport command (e.g. a bot command)", true, nil}, - "forward": command{2, []string{"message_id", "target_chat"}, "forwards a message", true, nil}, - "vcard": command{0, []string{}, "print vCard as text", true, nil}, - "add": command{1, []string{"@username"}, "add @username to your chat list", true, nil}, - "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil}, - "group": command{1, []string{"title"}, "create groupchat «title» with current user", true, ¬ForGroups}, - "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil}, - "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil}, - "secret": command{0, []string{}, "create secretchat with current user", true, ¬ForGroups}, - "search": command{0, []string{"string", "[limit]"}, "search in current chat", true, nil}, - "history": command{0, []string{"limit"}, "get last [limit] messages from current chat", true, nil}, - "block": command{0, []string{}, "blacklist current user", true, ¬ForGroups}, - "unblock": command{0, []string{}, "unblacklist current user", true, ¬ForGroups}, - "invite": command{1, []string{"id or @username"}, "add user to current chat", true, ¬ForPM}, - "link": command{0, []string{}, "get invite link for current chat", true, ¬ForPM}, - "kick": command{1, []string{"id or @username"}, "remove user from current chat", true, ¬ForPM}, - "mute": command{0, []string{"id or @username", "hours"}, "mute the whole chat or a user in current chat", true, ¬ForPMAndBasic}, - "unmute": command{0, []string{"id or @username"}, "unmute the whole chat or a user in the current chat", true, ¬ForPMAndBasic}, - "ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever", true, ¬ForPM}, - "unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)", true, ¬ForPM}, - "promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat", true, ¬ForPM}, - "leave": command{0, []string{}, "leave current chat", true, ¬ForPM}, - "leave!": command{0, []string{}, "leave current chat (for owners)", true, ¬ForPM}, - "ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying", true, &onlyForSecret}, - "close": command{0, []string{}, "close current secret chat", true, &onlyForSecret}, - "delete": command{0, []string{}, "delete current chat from chat list", true, nil}, - "members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)", true, nil}, + "help": command{0, []string{}, "help", false, nil, false}, + "d": command{0, []string{"n"}, "delete your last message(s)", true, nil, true}, + "s": command{1, []string{"edited message"}, "edit your last message", true, nil, true}, + "silent": command{1, []string{"message"}, "send a message without sound", true, nil, true}, + "schedule": command{2, []string{"{online | 2006-01-02T15:04:05 | 15:04:05}", "message"}, "schedules a message either to timestamp or to whenever the user goes online", true, nil, true}, + "raw": command{1, []string{"message"}, "send a raw message not interpeted as a transport command (e.g. a bot command)", true, nil, true}, + "forward": command{2, []string{"message_id", "target_chat"}, "forwards a message", true, nil, true}, + "vcard": command{0, []string{}, "print vCard as text", true, nil, true}, + "add": command{1, []string{"@username"}, "add @username to your chat list", true, nil, true}, + "join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname", true, nil, true}, + "group": command{1, []string{"title"}, "create groupchat «title» with current user", true, ¬ForGroups, true}, + "supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»", true, nil, true}, + "channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»", true, nil, true}, + "secret": command{0, []string{}, "create secretchat with current user", true, ¬ForGroups, true}, + "search": command{0, []string{"string", "[limit]"}, "search in current chat", true, nil, true}, + "history": command{0, []string{"limit"}, "get last [limit] messages from current chat", true, nil, true}, + "block": command{0, []string{}, "blacklist current user", true, ¬ForGroups, true}, + "unblock": command{0, []string{}, "unblacklist current user", true, ¬ForGroups, true}, + "invite": command{1, []string{"id or @username"}, "add user to current chat", true, ¬ForPM, true}, + "link": command{0, []string{}, "get invite link for current chat", true, ¬ForPM, true}, + "kick": command{1, []string{"id or @username"}, "remove user from current chat", true, ¬ForPM, true}, + "mute": command{0, []string{"id or @username", "hours"}, "mute the whole chat or a user in current chat", true, ¬ForPMAndBasic, true}, + "unmute": command{0, []string{"id or @username"}, "unmute the whole chat or a user in the current chat", true, ¬ForPMAndBasic, true}, + "ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever", true, ¬ForPM, true}, + "unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)", true, ¬ForPM, true}, + "promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat", true, ¬ForPM, true}, + "leave": command{0, []string{}, "leave current chat", true, ¬ForPM, true}, + "leave!": command{0, []string{}, "leave current chat (for owners)", true, ¬ForPM, true}, + "ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying", true, &onlyForSecret, true}, + "close": command{0, []string{}, "close current secret chat", true, &onlyForSecret, true}, + "delete": command{0, []string{}, "delete current chat from chat list", true, nil, true}, + "members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)", true, nil, true}, } var transportConfigurationOptions = map[string]configurationOption{ @@ -133,6 +133,7 @@ type command struct { Description string LoginOnly bool NotFor *[]ChatType + OnlineOnly bool } type configurationOption struct { arguments string @@ -147,6 +148,15 @@ const ( CommandTypeChat ) +// OnlineFilter is a tri-state condition for commands selection +type OnlineFilter int + +const ( + OnlineFilterOnline OnlineFilter = iota + OnlineFilterNotOnline + OnlineFilterAny +) + // GetCommands exposes the set of commands func GetCommands(typ CommandType) map[string]command { var commandMap map[string]command @@ -169,14 +179,20 @@ func GetCommand(typ CommandType, cmd string) (command, bool) { } // SortedCommandKeys sorts a slice with command keys -func SortedCommandKeys(commandMap map[string]command) []string { +func SortedCommandKeys(commandMap map[string]command, onlineFilter OnlineFilter) []string { keys := make([]string, len(commandMap)) i := 0 for k := range commandMap { + command := commandMap[k] + if (onlineFilter == OnlineFilterOnline && !command.OnlineOnly) || (onlineFilter == OnlineFilterNotOnline && command.OnlineOnly) { + continue + } + keys[i] = k i++ } + keys = keys[:i] sort.Strings(keys) @@ -219,24 +235,32 @@ func IsCommandForChatType(cmd command, chatType ChatType) bool { return true } -func (c *Client) helpString(typ CommandType, chatId int64) string { - var str strings.Builder - - commandMap := GetCommands(typ) - chatType, _, chatTypeErr := c.GetChatType(chatId, true) - - str.WriteString("Available commands:\n") - for _, name := range SortedCommandKeys(commandMap) { +func commandsToHelpString(str *strings.Builder, chatType ChatType, onlineFilter OnlineFilter, commandMap map[string]command) { + for _, name := range SortedCommandKeys(commandMap, onlineFilter) { command := commandMap[name] - if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { + if !IsCommandForChatType(command, chatType) { continue } str.WriteString(CommandToHelpString(name, command)) str.WriteString("\n") } +} + +func (c *Client) helpString(typ CommandType, chatId int64) string { + var str strings.Builder + + commandMap := GetCommands(typ) + chatType, _, _ := c.GetChatType(chatId, true) + + str.WriteString("Available commands:\n") if typ == CommandTypeTransport { - str.WriteString("Configuration options\n") + commandsToHelpString(&str, chatType, OnlineFilterNotOnline, commandMap) + + str.WriteString("\nOnline-only commands:\n") + commandsToHelpString(&str, chatType, OnlineFilterOnline, commandMap) + + str.WriteString("\nConfiguration options\n") for _, name := range persistence.ConfigKeys { option := transportConfigurationOptions[name] str.WriteString(name) @@ -246,6 +270,8 @@ func (c *Client) helpString(typ CommandType, chatId int64) string { str.WriteString(option.description) str.WriteString("\n") } + } else if typ == CommandTypeChat { + commandsToHelpString(&str, chatType, OnlineFilterAny, commandMap) } str.WriteString("\nYou may use ! instead of / if it conflicts with internal commands of a client") @@ -310,6 +336,9 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin if len(args) < command.RequiredArgs { return notEnoughArguments, false } + if command.OnlineOnly && !c.Online() { + return notOnline, false + } switch cmd { case "login", "code", "password": @@ -348,6 +377,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin c.authorizer.Password <- args[0] } } + return "", true // sign out case "logout": if !c.Online() && !c.locks.loginFinish.IsPending() { @@ -376,10 +406,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin return "Cancelled", true // set @username case "setusername": - if !c.Online() { - return notOnline, false - } - var username string if len(args) > 0 { username = args[0] @@ -424,10 +450,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin } // set About case "setbio": - if !c.Online() { - return notOnline, false - } - _, err := c.client.SetBio(&client.SetBioRequest{ Bio: rawCmdArguments(cmdline, 0), }) @@ -436,10 +458,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin } // set password case "setpassword": - if !c.Online() { - return notOnline, false - } - var oldPassword string var newPassword string if len(args) > 0 { @@ -557,9 +575,16 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) (strin go c.promptMUC() case LoginStageMUC: c.wizardStageOrPrompt(LoginStageSuccess, "") + default: + return "Not applicable here", false } case "finish": - c.wizardStageOrPrompt(LoginStageSuccess, "") + switch c.loginStage { + case LoginStagePreset, LoginStageMUC: + c.wizardStageOrPrompt(LoginStageSuccess, "") + default: + return "Not applicable here", false + } } return "", true @@ -572,10 +597,6 @@ func (c *Client) promptMUC() { // ProcessChatCommand executes a command sent in a mapped chat // and returns a response, the status of command support and the execution success result func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, bool) { - if !c.Online() { - return notOnline, true, false - } - cmd, args := parseCommand(cmdline) command, ok := chatCommands[cmd] if !ok { @@ -584,6 +605,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, if len(args) < command.RequiredArgs { return notEnoughArguments, true, false } + if command.OnlineOnly && !c.Online() { + return notOnline, true, false + } + chatType, _, chatTypeErr := c.GetChatType(chatID, true) if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { @@ -629,6 +654,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, if err != nil { return err.Error(), true, false } + return "", true, true // edit message case "s": if c.me == nil { @@ -662,6 +688,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } else { return "Message processing error", true, false } + return "", true, true // send without sound case "silent": content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0)) @@ -767,6 +794,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } else { return "Message processing error", true, false } + return "", true, true // forward a message to chat case "forward": messageId, err := strconv.ParseInt(args[0], 10, 64) @@ -1109,6 +1137,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, } c.sendMessagesReverse(chatID, messages.Messages, true, "") + return "", true, true // get latest entries from history case "history": var limit int32 = 10 @@ -1124,6 +1153,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, return err.Error(), true, false } c.sendMessagesReverse(chatID, messages, true, "") + return "", true, true // chat members case "members": var query string @@ -1153,7 +1183,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, return "", false, false } - return "", true, true + return "Success", true, true } func (c *Client) cmdAdd(args []string) (string, bool) { @@ -1169,7 +1199,7 @@ func (c *Client) cmdAdd(args []string) (string, bool) { c.subscribeToID(chat.Id, chat, true) - return "", true + return "Subscription sent", true } func (c *Client) cmdJoin(args []string) (string, bool) { @@ -1198,7 +1228,7 @@ func (c *Client) cmdJoin(args []string) (string, bool) { } } - return "", true + return "Joined", true } func (c *Client) cmdSupergroup(args []string, cmdline string) (string, bool) { @@ -1210,7 +1240,7 @@ func (c *Client) cmdSupergroup(args []string, cmdline string) (string, bool) { return err.Error(), false } - return "", true + return "Created", true } func (c *Client) cmdChannel(args []string, cmdline string) (string, bool) { @@ -1223,5 +1253,5 @@ func (c *Client) cmdChannel(args []string, cmdline string) (string, bool) { return err.Error(), false } - return "", true + return "Created", true } diff --git a/telegram/connect.go b/telegram/connect.go index 4336145..3909d59 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -95,7 +95,7 @@ func (stateHandler *clientAuthorizer) Close() { } // Connect starts TDlib connection -func (c *Client) Connect(resource string) error { +func (c *Client) Connect(resource string, wasSessionLoginEmpty bool) error { log.Warn("Attempting to connect to Telegram network...") // avoid conflict if another authorization is pending already @@ -165,7 +165,13 @@ func (c *Client) Connect(resource string) error { } gateway.SubscribeToTransport(c.xmpp, c.jid) - c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login)) + loggedInString := "Logged in as: " + c.Session.Login + c.sendPresence(gateway.SPStatus(loggedInString)) + if wasSessionLoginEmpty { + for _, jid := range c.GetCarbonFullJids(true, "", false) { + gateway.SendServiceMessage(jid, loggedInString, c.xmpp) + } + } }() log.Warn("Client connected!") @@ -178,7 +184,7 @@ func (c *Client) TryLogin(resource string, login string) error { if wasSessionLoginEmpty && c.authorizer == nil { go func() { - err := c.Connect(resource) + err := c.Connect(resource, wasSessionLoginEmpty) if err != nil { log.Error(errors.Wrap(err, "TDlib connection failure")) } diff --git a/telegram/handlers.go b/telegram/handlers.go index 35be03c..f63ef35 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -293,92 +293,121 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { sId := strconv.FormatInt(update.MessageId, 10) var isCarbon bool - message, messageErr := c.client.GetMessage(&client.GetMessageRequest{ - ChatId: update.ChatId, - MessageId: update.MessageId, - }) - var prefix string - if messageErr == nil { - if message.EditDate == 0 { - return + go func() { + message, messageErr := c.client.GetMessage(&client.GetMessageRequest{ + ChatId: update.ChatId, + MessageId: update.MessageId, + }) + if messageErr != nil { + // odnako za vremya puti + // sobaka mogla podrasti + c.MessageIdChangesLock.Lock() + idsMap, idsMapOk := c.MessageIdChanges[update.ChatId] + hadNoId := false + if idsMapOk { + newId, newIdOk := idsMap[update.MessageId] + if newIdOk { + if newId.Id == 0 { + hadNoId = true + c.MessageIdChangesLock.Unlock() + newId.Lock() + } + log.Infof("falling back to updated message id: %v/%v->%v", update.ChatId, update.MessageId, newId.Id) + message, messageErr = c.client.GetMessage(&client.GetMessageRequest{ + ChatId: update.ChatId, + MessageId: newId.Id, + }) + } + } + if !hadNoId { + c.MessageIdChangesLock.Unlock() + } } - isCarbon = c.isCarbonsEnabled() && message.IsOutgoing && !isMUC - // reply correction support in clients is suboptimal yet, so cut them out for now - prefix, _ = c.messageToPrefix(message, "", "", true) - } else { - log.Errorf("No message %v/%v found, cannot reliably determine if it is a carbon and if it is edited", update.ChatId, update.MessageId) - } - - // use XEP-0308 edits only if the last message is edited for sure, fallback otherwise - if c.Session.NativeEdits { - lastXmppId, ok := c.getLastChatMessageId(update.ChatId) - if xmppIdErr != nil { - xmppId = sId - } - if ok && lastXmppId == xmppId { - replaceId = xmppId - } else { - log.Infof("Mismatching message ids: %v %v, falling back to separate edit message", lastXmppId, xmppId) - } - } - - var forceFallback bool - - var from string - var originalFrom string - var nickname string - if isMUC { + var prefix string if messageErr == nil { - senderId := c.getMessageSenderId(message) - nickname = c.GetMUCNickname(senderId) - originalFrom = gateway.CHATJID(senderId, true) + if message.EditDate == 0 { + return + } + log.Debugf("editDate: %v", message.EditDate) - safeToSend = c.assureMUCOccupant(update.ChatId, senderId, message.SenderId, chat) - from = gateway.MUCJID(update.ChatId) + "/" + nickname + isCarbon = c.isCarbonsEnabled() && message.IsOutgoing && !isMUC + // reply correction support in clients is suboptimal yet, so cut them out for now + prefix, _ = c.messageToPrefix(message, "", "", true) } else { - nickname = "#ERROR#" - forceFallback = true - from = gateway.MUCJID(update.ChatId) + log.Errorf("No message %v/%v found, cannot reliably determine if it is a carbon and if it is edited: %v", update.ChatId, update.MessageId, messageErr.Error()) } - } else { - from = gateway.CHATNODE(update.ChatId) - } - var text strings.Builder + // use XEP-0308 edits only if the last message is edited for sure, fallback otherwise + if c.Session.NativeEdits { + lastXmppId, ok := c.getLastChatMessageId(update.ChatId) + if xmppIdErr != nil { + xmppId = sId + } + if ok && lastXmppId == xmppId { + replaceId = xmppId + } else { + log.Infof("Mismatching message ids: %v %v, falling back to separate edit message", lastXmppId, xmppId) + } + } - if replaceId == "" || forceFallback { - var editChar string - if c.Session.AsciiArrows { - editChar = "e" + var forceFallback bool + + var from string + var originalFrom string + var nickname string + if isMUC { + if messageErr == nil { + senderId := c.getMessageSenderId(message) + nickname = c.GetMUCNickname(senderId) + originalFrom = gateway.CHATJID(senderId, true) + + safeToSend = c.assureMUCOccupant(update.ChatId, senderId, message.SenderId, chat) + from = gateway.MUCJID(update.ChatId) + "/" + nickname + } else { + nickname = "#ERROR#" + forceFallback = true + from = gateway.MUCJID(update.ChatId) + } } else { - editChar = "✎" + from = gateway.CHATNODE(update.ChatId) } - text.WriteString(fmt.Sprintf("%s %v | ", editChar, update.MessageId)) - } else if prefix != "" { - text.WriteString(prefix) - text.WriteString(c.getPrefixSeparator(update.ChatId)) - } - text.WriteString(formatter.Format( - textContent.Text.Text, - textContent.Text.Entities, - markupFunction, - c, - )) + var text strings.Builder - id := "e"+sId - uuid, err := uuid.NewRandom() - if err == nil { - id = id+":"+uuid.String() - } - for _, jid := range jids { - if safeToSend { - gateway.SendMessage(jid, from, text.String(), id, c.xmpp, nil, 0, replaceId, isCarbon, isMUC, false, originalFrom, "", "", "", nil) - } else { - gateway.SendMUCAnnouncement(jid, from, text.String(), nickname, id, c.xmpp) + if replaceId == "" || forceFallback { + var editChar string + if c.Session.AsciiArrows { + editChar = "e" + } else { + editChar = "✎" + } + text.WriteString(fmt.Sprintf("%s %v | ", editChar, update.MessageId)) + } else if prefix != "" { + text.WriteString(prefix) + text.WriteString(c.getPrefixSeparator(update.ChatId)) } - } + + text.WriteString(formatter.Format( + textContent.Text.Text, + textContent.Text.Entities, + markupFunction, + c, + )) + + id := "e"+sId + uuid, err := uuid.NewRandom() + if err == nil { + id = id+":"+uuid.String() + } + for _, jid := range jids { + if safeToSend { + gateway.SendMessage(jid, from, text.String(), id, c.xmpp, nil, 0, replaceId, isCarbon, isMUC, false, originalFrom, "", "", "", nil) + } else { + gateway.SendMUCAnnouncement(jid, from, text.String(), nickname, id, c.xmpp) + } + } + }() } } @@ -394,6 +423,10 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { c.locks.pinOutboxLock.Unlock() if update.IsPermanent { + for _, deleteId := range update.MessageIds { + c.tryUnlockMessageId(update.ChatId, deleteId) + } + if c.Session.IsChatIgnored(update.ChatId) { return } @@ -464,6 +497,20 @@ func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucc log.Errorf("failed to replace %v with %v: %v", update.OldMessageId, update.Message.Id, err.Error()) } + c.MessageIdChangesLock.Lock() + idsMap, ok := c.MessageIdChanges[update.Message.ChatId] + if !ok { + idsMap = make(map[int64]*newId) + c.MessageIdChanges[update.Message.ChatId] = idsMap + } + id, ok := idsMap[update.OldMessageId] + if !ok { + id = newNewId() + idsMap[update.OldMessageId] = id + } + id.Id = update.Message.Id + c.MessageIdChangesLock.Unlock() + c.sendMarker(update.Message.ChatId, update.Message.Id, gateway.MarkerTypeReceived) // clean uploaded files @@ -473,6 +520,8 @@ func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucc } } func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) { + c.tryUnlockMessageId(update.Message.ChatId, update.OldMessageId) + c.locks.pinOutboxLock.Lock() ch, chOk := c.pinOutbox[IntPair{update.Message.ChatId, update.OldMessageId}] if chOk { @@ -563,3 +612,15 @@ func (c *Client) updateChatPermissions(update *client.UpdateChatPermissions) { c.locks.mucCacheLock.Unlock() } } + +func (c *Client) tryUnlockMessageId(chatId, messageId int64) { + c.MessageIdChangesLock.Lock() + idsMap, ok := c.MessageIdChanges[chatId] + if ok { + id, ok := idsMap[messageId] + if ok { + id.Unlock() + } + } + c.MessageIdChangesLock.Unlock() +} diff --git a/xmpp/component.go b/xmpp/component.go index 8a6baed..1f0ce06 100644 --- a/xmpp/component.go +++ b/xmpp/component.go @@ -150,6 +150,18 @@ func heartbeat(component *xmpp.Component) { } } session.DelayedStatusesLock.Unlock() + + // shrink message id maps + session.MessageIdChangesLock.Lock() + for _, idsMap := range session.MessageIdChanges { + for oldMessageId, newId := range idsMap { + if newId.Ts < now - 60 { + newId.Unlock() + delete(idsMap, oldMessageId) + } + } + } + session.MessageIdChangesLock.Unlock() } sessionLock.Unlock() @@ -209,7 +221,7 @@ func getTelegramInstance(jid string, savedSession *persistence.Session, componen return session, false } if savedSession.KeepOnline { - if err = session.Connect(""); err != nil { + if err = session.Connect("", false); err != nil { log.Error(err) return session, false } diff --git a/xmpp/handlers.go b/xmpp/handlers.go index d0ee57b..a0f7670 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -560,7 +560,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) { // due to the weird implementation of go-tdlib wrapper, it won't // return the client instance until successful authorization go func() { - err := session.Connect(resource) + err := session.Connect(resource, false) if err != nil { log.Error(errors.Wrap(err, "TDlib connection failure")) } else { @@ -1175,7 +1175,7 @@ func handleGetDiscoItems(s xmpp.Sender, iq *stanza.IQ, di *stanza.DiscoItems) { } commands := telegram.GetCommands(cmdType) - for _, name := range telegram.SortedCommandKeys(commands) { + for _, name := range telegram.SortedCommandKeys(commands, telegram.OnlineFilterAny) { command := commands[name] if chatTypeErr == nil && !telegram.IsCommandForChatType(command, chatType) { continue