From 06964d832e58b30a82438072d174d3d30610463a Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sat, 7 Jun 2025 03:24:48 -0400 Subject: [PATCH] Get rid of edited message hash comparison in favour of EditDate check --- Makefile | 2 +- telegabber.go | 2 +- telegram/client.go | 3 --- telegram/handlers.go | 38 ++++++++++++++--------------- telegram/utils.go | 57 -------------------------------------------- 5 files changed, 20 insertions(+), 82 deletions(-) diff --git a/Makefile b/Makefile index bfac3a1..0022e89 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ COMMIT := $(shell git rev-parse --short HEAD) TD_COMMIT := "5bbfc1cf5dab94f82e02f3430ded7241d4653551" -VERSION := "v1.12.2" +VERSION := "v1.12.3" MAKEOPTS := "-j4" all: diff --git a/telegabber.go b/telegabber.go index f4bed43..3ef050c 100644 --- a/telegabber.go +++ b/telegabber.go @@ -16,7 +16,7 @@ import ( goxmpp "gosrc.io/xmpp" ) -var version string = "1.12.2" +var version string = "1.12.3" var commit string var sm *goxmpp.StreamManager diff --git a/telegram/client.go b/telegram/client.go index daaf627..005ec2f 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -2,7 +2,6 @@ package telegram import ( "github.com/pkg/errors" - "hash/maphash" "path/filepath" "strconv" "sync" @@ -56,7 +55,6 @@ type Client struct { lastMsgHashes map[int64]uint64 lastMsgIds map[int64]string - msgHashSeed maphash.Seed LastBotCmdString string @@ -149,7 +147,6 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component DelayedStatuses: make(map[int64]*DelayedStatus), lastMsgHashes: make(map[int64]uint64), lastMsgIds: make(map[int64]string), - msgHashSeed: maphash.MakeSeed(), XmppClientFeatures: make(map[string]*[]string), AvatarHashes: make(map[int64]*HashedAvatar), locks: clientLocks{ diff --git a/telegram/handlers.go b/telegram/handlers.go index d1e4dce..5a193f6 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -245,8 +245,6 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { lock.Lock() defer lock.Unlock() - c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content) - var forceCmd bool if c.LastBotCmdString != "" && update.Message.IsOutgoing { if update.Message.Content.MessageContentType() == client.TypeMessageText { @@ -283,8 +281,6 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { markupFunction := c.getFormatter() - defer c.updateLastMessageHash(update.ChatId, update.MessageId, update.NewContent) - log.Debugf("newContent: %#v", update.NewContent) lock := c.getChatMessageLock(update.ChatId) @@ -308,7 +304,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { return } - if update.NewContent.MessageContentType() == client.TypeMessageText && c.hasLastMessageHashChanged(update.ChatId, update.MessageId, update.NewContent) { + if update.NewContent.MessageContentType() == client.TypeMessageText { textContent := update.NewContent.(*client.MessageText) log.Debugf("textContent: %#v", textContent.Text) @@ -316,6 +312,23 @@ 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 + } + + isCarbon = c.isCarbonsEnabled() && message.IsOutgoing + // 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) @@ -329,19 +342,6 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { } } - message, messageErr := c.client.GetMessage(&client.GetMessageRequest{ - ChatId: update.ChatId, - MessageId: update.MessageId, - }) - var prefix string - if messageErr == nil { - isCarbon = c.isCarbonsEnabled() && message.IsOutgoing - // 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's a carbon", update.ChatId, update.MessageId) - } - var text strings.Builder if replaceId == "" { @@ -411,8 +411,6 @@ func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucc log.Errorf("failed to replace %v with %v: %v", update.OldMessageId, update.Message.Id, err.Error()) } - c.updateLastMessageHash(update.Message.ChatId, update.Message.Id, update.Message.Content) - c.sendMarker(update.Message.ChatId, update.Message.Id, gateway.MarkerTypeReceived) // clean uploaded files diff --git a/telegram/utils.go b/telegram/utils.go index 592cde1..8e98326 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -4,10 +4,8 @@ import ( "bytes" "crypto/sha1" "encoding/base64" - "encoding/binary" "fmt" "github.com/pkg/errors" - "hash/maphash" "io" "io/ioutil" "net/http" @@ -1758,61 +1756,6 @@ func (c *Client) getCarbonFullJids(isOutgoing bool, ignoredResource string) []st return jids } -func (c *Client) calculateMessageHash(messageId int64, content client.MessageContent) uint64 { - var h maphash.Hash - h.SetSeed(c.msgHashSeed) - - buf8 := make([]byte, 8) - binary.BigEndian.PutUint64(buf8, uint64(messageId)) - h.Write(buf8) - - if content != nil && content.MessageContentType() == client.TypeMessageText { - textContent, ok := content.(*client.MessageText) - if !ok { - uhOh() - } - - if textContent.Text != nil { - h.WriteString(textContent.Text.Text) - for _, entity := range textContent.Text.Entities { - buf4 := make([]byte, 4) - binary.BigEndian.PutUint32(buf4, uint32(entity.Offset)) - h.Write(buf4) - binary.BigEndian.PutUint32(buf4, uint32(entity.Length)) - h.Write(buf4) - h.WriteString(entity.Type.TextEntityTypeType()) - } - } - } - - return h.Sum64() -} - -func (c *Client) updateLastMessageHash(chatId, messageId int64, content client.MessageContent) { - c.locks.lastMsgHashesLock.Lock() - defer c.locks.lastMsgHashesLock.Unlock() - - c.lastMsgHashes[chatId] = c.calculateMessageHash(messageId, content) -} - -func (c *Client) hasLastMessageHashChanged(chatId, messageId int64, content client.MessageContent) bool { - c.locks.lastMsgHashesLock.Lock() - defer c.locks.lastMsgHashesLock.Unlock() - - oldHash, ok := c.lastMsgHashes[chatId] - newHash := c.calculateMessageHash(messageId, content) - - if !ok { - log.Warnf("Last message hash for chat %v does not exist", chatId) - } - log.WithFields(log.Fields{ - "old hash": oldHash, - "new hash": newHash, - }).Info("Message hashes") - - return !ok || oldHash != newHash -} - func (c *Client) UpdateLastChatMessageId(chatId int64, messageId string) { c.locks.lastMsgIdsLock.Lock() defer c.locks.lastMsgIdsLock.Unlock()