From 715107f5a22a68baf55b7c6c3a32ab8c4a113176 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Fri, 21 Oct 2022 14:02:33 -0500 Subject: [PATCH] treewide: upgrading to latest mautrix standards Signed-off-by: Sumner Evans --- .editorconfig | 5 +- bridgestate.go | 17 + commands.go | 984 ++-------------- community.go | 132 --- config/bridge.go | 336 ++---- config/config.go | 96 +- config/registration.go | 73 -- config/upgrade.go | 138 +++ crypto.go | 335 ------ custompuppet.go | 177 ++- database/cryptostore.go | 107 -- database/database.go | 107 +- database/message.go | 155 +-- database/migrate.go | 158 --- database/portal.go | 191 ++-- database/puppet.go | 157 +-- database/reactions.go | 105 +- database/statestore.go | 368 ------ database/upgrades/00-latest-revision.sql | 83 ++ .../upgrades/2018-09-01-initial-schema.go | 182 --- database/upgrades/upgrades.go | 139 +-- database/user.go | 241 ++-- database/userportal.go | 35 + example-config.yaml | 36 +- formatting.go | 23 +- go.mod | 49 +- go.sum | 201 +--- {groupmeExt => groupmeext}/client.go | 17 +- {groupmeExt => groupmeext}/message.go | 6 +- {groupmeExt => groupmeext}/subscription.go | 2 +- {groupmeExt => groupmeext}/user.go | 2 +- main.go | 441 ++------ matrix.go | 446 +------- metrics.go | 68 +- portal.go | 1005 ++++------------- provisioning.go | 2 +- puppet.go | 96 +- segment.go | 90 ++ types/types.go | 28 - user.go | 412 +++---- whatsapp-ext/call.go | 72 -- whatsapp-ext/chat.go | 183 --- whatsapp-ext/cmd.go | 69 -- whatsapp-ext/conn.go | 65 -- whatsapp-ext/group.go | 66 -- whatsapp-ext/jsonmessage.go | 105 -- whatsapp-ext/msginfo.go | 95 -- whatsapp-ext/presence.go | 64 -- whatsapp-ext/props.go | 73 -- whatsapp-ext/protomessage.go | 59 - whatsapp-ext/stream.go | 76 -- whatsapp-ext/whatsapp.go | 164 --- 52 files changed, 1887 insertions(+), 6449 deletions(-) create mode 100644 bridgestate.go delete mode 100644 community.go delete mode 100644 config/registration.go create mode 100644 config/upgrade.go delete mode 100644 crypto.go delete mode 100644 database/cryptostore.go delete mode 100644 database/migrate.go delete mode 100644 database/statestore.go create mode 100644 database/upgrades/00-latest-revision.sql delete mode 100644 database/upgrades/2018-09-01-initial-schema.go create mode 100644 database/userportal.go rename {groupmeExt => groupmeext}/client.go (77%) rename {groupmeExt => groupmeext}/message.go (94%) rename {groupmeExt => groupmeext}/subscription.go (98%) rename {groupmeExt => groupmeext}/user.go (80%) create mode 100644 segment.go delete mode 100644 types/types.go delete mode 100644 whatsapp-ext/call.go delete mode 100644 whatsapp-ext/chat.go delete mode 100644 whatsapp-ext/cmd.go delete mode 100644 whatsapp-ext/conn.go delete mode 100644 whatsapp-ext/group.go delete mode 100644 whatsapp-ext/jsonmessage.go delete mode 100644 whatsapp-ext/msginfo.go delete mode 100644 whatsapp-ext/presence.go delete mode 100644 whatsapp-ext/props.go delete mode 100644 whatsapp-ext/protomessage.go delete mode 100644 whatsapp-ext/stream.go delete mode 100644 whatsapp-ext/whatsapp.go diff --git a/.editorconfig b/.editorconfig index 7a8b853..547604b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,11 +10,8 @@ insert_final_newline = true [*.{yaml,yml}] indent_style = space -indent_size = 2 +indent_size = 4 [*.sql] indent_style = space indent_size = 4 - -[.gitlab-ci.yml] -indent_size = 2 diff --git a/bridgestate.go b/bridgestate.go new file mode 100644 index 0000000..fceabf0 --- /dev/null +++ b/bridgestate.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func (user *User) GetRemoteID() string { + if user == nil || !user.GMID.Valid() { + return "" + } + return user.GMID.String() +} + +func (user *User) GetRemoteName() string { + if user == nil || !user.GMID.Valid() { + return "" + } + return fmt.Sprintf("+%s", user.GMID.String()) +} diff --git a/commands.go b/commands.go index 725ed00..8584f2c 100644 --- a/commands.go +++ b/commands.go @@ -17,939 +17,95 @@ package main import ( - "fmt" - "math" - "sort" - - "strconv" - "strings" - - "maunium.net/go/maulogger/v2" - - "github.com/beeper/groupme/database" - "github.com/beeper/groupme/types" - whatsappExt "github.com/beeper/groupme/whatsapp-ext" - - "maunium.net/go/mautrix" - "maunium.net/go/mautrix/appservice" - "maunium.net/go/mautrix/event" - "maunium.net/go/mautrix/format" - "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/bridge/commands" ) -type CommandHandler struct { - bridge *Bridge - log maulogger.Logger +type WrappedCommandEvent struct { + *commands.Event + Bridge *GMBridge + User *User + Portal *Portal } -// NewCommandHandler creates a CommandHandler -func NewCommandHandler(bridge *Bridge) *CommandHandler { - return &CommandHandler{ - bridge: bridge, - log: bridge.Log.Sub("Command handler"), - } -} - -// CommandEvent stores all data which might be used to handle commands -type CommandEvent struct { - Bot *appservice.IntentAPI - Bridge *Bridge - Portal *Portal - Handler *CommandHandler - RoomID id.RoomID - User *User - Command string - Args []string -} - -// Reply sends a reply to command as notice -func (ce *CommandEvent) Reply(msg string, args ...interface{}) { - content := format.RenderMarkdown(fmt.Sprintf(msg, args...), true, false) - content.MsgType = event.MsgNotice - intent := ce.Bot - if ce.Portal != nil && ce.Portal.IsPrivateChat() { - intent = ce.Portal.MainIntent() - } - _, err := intent.SendMessageEvent(ce.RoomID, event.EventMessage, content) - if err != nil { - ce.Handler.log.Warnfln("Failed to reply to command from %s: %v", ce.User.MXID, err) - } -} - -// Handle handles messages to the bridge -func (handler *CommandHandler) Handle(roomID id.RoomID, user *User, message string) { - args := strings.Fields(message) - if len(args) == 0 { - args = []string{"unknown-command"} - } - ce := &CommandEvent{ - Bot: handler.bridge.Bot, - Bridge: handler.bridge, - Portal: handler.bridge.GetPortalByMXID(roomID), - Handler: handler, - RoomID: roomID, - User: user, - Command: strings.ToLower(args[0]), - Args: args[1:], - } - handler.log.Debugfln("%s sent '%s' in %s", user.MXID, message, roomID) - if roomID == handler.bridge.Config.Bridge.Relaybot.ManagementRoom { - handler.CommandRelaybot(ce) - } else { - handler.CommandMux(ce) - } -} - -func (handler *CommandHandler) CommandMux(ce *CommandEvent) { - switch ce.Command { - case "relaybot": - handler.CommandRelaybot(ce) - case "login": - handler.CommandLogin(ce) - case "logout-matrix": - handler.CommandLogoutMatrix(ce) - case "help": - handler.CommandHelp(ce) - case "version": - handler.CommandVersion(ce) - case "reconnect", "connect": - handler.CommandReconnect(ce) - case "disconnect": - handler.CommandDisconnect(ce) - // case "ping": - // handler.CommandPing(ce) - case "delete-connection": - handler.CommandDeleteConnection(ce) - case "delete-session": - handler.CommandDeleteSession(ce) - case "delete-portal": - handler.CommandDeletePortal(ce) - case "delete-all-portals": - handler.CommandDeleteAllPortals(ce) - case "discard-megolm-session", "discard-session": - handler.CommandDiscardMegolmSession(ce) - case "dev-test": - handler.CommandDevTest(ce) - case "set-pl": - handler.CommandSetPowerLevel(ce) - case "logout": - handler.CommandLogout(ce) - case "toggle": - handler.CommandToggle(ce) - case "login-matrix", "sync", "list", "open", "pm", "invite-link", "join", "create": - if !ce.User.HasSession() { - ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.") - return - } else if !ce.User.IsConnected() { - ce.Reply("You are not connected to WhatsApp. Use the `reconnect` command to reconnect.") - return +func wrapCommand(handler func(*WrappedCommandEvent)) func(*commands.Event) { + return func(ce *commands.Event) { + user := ce.User.(*User) + var portal *Portal + if ce.Portal != nil { + portal = ce.Portal.(*Portal) } - - switch ce.Command { - case "login-matrix": - handler.CommandLoginMatrix(ce) - case "sync": - handler.CommandSync(ce) - case "list": - handler.CommandList(ce) - case "open": - handler.CommandOpen(ce) - case "pm": - handler.CommandPM(ce) - case "invite-link": - handler.CommandInviteLink(ce) - case "join": - handler.CommandJoin(ce) - case "create": - handler.CommandCreate(ce) - } - default: - ce.Reply("Unknown command, use the `help` command for help.") + br := ce.Bridge.Child.(*GMBridge) + handler(&WrappedCommandEvent{ce, br, user, portal}) } } -func (handler *CommandHandler) CommandDiscardMegolmSession(ce *CommandEvent) { - if handler.bridge.Crypto == nil { - ce.Reply("This bridge instance doesn't have end-to-bridge encryption enabled") - } else if !ce.User.Admin { - ce.Reply("Only the bridge admin can reset Megolm sessions") - } else { - handler.bridge.Crypto.ResetSession(ce.RoomID) - ce.Reply("Successfully reset Megolm session in this room. New decryption keys will be shared the next time a message is sent from WhatsApp.") - } +var ( + HelpSectionConnectionManagement = commands.HelpSection{Name: "Connection management", Order: 11} + HelpSectionCreatingPortals = commands.HelpSection{Name: "Creating portals", Order: 15} + HelpSectionPortalManagement = commands.HelpSection{Name: "Portal management", Order: 20} + HelpSectionInvites = commands.HelpSection{Name: "Group invites", Order: 25} + HelpSectionMiscellaneous = commands.HelpSection{Name: "Miscellaneous", Order: 30} +) + +func (br *GMBridge) RegisterCommands() { + proc := br.CommandProcessor.(*commands.Processor) + proc.AddHandlers( + // cmdSetRelay, + // cmdUnsetRelay, + // cmdInviteLink, + // cmdResolveLink, + // cmdJoin, + // cmdAccept, + // cmdCreate, + cmdLogin, + // cmdLogout, + // cmdTogglePresence, + // cmdDeleteSession, + // cmdReconnect, + // cmdDisconnect, + // cmdPing, + // cmdDeletePortal, + // cmdDeleteAllPortals, + // cmdBackfill, + // cmdList, + // cmdSearch, + // cmdOpen, + // cmdPM, + // cmdSync, + // cmdDisappearingTimer, + ) } -func (handler *CommandHandler) CommandRelaybot(ce *CommandEvent) { - if handler.bridge.Relaybot == nil { - ce.Reply("The relaybot is disabled") - } else if !ce.User.Admin { - ce.Reply("Only admins can manage the relaybot") - } else { - if ce.Command == "relaybot" { - if len(ce.Args) == 0 { - ce.Reply("**Usage:** `relaybot `") - return - } - ce.Command = strings.ToLower(ce.Args[0]) - ce.Args = ce.Args[1:] - } - ce.User = handler.bridge.Relaybot - handler.CommandMux(ce) - } +var cmdLogin = &commands.FullHandler{ + Func: wrapCommand(fnLogin), + Name: "login", + Help: commands.HelpMeta{ + Section: commands.HelpSectionAuth, + Description: "Link the bridge to your GroupMe account.", + }, } -func (handler *CommandHandler) CommandDevTest(_ *CommandEvent) { - -} - -const cmdVersionHelp = `version - View the bridge version` - -func (handler *CommandHandler) CommandVersion(ce *CommandEvent) { - version := fmt.Sprintf("v%s.unknown", Version) - if Tag == Version { - version = fmt.Sprintf("[v%s](%s/releases/v%s) (%s)", Version, URL, Tag, BuildTime) - } else if len(Commit) > 8 { - version = fmt.Sprintf("v%s.[%s](%s/commit/%s) (%s)", Version, Commit[:8], URL, Commit, BuildTime) - } - ce.Reply(fmt.Sprintf("[%s](%s) %s", Name, URL, version)) -} - -const cmdInviteLinkHelp = `invite-link - Get an invite link to the current group chat.` - -func (handler *CommandHandler) CommandInviteLink(ce *CommandEvent) { - // if ce.Portal == nil { - // ce.Reply("Not a portal room") - // return - // } else if ce.Portal.IsPrivateChat() { - // ce.Reply("Can't get invite link to private chat") - // return - // } - - // link, err := ce.User.Conn.GroupInviteLink(ce.Portal.Key.JID) - // if err != nil { - // ce.Reply("Failed to get invite link: %v", err) - // return - // } - // ce.Reply("%s%s", inviteLinkPrefix, link) -} - -const cmdJoinHelp = `join - Join a group chat with an invite link.` -const inviteLinkPrefix = "https://chat.whatsapp.com/" - -func (handler *CommandHandler) CommandJoin(ce *CommandEvent) { - // if len(ce.Args) == 0 { - // ce.Reply("**Usage:** `join `") - // return - // } else if len(ce.Args[0]) <= len(inviteLinkPrefix) || ce.Args[0][:len(inviteLinkPrefix)] != inviteLinkPrefix { - // ce.Reply("That doesn't look like a WhatsApp invite link") - // return - // } - - // jid, err := ce.User.Conn.GroupAcceptInviteCode(ce.Args[0][len(inviteLinkPrefix):]) - // if err != nil { - // ce.Reply("Failed to join group: %v", err) - // return - // } - - // handler.log.Debugln("%s successfully joined group %s", ce.User.MXID, jid) - // portal := handler.bridge.GetPortalByJID(database.GroupPortalKey(jid)) - // if len(portal.MXID) > 0 { - // portal.Sync(ce.User, whatsapp.Contact{Jid: portal.Key.JID}) - // ce.Reply("Successfully joined group \"%s\" and synced portal room: [%s](https://matrix.to/#/%s)", portal.Name, portal.Name, portal.MXID) - // } else { - // err = portal.CreateMatrixRoom(ce.User) - // if err != nil { - // ce.Reply("Failed to create portal room: %v", err) - // return - // } - - // ce.Reply("Successfully joined group \"%s\" and created portal room: [%s](https://matrix.to/#/%s)", portal.Name, portal.Name, portal.MXID) - // } -} - -const cmdCreateHelp = `create - Create a group chat.` - -func (handler *CommandHandler) CommandCreate(ce *CommandEvent) { - // if ce.Portal != nil { - // ce.Reply("This is already a portal room") - // return - // } - - // members, err := ce.Bot.JoinedMembers(ce.RoomID) - // if err != nil { - // ce.Reply("Failed to get room members: %v", err) - // return - // } - - // var roomNameEvent event.RoomNameEventContent - // err = ce.Bot.StateEvent(ce.RoomID, event.StateRoomName, "", &roomNameEvent) - // if err != nil && !errors.Is(err, mautrix.MNotFound) { - // ce.Reply("Failed to get room name") - // return - // } else if len(roomNameEvent.Name) == 0 { - // ce.Reply("Please set a name for the room first") - // return - // } - - // var encryptionEvent event.EncryptionEventContent - // err = ce.Bot.StateEvent(ce.RoomID, event.StateEncryption, "", &encryptionEvent) - // if err != nil && !errors.Is(err, mautrix.MNotFound) { - // ce.Reply("Failed to get room encryption status") - // return - // } - - // participants := []string{ce.User.JID} - // for userID := range members.Joined { - // jid, ok := handler.bridge.ParsePuppetMXID(userID) - // if ok && jid != ce.User.JID { - // participants = append(participants, jid) - // } - // } - - // resp, err := ce.User.Conn.CreateGroup(roomNameEvent.Name, participants) - // if err != nil { - // ce.Reply("Failed to create group: %v", err) - // return - // } - // portal := handler.bridge.GetPortalByJID(database.GroupPortalKey(resp.GroupID)) - // portal.roomCreateLock.Lock() - // defer portal.roomCreateLock.Unlock() - // if len(portal.MXID) != 0 { - // portal.log.Warnln("Detected race condition in room creation") - // // TODO race condition, clean up the old room - // } - // portal.MXID = ce.RoomID - // portal.Name = roomNameEvent.Name - // portal.Encrypted = encryptionEvent.Algorithm == id.AlgorithmMegolmV1 - // if !portal.Encrypted && handler.bridge.Config.Bridge.Encryption.Default { - // _, err = portal.MainIntent().SendStateEvent(portal.MXID, event.StateEncryption, "", &event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}) - // if err != nil { - // portal.log.Warnln("Failed to enable e2be:", err) - // } - // portal.Encrypted = true - // } - - // portal.Update() - // portal.UpdateBridgeInfo() - - // ce.Reply("Successfully created WhatsApp group %s", portal.Key.JID) - // ce.User.addPortalToCommunity(portal) -} - -const cmdSetPowerLevelHelp = `set-pl [user ID] - Change the power level in a portal room. Only for bridge admins.` - -func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) { - if ce.Portal == nil { - ce.Reply("Not a portal room") - return - } - var level int - var userID id.UserID - var err error - if len(ce.Args) == 1 { - level, err = strconv.Atoi(ce.Args[0]) - if err != nil { - ce.Reply("Invalid power level \"%s\"", ce.Args[0]) - return - } - userID = ce.User.MXID - } else if len(ce.Args) == 2 { - userID = id.UserID(ce.Args[0]) - _, _, err := userID.Parse() - if err != nil { - ce.Reply("Invalid user ID \"%s\"", ce.Args[0]) - return - } - level, err = strconv.Atoi(ce.Args[1]) - if err != nil { - ce.Reply("Invalid power level \"%s\"", ce.Args[1]) - return - } - } else { - ce.Reply("**Usage:** `set-pl [user] `") - return - } - intent := ce.Portal.MainIntent() - _, err = intent.SetPowerLevel(ce.RoomID, userID, level) - if err != nil { - ce.Reply("Failed to set power levels: %v", err) - } -} - -const cmdLoginHelp = `login - Authenticate this Bridge as a GroupMe Client, requires an access token from https://dev.groupme.com/` - -// CommandLogin handles login command -func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { - // if !ce.User.Connect(true) { - // ce.User.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.") - // return - // } - ce.User.Login(ce) -} - -const cmdLogoutHelp = `logout - Logout from GroupMe` - -// CommandLogout handles !logout command -func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { - // if ce.User.Session == nil { - // ce.Reply("You're not logged in.") - // return - // } else if !ce.User.IsConnected() { - // ce.Reply("You are not connected to WhatsApp. Use the `reconnect` command to reconnect, or `delete-session` to forget all login information.") - // return - // } - // puppet := handler.bridge.GetPuppetByJID(ce.User.JID) - // if puppet.CustomMXID != "" { - // err := puppet.SwitchCustomMXID("", "") - // if err != nil { - // ce.User.log.Warnln("Failed to logout-matrix while logging out of WhatsApp:", err) - // } - // } - // err := ce.User.Conn.Logout() - // if err != nil { - // ce.User.log.Warnln("Error while logging out:", err) - // ce.Reply("Unknown error while logging out: %v", err) - // return - // } - // _, err = ce.User.Conn.Disconnect() - // if err != nil { - // ce.User.log.Warnln("Error while disconnecting after logout:", err) - // } - // ce.User.Conn.RemoveHandlers() - //ce.User.Conn.Stop(context.TODO()) - ce.User.Conn = nil - ce.User.removeFromJIDMap() - ce.User.Token = "" - //ce.User.JID = "" - ce.User.Client = nil - ce.User.Update() - // // TODO this causes a foreign key violation, which should be fixed - // //ce.User.JID = "" - // ce.User.SetSession(nil) - ce.Reply("Logged out successfully.") -} - -const cmdToggleHelp = `toggle - Toggle bridging of presence or read receipts` - -func (handler *CommandHandler) CommandToggle(ce *CommandEvent) { - // if len(ce.Args) == 0 || (ce.Args[0] != "presence" && ce.Args[0] != "receipts") { - // ce.Reply("**Usage:** `toggle `") - // return - // } - // if ce.User.Session == nil { - // ce.Reply("You're not logged in.") - // return - // } - // customPuppet := handler.bridge.GetPuppetByCustomMXID(ce.User.MXID) - // if customPuppet == nil { - // ce.Reply("You're not logged in with your Matrix account.") - // return - // } - // if ce.Args[0] == "presence" { - // customPuppet.EnablePresence = !customPuppet.EnablePresence - // var newPresence whatsapp.Presence - // if customPuppet.EnablePresence { - // newPresence = whatsapp.PresenceAvailable - // ce.Reply("Enabled presence bridging") - // } else { - // newPresence = whatsapp.PresenceUnavailable - // ce.Reply("Disabled presence bridging") - // } - // if ce.User.IsConnected() { - // _, err := ce.User.Conn.Presence("", newPresence) - // if err != nil { - // ce.User.log.Warnln("Failed to set presence:", err) - // } - // } - // } else if ce.Args[0] == "receipts" { - // customPuppet.EnableReceipts = !customPuppet.EnableReceipts - // if customPuppet.EnableReceipts { - // ce.Reply("Enabled read receipt bridging") - // } else { - // ce.Reply("Disabled read receipt bridging") - // } - // } - // customPuppet.Update() -} - -const cmdDeleteSessionHelp = `delete-session - Delete session information and disconnect from WhatsApp without sending a logout request` - -func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) { - // if ce.User.Session == nil && ce.User.Conn == nil { - // ce.Reply("Nothing to purge: no session information stored and no active connection.") - // return - // } - // ce.User.removeFromJIDMap() - // ce.User.SetSession(nil) - // if ce.User.Conn != nil { - // _, _ = ce.User.Conn.Disconnect() - // ce.User.Conn.RemoveHandlers() - // ce.User.Conn = nil - // } - // ce.Reply("Session information purged") -} - -const cmdReconnectHelp = `reconnect - Reconnect to GroupMe` - -func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { - if ce.User.Conn == nil { - if len(ce.User.Token) == 0 { - ce.Reply("No existing connection and no token. Did you mean `login`?") +func fnLogin(ce *WrappedCommandEvent) { + if ce.User.Client != nil { + if ce.User.IsConnected() { + ce.Reply("You're already logged in") } else { - ce.Reply("No existing connection, creating one...") - ce.User.Connect() + ce.Reply("You're already logged in. Perhaps you wanted to `reconnect`?") } return } - wasConnected := true - //ce.User.Conn.Stop(context.TODO()) - ce.User.Conn = nil - //TODO: better connection handling - - // if err == whatsapp.ErrNotConnected { - // wasConnected = false - // } else if err != nil { - // ce.User.log.Warnln("Error while disconnecting:", err) - // } else if len(sess.Wid) > 0 { - // ce.User.SetSession(&sess) - // } - - // err = ce.User.Conn.Restore() - // if err == whatsapp.ErrInvalidSession { - // if ce.User.Session != nil { - // ce.User.log.Debugln("Got invalid session error when reconnecting, but user has session. Retrying using RestoreWithSession()...") - // var sess whatsapp.Session - // sess, err = ce.User.Conn.RestoreWithSession(*ce.User.Session) - // if err == nil { - // ce.User.SetSession(&sess) - // } - // } else { - // ce.Reply("You are not logged in.") - // return - // } - // } else if err == whatsapp.ErrLoginInProgress { - // ce.Reply("A login or reconnection is already in progress.") - // return - // } else if err == whatsapp.ErrAlreadyLoggedIn { - // ce.Reply("You were already connected.") - // return - // } - // if err != nil { - // ce.User.log.Warnln("Error while reconnecting:", err) - // if err.Error() == "restore session connection timed out" { - // ce.Reply("Reconnection timed out. Is WhatsApp on your phone reachable?") - // } else { - // ce.Reply("Unknown error while reconnecting: %v", err) - // } - // ce.User.log.Debugln("Disconnecting due to failed session restore in reconnect command...") - // sess, err := ce.User.Conn.Disconnect() - // if err != nil { - // ce.User.log.Errorln("Failed to disconnect after failed session restore in reconnect command:", err) - // } else if len(sess.Wid) > 0 { - // ce.User.SetSession(&sess) - // } - // return - // } - // ce.User.ConnectionErrors = 0 - connected := ce.User.Connect() - if !connected { - ce.Reply("Unsuccessful connection") + if len(ce.Args) < 1 { + ce.Reply(`Get your access token from https://dev.groupme.com/ which should be the first argument to login`) return } - var msg string - if wasConnected { - msg = "Reconnected successfully." - } else { - msg = "Connected successfully." + defer ce.Bot.RedactEvent(ce.RoomID, ce.EventID) + + err := ce.User.Login(ce.Args[0]) + if err != nil { + ce.Reply("Failed to log in: %v", err) } - ce.Reply(msg) - // ce.User.PostLogin() -} - -const cmdDeleteConnectionHelp = `delete-connection - Disconnect ignoring errors and delete internal connection state.` - -func (handler *CommandHandler) CommandDeleteConnection(ce *CommandEvent) { - // if ce.User.Conn == nil { - // ce.Reply("You don't have a WhatsApp connection.") - // return - // } - // sess, err := ce.User.Conn.Disconnect() - // if err == nil && len(sess.Wid) > 0 { - // ce.User.SetSession(&sess) - // } - // ce.User.Conn.RemoveHandlers() - // ce.User.Conn = nil - // ce.User.bridge.Metrics.TrackConnectionState(ce.User.JID, false) - // ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.") -} - -const cmdDisconnectHelp = `disconnect - Disconnect from WhatsApp (without logging out)` - -func (handler *CommandHandler) CommandDisconnect(ce *CommandEvent) { - if ce.User.Conn == nil { - ce.Reply("You don't have a WhatsApp connection.") - return - } - //ce.User.Conn.Stop(context.TODO()) - // if err == whatsapp.ErrNotConnected { - // ce.Reply("You were not connected.") - // return - // } else if err != nil { - // ce.User.log.Warnln("Error while disconnecting:", err) - // ce.Reply("Unknown error while disconnecting: %v", err) - // return - // } - ce.User.bridge.Metrics.TrackConnectionState(ce.User.JID, false) - ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.") -} - -const cmdPingHelp = `ping - Check your connection to WhatsApp.` - -func (handler *CommandHandler) CommandPing(ce *CommandEvent) { - // if ce.User.Session == nil { - // if ce.User.IsLoginInProgress() { - // ce.Reply("You're not logged into WhatsApp, but there's a login in progress.") - // } else { - // ce.Reply("You're not logged into WhatsApp.") - // } - // } else if ce.User.Conn == nil { - // ce.Reply("You don't have a WhatsApp connection.") - // } else if err := ce.User.Conn.AdminTest(); err != nil { - // if ce.User.IsLoginInProgress() { - // ce.Reply("Connection not OK: %v, but login in progress", err) - // } else { - // ce.Reply("Connection not OK: %v", err) - // } - // } else { - // ce.Reply("Connection to WhatsApp OK") - // } -} - -const cmdHelpHelp = `help - Prints this help` - -// CommandHelp handles help command -func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { - cmdPrefix := "" - if ce.User.ManagementRoom != ce.RoomID || ce.User.IsRelaybot { - cmdPrefix = handler.bridge.Config.Bridge.CommandPrefix + " " - } - - ce.Reply("* " + strings.Join([]string{ - cmdPrefix + cmdHelpHelp, - cmdPrefix + cmdLoginHelp, - // cmdPrefix + cmdLogoutHelp, - // cmdPrefix + cmdDeleteSessionHelp, - cmdPrefix + cmdReconnectHelp, - // cmdPrefix + cmdDisconnectHelp, - // cmdPrefix + cmdDeleteConnectionHelp, - // cmdPrefix + cmdPingHelp, - // cmdPrefix + cmdLoginMatrixHelp, - cmdPrefix + cmdLogoutMatrixHelp, - // cmdPrefix + cmdToggleHelp, - cmdPrefix + cmdSyncHelp, - cmdPrefix + cmdListHelp, - cmdPrefix + cmdOpenHelp, - cmdPrefix + cmdPMHelp, - // cmdPrefix + cmdInviteLinkHelp, - // cmdPrefix + cmdJoinHelp, - // cmdPrefix + cmdCreateHelp, - // cmdPrefix + cmdSetPowerLevelHelp, - cmdPrefix + cmdDeletePortalHelp, - cmdPrefix + cmdDeleteAllPortalsHelp, - }, "\n* ")) -} - -const cmdSyncHelp = `sync - Synchronize contacts from phone and optionally create portals for group chats.` //TODO: add [--create-all] - -// CommandSync handles sync command -func (handler *CommandHandler) CommandSync(ce *CommandEvent) { - user := ce.User - //create := len(ce.Args) > 0 && ce.Args[0] == "--create-all" - - go user.HandleChatList() - // ce.Reply("Updating contact and chat list...") - // handler.log.Debugln("Importing contacts of", user.MXID) - // _, err := user.Conn.Contacts() - // if err != nil { - // user.log.Errorln("Error updating contacts:", err) - // ce.Reply("Failed to sync contact list (see logs for details)") - // return - // } - // handler.log.Debugln("Importing chats of", user.MXID) - // _, err = user.Conn.Chats() - // if err != nil { - // user.log.Errorln("Error updating chats:", err) - // ce.Reply("Failed to sync chat list (see logs for details)") - // return - // } - - // ce.Reply("Syncing contacts...") - // user.syncPuppets(nil) - // ce.Reply("Syncing chats...") - // user.syncPortals(nil, create) - - ce.Reply("Syncing...") -} - -const cmdDeletePortalHelp = `delete-portal - Delete the current portal. If the portal is used by other people, this is limited to bridge admins.` - -func (handler *CommandHandler) CommandDeletePortal(ce *CommandEvent) { - if ce.Portal == nil { - ce.Reply("You must be in a portal room to use that command") - return - } - - if !ce.User.Admin { - users := ce.Portal.GetUserIDs() - if len(users) > 1 || (len(users) == 1 && users[0] != ce.User.MXID) { - ce.Reply("Only bridge admins can delete portals with other Matrix users") - return - } - } - - ce.Portal.log.Infoln(ce.User.MXID, "requested deletion of portal.") - ce.Portal.Delete() - ce.Portal.Cleanup(false) -} - -const cmdDeleteAllPortalsHelp = `delete-all-portals - Delete all your portals that aren't used by any other user.'` - -func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) { - portals := ce.User.GetPortals() - portalsToDelete := make([]*Portal, 0, len(portals)) - for _, portal := range portals { - users := portal.GetUserIDs() - if len(users) == 1 && users[0] == ce.User.MXID { - portalsToDelete = append(portalsToDelete, portal) - } - } - leave := func(portal *Portal) { - if len(portal.MXID) > 0 { - _, _ = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{ - Reason: "Deleting portal", - UserID: ce.User.MXID, - }) - } - } - customPuppet := handler.bridge.GetPuppetByCustomMXID(ce.User.MXID) - if customPuppet != nil && customPuppet.CustomIntent() != nil { - intent := customPuppet.CustomIntent() - leave = func(portal *Portal) { - if len(portal.MXID) > 0 { - _, _ = intent.LeaveRoom(portal.MXID) - _, _ = intent.ForgetRoom(portal.MXID) - } - } - } - ce.Reply("Found %d portals with no other users, deleting...", len(portalsToDelete)) - for _, portal := range portalsToDelete { - portal.Delete() - leave(portal) - } - ce.Reply("Finished deleting portal info. Now cleaning up rooms in background. " + - "You may already continue using the bridge. Use `sync` to recreate portals.") - - go func() { - for _, portal := range portalsToDelete { - portal.Cleanup(false) - } - ce.Reply("Finished background cleanup of deleted portal rooms.") - }() -} - -const cmdListHelp = `list [page] [items per page] - Get a list of all contacts and groups.` - -func formatContacts(contacts bool, input map[string]string) (result []string) { - for jid, contact := range input { - result = append(result, fmt.Sprintf("* %s - `%s`", contact, jid)) - } - sort.Sort(sort.StringSlice(result)) - return -} - -func (handler *CommandHandler) CommandList(ce *CommandEvent) { - if len(ce.Args) == 0 { - ce.Reply("**Usage:** `list [page] [items per page]`") - return - } - mode := strings.ToLower(ce.Args[0]) - if mode[0] != 'g' && mode[0] != 'c' { - ce.Reply("**Usage:** `list [page] [items per page]`") - return - } - var err error - page := 1 - max := 100 - if len(ce.Args) > 1 { - page, err = strconv.Atoi(ce.Args[1]) - if err != nil || page <= 0 { - ce.Reply("\"%s\" isn't a valid page number", ce.Args[1]) - return - } - } - if len(ce.Args) > 2 { - max, err = strconv.Atoi(ce.Args[2]) - if err != nil || max <= 0 { - ce.Reply("\"%s\" isn't a valid number of items per page", ce.Args[2]) - return - } else if max > 400 { - ce.Reply("Warning: a high number of items per page may fail to send a reply") - } - } - contacts := mode[0] == 'c' - typeName := "Groups" - if contacts { - typeName = "Contacts" - } - - //real deal - v := make(map[types.GroupMeID]string) - - if contacts { - for i, j := range ce.User.ChatList { - v[i] = j.OtherUser.Name - } - } else { - for i, j := range ce.User.GroupList { - v[i] = j.Name - } - - } - result := formatContacts(contacts, v) - - if len(result) == 0 { - ce.Reply("No %s found", strings.ToLower(typeName)) - return - } - pages := int(math.Ceil(float64(len(result)) / float64(max))) - if (page-1)*max >= len(result) { - if pages == 1 { - ce.Reply("There is only 1 page of %s", strings.ToLower(typeName)) - } else { - ce.Reply("There are only %d pages of %s", pages, strings.ToLower(typeName)) - } - return - } - lastIndex := page * max - if lastIndex > len(result) { - lastIndex = len(result) - } - result = result[(page-1)*max : lastIndex] - ce.Reply("### %s (page %d of %d)\n\n%s", typeName, page, pages, strings.Join(result, "\n")) -} - -const cmdOpenHelp = `open <_group JID_> - Open a group chat portal.` - -func (handler *CommandHandler) CommandOpen(ce *CommandEvent) { - if len(ce.Args) == 0 { - ce.Reply("**Usage:** `open `") - return - } - - user := ce.User - jid := ce.Args[0] - - if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) { - ce.Reply("That looks like a user JID. Did you mean `pm %s`?", jid[:len(jid)-len(whatsappExt.NewUserSuffix)]) - return - } - - contact, ok := user.GroupList[jid] - if !ok { - ce.Reply("Group JID not found in contacts. Try syncing contacts with `sync` first.") - return - } - handler.log.Debugln("Importing", jid, "for", user.MXID) - portal := user.bridge.GetPortalByJID(database.GroupPortalKey(jid)) - if len(portal.MXID) > 0 { - portal.Sync(user, &contact) - ce.Reply("Portal room synced.") - } else { - portal.Sync(user, &contact) - ce.Reply("Portal room created.") - } - _, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: user.MXID}) -} - -const cmdPMHelp = `pm - To direct message someone already in a shared group start a direct chat with them in Matrix` - -func (handler *CommandHandler) CommandPM(ce *CommandEvent) { - // ce.Reply(fmt.Sprintf("**DEPRECATED COMMAND:** `%s`", cmdPMHelp)) - // if len(ce.Args) == 0 { - // ce.Reply(fmt.Sprintf("**DEPRECATED COMMAND:** `%s`", cmdPMHelp)) - // return - // } - - // force := ce.Args[0] == "--force" - // if force { - // ce.Args = ce.Args[1:] - // } - // - // user := ce.User - // - // jid := ce.Args[0] - // - // handler.log.Debugln("Importing", jid, "for", user.MXID) - // - // contact, ok := user.Conn.Store.Contacts[jid] - // if !ok { - // if !force { - // ce.Reply("Phone number not found in contacts. Try syncing contacts with `sync` first. " + - // "To create a portal anyway, use `pm --force `.") - // return - // } - // contact = whatsapp.Contact{Jid: jid} - // } - // puppet := user.bridge.GetPuppetByJID(contact.Jid) - // puppet.Sync(user, contact) - // portal := user.bridge.GetPortalByJID(database.NewPortalKey(contact.Jid, user.JID)) - // if len(portal.MXID) > 0 { - // err := portal.MainIntent().EnsureInvited(portal.MXID, user.MXID) - // if err != nil { - // portal.log.Warnfln("Failed to invite %s to portal: %v. Creating new portal", user.MXID, err) - // } else { - // ce.Reply("You already have a private chat portal with that user at [%s](https://matrix.to/#/%s)", puppet.Displayname, portal.MXID) - // return - // } - // } - // err := portal.CreateMatrixRoom(user) - // if err != nil { - // ce.Reply("Failed to create portal room: %v", err) - // return - // } - // ce.Reply("Created portal room and invited you to it.") -} - -const cmdLoginMatrixHelp = `login-matrix <_access token_> - Replace your WhatsApp account's Matrix puppet with your real Matrix account.'` - -func (handler *CommandHandler) CommandLoginMatrix(ce *CommandEvent) { - //if len(ce.Args) == 0 { - // ce.Reply("**Usage:** `login-matrix `") - // return - //} - //puppet := handler.bridge.GetPuppetByJID(ce.User.JID) - //err := puppet.SwitchCustomMXID(ce.Args[0], ce.User.MXID) - //if err != nil { - // ce.Reply("Failed to switch puppet: %v", err) - // return - //} - //ce.Reply("Successfully switched puppet") -} - -const cmdLogoutMatrixHelp = `logout-matrix - Switch your WhatsApp account's Matrix puppet back to the default one.` - -func (handler *CommandHandler) CommandLogoutMatrix(ce *CommandEvent) { - //puppet := handler.bridge.GetPuppetByJID(ce.User.JID) - //if len(puppet.CustomMXID) == 0 { - // ce.Reply("You had not changed your WhatsApp account's Matrix puppet.") - // return - //} - //err := puppet.SwitchCustomMXID("", "") - //if err != nil { - // ce.Reply("Failed to remove custom puppet: %v", err) - // return - //} - //ce.Reply("Successfully removed custom puppet") + + ce.Reply("Logged in successfully!") } diff --git a/community.go b/community.go deleted file mode 100644 index 72150e8..0000000 --- a/community.go +++ /dev/null @@ -1,132 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package main - -import ( - "fmt" - "net/http" - - "maunium.net/go/mautrix" -) - -func (user *User) inviteToCommunity() { - url := user.bridge.Bot.BuildURL("groups", user.CommunityID, "admin", "users", "invite", user.MXID) - reqBody := map[string]interface{}{} - _, err := user.bridge.Bot.MakeRequest(http.MethodPut, url, &reqBody, nil) - if err != nil { - user.log.Warnfln("Failed to invite user to personal filtering community %s: %v", user.CommunityID, err) - } -} - -func (user *User) updateCommunityProfile() { - url := user.bridge.Bot.BuildURL("groups", user.CommunityID, "profile") - profileReq := struct { - Name string `json:"name"` - AvatarURL string `json:"avatar_url"` - ShortDescription string `json:"short_description"` - }{"WhatsApp", user.bridge.Config.AppService.Bot.Avatar, "Your WhatsApp bridged chats"} - _, err := user.bridge.Bot.MakeRequest(http.MethodPost, url, &profileReq, nil) - if err != nil { - user.log.Warnfln("Failed to update metadata of %s: %v", user.CommunityID, err) - } -} - -func (user *User) createCommunity() { - if user.IsRelaybot || !user.bridge.Config.Bridge.EnableCommunities() { - return - } - - localpart, server, _ := user.MXID.Parse() - community := user.bridge.Config.Bridge.FormatCommunity(localpart, server) - user.log.Debugln("Creating personal filtering community", community) - bot := user.bridge.Bot - req := struct { - Localpart string `json:"localpart"` - }{community} - resp := struct { - GroupID string `json:"group_id"` - }{} - _, err := bot.MakeRequest(http.MethodPost, bot.BuildURL("create_group"), &req, &resp) - if err != nil { - if httpErr, ok := err.(mautrix.HTTPError); ok { - if httpErr.RespError.Err != "Group already exists" { - user.log.Warnln("Server responded with error creating personal filtering community:", err) - return - } else { - user.log.Debugln("Personal filtering community", resp.GroupID, "already existed") - user.CommunityID = fmt.Sprintf("+%s:%s", req.Localpart, user.bridge.Config.Homeserver.Domain) - } - } else { - user.log.Warnln("Unknown error creating personal filtering community:", err) - return - } - } else { - user.log.Infoln("Created personal filtering community %s", resp.GroupID) - user.CommunityID = resp.GroupID - user.inviteToCommunity() - user.updateCommunityProfile() - } -} - -func (user *User) addPuppetToCommunity(puppet *Puppet) bool { - if user.IsRelaybot || len(user.CommunityID) == 0 { - return false - } - bot := user.bridge.Bot - url := bot.BuildURL("groups", user.CommunityID, "admin", "users", "invite", puppet.MXID) - blankReqBody := map[string]interface{}{} - _, err := bot.MakeRequest(http.MethodPut, url, &blankReqBody, nil) - if err != nil { - user.log.Warnfln("Failed to invite %s to %s: %v", puppet.MXID, user.CommunityID, err) - return false - } - reqBody := map[string]map[string]string{ - "m.visibility": { - "type": "private", - }, - } - url = bot.BuildURLWithQuery(mautrix.URLPath{"groups", user.CommunityID, "self", "accept_invite"}, map[string]string{ - "user_id": puppet.MXID.String(), - }) - _, err = bot.MakeRequest(http.MethodPut, url, &reqBody, nil) - if err != nil { - user.log.Warnfln("Failed to join %s as %s: %v", user.CommunityID, puppet.MXID, err) - return false - } - user.log.Debugln("Added", puppet.MXID, "to", user.CommunityID) - return true -} - -func (user *User) addPortalToCommunity(portal *Portal) bool { - if user.IsRelaybot || len(user.CommunityID) == 0 || len(portal.MXID) == 0 { - return false - } - bot := user.bridge.Bot - url := bot.BuildURL("groups", user.CommunityID, "admin", "rooms", portal.MXID) - reqBody := map[string]map[string]string{ - "m.visibility": { - "type": "private", - }, - } - _, err := bot.MakeRequest(http.MethodPut, url, &reqBody, nil) - if err != nil { - user.log.Warnfln("Failed to add %s to %s: %v", portal.MXID, user.CommunityID, err) - return false - } - user.log.Debugln("Added", portal.MXID, "to", user.CommunityID) - return true -} diff --git a/config/bridge.go b/config/bridge.go index bea4bc4..d650fbc 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -17,112 +17,131 @@ package config import ( - "bytes" - "strconv" + "errors" + "fmt" "strings" "text/template" + "time" - "github.com/karmanyaahm/groupme" + "maunium.net/go/mautrix/bridge/bridgeconfig" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" - - "github.com/beeper/groupme/types" ) +type DeferredConfig struct { + StartDaysAgo int `yaml:"start_days_ago"` + MaxBatchEvents int `yaml:"max_batch_events"` + BatchDelay int `yaml:"batch_delay"` +} + type BridgeConfig struct { UsernameTemplate string `yaml:"username_template"` DisplaynameTemplate string `yaml:"displayname_template"` - CommunityTemplate string `yaml:"community_template"` - ConnectionTimeout int `yaml:"connection_timeout"` - FetchMessageOnTimeout bool `yaml:"fetch_message_on_timeout"` - DeliveryReceipts bool `yaml:"delivery_receipts"` - LoginQRRegenCount int `yaml:"login_qr_regen_count"` - MaxConnectionAttempts int `yaml:"max_connection_attempts"` - ConnectionRetryDelay int `yaml:"connection_retry_delay"` - ReportConnectionRetry bool `yaml:"report_connection_retry"` - ChatListWait int `yaml:"chat_list_wait"` - PortalSyncWait int `yaml:"portal_sync_wait"` - UserMessageBuffer int `yaml:"user_message_buffer"` - PortalMessageBuffer int `yaml:"portal_message_buffer"` + PersonalFilteringSpaces bool `yaml:"personal_filtering_spaces"` - CallNotices struct { - Start bool `yaml:"start"` - End bool `yaml:"end"` - } `yaml:"call_notices"` + DeliveryReceipts bool `yaml:"delivery_receipts"` + MessageStatusEvents bool `yaml:"message_status_events"` + MessageErrorNotices bool `yaml:"message_error_notices"` + PortalMessageBuffer int `yaml:"portal_message_buffer"` - InitialChatSync int `yaml:"initial_chat_sync_count"` - InitialHistoryFill int `yaml:"initial_history_fill_count"` - HistoryDisableNotifs bool `yaml:"initial_history_disable_notifications"` - RecoverChatSync int `yaml:"recovery_chat_sync_count"` - RecoverHistory bool `yaml:"recovery_history_backfill"` - SyncChatMaxAge uint64 `yaml:"sync_max_chat_age"` + SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"` + SyncDirectChatList bool `yaml:"sync_direct_chat_list"` + SyncManualMarkedUnread bool `yaml:"sync_manual_marked_unread"` + DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"` - SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"` - SyncDirectChatList bool `yaml:"sync_direct_chat_list"` - DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"` - DefaultBridgePresence bool `yaml:"default_bridge_presence"` - LoginSharedSecret string `yaml:"login_shared_secret"` + HistorySync struct { + CreatePortals bool `yaml:"create_portals"` + Backfill bool `yaml:"backfill"` - InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"` - PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"` - ResendBridgeInfo bool `yaml:"resend_bridge_info"` + DoublePuppetBackfill bool `yaml:"double_puppet_backfill"` + RequestFullSync bool `yaml:"request_full_sync"` + MaxInitialConversations int `yaml:"max_initial_conversations"` + UnreadHoursThreshold int `yaml:"unread_hours_threshold"` - WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"` + Immediate struct { + WorkerCount int `yaml:"worker_count"` + MaxEvents int `yaml:"max_events"` + } `yaml:"immediate"` - AllowUserInvite bool `yaml:"allow_user_invite"` + Deferred []DeferredConfig `yaml:"deferred"` + } `yaml:"history_sync"` + + DoublePuppetServerMap map[string]string `yaml:"double_puppet_server_map"` + DoublePuppetAllowDiscovery bool `yaml:"double_puppet_allow_discovery"` + LoginSharedSecretMap map[string]string `yaml:"login_shared_secret_map"` + + ResendBridgeInfo bool `yaml:"resend_bridge_info"` + + PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"` + FederateRooms bool `yaml:"federate_rooms"` + AllowUserInvite bool `yaml:"allow_user_invite"` + + MessageHandlingTimeout struct { + ErrorAfterStr string `yaml:"error_after"` + DeadlineStr string `yaml:"deadline"` + + ErrorAfter time.Duration `yaml:"-"` + Deadline time.Duration `yaml:"-"` + } `yaml:"message_handling_timeout"` CommandPrefix string `yaml:"command_prefix"` - Encryption struct { - Allow bool `yaml:"allow"` - Default bool `yaml:"default"` + ManagementRoomText bridgeconfig.ManagementRoomTexts `yaml:"management_room_text"` - KeySharing struct { - Allow bool `yaml:"allow"` - RequireCrossSigning bool `yaml:"require_cross_signing"` - RequireVerification bool `yaml:"require_verification"` - } `yaml:"key_sharing"` - } `yaml:"encryption"` + Encryption bridgeconfig.EncryptionConfig `yaml:"encryption"` - Permissions PermissionConfig `yaml:"permissions"` + Provisioning struct { + Prefix string `yaml:"prefix"` + SharedSecret string `yaml:"shared_secret"` + } `yaml:"provisioning"` - Relaybot RelaybotConfig `yaml:"relaybot"` + Permissions bridgeconfig.PermissionConfig `yaml:"permissions"` - usernameTemplate *template.Template `yaml:"-"` - displaynameTemplate *template.Template `yaml:"-"` - communityTemplate *template.Template `yaml:"-"` + ParsedUsernameTemplate *template.Template `yaml:"-"` + displaynameTemplate *template.Template `yaml:"-"` } -func (bc *BridgeConfig) setDefaults() { - bc.ConnectionTimeout = 20 - bc.FetchMessageOnTimeout = false - bc.DeliveryReceipts = false - bc.LoginQRRegenCount = 2 - bc.MaxConnectionAttempts = 3 - bc.ConnectionRetryDelay = -1 - bc.ReportConnectionRetry = true - bc.ChatListWait = 30 - bc.PortalSyncWait = 600 - bc.UserMessageBuffer = 1024 - bc.PortalMessageBuffer = 128 +func (bc BridgeConfig) GetEncryptionConfig() bridgeconfig.EncryptionConfig { + return bc.Encryption +} - bc.CallNotices.Start = true - bc.CallNotices.End = true +func (bc BridgeConfig) EnableMessageStatusEvents() bool { + return bc.MessageStatusEvents +} - bc.InitialChatSync = 10 - bc.InitialHistoryFill = 20 - bc.RecoverChatSync = -1 - bc.RecoverHistory = true - bc.SyncChatMaxAge = 259200 +func (bc BridgeConfig) EnableMessageErrorNotices() bool { + return bc.MessageErrorNotices +} - bc.SyncWithCustomPuppets = true - bc.DefaultBridgePresence = true - bc.DefaultBridgeReceipts = true - bc.LoginSharedSecret = "" +func (bc BridgeConfig) GetCommandPrefix() string { + return bc.CommandPrefix +} - bc.InviteOwnPuppetForBackfilling = true - bc.PrivateChatPortalMeta = false +func (bc BridgeConfig) GetManagementRoomTexts() bridgeconfig.ManagementRoomTexts { + return bc.ManagementRoomText +} + +func (bc BridgeConfig) GetResendBridgeInfo() bool { + return bc.ResendBridgeInfo +} + +func boolToInt(val bool) int { + if val { + return 1 + } + return 0 +} + +func (bc BridgeConfig) Validate() error { + _, hasWildcard := bc.Permissions["*"] + _, hasExampleDomain := bc.Permissions["example.com"] + _, hasExampleUser := bc.Permissions["@admin:example.com"] + exampleLen := boolToInt(hasWildcard) + boolToInt(hasExampleUser) + boolToInt(hasExampleDomain) + if len(bc.Permissions) <= exampleLen { + return errors.New("bridge.permissions not configured") + } + return nil } type umBridgeConfig BridgeConfig @@ -133,9 +152,11 @@ func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } - bc.usernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate) + bc.ParsedUsernameTemplate, err = template.New("username").Parse(bc.UsernameTemplate) if err != nil { return err + } else if !strings.Contains(bc.FormatUsername("1234567890"), "1234567890") { + return fmt.Errorf("username template is missing user ID placeholder") } bc.displaynameTemplate, err = template.New("displayname").Parse(bc.DisplaynameTemplate) @@ -143,8 +164,14 @@ func (bc *BridgeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } - if len(bc.CommunityTemplate) > 0 { - bc.communityTemplate, err = template.New("community").Parse(bc.CommunityTemplate) + if bc.MessageHandlingTimeout.ErrorAfterStr != "" { + bc.MessageHandlingTimeout.ErrorAfter, err = time.ParseDuration(bc.MessageHandlingTimeout.ErrorAfterStr) + if err != nil { + return err + } + } + if bc.MessageHandlingTimeout.DeadlineStr != "" { + bc.MessageHandlingTimeout.Deadline, err = time.ParseDuration(bc.MessageHandlingTimeout.DeadlineStr) if err != nil { return err } @@ -157,144 +184,15 @@ type UsernameTemplateArgs struct { UserID id.UserID } -func (bc BridgeConfig) FormatDisplayname(contact groupme.Member) (string, int8) { - var buf bytes.Buffer - if index := strings.IndexRune(contact.ID.String(), '@'); index > 0 { - contact.ID = groupme.ID("+" + contact.UserID.String()[:index]) - } - bc.displaynameTemplate.Execute(&buf, contact) - var quality int8 - switch { - case len(contact.Nickname) > 0: - quality = 3 - //TODO what - case len(contact.UserID) > 0: - quality = 1 - default: - quality = 0 - } - return buf.String(), quality -} - -func (bc BridgeConfig) FormatUsername(userID types.GroupMeID) string { - var buf bytes.Buffer - bc.usernameTemplate.Execute(&buf, userID) +func (bc BridgeConfig) FormatUsername(username string) string { + var buf strings.Builder + _ = bc.ParsedUsernameTemplate.Execute(&buf, username) return buf.String() } -type CommunityTemplateArgs struct { - Localpart string - Server string -} - -func (bc BridgeConfig) EnableCommunities() bool { - return bc.communityTemplate != nil -} - -func (bc BridgeConfig) FormatCommunity(localpart, server string) string { - var buf bytes.Buffer - bc.communityTemplate.Execute(&buf, CommunityTemplateArgs{localpart, server}) - return buf.String() -} - -type PermissionConfig map[string]PermissionLevel - -type PermissionLevel int - -const ( - PermissionLevelDefault PermissionLevel = 0 - PermissionLevelRelaybot PermissionLevel = 5 - PermissionLevelUser PermissionLevel = 10 - PermissionLevelAdmin PermissionLevel = 100 -) - -func (pc *PermissionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { - rawPC := make(map[string]string) - err := unmarshal(&rawPC) - if err != nil { - return err - } - - if *pc == nil { - *pc = make(map[string]PermissionLevel) - } - for key, value := range rawPC { - switch strings.ToLower(value) { - case "relaybot": - (*pc)[key] = PermissionLevelRelaybot - case "user": - (*pc)[key] = PermissionLevelUser - case "admin": - (*pc)[key] = PermissionLevelAdmin - default: - val, err := strconv.Atoi(value) - if err != nil { - (*pc)[key] = PermissionLevelDefault - } else { - (*pc)[key] = PermissionLevel(val) - } - } - } - return nil -} - -func (pc *PermissionConfig) MarshalYAML() (interface{}, error) { - if *pc == nil { - return nil, nil - } - rawPC := make(map[string]string) - for key, value := range *pc { - switch value { - case PermissionLevelRelaybot: - rawPC[key] = "relaybot" - case PermissionLevelUser: - rawPC[key] = "user" - case PermissionLevelAdmin: - rawPC[key] = "admin" - default: - rawPC[key] = strconv.Itoa(int(value)) - } - } - return rawPC, nil -} - -func (pc PermissionConfig) IsRelaybotWhitelisted(userID id.UserID) bool { - return pc.GetPermissionLevel(userID) >= PermissionLevelRelaybot -} - -func (pc PermissionConfig) IsWhitelisted(userID id.UserID) bool { - return pc.GetPermissionLevel(userID) >= PermissionLevelUser -} - -func (pc PermissionConfig) IsAdmin(userID id.UserID) bool { - return pc.GetPermissionLevel(userID) >= PermissionLevelAdmin -} - -func (pc PermissionConfig) GetPermissionLevel(userID id.UserID) PermissionLevel { - permissions, ok := pc[string(userID)] - if ok { - return permissions - } - - _, homeserver, _ := userID.Parse() - permissions, ok = pc[homeserver] - if len(homeserver) > 0 && ok { - return permissions - } - - permissions, ok = pc["*"] - if ok { - return permissions - } - - return PermissionLevelDefault -} - type RelaybotConfig struct { - Enabled bool `yaml:"enabled"` - ManagementRoom id.RoomID `yaml:"management"` - InviteUsers []id.UserID `yaml:"invites"` - + Enabled bool `yaml:"enabled"` + AdminOnly bool `yaml:"admin_only"` MessageFormats map[event.MessageType]string `yaml:"message_formats"` messageTemplates *template.Template `yaml:"-"` } @@ -319,8 +217,8 @@ func (rc *RelaybotConfig) UnmarshalYAML(unmarshal func(interface{}) error) error } type Sender struct { - UserID id.UserID - *event.MemberEventContent + UserID string + event.MemberEventContent } type formatData struct { @@ -329,11 +227,15 @@ type formatData struct { Content *event.MessageEventContent } -func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member *event.MemberEventContent) (string, error) { +func (rc *RelaybotConfig) FormatMessage(content *event.MessageEventContent, sender id.UserID, member event.MemberEventContent) (string, error) { + if len(member.Displayname) == 0 { + member.Displayname = sender.String() + } + member.Displayname = template.HTMLEscapeString(member.Displayname) var output strings.Builder err := rc.messageTemplates.ExecuteTemplate(&output, string(content.MsgType), formatData{ Sender: Sender{ - UserID: sender, + UserID: template.HTMLEscapeString(sender.String()), MemberEventContent: member, }, Content: content, diff --git a/config/config.go b/config/config.go index c51e462..5ec3548 100644 --- a/config/config.go +++ b/config/config.go @@ -17,50 +17,14 @@ package config import ( - "io/ioutil" - - "gopkg.in/yaml.v2" - - "maunium.net/go/mautrix/appservice" + "maunium.net/go/mautrix/bridge/bridgeconfig" + "maunium.net/go/mautrix/id" ) type Config struct { - Homeserver struct { - Address string `yaml:"address"` - Domain string `yaml:"domain"` - Asmux bool `yaml:"asmux"` - } `yaml:"homeserver"` + *bridgeconfig.BaseConfig `yaml:",inline"` - AppService struct { - Address string `yaml:"address"` - Hostname string `yaml:"hostname"` - Port uint16 `yaml:"port"` - - Database struct { - Type string `yaml:"type"` - URI string `yaml:"uri"` - - MaxOpenConns int `yaml:"max_open_conns"` - MaxIdleConns int `yaml:"max_idle_conns"` - } `yaml:"database"` - - StateStore string `yaml:"state_store_path,omitempty"` - - Provisioning struct { - Prefix string `yaml:"prefix"` - SharedSecret string `yaml:"shared_secret"` - } `yaml:"provisioning"` - - ID string `yaml:"id"` - Bot struct { - Username string `yaml:"username"` - Displayname string `yaml:"displayname"` - Avatar string `yaml:"avatar"` - } `yaml:"bot"` - - ASToken string `yaml:"as_token"` - HSToken string `yaml:"hs_token"` - } `yaml:"appservice"` + SegmentKey string `yaml:"segment_key"` Metrics struct { Enabled bool `yaml:"enabled"` @@ -68,50 +32,28 @@ type Config struct { } `yaml:"metrics"` GroupMe struct { - OSName string `yaml:"os_name"` - BrowserName string `yaml:"browser_name"` + OSName string `yaml:"os_name"` + BrowserName string `yaml:"browser_name"` + ConnectionTimeout int `yaml:"connection_timeout"` } `yaml:"groupme"` Bridge BridgeConfig `yaml:"bridge"` - - Logging appservice.LogConfig `yaml:"logging"` } -func (config *Config) setDefaults() { - config.AppService.Database.MaxOpenConns = 20 - config.AppService.Database.MaxIdleConns = 2 - config.GroupMe.OSName = "Go GroupMe bridge" - config.GroupMe.BrowserName = "mx-gm" - config.Bridge.setDefaults() +func (config *Config) CanAutoDoublePuppet(userID id.UserID) bool { + _, homeserver, _ := userID.Parse() + _, hasSecret := config.Bridge.LoginSharedSecretMap[homeserver] + return hasSecret } -func Load(path string) (*Config, error) { - data, err := ioutil.ReadFile(path) - if err != nil { - return nil, err +func (config *Config) CanDoublePuppetBackfill(userID id.UserID) bool { + if !config.Bridge.HistorySync.DoublePuppetBackfill { + return false } - - var config = &Config{} - config.setDefaults() - err = yaml.Unmarshal(data, config) - return config, err -} - -func (config *Config) Save(path string) error { - data, err := yaml.Marshal(config) - if err != nil { - return err + _, homeserver, _ := userID.Parse() + // Batch sending can only use local users, so don't allow double puppets on other servers. + if homeserver != config.Homeserver.Domain && config.Homeserver.Software != bridgeconfig.SoftwareHungry { + return false } - return ioutil.WriteFile(path, data, 0600) -} - -func (config *Config) MakeAppService() (*appservice.AppService, error) { - as := appservice.Create() - as.HomeserverDomain = config.Homeserver.Domain - as.HomeserverURL = config.Homeserver.Address - as.Host.Hostname = config.AppService.Hostname - as.Host.Port = config.AppService.Port - var err error - as.Registration, err = config.GetRegistration() - return as, err + return true } diff --git a/config/registration.go b/config/registration.go deleted file mode 100644 index f6a1505..0000000 --- a/config/registration.go +++ /dev/null @@ -1,73 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package config - -import ( - "fmt" - "regexp" - - "maunium.net/go/mautrix/appservice" -) - -func (config *Config) NewRegistration() (*appservice.Registration, error) { - registration := appservice.CreateRegistration() - - err := config.copyToRegistration(registration) - if err != nil { - return nil, err - } - - config.AppService.ASToken = registration.AppToken - config.AppService.HSToken = registration.ServerToken - - // Workaround for https://github.com/matrix-org/synapse/pull/5758 - registration.SenderLocalpart = appservice.RandomString(32) - botRegex := regexp.MustCompile(fmt.Sprintf("^@%s:%s$", config.AppService.Bot.Username, config.Homeserver.Domain)) - registration.Namespaces.RegisterUserIDs(botRegex, true) - - return registration, nil -} - -func (config *Config) GetRegistration() (*appservice.Registration, error) { - registration := appservice.CreateRegistration() - - err := config.copyToRegistration(registration) - if err != nil { - return nil, err - } - - registration.AppToken = config.AppService.ASToken - registration.ServerToken = config.AppService.HSToken - return registration, nil -} - -func (config *Config) copyToRegistration(registration *appservice.Registration) error { - registration.ID = config.AppService.ID - registration.URL = config.AppService.Address - falseVal := false - registration.RateLimited = &falseVal - registration.SenderLocalpart = config.AppService.Bot.Username - - userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$", - config.Bridge.FormatUsername("[0-9]+"), - config.Homeserver.Domain)) - if err != nil { - return err - } - registration.Namespaces.RegisterUserIDs(userIDRegex, true) - return nil -} diff --git a/config/upgrade.go b/config/upgrade.go new file mode 100644 index 0000000..c15c58e --- /dev/null +++ b/config/upgrade.go @@ -0,0 +1,138 @@ +package config + +import ( + "strings" + + "maunium.net/go/mautrix/bridge/bridgeconfig" + "maunium.net/go/mautrix/util" + up "maunium.net/go/mautrix/util/configupgrade" +) + +func DoUpgrade(helper *up.Helper) { + bridgeconfig.Upgrader.DoUpgrade(helper) + + helper.Copy(up.Str|up.Null, "segment_key") + + helper.Copy(up.Bool, "metrics", "enabled") + helper.Copy(up.Str, "metrics", "listen") + + helper.Copy(up.Int, "groupme", "connection_timeout") + helper.Copy(up.Bool, "groupme", "fetch_message_on_timeout") + + helper.Copy(up.Str, "bridge", "username_template") + helper.Copy(up.Str, "bridge", "displayname_template") + helper.Copy(up.Bool, "bridge", "personal_filtering_spaces") + helper.Copy(up.Bool, "bridge", "delivery_receipts") + helper.Copy(up.Bool, "bridge", "message_status_events") + helper.Copy(up.Bool, "bridge", "message_error_notices") + + helper.Copy(up.Int, "bridge", "portal_message_buffer") + helper.Copy(up.Bool, "bridge", "call_start_notices") + helper.Copy(up.Bool, "bridge", "identity_change_notices") + helper.Copy(up.Bool, "bridge", "user_avatar_sync") + helper.Copy(up.Bool, "bridge", "bridge_matrix_leave") + helper.Copy(up.Bool, "bridge", "sync_with_custom_puppets") + helper.Copy(up.Bool, "bridge", "sync_direct_chat_list") + helper.Copy(up.Bool, "bridge", "default_bridge_receipts") + helper.Copy(up.Bool, "bridge", "default_bridge_presence") + helper.Copy(up.Bool, "bridge", "send_presence_on_typing") + helper.Copy(up.Bool, "bridge", "force_active_delivery_receipts") + helper.Copy(up.Map, "bridge", "double_puppet_server_map") + helper.Copy(up.Bool, "bridge", "double_puppet_allow_discovery") + if legacySecret, ok := helper.Get(up.Str, "bridge", "login_shared_secret"); ok && len(legacySecret) > 0 { + baseNode := helper.GetBaseNode("bridge", "login_shared_secret_map") + baseNode.Map[helper.GetBase("homeserver", "domain")] = up.StringNode(legacySecret) + baseNode.UpdateContent() + } else { + helper.Copy(up.Map, "bridge", "login_shared_secret_map") + } + helper.Copy(up.Bool, "bridge", "private_chat_portal_meta") + helper.Copy(up.Bool, "bridge", "parallel_member_sync") + helper.Copy(up.Bool, "bridge", "bridge_notices") + helper.Copy(up.Bool, "bridge", "resend_bridge_info") + helper.Copy(up.Bool, "bridge", "mute_bridging") + helper.Copy(up.Str|up.Null, "bridge", "archive_tag") + helper.Copy(up.Str|up.Null, "bridge", "pinned_tag") + helper.Copy(up.Bool, "bridge", "tag_only_on_create") + helper.Copy(up.Bool, "bridge", "enable_status_broadcast") + helper.Copy(up.Bool, "bridge", "disable_status_broadcast_send") + helper.Copy(up.Bool, "bridge", "mute_status_broadcast") + helper.Copy(up.Str|up.Null, "bridge", "status_broadcast_tag") + helper.Copy(up.Bool, "bridge", "whatsapp_thumbnail") + helper.Copy(up.Bool, "bridge", "allow_user_invite") + helper.Copy(up.Str, "bridge", "command_prefix") + helper.Copy(up.Bool, "bridge", "federate_rooms") + helper.Copy(up.Bool, "bridge", "disappearing_messages_in_groups") + helper.Copy(up.Bool, "bridge", "disable_bridge_alerts") + helper.Copy(up.Bool, "bridge", "crash_on_stream_replaced") + helper.Copy(up.Bool, "bridge", "url_previews") + helper.Copy(up.Bool, "bridge", "caption_in_message") + helper.Copy(up.Bool, "bridge", "send_whatsapp_edits") + helper.Copy(up.Str|up.Null, "bridge", "message_handling_timeout", "error_after") + helper.Copy(up.Str|up.Null, "bridge", "message_handling_timeout", "deadline") + + helper.Copy(up.Str, "bridge", "management_room_text", "welcome") + helper.Copy(up.Str, "bridge", "management_room_text", "welcome_connected") + helper.Copy(up.Str, "bridge", "management_room_text", "welcome_unconnected") + helper.Copy(up.Str|up.Null, "bridge", "management_room_text", "additional_help") + helper.Copy(up.Bool, "bridge", "encryption", "allow") + helper.Copy(up.Bool, "bridge", "encryption", "default") + helper.Copy(up.Bool, "bridge", "encryption", "require") + helper.Copy(up.Bool, "bridge", "encryption", "appservice") + helper.Copy(up.Str, "bridge", "encryption", "verification_levels", "receive") + helper.Copy(up.Str, "bridge", "encryption", "verification_levels", "send") + helper.Copy(up.Str, "bridge", "encryption", "verification_levels", "share") + + legacyKeyShareAllow, ok := helper.Get(up.Bool, "bridge", "encryption", "key_sharing", "allow") + if ok { + helper.Set(up.Bool, legacyKeyShareAllow, "bridge", "encryption", "allow_key_sharing") + legacyKeyShareRequireCS, legacyOK1 := helper.Get(up.Bool, "bridge", "encryption", "key_sharing", "require_cross_signing") + legacyKeyShareRequireVerification, legacyOK2 := helper.Get(up.Bool, "bridge", "encryption", "key_sharing", "require_verification") + if legacyOK1 && legacyOK2 && legacyKeyShareRequireVerification == "false" && legacyKeyShareRequireCS == "false" { + helper.Set(up.Str, "unverified", "bridge", "encryption", "verification_levels", "share") + } + } else { + helper.Copy(up.Bool, "bridge", "encryption", "allow_key_sharing") + } + + helper.Copy(up.Bool, "bridge", "encryption", "rotation", "enable_custom") + helper.Copy(up.Int, "bridge", "encryption", "rotation", "milliseconds") + helper.Copy(up.Int, "bridge", "encryption", "rotation", "messages") + if prefix, ok := helper.Get(up.Str, "appservice", "provisioning", "prefix"); ok { + helper.Set(up.Str, strings.TrimSuffix(prefix, "/v1"), "bridge", "provisioning", "prefix") + } else { + helper.Copy(up.Str, "bridge", "provisioning", "prefix") + } + if secret, ok := helper.Get(up.Str, "appservice", "provisioning", "shared_secret"); ok && secret != "generate" { + helper.Set(up.Str, secret, "bridge", "provisioning", "shared_secret") + } else if secret, ok = helper.Get(up.Str, "bridge", "provisioning", "shared_secret"); !ok || secret == "generate" { + sharedSecret := util.RandomString(64) + helper.Set(up.Str, sharedSecret, "bridge", "provisioning", "shared_secret") + } else { + helper.Copy(up.Str, "bridge", "provisioning", "shared_secret") + } + helper.Copy(up.Map, "bridge", "permissions") + helper.Copy(up.Bool, "bridge", "relay", "enabled") + helper.Copy(up.Bool, "bridge", "relay", "admin_only") + helper.Copy(up.Map, "bridge", "relay", "message_formats") +} + +var SpacedBlocks = [][]string{ + {"homeserver", "software"}, + {"appservice"}, + {"appservice", "hostname"}, + {"appservice", "database"}, + {"appservice", "id"}, + {"appservice", "as_token"}, + {"segment_key"}, + {"metrics"}, + {"groupme"}, + {"bridge"}, + {"bridge", "command_prefix"}, + {"bridge", "management_room_text"}, + {"bridge", "encryption"}, + {"bridge", "provisioning"}, + {"bridge", "permissions"}, + {"bridge", "relay"}, + {"logging"}, +} diff --git a/crypto.go b/crypto.go deleted file mode 100644 index 65d3a37..0000000 --- a/crypto.go +++ /dev/null @@ -1,335 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -//go:build cgo && !nocrypto -// +build cgo,!nocrypto - -package main - -import ( - "fmt" - "runtime/debug" - "time" - - "maunium.net/go/maulogger/v2" - - "maunium.net/go/mautrix" - "maunium.net/go/mautrix/crypto" - "maunium.net/go/mautrix/event" - "maunium.net/go/mautrix/id" - - "github.com/beeper/groupme/database" -) - -var NoSessionFound = crypto.NoSessionFound - -var levelTrace = maulogger.Level{ - Name: "Trace", - Severity: -10, - Color: -1, -} - -type CryptoHelper struct { - bridge *Bridge - client *mautrix.Client - mach *crypto.OlmMachine - store *database.SQLCryptoStore - log maulogger.Logger - baseLog maulogger.Logger -} - -func NewCryptoHelper(bridge *Bridge) Crypto { - if !bridge.Config.Bridge.Encryption.Allow { - bridge.Log.Debugln("Bridge built with end-to-bridge encryption, but disabled in config") - return nil - } - baseLog := bridge.Log.Sub("Crypto") - return &CryptoHelper{ - bridge: bridge, - log: baseLog.Sub("Helper"), - baseLog: baseLog, - } -} - -func (helper *CryptoHelper) Init() error { - helper.log.Debugln("Initializing end-to-bridge encryption...") - - helper.store = database.NewSQLCryptoStore(helper.bridge.DB, helper.bridge.AS.BotMXID(), - fmt.Sprintf("@%s:%s", helper.bridge.Config.Bridge.FormatUsername("%"), helper.bridge.AS.HomeserverDomain)) - - var err error - helper.client, err = helper.loginBot() - if err != nil { - return err - } - - helper.log.Debugln("Logged in as bridge bot with device ID", helper.client.DeviceID) - logger := &cryptoLogger{helper.baseLog} - stateStore := &cryptoStateStore{helper.bridge} - helper.mach = crypto.NewOlmMachine(helper.client, logger, helper.store, stateStore) - helper.mach.AllowKeyShare = helper.allowKeyShare - - helper.client.Syncer = &cryptoSyncer{helper.mach} - helper.client.Store = &cryptoClientStore{helper.store} - - return helper.mach.Load() -} - -func (helper *CryptoHelper) allowKeyShare(device *crypto.DeviceIdentity, info event.RequestedKeyInfo) *crypto.KeyShareRejection { - cfg := helper.bridge.Config.Bridge.Encryption.KeySharing - if !cfg.Allow { - return &crypto.KeyShareRejectNoResponse - } else if device.Trust == crypto.TrustStateBlacklisted { - return &crypto.KeyShareRejectBlacklisted - } else if device.Trust == crypto.TrustStateVerified || !cfg.RequireVerification { - portal := helper.bridge.GetPortalByMXID(info.RoomID) - if portal == nil { - helper.log.Debugfln("Rejecting key request for %s from %s/%s: room is not a portal", info.SessionID, device.UserID, device.DeviceID) - return &crypto.KeyShareRejection{Code: event.RoomKeyWithheldUnavailable, Reason: "Requested room is not a portal room"} - } - user := helper.bridge.GetUserByMXID(device.UserID) - if !user.Admin && !user.IsInPortal(portal.Key) { - helper.log.Debugfln("Rejecting key request for %s from %s/%s: user is not in portal", info.SessionID, device.UserID, device.DeviceID) - return &crypto.KeyShareRejection{Code: event.RoomKeyWithheldUnauthorized, Reason: "You're not in that portal"} - } - helper.log.Debugfln("Accepting key request for %s from %s/%s", info.SessionID, device.UserID, device.DeviceID) - return nil - } else { - return &crypto.KeyShareRejectUnverified - } -} - -func (helper *CryptoHelper) loginBot() (*mautrix.Client, error) { - deviceID := helper.store.FindDeviceID() - if len(deviceID) > 0 { - helper.log.Debugln("Found existing device ID for bot in database:", deviceID) - } - client, err := mautrix.NewClient(helper.bridge.AS.HomeserverURL, "", "") - if err != nil { - return nil, fmt.Errorf("failed to initialize client: %w", err) - } - client.Logger = helper.baseLog.Sub("Bot") - client.Client = helper.bridge.AS.HTTPClient - client.DefaultHTTPRetries = helper.bridge.AS.DefaultHTTPRetries - flows, err := client.GetLoginFlows() - if err != nil { - return nil, fmt.Errorf("failed to get supported login flows: %w", err) - } - if !flows.HasFlow(mautrix.AuthTypeHalfyAppservice) { - return nil, fmt.Errorf("homeserver does not support appservice login") - } - // if !flows.HasFlow(mautrix.AuthTypeAppservice) { - // // TODO after synapse 1.22, turn this into an error - // helper.log.Warnln("Encryption enabled in config, but homeserver does not advertise appservice login") - // //return nil, fmt.Errorf("homeserver does not support appservice login") - // } - - // We set the API token to the AS token here to authenticate the appservice login - // It'll get overridden after the login - client.AccessToken = helper.bridge.AS.Registration.AppToken - resp, err := client.Login(&mautrix.ReqLogin{ - Type: mautrix.AuthTypeHalfyAppservice, - Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(helper.bridge.AS.BotMXID())}, - DeviceID: deviceID, - InitialDeviceDisplayName: "GroupMe Bridge", - StoreCredentials: true, - }) - if err != nil { - return nil, fmt.Errorf("failed to log in as bridge bot: %w", err) - } - helper.store.DeviceID = resp.DeviceID - return client, nil - - // client.AccessToken = helper.bridge.AS.Registration.AppToken - // resp, err := client.Login(&mautrix.ReqLogin{ - // Type: mautrix.AuthTypeAppservice, - // Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(helper.bridge.AS.BotMXID())}, - // DeviceID: deviceID, - // InitialDeviceDisplayName: "GroupMe Bridge", - // StoreCredentials: true, - // }) - // if err != nil { - // return nil, fmt.Errorf("failed to log in as bridge bot: %w", err) - // } - // if len(deviceID) == 0 { - // helper.store.DeviceID = resp.DeviceID - // } - // return client, nil -} - -func (helper *CryptoHelper) Start() { - helper.log.Debugln("Starting syncer for receiving to-device messages") - err := helper.client.Sync() - if err != nil { - helper.log.Errorln("Fatal error syncing:", err) - } else { - helper.log.Infoln("Bridge bot to-device syncer stopped without error") - } -} - -func (helper *CryptoHelper) Stop() { - helper.log.Debugln("CryptoHelper.Stop() called, stopping bridge bot sync") - helper.client.StopSync() -} - -func (helper *CryptoHelper) Decrypt(evt *event.Event) (*event.Event, error) { - return helper.mach.DecryptMegolmEvent(evt) -} - -func (helper *CryptoHelper) Encrypt(roomID id.RoomID, evtType event.Type, content event.Content) (*event.EncryptedEventContent, error) { - encrypted, err := helper.mach.EncryptMegolmEvent(roomID, evtType, &content) - if err != nil { - if err != crypto.SessionExpired && err != crypto.SessionNotShared && err != crypto.NoGroupSession { - return nil, err - } - helper.log.Debugfln("Got %v while encrypting event for %s, sharing group session and trying again...", err, roomID) - users, err := helper.store.GetRoomMembers(roomID) - if err != nil { - return nil, fmt.Errorf("failed to get room member list: %w", err) - } - err = helper.mach.ShareGroupSession(roomID, users) - if err != nil { - return nil, fmt.Errorf("failed to share group session: %w", err) - } - encrypted, err = helper.mach.EncryptMegolmEvent(roomID, evtType, &content) - if err != nil { - return nil, fmt.Errorf("failed to encrypt event after re-sharing group session: %w", err) - } - } - return encrypted, nil -} - -func (helper *CryptoHelper) WaitForSession(roomID id.RoomID, senderKey id.SenderKey, sessionID id.SessionID, timeout time.Duration) bool { - return helper.mach.WaitForSession(roomID, senderKey, sessionID, timeout) -} - -func (helper *CryptoHelper) ResetSession(roomID id.RoomID) { - err := helper.mach.CryptoStore.RemoveOutboundGroupSession(roomID) - if err != nil { - helper.log.Debugfln("Error manually removing outbound group session in %s: %v", roomID, err) - } -} - -func (helper *CryptoHelper) HandleMemberEvent(evt *event.Event) { - helper.mach.HandleMemberEvent(evt) -} - -type cryptoSyncer struct { - *crypto.OlmMachine -} - -func (syncer *cryptoSyncer) ProcessResponse(resp *mautrix.RespSync, since string) error { - done := make(chan struct{}) - go func() { - defer func() { - if err := recover(); err != nil { - syncer.Log.Error("Processing sync response (%s) panicked: %v\n%s", since, err, debug.Stack()) - } - done <- struct{}{} - }() - syncer.Log.Trace("Starting sync response handling (%s)", since) - syncer.ProcessSyncResponse(resp, since) - syncer.Log.Trace("Successfully handled sync response (%s)", since) - }() - select { - case <-done: - case <-time.After(30 * time.Second): - syncer.Log.Warn("Handling sync response (%s) is taking unusually long", since) - } - return nil -} - -func (syncer *cryptoSyncer) OnFailedSync(_ *mautrix.RespSync, err error) (time.Duration, error) { - syncer.Log.Error("Error /syncing, waiting 10 seconds: %v", err) - return 10 * time.Second, nil -} - -func (syncer *cryptoSyncer) GetFilterJSON(_ id.UserID) *mautrix.Filter { - everything := []event.Type{{Type: "*"}} - return &mautrix.Filter{ - Presence: mautrix.FilterPart{NotTypes: everything}, - AccountData: mautrix.FilterPart{NotTypes: everything}, - Room: mautrix.RoomFilter{ - IncludeLeave: false, - Ephemeral: mautrix.FilterPart{NotTypes: everything}, - AccountData: mautrix.FilterPart{NotTypes: everything}, - State: mautrix.FilterPart{NotTypes: everything}, - Timeline: mautrix.FilterPart{NotTypes: everything}, - }, - } -} - -type cryptoLogger struct { - int maulogger.Logger -} - -func (c *cryptoLogger) Error(message string, args ...interface{}) { - c.int.Errorfln(message, args...) -} - -func (c *cryptoLogger) Warn(message string, args ...interface{}) { - c.int.Warnfln(message, args...) -} - -func (c *cryptoLogger) Debug(message string, args ...interface{}) { - c.int.Debugfln(message, args...) -} - -func (c *cryptoLogger) Trace(message string, args ...interface{}) { - c.int.Logfln(levelTrace, message, args...) -} - -type cryptoClientStore struct { - int *database.SQLCryptoStore -} - -func (c cryptoClientStore) SaveFilterID(_ id.UserID, _ string) {} -func (c cryptoClientStore) LoadFilterID(_ id.UserID) string { return "" } -func (c cryptoClientStore) SaveRoom(_ *mautrix.Room) {} -func (c cryptoClientStore) LoadRoom(_ id.RoomID) *mautrix.Room { return nil } - -func (c cryptoClientStore) SaveNextBatch(_ id.UserID, nextBatchToken string) { - c.int.PutNextBatch(nextBatchToken) -} - -func (c cryptoClientStore) LoadNextBatch(_ id.UserID) string { - return c.int.GetNextBatch() -} - -var _ mautrix.Storer = (*cryptoClientStore)(nil) - -type cryptoStateStore struct { - bridge *Bridge -} - -var _ crypto.StateStore = (*cryptoStateStore)(nil) - -func (c *cryptoStateStore) IsEncrypted(id id.RoomID) bool { - portal := c.bridge.GetPortalByMXID(id) - if portal != nil { - return portal.Encrypted - } - return false -} - -func (c *cryptoStateStore) FindSharedRooms(id id.UserID) []id.RoomID { - return c.bridge.StateStore.FindSharedRooms(id) -} - -func (c *cryptoStateStore) GetEncryptionEvent(id.RoomID) *event.EncryptionEventContent { - // TODO implement - return nil -} diff --git a/custompuppet.go b/custompuppet.go index 1ce8ab5..d32d346 100644 --- a/custompuppet.go +++ b/custompuppet.go @@ -21,6 +21,7 @@ import ( "crypto/sha512" "encoding/hex" "errors" + "fmt" "time" "maunium.net/go/mautrix" @@ -42,7 +43,7 @@ func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error puppet.CustomMXID = mxid puppet.AccessToken = accessToken - err := puppet.StartCustomMXID() + err := puppet.StartCustomMXID(false) if err != nil { return err } @@ -53,7 +54,6 @@ func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error if len(puppet.CustomMXID) > 0 { puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet } - puppet.EnablePresence = puppet.bridge.Config.Bridge.DefaultBridgePresence puppet.EnableReceipts = puppet.bridge.Config.Bridge.DefaultBridgeReceipts puppet.bridge.AS.StateStore.MarkRegistered(puppet.CustomMXID) puppet.Update() @@ -62,31 +62,72 @@ func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error } func (puppet *Puppet) loginWithSharedSecret(mxid id.UserID) (string, error) { + _, homeserver, _ := mxid.Parse() puppet.log.Debugfln("Logging into %s with shared secret", mxid) - mac := hmac.New(sha512.New, []byte(puppet.bridge.Config.Bridge.LoginSharedSecret)) - mac.Write([]byte(mxid)) - resp, err := puppet.bridge.AS.BotClient().Login(&mautrix.ReqLogin{ - Type: mautrix.AuthTypePassword, + loginSecret := puppet.bridge.Config.Bridge.LoginSharedSecretMap[homeserver] + client, err := puppet.bridge.newDoublePuppetClient(mxid, "") + if err != nil { + return "", fmt.Errorf("failed to create mautrix client to log in: %v", err) + } + req := mautrix.ReqLogin{ Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(mxid)}, - Password: hex.EncodeToString(mac.Sum(nil)), - DeviceID: "WhatsApp Bridge", - InitialDeviceDisplayName: "WhatsApp Bridge", - }) + DeviceID: "GroupMe Bridge", + InitialDeviceDisplayName: "GroupMe Bridge", + } + if loginSecret == "appservice" { + client.AccessToken = puppet.bridge.AS.Registration.AppToken + req.Type = mautrix.AuthTypeAppservice + } else { + mac := hmac.New(sha512.New, []byte(loginSecret)) + mac.Write([]byte(mxid)) + req.Password = hex.EncodeToString(mac.Sum(nil)) + req.Type = mautrix.AuthTypePassword + } + resp, err := client.Login(&req) if err != nil { return "", err } return resp.AccessToken, nil } +func (br *GMBridge) newDoublePuppetClient(mxid id.UserID, accessToken string) (*mautrix.Client, error) { + _, homeserver, err := mxid.Parse() + if err != nil { + return nil, err + } + homeserverURL, found := br.Config.Bridge.DoublePuppetServerMap[homeserver] + if !found { + if homeserver == br.AS.HomeserverDomain { + homeserverURL = br.AS.HomeserverURL + } else if br.Config.Bridge.DoublePuppetAllowDiscovery { + resp, err := mautrix.DiscoverClientAPI(homeserver) + if err != nil { + return nil, fmt.Errorf("failed to find homeserver URL for %s: %v", homeserver, err) + } + homeserverURL = resp.Homeserver.BaseURL + br.Log.Debugfln("Discovered URL %s for %s to enable double puppeting for %s", homeserverURL, homeserver, mxid) + } else { + return nil, fmt.Errorf("double puppeting from %s is not allowed", homeserver) + } + } + client, err := mautrix.NewClient(homeserverURL, mxid, accessToken) + if err != nil { + return nil, err + } + client.Logger = br.AS.Log.Sub(mxid.String()) + client.Client = br.AS.HTTPClient + client.DefaultHTTPRetries = br.AS.DefaultHTTPRetries + return client, nil +} + func (puppet *Puppet) newCustomIntent() (*appservice.IntentAPI, error) { if len(puppet.CustomMXID) == 0 { return nil, ErrNoCustomMXID } - client, err := mautrix.NewClient(puppet.bridge.AS.HomeserverURL, puppet.CustomMXID, puppet.AccessToken) + client, err := puppet.bridge.newDoublePuppetClient(puppet.CustomMXID, puppet.AccessToken) if err != nil { return nil, err } - client.Logger = puppet.bridge.AS.Log.Sub(string(puppet.CustomMXID)) client.Syncer = puppet client.Store = puppet @@ -102,11 +143,10 @@ func (puppet *Puppet) clearCustomMXID() { puppet.CustomMXID = "" puppet.AccessToken = "" puppet.customIntent = nil - puppet.customTypingIn = nil puppet.customUser = nil } -func (puppet *Puppet) StartCustomMXID() error { +func (puppet *Puppet) StartCustomMXID(reloginOnFail bool) error { if len(puppet.CustomMXID) == 0 { puppet.clearCustomMXID() return nil @@ -118,15 +158,16 @@ func (puppet *Puppet) StartCustomMXID() error { } resp, err := intent.Whoami() if err != nil { - puppet.clearCustomMXID() - return err - } - if resp.UserID != puppet.CustomMXID { + if !reloginOnFail || (errors.Is(err, mautrix.MUnknownToken) && !puppet.tryRelogin(err, "initializing double puppeting")) { + puppet.clearCustomMXID() + return err + } + intent.AccessToken = puppet.AccessToken + } else if resp.UserID != puppet.CustomMXID { puppet.clearCustomMXID() return ErrMismatchingMXID } puppet.customIntent = intent - puppet.customTypingIn = make(map[id.RoomID]bool) puppet.customUser = puppet.bridge.GetUserByMXID(puppet.CustomMXID) puppet.startSyncing() return nil @@ -154,16 +195,13 @@ func (puppet *Puppet) stopSyncing() { } func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, _ string) error { - if !puppet.customUser.IsConnected() { - puppet.log.Debugln("Skipping sync processing: custom user not connected to whatsapp") + if !puppet.customUser.IsLoggedIn() { + puppet.log.Debugln("Skipping sync processing: custom user not connected to GroupMe") return nil } for roomID, events := range resp.Rooms.Join { - portal := puppet.bridge.GetPortalByMXID(roomID) - if portal == nil { - continue - } for _, evt := range events.Ephemeral.Events { + evt.RoomID = roomID err := evt.Content.ParseRaw(evt.Type) if err != nil { continue @@ -171,85 +209,40 @@ func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, _ string) error { switch evt.Type { case event.EphemeralEventReceipt: if puppet.EnableReceipts { - go puppet.handleReceiptEvent(portal, evt) + go puppet.bridge.MatrixHandler.HandleReceipt(evt) } case event.EphemeralEventTyping: - go puppet.handleTypingEvent(portal, evt) + go puppet.bridge.MatrixHandler.HandleTyping(evt) } } } - if puppet.EnablePresence { - for _, evt := range resp.Presence.Events { - if evt.Sender != puppet.CustomMXID { - continue - } - err := evt.Content.ParseRaw(evt.Type) - if err != nil { - continue - } - go puppet.handlePresenceEvent(evt) - } - } return nil } -func (puppet *Puppet) handlePresenceEvent(event *event.Event) { - // presence := whatsapp.PresenceAvailable - // if event.Content.Raw["presence"].(string) != "online" { - // presence = whatsapp.PresenceUnavailable - // puppet.customUser.log.Debugln("Marking offline") - // } else { - // puppet.customUser.log.Debugln("Marking online") - // } - // _, err := puppet.customUser.Conn.Presence("", presence) - // if err != nil { - // puppet.customUser.log.Warnln("Failed to set presence:", err) - // } -} - -func (puppet *Puppet) handleReceiptEvent(portal *Portal, event *event.Event) { - // for eventID, receipts := range *event.Content.AsReceipt() { - // if _, ok := receipts.Read[puppet.CustomMXID]; !ok { - // continue - // } - // message := puppet.bridge.DB.Message.GetByMXID(eventID) - // if message == nil { - // continue - // } - // puppet.customUser.log.Debugfln("Marking %s/%s in %s/%s as read", message.JID, message.MXID, portal.Key.JID, portal.MXID) - // _, err := puppet.customUser.Conn.Read(portal.Key.JID, message.JID) - // if err != nil { - // puppet.customUser.log.Warnln("Error marking read:", err) - // } - // } -} - -func (puppet *Puppet) handleTypingEvent(portal *Portal, evt *event.Event) { - // isTyping := false - // for _, userID := range evt.Content.AsTyping().UserIDs { - // if userID == puppet.CustomMXID { - // isTyping = true - // break - // } - // } - // if puppet.customTypingIn[evt.RoomID] != isTyping { - // puppet.customTypingIn[evt.RoomID] = isTyping - // presence := whatsapp.PresenceComposing - // if !isTyping { - // puppet.customUser.log.Debugfln("Marking not typing in %s/%s", portal.Key.JID, portal.MXID) - // presence = whatsapp.PresencePaused - // } else { - // puppet.customUser.log.Debugfln("Marking typing in %s/%s", portal.Key.JID, portal.MXID) - // } - // _, err := puppet.customUser.Conn.Presence(portal.Key.JID, presence) - // if err != nil { - // puppet.customUser.log.Warnln("Error setting typing:", err) - // } - // } +func (puppet *Puppet) tryRelogin(cause error, action string) bool { + if !puppet.bridge.Config.CanAutoDoublePuppet(puppet.CustomMXID) { + return false + } + puppet.log.Debugfln("Trying to relogin after '%v' while %s", cause, action) + accessToken, err := puppet.loginWithSharedSecret(puppet.CustomMXID) + if err != nil { + puppet.log.Errorfln("Failed to relogin after '%v' while %s: %v", cause, action, err) + return false + } + puppet.log.Infofln("Successfully relogined after '%v' while %s", cause, action) + puppet.AccessToken = accessToken + return true } func (puppet *Puppet) OnFailedSync(_ *mautrix.RespSync, err error) (time.Duration, error) { puppet.log.Warnln("Sync error:", err) + if errors.Is(err, mautrix.MUnknownToken) { + if !puppet.tryRelogin(err, "syncing") { + return 0, err + } + puppet.customIntent.AccessToken = puppet.AccessToken + return 0, nil + } return 10 * time.Second, nil } diff --git a/database/cryptostore.go b/database/cryptostore.go deleted file mode 100644 index 3bf81d7..0000000 --- a/database/cryptostore.go +++ /dev/null @@ -1,107 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -//go:build cgo && !nocrypto -// +build cgo,!nocrypto - -package database - -import ( - "database/sql" - - log "maunium.net/go/maulogger/v2" - - "maunium.net/go/mautrix/crypto" - "maunium.net/go/mautrix/id" -) - -type SQLCryptoStore struct { - *crypto.SQLCryptoStore - UserID id.UserID - GhostIDFormat string -} - -var _ crypto.Store = (*SQLCryptoStore)(nil) - -func NewSQLCryptoStore(db *Database, userID id.UserID, ghostIDFormat string) *SQLCryptoStore { - raw, _ := db.DB.DB() - return &SQLCryptoStore{ - SQLCryptoStore: crypto.NewSQLCryptoStore(raw, db.dialect, "", "", - []byte("github.com/beeper/groupme"), - &cryptoLogger{db.log.Sub("CryptoStore")}), - UserID: userID, - GhostIDFormat: ghostIDFormat, - } -} - -func (store *SQLCryptoStore) FindDeviceID() (deviceID id.DeviceID) { - err := store.DB.QueryRow("SELECT device_id FROM crypto_account WHERE account_id=$1", store.AccountID).Scan(&deviceID) - if err != nil && err != sql.ErrNoRows { - store.Log.Warn("Failed to scan device ID: %v", err) - } - return -} - -func (store *SQLCryptoStore) GetRoomMembers(roomID id.RoomID) (members []id.UserID, err error) { - var rows *sql.Rows - rows, err = store.DB.Query(` - SELECT user_id FROM mx_user_profile - WHERE room_id=$1 - AND (membership='join' OR membership='invite') - AND user_id<>$2 - AND user_id NOT LIKE $3 - `, roomID, store.UserID, store.GhostIDFormat) - if err != nil { - return - } - for rows.Next() { - var userID id.UserID - err := rows.Scan(&userID) - if err != nil { - store.Log.Warn("Failed to scan member in %s: %v", roomID, err) - } else { - members = append(members, userID) - } - } - return -} - -// TODO merge this with the one in the parent package -type cryptoLogger struct { - int log.Logger -} - -var levelTrace = log.Level{ - Name: "Trace", - Severity: -10, - Color: -1, -} - -func (c *cryptoLogger) Error(message string, args ...interface{}) { - c.int.Errorfln(message, args...) -} - -func (c *cryptoLogger) Warn(message string, args ...interface{}) { - c.int.Warnfln(message, args...) -} - -func (c *cryptoLogger) Debug(message string, args ...interface{}) { - c.int.Debugfln(message, args...) -} - -func (c *cryptoLogger) Trace(message string, args ...interface{}) { - c.int.Logfln(levelTrace, message, args...) -} diff --git a/database/database.go b/database/database.go index 9b61ba1..6c5c5aa 100644 --- a/database/database.go +++ b/database/database.go @@ -17,26 +17,21 @@ package database import ( - "os" - "strings" + "errors" + "net" + "github.com/lib/pq" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" - log "maunium.net/go/maulogger/v2" - - "gorm.io/driver/postgres" - "gorm.io/driver/sqlite" - "gorm.io/gorm" - "gorm.io/gorm/schema" + "maunium.net/go/maulogger/v2" + "maunium.net/go/mautrix/util/dbutil" "github.com/beeper/groupme/database/upgrades" ) type Database struct { - *gorm.DB - log log.Logger - dialect string + *dbutil.Database User *UserQuery Portal *PortalQuery @@ -45,92 +40,42 @@ type Database struct { Reaction *ReactionQuery } -func New(dbType string, uri string, baseLog log.Logger) (*Database, error) { - - var conn gorm.Dialector - - if dbType == "sqlite3" { - //_, _ = conn.Exec("PRAGMA foreign_keys = ON") - log.Fatalln("no sqlite for now only postgresql") - os.Exit(1) - conn = sqlite.Open(uri) - } else { - conn = postgres.Open(uri) - } - - gdb, err := gorm.Open(conn, &gorm.Config{ - // Logger: logger.Default.LogMode(logger.Info), - // Logger: baseLog, - - DisableForeignKeyConstraintWhenMigrating: true, - NamingStrategy: schema.NamingStrategy{ - NameReplacer: strings.NewReplacer("JID", "Jid", "MXID", "Mxid"), - }, - }) - if err != nil { - panic("failed to connect database") - } - db := &Database{ - DB: gdb, - log: baseLog.Sub("Database"), - dialect: dbType, - } +func New(baseDB *dbutil.Database, log maulogger.Logger) *Database { + db := &Database{Database: baseDB} + db.UpgradeTable = upgrades.Table db.User = &UserQuery{ db: db, - log: db.log.Sub("User"), + log: log.Sub("User"), } db.Portal = &PortalQuery{ db: db, - log: db.log.Sub("Portal"), + log: log.Sub("Portal"), } db.Puppet = &PuppetQuery{ db: db, - log: db.log.Sub("Puppet"), + log: log.Sub("Puppet"), } db.Message = &MessageQuery{ db: db, - log: db.log.Sub("Message"), + log: log.Sub("Message"), } db.Reaction = &ReactionQuery{ db: db, - log: db.log.Sub("Reaction"), + log: log.Sub("Reaction"), } - - return db, nil + return db } -func (db *Database) Init() error { - println("actual upgrade") - err := db.AutoMigrate(&Portal{}, &Puppet{}) - if err != nil { - return err +func isRetryableError(err error) bool { + if pqError := (&pq.Error{}); errors.As(err, &pqError) { + switch pqError.Code.Class() { + case "08", // Connection Exception + "53", // Insufficient Resources (e.g. too many connections) + "57": // Operator Intervention (e.g. server restart) + return true + } + } else if netError := (&net.OpError{}); errors.As(err, &netError) { + return true } - err = db.AutoMigrate(&Message{}) - if err != nil { - return err - } - - err = db.AutoMigrate(&Reaction{}) - if err != nil { - return err - } - - err = db.AutoMigrate(&mxRegistered{}, &MxUserProfile{}) - if err != nil { - return err - } - - err = db.AutoMigrate(&User{}) - if err != nil { - return err - } - err = db.AutoMigrate(&UserPortal{}) - if err != nil { - return err - } - return upgrades.Run(db.log.Sub("Upgrade"), db.dialect, db.DB) -} - -type Scannable interface { - Scan(...interface{}) error + return false } diff --git a/database/message.go b/database/message.go index 9c64614..b8299d3 100644 --- a/database/message.go +++ b/database/message.go @@ -17,11 +17,15 @@ package database import ( + "database/sql" + "errors" + "time" + log "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/util/dbutil" - "github.com/beeper/groupme/groupmeExt" - "github.com/beeper/groupme/types" + "github.com/karmanyaahm/groupme" ) type MessageQuery struct { @@ -36,101 +40,106 @@ func (mq *MessageQuery) New() *Message { } } +const ( + getAllMessagesSelect = ` + SELECT chat_gmid, chat_receiver, gmid, mxid, sender, timestamp, sent + FROM messages + ` + getAllMessagesQuery = getAllMessagesSelect + ` + WHERE chat_gmid=$1 AND chat_receiver=$2 + ` + getByGMIDQuery = getAllMessagesQuery + "AND jid=$3" + getByMXIDQuery = getAllMessagesSelect + "WHERE mxid=$1" + getLastMessageInChatQuery = getAllMessagesQuery + ` + AND timestamp<=$3 AND sent=true + ORDER BY timestamp DESC + LIMIT 1 + ` + getFirstMessageInChatQuery = getAllMessagesQuery + ` + AND sent=true + ORDER BY timestamp ASC + LIMIT 1 + ` + getMessagesBetweenQuery = getAllMessagesQuery + ` + AND timestamp>$3 AND timestamp<=$4 AND sent=true + ORDER BY timestamp ASC + ` +) + func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) { - ans := mq.db.Where("chat_jid = ? AND chat_receiver = ?", chat.JID, chat.Receiver).Find(&messages) - if ans.Error != nil || len(messages) == 0 { + rows, err := mq.db.Query(getAllMessagesQuery, chat.GMID, chat.Receiver) + if err != nil || rows == nil { return nil } + for rows.Next() { + messages = append(messages, mq.New().Scan(rows)) + } return } -func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *Message { - var message Message - ans := mq.db.Where("chat_jid = ? AND chat_receiver = ? AND jid = ?", chat.JID, chat.Receiver, jid).Limit(1).Find(&message) - if ans.Error != nil || ans.RowsAffected == 0 { - return nil - } - return &message +func (mq *MessageQuery) GetByGMID(chat PortalKey, gmid groupme.ID) *Message { + return mq.maybeScan(mq.db.QueryRow(getByGMIDQuery, chat.GMID, chat.Receiver, gmid)) } func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message { - var message Message - ans := mq.db.Where("mxid = ?", mxid).Limit(1).Find(&message) - if ans.Error != nil || ans.RowsAffected == 0 { - return nil - } - return &message + return mq.maybeScan(mq.db.QueryRow(getByMXIDQuery, mxid)) } func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message { - var message Message - ans := mq.db.Where("chat_jid = ? AND chat_receiver = ?", chat.JID, chat.Receiver).Order("timestamp desc").Limit(1).Find(&message) - if ans.Error != nil || ans.RowsAffected == 0 { + return mq.GetLastInChatBefore(chat, time.Now().Add(60*time.Second)) +} + +func (mq *MessageQuery) GetLastInChatBefore(chat PortalKey, maxTimestamp time.Time) *Message { + return mq.maybeScan(mq.db.QueryRow(getLastMessageInChatQuery, chat.GMID, chat.Receiver, maxTimestamp.Unix())) +} + +func (mq *MessageQuery) GetFirstInChat(chat PortalKey) *Message { + return mq.maybeScan(mq.db.QueryRow(getFirstMessageInChatQuery, chat.GMID, chat.Receiver)) +} + +func (mq *MessageQuery) GetMessagesBetween(chat PortalKey, minTimestamp, maxTimestamp time.Time) (messages []*Message) { + rows, err := mq.db.Query(getMessagesBetweenQuery, chat.GMID, chat.Receiver, minTimestamp.Unix(), maxTimestamp.Unix()) + if err != nil || rows == nil { return nil } - return &message + for rows.Next() { + messages = append(messages, mq.New().Scan(rows)) + } + return +} +func (mq *MessageQuery) maybeScan(row *sql.Row) *Message { + if row == nil { + return nil + } + return mq.New().Scan(row) } type Message struct { db *Database log log.Logger - Chat PortalKey `gorm:"embedded;embeddedPrefix:chat_"` - JID types.GroupMeID `gorm:"primaryKey;unique;notNull"` - MXID id.EventID `gorm:"primaryKey;unique;notNull"` - Sender types.GroupMeID `gorm:"notNull"` - Timestamp uint64 `gorm:"notNull;default:0"` - Content *groupmeExt.Message `gorm:"type:TEXT;notNull"` + Chat PortalKey + GMID groupme.ID + MXID id.EventID + Sender groupme.ID + Timestamp time.Time + Sent bool - Portal Portal `gorm:"foreignKey:chat_jid,chat_receiver;references:jid,receiver;constraint:onDelete:CASCADE;"` + Portal Portal } -// func (msg *Message) Scan(row Scannable) *Message { -// var content []byte -// err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &msg.Timestamp, &content) -// if err != nil { -// if err != sql.ErrNoRows { -// msg.log.Errorln("Database scan failed:", err) -// } -// return nil -// } - -// msg.decodeBinaryContent(content) - -// return msg -// } - -// func (msg *Message) decodeBinaryContent(content []byte) { -// msg.Content = &waProto.Message{} -// reader := bytes.NewReader(content) -// dec := json.NewDecoder(reader) -// err := dec.Decode(msg.Content) -// if err != nil { -// msg.log.Warnln("Failed to decode message content:", err) -// } -// } - -// func (msg *Message) encodeBinaryContent() []byte { -// var buf bytes.Buffer -// enc := json.NewEncoder(&buf) -// err := enc.Encode(msg.Content) -// if err != nil { -// msg.log.Warnln("Failed to encode message content:", err) -// } -// return buf.Bytes() -// } - -func (msg *Message) Insert() { - ans := msg.db.Create(&msg) - if ans.Error != nil { - msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, ans.Error) +func (msg *Message) Scan(row dbutil.Scannable) *Message { + var ts int64 + err := row.Scan(&msg.Chat.GMID, &msg.Chat.Receiver, &msg.GMID, &msg.MXID, &msg.Sender, &ts, &msg.Sent) + if err != nil { + if !errors.Is(err, sql.ErrNoRows) { + msg.log.Errorln("Database scan failed:", err) + } + return nil } -} - -func (msg *Message) Delete() { - ans := msg.db.Delete(&msg) - if ans.Error != nil { - msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, ans.Error) + if ts != 0 { + msg.Timestamp = time.Unix(ts, 0) } + return msg } diff --git a/database/migrate.go b/database/migrate.go deleted file mode 100644 index 4c3c31d..0000000 --- a/database/migrate.go +++ /dev/null @@ -1,158 +0,0 @@ -package database - -// import ( -// "fmt" -// "math" -// "strings" -// ) - -// // func countRows(db *Database, table string) (int, error) { -// // countRow := db.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", table)) -// // var count int -// // err := countRow.Scan(&count) -// // return count, err -// // } - -// const VariableCountLimit = 512 - -// func migrateTable(old *Database, new *Database, table string, columns ...string) error { -// columnNames := strings.Join(columns, ",") -// fmt.Printf("Migrating %s: ", table) -// rowCount, err := countRows(old, table) -// if err != nil { -// return err -// } -// fmt.Print("found ", rowCount, " rows of data, ") -// rows, err := old.Query(fmt.Sprintf("SELECT %s FROM \"%s\"", columnNames, table)) -// if err != nil { -// return err -// } -// serverColNames, err := rows.Columns() -// if err != nil { -// return err -// } -// colCount := len(serverColNames) -// valueStringFormat := strings.Repeat("$%d, ", colCount) -// valueStringFormat = fmt.Sprintf("(%s)", valueStringFormat[:len(valueStringFormat)-2]) -// cols := make([]interface{}, colCount) -// colPtrs := make([]interface{}, colCount) -// for i := 0; i < colCount; i++ { -// colPtrs[i] = &cols[i] -// } -// batchSize := VariableCountLimit / colCount -// values := make([]interface{}, batchSize*colCount) -// valueStrings := make([]string, batchSize) -// var inserted int64 -// batchCount := int(math.Ceil(float64(rowCount) / float64(batchSize))) -// tx, err := new.Begin() -// if err != nil { -// return err -// } -// fmt.Printf("migrating in %d batches: ", batchCount) -// for rowCount > 0 { -// var i int -// for ; rows.Next() && i < batchSize; i++ { -// colPtrs := make([]interface{}, colCount) -// valueStringArgs := make([]interface{}, colCount) -// for j := 0; j < colCount; j++ { -// pos := i*colCount + j -// colPtrs[j] = &values[pos] -// valueStringArgs[j] = pos + 1 -// } -// valueStrings[i] = fmt.Sprintf(valueStringFormat, valueStringArgs...) -// err = rows.Scan(colPtrs...) -// if err != nil { -// panic(err) -// } -// } -// slicedValues := values -// slicedValueStrings := valueStrings -// if i < len(valueStrings) { -// slicedValueStrings = slicedValueStrings[:i] -// slicedValues = slicedValues[:i*colCount] -// } -// if len(slicedValues) == 0 { -// break -// } -// res, err := tx.Exec(fmt.Sprintf("INSERT INTO \"%s\" (%s) VALUES %s", table, columnNames, strings.Join(slicedValueStrings, ",")), slicedValues...) -// if err != nil { -// panic(err) -// } -// count, _ := res.RowsAffected() -// inserted += count -// rowCount -= batchSize -// fmt.Print("#") -// } -// err = tx.Commit() -// if err != nil { -// return err -// } -// fmt.Println(" -- done with", inserted, "rows inserted") -// return nil -// } - -func Migrate(old *Database, new *Database) { - print("skipping migration because test") -} - -// err := migrateTable(old, new, "portal", "jid", "receiver", "mxid", "name", "topic", "avatar", "avatar_url", "encrypted") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "user", "mxid", "jid", "management_room", "client_id", "client_token", "server_token", "enc_key", "mac_key", "last_connection") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "puppet", "jid", "avatar", "displayname", "name_quality", "custom_mxid", "access_token", "next_batch", "avatar_url", "enable_presence", "enable_receipts") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "user_portal", "user_jid", "portal_jid", "portal_receiver", "in_community") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "message", "chat_jid", "chat_receiver", "jid", "mxid", "sender", "content", "timestamp") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "mx_registrations", "user_id") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "mx_user_profile", "room_id", "user_id", "membership") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "mx_room_state", "room_id", "power_levels") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "crypto_account", "account_id", "device_id", "shared", "sync_token", "account") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "crypto_message_index", "sender_key", "session_id", `"index"`, "event_id", "timestamp") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "crypto_tracked_user", "user_id") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "crypto_device", "user_id", "device_id", "identity_key", "signing_key", "trust", "deleted", "name") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "crypto_olm_session", "account_id", "session_id", "sender_key", "session", "created_at", "last_used") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "crypto_megolm_inbound_session", "account_id", "session_id", "sender_key", "signing_key", "room_id", "session", "forwarding_chains") -// if err != nil { -// panic(err) -// } -// err = migrateTable(old, new, "crypto_megolm_outbound_session", "account_id", "room_id", "session_id", "session", "shared", "max_messages", "message_count", "max_age", "created_at", "last_used") -// if err != nil { -// panic(err) -// } -// } diff --git a/database/portal.go b/database/portal.go index 1531d28..b0d8a11 100644 --- a/database/portal.go +++ b/database/portal.go @@ -17,31 +17,33 @@ package database import ( + "database/sql" + "fmt" "strconv" "strings" - "gorm.io/gorm" log "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/util/dbutil" - "github.com/beeper/groupme/types" + "github.com/karmanyaahm/groupme" ) -// JID is the puppet or the group +// GMID is the puppet or the group // Receiver is the "Other Person" in a DM or the group itself in a group type PortalKey struct { - JID types.GroupMeID `gorm:"primaryKey"` - Receiver types.GroupMeID `gorm:"primaryKey"` + GMID groupme.ID + Receiver groupme.ID } -func ParsePortalKey(inp types.GroupMeID) *PortalKey { +func ParsePortalKey(inp string) *PortalKey { parts := strings.Split(inp, "+") if len(parts) == 1 { if i, err := strconv.Atoi(inp); i == 0 || err != nil { return nil } - return &PortalKey{inp, inp} + return &PortalKey{groupme.ID(inp), groupme.ID(inp)} } else if len(parts) == 2 { if i, err := strconv.Atoi(parts[0]); i == 0 || err != nil { return nil @@ -51,38 +53,38 @@ func ParsePortalKey(inp types.GroupMeID) *PortalKey { } return &PortalKey{ - JID: parts[1], - Receiver: parts[0], + GMID: groupme.ID(parts[1]), + Receiver: groupme.ID(parts[0]), } } else { return nil } } -func GroupPortalKey(jid types.GroupMeID) PortalKey { +func GroupPortalKey(gmid groupme.ID) PortalKey { return PortalKey{ - JID: jid, - Receiver: jid, + GMID: gmid, + Receiver: gmid, } } -func NewPortalKey(jid, receiver types.GroupMeID) PortalKey { +func NewPortalKey(gmid, receiver groupme.ID) PortalKey { return PortalKey{ - JID: jid, + GMID: gmid, Receiver: receiver, } } func (key PortalKey) String() string { - if key.Receiver == key.JID { - return key.JID + if key.Receiver == key.GMID { + return key.GMID.String() } - return key.JID + "+" + key.Receiver + return key.GMID.String() + "+" + key.Receiver.String() } func (key PortalKey) IsPrivate() bool { //also see FindPrivateChats - return key.JID != key.Receiver + return key.GMID != key.Receiver } type PortalQuery struct { @@ -97,83 +99,86 @@ func (pq *PortalQuery) New() *Portal { } } -func (pq *PortalQuery) GetAll() []*Portal { - return pq.getAll(pq.db.DB) +const ( + portalColumns = "gmid, receiver, mxid, name, name_set, topic, topic_set, avatar, avatar_url, avatar_set, encrypted" + getAllPortalsQuery = "SELECT " + portalColumns + " FROM portal" + getPortalByGMIDQuery = getAllPortalsQuery + " WHERE gmid=$1 AND receiver=$2" + getPortalByMXIDQuery = getAllPortalsQuery + " WHERE mxid=$1" + getAllPortalsByGMID = getAllPortalsQuery + " WHERE gmid=$1" + getAllPrivateChats = getAllPortalsQuery + " WHERE receiver=$1 AND receiver <> gmid" +) +func (pq *PortalQuery) GetAll() []*Portal { + return pq.getAll(getAllPortalsQuery) } -func (pq *PortalQuery) GetByJID(key PortalKey) *Portal { - return pq.get(pq.db.DB.Where("jid = ? AND receiver = ?", key.JID, key.Receiver)) - +func (pq *PortalQuery) GetByGMID(key PortalKey) *Portal { + return pq.get(getPortalByGMIDQuery, key.GMID, key.Receiver) } func (pq *PortalQuery) GetByMXID(mxid id.RoomID) *Portal { - return pq.get(pq.db.DB.Where("mxid = ?", mxid)) + return pq.get(getPortalByMXIDQuery, mxid) } -func (pq *PortalQuery) GetAllByJID(jid types.GroupMeID) []*Portal { - return pq.getAll(pq.db.DB.Where("jid = ?", jid)) - +func (pq *PortalQuery) GetAllByGMID(gmid groupme.ID) []*Portal { + return pq.getAll(getAllPortalsByGMID, gmid) } -func (pq *PortalQuery) FindPrivateChats(receiver types.GroupMeID) []*Portal { - //also see IsPrivate - return pq.getAll(pq.db.DB.Where("receiver = ? AND receiver <> jid", receiver)) - +func (pq *PortalQuery) FindPrivateChats(receiver groupme.ID) []*Portal { + return pq.getAll(getAllPrivateChats, receiver) } -func (pq *PortalQuery) getAll(db *gorm.DB) (portals []*Portal) { - ans := db.Find(&portals) - if ans.Error != nil || len(portals) == 0 { +func (pq *PortalQuery) getAll(query string, args ...any) (portals []*Portal) { + rows, err := pq.db.Query(query, args...) + if err != nil || rows == nil { return nil } - for _, i := range portals { - i.db = pq.db - i.log = pq.log + defer rows.Close() + for rows.Next() { + portals = append(portals, pq.New().Scan(rows)) } return - } -func (pq *PortalQuery) get(db *gorm.DB) *Portal { - var portal Portal - ans := db.Limit(1).Find(&portal) - if ans.Error != nil || db.RowsAffected == 0 { +func (pq *PortalQuery) get(query string, args ...interface{}) *Portal { + row := pq.db.QueryRow(query, args...) + if row == nil { return nil } - portal.db = pq.db - portal.log = pq.log - - return &portal + return pq.New().Scan(row) } type Portal struct { db *Database log log.Logger - Key PortalKey `gorm:"primaryKey;embedded"` + Key PortalKey MXID id.RoomID Name string + NameSet bool Topic string + TopicSet bool Avatar string AvatarURL id.ContentURI - Encrypted bool `gorm:"notNull;default:false"` + AvatarSet bool + Encrypted bool } -// func (portal *Portal) Scan(row Scannable) *Portal { -// var mxid, avatarURL sql.NullString -// err := row.Scan(&portal.Key.JID, &portal.Key.Receiver, &mxid, &portal.Name, &portal.Topic, &portal.Avatar, &avatarURL, &portal.Encrypted) -// if err != nil { -// if err != sql.ErrNoRows { -// portal.log.Errorln("Database scan failed:", err) -// } -// return nil -// } -// portal.MXID = id.RoomID(mxid.String) -// portal.AvatarURL, _ = id.ParseContentURI(avatarURL.String) -// return portal -// } +func (portal *Portal) Scan(row dbutil.Scannable) *Portal { + var mxid, avatarURL sql.NullString + + err := row.Scan(&portal.Key.GMID, &portal.Key.Receiver, &mxid, &portal.Name, &portal.NameSet, &portal.Topic, &portal.TopicSet, &portal.Avatar, &avatarURL, &portal.AvatarSet, &portal.Encrypted) + if err != nil { + if err != sql.ErrNoRows { + portal.log.Errorln("Database scan failed:", err) + } + return nil + } + portal.MXID = id.RoomID(mxid.String) + portal.AvatarURL, _ = id.ParseContentURI(avatarURL.String) + return portal +} func (portal *Portal) mxidPtr() *id.RoomID { if len(portal.MXID) > 0 { @@ -183,50 +188,40 @@ func (portal *Portal) mxidPtr() *id.RoomID { } func (portal *Portal) Insert() { - - ans := portal.db.Create(&portal) - print("beware of types") - if ans.Error != nil { - portal.log.Warnfln("Failed to insert %s: %v", portal.Key, ans.Error) + _, err := portal.db.Exec(fmt.Sprintf(` + INSERT INTO portal (%s) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) + `, portalColumns), + portal.Key.GMID, portal.Key.Receiver, portal.mxidPtr(), portal.Name, portal.NameSet, portal.Topic, portal.TopicSet, portal.Avatar, portal.AvatarURL.String(), portal.AvatarSet, portal.Encrypted) + if err != nil { + portal.log.Warnfln("Failed to insert %s: %v", portal.Key, err) } } -func (portal *Portal) Update() { - ans := portal.db.Where("jid = ? AND receiver = ?", portal.Key.JID, portal.Key.Receiver).Save(&portal) - print("check .model vs not") - - if ans.Error != nil { - portal.log.Warnfln("Failed to update %s: %v", portal.Key, ans.Error) +func (portal *Portal) Update(txn dbutil.Transaction) { + query := ` + UPDATE portal + SET mxid=$1, name=$2, name_set=$3, topic=$4, topic_set=$5, avatar=$6, avatar_url=$7, avatar_set=$8, encrypted=$9 + WHERE gmid=$10 AND receiver=$11 + ` + args := []interface{}{ + portal.mxidPtr(), portal.Name, portal.NameSet, portal.Topic, portal.TopicSet, portal.Avatar, portal.AvatarURL.String(), + portal.AvatarSet, portal.Encrypted, portal.Key.GMID, portal.Key.Receiver, + } + var err error + if txn != nil { + _, err = txn.Exec(query, args...) + } else { + _, err = portal.db.Exec(query, args...) + } + if err != nil { + portal.log.Warnfln("Failed to update %s: %v", portal.Key, err) } } func (portal *Portal) Delete() { - ans := portal.db.Where("jid = ? AND receiver = ?", portal.Key.JID, portal.Key.Receiver).Delete(&portal) - if ans.Error != nil { - portal.log.Warnfln("Failed to delete %s: %v", portal.Key, ans.Error) - } -} - -func (portal *Portal) GetUserIDs() []id.UserID { - //TODO: gorm this - rows, err := portal.db.Raw(`SELECT "users".mxid FROM "users", user_portals - WHERE "users".jid=user_portals.user_jid - AND user_portals.portal_jid = ? - AND user_portals.portal_receiver = ?`, - portal.Key.JID, portal.Key.Receiver).Rows() + _, err := portal.db.Exec("DELETE FROM portal WHERE gmid=$1 AND receiver=$2", portal.Key.GMID, portal.Key.Receiver) if err != nil { - portal.log.Debugln("Failed to get portal user ids:", err) - return nil + portal.log.Warnfln("Failed to delete %s: %v", portal.Key, err) } - var userIDs []id.UserID - for rows.Next() { - var userID id.UserID - err = rows.Scan(&userID) - if err != nil { - portal.log.Warnln("Failed to scan row:", err) - continue - } - userIDs = append(userIDs, userID) - } - return userIDs } diff --git a/database/puppet.go b/database/puppet.go index 65f1f63..36f0c59 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -17,11 +17,14 @@ package database import ( + "database/sql" + log "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/util/dbutil" - "github.com/beeper/groupme/types" + "github.com/karmanyaahm/groupme" ) type PuppetQuery struct { @@ -34,120 +37,126 @@ func (pq *PuppetQuery) New() *Puppet { db: pq.db, log: pq.log, - EnablePresence: true, EnableReceipts: true, } } +const ( + puppetColumns = "gmid, displayname, name_set, avatar, avatar_url, avatar_set, custom_mxid, access_token, next_batch, enable_receipts" + getAllPuppetsQuery = "SELECT " + puppetColumns + " FROM puppets" + getPuppetQuery = getAllPuppetsQuery + " WHERE gmid=$1" + getPuppetByCustomMXIDQuery = getAllPuppetsQuery + " WHERE custom_mxid=$1" + getAllPuppetsWithCustomMXIDQuery = getAllPuppetsQuery + " WHERE custom_mxid<>''" +) + func (pq *PuppetQuery) GetAll() (puppets []*Puppet) { - ans := pq.db.Find(&puppets) - if ans.Error != nil || len(puppets) == 0 { + rows, err := pq.db.Query(getAllPuppetsQuery) + if err != nil || rows == nil { return nil } - for _, puppet := range puppets { - pq.initializePuppet(puppet) + defer rows.Close() + for rows.Next() { + puppets = append(puppets, pq.New().Scan(rows)) } - // defer rows.Close() - // for rows.Next() { - // puppets = append(puppets, pq.New().Scan(rows)) - // } return } -func (pq *PuppetQuery) Get(jid types.GroupMeID) *Puppet { - puppet := Puppet{} - ans := pq.db.Where("jid = ?", jid).Limit(1).Find(&puppet) - if ans.Error != nil || ans.RowsAffected == 0 { +func (pq *PuppetQuery) Get(gmid groupme.ID) *Puppet { + row := pq.db.QueryRow(getPuppetQuery, gmid) + if row == nil { return nil } - pq.initializePuppet(&puppet) - return &puppet + return pq.New().Scan(row) } func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet { - puppet := Puppet{} - ans := pq.db.Where("custom_mxid = ?", mxid).Limit(1).Find(&puppet) - if ans.Error != nil || ans.RowsAffected == 0 { + row := pq.db.QueryRow(getPuppetByCustomMXIDQuery, mxid) + if row == nil { return nil } - pq.initializePuppet(&puppet) - return &puppet + return pq.New().Scan(row) } func (pq *PuppetQuery) GetAllWithCustomMXID() (puppets []*Puppet) { - - ans := pq.db.Find(&puppets, "custom_mxid <> ''") - if ans.Error != nil || len(puppets) != 0 { + rows, err := pq.db.Query(getAllPuppetsWithCustomMXIDQuery) + if err != nil || rows == nil { return nil } - for _, puppet := range puppets { - pq.initializePuppet(puppet) + defer rows.Close() + for rows.Next() { + puppets = append(puppets, pq.New().Scan(rows)) } - // defer rows.Close() - // for rows.Next() { - // puppets = append(puppets, pq.New().Scan(rows)) - // } return } -func (pq *PuppetQuery) initializePuppet(p *Puppet) { - p.db = pq.db - p.log = pq.log -} - -//Puppet is comment +// Puppet is comment type Puppet struct { db *Database log log.Logger - JID types.GroupMeID `gorm:"primaryKey"` - //Avatar string - //AvatarURL types.ContentURI - //Displayname string - //NameQuality int8 + GMID groupme.ID - CustomMXID id.UserID `gorm:"column:custom_mxid;"` + Displayname string + NameSet bool + + Avatar string + AvatarURL id.ContentURI + AvatarSet bool + + CustomMXID id.UserID AccessToken string NextBatch string - EnablePresence bool `gorm:"notNull;default:true"` - EnableReceipts bool `gorm:"notNull;default:true"` + EnableReceipts bool } -// func (puppet *Puppet) Scan(row Scannable) *Puppet { -// var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString -// var quality sql.NullInt64 -// var enablePresence, enableReceipts sql.NullBool -// err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch, &enablePresence, &enableReceipts) -// if err != nil { -// if err != sql.ErrNoRows { -// puppet.log.Errorln("Database scan failed:", err) -// } -// return nil -// } -// puppet.Displayname = displayname.String -// puppet.Avatar = avatar.String -// puppet.AvatarURL, _ = id.ParseContentURI(avatarURL.String) -// puppet.NameQuality = int8(quality.Int64) -// puppet.CustomMXID = id.UserID(customMXID.String) -// puppet.AccessToken = accessToken.String -// puppet.NextBatch = nextBatch.String -// puppet.EnablePresence = enablePresence.Bool -// puppet.EnableReceipts = enableReceipts.Bool -// return puppet -// } +func (puppet *Puppet) Scan(row dbutil.Scannable) *Puppet { + var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString + var enableReceipts, nameSet, avatarSet sql.NullBool + var gmid string + err := row.Scan(&gmid, &displayname, &nameSet, &avatar, &avatarURL, &avatarSet, &customMXID, &accessToken, &nextBatch, &enableReceipts) + if err != nil { + if err != sql.ErrNoRows { + puppet.log.Errorln("Database scan failed:", err) + } + return nil + } + puppet.GMID = groupme.ID(gmid) + puppet.Displayname = displayname.String + puppet.NameSet = nameSet.Bool + puppet.Avatar = avatar.String + puppet.AvatarURL, _ = id.ParseContentURI(avatarURL.String) + puppet.AvatarSet = avatarSet.Bool + puppet.CustomMXID = id.UserID(customMXID.String) + puppet.AccessToken = accessToken.String + puppet.NextBatch = nextBatch.String + puppet.EnableReceipts = enableReceipts.Bool + return puppet +} func (puppet *Puppet) Insert() { - // _, err := puppet.db.Exec("INSERT INTO puppet (jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", - // puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts) - ans := puppet.db.Create(&puppet) - if ans.Error != nil { - puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, ans.Error) + _, err := puppet.db.Exec(` + INSERT INTO puppet (username, avatar, avatar_url, avatar_set, displayname, name_set, + custom_mxid, access_token, next_batch, enable_receipts) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + `, puppet.GMID, puppet.Avatar, puppet.AvatarURL.String(), puppet.AvatarSet, puppet.Displayname, + puppet.NameSet, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, + puppet.EnableReceipts, + ) + if err != nil { + puppet.log.Warnfln("Failed to insert %s: %v", puppet.GMID, err) } } func (puppet *Puppet) Update() { - ans := puppet.db.Where("jid = ?", puppet.JID).Updates(&puppet) - if ans.Error != nil { - puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, ans.Error) + _, err := puppet.db.Exec(` + UPDATE puppet + SET displayname=$1, name_set=$2, avatar=$3, avatar_url=$4, avatar_set=$5, custom_mxid=$6, + access_token=$7, next_batch=$8, enable_receipts=$10 + WHERE username=$11 + `, puppet.Displayname, puppet.NameSet, puppet.Avatar, puppet.AvatarURL.String(), puppet.AvatarSet, + puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnableReceipts, + puppet.GMID) + if err != nil { + puppet.log.Warnfln("Failed to update %s: %v", puppet.GMID, err) } } diff --git a/database/reactions.go b/database/reactions.go index b55be57..01f9ec2 100644 --- a/database/reactions.go +++ b/database/reactions.go @@ -1,10 +1,14 @@ package database import ( + "database/sql" + "errors" + log "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/util/dbutil" - "github.com/beeper/groupme/types" + "github.com/karmanyaahm/groupme" ) type ReactionQuery struct { @@ -19,56 +23,81 @@ func (mq *ReactionQuery) New() *Reaction { } } -func (mq *ReactionQuery) GetByJID(jid types.GroupMeID) (reactions []*Reaction) { - ans := mq.db.Model(&Reaction{}). - Preload("Puppet"). // TODO: Do this in seperate function? - Where("message_jid = ?", jid). - Limit(1).Find(&reactions) +const ( + getReactionByTargetGMIDQuery = ` + SELECT chat_gmid, chat_receiver, target_gmid, sender, mxid, gmid + FROM reaction + WHERE chat_gmid=$1 AND chat_receiver=$2 AND target_gmid=$3 AND sender=$4 + ` + getReactionByMXIDQuery = ` + SELECT chat_gmid, chat_receiver, target_gmid, sender, mxid, gmid FROM reaction + WHERE mxid=$1 + ` + upsertReactionQuery = ` + INSERT INTO reaction (chat_gmid, chat_receiver, target_gmid, sender, mxid, gmid) + VALUES ($1, $2, $3, $4, $5, $6) + ON CONFLICT (chat_gmid, chat_receiver, target_gmid, sender) + DO UPDATE SET mxid=excluded.mxid, gmid=excluded.gmid + ` + deleteReactionQuery = ` + DELETE FROM reaction WHERE chat_gmid=$1 AND chat_receiver=$2 AND target_gmid=$3 AND sender=$4 AND mxid=$5 + ` +) - if ans.Error != nil || ans.RowsAffected == 0 { - return nil - } - - for _, reaction := range reactions { - reaction.db = mq.db - reaction.log = mq.log - } - - return +func (rq *ReactionQuery) GetByTargetGMID(chat PortalKey, gmid groupme.ID, sender groupme.ID) *Reaction { + return rq.maybeScan(rq.db.QueryRow(getReactionByTargetGMIDQuery, chat.GMID, chat.Receiver, gmid, sender)) } -// ans := mq.db.Model(&Reaction{}). -// Joins("INNER JOIN users on users.mxid = reactions.user_mxid"). -// Where("reactions.message_jid = ? AND users.jid = ?", jid, uid). -// Limit(1).Find(&reactions) +func (rq *ReactionQuery) GetByMXID(mxid id.EventID) *Reaction { + return rq.maybeScan(rq.db.QueryRow(getReactionByMXIDQuery, mxid)) +} + +func (rq *ReactionQuery) maybeScan(row *sql.Row) *Reaction { + if row == nil { + return nil + } + return rq.New().Scan(row) +} type Reaction struct { db *Database log log.Logger - MXID id.EventID `gorm:"primaryKey"` - - //Message - MessageJID types.GroupMeID `gorm:"notNull"` - MessageMXID id.EventID `gorm:"notNull"` - - Message Message `gorm:"foreignKey:MessageMXID,MessageJID;references:MXID,JID;"` - - //User - PuppetJID types.GroupMeID `gorm:"notNull"` - Puppet Puppet `gorm:"foreignKey:PuppetJID;references:jid;"` + Chat PortalKey + TargetGMID groupme.ID + Sender groupme.ID + MXID id.EventID + GMID groupme.ID } -func (reaction *Reaction) Insert() { - ans := reaction.db.Create(&reaction) - if ans.Error != nil { - reaction.log.Warnfln("Failed to insert %s@%s: %v", reaction.MXID, reaction.MessageJID, ans.Error) +func (reaction *Reaction) Scan(row dbutil.Scannable) *Reaction { + err := row.Scan(&reaction.Chat.GMID, &reaction.Chat.Receiver, &reaction.TargetGMID, &reaction.Sender, &reaction.MXID, &reaction.GMID) + if err != nil { + if !errors.Is(err, sql.ErrNoRows) { + reaction.log.Errorln("Database scan failed:", err) + } + return nil } + return reaction +} + +func (reaction *Reaction) Upsert(txn dbutil.Execable) { + if txn == nil { + txn = reaction.db + } + _, err := txn.Exec(upsertReactionQuery, reaction.Chat.GMID, reaction.Chat.Receiver, reaction.TargetGMID, reaction.Sender, reaction.MXID, reaction.GMID) + if err != nil { + reaction.log.Warnfln("Failed to upsert reaction to %s@%s by %s: %v", reaction.Chat, reaction.TargetGMID, reaction.Sender, err) + } +} + +func (reaction *Reaction) GetTarget() *Message { + return reaction.db.Message.GetByGMID(reaction.Chat, reaction.TargetGMID) } func (reaction *Reaction) Delete() { - ans := reaction.db.Delete(&reaction) - if ans.Error != nil { - reaction.log.Warnfln("Failed to insert %s@%s: %v", reaction.MXID, reaction.MessageJID, ans.Error) + _, err := reaction.db.Exec(deleteReactionQuery, reaction.Chat.GMID, reaction.Chat.Receiver, reaction.TargetGMID, reaction.Sender, reaction.MXID) + if err != nil { + reaction.log.Warnfln("Failed to delete reaction %s: %v", reaction.MXID, err) } } diff --git a/database/statestore.go b/database/statestore.go deleted file mode 100644 index 844c4a2..0000000 --- a/database/statestore.go +++ /dev/null @@ -1,368 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package database - -import ( - "errors" - "sync" - - "gorm.io/gorm" - "gorm.io/gorm/clause" - log "maunium.net/go/maulogger/v2" - - "maunium.net/go/mautrix/appservice" - "maunium.net/go/mautrix/event" - "maunium.net/go/mautrix/id" -) - -type SQLStateStore struct { - *appservice.TypingStateStore - - db *Database - log log.Logger - - Typing map[id.RoomID]map[id.UserID]int64 - typingLock sync.RWMutex -} - -type mxRegistered struct { - UserID string `gorm:"primaryKey"` -} - -var _ appservice.StateStore = (*SQLStateStore)(nil) - -func NewSQLStateStore(db *Database) *SQLStateStore { - return &SQLStateStore{ - TypingStateStore: appservice.NewTypingStateStore(), - db: db, - log: db.log.Sub("StateStore"), - } -} - -func (store *SQLStateStore) IsRegistered(userID id.UserID) bool { - v := mxRegistered{UserID: userID.String()} - var count int64 - ans := store.db.Model(&mxRegistered{}).Where(&v).Count(&count) - - if errors.Is(ans.Error, gorm.ErrRecordNotFound) { - return false - } - if ans.Error != nil { - store.log.Warnfln("Failed to scan registration existence for %s: %v", userID, ans.Error) - } - return count >= 1 -} - -func (store *SQLStateStore) MarkRegistered(userID id.UserID) { - - ans := store.db.Create(mxRegistered{userID.String()}) - - if ans.Error != nil { - store.log.Warnfln("Failed to mark %s as registered: %v", userID, ans.Error) - } -} - -type MxUserProfile struct { - RoomID string `gorm:"primaryKey"` - UserID string `gorm:"primaryKey"` - Membership string `gorm:"notNull"` - - DisplayName string - AvatarURL string - Avatar string -} - -func (store *SQLStateStore) GetRoomMembers(roomID id.RoomID) map[id.UserID]*event.MemberEventContent { - members := make(map[id.UserID]*event.MemberEventContent) - var users []MxUserProfile - ans := store.db.Where("room_id = ?", roomID.String()).Find(&users) - if ans.Error != nil { - return members - } - - var userID id.UserID - var member event.MemberEventContent - for _, user := range users { - // if err != nil { - // store.log.Warnfln("Failed to scan member in %s: %v", roomID, err) - // continue - // } - userID = id.UserID(user.UserID) - member = event.MemberEventContent{ - Membership: event.Membership(user.Membership), - Displayname: user.DisplayName, - AvatarURL: id.ContentURIString(user.AvatarURL), - } - - members[userID] = &member - } - return members -} - -func (store *SQLStateStore) GetMembership(roomID id.RoomID, userID id.UserID) event.Membership { - var user MxUserProfile - ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Limit(1).Find(&user) - membership := event.MembershipLeave - if ans.Error != nil && ans.Error != gorm.ErrRecordNotFound { - store.log.Warnfln("Failed to scan membership of %s in %s: %v", userID, roomID, ans.Error) - } - membership = event.Membership(user.Membership) - - return membership -} - -func (store *SQLStateStore) GetMember(roomID id.RoomID, userID id.UserID) *event.MemberEventContent { - member, ok := store.TryGetMember(roomID, userID) - if !ok { - member.Membership = event.MembershipLeave - } - return member -} - -func (store *SQLStateStore) TryGetMember(roomID id.RoomID, userID id.UserID) (*event.MemberEventContent, bool) { - var user MxUserProfile - ans := store.db.Where("room_id = ? AND user_id = ?", roomID, userID).Limit(1).Find(&user) - - if ans.Error != nil && ans.Error != gorm.ErrRecordNotFound { - store.log.Warnfln("Failed to scan member info of %s in %s: %v", userID, roomID, ans.Error) - } - eventMember := event.MemberEventContent{ - Membership: event.Membership(user.Membership), - Displayname: user.DisplayName, - AvatarURL: id.ContentURIString(user.AvatarURL), - } - - return &eventMember, ans.Error != nil -} - -func (store *SQLStateStore) TryGetMemberRaw(roomID id.RoomID, userID id.UserID) (user MxUserProfile, err bool) { - user.RoomID = roomID.String() - user.UserID = userID.String() - - ans := store.db.Limit(1).Find(&user) - - if ans.Error == gorm.ErrRecordNotFound { - err = true - return - } else if ans.Error != nil { - store.log.Warnfln("Failed to scan member info of %s in %s: %v", userID, roomID, ans.Error) - err = true - return - } - - return user, false -} - -func (store *SQLStateStore) SetMemberRaw(member *MxUserProfile) { - ans := store.db.Clauses(clause.OnConflict{ - UpdateAll: true, - }).Create(member) - - if ans.Error != nil { - store.log.Warnfln("Failed to set membership of %s in %s to %s: %v", member.UserID, member.RoomID, member, ans.Error) - } -} - -func (store *SQLStateStore) FindSharedRooms(userID id.UserID) (rooms []id.RoomID) { - - rows, err := store.db.Table("mx_user_profile").Select("room_id"). - Joins("LEFT JOIN portal ON portal.mxid=mx_user_profile.room_id"). - Where("user_id = ? AND portal.encrypted=true", userID).Rows() - defer rows.Close() - - if err != nil { - store.log.Warnfln("Failed to query shared rooms with %s: %v", userID, err) - return - } - print("running maybe maybe code f937060306") - for rows.Next() { - var roomID id.RoomID - err := rows.Scan(&roomID) - if err != nil { - store.log.Warnfln("Failed to scan room ID: %v", err) - } else { - rooms = append(rooms, roomID) - } - } - return -} - -func (store *SQLStateStore) IsInRoom(roomID id.RoomID, userID id.UserID) bool { - return store.IsMembership(roomID, userID, "join") -} - -func (store *SQLStateStore) IsInvited(roomID id.RoomID, userID id.UserID) bool { - return store.IsMembership(roomID, userID, "join", "invite") -} - -func (store *SQLStateStore) IsMembership(roomID id.RoomID, userID id.UserID, allowedMemberships ...event.Membership) bool { - membership := store.GetMembership(roomID, userID) - for _, allowedMembership := range allowedMemberships { - if allowedMembership == membership { - return true - } - } - return false -} - -func (store *SQLStateStore) SetMembership(roomID id.RoomID, userID id.UserID, membership event.Membership) { - var err error - user := MxUserProfile{ - RoomID: roomID.String(), - UserID: userID.String(), - Membership: string(membership), - } - print("weird thing 2 502650285") - - ans := store.db.Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "room_id"}, {Name: "user_id"}}, - DoUpdates: clause.AssignmentColumns([]string{"membership"}), - }).Create(&user) - - if ans.Error != nil { - store.log.Warnfln("Failed to set membership of %s in %s to %s: %v", userID, roomID, membership, err) - } -} -func (store *SQLStateStore) SetMember(roomID id.RoomID, userID id.UserID, member *event.MemberEventContent) { - - user := MxUserProfile{ - RoomID: roomID.String(), - UserID: userID.String(), - Membership: string(member.Membership), - DisplayName: member.Displayname, - // AvatarURL: string(member.AvatarURL),//try ignoring - } - ans := store.db.Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "room_id"}, {Name: "user_id"}}, - DoUpdates: clause.AssignmentColumns([]string{"membership", "display_name"}), - }).Create(&user) - - if ans.Error != nil { - store.log.Warnfln("Failed to set membership of %s in %s to %s: %v", userID, roomID, member, ans.Error) - } -} - -func (store *SQLStateStore) SetPowerLevels(roomID id.RoomID, levels *event.PowerLevelsEventContent) { - // levelsBytes, err := json.Marshal(levels) - // if err != nil { - // store.log.Errorfln("Failed to marshal power levels of %s: %v", roomID, err) - // return - // } - // if store.db.dialect == "postgres" { - // _, err = store.db.Exec(`INSERT INTO mx_room_state (room_id, power_levels) VALUES ($1, $2) - // ON CONFLICT (room_id) DO UPDATE SET power_levels=$2`, roomID, levelsBytes) - // } else if store.db.dialect == "sqlite3" { - // _, err = store.db.Exec("INSERT OR REPLACE INTO mx_room_state (room_id, power_levels) VALUES ($1, $2)", roomID, levelsBytes) - // } else { - // err = fmt.Errorf("unsupported dialect %s", store.db.dialect) - // } - // if err != nil { - // store.log.Warnfln("Failed to store power levels of %s: %v", roomID, err) - // } -} - -func (store *SQLStateStore) GetPowerLevels(roomID id.RoomID) (levels *event.PowerLevelsEventContent) { - // row := store.db.QueryRow("SELECT power_levels FROM mx_room_state WHERE room_id=$1", roomID) - // if row == nil { - // return - // } - // var data []byte - // err := row.Scan(&data) - // if err != nil { - // store.log.Errorln("Failed to scan power levels of %s: %v", roomID, err) - // return - // } - // levels = &event.PowerLevelsEventContent{} - // err = json.Unmarshal(data, levels) - // if err != nil { - // store.log.Errorln("Failed to parse power levels of %s: %v", roomID, err) - // return nil - // } - return -} - -func (store *SQLStateStore) GetPowerLevel(roomID id.RoomID, userID id.UserID) int { - // if store.db.dialect == "postgres" { - // row := store.db.QueryRow(`SELECT - // COALESCE((power_levels->'users'->$2)::int, (power_levels->'users_default')::int, 0) - // FROM mx_room_state WHERE room_id=$1`, roomID, userID) - // if row == nil { - // // Power levels not in db - // return 0 - // } - // var powerLevel int - // err := row.Scan(&powerLevel) - // if err != nil { - // store.log.Errorln("Failed to scan power level of %s in %s: %v", userID, roomID, err) - // } - // return powerLevel - // } - return store.GetPowerLevels(roomID).GetUserLevel(userID) -} - -func (store *SQLStateStore) GetPowerLevelRequirement(roomID id.RoomID, eventType event.Type) int { - // if store.db.dialect == "postgres" { - // defaultType := "events_default" - // defaultValue := 0 - // if eventType.IsState() { - // defaultType = "state_default" - // defaultValue = 50 - // } - // row := store.db.QueryRow(`SELECT - // COALESCE((power_levels->'events'->$2)::int, (power_levels->'$3')::int, $4) - // FROM mx_room_state WHERE room_id=$1`, roomID, eventType.Type, defaultType, defaultValue) - // if row == nil { - // // Power levels not in db - // return defaultValue - // } - // var powerLevel int - // err := row.Scan(&powerLevel) - // if err != nil { - // store.log.Errorln("Failed to scan power level for %s in %s: %v", eventType, roomID, err) - // } - // return powerLevel - // } - return store.GetPowerLevels(roomID).GetEventLevel(eventType) -} - -func (store *SQLStateStore) HasPowerLevel(roomID id.RoomID, userID id.UserID, eventType event.Type) bool { - // if store.db.dialect == "postgres" { - // defaultType := "events_default" - // defaultValue := 0 - // if eventType.IsState() { - // defaultType = "state_default" - // defaultValue = 50 - // } - // row := store.db.QueryRow(`SELECT - // COALESCE((power_levels->'users'->$2)::int, (power_levels->'users_default')::int, 0) - // >= - // COALESCE((power_levels->'events'->$3)::int, (power_levels->'$4')::int, $5) - // FROM mx_room_state WHERE room_id=$1`, roomID, userID, eventType.Type, defaultType, defaultValue) - // if row == nil { - // // Power levels not in db - // return defaultValue == 0 - // } - // var hasPower bool - // err := row.Scan(&hasPower) - // if err != nil { - // store.log.Errorln("Failed to scan power level for %s in %s: %v", eventType, roomID, err) - // } - // return hasPower - // } - // return store.GetPowerLevel(roomID, userID) >= store.GetPowerLevelRequirement(roomID, eventType) - return false -} diff --git a/database/upgrades/00-latest-revision.sql b/database/upgrades/00-latest-revision.sql new file mode 100644 index 0000000..fd98175 --- /dev/null +++ b/database/upgrades/00-latest-revision.sql @@ -0,0 +1,83 @@ +-- v0 -> v1: Latest revision + +CREATE TABLE "user" ( + mxid TEXT PRIMARY KEY, + gmid TEXT UNIQUE, + + auth_token TEXT, + + management_room TEXT, + space_room TEXT, +); + +CREATE TABLE portal ( + gmid TEXT, + receiver TEXT, + mxid TEXT UNIQUE, + + name TEXT NOT NULL, + name_set BOOLEAN NOT NULL DEFAULT false, + topic TEXT NOT NULL, + topic_set BOOLEAN NOT NULL DEFAULT false, + avatar TEXT NOT NULL, + avatar_url TEXT, + avatar_set BOOLEAN NOT NULL DEFAULT false, + encrypted BOOLEAN NOT NULL DEFAULT false, + + PRIMARY KEY (gmid, receiver) +); + +CREATE TABLE puppet ( + gmid TEXT PRIMARY KEY, + displayname TEXT, + name_set BOOLEAN NOT NULL DEFAULT false, + avatar TEXT, + avatar_url TEXT, + avatar_set BOOLEAN NOT NULL DEFAULT false, + + custom_mxid TEXT, + access_token TEXT, + next_batch TEXT, + + enable_presence BOOLEAN NOT NULL DEFAULT true, + enable_receipts BOOLEAN NOT NULL DEFAULT true +); + +CREATE TABLE message ( + chat_gmid TEXT, + chat_receiver TEXT, + gmid TEXT, + mxid TEXT UNIQUE, + sender TEXT, + timestamp BIGINT, + sent BOOLEAN, + + PRIMARY KEY (chat_gmid, chat_receiver, gmid), + FOREIGN KEY (chat_gmid, chat_receiver) REFERENCES portal(gmid, receiver) ON DELETE CASCADE +); + +CREATE TABLE reaction ( + chat_gmid TEXT, + chat_receiver TEXT, + target_gmid TEXT, + sender TEXT, + + mxid TEXT NOT NULL, + gmid TEXT NOT NULL, + + PRIMARY KEY (chat_gmid, chat_receiver, target_gmid, sender), + FOREIGN KEY (chat_gmid, chat_receiver, target_gmid) REFERENCES message(chat_gmid, chat_receiver, gmid) + ON DELETE CASCADE ON UPDATE CASCADE +) + +CREATE TABLE user_portal ( + user_mxid TEXT, + portal_gmid TEXT, + portal_receiver TEXT, + in_space BOOLEAN NOT NULL DEFAULT false, + + PRIMARY KEY (user_mxid, portal_gmid, portal_receiver), + + FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY (portal_gmid, portal_receiver) REFERENCES portal(gmid, receiver) ON UPDATE CASCADE ON DELETE CASCADE +); diff --git a/database/upgrades/2018-09-01-initial-schema.go b/database/upgrades/2018-09-01-initial-schema.go deleted file mode 100644 index bed0bed..0000000 --- a/database/upgrades/2018-09-01-initial-schema.go +++ /dev/null @@ -1,182 +0,0 @@ -package upgrades - -import ( - "gorm.io/gorm" -) - -func init() { - upgrades[0] = upgrade{"Initial schema", func(tx *gorm.DB, ctx context) error { - tx.Exec(`CREATE TABLE IF NOT EXISTS portal ( - jid VARCHAR(255), - receiver VARCHAR(255), - mxid VARCHAR(255) UNIQUE, - - name VARCHAR(255) NOT NULL, - topic VARCHAR(512) NOT NULL, - avatar VARCHAR(255) NOT NULL, - avatar_url VARCHAR(255), - encrypted BOOLEAN NOT NULL DEFAULT false, - - PRIMARY KEY (jid, receiver) - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS puppet ( - jid VARCHAR(255) PRIMARY KEY, - avatar VARCHAR(255), - displayname VARCHAR(255), - name_quality SMALLINT, - custom_mxid VARCHAR(255), - access_token VARCHAR(1023), - next_batch VARCHAR(255), - avatar_url VARCHAR(255), - enable_presence BOOLEAN NOT NULL DEFAULT true, - enable_receipts BOOLEAN NOT NULL DEFAULT true - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS "user" ( - mxid VARCHAR(255) PRIMARY KEY, - jid VARCHAR(255) UNIQUE, - - management_room VARCHAR(255), - - client_id VARCHAR(255), - client_token VARCHAR(255), - server_token VARCHAR(255), - enc_key bytea, - mac_key bytea, - last_connection BIGINT NOT NULL DEFAULT 0 - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS "user_portal" ( - user_jid VARCHAR(255), - portal_jid VARCHAR(255), - portal_receiver VARCHAR(255), - in_community BOOLEAN NOT NULL DEFAULT FALSE, - - PRIMARY KEY (user_jid, portal_jid, portal_receiver), - - FOREIGN KEY (user_jid) REFERENCES "user"(jid) ON DELETE CASCADE, - FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS message ( - chat_jid VARCHAR(255), - chat_receiver VARCHAR(255), - jid VARCHAR(255), - mxid VARCHAR(255) NOT NULL UNIQUE, - sender VARCHAR(255) NOT NULL, - content bytea NOT NULL, - timestamp BIGINT NOT NULL DEFAULT 0, - - PRIMARY KEY (chat_jid, chat_receiver, jid), - FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS mx_registrations ( - user_id VARCHAR(255) PRIMARY KEY - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS mx_room_state ( - room_id VARCHAR(255) PRIMARY KEY, - power_levels TEXT - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS mx_user_profile ( - room_id VARCHAR(255), - user_id VARCHAR(255), - membership VARCHAR(15) NOT NULL, - PRIMARY KEY (room_id, user_id), - displayname TEXT, - avatar_url VARCHAR(255) - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_olm_session ( - session_id CHAR(43) NOT NULL, - sender_key CHAR(43) NOT NULL, - session bytea NOT NULL, - created_at timestamp NOT NULL, - last_used timestamp NOT NULL, - account_id TEXT NOT NULL, - PRIMARY KEY (account_id, session_id) - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_megolm_inbound_session ( - session_id CHAR(43) NOT NULL, - sender_key CHAR(43) NOT NULL, - signing_key CHAR(43) NOT NULL, - room_id TEXT NOT NULL, - session bytea NOT NULL, - forwarding_chains bytea NOT NULL, - account_id TEXT NOT NULL, - withheld_code TEXT, - withheld_reason TEXT, - PRIMARY KEY (session_id, account_id) - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_device ( - user_id VARCHAR(255), - device_id VARCHAR(255), - identity_key CHAR(43) NOT NULL, - signing_key CHAR(43) NOT NULL, - trust SMALLINT NOT NULL, - deleted BOOLEAN NOT NULL, - name VARCHAR(255) NOT NULL, - - PRIMARY KEY (user_id, device_id) - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_tracked_user ( - user_id VARCHAR(255) PRIMARY KEY - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_message_index ( - sender_key CHAR(43), - session_id CHAR(43), - "index" INTEGER, - event_id VARCHAR(255) NOT NULL, - timestamp BIGINT NOT NULL, - - PRIMARY KEY (sender_key, session_id, "index") - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_account ( - device_id TEXT NOT NULL, - shared BOOLEAN NOT NULL, - sync_token TEXT NOT NULL, - account bytea NOT NULL, - account_id TEXT PRIMARY KEY - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_megolm_outbound_session ( - room_id VARCHAR(255) NOT NULL, - session_id CHAR(43) NOT NULL UNIQUE, - session bytea NOT NULL, - shared BOOLEAN NOT NULL, - max_messages INTEGER NOT NULL, - message_count INTEGER NOT NULL, - max_age BIGINT NOT NULL, - created_at timestamp NOT NULL, - last_used timestamp NOT NULL, - account_id TEXT, - PRIMARY KEY (room_id, account_id) - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_cross_signing_keys ( - user_id TEXT NOT NULL, - usage TEXT NOT NULL, - key CHAR(43) NOT NULL, - PRIMARY KEY (user_id, usage) - )`) - - tx.Exec(`CREATE TABLE IF NOT EXISTS crypto_cross_signing_signatures ( - signed_user_id TEXT NOT NULL, - signed_key TEXT NOT NULL, - signer_user_id TEXT NOT NULL, - signer_key TEXT NOT NULL, - signature TEXT NOT NULL, - PRIMARY KEY (signed_user_id, signed_key, signer_user_id, signer_key) - )`) - - return nil - }} -} diff --git a/database/upgrades/upgrades.go b/database/upgrades/upgrades.go index 9dffb46..7165cdb 100644 --- a/database/upgrades/upgrades.go +++ b/database/upgrades/upgrades.go @@ -1,123 +1,36 @@ +// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge. +// Copyright (C) 2022 Tulir Asokan +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + package upgrades import ( + "embed" "errors" - "fmt" - "strings" - "gorm.io/gorm" - log "maunium.net/go/maulogger/v2" + "maunium.net/go/mautrix/util/dbutil" ) -type Dialect int +var Table dbutil.UpgradeTable -const ( - Postgres Dialect = iota - SQLite -) +//go:embed *.sql +var rawUpgrades embed.FS -func (dialect Dialect) String() string { - switch dialect { - case Postgres: - return "postgres" - case SQLite: - return "sqlite3" - default: - return "" - } -} - -type upgradeFunc func(*gorm.DB, context) error - -type context struct { - dialect Dialect - db *gorm.DB - log log.Logger -} - -type upgrade struct { - message string - fn upgradeFunc -} - -type version struct { - gorm.Model - V int -} - -const NumberOfUpgrades = 1 - -var upgrades [NumberOfUpgrades]upgrade - -var UnsupportedDatabaseVersion = fmt.Errorf("unsupported database version") - -func GetVersion(db *gorm.DB) (int, error) { - var ver = version{V: 0} - result := db.FirstOrCreate(&ver, &ver) - if result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) || - errors.Is(result.Error, gorm.ErrInvalidField) { - db.Create(&ver) - print("create version") - - } else { - return 0, result.Error - } - } - return int(ver.V), nil -} - -func SetVersion(tx *gorm.DB, newVersion int) error { - err := tx.Where("v IS NOT NULL").Delete(&version{}) - if err.Error != nil { - return err.Error - } - - val := version{V: newVersion} - tx = tx.Create(&val) - return tx.Error -} - -func Run(log log.Logger, dialectName string, db *gorm.DB) error { - var dialect Dialect - switch strings.ToLower(dialectName) { - case "postgres": - dialect = Postgres - case "sqlite3": - dialect = SQLite - default: - return fmt.Errorf("unknown dialect %s", dialectName) - } - - db.AutoMigrate(&version{}) - version, err := GetVersion(db) - - if err != nil { - return err - } - - if version > NumberOfUpgrades { - return UnsupportedDatabaseVersion - } - - log.Infofln("Database currently on v%d, latest: v%d", version, NumberOfUpgrades) - for i, upgrade := range upgrades[version:] { - log.Infofln("Upgrading database to v%d: %s", version+i+1, upgrade.message) - err = db.Transaction(func(tx *gorm.DB) error { - err = upgrade.fn(tx, context{dialect, db, log}) - if err != nil { - return err - } - err = SetVersion(tx, version+i+1) - if err != nil { - return err - } - return nil - }) - if err != nil { - return err - } - - } - return nil +func init() { + Table.Register(-1, 35, "Unsupported version", func(tx dbutil.Transaction, database *dbutil.Database) error { + return errors.New("please upgrade to mautrix-whatsapp v0.4.0 before upgrading to a newer version") + }) + Table.RegisterFS(rawUpgrades) } diff --git a/database/user.go b/database/user.go index f420e01..5736a71 100644 --- a/database/user.go +++ b/database/user.go @@ -17,14 +17,17 @@ package database import ( + "database/sql" "strings" + "sync" "time" log "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/util/dbutil" - "github.com/beeper/groupme/types" + "github.com/karmanyaahm/groupme" ) type UserQuery struct { @@ -33,218 +36,114 @@ type UserQuery struct { } func (uq *UserQuery) New() *User { - return &User{ - db: uq.db, - log: uq.log, - } + return &User{db: uq.db, log: uq.log} } +const ( + userColumns = "gmid, mxid, auth_token, management_room, space_room" + getAllUsersQuery = "SELECT " + userColumns + ` FROM "user"` + getUserByMXIDQuery = getAllUsersQuery + ` WHERE mxid=$1` + getUserByGMIDQuery = getAllUsersQuery + ` WHERE gmid=$1` + insertUserQuery = `INSERT INTO "user" (` + userColumns + `) VALUES ($1, $2, $3, $4, $5)` + updateUserQurey = ` + UPDATE "user" + SET gmid=$1, auth_token=$2, management_room=$3, space_room=$4 + WHERE mxid=$5 + ` +) + func (uq *UserQuery) GetAll() (users []*User) { - ans := uq.db.Find(&users) - if ans.Error != nil || len(users) == 0 { + rows, err := uq.db.Query(getAllUsersQuery) + if err != nil || rows == nil { return nil } - for _, i := range users { - i.db = uq.db - i.log = uq.log + defer rows.Close() + for rows.Next() { + users = append(users, uq.New().Scan(rows)) } return } func (uq *UserQuery) GetByMXID(userID id.UserID) *User { - var user User - ans := uq.db.Where("mxid = ?", userID).Take(&user) - user.db = uq.db - user.log = uq.log - if ans.Error != nil { + row := uq.db.QueryRow(getUserByMXIDQuery, userID) + if row == nil { return nil } - return &user + return uq.New().Scan(row) } -func (uq *UserQuery) GetByJID(userID types.GroupMeID) *User { - var user User - ans := uq.db.Where("jid = ?", userID).Limit(1).Find(&user) - if ans.Error != nil || ans.RowsAffected == 0 { +func (uq *UserQuery) GetByGMID(gmid groupme.ID) *User { + row := uq.db.QueryRow(getUserByGMIDQuery, gmid) + if row == nil { return nil } - user.db = uq.db - user.log = uq.log - - return &user + return uq.New().Scan(row) } type User struct { db *Database log log.Logger - MXID id.UserID `gorm:"primaryKey"` - JID types.GroupMeID `gorm:"unique"` - Token types.AuthToken - + MXID id.UserID + GMID groupme.ID ManagementRoom id.RoomID - LastConnection uint64 `gorm:"notNull;default:0"` + SpaceRoom id.RoomID + + Token string + + lastReadCache map[PortalKey]time.Time + lastReadCacheLock sync.Mutex + inSpaceCache map[PortalKey]bool + inSpaceCacheLock sync.Mutex } -//func (user *User) Scan(row Scannable) *User { -// var jid, clientID, clientToken, serverToken sql.NullString -// var encKey, macKey []byte -// err := row.Scan(&user.MXID, &jid, &user.ManagementRoom, &user.LastConnection, &clientID, &clientToken, &serverToken, &encKey, &macKey) -// if err != nil { -// if err != sql.ErrNoRows { -// user.log.Errorln("Database scan failed:", err) -// } -// return nil -// } -// if len(jid.String) > 0 && len(clientID.String) > 0 { -// user.JID = jid.String + whatsappExt.NewUserSuffix -// // user.Session = &whatsapp.Session{ -// // ClientId: clientID.String, -// // ClientToken: clientToken.String, -// // ServerToken: serverToken.String, -// // EncKey: encKey, -// // MacKey: macKey, -// // Wid: jid.String + whatsappExt.OldUserSuffix, -// // } -// } // else { -// // user.Session = nil -// // } -// return user -//} +func (user *User) Scan(row dbutil.Scannable) *User { + var gmid, authToken sql.NullString + err := row.Scan(&gmid, &user.MXID, &authToken, &user.ManagementRoom, &user.SpaceRoom) + if err != nil { + if err != sql.ErrNoRows { + user.log.Errorln("Database scan failed:", err) + } + return nil + } + if len(gmid.String) > 0 { + user.GMID = groupme.ID(gmid.String) + } + user.Token = authToken.String + return user +} -func stripSuffix(jid types.GroupMeID) string { - if len(jid) == 0 { - return jid +func stripSuffix(gmid groupme.ID) string { + if len(gmid) == 0 { + return gmid.String() } - index := strings.IndexRune(jid, '@') + index := strings.IndexRune(gmid.String(), '@') if index < 0 { - return jid + return gmid.String() } - return jid[:index] + return gmid.String()[:index] } -func (user *User) jidPtr() *string { - if len(user.JID) > 0 { - str := stripSuffix(user.JID) +func (user *User) gmidPtr() *string { + if len(user.GMID) > 0 { + str := stripSuffix(user.GMID) return &str } return nil } -//func (user *User) sessionUnptr() (sess whatsapp.Session) { -// // if user.Session != nil { -// // sess = *user.Session -// // } -// return -//} - func (user *User) Insert() { - ans := user.db.Create(&user) - if ans.Error != nil { - user.log.Warnfln("Failed to insert %s: %v", user.MXID, ans.Error) + _, err := user.db.Exec(insertUserQuery, user.gmidPtr(), user.MXID, user.Token, user.ManagementRoom, user.SpaceRoom) + if err != nil { + user.log.Warnfln("Failed to insert %s: %v", user.MXID, err) } } -func (user *User) UpdateLastConnection() { - user.LastConnection = uint64(time.Now().Unix()) - user.Update() -} - func (user *User) Update() { - ans := user.db.Save(&user) - if ans.Error != nil { - user.log.Warnfln("Failed to update user: %v", ans.Error) + _, err := user.db.Exec(updateUserQurey, user.gmidPtr(), user.Token, user.ManagementRoom, user.SpaceRoom, user.MXID) + if err != nil { + user.log.Warnfln("Failed to update %s: %v", user.MXID, err) } - -} - -type PortalKeyWithMeta struct { - PortalKey - InCommunity bool -} - -type UserPortal struct { - UserJID types.GroupMeID `gorm:"primaryKey;"` - - PortalJID types.GroupMeID `gorm:"primaryKey;"` - PortalReceiver types.GroupMeID `gorm:"primaryKey;"` - - InCommunity bool `gorm:"notNull;default:false;"` - - User User `gorm:"foreignKey:UserJID;references:jid;constraint:OnDelete:CASCADE;"` - Portal Portal `gorm:"foreignKey:PortalJID,PortalReceiver;references:JID,Receiver;constraint:OnDelete:CASCADE;"` -} - -func (user *User) SetPortalKeys(newKeys []PortalKeyWithMeta) error { - tx := user.db.Begin() - ans := tx.Where("user_jid = ?", *user.jidPtr()).Delete(UserPortal{}) - - if ans.Error != nil { - _ = tx.Rollback() - return ans.Error - } - - for _, key := range newKeys { - ans = tx.Create(&UserPortal{ - UserJID: *user.jidPtr(), - PortalJID: key.JID, - PortalReceiver: key.Receiver, - InCommunity: key.InCommunity, - }) - if ans.Error != nil { - _ = tx.Rollback() - return ans.Error - } - } - - println("portalkey transaction complete") - return tx.Commit().Error -} - -func (user *User) IsInPortal(key PortalKey) bool { - var count int64 - user.db.Find(&UserPortal{ - UserJID: *user.jidPtr(), - PortalJID: key.JID, - PortalReceiver: key.Receiver, - }).Count(&count) //TODO: efficient - return count > 0 -} - -func (user *User) GetPortalKeys() []PortalKey { - var up []UserPortal - ans := user.db.Where("user_jid = ?", *user.jidPtr()).Find(&up) - if ans.Error != nil { - user.log.Warnln("Failed to get user portal keys:", ans.Error) - return nil - } - var keys []PortalKey - for _, i := range up { - key := PortalKey{ - JID: i.PortalJID, - Receiver: i.PortalReceiver, - } - keys = append(keys, key) - } - return keys -} - -func (user *User) GetInCommunityMap() map[PortalKey]bool { - var up []UserPortal - ans := user.db.Where("user_jid = ?", *user.jidPtr()).Find(&up) - if ans.Error != nil { - user.log.Warnln("Failed to get user portal keys:", ans.Error) - return nil - } - keys := make(map[PortalKey]bool) - for _, i := range up { - key := PortalKey{ - JID: i.PortalJID, - Receiver: i.PortalReceiver, - } - keys[key] = i.InCommunity - } - return keys } diff --git a/database/userportal.go b/database/userportal.go new file mode 100644 index 0000000..3d76d27 --- /dev/null +++ b/database/userportal.go @@ -0,0 +1,35 @@ +package database + +import ( + "database/sql" + "errors" +) + +func (user *User) IsInSpace(portal PortalKey) bool { + user.inSpaceCacheLock.Lock() + defer user.inSpaceCacheLock.Unlock() + if cached, ok := user.inSpaceCache[portal]; ok { + return cached + } + var inSpace bool + err := user.db.QueryRow("SELECT in_space FROM user_portal WHERE user_mxid=$1 AND portal_gmid=$2 AND portal_receiver=$3", user.MXID, portal.GMID, portal.Receiver).Scan(&inSpace) + if err != nil && !errors.Is(err, sql.ErrNoRows) { + user.log.Warnfln("Failed to scan in space status from user portal table: %v", err) + } + user.inSpaceCache[portal] = inSpace + return inSpace +} + +func (user *User) MarkInSpace(portal PortalKey) { + user.inSpaceCacheLock.Lock() + defer user.inSpaceCacheLock.Unlock() + _, err := user.db.Exec(` + INSERT INTO user_portal (user_mxid, portal_gmid, portal_receiver, in_space) VALUES ($1, $2, $3, true) + ON CONFLICT (user_mxid, portal_gmid, portal_receiver) DO UPDATE SET in_space=true + `, user.MXID, portal.GMID, portal.Receiver) + if err != nil { + user.log.Warnfln("Failed to update in space status: %v", err) + } else { + user.inSpaceCache[portal] = true + } +} diff --git a/example-config.yaml b/example-config.yaml index 276db87..9a8c952 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -57,6 +57,15 @@ metrics: # IP and port where the metrics listener should be. The path is always /metrics listen: 127.0.0.1:8001 +# GroupMe configuration +groupme: + # GroupMe connection timeout in seconds. + connection_timeout: 20 + # If GroupMe doesn't respond within connection_timeout, should the bridge + # try to fetch the message to see if it was actually bridged? Use this if + # you have problems with sends timing out but actually succeeding. + fetch_message_on_timeout: false + # Bridge config bridge: # Localpart template of MXIDs for WhatsApp users. @@ -67,26 +76,15 @@ bridge: # {{.Nickname}} - the nickname in that room # {{.ImageURL}} - User's avatar URL is available but irrelevant here displayname_template: "{{if .Nickname}}{{.Nickname}}{{else}}{{call .UserID.String}}{{end}} (GM)" - # Localpart template for per-user room grouping community IDs. - # On startup, the bridge will try to create these communities, add all of the specific user's - # portals to the community, and invite the Matrix user to it. - # (Note that, by default, non-admins might not have your homeserver's permission to create - # communities.) - # {{.Localpart}} is the MXID localpart and {{.Server}} is the MXID server part of the user. - # whatsapp_{{.Localpart}}={{.Server}} is a good value that should work for any user. - # communities are NOT YET TESTED in the GroupMe bridge - community_template: null - - # GroupMe connection timeout in seconds. - connection_timeout: 20 - # If groupme doesn't respond within connection_timeout, should the bridge try to fetch the message - # to see if it was actually bridged? Use this if you have problems with sends timing out but actually - # succeeding. - fetch_message_on_timeout: false - # Whether or not the bridge should send a read receipt from the bridge bot when a message has been - # sent to WhatsApp. If fetch_message_on_timeout is enabled, a successful post-timeout fetch will - # trigger a read receipt too. + # Should the bridge create a space for each logged-in user and add bridged rooms to it? + # Users who logged in before turning this on should run `!wa sync space` to create and fill the space for the first time. + personal_filtering_spaces: false + # Should the bridge send a read receipt from the bridge bot when a message has been sent to WhatsApp? delivery_receipts: false + # Whether the bridge should send the message status as a custom com.beeper.message_send_status event. + message_status_events: false + # Whether the bridge should send error notices via m.notice events when a message fails to bridge. + message_error_notices: true # Maximum number of times to retry connecting on connection error. max_connection_attempts: 3 diff --git a/formatting.go b/formatting.go index 29a99ee..2339530 100644 --- a/formatting.go +++ b/formatting.go @@ -21,10 +21,9 @@ import ( "regexp" "strings" + "github.com/karmanyaahm/groupme" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" - - "github.com/beeper/groupme/types" ) var italicRegex = regexp.MustCompile("([\\s>~*]|^)_(.+?)_([^a-zA-Z\\d]|$)") @@ -32,10 +31,10 @@ var boldRegex = regexp.MustCompile("([\\s>_~]|^)\\*(.+?)\\*([^a-zA-Z\\d]|$)") var strikethroughRegex = regexp.MustCompile("([\\s>_*]|^)~(.+?)~([^a-zA-Z\\d]|$)") var codeBlockRegex = regexp.MustCompile("```(?:.|\n)+?```") -const mentionedJIDsContextKey = "net.maunium.groupme.mentioned_jids" +const mentionedGMIDsContextKey = "net.maunium.groupme.mentioned_gmids" type Formatter struct { - bridge *Bridge + bridge *GMBridge matrixHTMLParser *format.HTMLParser @@ -44,7 +43,7 @@ type Formatter struct { waReplFuncText map[*regexp.Regexp]func(string) string } -func NewFormatter(bridge *Bridge) *Formatter { +func NewFormatter(bridge *GMBridge) *Formatter { formatter := &Formatter{ bridge: bridge, matrixHTMLParser: &format.HTMLParser{ @@ -55,11 +54,11 @@ func NewFormatter(bridge *Bridge) *Formatter { if mxid[0] == '@' { puppet := bridge.GetPuppetByMXID(id.UserID(mxid)) if puppet != nil { - jids, ok := ctx[mentionedJIDsContextKey].([]types.GroupMeID) + gmids, ok := ctx[mentionedGMIDsContextKey].([]groupme.ID) if !ok { - ctx[mentionedJIDsContextKey] = []types.GroupMeID{puppet.JID} + ctx[mentionedGMIDsContextKey] = []groupme.ID{puppet.GMID} } else { - ctx[mentionedJIDsContextKey] = append(jids, puppet.JID) + ctx[mentionedGMIDsContextKey] = append(gmids, puppet.GMID) } return "@" + puppet.PhoneNumber() } @@ -101,7 +100,7 @@ func NewFormatter(bridge *Bridge) *Formatter { return formatter } -//func (formatter *Formatter) getMatrixInfoByJID(jid types.GroupMeID) (mxid id.UserID, displayname string) { +//func (formatter *Formatter) getMatrixInfoByJID(jid groupme.ID) (mxid id.UserID, displayname string) { // if user := formatter.bridge.GetUserByJID(jid); user != nil { // mxid = user.MXID // displayname = string(user.MXID) @@ -112,7 +111,7 @@ func NewFormatter(bridge *Bridge) *Formatter { // return //} -//func (formatter *Formatter) ParseWhatsApp(content *event.MessageEventContent, mentionedJIDs []types.GroupMeID) { +//func (formatter *Formatter) ParseWhatsApp(content *event.MessageEventContent, mentionedJIDs []groupme.ID) { // output := html.EscapeString(content.Body) // for regex, replacement := range formatter.waReplString { // output = regex.ReplaceAllString(output, replacement) @@ -136,9 +135,9 @@ func NewFormatter(bridge *Bridge) *Formatter { // } //} -func (formatter *Formatter) ParseMatrix(html string) (string, []types.GroupMeID) { +func (formatter *Formatter) ParseMatrix(html string) (string, []groupme.ID) { ctx := make(format.Context) result := formatter.matrixHTMLParser.Parse(html, ctx) - mentionedJIDs, _ := ctx[mentionedJIDsContextKey].([]types.GroupMeID) + mentionedJIDs, _ := ctx[mentionedGMIDsContextKey].([]groupme.ID) return result, mentionedJIDs } diff --git a/go.mod b/go.mod index 46adfb9..87b5b00 100644 --- a/go.mod +++ b/go.mod @@ -1,28 +1,49 @@ module github.com/beeper/groupme -go 1.15 +go 1.19 require ( github.com/Rhymen/go-whatsapp v0.1.1 github.com/gabriel-vasile/mimetype v1.1.2 - github.com/gorilla/websocket v1.4.2 - github.com/jackc/pgproto3/v2 v2.0.7 // indirect + github.com/gorilla/websocket v1.5.0 github.com/karmanyaahm/groupme v0.0.0 github.com/karmanyaahm/wray v0.0.0-20210303233435-756d58657c14 - github.com/lib/pq v1.9.0 - github.com/mattn/go-sqlite3 v1.14.6 + github.com/lib/pq v1.10.7 + github.com/mattn/go-sqlite3 v1.14.15 github.com/prometheus/client_golang v1.9.0 - github.com/prometheus/procfs v0.6.0 // indirect - golang.org/x/text v0.3.5 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect - google.golang.org/protobuf v1.25.0 // indirect - gopkg.in/yaml.v2 v2.4.0 - gorm.io/driver/postgres v1.0.8 - gorm.io/driver/sqlite v1.1.4 gorm.io/gorm v1.20.12 maunium.net/go/mauflag v1.0.0 - maunium.net/go/maulogger/v2 v2.2.4 - maunium.net/go/mautrix v0.9.24 + maunium.net/go/maulogger/v2 v2.3.2 + maunium.net/go/mautrix v0.12.3-0.20221020190005-d0c13d2f04a1 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.1.1 // indirect + github.com/golang/protobuf v1.4.3 // indirect + github.com/google/uuid v1.2.0 // indirect + github.com/gorilla/mux v1.8.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.1 // indirect + github.com/mattn/go-colorable v0.1.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/prometheus/client_model v0.2.0 // indirect + github.com/prometheus/common v0.15.0 // indirect + github.com/prometheus/procfs v0.6.0 // indirect + github.com/rs/zerolog v1.28.0 // indirect + github.com/tidwall/gjson v1.14.3 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect + github.com/tidwall/sjson v1.2.5 // indirect + github.com/yuin/goldmark v1.5.2 // indirect + golang.org/x/crypto v0.0.0-20221012134737-56aed061732a // indirect + golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect + golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + google.golang.org/protobuf v1.25.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) replace github.com/karmanyaahm/groupme => ../groupme-lib diff --git a/go.sum b/go.sum index 4d1cf13..e138f71 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f/go.mod h1:4a58ifQTEe2uwwsaqbh3i2un5/CBPg+At/qHpt18Tmk= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/Rhymen/go-whatsapp v0.0.0/go.mod h1:rdQr95g2C1xcOfM7QGOhza58HeI3I+tZ/bbluv7VazA= github.com/Rhymen/go-whatsapp v0.1.1 h1:OK+bCugQcr2YjyYKeDzULqCtM50TPUFM6LvQtszKfcw= @@ -13,7 +14,6 @@ github.com/Rhymen/go-whatsapp/examples/sendTextMessages v0.0.0-20190325075644-cc github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= -github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -35,16 +35,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= -github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= -github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= -github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= -github.com/btcsuite/btcutil v1.0.2 h1:9iZ1Terx9fMIOtq1VrwdqfsATL9MC2l8ZrUY6YZ2uts= -github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts= -github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= -github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= -github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= -github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= -github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -52,18 +42,14 @@ github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= -github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= -github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -91,8 +77,7 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= -github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -139,8 +124,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= @@ -168,60 +153,6 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= -github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= -github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= -github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= -github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= -github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= -github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= -github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= -github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= -github.com/jackc/pgconn v1.4.0/go.mod h1:Y2O3ZDF0q4mMacyWV3AstPJpeHXWGEetiFttmq5lahk= -github.com/jackc/pgconn v1.5.0/go.mod h1:QeD3lBfpTFe8WUnPZWN5KY/mB8FGMIYRdd8P8Jr0fAI= -github.com/jackc/pgconn v1.5.1-0.20200601181101-fa742c524853/go.mod h1:QeD3lBfpTFe8WUnPZWN5KY/mB8FGMIYRdd8P8Jr0fAI= -github.com/jackc/pgconn v1.8.0 h1:FmjZ0rOyXTr1wfWs45i4a9vjnjWUAGpMuQLD9OSs+lw= -github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= -github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= -github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= -github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2 h1:JVX6jT/XfzNqIjye4717ITLaNwV9mWbJx0dLCpcRzdA= -github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= -github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= -github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= -github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= -github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= -github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= -github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= -github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= -github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= -github.com/jackc/pgproto3/v2 v2.0.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgproto3/v2 v2.0.7 h1:6Pwi1b3QdY65cuv6SyVO0FgPd5J3Bl7wf/nQQjinHMA= -github.com/jackc/pgproto3/v2 v2.0.7/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgservicefile v0.0.0-20200307190119-3430c5407db8/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= -github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= -github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= -github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= -github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= -github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= -github.com/jackc/pgtype v1.2.0/go.mod h1:5m2OfMh1wTK7x+Fk952IDmI4nw3nPrvtQdM0ZT4WpC0= -github.com/jackc/pgtype v1.3.1-0.20200510190516-8cd94a14c75a/go.mod h1:vaogEUkALtxZMCH411K+tKzNpwzCKU+AnPzBKZ+I+Po= -github.com/jackc/pgtype v1.3.1-0.20200606141011-f6355165a91c/go.mod h1:cvk9Bgu/VzJ9/lxTO5R5sf80p0DiucVtN7ZxvaC4GmQ= -github.com/jackc/pgtype v1.6.2 h1:b3pDeuhbbzBYcg5kwNmNDun4pFUD/0AAr1kLXZLeNt8= -github.com/jackc/pgtype v1.6.2/go.mod h1:JCULISAZBFGrHaOXIIFiyfzW5VY0GRitRr8NeJsrdig= -github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= -github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= -github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= -github.com/jackc/pgx/v4 v4.5.0/go.mod h1:EpAKPLdnTorwmPUUsqrPxy5fphV18j9q3wrfRXgo+kA= -github.com/jackc/pgx/v4 v4.6.1-0.20200510190926-94ba730bb1e9/go.mod h1:t3/cdRQl6fOLDxqtlyhe9UWgfIi9R8+8v8GKV5TRA/o= -github.com/jackc/pgx/v4 v4.6.1-0.20200606145419-4e5062306904/go.mod h1:ZDaNWkt9sW1JMiNn0kdYBaLelIhw7Pg4qd+Vk6tw7Hg= -github.com/jackc/pgx/v4 v4.10.1 h1:/6Q3ye4myIj6AaplUm+eRcz4OhK9HAvFf4ePsG40LJY= -github.com/jackc/pgx/v4 v4.10.1/go.mod h1:QlrWebbs3kqEZPHCTGyxecvzG6tvIsYu+A5b1raylkA= -github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E= @@ -229,7 +160,6 @@ github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= -github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -242,41 +172,31 @@ github.com/karmanyaahm/wray v0.0.0-20210303233435-756d58657c14 h1:NrATjZKvkY+ojL github.com/karmanyaahm/wray v0.0.0-20210303233435-756d58657c14/go.mod h1:ysD86MIEevmAkdfdg5s6Qt3I07RN6fvMAyna7jCGG2o= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.9.0 h1:L8nSXQQzAYByakOFMTwpjRoHsMJklur4Gi59b6VivR8= -github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= +github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= -github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= -github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= -github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= @@ -360,22 +280,15 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= -github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= -github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY= +github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= -github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= -github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= -github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc h1:jUIKcSPO9MoMJBbEoyE/RJoE8vz7Mb8AjvifMMwSyvY= -github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo= @@ -392,59 +305,48 @@ github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3 github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/tidwall/gjson v1.6.8 h1:CTmXMClGYPAmln7652e69B7OLXfTi5ABcPPwjIWUv7w= -github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= -github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE= -github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU= -github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= -github.com/tidwall/sjson v1.1.5 h1:wsUceI/XDyZk3J1FUvuuYlK62zJv2HO2Pzb8A5EWdUE= -github.com/tidwall/sjson v1.1.5/go.mod h1:VuJzsZnTowhSxWdOgsAnb886i4AjEyTkk7tNtsL7EYE= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= +github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= -github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= +github.com/yuin/goldmark v1.5.2 h1:ALmeCk/px5FSm1MAcFBAsVKZjDuMVj8Tm7FFIlMJnqU= +github.com/yuin/goldmark v1.5.2/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= -go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= -go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= -go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= -go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= -golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g= -golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/crypto v0.0.0-20221012134737-56aed061732a h1:NmSIgad6KjE6VvHciPZuNRTKxGhlPfD6OA87W/PLkqg= +golang.org/x/crypto v0.0.0-20221012134737-56aed061732a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -470,8 +372,8 @@ golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d h1:1aflnvSoWWLI2k/dMUAl5lvU1YO4Mb4hz0gh+1rjcxU= -golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU= +golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -491,32 +393,24 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= -golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -527,15 +421,11 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -581,7 +471,6 @@ gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qS gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o= -gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= @@ -591,15 +480,9 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/driver/postgres v1.0.8 h1:PAgM+PaHOSAeroTjHkCHCBIHHoBIf9RgPWGo8dF2DA8= -gorm.io/driver/postgres v1.0.8/go.mod h1:4eOzrI1MUfm6ObJU/UcmbXyiHSs8jSwH95G5P5dxcAg= -gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM= -gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw= -gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/gorm v1.20.12 h1:ebZ5KrSHzet+sqOCVdH9mTjW91L298nX3v5lVxAzSUY= gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= @@ -608,9 +491,9 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= 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/maulogger/v2 v2.2.4 h1:oV2GDeM4fx1uRysdpDC0FcrPg+thFicSd9XzPcYMbVY= -maunium.net/go/maulogger/v2 v2.2.4/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= -maunium.net/go/mautrix v0.9.24 h1:NEwWLHcJ/hPF0TBppdezfbVaxwWY9E9f2KDkG4Q6GC0= -maunium.net/go/mautrix v0.9.24/go.mod h1:7IzKfWvpQtN+W2Lzxc0rLvIxFM3ryKX6Ys3S/ZoWbg8= +maunium.net/go/maulogger/v2 v2.3.2 h1:1XmIYmMd3PoQfp9J+PaHhpt80zpfmMqaShzUTC7FwY0= +maunium.net/go/maulogger/v2 v2.3.2/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= +maunium.net/go/mautrix v0.12.3-0.20221020190005-d0c13d2f04a1 h1:daraaP+GcSrFLgVckFpp+ciVrtQeG5s2w3Fi8AInaj8= +maunium.net/go/mautrix v0.12.3-0.20221020190005-d0c13d2f04a1/go.mod h1:bCw45Qx/m9qsz7eazmbe7Rzq5ZbTPzwRE1UgX2S9DXs= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU= diff --git a/groupmeExt/client.go b/groupmeext/client.go similarity index 77% rename from groupmeExt/client.go rename to groupmeext/client.go index 7c22a10..a4cacbc 100644 --- a/groupmeExt/client.go +++ b/groupmeext/client.go @@ -1,11 +1,9 @@ -package groupmeExt +package groupmeext import ( "context" "github.com/karmanyaahm/groupme" - - "github.com/beeper/groupme/types" ) type Client struct { @@ -36,9 +34,9 @@ func (c Client) IndexAllChats() ([]*groupme.Chat, error) { }) } -func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFromMe bool, private bool) ([]*groupme.Message, error) { +func (c Client) LoadMessagesAfter(groupID groupme.ID, lastMessageID string, lastMessageFromMe bool, private bool) ([]*groupme.Message, error) { if private { - ans, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{ + ans, e := c.IndexDirectMessages(context.TODO(), groupID.String(), &groupme.IndexDirectMessagesQuery{ SinceID: groupme.ID(lastMessageID), //Limit: num, }) @@ -52,7 +50,7 @@ func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFrom } return ans.Messages, nil } else { - i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{ + i, e := c.IndexMessages(context.TODO(), groupID, &groupme.IndexMessagesQuery{ AfterID: groupme.ID(lastMessageID), //20 for consistency with dms Limit: 20, @@ -91,11 +89,10 @@ func (c Client) LoadMessagesBefore(groupID, lastMessageID string, private bool) } } -func (c *Client) RemoveFromGroup(uid, groupID types.GroupMeID) error { - - group, err := c.ShowGroup(context.TODO(), groupme.ID(groupID)) +func (c *Client) RemoveFromGroup(uid, groupID groupme.ID) error { + group, err := c.ShowGroup(context.TODO(), groupID) if err != nil { return err } - return c.RemoveMember(context.TODO(), groupme.ID(groupID), group.GetMemberByUserID(groupme.ID(uid)).ID) + return c.RemoveMember(context.TODO(), groupID, group.GetMemberByUserID(uid).ID) } diff --git a/groupmeExt/message.go b/groupmeext/message.go similarity index 94% rename from groupmeExt/message.go rename to groupmeext/message.go index 4453b60..f680dd3 100644 --- a/groupmeExt/message.go +++ b/groupmeext/message.go @@ -1,4 +1,4 @@ -package groupmeExt +package groupmeext import ( "bytes" @@ -10,8 +10,6 @@ import ( "net/http" "github.com/karmanyaahm/groupme" - - "github.com/beeper/groupme/types" ) type Message struct{ groupme.Message } @@ -60,7 +58,7 @@ func DownloadImage(URL string) (bytes *[]byte, mime string, err error) { return } -func DownloadFile(RoomJID types.GroupMeID, FileID string, token string) (contents []byte, fname, mime string) { +func DownloadFile(RoomJID groupme.ID, FileID string, token string) (contents []byte, fname, mime string) { client := &http.Client{} b, _ := json.Marshal(struct { FileIDS []string `json:"file_ids"` diff --git a/groupmeExt/subscription.go b/groupmeext/subscription.go similarity index 98% rename from groupmeExt/subscription.go rename to groupmeext/subscription.go index a060aac..fb75844 100644 --- a/groupmeExt/subscription.go +++ b/groupmeext/subscription.go @@ -1,4 +1,4 @@ -package groupmeExt +package groupmeext import ( log "maunium.net/go/maulogger/v2" diff --git a/groupmeExt/user.go b/groupmeext/user.go similarity index 80% rename from groupmeExt/user.go rename to groupmeext/user.go index 2ae92d5..1e5ceb7 100644 --- a/groupmeExt/user.go +++ b/groupmeext/user.go @@ -1,4 +1,4 @@ -package groupmeExt +package groupmeext const ( OldUserSuffix = "@c.groupme.com" diff --git a/main.go b/main.go index 7aee427..387473f 100644 --- a/main.go +++ b/main.go @@ -17,320 +17,89 @@ package main import ( - "fmt" - "os" - "os/signal" - "runtime/pprof" - "strings" "sync" - "syscall" - "time" - flag "maunium.net/go/mauflag" - log "maunium.net/go/maulogger/v2" - - "maunium.net/go/mautrix" - "maunium.net/go/mautrix/appservice" - "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/bridge" + "maunium.net/go/mautrix/bridge/commands" + "maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/util/configupgrade" + + "github.com/karmanyaahm/groupme" "github.com/beeper/groupme/config" "github.com/beeper/groupme/database" - "github.com/beeper/groupme/database/upgrades" - "github.com/beeper/groupme/types" ) +// Information to find out exactly which commit the bridge was built from. +// These are filled at build time with the -X linker flag. var ( - // These are static - Name = "go-groupme" - URL = "https://github.com/tulir/mautrix-whatsapp" - // This is changed when making a release - Version = "0.1.5" - // This is filled by init() - WAVersion = "" - // These are filled at build time with the -X linker flag Tag = "unknown" Commit = "unknown" BuildTime = "unknown" ) -func init() { - if len(Tag) > 0 && Tag[0] == 'v' { - Tag = Tag[1:] - } - if Tag != Version && !strings.HasSuffix(Version, "+dev") { - Version += "+dev" - } - WAVersion = strings.FieldsFunc(Version, func(r rune) bool { return r == '-' || r == '+' })[0] -} +//go:embed example-config.yaml +var ExampleConfig string -var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String() - -//var baseConfigPath = flag.MakeFull("b", "base-config", "The path to the example config file.", "example-config.yaml").String() -var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String() -var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool() -var version = flag.MakeFull("v", "version", "View bridge version and quit.", "false").Bool() -var ignoreUnsupportedDatabase = flag.Make().LongKey("ignore-unsupported-database").Usage("Run even if database is too new").Default("false").Bool() -var migrateFrom = flag.Make().LongKey("migrate-db").Usage("Source database type and URI to migrate from.").Bool() -var wantHelp, _ = flag.MakeHelpFlag() - -func (bridge *Bridge) GenerateRegistration() { - reg, err := bridge.Config.NewRegistration() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Failed to generate registration:", err) - os.Exit(20) - } - - err = reg.Save(*registrationPath) - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Failed to save registration:", err) - os.Exit(21) - } - - err = bridge.Config.Save(*configPath) - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Failed to save config:", err) - os.Exit(22) - } - fmt.Println("Registration generated. Add the path to the registration to your Synapse config, restart it, then start the bridge.") - os.Exit(0) -} - -func (bridge *Bridge) MigrateDatabase() { - oldDB, err := database.New(flag.Arg(0), flag.Arg(1), bridge.Log) - if err != nil { - fmt.Println("Failed to open old database:", err) - os.Exit(30) - } - err = oldDB.Init() - if err != nil { - fmt.Println("Failed to upgrade old database:", err) - os.Exit(31) - } - - newDB, err := database.New(bridge.Config.AppService.Database.Type, bridge.Config.AppService.Database.URI, bridge.Log) - if err != nil { - fmt.Println("Failed to open new database:", err) - os.Exit(32) - } - err = newDB.Init() - if err != nil { - fmt.Println("Failed to upgrade new database:", err) - os.Exit(33) - } - - database.Migrate(oldDB, newDB) -} - -type Bridge struct { - AS *appservice.AppService - EventProcessor *appservice.EventProcessor - MatrixHandler *MatrixHandler - Config *config.Config - DB *database.Database - Log log.Logger - StateStore *database.SQLStateStore - Provisioning *ProvisioningAPI - Bot *appservice.IntentAPI - Formatter *Formatter - Relaybot *User - Crypto Crypto - Metrics *MetricsHandler +type GMBridge struct { + bridge.Bridge + Config *config.Config + DB *database.Database + Provisioning *ProvisioningAPI + Formatter *Formatter + Metrics *MetricsHandler usersByMXID map[id.UserID]*User - usersByJID map[types.GroupMeID]*User + usersByUsername map[string]*User + usersByGMID map[groupme.ID]*User // TODO REMOVE? usersLock sync.Mutex + spaceRooms map[id.RoomID]*User + spaceRoomsLock sync.Mutex managementRooms map[id.RoomID]*User managementRoomsLock sync.Mutex portalsByMXID map[id.RoomID]*Portal - portalsByJID map[database.PortalKey]*Portal + portalsByGMID map[database.PortalKey]*Portal portalsLock sync.Mutex - puppets map[types.GroupMeID]*Puppet + puppets map[groupme.ID]*Puppet puppetsByCustomMXID map[id.UserID]*Puppet puppetsLock sync.Mutex } -type Crypto interface { - HandleMemberEvent(*event.Event) - Decrypt(*event.Event) (*event.Event, error) - Encrypt(id.RoomID, event.Type, event.Content) (*event.EncryptedEventContent, error) - WaitForSession(id.RoomID, id.SenderKey, id.SessionID, time.Duration) bool - ResetSession(id.RoomID) - Init() error - Start() - Stop() -} +func (br *GMBridge) Init() { + br.CommandProcessor = commands.NewProcessor(&br.Bridge) + br.RegisterCommands() -func NewBridge() *Bridge { - bridge := &Bridge{ - usersByMXID: make(map[id.UserID]*User), - usersByJID: make(map[types.GroupMeID]*User), - managementRooms: make(map[id.RoomID]*User), - portalsByMXID: make(map[id.RoomID]*Portal), - portalsByJID: make(map[database.PortalKey]*Portal), - puppets: make(map[types.GroupMeID]*Puppet), - puppetsByCustomMXID: make(map[id.UserID]*Puppet), + Segment.log = br.Log.Sub("Segment") + Segment.key = br.Config.SegmentKey + if Segment.IsEnabled() { + Segment.log.Infoln("Segment metrics are enabled") } - var err error - bridge.Config, err = config.Load(*configPath) - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Failed to load config:", err) - os.Exit(10) - } - return bridge -} + br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database")) -func (bridge *Bridge) ensureConnection() { - for { - resp, err := bridge.Bot.Whoami() - if err != nil { - if httpErr, ok := err.(mautrix.HTTPError); ok && httpErr.RespError != nil && httpErr.RespError.ErrCode == "M_UNKNOWN_ACCESS_TOKEN" { - bridge.Log.Fatalln("Access token invalid. Is the registration installed in your homeserver correctly?") - os.Exit(16) - } - bridge.Log.Errorfln("Failed to connect to homeserver: %v. Retrying in 10 seconds...", err) - time.Sleep(10 * time.Second) - } else if resp.UserID != bridge.Bot.UserID { - bridge.Log.Fatalln("Unexpected user ID in whoami call: got %s, expected %s", resp.UserID, bridge.Bot.UserID) - os.Exit(17) - } else { - break - } - } -} - -func (bridge *Bridge) Init() { - var err error - - bridge.AS, err = bridge.Config.MakeAppService() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Failed to initialize AppService:", err) - os.Exit(11) - } - _, _ = bridge.AS.Init() - bridge.Bot = bridge.AS.BotIntent() - - bridge.Log = log.Create() - bridge.Config.Logging.Configure(bridge.Log) - log.DefaultLogger = bridge.Log.(*log.BasicLogger) - if len(bridge.Config.Logging.FileNameFormat) > 0 { - err = log.OpenFile() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Failed to open log file:", err) - os.Exit(12) - } - } - bridge.AS.Log = log.Sub("Matrix") - - bridge.Log.Debugln("Initializing database connection") - print("test1") - - bridge.DB, err = database.New(bridge.Config.AppService.Database.Type, bridge.Config.AppService.Database.URI, bridge.Log) - - if err != nil { - bridge.Log.Fatalln("Failed to initialize database connection:", err) - os.Exit(14) - } - - if len(bridge.Config.AppService.StateStore) > 0 && bridge.Config.AppService.StateStore != "./mx-state.json" { - version, err := upgrades.GetVersion(bridge.DB.DB) - if version < 0 && err == nil { - bridge.Log.Fatalln("Non-standard state store path. Please move the state store to ./mx-state.json " + - "and update the config. The state store will be migrated into the db on the next launch.") - os.Exit(18) - } - } - - bridge.Log.Debugln("Initializing state store") - bridge.StateStore = database.NewSQLStateStore(bridge.DB) - bridge.AS.StateStore = bridge.StateStore - - // bridge.DB.SetMaxOpenConns(bridge.Config.AppService.Database.MaxOpenConns) - // bridge.DB.SetMaxIdleConns(bridge.Config.AppService.Database.MaxIdleConns) - - ss := bridge.Config.AppService.Provisioning.SharedSecret + ss := br.Config.Bridge.Provisioning.SharedSecret if len(ss) > 0 && ss != "disable" { - bridge.Provisioning = &ProvisioningAPI{bridge: bridge} + br.Provisioning = &ProvisioningAPI{bridge: br} } - bridge.Log.Debugln("Initializing Matrix event processor") - bridge.EventProcessor = appservice.NewEventProcessor(bridge.AS) - bridge.Log.Debugln("Initializing Matrix event handler") - bridge.MatrixHandler = NewMatrixHandler(bridge) - bridge.Formatter = NewFormatter(bridge) - bridge.Crypto = NewCryptoHelper(bridge) - bridge.Metrics = NewMetricsHandler(bridge.Config.Metrics.Listen, bridge.Log.Sub("Metrics"), bridge.DB) + br.Formatter = NewFormatter(br) + br.Metrics = NewMetricsHandler(br.Config.Metrics.Listen, br.Log.Sub("Metrics"), br.DB) + br.MatrixHandler.TrackEventDuration = br.Metrics.TrackMatrixEvent } -func (bridge *Bridge) Start() { - bridge.Log.Debugln("Running database upgrades") - err := bridge.DB.Init() - if err != nil && (err != upgrades.UnsupportedDatabaseVersion || !*ignoreUnsupportedDatabase) { - bridge.Log.Fatalln("Failed to initialize database:", err) - os.Exit(15) - } - bridge.Log.Debugln("Checking connection to homeserver") - bridge.ensureConnection() - if bridge.Crypto != nil { - err := bridge.Crypto.Init() - if err != nil { - bridge.Log.Fatalln("Error initializing end-to-bridge encryption:", err) - os.Exit(19) - } - } +func (bridge *GMBridge) Start() { if bridge.Provisioning != nil { bridge.Log.Debugln("Initializing provisioning API") bridge.Provisioning.Init() } - bridge.LoadRelaybot() - bridge.Log.Debugln("Starting application service HTTP server") - go bridge.AS.Start() - bridge.Log.Debugln("Starting event processor") - go bridge.EventProcessor.Start() - go bridge.UpdateBotProfile() - if bridge.Crypto != nil { - go bridge.Crypto.Start() - } go bridge.StartUsers() if bridge.Config.Metrics.Enabled { go bridge.Metrics.Start() } - - if bridge.Config.Bridge.ResendBridgeInfo { - go bridge.ResendBridgeInfo() - } } -func (bridge *Bridge) ResendBridgeInfo() { - bridge.Config.Bridge.ResendBridgeInfo = false - err := bridge.Config.Save(*configPath) - if err != nil { - bridge.Log.Errorln("Failed to save config after setting resend_bridge_info to false:", err) - } - bridge.Log.Infoln("Re-sending bridge info state event to all portals") - for _, portal := range bridge.GetAllPortals() { - portal.UpdateBridgeInfo() - } - bridge.Log.Infoln("Finished re-sending bridge info state events") -} - -func (bridge *Bridge) LoadRelaybot() { - if !bridge.Config.Bridge.Relaybot.Enabled { - return - } - bridge.Relaybot = bridge.GetUserByMXID("relaybot") - if bridge.Relaybot.HasSession() { - bridge.Log.Debugln("Relaybot is enabled") - } else { - bridge.Log.Debugln("Relaybot is enabled, but not logged in") - } - bridge.Relaybot.ManagementRoom = bridge.Config.Bridge.Relaybot.ManagementRoom - bridge.Relaybot.IsRelaybot = true - bridge.Relaybot.Connect() -} - -func (bridge *Bridge) UpdateBotProfile() { +func (bridge *GMBridge) UpdateBotProfile() { bridge.Log.Debugln("Updating bot profile") botConfig := bridge.Config.AppService.Bot @@ -358,16 +127,23 @@ func (bridge *Bridge) UpdateBotProfile() { } } -func (bridge *Bridge) StartUsers() { - bridge.Log.Debugln("Starting users") - for _, user := range bridge.GetAllUsers() { +func (br *GMBridge) StartUsers() { + br.Log.Debugln("Starting users") + foundAnySessions := false + for _, user := range br.GetAllUsers() { + if user.GMID.String() != "" { + foundAnySessions = true + } go user.Connect() } - bridge.Log.Debugln("Starting custom puppets") - for _, loopuppet := range bridge.GetAllPuppetsWithCustomMXID() { + if !foundAnySessions { + br.SendGlobalBridgeState(status.BridgeState{StateEvent: status.StateUnconfigured}.Fill(nil)) + } + br.Log.Debugln("Starting custom puppets") + for _, loopuppet := range br.GetAllPuppetsWithCustomMXID() { go func(puppet *Puppet) { puppet.log.Debugln("Starting custom puppet", puppet.CustomMXID) - err := puppet.StartCustomMXID() + err := puppet.StartCustomMXID(true) if err != nil { puppet.log.Errorln("Failed to start custom puppet:", err) } @@ -375,89 +151,58 @@ func (bridge *Bridge) StartUsers() { } } -func (bridge *Bridge) Stop() { - if bridge.Crypto != nil { - bridge.Crypto.Stop() - } - bridge.AS.Stop() - bridge.Metrics.Stop() - bridge.EventProcessor.Stop() - for _, user := range bridge.usersByJID { - if user.Conn == nil { +func (br *GMBridge) Stop() { + br.Metrics.Stop() + // TODO anything needed to disconnect the users? + for _, user := range br.usersByUsername { + if user.Client == nil { continue } - bridge.Log.Debugln("Disconnecting", user.MXID) - //sess, err := - //user.Conn.Stop(context.TODO()) - // if err != nil { - // bridge.Log.Errorfln("Error while disconnecting %s: %v", user.MXID, err) - // } else if len(sess.Wid) > 0 { - // user.SetSession(&sess) - // } + br.Log.Debugln("Disconnecting", user.MXID) } } -var cpuprofile = flag.MakeFull("", "cpuprofile", "write cpu profile to `file`", "").String() +func (br *GMBridge) GetExampleConfig() string { + return ExampleConfig +} -func (bridge *Bridge) Main() { - - if *generateRegistration { - bridge.GenerateRegistration() - return - } else if *migrateFrom { - bridge.MigrateDatabase() - return +func (br *GMBridge) GetConfigPtr() interface{} { + br.Config = &config.Config{ + BaseConfig: &br.Bridge.Config, } - bridge.Init() - bridge.Log.Infoln("Bridge initialization complete, starting...") - bridge.Start() - bridge.Log.Infoln("Bridge started!") - - if *cpuprofile != "" { - println("profiling") - f, err := os.Create(*cpuprofile) - if err != nil { - log.Fatal("could not create CPU profile: ", err) - } - defer f.Close() // error handling omitted for example - if err := pprof.StartCPUProfile(f); err != nil { - log.Fatal("could not start CPU profile: ", err) - } - defer pprof.StopCPUProfile() - } - - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt, syscall.SIGTERM) - <-c - - bridge.Log.Infoln("Interrupt received, stopping...") - bridge.Stop() - bridge.Log.Infoln("Bridge stopped.") - //os.Exit(0) + br.Config.BaseConfig.Bridge = &br.Config.Bridge + return br.Config } func main() { - flag.SetHelpTitles( - "go-groupme - A Matrix-GroupMe puppeting bridge.", - "go-groupme [-h] [-c ] [-r ] [-g] [--migrate-db ]") - err := flag.Parse() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - flag.PrintHelp() - os.Exit(1) - } else if *wantHelp { - flag.PrintHelp() - os.Exit(0) - } else if *version { - if Tag == Version { - fmt.Printf("%s %s (%s)\n", Name, Tag, BuildTime) - } else if len(Commit) > 8 { - fmt.Printf("%s %s.%s (%s)\n", Name, Version, Commit[:8], BuildTime) - } else { - fmt.Printf("%s %s.unknown\n", Name, Version) - } - return + br := &GMBridge{ + usersByMXID: make(map[id.UserID]*User), + usersByUsername: make(map[string]*User), + spaceRooms: make(map[id.RoomID]*User), + managementRooms: make(map[id.RoomID]*User), + portalsByMXID: make(map[id.RoomID]*Portal), + portalsByGMID: make(map[database.PortalKey]*Portal), + puppets: make(map[groupme.ID]*Puppet), + puppetsByCustomMXID: make(map[id.UserID]*Puppet), } + br.Bridge = bridge.Bridge{ + Name: "groupme-matrix", + URL: "https://github.com/beeper/groupme", + Description: "A Matrix-GroupMe puppeting bridge.", + Version: "0.1.0", + ProtocolName: "GroupMe", - NewBridge().Main() + CryptoPickleKey: "github.com/beeper/groupme", + + ConfigUpgrader: &configupgrade.StructUpgrader{ + SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade), + Blocks: config.SpacedBlocks, + Base: ExampleConfig, + }, + + Child: br, + } + br.InitVersion(Tag, Commit, BuildTime) + + br.Main() } diff --git a/matrix.go b/matrix.go index 02ac6c5..77c5858 100644 --- a/matrix.go +++ b/matrix.go @@ -17,15 +17,10 @@ package main import ( - "errors" "fmt" - "strings" - "time" - - "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix" - "maunium.net/go/mautrix/appservice" + "maunium.net/go/mautrix/bridge" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" @@ -33,433 +28,76 @@ import ( "github.com/beeper/groupme/database" ) -type MatrixHandler struct { - bridge *Bridge - as *appservice.AppService - log maulogger.Logger - cmd *CommandHandler -} - -func NewMatrixHandler(bridge *Bridge) *MatrixHandler { - handler := &MatrixHandler{ - bridge: bridge, - as: bridge.AS, - log: bridge.Log.Sub("Matrix"), - cmd: NewCommandHandler(bridge), - } - bridge.EventProcessor.On(event.EventMessage, handler.HandleMessage) - bridge.EventProcessor.On(event.EventEncrypted, handler.HandleEncrypted) - bridge.EventProcessor.On(event.EventSticker, handler.HandleMessage) - bridge.EventProcessor.On(event.EventRedaction, handler.HandleRedaction) - bridge.EventProcessor.On(event.StateMember, handler.HandleMembership) - bridge.EventProcessor.On(event.StateRoomName, handler.HandleRoomMetadata) - bridge.EventProcessor.On(event.StateRoomAvatar, handler.HandleRoomMetadata) - bridge.EventProcessor.On(event.StateTopic, handler.HandleRoomMetadata) - bridge.EventProcessor.On(event.StateEncryption, handler.HandleEncryption) - return handler -} - -func (mx *MatrixHandler) HandleEncryption(evt *event.Event) { - defer mx.bridge.Metrics.TrackEvent(evt.Type)() - if evt.Content.AsEncryption().Algorithm != id.AlgorithmMegolmV1 { - return - } - portal := mx.bridge.GetPortalByMXID(evt.RoomID) - if portal != nil && !portal.Encrypted { - mx.log.Debugfln("%s enabled encryption in %s", evt.Sender, evt.RoomID) - portal.Encrypted = true - portal.Update() - } -} - -func (mx *MatrixHandler) joinAndCheckMembers(evt *event.Event, intent *appservice.IntentAPI) *mautrix.RespJoinedMembers { - resp, err := intent.JoinRoomByID(evt.RoomID) - if err != nil { - mx.log.Debugfln("Failed to join room %s as %s with invite from %s: %v", evt.RoomID, intent.UserID, evt.Sender, err) - return nil - } - - members, err := intent.JoinedMembers(resp.RoomID) - if err != nil { - mx.log.Debugfln("Failed to get members in room %s after accepting invite from %s as %s: %v", resp.RoomID, evt.Sender, intent.UserID, err) - _, _ = intent.LeaveRoom(resp.RoomID) - return nil - } - - if len(members.Joined) < 2 { - mx.log.Debugln("Leaving empty room", resp.RoomID, "after accepting invite from", evt.Sender, "as", intent.UserID) - _, _ = intent.LeaveRoom(resp.RoomID) - return nil - } - return members -} - -func (mx *MatrixHandler) HandleBotInvite(evt *event.Event) { - intent := mx.as.BotIntent() - - user := mx.bridge.GetUserByMXID(evt.Sender) - if user == nil { - return - } - - members := mx.joinAndCheckMembers(evt, intent) - if members == nil { - return - } - - if !user.Whitelisted { - _, _ = intent.SendNotice(evt.RoomID, "You are not whitelisted to use this bridge.\n"+ - "If you're the owner of this bridge, see the bridge.permissions section in your config file.") - _, _ = intent.LeaveRoom(evt.RoomID) - return - } - - if evt.RoomID == mx.bridge.Config.Bridge.Relaybot.ManagementRoom { - _, _ = intent.SendNotice(evt.RoomID, "This is the relaybot management room. Send `!wa help` to get a list of commands.") - mx.log.Debugln("Joined relaybot management room", evt.RoomID, "after invite from", evt.Sender) - return - } - - hasPuppets := false - for mxid, _ := range members.Joined { - if mxid == intent.UserID || mxid == evt.Sender { - continue - } else if _, ok := mx.bridge.ParsePuppetMXID(mxid); ok { - hasPuppets = true - continue - } - mx.log.Debugln("Leaving multi-user room", evt.RoomID, "after accepting invite from", evt.Sender) - _, _ = intent.SendNotice(evt.RoomID, "This bridge is user-specific, please don't invite me into rooms with other users.") - _, _ = intent.LeaveRoom(evt.RoomID) - return - } - - if !hasPuppets && (len(user.ManagementRoom) == 0 || evt.Content.AsMember().IsDirect) { - user.SetManagementRoom(evt.RoomID) - _, _ = intent.SendNotice(user.ManagementRoom, "This room has been registered as your bridge management/status room. Send `help` to get a list of commands.") - mx.log.Debugln(evt.RoomID, "registered as a management room with", evt.Sender) - } -} - -func (mx *MatrixHandler) handlePrivatePortal(roomID id.RoomID, inviter *User, puppet *Puppet, key database.PortalKey) { - portal := mx.bridge.GetPortalByJID(key) +func (br *GMBridge) CreatePrivatePortal(roomID id.RoomID, brInviter bridge.User, brGhost bridge.Ghost) { + inviter := brInviter.(*User) + puppet := brGhost.(*Puppet) + key := database.NewPortalKey(puppet.GMID, inviter.GMID) + portal := br.GetPortalByGMID(key) if len(portal.MXID) == 0 { - mx.createPrivatePortalFromInvite(roomID, inviter, puppet, portal) + br.createPrivatePortalFromInvite(roomID, inviter, puppet, portal) return } - err := portal.MainIntent().EnsureInvited(portal.MXID, inviter.MXID) - if err != nil { - mx.log.Warnfln("Failed to invite %s to existing private chat portal %s with %s: %v. Redirecting portal to new room...", inviter.MXID, portal.MXID, puppet.JID, err) - mx.createPrivatePortalFromInvite(roomID, inviter, puppet, portal) + ok := portal.ensureUserInvited(inviter) + if !ok { + br.Log.Warnfln("Failed to invite %s to existing private chat portal %s with %s. Redirecting portal to new room...", inviter.MXID, portal.MXID, puppet.GMID) + br.createPrivatePortalFromInvite(roomID, inviter, puppet, portal) return } intent := puppet.DefaultIntent() - _, _ = intent.SendNotice(roomID, "You already have a private chat portal with me at %s") - mx.log.Debugln("Leaving private chat room", roomID, "as", puppet.MXID, "after accepting invite from", inviter.MXID, "as we already have chat with the user") + errorMessage := fmt.Sprintf("You already have a private chat portal with me at [%[1]s](https://matrix.to/#/%[1]s)", portal.MXID) + errorContent := format.RenderMarkdown(errorMessage, true, false) + _, _ = intent.SendMessageEvent(roomID, event.EventMessage, errorContent) + br.Log.Debugfln("Leaving private chat room %s as %s after accepting invite from %s as we already have chat with the user", roomID, puppet.MXID, inviter.MXID) _, _ = intent.LeaveRoom(roomID) } -func (mx *MatrixHandler) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) { +func (br *GMBridge) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) { + // TODO check if room is already encrypted + var existingEncryption event.EncryptionEventContent + var encryptionEnabled bool + err := portal.MainIntent().StateEvent(roomID, event.StateEncryption, "", &existingEncryption) + if err != nil { + portal.log.Warnfln("Failed to check if encryption is enabled in private chat room %s", roomID) + } else { + encryptionEnabled = existingEncryption.Algorithm == id.AlgorithmMegolmV1 + } portal.MXID = roomID - portal.Topic = "WhatsApp private chat" - portal.Key = database.PortalKey{JID: puppet.JID, Receiver: inviter.JID} - _, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic) - if portal.bridge.Config.Bridge.PrivateChatPortalMeta { - m, _ := mx.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID) - portal.Name = m.DisplayName - portal.AvatarURL = id.MustParseContentURI(m.AvatarURL) - print("possible bug with pointer above") - portal.Avatar = m.Avatar + if portal.bridge.Config.Bridge.PrivateChatPortalMeta || br.Config.Bridge.Encryption.Default || encryptionEnabled { + portal.Name = puppet.Displayname + portal.AvatarURL = puppet.AvatarURL + portal.Avatar = puppet.Avatar _, _ = portal.MainIntent().SetRoomName(portal.MXID, portal.Name) _, _ = portal.MainIntent().SetRoomAvatar(portal.MXID, portal.AvatarURL) } else { portal.Name = "" } - portal.log.Infoln("Created private chat portal in %s after invite from", roomID, inviter.MXID) + portal.log.Infofln("Created private chat portal in %s after invite from %s", roomID, inviter.MXID) intent := puppet.DefaultIntent() - if mx.bridge.Config.Bridge.Encryption.Default { - _, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: mx.bridge.Bot.UserID}) + if br.Config.Bridge.Encryption.Default || encryptionEnabled { + _, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: br.Bot.UserID}) if err != nil { portal.log.Warnln("Failed to invite bridge bot to enable e2be:", err) } - err = mx.bridge.Bot.EnsureJoined(roomID) + err = br.Bot.EnsureJoined(roomID) if err != nil { portal.log.Warnln("Failed to join as bridge bot to enable e2be:", err) } - _, err = intent.SendStateEvent(roomID, event.StateEncryption, "", &event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}) - if err != nil { - portal.log.Warnln("Failed to enable e2be:", err) + if !encryptionEnabled { + _, err = intent.SendStateEvent(roomID, event.StateEncryption, "", portal.GetEncryptionEventContent()) + if err != nil { + portal.log.Warnln("Failed to enable e2be:", err) + } } - mx.as.StateStore.SetMembership(roomID, inviter.MXID, event.MembershipJoin) - mx.as.StateStore.SetMembership(roomID, puppet.MXID, event.MembershipJoin) - mx.as.StateStore.SetMembership(roomID, mx.bridge.Bot.UserID, event.MembershipJoin) + br.AS.StateStore.SetMembership(roomID, inviter.MXID, event.MembershipJoin) + br.AS.StateStore.SetMembership(roomID, puppet.MXID, event.MembershipJoin) + br.AS.StateStore.SetMembership(roomID, br.Bot.UserID, event.MembershipJoin) portal.Encrypted = true } - portal.Update() + portal.Update(nil) portal.UpdateBridgeInfo() _, _ = intent.SendNotice(roomID, "Private chat portal created") - - err := portal.FillInitialHistory(inviter) - if err != nil { - portal.log.Errorln("Failed to fill history:", err) - } - - inviter.addPortalToCommunity(portal) - inviter.addPuppetToCommunity(puppet) -} - -func (mx *MatrixHandler) HandlePuppetInvite(evt *event.Event, inviter *User, puppet *Puppet) { - intent := puppet.DefaultIntent() - members := mx.joinAndCheckMembers(evt, intent) - if members == nil { - return - } - var hasBridgeBot, hasOtherUsers bool - for mxid, _ := range members.Joined { - if mxid == intent.UserID || mxid == inviter.MXID { - continue - } else if mxid == mx.bridge.Bot.UserID { - hasBridgeBot = true - } else { - hasOtherUsers = true - } - } - if !hasBridgeBot && !hasOtherUsers { - key := database.NewPortalKey(puppet.JID, inviter.JID) - mx.handlePrivatePortal(evt.RoomID, inviter, puppet, key) - } else if !hasBridgeBot { - mx.log.Debugln("Leaving multi-user room", evt.RoomID, "as", puppet.MXID, "after accepting invite from", evt.Sender) - _, _ = intent.SendNotice(evt.RoomID, "Please invite the bridge bot first if you want to bridge to a WhatsApp group.") - _, _ = intent.LeaveRoom(evt.RoomID) - } else { - _, _ = intent.SendNotice(evt.RoomID, "This puppet will remain inactive until this room is bridged to a WhatsApp group.") - } -} - -func (mx *MatrixHandler) HandleMembership(evt *event.Event) { - if _, isPuppet := mx.bridge.ParsePuppetMXID(evt.Sender); evt.Sender == mx.bridge.Bot.UserID || isPuppet { - return - } - defer mx.bridge.Metrics.TrackEvent(evt.Type)() - - if mx.bridge.Crypto != nil { - mx.bridge.Crypto.HandleMemberEvent(evt) - } - - content := evt.Content.AsMember() - if content.Membership == event.MembershipInvite && id.UserID(evt.GetStateKey()) == mx.as.BotMXID() { - mx.HandleBotInvite(evt) - return - } - - user := mx.bridge.GetUserByMXID(evt.Sender) - if user == nil || !user.Whitelisted || !user.IsConnected() { - return - } - - portal := mx.bridge.GetPortalByMXID(evt.RoomID) - if portal == nil { - puppet := mx.bridge.GetPuppetByMXID(id.UserID(evt.GetStateKey())) - if content.Membership == event.MembershipInvite && puppet != nil { - mx.HandlePuppetInvite(evt, user, puppet) - } - return - } - - isSelf := id.UserID(evt.GetStateKey()) == evt.Sender - - if content.Membership == event.MembershipLeave { - if isSelf { - if evt.Unsigned.PrevContent != nil { - _ = evt.Unsigned.PrevContent.ParseRaw(evt.Type) - prevContent, ok := evt.Unsigned.PrevContent.Parsed.(*event.MemberEventContent) - if ok { - if portal.IsPrivateChat() || prevContent.Membership == "join" { - portal.HandleMatrixLeave(user) - } - } - } - } else { - portal.HandleMatrixKick(user, evt) - } - } else if content.Membership == event.MembershipInvite && !isSelf { - portal.HandleMatrixInvite(user, evt) - } -} - -func (mx *MatrixHandler) HandleRoomMetadata(evt *event.Event) { - // defer mx.bridge.Metrics.TrackEvent(evt.Type)() - // user := mx.bridge.GetUserByMXID(evt.Sender) - // if user == nil || !user.Whitelisted || !user.IsConnected() { - // return - // } - - // portal := mx.bridge.GetPortalByMXID(evt.RoomID) - // if portal == nil || portal.IsPrivateChat() { - // return - // } - - // var resp <-chan string - // var err error - // switch content := evt.Content.Parsed.(type) { - // case *event.RoomNameEventContent: - // resp, err = user.Conn.UpdateGroupSubject(content.Name, portal.Key.JID) - // case *event.TopicEventContent: - // resp, err = user.Conn.UpdateGroupDescription(portal.Key.JID, content.Topic) - // case *event.RoomAvatarEventContent: - // return - // } - // if err != nil { - // mx.log.Errorln(err) - // } else { - // out := <-resp - // mx.log.Infoln(out) - // } -} - -func (mx *MatrixHandler) shouldIgnoreEvent(evt *event.Event) bool { - if _, isPuppet := mx.bridge.ParsePuppetMXID(evt.Sender); evt.Sender == mx.bridge.Bot.UserID || isPuppet { - return true - } - isCustomPuppet, ok := evt.Content.Raw["net.maunium.whatsapp.puppet"].(bool) - if ok && isCustomPuppet && mx.bridge.GetPuppetByCustomMXID(evt.Sender) != nil { - return true - } - user := mx.bridge.GetUserByMXID(evt.Sender) - if !user.RelaybotWhitelisted { - return true - } - return false -} - -const sessionWaitTimeout = 5 * time.Second - -func (mx *MatrixHandler) HandleEncrypted(evt *event.Event) { - println("IDK iF encryption works yet") - defer mx.bridge.Metrics.TrackEvent(evt.Type)() - if mx.shouldIgnoreEvent(evt) || mx.bridge.Crypto == nil { - return - } - - decrypted, err := mx.bridge.Crypto.Decrypt(evt) - if errors.Is(err, NoSessionFound) { - content := evt.Content.AsEncrypted() - mx.log.Debugfln("Couldn't find session %s trying to decrypt %s, waiting %d seconds...", content.SessionID, evt.ID, int(sessionWaitTimeout.Seconds())) - if mx.bridge.Crypto.WaitForSession(evt.RoomID, content.SenderKey, content.SessionID, sessionWaitTimeout) { - mx.log.Debugfln("Got session %s after waiting, trying to decrypt %s again", content.SessionID, evt.ID) - decrypted, err = mx.bridge.Crypto.Decrypt(evt) - } else { - go mx.waitLongerForSession(evt) - return - } - } - if err != nil { - mx.log.Warnfln("Failed to decrypt %s: %v", evt.ID, err) - _, _ = mx.bridge.Bot.SendNotice(evt.RoomID, fmt.Sprintf( - "\u26a0 Your message was not bridged: %v", err)) - return - } - mx.bridge.EventProcessor.Dispatch(decrypted) -} - -func (mx *MatrixHandler) waitLongerForSession(evt *event.Event) { - const extendedTimeout = sessionWaitTimeout * 2 - - content := evt.Content.AsEncrypted() - mx.log.Debugfln("Couldn't find session %s trying to decrypt %s, waiting %d more seconds...", - content.SessionID, evt.ID, int(extendedTimeout.Seconds())) - - resp, err := mx.bridge.Bot.SendNotice(evt.RoomID, fmt.Sprintf( - "\u26a0 Your message was not bridged: the bridge hasn't received the decryption keys. "+ - "The bridge will retry for %d seconds. If this error keeps happening, try restarting your client.", - int(extendedTimeout.Seconds()))) - if err != nil { - mx.log.Errorfln("Failed to send decryption error to %s: %v", evt.RoomID, err) - } - update := event.MessageEventContent{MsgType: event.MsgNotice} - - if mx.bridge.Crypto.WaitForSession(evt.RoomID, content.SenderKey, content.SessionID, extendedTimeout) { - mx.log.Debugfln("Got session %s after waiting more, trying to decrypt %s again", content.SessionID, evt.ID) - decrypted, err := mx.bridge.Crypto.Decrypt(evt) - if err == nil { - mx.bridge.EventProcessor.Dispatch(decrypted) - _, _ = mx.bridge.Bot.RedactEvent(evt.RoomID, resp.EventID) - return - } - mx.log.Warnfln("Failed to decrypt %s: %v", err) - update.Body = fmt.Sprintf("\u26a0 Your message was not bridged: %v", err) - } else { - mx.log.Debugfln("Didn't get %s, giving up on %s", content.SessionID, evt.ID) - update.Body = "\u26a0 Your message was not bridged: the bridge hasn't received the decryption keys. " + - "If this keeps happening, try restarting your client." - } - - newContent := update - update.NewContent = &newContent - if resp != nil { - update.RelatesTo = &event.RelatesTo{ - Type: event.RelReplace, - EventID: resp.EventID, - } - } - _, _ = mx.bridge.Bot.SendMessageEvent(evt.RoomID, event.EventMessage, &update) -} - -func (mx *MatrixHandler) HandleMessage(evt *event.Event) { - defer mx.bridge.Metrics.TrackEvent(evt.Type)() - if mx.shouldIgnoreEvent(evt) { - return - } - - user := mx.bridge.GetUserByMXID(evt.Sender) - content := evt.Content.AsMessage() - if user.Whitelisted && content.MsgType == event.MsgText { - commandPrefix := mx.bridge.Config.Bridge.CommandPrefix - hasCommandPrefix := strings.HasPrefix(content.Body, commandPrefix) - if hasCommandPrefix { - content.Body = strings.TrimLeft(content.Body[len(commandPrefix):], " ") - } - if hasCommandPrefix || evt.RoomID == user.ManagementRoom { - mx.cmd.Handle(evt.RoomID, user, content.Body) - return - } - } - - portal := mx.bridge.GetPortalByMXID(evt.RoomID) - if portal != nil && (user.Whitelisted || portal.HasRelaybot()) { - portal.HandleMatrixMessage(user, evt) - } -} - -func (mx *MatrixHandler) HandleRedaction(evt *event.Event) { - defer mx.bridge.Metrics.TrackEvent(evt.Type)() - if _, isPuppet := mx.bridge.ParsePuppetMXID(evt.Sender); evt.Sender == mx.bridge.Bot.UserID || isPuppet { - return - } - - user := mx.bridge.GetUserByMXID(evt.Sender) - - if !user.Whitelisted { - return - } - - if !user.HasSession() { - return - } else if !user.IsConnected() { - msg := format.RenderMarkdown(fmt.Sprintf("[%[1]s](https://matrix.to/#/%[1]s): \u26a0 "+ - "You are not connected to WhatsApp, so your redaction was not bridged. "+ - "Use `%[2]s reconnect` to reconnect.", user.MXID, mx.bridge.Config.Bridge.CommandPrefix), true, false) - msg.MsgType = event.MsgNotice - _, _ = mx.bridge.Bot.SendMessageEvent(evt.RoomID, event.EventMessage, msg) - return - } - - portal := mx.bridge.GetPortalByMXID(evt.RoomID) - if portal != nil { - portal.HandleMatrixRedaction(user, evt) - } } diff --git a/metrics.go b/metrics.go index 5066ee0..b197e36 100644 --- a/metrics.go +++ b/metrics.go @@ -22,6 +22,7 @@ import ( "runtime/debug" "time" + "github.com/karmanyaahm/groupme" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -31,7 +32,6 @@ import ( "maunium.net/go/mautrix/id" "github.com/beeper/groupme/database" - "github.com/beeper/groupme/types" ) type MetricsHandler struct { @@ -43,7 +43,7 @@ type MetricsHandler struct { ctx context.Context stopRecorder func() - messageHandling *prometheus.HistogramVec + matrixEventHandling *prometheus.HistogramVec countCollection prometheus.Histogram disconnections *prometheus.CounterVec puppetCount prometheus.Gauge @@ -56,17 +56,17 @@ type MetricsHandler struct { unencryptedPrivateCount prometheus.Gauge connected prometheus.Gauge - connectedState map[types.GroupMeID]bool + connectedState map[groupme.ID]bool loggedIn prometheus.Gauge - loggedInState map[types.GroupMeID]bool + loggedInState map[groupme.ID]bool syncLocked prometheus.Gauge - syncLockedState map[types.GroupMeID]bool + syncLockedState map[groupme.ID]bool bufferLength *prometheus.GaugeVec } func NewMetricsHandler(address string, log log.Logger, db *database.Database) *MetricsHandler { portalCount := promauto.NewGaugeVec(prometheus.GaugeOpts{ - Name: "whatsapp_portals_total", + Name: "groupme_portals_total", Help: "Number of portal rooms on Matrix", }, []string{"type", "encrypted"}) return &MetricsHandler{ @@ -75,28 +75,28 @@ func NewMetricsHandler(address string, log log.Logger, db *database.Database) *M log: log, running: false, - messageHandling: promauto.NewHistogramVec(prometheus.HistogramOpts{ + matrixEventHandling: promauto.NewHistogramVec(prometheus.HistogramOpts{ Name: "matrix_event", Help: "Time spent processing Matrix events", }, []string{"event_type"}), countCollection: promauto.NewHistogram(prometheus.HistogramOpts{ - Name: "whatsapp_count_collection", - Help: "Time spent collecting the whatsapp_*_total metrics", + Name: "groupme_count_collection", + Help: "Time spent collecting the groupme_*_total metrics", }), disconnections: promauto.NewCounterVec(prometheus.CounterOpts{ - Name: "whatsapp_disconnections", - Help: "Number of times a Matrix user has been disconnected from WhatsApp", + Name: "groupme_disconnections", + Help: "Number of times a Matrix user has been disconnected from GroupMe", }, []string{"user_id"}), puppetCount: promauto.NewGauge(prometheus.GaugeOpts{ - Name: "whatsapp_puppets_total", - Help: "Number of WhatsApp users bridged into Matrix", + Name: "groupme_puppets_total", + Help: "Number of GroupMe users bridged into Matrix", }), userCount: promauto.NewGauge(prometheus.GaugeOpts{ - Name: "whatsapp_users_total", + Name: "groupme_users_total", Help: "Number of Matrix users using the bridge", }), messageCount: promauto.NewGauge(prometheus.GaugeOpts{ - Name: "whatsapp_messages_total", + Name: "groupme_messages_total", Help: "Number of messages bridged", }), portalCount: portalCount, @@ -109,17 +109,17 @@ func NewMetricsHandler(address string, log log.Logger, db *database.Database) *M Name: "bridge_logged_in", Help: "Users logged into the bridge", }), - loggedInState: make(map[types.GroupMeID]bool), + loggedInState: make(map[groupme.ID]bool), connected: promauto.NewGauge(prometheus.GaugeOpts{ Name: "bridge_connected", - Help: "Bridge users connected to WhatsApp", + Help: "Bridge users connected to GroupMe", }), - connectedState: make(map[types.GroupMeID]bool), + connectedState: make(map[groupme.ID]bool), syncLocked: promauto.NewGauge(prometheus.GaugeOpts{ Name: "bridge_sync_locked", Help: "Bridge users locked in post-login sync", }), - syncLockedState: make(map[types.GroupMeID]bool), + syncLockedState: make(map[groupme.ID]bool), bufferLength: promauto.NewGaugeVec(prometheus.GaugeOpts{ Name: "bridge_buffer_size", Help: "Number of messages in buffer", @@ -129,14 +129,14 @@ func NewMetricsHandler(address string, log log.Logger, db *database.Database) *M func noop() {} -func (mh *MetricsHandler) TrackEvent(eventType event.Type) func() { +func (mh *MetricsHandler) TrackMatrixEvent(eventType event.Type) func() { if !mh.running { return noop } start := time.Now() return func() { duration := time.Now().Sub(start) - mh.messageHandling. + mh.matrixEventHandling. With(prometheus.Labels{"event_type": eventType.Type}). Observe(duration.Seconds()) } @@ -149,13 +149,13 @@ func (mh *MetricsHandler) TrackDisconnection(userID id.UserID) { mh.disconnections.With(prometheus.Labels{"user_id": string(userID)}).Inc() } -func (mh *MetricsHandler) TrackLoginState(jid types.GroupMeID, loggedIn bool) { +func (mh *MetricsHandler) TrackLoginState(gmid groupme.ID, loggedIn bool) { if !mh.running { return } - currentVal, ok := mh.loggedInState[jid] + currentVal, ok := mh.loggedInState[gmid] if !ok || currentVal != loggedIn { - mh.loggedInState[jid] = loggedIn + mh.loggedInState[gmid] = loggedIn if loggedIn { mh.loggedIn.Inc() } else { @@ -164,13 +164,13 @@ func (mh *MetricsHandler) TrackLoginState(jid types.GroupMeID, loggedIn bool) { } } -func (mh *MetricsHandler) TrackConnectionState(jid types.GroupMeID, connected bool) { +func (mh *MetricsHandler) TrackConnectionState(gmid groupme.ID, connected bool) { if !mh.running { return } - currentVal, ok := mh.connectedState[jid] + currentVal, ok := mh.connectedState[gmid] if !ok || currentVal != connected { - mh.connectedState[jid] = connected + mh.connectedState[gmid] = connected if connected { mh.connected.Inc() } else { @@ -179,13 +179,13 @@ func (mh *MetricsHandler) TrackConnectionState(jid types.GroupMeID, connected bo } } -func (mh *MetricsHandler) TrackSyncLock(jid types.GroupMeID, locked bool) { +func (mh *MetricsHandler) TrackSyncLock(gmid groupme.ID, locked bool) { if !mh.running { return } - currentVal, ok := mh.syncLockedState[jid] + currentVal, ok := mh.syncLockedState[gmid] if !ok || currentVal != locked { - mh.syncLockedState[jid] = locked + mh.syncLockedState[gmid] = locked if locked { mh.syncLocked.Inc() } else { @@ -230,10 +230,10 @@ func (mh *MetricsHandler) updateStats() { // var encryptedGroupCount, encryptedPrivateCount, unencryptedGroupCount, unencryptedPrivateCount int // err = mh.db.QueryRowContext(mh.ctx, ` // SELECT - // COUNT(CASE WHEN jid LIKE '%@g.us' AND encrypted THEN 1 END) AS encrypted_group_portals, - // COUNT(CASE WHEN jid LIKE '%@s.whatsapp.net' AND encrypted THEN 1 END) AS encrypted_private_portals, - // COUNT(CASE WHEN jid LIKE '%@g.us' AND NOT encrypted THEN 1 END) AS unencrypted_group_portals, - // COUNT(CASE WHEN jid LIKE '%@s.whatsapp.net' AND NOT encrypted THEN 1 END) AS unencrypted_private_portals + // COUNT(CASE WHEN gmid LIKE '%@g.us' AND encrypted THEN 1 END) AS encrypted_group_portals, + // COUNT(CASE WHEN gmid LIKE '%@s.groupme.net' AND encrypted THEN 1 END) AS encrypted_private_portals, + // COUNT(CASE WHEN gmid LIKE '%@g.us' AND NOT encrypted THEN 1 END) AS unencrypted_group_portals, + // COUNT(CASE WHEN gmid LIKE '%@s.groupme.net' AND NOT encrypted THEN 1 END) AS unencrypted_private_portals // FROM portal WHERE mxid<>'' // `).Scan(&encryptedGroupCount, &encryptedPrivateCount, &unencryptedGroupCount, &unencryptedPrivateCount) // if err != nil { diff --git a/portal.go b/portal.go index a92d1b4..73a3f0d 100644 --- a/portal.go +++ b/portal.go @@ -19,22 +19,13 @@ package main import ( "bytes" "context" - "encoding/hex" "errors" "fmt" - "html" "image" - "image/gif" - "image/jpeg" - "image/png" "io/ioutil" "math" - "math/rand" "net/http" "net/url" - "os" - "os/exec" - "path/filepath" "strconv" "strings" "sync" @@ -42,6 +33,8 @@ import ( log "maunium.net/go/maulogger/v2" + "maunium.net/go/mautrix/bridge" + "maunium.net/go/mautrix/bridge/bridgeconfig" "maunium.net/go/mautrix/crypto/attachment" "github.com/Rhymen/go-whatsapp" @@ -51,17 +44,13 @@ import ( "maunium.net/go/mautrix" "maunium.net/go/mautrix/appservice" "maunium.net/go/mautrix/event" - "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" - "maunium.net/go/mautrix/pushrules" "github.com/beeper/groupme/database" - "github.com/beeper/groupme/groupmeExt" - "github.com/beeper/groupme/types" - whatsappExt "github.com/beeper/groupme/whatsapp-ext" + "github.com/beeper/groupme/groupmeext" ) -func (bridge *Bridge) GetPortalByMXID(mxid id.RoomID) *Portal { +func (bridge *GMBridge) GetPortalByMXID(mxid id.RoomID) *Portal { bridge.portalsLock.Lock() defer bridge.portalsLock.Unlock() portal, ok := bridge.portalsByMXID[mxid] @@ -71,25 +60,57 @@ func (bridge *Bridge) GetPortalByMXID(mxid id.RoomID) *Portal { return portal } -func (bridge *Bridge) GetPortalByJID(key database.PortalKey) *Portal { +func (br *GMBridge) GetIPortal(mxid id.RoomID) bridge.Portal { + p := br.GetPortalByMXID(mxid) + if p == nil { + return nil + } + return p +} + +func (portal *Portal) IsEncrypted() bool { + return portal.Encrypted +} + +func (portal *Portal) MarkEncrypted() { + portal.Encrypted = true + portal.Update(nil) +} + +func (portal *Portal) ReceiveMatrixEvent(user bridge.User, evt *event.Event) { + if user.GetPermissionLevel() >= bridgeconfig.PermissionLevelUser { + portal.matrixMessages <- PortalMatrixMessage{user: user.(*User), evt: evt, receivedAt: time.Now()} + } +} + +func (bridge *GMBridge) GetPortalByGMID(key database.PortalKey) *Portal { bridge.portalsLock.Lock() defer bridge.portalsLock.Unlock() - portal, ok := bridge.portalsByJID[key] + portal, ok := bridge.portalsByGMID[key] if !ok { - return bridge.loadDBPortal(bridge.DB.Portal.GetByJID(key), &key) + return bridge.loadDBPortal(bridge.DB.Portal.GetByGMID(key), &key) } return portal } -func (bridge *Bridge) GetAllPortals() []*Portal { - return bridge.dbPortalsToPortals(bridge.DB.Portal.GetAll()) +func (br *GMBridge) GetAllPortals() []*Portal { + return br.dbPortalsToPortals(br.DB.Portal.GetAll()) } -func (bridge *Bridge) GetAllPortalsByJID(jid types.GroupMeID) []*Portal { - return bridge.dbPortalsToPortals(bridge.DB.Portal.GetAllByJID(jid)) +func (br *GMBridge) GetAllIPortals() (iportals []bridge.Portal) { + portals := br.GetAllPortals() + iportals = make([]bridge.Portal, len(portals)) + for i, portal := range portals { + iportals[i] = portal + } + return iportals } -func (bridge *Bridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal { +func (br *GMBridge) GetAllPortalsByGMID(gmid groupme.ID) []*Portal { + return br.dbPortalsToPortals(br.DB.Portal.GetAllByGMID(gmid)) +} + +func (bridge *GMBridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal { bridge.portalsLock.Lock() defer bridge.portalsLock.Unlock() output := make([]*Portal, len(dbPortals)) @@ -97,7 +118,7 @@ func (bridge *Bridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal if dbPortal == nil { continue } - portal, ok := bridge.portalsByJID[dbPortal.Key] + portal, ok := bridge.portalsByGMID[dbPortal.Key] if !ok { portal = bridge.loadDBPortal(dbPortal, nil) } @@ -106,7 +127,7 @@ func (bridge *Bridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal return output } -func (bridge *Bridge) loadDBPortal(dbPortal *database.Portal, key *database.PortalKey) *Portal { +func (bridge *GMBridge) loadDBPortal(dbPortal *database.Portal, key *database.PortalKey) *Portal { if dbPortal == nil { if key == nil { return nil @@ -116,7 +137,7 @@ func (bridge *Bridge) loadDBPortal(dbPortal *database.Portal, key *database.Port dbPortal.Insert() } portal := bridge.NewPortal(dbPortal) - bridge.portalsByJID[portal.Key] = portal + bridge.portalsByGMID[portal.Key] = portal if len(portal.MXID) > 0 { bridge.portalsByMXID[portal.MXID] = portal } @@ -127,7 +148,7 @@ func (portal *Portal) GetUsers() []*User { return nil } -func (bridge *Bridge) NewManualPortal(key database.PortalKey) *Portal { +func (bridge *GMBridge) NewManualPortal(key database.PortalKey) *Portal { portal := &Portal{ Portal: bridge.DB.Portal.New(), bridge: bridge, @@ -142,7 +163,7 @@ func (bridge *Bridge) NewManualPortal(key database.PortalKey) *Portal { return portal } -func (bridge *Bridge) NewPortal(dbPortal *database.Portal) *Portal { +func (bridge *GMBridge) NewPortal(dbPortal *database.Portal) *Portal { portal := &Portal{ Portal: dbPortal, bridge: bridge, @@ -165,10 +186,16 @@ type PortalMessage struct { timestamp uint64 } +type PortalMatrixMessage struct { + evt *event.Event + user *User + receivedAt time.Time +} + type Portal struct { *database.Portal - bridge *Bridge + bridge *GMBridge log log.Logger roomCreateLock sync.Mutex @@ -177,13 +204,14 @@ type Portal struct { recentlyHandledLock sync.Mutex recentlyHandledIndex uint8 - backfillLock sync.Mutex + encryptLock sync.Mutex backfilling bool lastMessageTs uint64 privateChatBackfillInvitePuppet func() - messages chan PortalMessage + messages chan PortalMessage + matrixMessages chan PortalMatrixMessage hasRelaybot *bool } @@ -204,9 +232,7 @@ func (portal *Portal) handleMessageLoop() { continue } } - portal.backfillLock.Lock() portal.handleMessage(msg) - portal.backfillLock.Unlock() } } @@ -230,7 +256,7 @@ func (portal *Portal) isRecentlyHandled(id groupme.ID) bool { } func (portal *Portal) isDuplicate(id groupme.ID) bool { - msg := portal.bridge.DB.Message.GetByJID(portal.Key, id.String()) + msg := portal.bridge.DB.Message.GetByGMID(portal.Key, id) if msg != nil { return true } @@ -244,22 +270,17 @@ func init() { func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid id.EventID) { msg := portal.bridge.DB.Message.New() msg.Chat = portal.Key - msg.JID = message.ID.String() + msg.GMID = message.ID msg.MXID = mxid - msg.Timestamp = uint64(message.CreatedAt.ToTime().Unix()) - if message.UserID.String() == source.JID { - msg.Sender = source.JID + msg.Timestamp = message.CreatedAt.ToTime() + if message.UserID == source.GMID { + msg.Sender = source.GMID } else if portal.IsPrivateChat() { - msg.Sender = portal.Key.JID + msg.Sender = portal.Key.GMID } else { - msg.Sender = message.ID.String() - if len(msg.Sender) == 0 { - println("AAAAAAAAAAAAAAAAAAAAAAAAAAIDK") - msg.Sender = message.SenderID.String() - } + msg.Sender = message.SenderID } - msg.Content = &groupmeExt.Message{Message: *message} - msg.Insert() + // msg.Insert() portal.recentlyHandledLock.Lock() portal.recentlyHandled[0] = "" //FIFO queue being implemented here //TODO: is this efficent @@ -270,20 +291,20 @@ func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid i func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appservice.IntentAPI { if portal.IsPrivateChat() { - if info.UserID.String() == user.GetJID() { //from me - return portal.bridge.GetPuppetByJID(user.JID).DefaultIntent() + if info.UserID == user.GetGMID() { //from me + return portal.bridge.GetPuppetByGMID(user.GMID).DefaultIntent() } return portal.MainIntent() } else if len(info.UserID.String()) == 0 { println("TODO weird uid stuff") - } else if info.UserID.String() == user.GetJID() { //from me - return portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal) + } else if info.UserID == user.GetGMID() { //from me + return portal.bridge.GetPuppetByGMID(user.GMID).IntentFor(portal) } - return portal.bridge.GetPuppetByJID(info.UserID.String()).IntentFor(portal) + return portal.bridge.GetPuppetByGMID(info.UserID).IntentFor(portal) } -func (portal *Portal) getReactionIntent(jid types.GroupMeID) *appservice.IntentAPI { - return portal.bridge.GetPuppetByJID(jid).IntentFor(portal) +func (portal *Portal) getReactionIntent(jid groupme.ID) *appservice.IntentAPI { + return portal.bridge.GetPuppetByGMID(jid).IntentFor(portal) } func (portal *Portal) startHandling(source *User, info *groupme.Message) *appservice.IntentAPI { @@ -322,13 +343,13 @@ func (portal *Portal) SyncParticipants(metadata *groupme.Group) { levels = portal.GetBasePowerLevels() changed = true } - participantMap := make(map[string]bool) + participantMap := make(map[groupme.ID]bool) for _, participant := range metadata.Members { - participantMap[participant.UserID.String()] = true - user := portal.bridge.GetUserByJID(participant.UserID.String()) + participantMap[participant.UserID] = true + user := portal.bridge.GetUserByGMID(participant.UserID) portal.userMXIDAction(user, portal.ensureMXIDInvited) - puppet := portal.bridge.GetPuppetByJID(participant.UserID.String()) + puppet := portal.bridge.GetPuppetByGMID(participant.UserID) err := puppet.IntentFor(portal).EnsureJoined(portal.MXID) if err != nil { portal.log.Warnfln("Failed to make puppet of %s join %s: %v", participant.ID.String(), portal.MXID, err) @@ -440,11 +461,11 @@ func (portal *Portal) UpdateAvatar(user *User, avatar string, updateInfo bool) b return true } -func (portal *Portal) UpdateName(name string, setBy types.GroupMeID, updateInfo bool) bool { +func (portal *Portal) UpdateName(name string, setBy groupme.ID, updateInfo bool) bool { if portal.Name != name { intent := portal.MainIntent() if len(setBy) > 0 { - intent = portal.bridge.GetPuppetByJID(setBy).IntentFor(portal) + intent = portal.bridge.GetPuppetByGMID(setBy).IntentFor(portal) } _, err := intent.SetRoomName(portal.MXID, name) if err == nil { @@ -459,11 +480,11 @@ func (portal *Portal) UpdateName(name string, setBy types.GroupMeID, updateInfo return false } -func (portal *Portal) UpdateTopic(topic string, setBy types.GroupMeID, updateInfo bool) bool { +func (portal *Portal) UpdateTopic(topic string, setBy groupme.ID, updateInfo bool) bool { if portal.Topic != topic { intent := portal.MainIntent() if len(setBy) > 0 { - intent = portal.bridge.GetPuppetByJID(setBy).IntentFor(portal) + intent = portal.bridge.GetPuppetByGMID(setBy).IntentFor(portal) } _, err := intent.SetRoomTopic(portal.MXID, topic) if err == nil { @@ -482,7 +503,7 @@ func (portal *Portal) UpdateMetadata(user *User) bool { if portal.IsPrivateChat() { return false } - group, err := user.Client.ShowGroup(context.TODO(), groupme.ID(strings.Replace(portal.Key.JID, groupmeExt.NewUserSuffix, "", 1))) + group, err := user.Client.ShowGroup(context.TODO(), groupme.ID(strings.Replace(portal.Key.GMID.String(), groupmeext.NewUserSuffix, "", 1))) if err != nil { portal.log.Errorln(err) return false @@ -512,13 +533,7 @@ func (portal *Portal) userMXIDAction(user *User, fn func(mxid id.UserID)) { return } - if user == portal.bridge.Relaybot { - for _, mxid := range portal.bridge.Config.Bridge.Relaybot.InviteUsers { - fn(mxid) - } - } else { - fn(user.MXID) - } + fn(user.MXID) } func (portal *Portal) ensureMXIDInvited(mxid id.UserID) { @@ -528,23 +543,13 @@ func (portal *Portal) ensureMXIDInvited(mxid id.UserID) { } } -func (portal *Portal) ensureUserInvited(user *User) { - portal.userMXIDAction(user, portal.ensureMXIDInvited) - - customPuppet := portal.bridge.GetPuppetByCustomMXID(user.MXID) - if customPuppet != nil && customPuppet.CustomIntent() != nil { - _ = customPuppet.CustomIntent().EnsureJoined(portal.MXID) - } +func (portal *Portal) ensureUserInvited(user *User) bool { + return user.ensureInvited(portal.MainIntent(), portal.MXID, portal.IsPrivateChat()) } func (portal *Portal) Sync(user *User, group *groupme.Group) { portal.log.Infoln("Syncing portal for", user.MXID) - if user.IsRelaybot { - yes := true - portal.hasRelaybot = &yes - } - sub := user.Conn.SubscribeToGroup if portal.IsPrivateChat() { sub = user.Conn.SubscribeToDM @@ -576,7 +581,7 @@ func (portal *Portal) Sync(user *User, group *groupme.Group) { update = portal.UpdateAvatar(user, group.ImageURL, false) || update if update { - portal.Update() + portal.Update(nil) portal.UpdateBridgeInfo() } } @@ -606,33 +611,6 @@ func (portal *Portal) GetBasePowerLevels() *event.PowerLevelsEventContent { } } -func (portal *Portal) ChangeAdminStatus(jids []string, setAdmin bool) { - levels, err := portal.MainIntent().PowerLevels(portal.MXID) - if err != nil { - levels = portal.GetBasePowerLevels() - } - newLevel := 0 - if setAdmin { - newLevel = 50 - } - changed := false - for _, jid := range jids { - puppet := portal.bridge.GetPuppetByJID(jid) - changed = levels.EnsureUserLevel(puppet.MXID, newLevel) || changed - - user := portal.bridge.GetUserByJID(jid) - if user != nil { - changed = levels.EnsureUserLevel(user.MXID, newLevel) || changed - } - } - if changed { - _, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels) - if err != nil { - portal.log.Errorln("Failed to change power levels:", err) - } - } -} - func (portal *Portal) RestrictMessageSending(restrict bool) { levels, err := portal.MainIntent().PowerLevels(portal.MXID) if err != nil { @@ -676,197 +654,6 @@ func (portal *Portal) RestrictMetadataChanges(restrict bool) { } } -func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error { - if !portal.bridge.Config.Bridge.RecoverHistory { - return nil - } - - endBackfill := portal.beginBackfill() - defer endBackfill() - - lastMessage := portal.bridge.DB.Message.GetLastInChat(portal.Key) - if lastMessage == nil { - return nil - } - if lastMessage.Timestamp >= lastMessageTime { - portal.log.Debugln("Not backfilling: no new messages") - return nil - } - - lastMessageID := lastMessage.JID - lastMessageFromMe := lastMessage.Sender == user.JID - portal.log.Infoln("Backfilling history since", lastMessageID, "for", user.MXID) - for len(lastMessageID) > 0 { - portal.log.Debugln("Fetching 50 messages of history after", lastMessageID) - messages, err := user.Client.LoadMessagesAfter(portal.Key.JID, lastMessageID, lastMessageFromMe, portal.IsPrivateChat()) - if err != nil { - return err - } - // messages, ok := resp.Content.([]interface{}) - if len(messages) == 0 { - portal.log.Debugfln("Didn't get more messages to backfill (resp.Content is %T)", messages) - break - } - - portal.handleHistory(user, messages) - - lastMessageProto := messages[len(messages)-1] - lastMessageID = lastMessageProto.ID.String() - lastMessageFromMe = lastMessageProto.UserID.String() == user.JID - } - portal.log.Infoln("Backfilling finished") - return nil -} - -func (portal *Portal) beginBackfill() func() { - portal.backfillLock.Lock() - portal.backfilling = true - var privateChatPuppetInvited bool - var privateChatPuppet *Puppet - if portal.IsPrivateChat() && portal.bridge.Config.Bridge.InviteOwnPuppetForBackfilling && portal.Key.JID != portal.Key.Receiver { - privateChatPuppet = portal.bridge.GetPuppetByJID(portal.Key.Receiver) - portal.privateChatBackfillInvitePuppet = func() { - if privateChatPuppetInvited { - return - } - privateChatPuppetInvited = true - _, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: privateChatPuppet.MXID}) - _ = privateChatPuppet.DefaultIntent().EnsureJoined(portal.MXID) - } - } - return func() { - portal.backfilling = false - portal.privateChatBackfillInvitePuppet = nil - portal.backfillLock.Unlock() - if privateChatPuppet != nil && privateChatPuppetInvited { - //_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID) - //why this shouldn't really happen NOTE - } - } -} - -func (portal *Portal) disableNotifications(user *User) { - if !portal.bridge.Config.Bridge.HistoryDisableNotifs { - return - } - puppet := portal.bridge.GetPuppetByCustomMXID(user.MXID) - if puppet == nil || puppet.customIntent == nil { - return - } - portal.log.Debugfln("Disabling notifications for %s for backfilling", user.MXID) - ruleID := fmt.Sprintf("net.maunium.silence_while_backfilling.%s", portal.MXID) - err := puppet.customIntent.PutPushRule("global", pushrules.OverrideRule, ruleID, &mautrix.ReqPutPushRule{ - Actions: []pushrules.PushActionType{pushrules.ActionDontNotify}, - Conditions: []pushrules.PushCondition{{ - Kind: pushrules.KindEventMatch, - Key: "room_id", - Pattern: string(portal.MXID), - }}, - }) - if err != nil { - portal.log.Warnfln("Failed to disable notifications for %s while backfilling: %v", user.MXID, err) - } -} - -func (portal *Portal) enableNotifications(user *User) { - if !portal.bridge.Config.Bridge.HistoryDisableNotifs { - return - } - puppet := portal.bridge.GetPuppetByCustomMXID(user.MXID) - if puppet == nil || puppet.customIntent == nil { - return - } - portal.log.Debugfln("Re-enabling notifications for %s after backfilling", user.MXID) - ruleID := fmt.Sprintf("net.maunium.silence_while_backfilling.%s", portal.MXID) - err := puppet.customIntent.DeletePushRule("global", pushrules.OverrideRule, ruleID) - if err != nil { - portal.log.Warnfln("Failed to re-enable notifications for %s after backfilling: %v", user.MXID, err) - } -} - -func (portal *Portal) FillInitialHistory(user *User) error { - if portal.bridge.Config.Bridge.InitialHistoryFill == 0 { - return nil - } - endBackfill := portal.beginBackfill() - defer endBackfill() - if portal.privateChatBackfillInvitePuppet != nil { - portal.privateChatBackfillInvitePuppet() - } - - n := portal.bridge.Config.Bridge.InitialHistoryFill - portal.log.Infoln("Filling initial history, maximum", n, "messages") - var messages []*groupme.Message - before := "" - chunkNum := 1 - for n > 0 { - count := 20 - if n < count { - count = n - } - portal.log.Debugfln("Fetching chunk %d (%d messages / %d cap) before message %s", chunkNum, count, n, before) - chunk, err := user.Client.LoadMessagesBefore(portal.Key.JID, before, portal.IsPrivateChat()) - if err != nil { - return err - } - if len(chunk) == 0 { - portal.log.Infoln("Chunk empty, starting handling of loaded messages") - break - } - - //reverses chunk to ascending order (oldest first) - i := 0 - j := len(chunk) - 1 - for i < j { - chunk[i], chunk[j] = chunk[j], chunk[i] - i++ - j-- - } - - messages = append(chunk, messages...) - - portal.log.Debugfln("Fetched chunk and received %d messages", len(chunk)) - - n -= len(chunk) - before = chunk[0].ID.String() - if len(before) == 0 { - portal.log.Infoln("No message ID for first message, starting handling of loaded messages") - break - } - } - portal.disableNotifications(user) - portal.handleHistory(user, messages) - portal.enableNotifications(user) - portal.log.Infoln("Initial history fill complete") - return nil -} - -func (portal *Portal) handleHistory(user *User, messages []*groupme.Message) { - portal.log.Infoln("Handling", len(messages), "messages of history") - for _, message := range messages { - // data, ok := rawMessage.(*groupme.Message) - // if !ok { - // portal.log.Warnln("Unexpected non-WebMessageInfo item in history response:", rawMessage) - // continue - // } - // data := whatsapp.ParseProtoMessage(message) - // if data == nil || data == whatsapp.ErrMessageTypeNotImplemented { - // st := message.GetMessageStubType() - // // Ignore some types that are known to fail - // if st == waProto.WebMessageInfo_CALL_MISSED_VOICE || st == waProto.WebMessageInfo_CALL_MISSED_VIDEO || - // st == waProto.WebMessageInfo_CALL_MISSED_GROUP_VOICE || st == waProto.WebMessageInfo_CALL_MISSED_GROUP_VIDEO { - // continue - // } - // portal.log.Warnln("Message", message.GetKey().GetId(), "failed to parse during backfilling") - // continue - // } - if portal.privateChatBackfillInvitePuppet != nil && message.UserID.String() == user.JID && portal.IsPrivateChat() { - portal.privateChatBackfillInvitePuppet() - } - portal.handleMessage(PortalMessage{portal.Key, user, message, uint64(message.CreatedAt.ToTime().Unix())}) - } -} - type BridgeInfoSection struct { ID string `json:"id"` DisplayName string `json:"displayname,omitempty"` @@ -898,12 +685,12 @@ func (portal *Portal) getBridgeInfo() (string, BridgeInfoContent) { ExternalURL: "https://www.whatsapp.com/", }, Channel: BridgeInfoSection{ - ID: portal.Key.JID, + ID: portal.Key.GMID.String(), DisplayName: portal.Name, AvatarURL: portal.AvatarURL.CUString(), }, } - bridgeInfoStateKey := fmt.Sprintf("net.maunium.whatsapp://whatsapp/%s", portal.Key.JID) + bridgeInfoStateKey := fmt.Sprintf("net.maunium.whatsapp://whatsapp/%s", portal.Key.GMID) return bridgeInfoStateKey, bridgeInfo } @@ -924,6 +711,15 @@ func (portal *Portal) UpdateBridgeInfo() { } } +func (portal *Portal) GetEncryptionEventContent() (evt *event.EncryptionEventContent) { + evt = &event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1} + if rot := portal.bridge.Config.Bridge.Encryption.Rotation; rot.EnableCustom { + evt.RotationPeriodMillis = rot.Milliseconds + evt.RotationPeriodMessages = rot.Messages + } + return +} + func (portal *Portal) CreateMatrixRoom(user *User) error { portal.roomCreateLock.Lock() defer portal.roomCreateLock.Unlock() @@ -940,33 +736,21 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { var metadata *groupme.Group if portal.IsPrivateChat() { - portal.log.Debugln("isPrivateChat") - puppet := portal.bridge.GetPuppetByJID(portal.Key.JID) - meta, err := portal.bridge.StateStore.TryGetMemberRaw("", puppet.MXID) - if err { - println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") - return errors.New("Cannot find user information") - } - - //m, _ := portal.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID) - if portal.bridge.Config.Bridge.PrivateChatPortalMeta { - portal.Name = meta.DisplayName - portal.AvatarURL = id.MustParseContentURI(meta.AvatarURL) - portal.Avatar = meta.Avatar + puppet := portal.bridge.GetPuppetByGMID(portal.Key.GMID) + if portal.bridge.Config.Bridge.PrivateChatPortalMeta || portal.bridge.Config.Bridge.Encryption.Default { + portal.Name = puppet.Displayname + portal.AvatarURL = puppet.AvatarURL + portal.Avatar = puppet.Avatar } else { portal.Name = "" } portal.Topic = "GroupMe private chat" - // } else if portal.IsStatusBroadcastRoom() { - // portal.Name = "WhatsApp Status Broadcast" - // portal.Topic = "WhatsApp status updates from your contacts" } else { - portal.log.Debugln("else: it's not a private chat") var err error - metadata, err = user.Client.ShowGroup(context.TODO(), groupme.ID(portal.Key.JID)) + metadata, err = user.Client.ShowGroup(context.TODO(), groupme.ID(portal.Key.GMID)) if err == nil { portal.Name = metadata.Name - // portal.Topic = metadata.Topic + portal.Topic = metadata.Description } portal.UpdateAvatar(user, metadata.ImageURL, false) } @@ -998,9 +782,6 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { } invite := []id.UserID{user.MXID} - if user.IsRelaybot { - invite = portal.bridge.Config.Bridge.Relaybot.InviteUsers - } if portal.bridge.Config.Bridge.Encryption.Default { initialState = append(initialState, &event.Event{ @@ -1030,7 +811,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { return errors.New("Empty room ID") } portal.MXID = resp.RoomID - portal.Update() + portal.Update(nil) portal.bridge.portalsLock.Lock() portal.bridge.portalsByMXID[portal.MXID] = portal portal.bridge.portalsLock.Unlock() @@ -1051,10 +832,8 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { _ = customPuppet.CustomIntent().EnsureJoined(portal.MXID) } } - user.addPortalToCommunity(portal) if portal.IsPrivateChat() { - puppet := user.bridge.GetPuppetByJID(portal.Key.JID) - user.addPuppetToCommunity(puppet) + puppet := user.bridge.GetPuppetByGMID(portal.Key.GMID) if portal.bridge.Config.Bridge.Encryption.Default { err = portal.bridge.Bot.EnsureJoined(portal.MXID) @@ -1065,10 +844,6 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { user.UpdateDirectChats(map[id.UserID][]id.RoomID{puppet.MXID: {portal.MXID}}) } - err = portal.FillInitialHistory(user) - if err != nil { - portal.log.Errorln("Failed to fill history:", err) - } return nil } @@ -1076,32 +851,22 @@ func (portal *Portal) IsPrivateChat() bool { return portal.Key.IsPrivate() } -func (portal *Portal) HasRelaybot() bool { - if portal.bridge.Relaybot == nil { - return false - } else if portal.hasRelaybot == nil { - val := portal.bridge.Relaybot.IsInPortal(portal.Key) - portal.hasRelaybot = &val - } - return *portal.hasRelaybot -} - func (portal *Portal) IsStatusBroadcastRoom() bool { - return portal.Key.JID == "status@broadcast" + return portal.Key.GMID == "status@broadcast" } func (portal *Portal) MainIntent() *appservice.IntentAPI { if portal.IsPrivateChat() { - return portal.bridge.GetPuppetByJID(portal.Key.JID).DefaultIntent() + return portal.bridge.GetPuppetByGMID(portal.Key.GMID).DefaultIntent() } return portal.bridge.Bot } -func (portal *Portal) SetReply(content *event.MessageEventContent, msgID types.GroupMeID) { +func (portal *Portal) SetReply(content *event.MessageEventContent, msgID groupme.ID) { if len(msgID) == 0 { return } - message := portal.bridge.DB.Message.GetByJID(portal.Key, msgID) + message := portal.bridge.DB.Message.GetByGMID(portal.Key, msgID) if message != nil { evt, err := portal.MainIntent().GetEvent(portal.MXID, message.MXID) if err != nil { @@ -1123,76 +888,18 @@ func (portal *Portal) SetReply(content *event.MessageEventContent, msgID types.G return } -func (portal *Portal) HandleMessageRevoke(user *User, message whatsappExt.MessageRevocation) { - msg := portal.bridge.DB.Message.GetByJID(portal.Key, message.Id) - if msg == nil { - return - } - var intent *appservice.IntentAPI - if message.FromMe { - if portal.IsPrivateChat() { - intent = portal.bridge.GetPuppetByJID(user.JID).CustomIntent() - } else { - intent = portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal) - } - } else if len(message.Participant) > 0 { - intent = portal.bridge.GetPuppetByJID(message.Participant).IntentFor(portal) - } - if intent == nil { - intent = portal.MainIntent() - } - _, err := intent.RedactEvent(portal.MXID, msg.MXID) - if err != nil { - portal.log.Errorln("Failed to redact %s: %v", msg.JID, err) - return - } - msg.Delete() -} - -//func (portal *Portal) HandleFakeMessage(_ *User, message FakeMessage) { -// if portal.isRecentlyHandled(message.ID) { -// return -// } -// -// content := event.MessageEventContent{ -// MsgType: event.MsgNotice, -// Body: message.Text, -// } -// if message.Alert { -// content.MsgType = event.MsgText -// } -// _, err := portal.sendMainIntentMessage(content) -// if err != nil { -// portal.log.Errorfln("Failed to handle fake message %s: %v", message.ID, err) -// return -// } -// -// portal.recentlyHandledLock.Lock() -// index := portal.recentlyHandledIndex -// portal.recentlyHandledIndex = (portal.recentlyHandledIndex + 1) % recentlyHandledLength -// portal.recentlyHandledLock.Unlock() -// portal.recentlyHandled[index] = message.ID -//} - -func (portal *Portal) sendMainIntentMessage(content interface{}) (*mautrix.RespSendEvent, error) { - return portal.sendMessage(portal.MainIntent(), event.EventMessage, content, 0) -} - const MessageSendRetries = 5 const MediaUploadRetries = 5 const BadGatewaySleep = 5 * time.Second -func (portal *Portal) sendMessage(intent *appservice.IntentAPI, eventType event.Type, content interface{}, timestamp int64) (*mautrix.RespSendEvent, error) { - return portal.sendMessageWithRetry(intent, eventType, content, timestamp, MessageSendRetries) -} func (portal *Portal) sendReaction(intent *appservice.IntentAPI, eventID id.EventID, reaction string) (*mautrix.RespSendEvent, error) { - return portal.sendMessage(intent, event.EventReaction, &event.ReactionEventContent{ - RelatesTo: event.RelatesTo{ - EventID: eventID, - Type: event.RelAnnotation, - Key: reaction, - }, - }, time.Now().Unix()) + var content event.ReactionEventContent + content.RelatesTo = event.RelatesTo{ + Type: event.RelAnnotation, + EventID: eventID, + Key: reaction, + } + return intent.SendMassagedMessageEvent(portal.MXID, event.EventReaction, &content, time.Now().UnixMilli()) } func isGatewayError(err error) bool { @@ -1203,38 +910,34 @@ func isGatewayError(err error) bool { return errors.As(err, &httpErr) && (httpErr.IsStatus(http.StatusBadGateway) || httpErr.IsStatus(http.StatusGatewayTimeout)) } -func (portal *Portal) sendMessageWithRetry(intent *appservice.IntentAPI, eventType event.Type, content interface{}, timestamp int64, retries int) (*mautrix.RespSendEvent, error) { - for ; ; retries-- { - resp, err := portal.sendMessageDirect(intent, eventType, content, timestamp) - if retries > 0 && isGatewayError(err) { - portal.log.Warnfln("Got gateway error trying to send message, retrying in %d seconds", int(BadGatewaySleep.Seconds())) - time.Sleep(BadGatewaySleep) - - } else { - return resp, err - } +func (portal *Portal) encrypt(intent *appservice.IntentAPI, content *event.Content, eventType event.Type) (event.Type, error) { + if !portal.Encrypted || portal.bridge.Crypto == nil { + return eventType, nil } + intent.AddDoublePuppetValue(content) + // TODO maybe the locking should be inside mautrix-go? + portal.encryptLock.Lock() + defer portal.encryptLock.Unlock() + err := portal.bridge.Crypto.Encrypt(portal.MXID, eventType, content) + if err != nil { + return eventType, fmt.Errorf("failed to encrypt event: %w", err) + } + return event.EventEncrypted, nil } -func (portal *Portal) sendMessageDirect(intent *appservice.IntentAPI, eventType event.Type, content interface{}, timestamp int64) (*mautrix.RespSendEvent, error) { - wrappedContent := event.Content{Parsed: content} - if timestamp != 0 && intent.IsCustomPuppet { - wrappedContent.Raw = map[string]interface{}{ - "net.maunium.whatsapp.puppet": intent.IsCustomPuppet, - } - } - if portal.Encrypted && portal.bridge.Crypto != nil { - encrypted, err := portal.bridge.Crypto.Encrypt(portal.MXID, eventType, wrappedContent) - if err != nil { - return nil, fmt.Errorf("failed to encrypt event: %w", err) - } - eventType = event.EventEncrypted - wrappedContent.Parsed = encrypted +func (portal *Portal) sendMessage(intent *appservice.IntentAPI, eventType event.Type, content *event.MessageEventContent, extraContent map[string]any, timestamp int64) (*mautrix.RespSendEvent, error) { + wrappedContent := event.Content{Parsed: content, Raw: extraContent} + var err error + eventType, err = portal.encrypt(intent, &wrappedContent, eventType) + if err != nil { + return nil, err } + + _, _ = intent.UserTyping(portal.MXID, false, 0) if timestamp == 0 { return intent.SendMessageEvent(portal.MXID, eventType, &wrappedContent) } else { - return intent.SendMassagedMessageEvent(portal.MXID, eventType, &wrappedContent, timestamp*1000) //milliseconds + return intent.SendMassagedMessageEvent(portal.MXID, eventType, &wrappedContent, timestamp) } } @@ -1242,7 +945,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment sendText = true switch attachment.Type { case "image": - imgData, mime, err := groupmeExt.DownloadImage(attachment.URL) + imgData, mime, err := groupmeext.DownloadImage(attachment.URL) if err != nil { return nil, true, fmt.Errorf("failed to load media info: %w", err) } @@ -1272,7 +975,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment fname1, fname2 = urlParts[1], urlParts[0] } else if len(urlParts) > 2 { fname1, fname2 = urlParts[2], urlParts[1] - } //TODO abstract groupme url parsing in groupmeExt + } //TODO abstract groupme url parsing in groupmeext fname := fmt.Sprintf("%s.%s", fname1, fname2) content := &event.MessageEventContent{ @@ -1296,7 +999,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment return content, true, nil case "video": - vidContents, mime := groupmeExt.DownloadVideo(attachment.VideoPreviewURL, attachment.URL, source.Token) + vidContents, mime := groupmeext.DownloadVideo(attachment.VideoPreviewURL, attachment.URL, source.Token) if mime == "" { mime = mimetype.Detect(vidContents).String() } @@ -1336,7 +1039,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment message.Text = strings.Replace(message.Text, attachment.URL, "", 1) return content, true, nil case "file": - fileData, fname, fmime := groupmeExt.DownloadFile(portal.Key.JID, attachment.FileID, source.Token) + fileData, fname, fmime := groupmeext.DownloadFile(portal.Key.GMID, attachment.FileID, source.Token) if fmime == "" { fmime = mimetype.Detect(fileData).String() } @@ -1407,7 +1110,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment Body: message.Text, MsgType: event.MsgText, } - portal.SetReply(content, attachment.ReplyID.String()) + portal.SetReply(content, attachment.ReplyID) return content, false, nil default: @@ -1579,7 +1282,7 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message) if msg == nil { continue } - resp, err := portal.sendMessage(intent, event.EventMessage, msg, message.CreatedAt.ToTime().Unix()) + resp, err := portal.sendMessage(intent, event.EventMessage, msg, nil, message.CreatedAt.ToTime().Unix()) if err != nil { portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err) portal.sendMediaBridgeFailure(source, intent, *message, err) @@ -1600,7 +1303,7 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message) _, _ = intent.UserTyping(portal.MXID, false, 0) if sendText { - resp, err := portal.sendMessage(intent, event.EventMessage, content, message.CreatedAt.ToTime().Unix()) + resp, err := portal.sendMessage(intent, event.EventMessage, content, nil, message.CreatedAt.ToTime().Unix()) if err != nil { portal.log.Errorfln("Failed to handle message %s: %v", message.ID, err) return @@ -1611,88 +1314,88 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message) portal.finishHandling(source, message, sentID) } -func (portal *Portal) handleReaction(msgID types.GroupMeID, ppl []types.GroupMeID) { - reactions := portal.bridge.DB.Reaction.GetByJID(msgID) - newLikes := newReactions(reactions, ppl) - removeLikes := oldReactions(reactions, ppl) +// func (portal *Portal) handleReaction(msgID groupme.ID, ppl []groupme.ID) { +// reactions := portal.bridge.DB.Reaction.GetByGMID(msgID) +// newLikes := newReactions(reactions, ppl) +// removeLikes := oldReactions(reactions, ppl) - var eventID id.EventID - if len(newLikes) > 0 { - message := portal.bridge.DB.Message.GetByJID(portal.Key, msgID) - if message == nil { - portal.log.Errorln("Received reaction for unknown message", msgID) - return - } - eventID = message.MXID - } +// var eventID id.EventID +// if len(newLikes) > 0 { +// message := portal.bridge.DB.Message.GetByGMID(portal.Key, msgID) +// if message == nil { +// portal.log.Errorln("Received reaction for unknown message", msgID) +// return +// } +// eventID = message.MXID +// } - for _, jid := range newLikes { - intent := portal.getReactionIntent(jid) - resp, err := portal.sendReaction(intent, eventID, "❤") - if err != nil { - portal.log.Errorln("Something wrong with sending reaction", msgID, jid, err) - continue - } +// for _, jid := range newLikes { +// intent := portal.getReactionIntent(jid) +// resp, err := portal.sendReaction(intent, eventID, "❤") +// if err != nil { +// portal.log.Errorln("Something wrong with sending reaction", msgID, jid, err) +// continue +// } - newReaction := portal.bridge.DB.Reaction.New() - newReaction.MXID = resp.EventID - newReaction.MessageJID = msgID - newReaction.MessageMXID = eventID - newReaction.PuppetJID = jid +// newReaction := portal.bridge.DB.Reaction.New() +// newReaction.MXID = resp.EventID +// newReaction.MessageJID = msgID +// newReaction.MessageMXID = eventID +// newReaction.PuppetJID = jid - newReaction.Insert() +// newReaction.Insert() - } +// } - for _, reaction := range removeLikes { - if len(reaction.Puppet.JID) == 0 { - portal.log.Warnln("Reaction user state wrong", reaction.MXID, msgID) - continue - } - intent := portal.getReactionIntent(reaction.PuppetJID) - _, err := intent.RedactEvent(portal.MXID, reaction.MXID) - if err != nil { - portal.log.Errorln("Something wrong with reaction redaction", reaction.MXID) - continue - } - reaction.Delete() +// for _, reaction := range removeLikes { +// if len(reaction.Puppet.JID) == 0 { +// portal.log.Warnln("Reaction user state wrong", reaction.MXID, msgID) +// continue +// } +// intent := portal.getReactionIntent(reaction.PuppetJID) +// _, err := intent.RedactEvent(portal.MXID, reaction.MXID) +// if err != nil { +// portal.log.Errorln("Something wrong with reaction redaction", reaction.MXID) +// continue +// } +// reaction.Delete() - } -} +// } +// } -func oldReactions(a []*database.Reaction, b []string) (ans []*database.Reaction) { - for _, i := range a { - flag := false - for _, j := range b { - if i.PuppetJID == j { - flag = true - break - } - } - if !flag { - ans = append(ans, i) - } - } +// func oldReactions(a []*database.Reaction, b []string) (ans []*database.Reaction) { +// for _, i := range a { +// flag := false +// for _, j := range b { +// if i.PuppetJID == j { +// flag = true +// break +// } +// } +// if !flag { +// ans = append(ans, i) +// } +// } - return -} +// return +// } -func newReactions(a []*database.Reaction, b []string) (ans []string) { - for _, j := range b { - flag := false - for _, i := range a { - if i.PuppetJID == j { - flag = true - break - } - } - if !flag { - ans = append(ans, j) - } - } +// func newReactions(a []*database.Reaction, b []string) (ans []string) { +// for _, j := range b { +// flag := false +// for _, i := range a { +// if i.GMID == j { +// flag = true +// break +// } +// } +// if !flag { +// ans = append(ans, j) +// } +// } - return -} +// return +// } func (portal *Portal) HandleLocationMessage(source *User, message whatsapp.LocationMessage) { // intent := portal.startHandling(source, message.Info) @@ -1801,7 +1504,7 @@ func (portal *Portal) sendMediaBridgeFailure(source *User, intent *appservice.In resp, err := portal.sendMessage(intent, event.EventMessage, &event.MessageEventContent{ MsgType: event.MsgNotice, Body: "Failed to bridge media", - }, int64(message.CreatedAt.ToTime().Unix()*1000)) + }, nil, int64(message.CreatedAt.ToTime().Unix()*1000)) if err != nil { portal.log.Errorfln("Failed to send media download error message for %s: %v", message.UserID.String(), err) } else { @@ -1850,274 +1553,6 @@ func (portal *Portal) removeUser(isSameUser bool, kicker *appservice.IntentAPI, } } -func (portal *Portal) HandleWhatsAppKick(senderJID string, jids []string) { - sender := portal.bridge.GetPuppetByJID(senderJID) - senderIntent := sender.IntentFor(portal) - for _, jid := range jids { - puppet := portal.bridge.GetPuppetByJID(jid) - portal.removeUser(puppet.JID == sender.JID, senderIntent, puppet.MXID, puppet.DefaultIntent()) - - user := portal.bridge.GetUserByJID(jid) - if user != nil { - var customIntent *appservice.IntentAPI - if puppet.CustomMXID == user.MXID { - customIntent = puppet.CustomIntent() - } - portal.removeUser(puppet.JID == sender.JID, senderIntent, user.MXID, customIntent) - } - } -} - -func (portal *Portal) HandleWhatsAppInvite(senderJID string, jids []string) { - senderIntent := portal.MainIntent() - if senderJID != "unknown" { - sender := portal.bridge.GetPuppetByJID(senderJID) - senderIntent = sender.IntentFor(portal) - } - for _, jid := range jids { - puppet := portal.bridge.GetPuppetByJID(jid) - _, err := senderIntent.InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: puppet.MXID}) - if err != nil { - portal.log.Warnfln("Failed to invite %s as %s: %v", puppet.MXID, senderIntent.UserID, err) - } - err = puppet.DefaultIntent().EnsureJoined(portal.MXID) - if err != nil { - portal.log.Errorfln("Failed to ensure %s is joined: %v", puppet.MXID, err) - } - } -} - -type base struct { - download func() ([]byte, error) - info whatsapp.MessageInfo - context whatsapp.ContextInfo - mimeType string -} - -type mediaMessage struct { - base - - thumbnail []byte - caption string - fileName string - length uint32 - sendAsSticker bool -} - -func (portal *Portal) uploadWithRetry(intent *appservice.IntentAPI, data []byte, mimeType string, retries int) (*mautrix.RespMediaUpload, error) { - for ; ; retries-- { - uploaded, err := intent.UploadBytes(data, mimeType) - if isGatewayError(err) { - portal.log.Warnfln("Got gateway error trying to upload media, retrying in %d seconds", int(BadGatewaySleep.Seconds())) - time.Sleep(BadGatewaySleep) - } else { - return uploaded, err - } - } -} - -func makeMessageID() *string { - b := make([]byte, 10) - rand.Read(b) - str := strings.ToUpper(hex.EncodeToString(b)) - return &str -} - -func (portal *Portal) downloadThumbnail(content *event.MessageEventContent, id id.EventID) []byte { - if len(content.GetInfo().ThumbnailURL) == 0 { - return nil - } - mxc, err := content.GetInfo().ThumbnailURL.Parse() - if err != nil { - portal.log.Errorln("Malformed thumbnail URL in %s: %v", id, err) - } - thumbnail, err := portal.MainIntent().DownloadBytes(mxc) - if err != nil { - portal.log.Errorln("Failed to download thumbnail in %s: %v", id, err) - return nil - } - thumbnailType := http.DetectContentType(thumbnail) - var img image.Image - switch thumbnailType { - case "image/png": - img, err = png.Decode(bytes.NewReader(thumbnail)) - case "image/gif": - img, err = gif.Decode(bytes.NewReader(thumbnail)) - case "image/jpeg": - return thumbnail - default: - return nil - } - var buf bytes.Buffer - err = jpeg.Encode(&buf, img, &jpeg.Options{ - Quality: jpeg.DefaultQuality, - }) - if err != nil { - portal.log.Errorln("Failed to re-encode thumbnail in %s: %v", id, err) - return nil - } - return buf.Bytes() -} - -func (portal *Portal) convertGifToVideo(gif []byte) ([]byte, error) { - dir, err := ioutil.TempDir("", "gif-convert-*") - if err != nil { - return nil, fmt.Errorf("failed to make temp dir: %w", err) - } - defer os.RemoveAll(dir) - - inputFile, err := os.OpenFile(filepath.Join(dir, "input.gif"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) - if err != nil { - return nil, fmt.Errorf("failed open input file: %w", err) - } - _, err = inputFile.Write(gif) - if err != nil { - _ = inputFile.Close() - return nil, fmt.Errorf("failed to write gif to input file: %w", err) - } - _ = inputFile.Close() - - outputFileName := filepath.Join(dir, "output.mp4") - cmd := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "warning", - "-f", "gif", "-i", inputFile.Name(), - "-pix_fmt", "yuv420p", "-c:v", "libx264", "-movflags", "+faststart", - "-filter:v", "crop='floor(in_w/2)*2:floor(in_h/2)*2'", - outputFileName) - vcLog := portal.log.Sub("VideoConverter").Writer(log.LevelWarn) - cmd.Stdout = vcLog - cmd.Stderr = vcLog - - err = cmd.Run() - if err != nil { - return nil, fmt.Errorf("failed to run ffmpeg: %w", err) - } - outputFile, err := os.OpenFile(filepath.Join(dir, "output.mp4"), os.O_RDONLY, 0) - if err != nil { - return nil, fmt.Errorf("failed to open output file: %w", err) - } - defer func() { - _ = outputFile.Close() - _ = os.Remove(outputFile.Name()) - }() - mp4, err := ioutil.ReadAll(outputFile) - if err != nil { - return nil, fmt.Errorf("failed to read mp4 from output file: %w", err) - } - return mp4, nil -} - -func (portal *Portal) preprocessMatrixMedia(sender *User, relaybotFormatted bool, content *event.MessageEventContent, eventID id.EventID, mediaType whatsapp.MediaType) *MediaUpload { - // var caption string - // var mentionedJIDs []types.GroupMeID - // if relaybotFormatted { - // caption, mentionedJIDs = portal.bridge.Formatter.ParseMatrix(content.FormattedBody) - // } - - // var file *event.EncryptedFileInfo - // rawMXC := content.URL - // if content.File != nil { - // file = content.File - // rawMXC = file.URL - // } - // mxc, err := rawMXC.Parse() - // if err != nil { - // portal.log.Errorln("Malformed content URL in %s: %v", eventID, err) - // return nil - // } - // data, err := portal.MainIntent().DownloadBytes(mxc) - // if err != nil { - // portal.log.Errorfln("Failed to download media in %s: %v", eventID, err) - // return nil - // } - // if file != nil { - // data, err = file.Decrypt(data) - // if err != nil { - // portal.log.Errorfln("Failed to decrypt media in %s: %v", eventID, err) - // return nil - // } - // } - // if mediaType == whatsapp.MediaVideo && content.GetInfo().MimeType == "image/gif" { - // data, err = portal.convertGifToVideo(data) - // if err != nil { - // portal.log.Errorfln("Failed to convert gif to mp4 in %s: %v", eventID, err) - // return nil - // } - // content.Info.MimeType = "video/mp4" - // } - - // url, mediaKey, fileEncSHA256, fileSHA256, fileLength, err := sender.Conn.Upload(bytes.NewReader(data), mediaType) - // if err != nil { - // portal.log.Errorfln("Failed to upload media in %s: %v", eventID, err) - // return nil - // } - - // return &MediaUpload{ - // Caption: caption, - // MentionedJIDs: mentionedJIDs, - // URL: url, - // MediaKey: mediaKey, - // FileEncSHA256: fileEncSHA256, - // FileSHA256: fileSHA256, - // FileLength: fileLength, - // Thumbnail: portal.downloadThumbnail(content, eventID), - // } - return nil -} - -type MediaUpload struct { - Caption string - MentionedJIDs []types.GroupMeID - URL string - MediaKey []byte - FileEncSHA256 []byte - FileSHA256 []byte - FileLength uint64 - Thumbnail []byte -} - -func (portal *Portal) sendMatrixConnectionError(sender *User, eventID id.EventID) bool { - if !sender.HasSession() { - portal.log.Debugln("Ignoring event", eventID, "from", sender.MXID, "as user has no session") - return true - } else if !sender.IsConnected() { - portal.log.Debugln("Ignoring event", eventID, "from", sender.MXID, "as user is not connected") - inRoom := "" - if portal.IsPrivateChat() { - inRoom = " in your management room" - } - reconnect := fmt.Sprintf("Use `%s reconnect`%s to reconnect.", portal.bridge.Config.Bridge.CommandPrefix, inRoom) - if sender.IsLoginInProgress() { - reconnect = "You have a login attempt in progress, please wait." - } - msg := format.RenderMarkdown("\u26a0 You are not connected to WhatsApp, so your message was not bridged. "+reconnect, true, false) - msg.MsgType = event.MsgNotice - _, err := portal.sendMainIntentMessage(msg) - if err != nil { - portal.log.Errorln("Failed to send bridging failure message:", err) - } - return true - } - return false -} - -func (portal *Portal) addRelaybotFormat(sender *User, content *event.MessageEventContent) bool { - member := portal.MainIntent().Member(portal.MXID, sender.MXID) - if len(member.Displayname) == 0 { - member.Displayname = string(sender.MXID) - } - - if content.Format != event.FormatHTML { - content.FormattedBody = strings.Replace(html.EscapeString(content.Body), "\n", "
", -1) - content.Format = event.FormatHTML - } - data, err := portal.bridge.Config.Bridge.Relaybot.FormatMessage(content, sender.MXID, member) - if err != nil { - portal.log.Errorln("Failed to apply relaybot format:", err) - } - content.FormattedBody = data - return true -} - func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) ([]*groupme.Message, *User) { content, ok := evt.Content.Parsed.(*event.MessageEventContent) if !ok { @@ -2143,7 +1578,7 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) ([]*g GroupID: groupme.ID(portal.Key.String()), ConversationID: groupme.ID(portal.Key.String()), ChatID: groupme.ID(portal.Key.String()), - RecipientID: groupme.ID(portal.Key.JID), + RecipientID: groupme.ID(portal.Key.GMID), } replyToID := content.GetReplyTo() if len(replyToID) > 0 { @@ -2540,7 +1975,7 @@ func (portal *Portal) HandleMatrixLeave(sender *User) { return } else { // TODO should we somehow deduplicate this call if this leave was sent by the bridge? - err := sender.Client.RemoveFromGroup(sender.JID, portal.Key.JID) + err := sender.Client.RemoveFromGroup(sender.JID, portal.Key.GMID) if err != nil { portal.log.Errorfln("Failed to leave group as %s: %v", sender.MXID, err) return diff --git a/provisioning.go b/provisioning.go index 13d4f4f..e1d129c 100644 --- a/provisioning.go +++ b/provisioning.go @@ -28,7 +28,7 @@ import ( ) type ProvisioningAPI struct { - bridge *Bridge + bridge *GMBridge log log.Logger } diff --git a/puppet.go b/puppet.go index 3cf1175..0f29dd2 100644 --- a/puppet.go +++ b/puppet.go @@ -26,17 +26,16 @@ import ( log "maunium.net/go/maulogger/v2" "maunium.net/go/mautrix/appservice" + "maunium.net/go/mautrix/bridge" "maunium.net/go/mautrix/id" "github.com/beeper/groupme/database" - "github.com/beeper/groupme/groupmeExt" - "github.com/beeper/groupme/types" - whatsappExt "github.com/beeper/groupme/whatsapp-ext" + "github.com/beeper/groupme/groupmeext" ) var userIDRegex *regexp.Regexp -func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.GroupMeID, bool) { +func (bridge *GMBridge) ParsePuppetMXID(mxid id.UserID) (groupme.ID, bool) { if userIDRegex == nil { userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$", bridge.Config.Bridge.FormatUsername("([0-9]+)"), @@ -47,32 +46,31 @@ func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.GroupMeID, bool) { return "", false } - jid := types.GroupMeID(match[1]) - return jid, true + return groupme.ID(match[1]), true } -func (bridge *Bridge) GetPuppetByMXID(mxid id.UserID) *Puppet { - jid, ok := bridge.ParsePuppetMXID(mxid) +func (bridge *GMBridge) GetPuppetByMXID(mxid id.UserID) *Puppet { + gmid, ok := bridge.ParsePuppetMXID(mxid) if !ok { return nil } - return bridge.GetPuppetByJID(jid) + return bridge.GetPuppetByGMID(gmid) } -func (bridge *Bridge) GetPuppetByJID(jid types.GroupMeID) *Puppet { +func (bridge *GMBridge) GetPuppetByGMID(gmid groupme.ID) *Puppet { bridge.puppetsLock.Lock() defer bridge.puppetsLock.Unlock() - puppet, ok := bridge.puppets[jid] + puppet, ok := bridge.puppets[gmid] if !ok { - dbPuppet := bridge.DB.Puppet.Get(jid) + dbPuppet := bridge.DB.Puppet.Get(gmid) if dbPuppet == nil { dbPuppet = bridge.DB.Puppet.New() - dbPuppet.JID = jid + dbPuppet.GMID = gmid dbPuppet.Insert() } puppet = bridge.NewPuppet(dbPuppet) - bridge.puppets[puppet.JID] = puppet + bridge.puppets[puppet.GMID] = puppet if len(puppet.CustomMXID) > 0 { bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet } @@ -80,7 +78,7 @@ func (bridge *Bridge) GetPuppetByJID(jid types.GroupMeID) *Puppet { return puppet } -func (bridge *Bridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet { +func (bridge *GMBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet { bridge.puppetsLock.Lock() defer bridge.puppetsLock.Unlock() puppet, ok := bridge.puppetsByCustomMXID[mxid] @@ -90,21 +88,57 @@ func (bridge *Bridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet { return nil } puppet = bridge.NewPuppet(dbPuppet) - bridge.puppets[puppet.JID] = puppet + bridge.puppets[puppet.GMID] = puppet bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet } return puppet } -func (bridge *Bridge) GetAllPuppetsWithCustomMXID() []*Puppet { +func (user *User) GetIDoublePuppet() bridge.DoublePuppet { + p := user.bridge.GetPuppetByCustomMXID(user.MXID) + if p == nil || p.CustomIntent() == nil { + return nil + } + return p +} + +func (user *User) GetIGhost() bridge.Ghost { + if user.GMID.String() == "" { + return nil + } + p := user.bridge.GetPuppetByGMID(user.GMID) + if p == nil { + return nil + } + return p +} + +func (br *GMBridge) IsGhost(id id.UserID) bool { + _, ok := br.ParsePuppetMXID(id) + return ok +} + +func (br *GMBridge) GetIGhost(id id.UserID) bridge.Ghost { + p := br.GetPuppetByMXID(id) + if p == nil { + return nil + } + return p +} + +func (puppet *Puppet) GetMXID() id.UserID { + return puppet.MXID +} + +func (bridge *GMBridge) GetAllPuppetsWithCustomMXID() []*Puppet { return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID()) } -func (bridge *Bridge) GetAllPuppets() []*Puppet { +func (bridge *GMBridge) GetAllPuppets() []*Puppet { return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll()) } -func (bridge *Bridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet { +func (bridge *GMBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet { bridge.puppetsLock.Lock() defer bridge.puppetsLock.Unlock() output := make([]*Puppet, len(dbPuppets)) @@ -112,10 +146,10 @@ func (bridge *Bridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet if dbPuppet == nil { continue } - puppet, ok := bridge.puppets[dbPuppet.JID] + puppet, ok := bridge.puppets[dbPuppet.GMID] if !ok { puppet = bridge.NewPuppet(dbPuppet) - bridge.puppets[dbPuppet.JID] = puppet + bridge.puppets[dbPuppet.GMID] = puppet if len(dbPuppet.CustomMXID) > 0 { bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet } @@ -125,29 +159,27 @@ func (bridge *Bridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet return output } -func (bridge *Bridge) FormatPuppetMXID(jid types.GroupMeID) id.UserID { +func (bridge *GMBridge) FormatPuppetMXID(gmid groupme.ID) id.UserID { return id.NewUserID( bridge.Config.Bridge.FormatUsername( - strings.Replace( - jid, - whatsappExt.NewUserSuffix, "", 1)), + gmid), bridge.Config.Homeserver.Domain) } -func (bridge *Bridge) NewPuppet(dbPuppet *database.Puppet) *Puppet { +func (bridge *GMBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet { return &Puppet{ Puppet: dbPuppet, bridge: bridge, - log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.JID)), + log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.GMID)), - MXID: bridge.FormatPuppetMXID(dbPuppet.JID), + MXID: bridge.FormatPuppetMXID(dbPuppet.GMID), } } type Puppet struct { *database.Puppet - bridge *Bridge + bridge *GMBridge log log.Logger typingIn id.RoomID @@ -162,13 +194,13 @@ type Puppet struct { func (puppet *Puppet) PhoneNumber() string { println("phone num") - return strings.Replace(puppet.JID, whatsappExt.NewUserSuffix, "", 1) + return strings.Replace(puppet.GMID, whatsappExt.NewUserSuffix, "", 1) } func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI { if (!portal.IsPrivateChat() && puppet.customIntent == nil) || (portal.backfilling && portal.bridge.Config.Bridge.InviteOwnPuppetForBackfilling) || - portal.Key.JID == puppet.JID { + portal.Key.GMID == puppet.GMID { return puppet.DefaultIntent() } return puppet.customIntent @@ -211,7 +243,7 @@ func (puppet *Puppet) UpdateAvatar(source *User, portalMXID id.RoomID, avatar st } //TODO check its actually groupme? - image, mime, err := groupmeExt.DownloadImage(avatar + ".large") + image, mime, err := groupmeext.DownloadImage(avatar + ".large") if err != nil { puppet.log.Warnln(err) return false diff --git a/segment.go b/segment.go new file mode 100644 index 0000000..f026ad8 --- /dev/null +++ b/segment.go @@ -0,0 +1,90 @@ +// mautrix-groupme - A Matrix-GroupMe puppeting bridge. +// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + + log "maunium.net/go/maulogger/v2" + "maunium.net/go/mautrix/id" +) + +const SegmentURL = "https://api.segment.io/v1/track" + +type SegmentClient struct { + key string + log log.Logger + client http.Client +} + +var Segment SegmentClient + +func (sc *SegmentClient) trackSync(userID id.UserID, event string, properties map[string]interface{}) error { + var buf bytes.Buffer + err := json.NewEncoder(&buf).Encode(map[string]interface{}{ + "userId": userID, + "event": event, + "properties": properties, + }) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", SegmentURL, &buf) + if err != nil { + return err + } + req.SetBasicAuth(sc.key, "") + resp, err := sc.client.Do(req) + if err != nil { + return err + } + _ = resp.Body.Close() + if resp.StatusCode != 200 { + return fmt.Errorf("unexpected status code %d", resp.StatusCode) + } + return nil +} + +func (sc *SegmentClient) IsEnabled() bool { + return len(sc.key) > 0 +} + +func (sc *SegmentClient) Track(userID id.UserID, event string, properties ...map[string]interface{}) { + if !sc.IsEnabled() { + return + } else if len(properties) > 1 { + panic("Track should be called with at most one property map") + } + + go func() { + props := map[string]interface{}{} + if len(properties) > 0 { + props = properties[0] + } + props["bridge"] = "groupme" + err := sc.trackSync(userID, event, props) + if err != nil { + sc.log.Errorfln("Error tracking %s: %v", event, err) + } else { + sc.log.Debugln("Tracked", event) + } + }() +} diff --git a/types/types.go b/types/types.go deleted file mode 100644 index 5d02085..0000000 --- a/types/types.go +++ /dev/null @@ -1,28 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package types - -// GroupMeID is a WhatsApp JID. -type GroupMeID = string - -// WhatsAppMessageID is the internal ID of a WhatsApp message. -type WhatsAppMessageID = string - -//AuthToken is the authentication token -type AuthToken = string - -type TmpID = GroupMeID diff --git a/user.go b/user.go index e6394b8..e2a79a7 100644 --- a/user.go +++ b/user.go @@ -22,6 +22,7 @@ import ( "fmt" "net/http" "sort" + "strings" "sync" "time" @@ -30,6 +31,9 @@ import ( "github.com/Rhymen/go-whatsapp" "maunium.net/go/mautrix" + "maunium.net/go/mautrix/appservice" + "maunium.net/go/mautrix/bridge" + "maunium.net/go/mautrix/bridge/bridgeconfig" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/format" "maunium.net/go/mautrix/id" @@ -37,31 +41,29 @@ import ( "github.com/karmanyaahm/groupme" "github.com/beeper/groupme/database" - "github.com/beeper/groupme/groupmeExt" - "github.com/beeper/groupme/types" - whatsappExt "github.com/beeper/groupme/whatsapp-ext" + "github.com/beeper/groupme/groupmeext" ) type User struct { *database.User Conn *groupme.PushSubscription - bridge *Bridge + bridge *GMBridge log log.Logger - Admin bool - Whitelisted bool - RelaybotWhitelisted bool + Admin bool + Whitelisted bool + PermissionLevel bridgeconfig.PermissionLevel - IsRelaybot bool + BridgeState *bridge.BridgeStateQueue - Client *groupmeExt.Client + Client *groupmeext.Client ConnectionErrors int CommunityID string - ChatList map[types.GroupMeID]groupme.Chat - GroupList map[types.GroupMeID]groupme.Group - RelationList map[types.GroupMeID]groupme.User + ChatList map[groupme.ID]groupme.Chat + GroupList map[groupme.ID]groupme.Group + RelationList map[groupme.ID]groupme.User cleanDisconnection bool batteryWarningsSent int @@ -73,125 +75,228 @@ type User struct { messageInput chan PortalMessage messageOutput chan PortalMessage - syncStart chan struct{} - syncWait sync.WaitGroup - mgmtCreateLock sync.Mutex + + spaceCreateLock sync.Mutex + spaceMembershipChecked bool } -func (bridge *Bridge) GetUserByMXID(userID id.UserID) *User { - _, isPuppet := bridge.ParsePuppetMXID(userID) - if isPuppet || userID == bridge.Bot.UserID { +func (br *GMBridge) getUserByMXID(userID id.UserID, onlyIfExists bool) *User { + _, isPuppet := br.ParsePuppetMXID(userID) + if isPuppet || userID == br.Bot.UserID { return nil } - bridge.usersLock.Lock() - defer bridge.usersLock.Unlock() - user, ok := bridge.usersByMXID[userID] + br.usersLock.Lock() + defer br.usersLock.Unlock() + user, ok := br.usersByMXID[userID] if !ok { - return bridge.loadDBUser(bridge.DB.User.GetByMXID(userID), &userID) + userIDPtr := &userID + if onlyIfExists { + userIDPtr = nil + } + return br.loadDBUser(br.DB.User.GetByMXID(userID), userIDPtr) } return user } -func (bridge *Bridge) GetUserByJID(userID types.GroupMeID) *User { +func (br *GMBridge) GetUserByMXID(userID id.UserID) *User { + return br.getUserByMXID(userID, false) +} + +func (br *GMBridge) GetIUser(userID id.UserID, create bool) bridge.User { + u := br.getUserByMXID(userID, !create) + if u == nil { + return nil + } + return u +} + +func (user *User) GetPermissionLevel() bridgeconfig.PermissionLevel { + return user.PermissionLevel +} + +func (user *User) GetManagementRoomID() id.RoomID { + return user.ManagementRoom +} + +func (user *User) GetMXID() id.UserID { + return user.MXID +} + +func (user *User) GetCommandState() map[string]interface{} { + return nil +} + +func (br *GMBridge) GetUserByMXIDIfExists(userID id.UserID) *User { + return br.getUserByMXID(userID, true) +} + +func (bridge *GMBridge) GetUserByGMID(gmid groupme.ID) *User { bridge.usersLock.Lock() defer bridge.usersLock.Unlock() - user, ok := bridge.usersByJID[userID] + user, ok := bridge.usersByGMID[gmid] if !ok { - return bridge.loadDBUser(bridge.DB.User.GetByJID(userID), nil) + return bridge.loadDBUser(bridge.DB.User.GetByGMID(gmid), nil) } return user } -func (user *User) addToJIDMap() { +func (user *User) addToGMIDMap() { user.bridge.usersLock.Lock() - user.bridge.usersByJID[user.JID] = user + user.bridge.usersByGMID[user.GMID] = user user.bridge.usersLock.Unlock() } -func (user *User) removeFromJIDMap() { +func (user *User) removeFromGMIDMap() { user.bridge.usersLock.Lock() - jidUser, ok := user.bridge.usersByJID[user.JID] + jidUser, ok := user.bridge.usersByGMID[user.GMID] if ok && user == jidUser { - delete(user.bridge.usersByJID, user.JID) + delete(user.bridge.usersByGMID, user.GMID) } user.bridge.usersLock.Unlock() - user.bridge.Metrics.TrackLoginState(user.JID, false) + user.bridge.Metrics.TrackLoginState(user.GMID, false) } -func (bridge *Bridge) GetAllUsers() []*User { - bridge.usersLock.Lock() - defer bridge.usersLock.Unlock() - dbUsers := bridge.DB.User.GetAll() +func (br *GMBridge) GetAllUsers() []*User { + br.usersLock.Lock() + defer br.usersLock.Unlock() + dbUsers := br.DB.User.GetAll() output := make([]*User, len(dbUsers)) for index, dbUser := range dbUsers { - user, ok := bridge.usersByMXID[dbUser.MXID] + user, ok := br.usersByMXID[dbUser.MXID] if !ok { - user = bridge.loadDBUser(dbUser, nil) + user = br.loadDBUser(dbUser, nil) } output[index] = user } return output } -func (bridge *Bridge) loadDBUser(dbUser *database.User, mxid *id.UserID) *User { +func (br *GMBridge) loadDBUser(dbUser *database.User, mxid *id.UserID) *User { if dbUser == nil { if mxid == nil { return nil } - dbUser = bridge.DB.User.New() + dbUser = br.DB.User.New() dbUser.MXID = *mxid dbUser.Insert() } - user := bridge.NewUser(dbUser) - bridge.usersByMXID[user.MXID] = user - if len(user.JID) > 0 { - bridge.usersByJID[user.JID] = user + user := br.NewUser(dbUser) + br.usersByMXID[user.MXID] = user + if len(user.GMID) > 0 { + br.usersByGMID[user.GMID] = user } if len(user.ManagementRoom) > 0 { - bridge.managementRooms[user.ManagementRoom] = user + br.managementRooms[user.ManagementRoom] = user } return user } -func (user *User) GetPortals() []*Portal { - user.bridge.portalsLock.Lock() - keys := user.User.GetPortalKeys() - portals := make([]*Portal, len(keys)) - - for i, key := range keys { - portal, ok := user.bridge.portalsByJID[key] - if !ok { - portal = user.bridge.loadDBPortal(user.bridge.DB.Portal.GetByJID(key), &key) - } - portals[i] = portal - } - user.bridge.portalsLock.Unlock() - return portals -} - -func (bridge *Bridge) NewUser(dbUser *database.User) *User { +func (br *GMBridge) NewUser(dbUser *database.User) *User { user := &User{ User: dbUser, - bridge: bridge, - log: bridge.Log.Sub("User").Sub(string(dbUser.MXID)), - - IsRelaybot: false, + bridge: br, + log: br.Log.Sub("User").Sub(string(dbUser.MXID)), chatListReceived: make(chan struct{}, 1), syncPortalsDone: make(chan struct{}, 1), syncStart: make(chan struct{}, 1), messageInput: make(chan PortalMessage), - messageOutput: make(chan PortalMessage, bridge.Config.Bridge.UserMessageBuffer), + messageOutput: make(chan PortalMessage, br.Config.Bridge.UserMessageBuffer), } - user.RelaybotWhitelisted = user.bridge.Config.Bridge.Permissions.IsRelaybotWhitelisted(user.MXID) - user.Whitelisted = user.bridge.Config.Bridge.Permissions.IsWhitelisted(user.MXID) - user.Admin = user.bridge.Config.Bridge.Permissions.IsAdmin(user.MXID) + + user.PermissionLevel = user.bridge.Config.Bridge.Permissions.Get(user.MXID) + user.Whitelisted = user.PermissionLevel >= bridgeconfig.PermissionLevelUser + user.Admin = user.PermissionLevel >= bridgeconfig.PermissionLevelAdmin + user.BridgeState = br.NewBridgeStateQueue(user, user.log) go user.handleMessageLoop() go user.runMessageRingBuffer() return user } +func (user *User) ensureInvited(intent *appservice.IntentAPI, roomID id.RoomID, isDirect bool) (ok bool) { + extraContent := make(map[string]interface{}) + if isDirect { + extraContent["is_direct"] = true + } + customPuppet := user.bridge.GetPuppetByCustomMXID(user.MXID) + if customPuppet != nil && customPuppet.CustomIntent() != nil { + extraContent["fi.mau.will_auto_accept"] = true + } + _, err := intent.InviteUser(roomID, &mautrix.ReqInviteUser{UserID: user.MXID}, extraContent) + var httpErr mautrix.HTTPError + if err != nil && errors.As(err, &httpErr) && httpErr.RespError != nil && strings.Contains(httpErr.RespError.Err, "is already in the room") { + user.bridge.StateStore.SetMembership(roomID, user.MXID, event.MembershipJoin) + ok = true + return + } else if err != nil { + user.log.Warnfln("Failed to invite user to %s: %v", roomID, err) + } else { + ok = true + } + + if customPuppet != nil && customPuppet.CustomIntent() != nil { + err = customPuppet.CustomIntent().EnsureJoined(roomID, appservice.EnsureJoinedParams{IgnoreCache: true}) + if err != nil { + user.log.Warnfln("Failed to auto-join %s: %v", roomID, err) + ok = false + } else { + ok = true + } + } + return +} + +func (user *User) GetSpaceRoom() id.RoomID { + if !user.bridge.Config.Bridge.PersonalFilteringSpaces { + return "" + } + + if len(user.SpaceRoom) == 0 { + user.spaceCreateLock.Lock() + defer user.spaceCreateLock.Unlock() + if len(user.SpaceRoom) > 0 { + return user.SpaceRoom + } + + resp, err := user.bridge.Bot.CreateRoom(&mautrix.ReqCreateRoom{ + Visibility: "private", + Name: "GroupMe", + Topic: "Your GroupMe bridged chats", + InitialState: []*event.Event{{ + Type: event.StateRoomAvatar, + Content: event.Content{ + Parsed: &event.RoomAvatarEventContent{ + URL: user.bridge.Config.AppService.Bot.ParsedAvatar, + }, + }, + }}, + CreationContent: map[string]interface{}{ + "type": event.RoomTypeSpace, + }, + PowerLevelOverride: &event.PowerLevelsEventContent{ + Users: map[id.UserID]int{ + user.bridge.Bot.UserID: 9001, + user.MXID: 50, + }, + }, + }) + + if err != nil { + user.log.Errorln("Failed to auto-create space room:", err) + } else { + user.SpaceRoom = resp.RoomID + user.Update() + user.ensureInvited(user.bridge.Bot, user.SpaceRoom, false) + } + } else if !user.spaceMembershipChecked && !user.bridge.StateStore.IsInRoom(user.SpaceRoom, user.MXID) { + user.ensureInvited(user.bridge.Bot, user.SpaceRoom, false) + } + user.spaceMembershipChecked = true + + return user.SpaceRoom +} + func (user *User) GetManagementRoom() id.RoomID { if len(user.ManagementRoom) == 0 { user.mgmtCreateLock.Lock() @@ -199,9 +304,14 @@ func (user *User) GetManagementRoom() id.RoomID { if len(user.ManagementRoom) > 0 { return user.ManagementRoom } + creationContent := make(map[string]interface{}) + if !user.bridge.Config.Bridge.FederateRooms { + creationContent["m.federate"] = false + } resp, err := user.bridge.Bot.CreateRoom(&mautrix.ReqCreateRoom{ - Topic: "WhatsApp bridge notices", - IsDirect: true, + Topic: "GroupMe bridge notices", + IsDirect: true, + CreationContent: creationContent, }) if err != nil { user.log.Errorln("Failed to auto-create management room:", err) @@ -240,22 +350,13 @@ func (user *User) Connect() bool { } user.log.Debugln("Connecting to WhatsApp") - timeout := time.Duration(user.bridge.Config.Bridge.ConnectionTimeout) + timeout := time.Duration(user.bridge.Config.GroupMe.ConnectionTimeout) if timeout == 0 { timeout = 20 } conn := groupme.NewPushSubscription(context.Background()) user.Conn = &conn - user.Conn.StartListening(context.TODO(), groupmeExt.NewFayeClient(user.log)) - // if err != nil { - // user.log.Errorln("Failed to connect to WhatsApp:", err) - // user.sendMarkdownBridgeAlert("\u26a0 Failed to connect to WhatsApp server. " + - // "This indicates a network problem on the bridge server. See bridge logs for more info.") - // return false - // } - // user.Conn = whatsappExt.ExtendConn(conn) - // _ = user.Conn.SetClientName(user.bridge.Config.WhatsApp.OSName, user.bridge.Config.WhatsApp.BrowserName, WAVersion) - // user.log.Debugln("WhatsApp connection successful") + user.Conn.StartListening(context.TODO(), groupmeext.NewFayeClient(user.log)) user.Conn.AddFullHandler(user) //TODO: typing notification? @@ -264,26 +365,7 @@ func (user *User) Connect() bool { func (user *User) RestoreSession() bool { if len(user.Token) > 0 { - // if err == whatsapp.ErrAlreadyLoggedIn { - // return true - // } else if err != nil { - // user.log.Errorln("Failed to restore session:", err) - // if errors.Is(err, whatsapp.ErrUnpaired) { - // user.sendMarkdownBridgeAlert("\u26a0 Failed to connect to WhatsApp: unpaired from phone. " + - // "To re-pair your phone, use `delete-session` and then `login`.") - // } else { - // user.sendMarkdownBridgeAlert("\u26a0 Failed to connect to WhatsApp. Make sure WhatsApp " + - // "on your phone is reachable and use `reconnect` to try connecting again.") - // } - // user.log.Debugln("Disconnecting due to failed session restore...") - // _, err := - // if err != nil { - // user.log.Errorln("Failed to disconnect after failed session restore:", err) - // } - // return false - // } - - err := user.Conn.SubscribeToUser(context.TODO(), groupme.ID(user.JID), user.Token) + err := user.Conn.SubscribeToUser(context.TODO(), groupme.ID(user.GMID), user.Token.String()) if err != nil { fmt.Println(err) } @@ -304,7 +386,7 @@ func (user *User) HasSession() bool { } func (user *User) IsConnected() bool { - println("better connectoin check TODO") + // TODO: better connection check return user.Conn != nil } @@ -317,65 +399,27 @@ func (user *User) IsLoginInProgress() bool { return false } -func (user *User) GetJID() types.GroupMeID { - if len(user.JID) == 0 { - u, _ := user.Client.MyUser(context.TODO()) - user.JID = u.ID.String() +func (user *User) GetGMID() groupme.ID { + if len(user.GMID) == 0 { + u, err := user.Client.MyUser(context.TODO()) + if err != nil { + user.log.Errorln("Failed to get own GroupMe ID:", err) + return "" + } + user.GMID = u.ID } - return user.JID + return user.GMID } -func (user *User) Login(ce *CommandEvent) { - // qrChan := make(chan string, 3) - // eventIDChan := make(chan id.EventID, 1) - // go user.loginQrChannel(ce, qrChan, eventIDChan) - // session, err := user.Conn.LoginWithRetry(qrChan, user.bridge.Config.Bridge.LoginQRRegenCount) - // qrChan <- "stop" - // if err != nil { - // var eventID id.EventID - // select { - // case eventID = <-eventIDChan: - // default: - // } - // reply := event.MessageEventContent{ - // MsgType: event.MsgText, - // } - // if err == whatsapp.ErrAlreadyLoggedIn { - // reply.Body = "You're already logged in" - // } else if err == whatsapp.ErrLoginInProgress { - // reply.Body = "You have a login in progress already." - // } else if err == whatsapp.ErrLoginTimedOut { - // reply.Body = "QR code scan timed out. Please try again." - // } else { - // user.log.Warnln("Failed to log in:", err) - // reply.Body = fmt.Sprintf("Unknown error while logging in: %v", err) - // } - // msg := reply - // if eventID != "" { - // msg.NewContent = &reply - // msg.RelatesTo = &event.RelatesTo{ - // Type: event.RelReplace, - // EventID: eventID, - // } - // } - // _, _ = ce.Bot.SendMessageEvent(ce.RoomID, event.EventMessage, &msg) - // return - // } - // // TODO there's a bit of duplication between this and the provisioning API login method - // // Also between the two logout methods (commands.go and provisioning.go) - // user.ConnectionErrors = 0 - // user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) - if len(ce.Args) < 1 { - ce.Reply(`Get your access token from https://dev.groupme.com/ which should be the first argument to login`) - return - } - user.Token = ce.Args[0] +func (user *User) Login(token string) error { + user.Token = token - user.addToJIDMap() - //user.SetSession(&session) - ce.Reply("Successfully logged in, synchronizing chats...") + user.addToGMIDMap() user.PostLogin() - user.Connect() + if user.Connect() { + return nil + } + return errors.New("failed to connect") } type Chat struct { @@ -385,11 +429,6 @@ type Chat struct { DM *groupme.Chat } -////returns private chat assuming one of group or dm have been initialized properly -//func (c Chat) IsPrivate() bool { -// return c.Group == nil -//} - type ChatList []Chat func (cl ChatList) Len() int { @@ -405,12 +444,9 @@ func (cl ChatList) Swap(i, j int) { } func (user *User) PostLogin() { - user.bridge.Metrics.TrackConnectionState(user.JID, true) - user.bridge.Metrics.TrackLoginState(user.JID, true) + user.bridge.Metrics.TrackConnectionState(user.GMID, true) + user.bridge.Metrics.TrackLoginState(user.GMID, true) user.bridge.Metrics.TrackBufferLength(user.MXID, 0) - user.log.Debugln("Locking processing of incoming messages and starting post-login sync") - user.syncWait.Add(1) - user.syncStart <- struct{}{} go user.intPostLogin() } @@ -423,7 +459,7 @@ func (user *User) tryAutomaticDoublePuppeting() { return } user.log.Debugln("Checking if double puppeting needs to be enabled") - puppet := user.bridge.GetPuppetByJID(user.JID) + puppet := user.bridge.GetPuppetByGMID(user.JID) if len(puppet.CustomMXID) > 0 { user.log.Debugln("User already has double-puppeting enabled") // Custom puppet already enabled @@ -486,7 +522,7 @@ func (user *User) postConnPing() bool { func (user *User) intPostLogin() { defer user.syncWait.Done() user.lastReconnection = time.Now().Unix() - user.Client = groupmeExt.NewClient(user.Token) + user.Client = groupmeext.NewClient(user.Token) if len(user.JID) == 0 { myuser, err := user.Client.MyUser(context.TODO()) if err != nil { @@ -525,22 +561,6 @@ func (user *User) intPostLogin() { } } -func (user *User) HandleStreamEvent(evt whatsappExt.StreamEvent) { - if evt.Type == whatsappExt.StreamSleep { - if user.lastReconnection+60 > time.Now().Unix() { - user.lastReconnection = 0 - user.log.Infoln("Stream went to sleep soon after reconnection, making new post-connection ping in 20 seconds") - go func() { - time.Sleep(20 * time.Second) - // TODO if this happens during the post-login sync, it can get stuck forever - user.postConnPing() - }() - } - } else { - user.log.Infofln("Stream event: %+v", evt) - } -} - func (user *User) HandleChatList() { chatMap := make(map[string]groupme.Group) chats, err := user.Client.IndexAllGroups() @@ -618,7 +638,7 @@ func (user *User) syncPortals(createAll bool) { if inCommunity, ok = existingKeys[chat.Portal.Key]; !ok || !inCommunity { inCommunity = user.addPortalToCommunity(chat.Portal) if chat.Portal.IsPrivateChat() { - puppet := user.bridge.GetPuppetByJID(chat.Portal.Key.JID) + puppet := user.bridge.GetPuppetByJID(chat.Portal.Key.GMID) user.addPuppetToCommunity(puppet) } } @@ -673,7 +693,7 @@ func (user *User) getDirectChats() map[id.UserID][]id.RoomID { privateChats := user.bridge.DB.Portal.FindPrivateChats(user.JID) for _, portal := range privateChats { if len(portal.MXID) > 0 { - res[user.bridge.FormatPuppetMXID(portal.Key.JID)] = []id.RoomID{portal.MXID} + res[user.bridge.FormatPuppetMXID(portal.Key.GMID)] = []id.RoomID{portal.MXID} } } return res @@ -845,12 +865,12 @@ func (user *User) HandleJSONParseError(err error) { user.log.Errorln("WhatsApp JSON parse error:", err) } -func (user *User) PortalKey(jid types.GroupMeID) database.PortalKey { - return database.NewPortalKey(jid, user.JID) +func (user *User) PortalKey(gmid groupme.ID) database.PortalKey { + return database.NewPortalKey(gmid, user.GMID) } -func (user *User) GetPortalByJID(jid types.GroupMeID) *Portal { - return user.bridge.GetPortalByJID(user.PortalKey(jid)) +func (user *User) GetPortalByJID(gmid groupme.ID) *Portal { + return user.bridge.GetPortalByGMID(user.PortalKey(gmid)) } func (user *User) runMessageRingBuffer() { @@ -871,8 +891,8 @@ func (user *User) handleMessageLoop() { select { case msg := <-user.messageOutput: user.bridge.Metrics.TrackBufferLength(user.MXID, len(user.messageOutput)) - puppet := user.bridge.GetPuppetByJID(msg.data.UserID.String()) - portal := user.bridge.GetPortalByJID(msg.chat) + puppet := user.bridge.GetPuppetByGMID(msg.data.UserID) + portal := user.bridge.GetPortalByGMID(msg.chat) if puppet != nil { puppet.Sync(user, portal.MXID, groupme.Member{ UserID: msg.data.UserID, @@ -881,12 +901,6 @@ func (user *User) handleMessageLoop() { }) //TODO: add params or docs? } portal.messages <- msg - case <-user.syncStart: - user.log.Debugln("Processing of incoming messages is locked") - user.bridge.Metrics.TrackSyncLock(user.JID, true) - user.syncWait.Wait() - user.bridge.Metrics.TrackSyncLock(user.JID, false) - user.log.Debugln("Processing of incoming messages unlocked") } } } diff --git a/whatsapp-ext/call.go b/whatsapp-ext/call.go deleted file mode 100644 index 99791fb..0000000 --- a/whatsapp-ext/call.go +++ /dev/null @@ -1,72 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - "strings" - - "github.com/Rhymen/go-whatsapp" -) - -type CallInfoType string - -const ( - CallOffer CallInfoType = "offer" - CallOfferVideo CallInfoType = "offer_video" - CallTransport CallInfoType = "transport" - CallRelayLatency CallInfoType = "relaylatency" - CallTerminate CallInfoType = "terminate" -) - -type CallInfo struct { - ID string `json:"id"` - Type CallInfoType `json:"type"` - From string `json:"from"` - - Platform string `json:"platform"` - Version []int `json:"version"` - - Data [][]interface{} `json:"data"` -} - -type CallInfoHandler interface { - whatsapp.Handler - HandleCallInfo(CallInfo) -} - -func (ext *ExtendedConn) handleMessageCall(message []byte) { - var event CallInfo - err := json.Unmarshal(message, &event) - if err != nil { - ext.jsonParseError(err) - return - } - event.From = strings.Replace(event.From, OldUserSuffix, NewUserSuffix, 1) - for _, handler := range ext.handlers { - callInfoHandler, ok := handler.(CallInfoHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(callInfoHandler) { - callInfoHandler.HandleCallInfo(event) - } else { - go callInfoHandler.HandleCallInfo(event) - } - } -} diff --git a/whatsapp-ext/chat.go b/whatsapp-ext/chat.go deleted file mode 100644 index 772bcaf..0000000 --- a/whatsapp-ext/chat.go +++ /dev/null @@ -1,183 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - "strings" - - "github.com/Rhymen/go-whatsapp" -) - -type ChatUpdateCommand string - -const ( - ChatUpdateCommandAction ChatUpdateCommand = "action" -) - -type ChatUpdate struct { - JID string `json:"id"` - Command ChatUpdateCommand `json:"cmd"` - Data ChatUpdateData `json:"data"` -} - -type ChatActionType string - -const ( - ChatActionNameChange ChatActionType = "subject" - ChatActionAddTopic ChatActionType = "desc_add" - ChatActionRemoveTopic ChatActionType = "desc_remove" - ChatActionRestrict ChatActionType = "restrict" - ChatActionAnnounce ChatActionType = "announce" - ChatActionPromote ChatActionType = "promote" - ChatActionDemote ChatActionType = "demote" - ChatActionIntroduce ChatActionType = "introduce" - ChatActionCreate ChatActionType = "create" - ChatActionRemove ChatActionType = "remove" - ChatActionAdd ChatActionType = "add" -) - -type ChatUpdateData struct { - Action ChatActionType - SenderJID string - - NameChange struct { - Name string `json:"subject"` - SetAt int64 `json:"s_t"` - SetBy string `json:"s_o"` - } - - AddTopic struct { - Topic string `json:"desc"` - ID string `json:"descId"` - SetAt int64 `json:"descTime"` - SetBy string `json:"descOwner"` - } - - RemoveTopic struct { - ID string `json:"descId"` - } - - Introduce struct { - CreationTime int64 `json:"creation"` - Admins []string `json:"admins"` - SuperAdmins []string `json:"superadmins"` - Regulars []string `json:"regulars"` - } - - Restrict bool - - Announce bool - - UserChange struct { - JIDs []string `json:"participants"` - } -} - -func (cud *ChatUpdateData) UnmarshalJSON(data []byte) error { - var arr []json.RawMessage - err := json.Unmarshal(data, &arr) - if err != nil { - return err - } else if len(arr) < 3 { - return nil - } - - err = json.Unmarshal(arr[0], &cud.Action) - if err != nil { - return err - } - - err = json.Unmarshal(arr[1], &cud.SenderJID) - if err != nil { - return err - } - cud.SenderJID = strings.Replace(cud.SenderJID, OldUserSuffix, NewUserSuffix, 1) - - var unmarshalTo interface{} - switch cud.Action { - case ChatActionIntroduce, ChatActionCreate: - err = json.Unmarshal(arr[2], &cud.NameChange) - if err != nil { - return err - } - err = json.Unmarshal(arr[2], &cud.AddTopic) - if err != nil { - return err - } - unmarshalTo = &cud.Introduce - case ChatActionNameChange: - unmarshalTo = &cud.NameChange - case ChatActionAddTopic: - unmarshalTo = &cud.AddTopic - case ChatActionRemoveTopic: - unmarshalTo = &cud.RemoveTopic - case ChatActionRestrict: - unmarshalTo = &cud.Restrict - case ChatActionAnnounce: - unmarshalTo = &cud.Announce - case ChatActionPromote, ChatActionDemote, ChatActionRemove, ChatActionAdd: - unmarshalTo = &cud.UserChange - default: - return nil - } - err = json.Unmarshal(arr[2], unmarshalTo) - if err != nil { - return err - } - cud.NameChange.SetBy = strings.Replace(cud.NameChange.SetBy, OldUserSuffix, NewUserSuffix, 1) - for index, jid := range cud.UserChange.JIDs { - cud.UserChange.JIDs[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1) - } - for index, jid := range cud.Introduce.SuperAdmins { - cud.Introduce.SuperAdmins[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1) - } - for index, jid := range cud.Introduce.Admins { - cud.Introduce.Admins[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1) - } - for index, jid := range cud.Introduce.Regulars { - cud.Introduce.Regulars[index] = strings.Replace(jid, OldUserSuffix, NewUserSuffix, 1) - } - return nil -} - -type ChatUpdateHandler interface { - whatsapp.Handler - HandleChatUpdate(ChatUpdate) -} - -func (ext *ExtendedConn) handleMessageChatUpdate(message []byte) { - var event ChatUpdate - err := json.Unmarshal(message, &event) - if err != nil { - ext.jsonParseError(err) - return - } - event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1) - for _, handler := range ext.handlers { - chatUpdateHandler, ok := handler.(ChatUpdateHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(chatUpdateHandler) { - chatUpdateHandler.HandleChatUpdate(event) - } else { - go chatUpdateHandler.HandleChatUpdate(event) - } - } -} diff --git a/whatsapp-ext/cmd.go b/whatsapp-ext/cmd.go deleted file mode 100644 index 7815c19..0000000 --- a/whatsapp-ext/cmd.go +++ /dev/null @@ -1,69 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - "strings" - - "github.com/Rhymen/go-whatsapp" -) - -type CommandType string - -const ( - CommandPicture CommandType = "picture" - CommandDisconnect CommandType = "disconnect" -) - -type Command struct { - Type CommandType `json:"type"` - JID string `json:"jid"` - - *ProfilePicInfo - Kind string `json:"kind"` - - Raw json.RawMessage `json:"-"` -} - -type CommandHandler interface { - whatsapp.Handler - HandleCommand(Command) -} - -func (ext *ExtendedConn) handleMessageCommand(message []byte) { - var event Command - err := json.Unmarshal(message, &event) - if err != nil { - ext.jsonParseError(err) - return - } - event.Raw = message - event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1) - for _, handler := range ext.handlers { - commandHandler, ok := handler.(CommandHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(commandHandler) { - commandHandler.HandleCommand(event) - } else { - go commandHandler.HandleCommand(event) - } - } -} diff --git a/whatsapp-ext/conn.go b/whatsapp-ext/conn.go deleted file mode 100644 index 161cb25..0000000 --- a/whatsapp-ext/conn.go +++ /dev/null @@ -1,65 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - - "github.com/Rhymen/go-whatsapp" -) - -type ConnInfo struct { - ProtocolVersion []int `json:"protoVersion"` - BinaryVersion int `json:"binVersion"` - Phone struct { - WhatsAppVersion string `json:"wa_version"` - MCC string `json:"mcc"` - MNC string `json:"mnc"` - OSVersion string `json:"os_version"` - DeviceManufacturer string `json:"device_manufacturer"` - DeviceModel string `json:"device_model"` - OSBuildNumber string `json:"os_build_number"` - } `json:"phone"` - Features map[string]interface{} `json:"features"` - PushName string `json:"pushname"` -} - -type ConnInfoHandler interface { - whatsapp.Handler - HandleConnInfo(ConnInfo) -} - -func (ext *ExtendedConn) handleMessageConn(message []byte) { - var event ConnInfo - err := json.Unmarshal(message, &event) - if err != nil { - ext.jsonParseError(err) - return - } - for _, handler := range ext.handlers { - connInfoHandler, ok := handler.(ConnInfoHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(connInfoHandler) { - connInfoHandler.HandleConnInfo(event) - } else { - go connInfoHandler.HandleConnInfo(event) - } - } -} diff --git a/whatsapp-ext/group.go b/whatsapp-ext/group.go deleted file mode 100644 index 9e2de34..0000000 --- a/whatsapp-ext/group.go +++ /dev/null @@ -1,66 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "github.com/beeper/groupme/types" -) - -type CreateGroupResponse struct { - Status int `json:"status"` - GroupID types.GroupMeID `json:"gid"` - Participants map[types.GroupMeID]struct { - Code string `json:"code"` - } `json:"participants"` - - Source string `json:"-"` -} - -type actualCreateGroupResponse struct { - Status int `json:"status"` - GroupID types.GroupMeID `json:"gid"` - Participants []map[types.GroupMeID]struct { - Code string `json:"code"` - } `json:"participants"` -} - -func (ext *ExtendedConn) CreateGroup(subject string, participants []types.GroupMeID) (*CreateGroupResponse, error) { - // respChan, err := ext.Conn.CreateGroup(subject, participants) - // if err != nil { - // return nil, err - // } - // var resp CreateGroupResponse - // var actualResp actualCreateGroupResponse - // resp.Source = <-respChan - // fmt.Println(">>>>>>", resp.Source) - // err = json.Unmarshal([]byte(resp.Source), &actualResp) - // if err != nil { - // return nil, err - // } - // resp.Status = actualResp.Status - // resp.GroupID = actualResp.GroupID - // resp.Participants = make(map[types.GroupMeID]struct { - // Code string `json:"code"` - // }) - // for _, participantMap := range actualResp.Participants { - // for jid, status := range participantMap { - // resp.Participants[jid] = status - // } - // } - // return &resp, nil - return nil, nil -} diff --git a/whatsapp-ext/jsonmessage.go b/whatsapp-ext/jsonmessage.go deleted file mode 100644 index 107ea7a..0000000 --- a/whatsapp-ext/jsonmessage.go +++ /dev/null @@ -1,105 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - - "github.com/Rhymen/go-whatsapp" -) - -type JSONMessage []json.RawMessage - -type JSONMessageType string - -const ( - MessageMsgInfo JSONMessageType = "MsgInfo" - MessageMsg JSONMessageType = "Msg" - MessagePresence JSONMessageType = "Presence" - MessageStream JSONMessageType = "Stream" - MessageConn JSONMessageType = "Conn" - MessageProps JSONMessageType = "Props" - MessageCmd JSONMessageType = "Cmd" - MessageChat JSONMessageType = "Chat" - MessageCall JSONMessageType = "Call" -) - -func (ext *ExtendedConn) HandleError(error) {} - -type UnhandledJSONMessageHandler interface { - whatsapp.Handler - HandleUnhandledJSONMessage(string) -} - -type JSONParseErrorHandler interface { - whatsapp.Handler - HandleJSONParseError(error) -} - -func (ext *ExtendedConn) jsonParseError(err error) { - for _, handler := range ext.handlers { - errorHandler, ok := handler.(JSONParseErrorHandler) - if !ok { - continue - } - errorHandler.HandleJSONParseError(err) - } -} - -func (ext *ExtendedConn) HandleJsonMessage(message string) { - msg := JSONMessage{} - err := json.Unmarshal([]byte(message), &msg) - if err != nil || len(msg) < 2 { - ext.jsonParseError(err) - return - } - - var msgType JSONMessageType - json.Unmarshal(msg[0], &msgType) - - switch msgType { - case MessagePresence: - ext.handleMessagePresence(msg[1]) - case MessageStream: - ext.handleMessageStream(msg[1:]) - case MessageConn: - ext.handleMessageConn(msg[1]) - case MessageProps: - ext.handleMessageProps(msg[1]) - case MessageMsgInfo, MessageMsg: - ext.handleMessageMsgInfo(msgType, msg[1]) - case MessageCmd: - ext.handleMessageCommand(msg[1]) - case MessageChat: - ext.handleMessageChatUpdate(msg[1]) - case MessageCall: - ext.handleMessageCall(msg[1]) - default: - for _, handler := range ext.handlers { - ujmHandler, ok := handler.(UnhandledJSONMessageHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(ujmHandler) { - ujmHandler.HandleUnhandledJSONMessage(message) - } else { - go ujmHandler.HandleUnhandledJSONMessage(message) - } - } - } -} diff --git a/whatsapp-ext/msginfo.go b/whatsapp-ext/msginfo.go deleted file mode 100644 index fe2e6c4..0000000 --- a/whatsapp-ext/msginfo.go +++ /dev/null @@ -1,95 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - "strings" - - "github.com/Rhymen/go-whatsapp" -) - -type MsgInfoCommand string - -const ( - MsgInfoCommandAck MsgInfoCommand = "ack" - MsgInfoCommandAcks MsgInfoCommand = "acks" -) - -type Acknowledgement int - -const ( - AckMessageSent Acknowledgement = 1 - AckMessageDelivered Acknowledgement = 2 - AckMessageRead Acknowledgement = 3 -) - -type JSONStringOrArray []string - -func (jsoa *JSONStringOrArray) UnmarshalJSON(data []byte) error { - var str string - if json.Unmarshal(data, &str) == nil { - *jsoa = []string{str} - return nil - } - var strs []string - json.Unmarshal(data, &strs) - *jsoa = strs - return nil -} - -type MsgInfo struct { - Command MsgInfoCommand `json:"cmd"` - IDs JSONStringOrArray `json:"id"` - Acknowledgement Acknowledgement `json:"ack"` - MessageFromJID string `json:"from"` - SenderJID string `json:"participant"` - ToJID string `json:"to"` - Timestamp int64 `json:"t"` -} - -type MsgInfoHandler interface { - whatsapp.Handler - HandleMsgInfo(MsgInfo) -} - -func (ext *ExtendedConn) handleMessageMsgInfo(msgType JSONMessageType, message []byte) { - var event MsgInfo - err := json.Unmarshal(message, &event) - if err != nil { - ext.jsonParseError(err) - return - } - event.MessageFromJID = strings.Replace(event.MessageFromJID, OldUserSuffix, NewUserSuffix, 1) - event.SenderJID = strings.Replace(event.SenderJID, OldUserSuffix, NewUserSuffix, 1) - event.ToJID = strings.Replace(event.ToJID, OldUserSuffix, NewUserSuffix, 1) - if msgType == MessageMsg { - event.SenderJID = event.ToJID - } - for _, handler := range ext.handlers { - msgInfoHandler, ok := handler.(MsgInfoHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(msgInfoHandler) { - msgInfoHandler.HandleMsgInfo(event) - } else { - go msgInfoHandler.HandleMsgInfo(event) - } - } -} diff --git a/whatsapp-ext/presence.go b/whatsapp-ext/presence.go deleted file mode 100644 index 9645ce6..0000000 --- a/whatsapp-ext/presence.go +++ /dev/null @@ -1,64 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - "strings" - - "github.com/Rhymen/go-whatsapp" -) - -type Presence struct { - JID string `json:"id"` - SenderJID string `json:"participant"` - Status whatsapp.Presence `json:"type"` - Timestamp int64 `json:"t"` - Deny bool `json:"deny"` -} - -type PresenceHandler interface { - whatsapp.Handler - HandlePresence(Presence) -} - -func (ext *ExtendedConn) handleMessagePresence(message []byte) { - var event Presence - err := json.Unmarshal(message, &event) - if err != nil { - ext.jsonParseError(err) - return - } - event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1) - if len(event.SenderJID) == 0 { - event.SenderJID = event.JID - } else { - event.SenderJID = strings.Replace(event.SenderJID, OldUserSuffix, NewUserSuffix, 1) - } - for _, handler := range ext.handlers { - presenceHandler, ok := handler.(PresenceHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(presenceHandler) { - presenceHandler.HandlePresence(event) - } else { - go presenceHandler.HandlePresence(event) - } - } -} diff --git a/whatsapp-ext/props.go b/whatsapp-ext/props.go deleted file mode 100644 index 8d8ca5f..0000000 --- a/whatsapp-ext/props.go +++ /dev/null @@ -1,73 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - - "github.com/Rhymen/go-whatsapp" -) - -type ProtocolProps struct { - WebPresence bool `json:"webPresence"` - NotificationQuery bool `json:"notificationQuery"` - FacebookCrashLog bool `json:"fbCrashlog"` - Bucket string `json:"bucket"` - GIFSearch string `json:"gifSearch"` - Spam bool `json:"SPAM"` - SetBlock bool `json:"SET_BLOCK"` - MessageInfo bool `json:"MESSAGE_INFO"` - MaxFileSize int `json:"maxFileSize"` - Media int `json:"media"` - GroupNameLength int `json:"maxSubject"` - GroupDescriptionLength int `json:"groupDescLength"` - MaxParticipants int `json:"maxParticipants"` - VideoMaxEdge int `json:"videoMaxEdge"` - ImageMaxEdge int `json:"imageMaxEdge"` - ImageMaxKilobytes int `json:"imageMaxKBytes"` - Edit int `json:"edit"` - FwdUIStartTimestamp int `json:"fwdUiStartTs"` - GroupsV3 int `json:"groupsV3"` - RestrictGroups int `json:"restrictGroups"` - AnnounceGroups int `json:"announceGroups"` -} - -type ProtocolPropsHandler interface { - whatsapp.Handler - HandleProtocolProps(ProtocolProps) -} - -func (ext *ExtendedConn) handleMessageProps(message []byte) { - var event ProtocolProps - err := json.Unmarshal(message, &event) - if err != nil { - ext.jsonParseError(err) - return - } - for _, handler := range ext.handlers { - protocolPropsHandler, ok := handler.(ProtocolPropsHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(protocolPropsHandler) { - protocolPropsHandler.HandleProtocolProps(event) - } else { - go protocolPropsHandler.HandleProtocolProps(event) - } - } -} diff --git a/whatsapp-ext/protomessage.go b/whatsapp-ext/protomessage.go deleted file mode 100644 index 5671000..0000000 --- a/whatsapp-ext/protomessage.go +++ /dev/null @@ -1,59 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "github.com/Rhymen/go-whatsapp" - "github.com/Rhymen/go-whatsapp/binary/proto" -) - -type MessageRevokeHandler interface { - whatsapp.Handler - HandleMessageRevoke(key MessageRevocation) -} - -type MessageRevocation struct { - Id string - RemoteJid string - FromMe bool - Participant string -} - -func (ext *ExtendedConn) HandleRawMessage(message *proto.WebMessageInfo) { - protoMsg := message.GetMessage().GetProtocolMessage() - if protoMsg != nil && protoMsg.GetType() == proto.ProtocolMessage_REVOKE { - key := protoMsg.GetKey() - deletedMessage := MessageRevocation{ - Id: key.GetId(), - RemoteJid: key.GetRemoteJid(), - FromMe: key.GetFromMe(), - Participant: key.GetParticipant(), - } - for _, handler := range ext.handlers { - mrHandler, ok := handler.(MessageRevokeHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(mrHandler) { - mrHandler.HandleMessageRevoke(deletedMessage) - } else { - go mrHandler.HandleMessageRevoke(deletedMessage) - } - } - } -} diff --git a/whatsapp-ext/stream.go b/whatsapp-ext/stream.go deleted file mode 100644 index e521063..0000000 --- a/whatsapp-ext/stream.go +++ /dev/null @@ -1,76 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - - "github.com/Rhymen/go-whatsapp" -) - -type StreamType string - -const ( - StreamUpdate = "update" - StreamSleep = "asleep" -) - -type StreamEvent struct { - Type StreamType - - IsOutdated bool - Version string - - Extra []json.RawMessage -} - -type StreamEventHandler interface { - whatsapp.Handler - HandleStreamEvent(StreamEvent) -} - -func (ext *ExtendedConn) handleMessageStream(message []json.RawMessage) { - var event StreamEvent - err := json.Unmarshal(message[0], &event.Type) - if err != nil { - ext.jsonParseError(err) - return - } - - if event.Type == StreamUpdate && len(message) >= 3 { - _ = json.Unmarshal(message[1], &event.IsOutdated) - _ = json.Unmarshal(message[2], &event.Version) - if len(message) >= 4 { - event.Extra = message[3:] - } - } else if len(message) >= 2 { - event.Extra = message[1:] - } - - for _, handler := range ext.handlers { - streamHandler, ok := handler.(StreamEventHandler) - if !ok { - continue - } - - if ext.shouldCallSynchronously(streamHandler) { - streamHandler.HandleStreamEvent(event) - } else { - go streamHandler.HandleStreamEvent(event) - } - } -} diff --git a/whatsapp-ext/whatsapp.go b/whatsapp-ext/whatsapp.go deleted file mode 100644 index 3ae6b64..0000000 --- a/whatsapp-ext/whatsapp.go +++ /dev/null @@ -1,164 +0,0 @@ -// mautrix-groupme - A Matrix-GroupMe puppeting bridge. -// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package whatsappExt - -import ( - "encoding/json" - "fmt" - "io" - "io/ioutil" - "net/http" - "strings" - - "github.com/Rhymen/go-whatsapp" -) - -const ( - OldUserSuffix = "@c.us" - NewUserSuffix = "@s.whatsapp.net" -) - -type ExtendedConn struct { - *whatsapp.Conn - - handlers []whatsapp.Handler -} - -func ExtendConn(conn *whatsapp.Conn) *ExtendedConn { - ext := &ExtendedConn{ - Conn: conn, - } - ext.Conn.AddHandler(ext) - return ext -} - -func (ext *ExtendedConn) AddHandler(handler whatsapp.Handler) { - ext.Conn.AddHandler(handler) - ext.handlers = append(ext.handlers, handler) -} - -func (ext *ExtendedConn) RemoveHandler(handler whatsapp.Handler) bool { - ext.Conn.RemoveHandler(handler) - for i, v := range ext.handlers { - if v == handler { - ext.handlers = append(ext.handlers[:i], ext.handlers[i+1:]...) - return true - } - } - return false -} - -func (ext *ExtendedConn) RemoveHandlers() { - ext.Conn.RemoveHandlers() - ext.handlers = make([]whatsapp.Handler, 0) -} - -func (ext *ExtendedConn) shouldCallSynchronously(handler whatsapp.Handler) bool { - sh, ok := handler.(whatsapp.SyncHandler) - return ok && sh.ShouldCallSynchronously() -} - -func (ext *ExtendedConn) ShouldCallSynchronously() bool { - return true -} - -type GroupInfo struct { - JID string `json:"jid"` - OwnerJID string `json:"owner"` - - Name string `json:"subject"` - NameSetTime int64 `json:"subjectTime"` - NameSetBy string `json:"subjectOwner"` - - Announce bool `json:"announce"` // Can only admins send messages? - - Topic string `json:"desc"` - TopicID string `json:"descId"` - TopicSetAt int64 `json:"descTime"` - TopicSetBy string `json:"descOwner"` - - GroupCreated int64 `json:"creation"` - - Status int16 `json:"status"` - - Participants []struct { - JID string `json:"id"` - IsAdmin bool `json:"isAdmin"` - IsSuperAdmin bool `json:"isSuperAdmin"` - } `json:"participants"` -} - -func (ext *ExtendedConn) GetGroupMetaData(jid string) (*GroupInfo, error) { - data, err := ext.Conn.GetGroupMetaData(jid) - if err != nil { - return nil, fmt.Errorf("failed to get group metadata: %v", err) - } - content := <-data - - info := &GroupInfo{} - err = json.Unmarshal([]byte(content), info) - if err != nil { - return info, fmt.Errorf("failed to unmarshal group metadata: %v", err) - } - - for index, participant := range info.Participants { - info.Participants[index].JID = strings.Replace(participant.JID, OldUserSuffix, NewUserSuffix, 1) - } - info.NameSetBy = strings.Replace(info.NameSetBy, OldUserSuffix, NewUserSuffix, 1) - info.TopicSetBy = strings.Replace(info.TopicSetBy, OldUserSuffix, NewUserSuffix, 1) - - return info, nil -} - -type ProfilePicInfo struct { - URL string `json:"eurl"` - Tag string `json:"tag"` - - Status int `json:"status"` -} - -func (ppi *ProfilePicInfo) Download() (io.ReadCloser, error) { - resp, err := http.Get(ppi.URL) - if err != nil { - return nil, err - } - return resp.Body, nil -} - -func (ppi *ProfilePicInfo) DownloadBytes() ([]byte, error) { - body, err := ppi.Download() - if err != nil { - return nil, err - } - defer body.Close() - data, err := ioutil.ReadAll(body) - return data, err -} - -func (ext *ExtendedConn) GetProfilePicThumb(jid string) (*ProfilePicInfo, error) { - data, err := ext.Conn.GetProfilePicThumb(jid) - if err != nil { - return nil, fmt.Errorf("failed to get avatar: %v", err) - } - content := <-data - info := &ProfilePicInfo{} - err = json.Unmarshal([]byte(content), info) - if err != nil { - return info, fmt.Errorf("failed to unmarshal avatar info: %v", err) - } - return info, nil -}