diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c9fef736..146b93a0 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -30,6 +30,7 @@ jobs: run: | go install golang.org/x/tools/cmd/goimports@latest go install honnef.co/go/tools/cmd/staticcheck@latest + go install github.com/beeper/pre-commit-go/bridgelint@latest export PATH="$HOME/go/bin:$PATH" - name: Run pre-commit diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1bfb5360..1c96b066 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,9 +21,12 @@ repos: - id: go-mod-tidy - repo: https://github.com/beeper/pre-commit-go - rev: v0.4.2 + rev: v0.5.0 hooks: - id: zerolog-ban-msgf - id: zerolog-use-stringer - id: prevent-literal-http-methods - id: zerolog-ban-global-log + - id: bridgelint + language: system + verbose: true diff --git a/go.mod b/go.mod index 0c70c3dc..f940c1de 100644 --- a/go.mod +++ b/go.mod @@ -14,13 +14,13 @@ require ( github.com/yuin/goldmark v1.8.6 go.mau.fi/util v0.11.0 go.mau.fi/webp v0.3.0 - go.mau.fi/whatsmeow v0.0.0-20261016130820-0b0a1cdac3e2 + go.mau.fi/whatsmeow v0.0.0-20261026125351-1fbae988179a golang.org/x/image v0.47.0 golang.org/x/net v0.61.0 golang.org/x/sync v0.24.0 google.golang.org/protobuf v1.36.12 gopkg.in/yaml.v3 v3.0.1 - maunium.net/go/mautrix v0.32.0 + maunium.net/go/mautrix v0.32.1-0.20261105141340-842796acf445 ) require ( diff --git a/go.sum b/go.sum index 576ec249..fb4e878f 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,8 @@ go.mau.fi/util v0.11.0 h1:JNEpkVRxfTdJUC0DIGKiRlLKzl4G/qLX/eVzoP7QuwY= go.mau.fi/util v0.11.0/go.mod h1:04AEFVsc4zNdIkDC1gkdqbg/nZFolHY50pyXh2DSy7s= go.mau.fi/webp v0.3.0 h1:gVHQZtz21Ziwj+CDuklbX9mqpsnDIFKxs/BJyV7iZzA= go.mau.fi/webp v0.3.0/go.mod h1:rlZFTev+dYxhvk+XNBP/5GcTt4gXmzAB4DU0aGUYIQo= -go.mau.fi/whatsmeow v0.0.0-20261016130820-0b0a1cdac3e2 h1:+EhsWM5VeE5Vu3KRlk3PmYAY0lEYkbhI5W4mTmM9X/Q= -go.mau.fi/whatsmeow v0.0.0-20261016130820-0b0a1cdac3e2/go.mod h1:iY4lm61nTLWsNiIE1EmHUZwhKGBeoV4dhnVE2uMtNoQ= +go.mau.fi/whatsmeow v0.0.0-20261026125351-1fbae988179a h1:ZyCV/mWfRkutxs5R0K3v+oyTbF4huprSlPZg/LEFQ7k= +go.mau.fi/whatsmeow v0.0.0-20261026125351-1fbae988179a/go.mod h1:iY4lm61nTLWsNiIE1EmHUZwhKGBeoV4dhnVE2uMtNoQ= go.mau.fi/zeroconfig v0.2.0 h1:e/OGEERqVRRKlgaro7E6bh8xXiKFSXB3eNNIud7FUjU= go.mau.fi/zeroconfig v0.2.0/go.mod h1:J0Vn0prHNOm493oZoQ84kq83ZaNCYZnq+noI1b1eN8w= golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= @@ -103,5 +103,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= -maunium.net/go/mautrix v0.32.0 h1:xuAZzzyWce0pFkBhV/MCLFiJdVs2rFkY3Eep7hNng4I= -maunium.net/go/mautrix v0.32.0/go.mod h1:tZSwPzw+/0pa0dxGufkh0uJqM84BNxB6e2I0+S0UyjU= +maunium.net/go/mautrix v0.32.1-0.20261105141340-842796acf445 h1:ZI9Y2YEmjqQDloOCiciHeynTRYjUwK0ZZbNcRH/5NOc= +maunium.net/go/mautrix v0.32.1-0.20261105141340-842796acf445/go.mod h1:tZSwPzw+/0pa0dxGufkh0uJqM84BNxB6e2I0+S0UyjU= diff --git a/pkg/connector/backfill.go b/pkg/connector/backfill.go index f1fc56e9..8f006e0b 100644 --- a/pkg/connector/backfill.go +++ b/pkg/connector/backfill.go @@ -142,8 +142,16 @@ func (wa *WhatsAppClient) downloadAndSaveWAHistorySyncData(ctx context.Context, Str("peer_data_request_session_id", evt.GetPeerDataRequestSessionID()). Msg("Downloading history sync") blob, err := wa.Client.DownloadHistorySync(log.WithContext(ctx), evt, true) - if err != nil { + if errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith403) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith404) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith410) { + log.Err(err).Msg("History sync blob is no longer available, dropping notification") + if err = wa.Main.DB.HSNotif.Delete(ctx, rowid); err != nil { + log.Err(err).Msg("Failed to delete queued history sync notification") + errored = true + } + return + } else if err != nil { log.Err(err).Msg("Failed to download history sync") + errored = true return } if blob.GetSyncType() == waHistorySync.HistorySync_ON_DEMAND { @@ -171,6 +179,7 @@ func (wa *WhatsAppClient) downloadAndSaveWAHistorySyncData(ctx context.Context, }) if err != nil { log.Err(err).Msg("Failed to store history sync notification data") + errored = true } else { resetTimer = blob.GetSyncType() == waHistorySync.HistorySync_INITIAL_BOOTSTRAP || blob.GetSyncType() == waHistorySync.HistorySync_RECENT || @@ -246,10 +255,17 @@ func (wa *WhatsAppClient) handleWAHistorySync( } else if jid.Server == types.BroadcastServer { log.Debug().Stringer("chat_jid", jid).Msg("Skipping broadcast list in history sync") continue + } else if jid == types.PSAJID { + // New messages in the official updates chat aren't bridged because they use a different path, + // so drop history as well. This should be removed if the PSA chat gets properly bridged in the future. + log.Debug().Stringer("chat_jid", jid).Msg("Skipping PSA in history sync") + continue } else { totalMessageCount += len(conv.GetMessages()) } - if jid.Server == types.DefaultUserServer { + if jid == types.MetaAIJID { + jid = types.NewMetaAIJID + } else if jid.Server == types.DefaultUserServer { lid, err := wa.GetStore().LIDs.GetLIDForPN(ctx, jid) if err != nil { log.Err(err).Stringer("pn", jid).Msg("Failed to get LID for phone number in history sync") @@ -282,6 +298,9 @@ func (wa *WhatsAppClient) handleWAHistorySync( Int("msg_index", i). Str("msg_id", rawMsg.GetMessage().GetKey().GetID()). Uint64("msg_time_seconds", rawMsg.GetMessage().GetMessageTimestamp()). + Stringer("stub_type", rawMsg.GetMessage().GetMessageStubType()). + Str("message_type", getMessageType(rawMsg.GetMessage().GetMessage())). + Bool("from_me", rawMsg.GetMessage().GetKey().GetFromMe()). Msg("Dropping historical message due to parse error") log.Trace(). Any("web_message_info", rawMsg.GetMessage()). @@ -435,7 +454,7 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) { return } wrappedInfo, err := wa.getChatInfo(ctx, conv.ChatJID, conv, true) - if errors.Is(err, whatsmeow.ErrNotInGroup) || errors.Is(err, whatsmeow.ErrGroupNotFound) { + if errors.Is(err, whatsmeow.ErrNotInGroup) || errors.Is(err, whatsmeow.ErrGroupNotFound) || errors.Is(err, ErrNewsletterNotFound) { log.Debug().Err(err).Stringer("chat_jid", conv.ChatJID). Msg("Skipping creating room because the user is not a participant") //err = wa.Main.DB.Message.DeleteAllInChat(ctx, wa.UserLogin.ID, conv.ChatJID) @@ -586,16 +605,36 @@ func (wa *WhatsAppClient) FetchMessages(ctx context.Context, params bridgev2.Fet } } } - resp, err := wa.convertHistorySyncMessages(ctx, params.Portal, portalJID, messages, true) + resp, err := wa.convertHistorySyncMessages(ctx, params.Portal, portalJID, messages, true, !params.Forward && !hasMore) if err != nil { return nil, fmt.Errorf("failed to convert messages: %w", err) } + // TODO might be better to always set true here and let GetBetween return an empty result to mark as false? resp.HasMore = hasMore resp.Forward = params.Forward resp.MarkRead = markRead + if params.Forward && !markRead && conv != nil && !ptr.Val(conv.MarkedAsUnread) { + resp.ReadUpTo = historySyncReadUpTo(resp.Messages, int(ptr.Val(conv.UnreadCount))) + } return resp, nil } +func historySyncReadUpTo(messages []*bridgev2.BackfillMessage, unreadCount int) time.Time { + if unreadCount <= 0 { + return time.Time{} + } + for i := len(messages) - 1; i >= 0; i-- { + if messages[i].Sender.IsFromMe { + continue + } + unreadCount-- + if unreadCount == 0 { + return messages[i].Timestamp.Add(-time.Nanosecond) + } + } + return time.Time{} +} + func (wa *WhatsAppClient) deleteHistorySyncMessages(ctx context.Context, portalJID types.JID, newestTS, oldestTS uint64) { var err error var rows int64 @@ -628,6 +667,7 @@ func (wa *WhatsAppClient) convertHistorySyncMessages( portalJID types.JID, messages []*waWeb.WebMessageInfo, explodeOnError bool, + deleteEverything bool, ) (*bridgev2.FetchMessagesResponse, error) { cli := wa.Client if cli == nil { @@ -664,6 +704,9 @@ func (wa *WhatsAppClient) convertHistorySyncMessages( if !wa.ensureAltJIDs(ctx, &evt.Info.MessageSource, false) { return nil, fmt.Errorf("failed to ensure alt JIDs for message %s", evt.Info.ID) } + if evt.Info.IsFromMe { + wa.addPreviousOwnLID(ctx, evt.Info.Sender) + } isViewOnce := evt.IsViewOnce || evt.IsViewOnceV2 || evt.IsViewOnceV2Extension converted, mediaReq := wa.convertHistorySyncMessage( ctx, cli, portal, &evt.Info, evt.Message, evt.RawMessage, isViewOnce, msg.Reactions, @@ -683,9 +726,11 @@ func (wa *WhatsAppClient) convertHistorySyncMessages( Messages: convertedMessages, Cursor: networkid.PaginationCursor(strconv.FormatUint(oldestTS, 10)), CompleteCallback: func() { - // TODO this only deletes after backfilling. If there's no need for backfill after a relogin, - // the messages will be stuck in the database - wa.deleteHistorySyncMessages(ctx, portalJID, newestTS, oldestTS) + if deleteEverything { + wa.deleteHistorySyncMessages(ctx, portalJID, 1<<62, oldestTS) + } else { + wa.deleteHistorySyncMessages(ctx, portalJID, newestTS, oldestTS) + } if len(mediaRequests) > 0 { go func(ctx context.Context) { for _, req := range mediaRequests { @@ -766,10 +811,12 @@ type onDemandHistorySyncRequest struct { SentAt time.Time } +const onDemandHistorySyncRequestTimeout = 1 * time.Hour + func (wa *WhatsAppClient) startOnDemandHistorySyncRequest(ctx context.Context, portalJID types.JID, msgID types.MessageID) bool { wa.onDemandHistorySyncLock.Lock() defer wa.onDemandHistorySyncLock.Unlock() - if req, inFlight := wa.onDemandHistorySyncRequests[portalJID]; inFlight { + if req, inFlight := wa.onDemandHistorySyncRequests[portalJID]; inFlight && time.Since(req.SentAt) < onDemandHistorySyncRequestTimeout { zerolog.Ctx(ctx).Debug(). Str("in_flight_request_msg_id", req.ID). Stringer("portal_jid", portalJID). @@ -803,7 +850,9 @@ func (wa *WhatsAppClient) handleOnDemandHistorySync(ctx context.Context, blob *w zerolog.Ctx(ctx).Err(err).Str("jid", conv.GetID()).Msg("Failed to parse portal JID") continue } - if portalJID.Server == types.DefaultUserServer { + if portalJID == types.MetaAIJID { + portalJID = types.NewMetaAIJID + } else if portalJID.Server == types.DefaultUserServer { lid, err := wa.GetStore().LIDs.GetLIDForPN(ctx, portalJID) if err != nil { zerolog.Ctx(ctx).Err(err).Stringer("lid", portalJID).Msg("Failed to get LID for phone number in on-demand history sync") @@ -844,7 +893,7 @@ func (wa *WhatsAppClient) handleOnDemandHistorySync(ctx context.Context, blob *w Int("message_count", len(messages)). Stringer("end_of_history_type", conv.GetEndOfHistoryTransferType()). Msg("Converting messages to bridge from on-demand history sync") - resp, err := wa.convertHistorySyncMessages(ctx, portal, portalJID, messages, false) + resp, err := wa.convertHistorySyncMessages(ctx, portal, portalJID, messages, false, false) if err != nil { return nil, err } diff --git a/pkg/connector/chatinfo.go b/pkg/connector/chatinfo.go index 61b9e740..ce8c7396 100644 --- a/pkg/connector/chatinfo.go +++ b/pkg/connector/chatinfo.go @@ -31,6 +31,7 @@ func (wa *WhatsAppClient) GetChatInfo(ctx context.Context, portal *bridgev2.Port } var ErrBroadcastList = errors.New("broadcast list bridging is currently not supported") +var ErrNewsletterNotFound = errors.New("newsletter doesn't exist") func (wa *WhatsAppClient) getChatInfo(ctx context.Context, portalJID types.JID, conv *wadb.Conversation, isNew bool) (wrapped *bridgev2.ChatInfo, err error) { switch portalJID.Server { @@ -56,6 +57,8 @@ func (wa *WhatsAppClient) getChatInfo(ctx context.Context, portalJID types.JID, info, err := wa.Client.GetNewsletterInfo(ctx, portalJID) if err != nil { return nil, err + } else if info == nil || info.ID.IsEmpty() { + return nil, ErrNewsletterNotFound } wrapped = wa.wrapNewsletterInfo(ctx, info) default: diff --git a/pkg/connector/client.go b/pkg/connector/client.go index cd52be28..678c8d22 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -55,6 +55,7 @@ func (wa *WhatsAppConnector) LoadUserLogin(ctx context.Context, login *bridgev2. mediaRetryLock: semaphore.NewWeighted(wa.Config.HistorySync.MediaRequests.MaxAsyncHandle), pushNamesSynced: exsync.NewEvent(), appStateFullSyncAttempted: make(map[appstate.WAPatchName]time.Time), + previousOwnLIDs: exsync.NewSetWithItems(login.Metadata.(*waid.UserLoginMetadata).PreviousOwnLIDs), onDemandHistorySyncRequests: make(map[types.JID]onDemandHistorySyncRequest), @@ -84,6 +85,7 @@ func (wa *WhatsAppConnector) LoadUserLogin(ctx context.Context, login *bridgev2. w.Client.ManualHistorySyncDownload = true w.Client.SendReportingTokens = true w.Client.AutomaticMessageRerequestFromPhone = true + w.Client.UnifiedSessionInterval = 24 * time.Hour w.Client.GetMessageForRetry = w.trackNotFoundRetry w.Client.PreRetryCallback = w.trackFoundRetry w.Client.BackgroundEventCtx = w.UserLogin.Log.WithContext(wa.Bridge.BackgroundCtx) @@ -137,6 +139,9 @@ type WhatsAppClient struct { onDemandHistorySyncLock sync.Mutex onDemandHistorySyncRequests map[types.JID]onDemandHistorySyncRequest + + previousOwnLIDs *exsync.Set[types.JID] + previousOwnLIDsLock sync.Mutex } var ( @@ -205,7 +210,26 @@ func (wa *WhatsAppClient) IsThisUser(_ context.Context, userID networkid.UserID) func (wa *WhatsAppClient) IsOwnJID(jid types.JID) bool { return (jid.Server == types.DefaultUserServer && jid.User == wa.JID.User) || - (jid.Server == types.HiddenUserServer && jid.User == wa.GetLID().User) + (jid.Server == types.HiddenUserServer && jid.User == wa.GetLID().User) || + wa.previousOwnLIDs.Has(jid.ToNonAD()) +} + +func (wa *WhatsAppClient) addPreviousOwnLID(ctx context.Context, jid types.JID) { + if jid.Server != types.HiddenUserServer || wa.IsOwnJID(jid) { + return + } + jid = jid.ToNonAD() + wa.previousOwnLIDsLock.Lock() + defer wa.previousOwnLIDsLock.Unlock() + if !wa.previousOwnLIDs.Add(jid) { + return + } + zerolog.Ctx(ctx).Info().Stringer("lid", jid).Msg("Found previous own LID in history sync") + wa.UserLogin.Metadata.(*waid.UserLoginMetadata).PreviousOwnLIDs = wa.previousOwnLIDs.AsList() + err := wa.UserLogin.Save(ctx) + if err != nil { + zerolog.Ctx(ctx).Err(err).Msg("Failed to save user login after adding previous own LID") + } } func (wa *WhatsAppClient) GetLID() types.JID { diff --git a/pkg/connector/commands.go b/pkg/connector/commands.go index 037d24e2..120d05ee 100644 --- a/pkg/connector/commands.go +++ b/pkg/connector/commands.go @@ -148,13 +148,7 @@ func fnSync(ce *commands.Event) { case "appstate": names := appstate.AllPatchNames[:] if len(ce.Args) > 1 { - names = exslices.CastFuncFilter(ce.Args[1:], func(name string) (appstate.WAPatchName, bool) { - if !slices.Contains(appstate.AllPatchNames[:], appstate.WAPatchName(name)) { - ce.Reply("Invalid app state name `%s`", name) - return "", false - } - return appstate.WAPatchName(name), true - }) + names = parseAppStateNames(ce, ce.Args[1:]) } for _, name := range names { err := wa.Client.FetchAppState(ce.Ctx, name, true, false) @@ -174,6 +168,50 @@ func fnSync(ce *commands.Event) { } } +func parseAppStateNames(ce *commands.Event, args []string) []appstate.WAPatchName { + return exslices.CastFuncFilter(args, func(name string) (appstate.WAPatchName, bool) { + if !slices.Contains(appstate.AllPatchNames[:], appstate.WAPatchName(name)) { + ce.Reply("Invalid app state name `%s`", name) + return "", false + } + return appstate.WAPatchName(name), true + }) +} + +var cmdResetAppState = &commands.FullHandler{ + Func: fnResetAppState, + Name: "reset-appstate", + Help: commands.HelpMeta{ + Section: commands.HelpSectionAdmin, + Description: "Ask your phone to reset broken app state. This logs out all linked devices.", + Args: "", + }, + RequiresLogin: true, +} + +func fnResetAppState(ce *commands.Event) { + login := ce.User.GetDefaultLogin() + if login == nil { + ce.Reply("Login not found") + return + } + if len(ce.Args) == 0 { + ce.Reply("Usage: `$cmdprefix reset-appstate `\n\nThis logs out all linked devices, including the bridge. You'll have to log in again afterwards.") + return + } + names := parseAppStateNames(ce, ce.Args) + if len(names) != len(ce.Args) { + return + } + wa := login.Client.(*WhatsAppClient) + _, err := wa.Client.SendPeerMessage(ce.Ctx, whatsmeow.BuildFatalAppStateExceptionNotification(names...)) + if err != nil { + ce.Reply("Failed to send app state reset request: %v", err) + return + } + ce.Reply("Sent app state reset request. Your phone should log out all linked devices shortly, after which you can log in again.") +} + var cmdInviteLink = &commands.FullHandler{ Func: fnInviteLink, Name: "invite-link", diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index d7261d3a..80b9d95e 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -110,7 +110,7 @@ func (wa *WhatsAppConnector) Init(bridge *bridgev2.Bridge) { wa.DB = wadb.New(bridge.ID, bridge.DB.Database, bridge.Log.With().Str("db_section", "whatsapp").Logger()) wa.MsgConv.DB = wa.DB wa.Bridge.Commands.(*commands.Processor).AddHandlers( - cmdAccept, cmdSync, cmdInviteLink, cmdResolveLink, cmdJoin, + cmdAccept, cmdSync, cmdResetAppState, cmdInviteLink, cmdResolveLink, cmdJoin, ) wa.mediaEditCache = make(MediaEditCache) wa.unmigratedDMs = exsync.NewSet[networkid.PortalKey]() @@ -164,11 +164,19 @@ func (wa *WhatsAppConnector) Start(ctx context.Context) error { if err != nil { return fmt.Errorf("failed to migrate to LID DMs: %w", err) } + err = wa.migrateMetaAIPortals(ctx) + if err != nil { + return fmt.Errorf("failed to migrate Meta AI portals: %w", err) + } go func() { err = wa.syncMismatchingGhosts(wa.Bridge.BackgroundCtx) if err != nil { wa.Bridge.Log.Err(err).Msg("Failed to sync mismatching ghosts") } + err = wa.removeRejoinedPNGhosts(wa.Bridge.BackgroundCtx) + if err != nil { + wa.Bridge.Log.Err(err).Msg("Failed to remove rejoined PN ghosts") + } }() return nil } @@ -235,7 +243,16 @@ func (wa *WhatsAppConnector) GenerateTransactionID(_ id.UserID, _ id.RoomID, _ e } func (wa *WhatsAppConnector) ResetHTTPTransport() { - // No-op for now, whatsmeow doesn't use the shared transport config yet + for _, login := range wa.Bridge.GetAllCachedUserLogins() { + client := login.Client.(*WhatsAppClient).Client + if client == nil { + continue + } + err := wa.updateProxy(login.Log.WithContext(wa.Bridge.BackgroundCtx), client, false) + if err != nil { + login.Log.Err(err).Msg("Failed to recreate HTTP clients") + } + } } func (wa *WhatsAppConnector) ResetNetworkConnections() { diff --git a/pkg/connector/directmedia.go b/pkg/connector/directmedia.go index b86f7dcf..03ef3984 100644 --- a/pkg/connector/directmedia.go +++ b/pkg/connector/directmedia.go @@ -164,7 +164,19 @@ func (wa *WhatsAppConnector) downloadStickerDirectMedia(ctx context.Context, par return nil, fmt.Errorf("no WhatsApp client found on login %s", parsedID.UserLogin) } sticker, err := wa.MsgConv.GetCachedSticker(ctx, waClient.Client, parsedID.Sticker.PackID, parsedID.Sticker.FileHash) - if err != nil { + var httpErr whatsmeow.DownloadHTTPError + if errors.As(err, &httpErr) { + switch httpErr.StatusCode { + case http.StatusNotFound, http.StatusGone: + return nil, mautrix.MNotFound.WithMessage("Sticker pack not found") + case http.StatusUnauthorized, http.StatusForbidden: + return nil, mautrix.MForbidden.WithMessage("WhatsApp denied access to the sticker pack") + case http.StatusTooManyRequests: + return nil, mautrix.MLimitExceeded.WithMessage("Rate limited by WhatsApp while fetching the sticker pack").WithCanRetry(true) + default: + return nil, mautrix.MUnknown.WithMessage("WhatsApp returned HTTP %d while fetching the sticker pack", httpErr.StatusCode).WithStatus(http.StatusBadGateway).WithCanRetry(true) + } + } else if err != nil { return nil, err } else if sticker == nil { return nil, mautrix.MNotFound.WithMessage("Sticker not found in pack") diff --git a/pkg/connector/errors.go b/pkg/connector/errors.go index 1ee1b205..38433d6c 100644 --- a/pkg/connector/errors.go +++ b/pkg/connector/errors.go @@ -21,11 +21,16 @@ import ( "errors" "fmt" "net/http" + "strconv" + "strings" + "time" "go.mau.fi/whatsmeow" "maunium.net/go/mautrix" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/event" + + "go.mau.fi/mautrix-whatsapp/pkg/waid" ) // wrapConversionError turns a failure to convert a Matrix event into a message status. Nothing has been @@ -85,6 +90,50 @@ func wrapWhatsAppError(err error) error { } } +// wrapSendError turns an error from sending a message to WhatsApp into a message status. +func (wa *WhatsAppClient) wrapSendError(err error) error { + switch sendErrorCode(err) { + case 403: + return bridgev2.WrapErrorInStatus(err). + WithMessage("you don't have permission to do that on WhatsApp"). + WithErrorReason(event.MessageStatusNoPermission). + WithStatus(event.MessageStatusFail). + WithIsCertain(true). + WithSendNotice(true) + case 463: + msg := "WhatsApp has temporarily restricted your account from messaging new contacts" + if until := wa.UserLogin.Metadata.(*waid.UserLoginMetadata).ReachoutTimelockUntil; until.After(time.Now()) { + msg = fmt.Sprintf("WhatsApp has restricted your account from messaging new contacts until %s", until.UTC().Format("Jan 2, 2006 at 3:04 PM MST")) + } + return bridgev2.WrapErrorInStatus(err). + WithMessage(msg). + WithErrorReason(event.MessageStatusNoPermission). + WithStatus(event.MessageStatusFail). + WithIsCertain(true). + WithSendNotice(true) + case 421, 479: + return bridgev2.WrapErrorInStatus(err). + WithMessage("WhatsApp rejected the message"). + WithErrorReason(event.MessageStatusNetworkError). + WithStatus(event.MessageStatusFail). + WithIsCertain(true). + WithSendNotice(true) + default: + return wrapWhatsAppError(err) + } +} + +// sendErrorCode returns the code WhatsApp rejected a message with, which whatsmeow only includes in the +// error text. +func sendErrorCode(err error) int { + if !errors.Is(err, whatsmeow.ErrServerReturnedError) { + return 0 + } + _, codeStr, _ := strings.Cut(err.Error(), whatsmeow.ErrServerReturnedError.Error()+" ") + code, _ := strconv.Atoi(codeStr) + return code +} + // wrapWhatsAppRespError turns an error from a whatsmeow request made during login or provisioning into // an HTTP error, keeping the original error in the chain. Clients retry 429 and 502-504 automatically, // which isn't wanted for provisioning requests, so those statuses aren't used. diff --git a/pkg/connector/events.go b/pkg/connector/events.go index 481d0d73..7f906e89 100644 --- a/pkg/connector/events.go +++ b/pkg/connector/events.go @@ -474,7 +474,9 @@ func (evt *WAMediaRetry) getRealSender() types.JID { func (evt *WAMediaRetry) GetSender() bridgev2.EventSender { realSender := pickLID(evt.getRealSender(), evt.senderLID) - return evt.wa.makeEventSender(evt.wa.Main.Bridge.BackgroundCtx, realSender) + sender := evt.wa.makeEventSender(evt.wa.Main.Bridge.BackgroundCtx, realSender) + sender.ForceEditOrigSender = true + return sender } func (evt *WAMediaRetry) GetTargetMessage() networkid.MessageID { diff --git a/pkg/connector/handlematrix.go b/pkg/connector/handlematrix.go index fec40c3c..84198506 100644 --- a/pkg/connector/handlematrix.go +++ b/pkg/connector/handlematrix.go @@ -107,6 +107,8 @@ func (wa *WhatsAppClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2 var ErrBroadcastSendDisabled = bridgev2.WrapErrorInStatus(errors.New("sending status messages is disabled")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported).WithStatus(event.MessageStatusFail) var ErrBroadcastReactionUnsupported = bridgev2.WrapErrorInStatus(errors.New("reacting to status messages is not currently supported")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported).WithStatus(event.MessageStatusFail) var ErrNotCommunityAdmin = bridgev2.WrapErrorInStatus(errors.New("only community admins can send announcements")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusNoPermission).WithStatus(event.MessageStatusFail) +var ErrEmptyGroupName = bridgev2.WrapErrorInStatus(errors.New("WhatsApp group names can't be empty")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported).WithStatus(event.MessageStatusFail) +var ErrAddBlockedByPrivacy = bridgev2.WrapErrorInStatus(errors.New("this user's privacy settings don't allow adding them to groups")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusNoPermission).WithStatus(event.MessageStatusFail) func (wa *WhatsAppClient) handleConvertedMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage, waMsg *waE2E.Message, req *whatsmeow.SendRequestExtra) (*bridgev2.MatrixMessageResponse, error) { if req == nil { @@ -139,7 +141,7 @@ func (wa *WhatsAppClient) handleConvertedMatrixMessage(ctx context.Context, msg if errors.Is(err, whatsmeow.ErrNoPermissionInGroup) { return nil, ErrNotCommunityAdmin } - return nil, wrapWhatsAppError(err) + return nil, wa.wrapSendError(err) } var pickedMessageID networkid.MessageID if resp.Sender == wa.GetLID() { @@ -218,7 +220,7 @@ func (wa *WhatsAppClient) HandleMatrixReaction(ctx context.Context, msg *bridgev Metadata: &waid.ReactionMetadata{ SenderDeviceID: wa.JID.Device, }, - }, wrapWhatsAppError(err) + }, wa.wrapSendError(err) } func (wa *WhatsAppClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) (retErr error) { @@ -248,7 +250,7 @@ func (wa *WhatsAppClient) HandleMatrixReactionRemove(ctx context.Context, msg *b defer wa.mcTrack(msg, time.Now(), &retErr) resp, err := wa.Client.SendMessage(ctx, portalJID, reactionMsg, extra) zerolog.Ctx(ctx).Trace().Any("response", resp).Msg("WhatsApp reaction response") - return wrapWhatsAppError(err) + return wa.wrapSendError(err) } func (wa *WhatsAppClient) HandleMatrixEdit(ctx context.Context, edit *bridgev2.MatrixEdit) (retErr error) { @@ -288,7 +290,7 @@ func (wa *WhatsAppClient) HandleMatrixEdit(ctx context.Context, edit *bridgev2.M ID: editID, }) log.Trace().Any("response", resp).Msg("WhatsApp edit response") - return wrapWhatsAppError(err) + return wa.wrapSendError(err) } func (wa *WhatsAppClient) HandleMatrixMessageRemove(ctx context.Context, msg *bridgev2.MatrixMessageRemove) (retErr error) { @@ -313,11 +315,11 @@ func (wa *WhatsAppClient) HandleMatrixMessageRemove(ctx context.Context, msg *br resp, err := wa.Client.SendMessage(ctx, portalJID, revokeMessage, extra) log.Trace().Any("response", resp).Msg("WhatsApp delete response") - return wrapWhatsAppError(err) + return wa.wrapSendError(err) } func (wa *WhatsAppClient) HandleMatrixReadReceipt(ctx context.Context, receipt *bridgev2.MatrixReadReceipt) (retErr error) { - if !receipt.ReadUpTo.After(receipt.LastRead) { + if receipt.Receipt.IsThread() || !receipt.ReadUpTo.After(receipt.LastRead) { return nil } if receipt.LastRead.IsZero() { @@ -483,6 +485,8 @@ func (wa *WhatsAppClient) HandleMatrixMembership(ctx context.Context, msg *bridg return nil, wrapWhatsAppError(err) } else if len(resp) == 0 { return nil, fmt.Errorf("no response for participant change") + } else if resp[0].Error == 403 && action == whatsmeow.ParticipantChangeAdd { + return nil, ErrAddBlockedByPrivacy } else if resp[0].Error != 0 { return nil, fmt.Errorf("failed to change participant: code %d", resp[0].Error) } @@ -501,6 +505,8 @@ func (wa *WhatsAppClient) HandleMatrixRoomName(ctx context.Context, msg *bridgev if msg.Portal.RoomType == database.RoomTypeDM { return false, fmt.Errorf("cannot set room name for DM") + } else if msg.Content.Name == "" { + return false, ErrEmptyGroupName } defer wa.mcTrack(msg, time.Now(), &retErr) diff --git a/pkg/connector/handlewhatsapp.go b/pkg/connector/handlewhatsapp.go index e7a9b65d..e6174b92 100644 --- a/pkg/connector/handlewhatsapp.go +++ b/pkg/connector/handlewhatsapp.go @@ -237,7 +237,11 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) (success bool) { }) wa.notifyOfflineSyncWaiter(fmt.Errorf("stream error: %s", message)) case *events.StreamReplaced: - wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Error: WAStreamReplaced}) + wa.UserLogin.BridgeState.Send(status.BridgeState{ + StateEvent: status.StateUnknownError, + Error: WAStreamReplaced, + BlockAutoReconnect: true, + }) wa.notifyOfflineSyncWaiter(fmt.Errorf("stream replaced")) case *events.KeepAliveTimeout: wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WAKeepaliveTimeout}) @@ -253,7 +257,11 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) (success bool) { wa.notifyOfflineSyncWaiter(fmt.Errorf("connection failure: %s (%s)", evt.Reason, evt.Message)) case *events.ClientOutdated: wa.UserLogin.Log.Error().Msg("Got a client outdated connect failure. The bridge is likely out of date, please update immediately.") - wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Error: WAClientOutdated}) + wa.UserLogin.BridgeState.Send(status.BridgeState{ + StateEvent: status.StateUnknownError, + Error: WAClientOutdated, + BlockAutoReconnect: true, + }) wa.notifyOfflineSyncWaiter(fmt.Errorf("client outdated")) case *events.TemporaryBan: wa.UserLogin.BridgeState.Send(status.BridgeState{ @@ -366,6 +374,14 @@ func (wa *WhatsAppClient) handleWAMessage(ctx context.Context, evt *events.Messa if assocType := messageAssoc.GetAssociationType(); assocType == waE2E.MessageAssociation_HD_IMAGE_DUAL_UPLOAD || assocType == waE2E.MessageAssociation_HD_VIDEO_DUAL_UPLOAD { parentKey := messageAssoc.GetParentMessageKey() protocolMsg, shouldHideEdit := makeHDMediaReplacementEdit(evt.Message, parentKey) + if protocolMsg.GetEditedMessage() == nil { + wa.UserLogin.Log.Debug(). + Str("message_id", evt.Info.ID). + Str("parent_id", parentKey.GetID()). + Stringer("assoc_type", assocType). + Msg("Ignoring HD replacement message without replacement content") + return + } dontRenderEdited = shouldHideEdit wa.UserLogin.Log.Debug(). Str("message_id", evt.Info.ID). @@ -514,6 +530,10 @@ func (wa *WhatsAppClient) handleWAReceipt(ctx context.Context, evt *events.Recei } else if evt.Chat.Server == types.NewsletterServer { messageSender = evt.Chat } + var messageSenderAlt types.JID + if evt.MessageSender.IsEmpty() && evt.Chat.Server == types.BotServer { + messageSenderAlt = wa.JID + } var chatAlt types.JID if evt.Chat.Server == types.DefaultUserServer { chatLID, _ := wa.GetStore().LIDs.GetLIDForPN(ctx, evt.Chat) @@ -527,6 +547,9 @@ func (wa *WhatsAppClient) handleWAReceipt(ctx context.Context, evt *events.Recei if !chatAlt.IsEmpty() { targets = append(targets, waid.MakeMessageID(chatAlt, messageSender, id)) } + if !messageSenderAlt.IsEmpty() { + targets = append(targets, waid.MakeMessageID(evt.Chat, messageSenderAlt, id)) + } } senderLID := evt.Sender if senderLID.Server == types.DefaultUserServer && !evt.SenderAlt.IsEmpty() { @@ -955,7 +978,9 @@ func (wa *WhatsAppClient) handleWAAppStateSyncComplete(ctx context.Context, evt defer wa.appStateRecoveryLock.Unlock() meta := wa.UserLogin.Metadata.(*waid.UserLoginMetadata) if ts, exists := meta.AppStateRecoveryAttempted[evt.Name]; exists { - delete(wa.appStateFullSyncAttempted, evt.Name) + if !evt.Recovery { + delete(wa.appStateFullSyncAttempted, evt.Name) + } delete(meta.AppStateRecoveryAttempted, evt.Name) err := wa.UserLogin.Save(ctx) if err != nil { @@ -993,7 +1018,7 @@ func (wa *WhatsAppClient) handleWAAppStateSyncError(ctx context.Context, evt *ev return } if !evt.FullSync { - if !lastFullSync.IsZero() { + if !lastFullSync.IsZero() && time.Since(lastFullSync) < 48*time.Hour { log.Debug(). Err(evt.Error). Time("last_full_sync_attempt", lastFullSync). diff --git a/pkg/connector/lidmigrate.go b/pkg/connector/lidmigrate.go index ec1c700e..4295be63 100644 --- a/pkg/connector/lidmigrate.go +++ b/pkg/connector/lidmigrate.go @@ -18,6 +18,7 @@ package connector import ( "context" + "errors" "fmt" "time" @@ -25,9 +26,11 @@ import ( "go.mau.fi/util/dbutil" "go.mau.fi/whatsmeow/types" + "maunium.net/go/mautrix" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/networkid" "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" "go.mau.fi/mautrix-whatsapp/pkg/waid" ) @@ -47,6 +50,8 @@ func (wa *WhatsAppClient) FindAltTargetMessage(ctx context.Context, targetMsg ne if err != nil { return nil, err } + } else if parsed.Chat == types.NewMetaAIJID { + altChat = types.MetaAIJID } if !altSender.IsEmpty() { altSenderID := *parsed @@ -58,6 +63,8 @@ func (wa *WhatsAppClient) FindAltTargetMessage(ctx context.Context, targetMsg ne altChatID.Chat = altChat if altSender.Server == types.DefaultUserServer { altChatID.Sender = altSender + } else if parsed.Sender == types.NewMetaAIJID { + altChatID.Sender = types.MetaAIJID } alts = append(alts, altChatID.String()) } @@ -211,6 +218,30 @@ func (wa *WhatsAppConnector) migrateToLIDDMs(ctx context.Context) error { return nil } +func (wa *WhatsAppConnector) migrateMetaAIPortals(ctx context.Context) error { + if wa.Bridge.Background || wa.Bridge.DB.KV.Get(ctx, "whatsapp_meta_ai_portals_migrated") == "true" { + return nil + } + log := zerolog.Ctx(ctx).With().Str("action", "migrate meta ai portals").Logger() + const findMetaAIPortals = "SELECT id, receiver FROM portal WHERE bridge_id=$1 AND id=$2" + portalKeys, err := scanPortalKey.NewRowIter(wa.Bridge.DB.Query(ctx, findMetaAIPortals, wa.Bridge.ID, waid.MakePortalID(types.MetaAIJID))).AsList() + if err != nil { + return fmt.Errorf("failed to get Meta AI portals: %w", err) + } + for _, key := range portalKeys { + res, err := wa.reIDPhoneDMToLID(ctx, types.MetaAIJID, types.NewMetaAIJID, key.Receiver) + if err != nil { + return fmt.Errorf("failed to re-ID Meta AI portal for %s: %w", key.Receiver, err) + } + log.Info(). + Str("receiver", string(key.Receiver)). + Stringer("result", res). + Msg("Re-ID'd Meta AI phone number portal") + } + wa.Bridge.DB.KV.Set(ctx, "whatsapp_meta_ai_portals_migrated", "true") + return nil +} + func (wa *WhatsAppConnector) syncMismatchingGhosts(ctx context.Context) error { if wa.Bridge.Background || wa.Bridge.DB.KV.Get(ctx, "whatsapp_lid_avatars_resynced") == "true" { return nil @@ -267,3 +298,84 @@ func (wa *WhatsAppConnector) syncMismatchingGhosts(ctx context.Context) error { wa.Bridge.DB.KV.Set(ctx, "whatsapp_lid_avatars_resynced", "true") return nil } + +func (wa *WhatsAppConnector) removeRejoinedPNGhosts(ctx context.Context) error { + if wa.Bridge.Background || wa.Bridge.DB.KV.Get(ctx, "whatsapp_lid_dm_pn_ghosts_removed_v2") == "true" { + return nil + } + stateGetter, ok := wa.Bridge.Matrix.(bridgev2.MatrixConnectorWithArbitraryRoomState) + if !ok { + return nil + } + log := zerolog.Ctx(ctx).With().Str("action", "remove rejoined pn ghosts").Logger() + const findLIDPortals = "SELECT id, mxid FROM portal WHERE bridge_id=$1 AND room_type='dm' AND id LIKE '%@lid' AND mxid IS NOT NULL" + lidPortals, err := scanLIDPortal.NewRowIter(wa.Bridge.DB.Query(ctx, findLIDPortals, wa.Bridge.ID)).AsList() + if err != nil { + return fmt.Errorf("failed to get LID portals: %w", err) + } + var removed, failed int + for _, portal := range lidPortals { + lid, err := waid.ParsePortalID(portal.ID) + if err != nil || lid.Server != types.HiddenUserServer { + continue + } + pn, err := wa.DeviceStore.LIDMap.GetPNForLID(ctx, lid) + if err != nil { + return fmt.Errorf("failed to get PN for LID portal %s: %w", portal.ID, err) + } else if pn.IsEmpty() { + continue + } + pnUserID := waid.MakeUserID(pn) + memberEvt, err := stateGetter.GetStateEvent(ctx, portal.MXID, event.StateMember, wa.Bridge.Matrix.FormatGhostMXID(pnUserID).String()) + if errors.Is(err, mautrix.MNotFound) || errors.Is(err, mautrix.MForbidden) { + continue + } else if err != nil { + log.Warn().Err(err).Stringer("room_id", portal.MXID).Msg("Failed to get PN ghost membership") + failed++ + continue + } else if memberEvt == nil || memberEvt.Content.AsMember().Membership != event.MembershipJoin { + continue + } + pnGhost, err := wa.Bridge.GetGhostByID(ctx, pnUserID) + if err != nil { + return fmt.Errorf("failed to get PN ghost for %s: %w", pn, err) + } + _, err = pnGhost.Intent.SendState(ctx, portal.MXID, event.StateMember, pnGhost.Intent.GetMXID().String(), &event.Content{ + Parsed: &event.MemberEventContent{Membership: event.MembershipLeave, Reason: "Migrating to LIDs"}, + Raw: map[string]any{"com.beeper.exclude_from_timeline": true}, + }, time.Time{}) + if err != nil { + log.Err(err).Stringer("room_id", portal.MXID).Stringer("pn", pn).Msg("Failed to remove PN ghost from LID DM") + if !errors.Is(err, mautrix.MForbidden) && !errors.Is(err, mautrix.MNotFound) { + failed++ + } + continue + } + log.Debug(). + Stringer("room_id", portal.MXID). + Stringer("pn", pn). + Stringer("join_sender", memberEvt.Sender). + Time("join_ts", time.UnixMilli(memberEvt.Timestamp)). + Msg("Removed PN ghost from LID DM") + removed++ + } + log.Info(). + Int("removed_ghosts", removed). + Int("failed", failed). + Int("total_lid_portals", len(lidPortals)). + Msg("Finished removing rejoined PN ghosts from LID DMs") + if failed == 0 && ctx.Err() == nil { + wa.Bridge.DB.KV.Set(ctx, "whatsapp_lid_dm_pn_ghosts_removed_v2", "true") + } + return nil +} + +type lidPortal struct { + ID networkid.PortalID + MXID id.RoomID +} + +var scanLIDPortal = dbutil.ConvertRowFn[lidPortal](func(row dbutil.Scannable) (portal lidPortal, err error) { + err = row.Scan(&portal.ID, &portal.MXID) + return +}) diff --git a/pkg/connector/login.go b/pkg/connector/login.go index 387d780c..ae57ac1a 100644 --- a/pkg/connector/login.go +++ b/pkg/connector/login.go @@ -137,6 +137,7 @@ var ( _ bridgev2.LoginProcessDisplayAndWait = (*WALogin)(nil) _ bridgev2.LoginProcessUserInput = (*WALogin)(nil) _ bridgev2.LoginProcessWithOverride = (*WALogin)(nil) + _ bridgev2.LoginProcessWithParams = (*WALogin)(nil) _ bridgev2.LoginProcessWebAuthn = (*WALogin)(nil) ) @@ -144,6 +145,14 @@ const LoginConnectWait = 30 * time.Second const LoginPairPhoneWait = 30 * time.Second func (wl *WALogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) { + return wl.StartWithParams(ctx, bridgev2.LoginStartParams{}) +} + +func (wl *WALogin) StartWithOverride(ctx context.Context, old *bridgev2.UserLogin) (*bridgev2.LoginStep, error) { + return wl.StartWithParams(ctx, bridgev2.LoginStartParams{Override: old}) +} + +func (wl *WALogin) StartWithParams(ctx context.Context, params bridgev2.LoginStartParams) (*bridgev2.LoginStep, error) { wl.Main.firstClientConnectOnce.Do(wl.Main.onFirstClientConnect) device := wl.Main.DeviceStore.NewDevice() wl.Client = whatsmeow.NewClient(device, waLog.Zerolog(wl.Log)) @@ -153,8 +162,20 @@ func (wl *WALogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) { if err := wl.Main.updateProxy(ctx, wl.Client, true); err != nil { return nil, err } + if params.WebSocketHTTP != nil { + wl.Client.SetPreLoginHTTPClient(&http.Client{Transport: params.WebSocketHTTP}) + } if wl.PhoneCode { + if params.Override != nil { + phoneNumber := fmt.Sprintf("+%s", params.Override.ID) + wl.Log.Debug(). + Str("phone_number", phoneNumber). + Msg("Auto-submitting phone number for relogin") + return wl.SubmitUserInput(ctx, map[string]string{ + "phone_number": phoneNumber, + }) + } return &bridgev2.LoginStep{ Type: bridgev2.LoginStepTypeUserInput, StepID: LoginStepIDPhoneNumber, @@ -184,20 +205,6 @@ func (wl *WALogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) { return makeQRStep(wl.QRs[0]), nil } -func (wl *WALogin) StartWithOverride(ctx context.Context, old *bridgev2.UserLogin) (*bridgev2.LoginStep, error) { - step, err := wl.Start(ctx) - if err == nil && step != nil && old != nil && step.StepID == LoginStepIDPhoneNumber { - phoneNumber := fmt.Sprintf("+%s", old.ID) - wl.Log.Debug(). - Str("phone_number", phoneNumber). - Msg("Auto-submitting phone number for relogin") - return wl.SubmitUserInput(ctx, map[string]string{ - "phone_number": phoneNumber, - }) - } - return step, err -} - func (wl *WALogin) SubmitUserInput(ctx context.Context, input map[string]string) (*bridgev2.LoginStep, error) { err := wl.Client.Connect() if err != nil { @@ -281,6 +288,7 @@ func (wl *WALogin) handleEvent(rawEvt any) { wl.QRs[i] = strings.Replace(code, evt.OldSecret, evt.NewSecret, 1) } wl.ADVRotate.Set() + return case *events.QRScannedWithoutMultidevice: wl.Log.Error().Msg("QR code scanned without multidevice enabled") wl.LoginError = ErrLoginMultideviceNotEnabled @@ -316,6 +324,7 @@ func (wl *WALogin) handleEvent(rawEvt any) { wl.LoginError = ErrUnexpectedDisconnect case *events.ManualLoginReconnect: wl.Received515.Set() + return default: wl.Log.Warn().Type("event_type", evt).Msg("Got unexpected event") return @@ -364,9 +373,14 @@ func (wl *WALogin) Wait(ctx context.Context) (*bridgev2.LoginStep, error) { case <-wl.PasskeyRequest.GetChan(): return wl.makePasskeyStep() case <-wl.ADVRotate.GetChan(): - wl.Log.Debug().Msg("ADV secret was rotated, returning new QR immediately") wl.ADVRotate.Clear() - return makeQRStep(wl.QRs[nextIndex]), nil + if currentIndex < 0 || currentIndex >= len(wl.QRs) { + wl.Log.Debug().Msg("ADV secret was rotated, but no QRs are left to return") + wl.Cancel() + return nil, ErrLoginTimeout + } + wl.Log.Debug().Msg("ADV secret was rotated, returning current QR immediately") + return makeQRStep(wl.QRs[currentIndex]), nil case <-wl.LoginComplete.GetChan(): // continue } diff --git a/pkg/connector/muse.go b/pkg/connector/muse.go index ee6b04d9..4a589ce7 100644 --- a/pkg/connector/muse.go +++ b/pkg/connector/muse.go @@ -4,7 +4,6 @@ import ( "context" "encoding/base64" "encoding/json" - "errors" "fmt" "runtime/debug" "time" @@ -57,9 +56,9 @@ func (wa *WhatsAppClient) resyncWASARootSecrets(ctx context.Context) { success := true if err := wa.Client.FetchAppState(ctx, appstate.WAPatchRegularHigh, true, false); err != nil { log.Err(err).Msg("Failed to resync WASA root secrets") - // Mark the sync as completed on mismatching LTHash errors, because those can get permanently stuck - // (retrying won't help). In case it's not stuck, a single call should be enough to request a resync. - if !errors.Is(err, appstate.ErrMismatchingLTHash) || ctx.Err() != nil { + // Mark the sync as completed on errors too, because some get permanently stuck (mismatching LTHash, + // expired patch blobs), and a failed full sync already requests app state recovery from the phone. + if ctx.Err() != nil { return } success = false diff --git a/pkg/connector/proxy.go b/pkg/connector/proxy.go index fe1d7ff5..8bd6d658 100644 --- a/pkg/connector/proxy.go +++ b/pkg/connector/proxy.go @@ -54,23 +54,38 @@ func (wa *WhatsAppConnector) getProxy(ctx context.Context, reason string) (strin } func (wa *WhatsAppConnector) updateProxy(ctx context.Context, client *whatsmeow.Client, isLogin bool) error { - if wa.Config.ProxyOnlyLogin && !isLogin { - return nil + var proxy string + if !wa.Config.ProxyOnlyLogin || isLogin { + reason := "connect" + if isLogin { + reason = "login" + } + var err error + proxy, err = wa.getProxy(ctx, reason) + if err != nil { + return fmt.Errorf("failed to get proxy address: %w", err) + } } - reason := "connect" - if isLogin { - reason = "login" + settings := wa.Bridge.GetHTTPClientSettings().WithGlobalTimeout(0) + if settings.HTTPProxy == nil { + settings.HTTPProxy = http.ProxyFromEnvironment } - if proxy, err := wa.getProxy(ctx, reason); err != nil { - return fmt.Errorf("failed to get proxy address: %w", err) - } else if proxy == "" { - return nil - } else if err = client.SetProxyAddress(proxy, whatsmeow.SetProxyOptions{ - OnlyLogin: wa.Config.ProxyOnlyLogin, - NoMedia: wa.Config.ProxyOnlyLogin, - }); err != nil { - return fmt.Errorf("failed to set proxy address: %w", err) + proxySettings := settings + if proxy != "" { + var err error + proxySettings, err = settings.WithProxy(proxy) + if err != nil { + return fmt.Errorf("failed to set proxy address: %w", err) + } + } + client.SetPreLoginHTTPClient(proxySettings.Compile()) + if wa.Config.ProxyOnlyLogin { + proxySettings = settings + } + client.SetWebsocketHTTPClient(proxySettings.Compile()) + client.SetMediaHTTPClient(proxySettings.WithResponseHeaderTimeout(0).Compile()) + if proxy != "" { + zerolog.Ctx(ctx).Debug().Msg("Enabled proxy") } - zerolog.Ctx(ctx).Debug().Msg("Enabled proxy") return nil } diff --git a/pkg/connector/userinfo.go b/pkg/connector/userinfo.go index c78b0a8e..387971e9 100644 --- a/pkg/connector/userinfo.go +++ b/pkg/connector/userinfo.go @@ -29,12 +29,15 @@ var ResyncLoopInterval = 4 * time.Hour var ResyncJitterSeconds = 3600 func (wa *WhatsAppClient) EnqueueGhostResync(ghost *bridgev2.Ghost) { + jid := waid.ParseUserID(ghost.ID) + if jid.Server == types.BotServer { + return + } lastSync := ghost.Metadata.(*waid.GhostMetadata).LastSync.Time - if lastSync.Add(ResyncMinInterval).After(time.Now()) { + if ghost.AvatarID != "" && lastSync.Add(ResyncMinInterval).After(time.Now()) { return } wa.resyncQueueLock.Lock() - jid := waid.ParseUserID(ghost.ID) if _, exists := wa.resyncQueue[jid]; !exists { wa.resyncQueue[jid] = resyncQueueItem{ghost: ghost} nextResyncIn := time.Until(wa.nextResync).String() @@ -116,14 +119,17 @@ func (wa *WhatsAppClient) doGhostResync(ctx context.Context, queue map[types.JID var ghostJIDs []types.JID var ghosts []*bridgev2.Ghost var portals []*bridgev2.Portal + selectedGhosts := make(map[types.JID]*bridgev2.Ghost) for jid, item := range queue { var lastSync time.Time + unknownAvatar := false if item.ghost != nil { lastSync = item.ghost.Metadata.(*waid.GhostMetadata).LastSync.Time + unknownAvatar = item.ghost.AvatarID == "" } else if item.portal != nil { lastSync = item.portal.Metadata.(*waid.PortalMetadata).LastSync.Time } - if lastSync.Add(ResyncMinInterval).After(time.Now()) { + if !unknownAvatar && lastSync.Add(ResyncMinInterval).After(time.Now()) { log.Debug(). Stringer("jid", jid). Time("last_sync", lastSync). @@ -131,12 +137,23 @@ func (wa *WhatsAppClient) doGhostResync(ctx context.Context, queue map[types.JID continue } if item.ghost != nil { - ghosts = append(ghosts, item.ghost) - ghostJIDs = append(ghostJIDs, jid) + selectedGhosts[jid] = item.ghost } else if item.portal != nil { portals = append(portals, item.portal) } } + for jid, ghost := range selectedGhosts { + if jid.Server == types.DefaultUserServer { + lid, err := wa.GetStore().LIDs.GetLIDForPN(ctx, jid) + if err != nil { + log.Warn().Err(err).Stringer("jid", jid).Msg("Failed to get LID for background sync") + } else if _, lidSelected := selectedGhosts[lid]; lidSelected { + continue + } + } + ghosts = append(ghosts, ghost) + ghostJIDs = append(ghostJIDs, jid) + } for _, portal := range portals { wa.UserLogin.QueueRemoteEvent(&simplevent.ChatResync{ EventMeta: simplevent.EventMeta{ @@ -165,7 +182,7 @@ func (wa *WhatsAppClient) doGhostResync(ctx context.Context, queue map[types.JID log.Warn().Stringer("jid", jid).Msg("Didn't get info for puppet in background sync") continue } - userInfo, err := wa.getUserInfo(ctx, jid, info.PictureID, info.PictureID != "" && string(ghost.AvatarID) != info.PictureID) + userInfo, err := wa.getUserInfo(ctx, jid, info.PictureID, ghost.AvatarID == "" || (info.PictureID != "" && string(ghost.AvatarID) != info.PictureID)) if err != nil { log.Err(err).Stringer("jid", jid).Msg("Failed to get user info for puppet in background sync") continue @@ -472,16 +489,19 @@ func (wa *WhatsAppClient) syncAltGhostWithInfo(ctx context.Context, jid types.JI } func makeInfoFromGhost(ghost *bridgev2.Ghost) *bridgev2.UserInfo { - return &bridgev2.UserInfo{ - Identifiers: ghost.Identifiers, - Name: &ghost.Name, - Avatar: &bridgev2.Avatar{ - ID: ghost.AvatarID, - Remove: ghost.AvatarID == "" || ghost.AvatarMXC == "", - MXC: ghost.AvatarMXC, - Hash: ghost.AvatarHash, - }, + info := &bridgev2.UserInfo{ + Identifiers: ghost.Identifiers, + Name: &ghost.Name, IsBot: &ghost.IsBot, ExtraProfile: ghost.ExtraProfile, } + if ghost.AvatarID != "" && ghost.AvatarID != "unauthorized" { + info.Avatar = &bridgev2.Avatar{ + ID: ghost.AvatarID, + Remove: ghost.AvatarMXC == "", + MXC: ghost.AvatarMXC, + Hash: ghost.AvatarHash, + } + } + return info } diff --git a/pkg/connector/wadb/upgrades/00-latest-schema.sql b/pkg/connector/wadb/upgrades/00-latest-schema.sql index 0f1fef94..c4172d09 100644 --- a/pkg/connector/wadb/upgrades/00-latest-schema.sql +++ b/pkg/connector/wadb/upgrades/00-latest-schema.sql @@ -1,4 +1,4 @@ --- v0 -> v10 (compatible with v3+): Latest revision +-- v0 -> v11 (compatible with v3+): Latest revision CREATE TABLE whatsapp_poll_option_id ( bridge_id TEXT NOT NULL, diff --git a/pkg/connector/wadb/upgrades/11-rearm-leftover-history-sync-backfill.sql b/pkg/connector/wadb/upgrades/11-rearm-leftover-history-sync-backfill.sql new file mode 100644 index 00000000..69e8b1e7 --- /dev/null +++ b/pkg/connector/wadb/upgrades/11-rearm-leftover-history-sync-backfill.sql @@ -0,0 +1,10 @@ +-- v11 (compatible with v3+): Re-arm backfill for chats with leftover history sync messages +UPDATE backfill_task +SET is_done=false, queue_done=false, dispatched_at=NULL, completed_at=NULL +WHERE (is_done OR queue_done) AND EXISTS ( + SELECT 1 + FROM whatsapp_history_sync_message msg + WHERE msg.bridge_id=backfill_task.bridge_id + AND msg.user_login_id=backfill_task.user_login_id + AND msg.chat_jid=backfill_task.portal_id +); diff --git a/pkg/msgconv/from-matrix.go b/pkg/msgconv/from-matrix.go index 192b8a6d..ba2adc23 100644 --- a/pkg/msgconv/from-matrix.go +++ b/pkg/msgconv/from-matrix.go @@ -27,12 +27,15 @@ import ( "image/color" "image/jpeg" "net/http" + "os" + "path/filepath" "slices" "strconv" "strings" "time" "github.com/rs/zerolog" + "go.mau.fi/util/exmime" "go.mau.fi/util/ffmpeg" "go.mau.fi/util/ptr" "go.mau.fi/util/random" @@ -515,6 +518,56 @@ func (mc *MessageConverter) convertToWebP(img []byte) ([]byte, int, error) { return webpBuffer.Bytes(), size, nil } +func convertUnsupportedVideoCodec(ctx context.Context, data []byte, mime string) ([]byte, string, error) { + if !ffmpeg.Supported() || !ffmpeg.ProbeSupported() { + return data, mime, nil + } + log := zerolog.Ctx(ctx) + tempdir, err := os.MkdirTemp("", "mautrix_whatsapp_video_*") + if err != nil { + log.Warn().Err(err).Msg("Failed to create temp dir for video, sending it as-is") + return data, mime, nil + } + defer os.RemoveAll(tempdir) + inputPath := filepath.Join(tempdir, "input"+exmime.ExtensionFromMimetype(mime)) + err = os.WriteFile(inputPath, data, 0600) + if err != nil { + log.Warn().Err(err).Msg("Failed to write video to temp file, sending it as-is") + return data, mime, nil + } + probe, err := ffmpeg.Probe(ctx, inputPath) + if err != nil { + log.Warn().Err(err).Msg("Failed to probe video, sending it as-is") + return data, mime, nil + } + videoSupported := !slices.ContainsFunc(probe.Streams, func(stream *ffmpeg.Stream) bool { + return stream.CodecType == "video" && stream.Disposition.AttachedPic == 0 && stream.CodecName != "h264" + }) + audioSupported := !slices.ContainsFunc(probe.Streams, func(stream *ffmpeg.Stream) bool { + return stream.CodecType == "audio" && stream.CodecName != "aac" + }) + if videoSupported && audioSupported { + return data, mime, nil + } + videoArgs := []string{"-c:v", "copy"} + if !videoSupported { + videoArgs = []string{ + "-pix_fmt", "yuv420p", "-c:v", "h264", "-b:v", "2M", + "-filter:v", "crop='floor(in_w/2)*2:floor(in_h/2)*2'", + } + } + outputPath := filepath.Join(tempdir, "output.mp4") + err = ffmpeg.ConvertPathWithDestination(ctx, inputPath, outputPath, nil, append(videoArgs, "-c:a", "aac", "-movflags", "+faststart"), false) + if err != nil { + return nil, mime, err + } + converted, err := os.ReadFile(outputPath) + if err != nil { + return nil, mime, err + } + return converted, "video/mp4", nil +} + func (mc *MessageConverter) getOriginalBridgedSticker(ctx context.Context, info *event.BridgedSticker) (*types.StickerPackItem, error) { if info == nil || info.Network != StickerSourceID || !strings.HasPrefix(info.PackURL, StickerPackURLPrefix) || info.ID == "" { return nil, nil @@ -609,7 +662,10 @@ func (mc *MessageConverter) reuploadFileToWhatsApp( case event.MsgVideo: switch mime { case "video/mp4", "video/3gpp": - // allowed + data, mime, err = convertUnsupportedVideoCodec(ctx, data, mime) + if err != nil { + return nil, nil, mime, fmt.Errorf("%w (%s to h264): %w", bridgev2.ErrMediaConvertFailed, mime, err) + } case "video/webm", "video/quicktime": sourceFormat := "webm" if mime == "video/quicktime" { diff --git a/pkg/msgconv/from-whatsapp.go b/pkg/msgconv/from-whatsapp.go index 1ff2e021..db18f35a 100644 --- a/pkg/msgconv/from-whatsapp.go +++ b/pkg/msgconv/from-whatsapp.go @@ -17,6 +17,7 @@ package msgconv import ( + "cmp" "context" "fmt" "html" @@ -92,7 +93,7 @@ func (mc *MessageConverter) getBasicUserInfo(ctx context.Context, user types.JID portal := getPortal(ctx) login := mc.Bridge.GetCachedUserLoginByID(waid.MakeUserLoginID(pnJID)) if login != nil && (portal.Receiver == "" || portal.Receiver == login.ID) { - return login.UserMXID, ghost.Name, nil + return login.UserMXID, cmp.Or(login.RemoteProfile.Name, ghost.Name), nil } } return ghost.Intent.GetMXID(), ghost.Name, nil diff --git a/pkg/msgconv/wa-location.go b/pkg/msgconv/wa-location.go index 4a1e043f..52f3f131 100644 --- a/pkg/msgconv/wa-location.go +++ b/pkg/msgconv/wa-location.go @@ -30,6 +30,40 @@ import ( "maunium.net/go/mautrix/event" ) +func formatCoordinates(lat, long float64) string { + latChar := 'N' + if lat < 0 { + latChar = 'S' + } + longChar := 'E' + if long < 0 { + longChar = 'W' + } + return fmt.Sprintf("%.4f° %c %.4f° %c", math.Abs(lat), latChar, math.Abs(long), longChar) +} + +func addLocationThumbnail(ctx context.Context, content *event.MessageEventContent, thumbnail []byte) { + if len(thumbnail) == 0 { + return + } + thumbnailMime := http.DetectContentType(thumbnail) + thumbnailURL, thumbnailFile, err := getIntent(ctx).UploadMedia(ctx, getPortal(ctx).MXID, thumbnail, "thumb.jpeg", thumbnailMime) + if err != nil { + return + } + cfg, _, _ := image.DecodeConfig(bytes.NewReader(thumbnail)) + content.Info = &event.FileInfo{ + ThumbnailInfo: &event.FileInfo{ + Size: len(thumbnail), + Width: cfg.Width, + Height: cfg.Height, + MimeType: thumbnailMime, + }, + ThumbnailURL: thumbnailURL, + ThumbnailFile: thumbnailFile, + } +} + func (mc *MessageConverter) convertLocationMessage(ctx context.Context, msg *waE2E.LocationMessage) (*bridgev2.ConvertedMessagePart, *waE2E.ContextInfo) { url := msg.GetURL() if len(url) == 0 { @@ -37,15 +71,7 @@ func (mc *MessageConverter) convertLocationMessage(ctx context.Context, msg *waE } name := msg.GetName() if len(name) == 0 { - latChar := 'N' - if msg.GetDegreesLatitude() < 0 { - latChar = 'S' - } - longChar := 'E' - if msg.GetDegreesLongitude() < 0 { - longChar = 'W' - } - name = fmt.Sprintf("%.4f° %c %.4f° %c", math.Abs(msg.GetDegreesLatitude()), latChar, math.Abs(msg.GetDegreesLongitude()), longChar) + name = formatCoordinates(msg.GetDegreesLatitude(), msg.GetDegreesLongitude()) } content := &event.MessageEventContent{ @@ -55,24 +81,7 @@ func (mc *MessageConverter) convertLocationMessage(ctx context.Context, msg *waE FormattedBody: fmt.Sprintf(`Location: %s
%s`, html.EscapeString(url), html.EscapeString(name), html.EscapeString(msg.GetAddress())), GeoURI: fmt.Sprintf("geo:%.5f,%.5f", msg.GetDegreesLatitude(), msg.GetDegreesLongitude()), } - - if len(msg.GetJPEGThumbnail()) > 0 { - thumbnailMime := http.DetectContentType(msg.GetJPEGThumbnail()) - thumbnailURL, thumbnailFile, err := getIntent(ctx).UploadMedia(ctx, getPortal(ctx).MXID, msg.GetJPEGThumbnail(), "thumb.jpeg", thumbnailMime) - if err == nil { - cfg, _, _ := image.DecodeConfig(bytes.NewReader(msg.GetJPEGThumbnail())) - content.Info = &event.FileInfo{ - ThumbnailInfo: &event.FileInfo{ - Size: len(msg.GetJPEGThumbnail()), - Width: cfg.Width, - Height: cfg.Height, - MimeType: thumbnailMime, - }, - ThumbnailURL: thumbnailURL, - ThumbnailFile: thumbnailFile, - } - } - } + addLocationThumbnail(ctx, content, msg.GetJPEGThumbnail()) return &bridgev2.ConvertedMessagePart{ Type: event.EventMessage, @@ -81,14 +90,40 @@ func (mc *MessageConverter) convertLocationMessage(ctx context.Context, msg *waE } func (mc *MessageConverter) convertLiveLocationMessage(ctx context.Context, msg *waE2E.LiveLocationMessage) (*bridgev2.ConvertedMessagePart, *waE2E.ContextInfo) { + if msg.DegreesLatitude == nil || msg.DegreesLongitude == nil { + content := &event.MessageEventContent{ + Body: "Started sharing live location", + MsgType: event.MsgNotice, + } + if len(msg.GetCaption()) > 0 { + content.Body += ": " + msg.GetCaption() + } + content.Body += "\n\nUse the WhatsApp app to see the location." + return &bridgev2.ConvertedMessagePart{ + Type: event.EventMessage, + Content: content, + }, msg.GetContextInfo() + } + + lat, long := msg.GetDegreesLatitude(), msg.GetDegreesLongitude() + url := fmt.Sprintf("https://maps.google.com/?q=%.5f,%.5f", lat, long) + name := formatCoordinates(lat, long) + const note = "Live updates are only visible in the WhatsApp app." + body := fmt.Sprintf("Live location: %s\n%s", name, url) + formattedBody := fmt.Sprintf(`Live location: %s`, html.EscapeString(url), html.EscapeString(name)) + if caption := msg.GetCaption(); len(caption) > 0 { + body += "\n" + caption + formattedBody += "
" + html.EscapeString(caption) + } content := &event.MessageEventContent{ - Body: "Started sharing live location", - MsgType: event.MsgNotice, + MsgType: event.MsgLocation, + Body: body + "\n\n" + note, + Format: event.FormatHTML, + FormattedBody: formattedBody + "

" + note, + GeoURI: fmt.Sprintf("geo:%.5f,%.5f", lat, long), } - if len(msg.GetCaption()) > 0 { - content.Body += ": " + msg.GetCaption() - } - content.Body += "\n\nUse the WhatsApp app to see the location." + addLocationThumbnail(ctx, content, msg.GetJPEGThumbnail()) + return &bridgev2.ConvertedMessagePart{ Type: event.EventMessage, Content: content, diff --git a/pkg/msgconv/wa-media.go b/pkg/msgconv/wa-media.go index 20a1a142..839f4c26 100644 --- a/pkg/msgconv/wa-media.go +++ b/pkg/msgconv/wa-media.go @@ -49,6 +49,9 @@ func (mc *MessageConverter) convertMediaMessage( isViewOnce bool, cachedPart *bridgev2.ConvertedMessagePart, ) (part *bridgev2.ConvertedMessagePart, contextInfo *waE2E.ContextInfo) { + if viewOnceMsg, ok := msg.(MediaMessageWithViewOnce); ok && viewOnceMsg.GetViewOnce() { + isViewOnce = true + } if mc.DisableViewOnce && isViewOnce { body := "You received a view once message. For added privacy, you can only open it on the WhatsApp app." if messageInfo.IsFromMe { @@ -91,7 +94,9 @@ func (mc *MessageConverter) convertMediaMessage( var err error portal := getPortal(ctx) idOverride := getEditTargetID(ctx) - preparedMedia.URL, err = portal.Bridge.Matrix.GenerateContentURI(ctx, waid.MakeMediaID(messageInfo, idOverride, portal.Receiver, getMediaIDVersion(msg))) + preparedMedia.URL, preparedMedia.File, err = portal.Bridge.GenerateContentURI( + ctx, portal.MXID, waid.MakeMediaID(messageInfo, idOverride, portal.Receiver, getMediaIDVersion(msg)), + ) if err != nil { panic(fmt.Errorf("failed to generate content URI: %w", err)) } @@ -230,6 +235,11 @@ type MediaMessageWithCaption interface { GetCaption() string } +type MediaMessageWithViewOnce interface { + MediaMessage + GetViewOnce() bool +} + type MediaMessageWithDimensions interface { MediaMessage GetHeight() uint32 @@ -253,10 +263,10 @@ func fixStickerDimensions(info *event.FileInfo) { info.Width = WhatsAppStickerSize info.Height = WhatsAppStickerSize } else if info.Width > info.Height { - info.Height /= info.Width / WhatsAppStickerSize + info.Height = info.Height * WhatsAppStickerSize / info.Width info.Width = WhatsAppStickerSize } else { - info.Width /= info.Height / WhatsAppStickerSize + info.Width = info.Width * WhatsAppStickerSize / info.Height info.Height = WhatsAppStickerSize } } diff --git a/pkg/msgconv/wa-rich-response.go b/pkg/msgconv/wa-rich-response.go index 41ac9ab9..e506a64e 100644 --- a/pkg/msgconv/wa-rich-response.go +++ b/pkg/msgconv/wa-rich-response.go @@ -29,6 +29,7 @@ import ( "github.com/yuin/goldmark/extension" "go.mau.fi/whatsmeow/types/richresponse" "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/crypto/canonicaljson" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/format/mdext" @@ -36,7 +37,8 @@ import ( func (mc *MessageConverter) convertUnifiedRichResponseMessage(ctx context.Context, data []byte) *bridgev2.ConvertedMessagePart { extra := map[string]any{} - if json.Valid(data) && len(data) < 24*1024 { + _, canonicalErr := canonicaljson.Marshal(json.RawMessage(data)) + if canonicalErr == nil && len(data) < 24*1024 { extra["fi.mau.whatsapp.rich_message"] = json.RawMessage(data) } else if len(data) < 16*1024 { extra["fi.mau.whatsapp.non_json_rich_message"] = base64.StdEncoding.EncodeToString(data) @@ -66,10 +68,15 @@ func (mc *MessageConverter) convertUnifiedRichResponseMessage(ctx context.Contex Extra: extra, } } + content := format.HTMLToContent(htmlBuf.String()) + if len(data)+len(content.Body)+len(content.FormattedBody) > 40000 { + delete(extra, "fi.mau.whatsapp.rich_message") + delete(extra, "fi.mau.whatsapp.non_json_rich_message") + } extra["fi.mau.whatsapp.rich_response_id"] = rrMsg.ResponseID return &bridgev2.ConvertedMessagePart{ Type: event.EventMessage, - Content: new(format.HTMLToContent(htmlBuf.String())), + Content: &content, Extra: extra, } } diff --git a/pkg/msgconv/wa-text.go b/pkg/msgconv/wa-text.go index 1a016334..52874fd8 100644 --- a/pkg/msgconv/wa-text.go +++ b/pkg/msgconv/wa-text.go @@ -48,22 +48,28 @@ func (mc *MessageConverter) convertTextMessage(ctx context.Context, msg *waE2E.M } func (mc *MessageConverter) cutOffLongMessage(content *event.MessageEventContent) { - if len(content.FormattedBody) <= 20000 && len(content.Body) <= 20000 { + size := len(content.Body) + len(content.FormattedBody) + if content.Mentions != nil { + for _, userID := range content.Mentions.UserIDs { + size += len(userID) + } + } + if size <= 40000 { return } + if content.Mentions != nil { + content.Mentions.UserIDs = slices.DeleteFunc(content.Mentions.UserIDs, mc.Bridge.IsGhostMXID) + } if content.FormattedBody == "" { if len(content.Body) > 40000 { content.Body = content.Body[:40000] + "\n\n[long message cut off]" } - } else if len(content.Body) > 500 { + } else if len(content.FormattedBody) > 20000 && len(content.Body) > 500 { content.Body = content.Body[:500] + "[...]" } if len(content.FormattedBody) > 40000 { content.FormattedBody = content.FormattedBody[:40000] + "
[long message cut off]" } - if content.Mentions != nil { - content.Mentions.UserIDs = slices.DeleteFunc(content.Mentions.UserIDs, mc.Bridge.IsGhostMXID) - } } func (mc *MessageConverter) parseFormatting(content *event.MessageEventContent, allowInlineURL, forceHTML bool) { diff --git a/pkg/waid/dbmeta.go b/pkg/waid/dbmeta.go index 15c0a422..b1a47e0e 100644 --- a/pkg/waid/dbmeta.go +++ b/pkg/waid/dbmeta.go @@ -45,6 +45,8 @@ type UserLoginMetadata struct { HistorySyncPortalsNeedCreating bool `json:"history_sync_portals_need_creating,omitzero"` ReachoutTimelockUntil time.Time `json:"reachout_timelock_until,omitzero"` + PreviousOwnLIDs []types.JID `json:"previous_own_lids,omitempty"` + Blocklist *types.Blocklist `json:"blocklist,omitempty"` MData json.RawMessage `json:"mdata,omitempty"`