211 lines · 8.6 KB · GoRaw
package mainimport ( "fmt" "strings" "go.mau.fi/whatsmeow/proto/waE2E" "go.mau.fi/whatsmeow/proto/waWeb" "go.mau.fi/whatsmeow/types" "go.mau.fi/whatsmeow/types/events")// PNResolver turns a hidden (LID) user id into the phone-number id when the session knows it.// Cotton keys people and chats by phone number, as the backup and the export do.type PNResolver func(types.JID) (types.JID, bool)func normalize(j types.JID, resolve PNResolver) (pn types.JID, lid types.JID) { j = j.ToNonAD() if j.Server == types.HiddenUserServer { lid = j if resolve != nil { if p, ok := resolve(j); ok && !p.IsEmpty() { return p.ToNonAD(), lid } } return j, lid } return j, types.EmptyJID}// Convert turns one whatsmeow message into the events Cotton stores: a message, an edit (a message// with edit_of), or a revoke. It returns nothing for the plumbing WhatsApp sends as messages (key// distribution, app-state notices, ephemeral settings), nor for a status (stories) post: Cotton// collects messages only (owner, 22 Nov 2026), so a status never leaves this helper.func Convert(evt *events.Message, origin string, resolve PNResolver) *Event { info := evt.Info if IsStatus(info.Chat) { return nil } chat, _ := normalize(info.Chat, resolve) sender, senderLID := normalize(info.Sender, resolve) if senderLID.IsEmpty() == false && sender.Server == types.HiddenUserServer && !info.SenderAlt.IsEmpty() && info.SenderAlt.Server == types.DefaultUserServer { sender = info.SenderAlt.ToNonAD() } if chat.Server == types.HiddenUserServer && !info.IsGroup { // A 1:1 chat addressed by LID: the other side's phone number is the sender (incoming) or the recipient alt (outgoing). if !info.IsFromMe && sender.Server == types.DefaultUserServer { chat = sender } else if info.IsFromMe && info.RecipientAlt.Server == types.DefaultUserServer { chat = info.RecipientAlt.ToNonAD() } } m := &Message{ ID: info.ID, Chat: chat.String(), FromMe: info.IsFromMe, IsGroup: info.IsGroup, Timestamp: info.Timestamp.Unix(), PushName: info.PushName, Origin: origin, } if !info.IsFromMe { m.Sender = sender.String() if !senderLID.IsEmpty() { m.SenderLID = senderLID.String() } } msg := evt.Message if msg == nil { return nil } if pm := msg.GetProtocolMessage(); pm != nil { switch pm.GetType() { case waE2E.ProtocolMessage_REVOKE: return &Event{Event: "revoke", Chat: m.Chat, Target: pm.GetKey().GetID(), Message: m} case waE2E.ProtocolMessage_MESSAGE_EDIT: edited := pm.GetEditedMessage() if edited == nil { return nil } if !fillContent(m, edited) { return nil } m.EditOf = pm.GetKey().GetID() return &Event{Event: "message", Message: m} default: return nil } } if !fillContent(m, msg) { return nil } reuploadable(m.Media, info) return &Event{Event: "message", Message: m}}// reuploadable names the message a file came in, as WhatsApp addressed it, so the file can be asked of the owner's// phone again once WhatsApp's servers no longer hold it (Hub.reupload).func reuploadable(p *MediaPointer, info types.MessageInfo) { if p == nil { return } p.MessageID, p.Chat, p.FromMe, p.IsGroup = string(info.ID), info.Chat.String(), info.IsFromMe, info.IsGroup if !info.Sender.IsEmpty() { p.Sender = info.Sender.String() }}// fillContent sets kind, text, quote and media from the message body; false when there is nothing a// person wrote or sent.func fillContent(m *Message, msg *waE2E.Message) bool { var ctx *waE2E.ContextInfo switch { case msg.GetConversation() != "": m.Kind, m.Text = "text", msg.GetConversation() case msg.GetExtendedTextMessage() != nil: t := msg.GetExtendedTextMessage() m.Kind, m.Text, ctx = "text", t.GetText(), t.GetContextInfo() case msg.GetImageMessage() != nil: x := msg.GetImageMessage() m.Kind, m.Text, ctx = "image", x.GetCaption(), x.GetContextInfo() m.Media = &MediaPointer{Type: "image", Mimetype: x.GetMimetype(), DirectPath: x.GetDirectPath(), MediaKey: x.GetMediaKey(), FileSHA256: x.GetFileSHA256(), FileEncSHA256: x.GetFileEncSHA256(), FileLength: x.GetFileLength()} case msg.GetVideoMessage() != nil || msg.GetPtvMessage() != nil: x := msg.GetVideoMessage() if x == nil { x = msg.GetPtvMessage() } m.Kind, m.Text, ctx = "video", x.GetCaption(), x.GetContextInfo() m.Media = &MediaPointer{Type: "video", Mimetype: x.GetMimetype(), DirectPath: x.GetDirectPath(), MediaKey: x.GetMediaKey(), FileSHA256: x.GetFileSHA256(), FileEncSHA256: x.GetFileEncSHA256(), FileLength: x.GetFileLength()} case msg.GetAudioMessage() != nil: x := msg.GetAudioMessage() m.Kind, ctx = "audio", x.GetContextInfo() if x.GetPTT() { m.Kind = "voice" } m.Media = &MediaPointer{Type: "audio", Mimetype: x.GetMimetype(), DirectPath: x.GetDirectPath(), MediaKey: x.GetMediaKey(), FileSHA256: x.GetFileSHA256(), FileEncSHA256: x.GetFileEncSHA256(), FileLength: x.GetFileLength()} case msg.GetDocumentMessage() != nil: x := msg.GetDocumentMessage() m.Kind, m.Text, ctx = "document", x.GetCaption(), x.GetContextInfo() m.Media = &MediaPointer{Type: "document", Mimetype: x.GetMimetype(), FileName: x.GetFileName(), DirectPath: x.GetDirectPath(), MediaKey: x.GetMediaKey(), FileSHA256: x.GetFileSHA256(), FileEncSHA256: x.GetFileEncSHA256(), FileLength: x.GetFileLength()} case msg.GetStickerMessage() != nil: x := msg.GetStickerMessage() m.Kind, ctx = "sticker", x.GetContextInfo() m.Media = &MediaPointer{Type: "sticker", Mimetype: x.GetMimetype(), DirectPath: x.GetDirectPath(), MediaKey: x.GetMediaKey(), FileSHA256: x.GetFileSHA256(), FileEncSHA256: x.GetFileEncSHA256(), FileLength: x.GetFileLength()} case msg.GetLocationMessage() != nil: x := msg.GetLocationMessage() m.Kind, ctx = "location", x.GetContextInfo() m.Text = strings.TrimSpace(fmt.Sprintf("%s %s", x.GetName(), x.GetAddress())) if m.Text == "" { m.Text = fmt.Sprintf("%.6f, %.6f", x.GetDegreesLatitude(), x.GetDegreesLongitude()) } case msg.GetContactMessage() != nil: x := msg.GetContactMessage() m.Kind, m.Text, ctx = "contact", x.GetDisplayName(), x.GetContextInfo() case msg.GetContactsArrayMessage() != nil: x := msg.GetContactsArrayMessage() names := make([]string, 0, len(x.GetContacts())) for _, c := range x.GetContacts() { names = append(names, c.GetDisplayName()) } m.Kind, m.Text, ctx = "contact", strings.Join(names, ", "), x.GetContextInfo() case msg.GetReactionMessage() != nil: x := msg.GetReactionMessage() m.Kind, m.Text, m.ReactionTo = "reaction", x.GetText(), x.GetKey().GetID() case msg.GetPollCreationMessage() != nil || msg.GetPollCreationMessageV2() != nil || msg.GetPollCreationMessageV3() != nil: x := msg.GetPollCreationMessage() if x == nil { x = msg.GetPollCreationMessageV2() } if x == nil { x = msg.GetPollCreationMessageV3() } m.Kind, m.Text, ctx = "poll", x.GetName(), x.GetContextInfo() m.Poll = &Poll{Selectable: x.GetSelectableOptionsCount()} for _, o := range x.GetOptions() { m.Poll.Options = append(m.Poll.Options, o.GetOptionName()) } case msg.GetCallLogMesssage() != nil: // A call WhatsApp logged in the chat (history): its line (calls.go). m.Call = callLog(msg.GetCallLogMesssage(), m.ID, m.FromMe, m.IsGroup) if m.Call == nil { return false } m.Kind = "call" default: return false } if ctx != nil && ctx.GetStanzaID() != "" { m.QuotedID = ctx.GetStanzaID() } return true}// IsStatus says whether a chat is WhatsApp's status (stories) feed, status@broadcast. A broadcast// list the owner sends to is not: its messages are messages.func IsStatus(chat types.JID) bool { return chat.User == types.StatusBroadcastJID.User && chat.Server == types.StatusBroadcastJID.Server}// historyRevoke is the delete for everyone a history entry stands for: WhatsApp's history keeps a message deleted// for everyone as a stub (REVOKE) under the deleted message's own key, with no content (CONTRACT "Deleted for// everyone in WhatsApp", the ask of mac-import). It goes as a revoke of that id, so a message Cotton holds is// erased and one it never held changes nothing. Nothing for any other entry.func historyRevoke(web *waWeb.WebMessageInfo, chat types.JID, resolve PNResolver) *Event { if web.GetMessage() != nil || web.GetMessageStubType() != waWeb.WebMessageInfo_REVOKE || web.GetKey().GetID() == "" || IsStatus(chat) { return nil } norm, _ := normalize(chat, resolve) m := &Message{ID: web.GetKey().GetID(), Chat: norm.String(), FromMe: web.GetKey().GetFromMe(), IsGroup: chat.Server == types.GroupServer, Timestamp: int64(web.GetMessageTimestamp()), Origin: "history"} return &Event{Event: "revoke", Chat: m.Chat, Target: m.ID, Message: m}}