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/commands.go b/commands.go index 725ed00..6792ce5 100644 --- a/commands.go +++ b/commands.go @@ -17,939 +17,41 @@ 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 - } - - 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.") - } -} - -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.") - } -} - -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) - } -} - -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`?") - } else { - ce.Reply("No existing connection, creating one...") - ce.User.Connect() - } - 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") - return - } - - var msg string - if wasConnected { - msg = "Reconnected successfully." - } else { - msg = "Connected successfully." - } - 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") +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, + ) } 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..160eb6d 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -17,112 +17,129 @@ 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"` + + 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"` 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 +150,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 +162,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 +182,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 +215,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 +225,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..5a2570b 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"` @@ -73,45 +37,22 @@ type Config struct { } `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..75036c3 100644 --- a/database/message.go +++ b/database/message.go @@ -17,10 +17,14 @@ 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" ) @@ -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 types.GroupMeMessageID) *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 types.GroupMeID + MXID id.EventID + Sender types.GroupMeID + 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..73d5d7d 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" ) -// 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 types.GroupMeID + Receiver types.GroupMeID } -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{types.NewGroupMeID(inp), types.NewGroupMeID(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: types.NewGroupMeID(parts[1]), + Receiver: types.NewGroupMeID(parts[0]), } } else { return nil } } -func GroupPortalKey(jid types.GroupMeID) PortalKey { +func GroupPortalKey(gmid types.GroupMeID) PortalKey { return PortalKey{ - JID: jid, - Receiver: jid, + GMID: gmid, + Receiver: gmid, } } -func NewPortalKey(jid, receiver types.GroupMeID) PortalKey { +func NewPortalKey(gmid, receiver types.GroupMeID) 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 types.GroupMeID) []*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)) - + 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..21671b8 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -17,9 +17,12 @@ 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" ) @@ -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 types.GroupMeID) *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 types.GroupMeID - 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 = types.NewGroupMeID(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..7614e6d 100644 --- a/database/reactions.go +++ b/database/reactions.go @@ -1,8 +1,12 @@ 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" ) @@ -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 types.GroupMeMessageID, sender types.GroupMeID) *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 types.GroupMeMessageID + Sender types.GroupMeID + MXID id.EventID + GMID types.GroupMeID } -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..a83b2d4 100644 --- a/database/user.go +++ b/database/user.go @@ -17,12 +17,15 @@ 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" ) @@ -33,218 +36,116 @@ 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 types.GroupMeID) *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"` + MXID id.UserID + GMID types.GroupMeID + ManagementRoom id.RoomID + SpaceRoom id.RoomID + Token types.AuthToken - ManagementRoom id.RoomID - LastConnection uint64 `gorm:"notNull;default:0"` + 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 = types.NewGroupMeID(gmid.String) + } + if len(authToken.String) > 0 { + user.Token = types.AuthToken(authToken.String) + } + return user +} -func stripSuffix(jid types.GroupMeID) string { - if len(jid) == 0 { - return jid +func stripSuffix(gmid types.GroupMeID) 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..702f77c 100644 --- a/formatting.go +++ b/formatting.go @@ -32,10 +32,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 +44,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 +55,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].([]types.GroupMeID) if !ok { - ctx[mentionedJIDsContextKey] = []types.GroupMeID{puppet.JID} + ctx[mentionedGMIDsContextKey] = []types.GroupMeID{puppet.GMID} } else { - ctx[mentionedJIDsContextKey] = append(jids, puppet.JID) + ctx[mentionedGMIDsContextKey] = append(gmids, puppet.GMID) } return "@" + puppet.PhoneNumber() } @@ -139,6 +139,6 @@ func NewFormatter(bridge *Bridge) *Formatter { func (formatter *Formatter) ParseMatrix(html string) (string, []types.GroupMeID) { ctx := make(format.Context) result := formatter.matrixHTMLParser.Parse(html, ctx) - mentionedJIDs, _ := ctx[mentionedJIDsContextKey].([]types.GroupMeID) + mentionedJIDs, _ := ctx[mentionedGMIDsContextKey].([]types.GroupMeID) 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/main.go b/main.go index 7aee427..a47b2c9 100644 --- a/main.go +++ b/main.go @@ -17,320 +17,88 @@ 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/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[types.GroupMeID]*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 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 +126,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.IsEmpty() { + 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 +150,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[types.GroupMeID]*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..c8af00a 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,77 @@ 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.Topic = PrivateChatTopic _, _ = 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..0d7446e 100644 --- a/metrics.go +++ b/metrics.go @@ -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 @@ -66,7 +66,7 @@ type MetricsHandler struct { 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, @@ -112,7 +112,7 @@ func NewMetricsHandler(address string, log log.Logger, db *database.Database) *M loggedInState: make(map[types.GroupMeID]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), syncLocked: promauto.NewGauge(prometheus.GaugeOpts{ @@ -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()) } @@ -231,9 +231,9 @@ func (mh *MetricsHandler) updateStats() { // 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 '%@s.groupme.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 jid 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..b2db8b1 100644 --- a/portal.go +++ b/portal.go @@ -61,7 +61,7 @@ import ( whatsappExt "github.com/beeper/groupme/whatsapp-ext" ) -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 +71,25 @@ func (bridge *Bridge) GetPortalByMXID(mxid id.RoomID) *Portal { return portal } -func (bridge *Bridge) GetPortalByJID(key database.PortalKey) *Portal { +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) GetAllPortalsByGMID(gmid types.GroupMeID) []*Portal { + return br.dbPortalsToPortals(br.DB.Portal.GetAllByGMID(gmid)) } -func (bridge *Bridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal { +func (bridge *GMBridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal { bridge.portalsLock.Lock() defer bridge.portalsLock.Unlock() output := make([]*Portal, len(dbPortals)) @@ -97,7 +97,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 +106,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 +116,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 +127,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 +142,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, @@ -168,7 +168,7 @@ type PortalMessage struct { type Portal struct { *database.Portal - bridge *Bridge + bridge *GMBridge log log.Logger roomCreateLock sync.Mutex @@ -250,7 +250,7 @@ func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid i if message.UserID.String() == source.JID { msg.Sender = source.JID } 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 { @@ -482,7 +482,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, groupmeExt.NewUserSuffix, "", 1))) if err != nil { portal.log.Errorln(err) return false @@ -528,13 +528,8 @@ 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) { @@ -698,7 +693,7 @@ func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error 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()) + messages, err := user.Client.LoadMessagesAfter(portal.Key.GMID, lastMessageID, lastMessageFromMe, portal.IsPrivateChat()) if err != nil { return err } @@ -723,7 +718,7 @@ func (portal *Portal) beginBackfill() func() { portal.backfilling = true var privateChatPuppetInvited bool var privateChatPuppet *Puppet - if portal.IsPrivateChat() && portal.bridge.Config.Bridge.InviteOwnPuppetForBackfilling && portal.Key.JID != portal.Key.Receiver { + if portal.IsPrivateChat() && portal.bridge.Config.Bridge.InviteOwnPuppetForBackfilling && portal.Key.GMID != portal.Key.Receiver { privateChatPuppet = portal.bridge.GetPuppetByJID(portal.Key.Receiver) portal.privateChatBackfillInvitePuppet = func() { if privateChatPuppetInvited { @@ -805,7 +800,7 @@ func (portal *Portal) FillInitialHistory(user *User) error { 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()) + chunk, err := user.Client.LoadMessagesBefore(portal.Key.GMID, before, portal.IsPrivateChat()) if err != nil { return err } @@ -898,12 +893,12 @@ func (portal *Portal) getBridgeInfo() (string, BridgeInfoContent) { ExternalURL: "https://www.whatsapp.com/", }, Channel: BridgeInfoSection{ - ID: portal.Key.JID, + ID: portal.Key.GMID, 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 +919,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() @@ -941,7 +945,7 @@ 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) + puppet := portal.bridge.GetPuppetByJID(portal.Key.GMID) meta, err := portal.bridge.StateStore.TryGetMemberRaw("", puppet.MXID) if err { println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") @@ -963,7 +967,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { } 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 @@ -1053,7 +1057,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { } user.addPortalToCommunity(portal) if portal.IsPrivateChat() { - puppet := user.bridge.GetPuppetByJID(portal.Key.JID) + puppet := user.bridge.GetPuppetByJID(portal.Key.GMID) user.addPuppetToCommunity(puppet) if portal.bridge.Config.Bridge.Encryption.Default { @@ -1087,12 +1091,12 @@ func (portal *Portal) HasRelaybot() bool { } 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.GetPuppetByJID(portal.Key.GMID).DefaultIntent() } return portal.bridge.Bot } @@ -1336,7 +1340,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() } @@ -2143,7 +2147,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 +2544,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..b9eb39c 100644 --- a/puppet.go +++ b/puppet.go @@ -36,7 +36,7 @@ import ( var userIDRegex *regexp.Regexp -func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.GroupMeID, bool) { +func (bridge *GMBridge) ParsePuppetMXID(mxid id.UserID) (types.GroupMeID, bool) { if userIDRegex == nil { userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$", bridge.Config.Bridge.FormatUsername("([0-9]+)"), @@ -51,7 +51,7 @@ func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.GroupMeID, bool) { return jid, true } -func (bridge *Bridge) GetPuppetByMXID(mxid id.UserID) *Puppet { +func (bridge *GMBridge) GetPuppetByMXID(mxid id.UserID) *Puppet { jid, ok := bridge.ParsePuppetMXID(mxid) if !ok { return nil @@ -60,7 +60,7 @@ func (bridge *Bridge) GetPuppetByMXID(mxid id.UserID) *Puppet { return bridge.GetPuppetByJID(jid) } -func (bridge *Bridge) GetPuppetByJID(jid types.GroupMeID) *Puppet { +func (bridge *GMBridge) GetPuppetByJID(jid types.GroupMeID) *Puppet { bridge.puppetsLock.Lock() defer bridge.puppetsLock.Unlock() puppet, ok := bridge.puppets[jid] @@ -80,7 +80,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] @@ -96,15 +96,15 @@ func (bridge *Bridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet { return puppet } -func (bridge *Bridge) GetAllPuppetsWithCustomMXID() []*Puppet { +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)) @@ -125,7 +125,7 @@ func (bridge *Bridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet return output } -func (bridge *Bridge) FormatPuppetMXID(jid types.GroupMeID) id.UserID { +func (bridge *GMBridge) FormatPuppetMXID(jid types.GroupMeID) id.UserID { return id.NewUserID( bridge.Config.Bridge.FormatUsername( strings.Replace( @@ -134,7 +134,7 @@ func (bridge *Bridge) FormatPuppetMXID(jid types.GroupMeID) id.UserID { 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, @@ -147,7 +147,7 @@ func (bridge *Bridge) NewPuppet(dbPuppet *database.Puppet) *Puppet { type Puppet struct { *database.Puppet - bridge *Bridge + bridge *GMBridge log log.Logger typingIn id.RoomID @@ -168,7 +168,7 @@ func (puppet *Puppet) PhoneNumber() string { 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.JID { return puppet.DefaultIntent() } return puppet.customIntent 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 index 5d02085..60d2439 100644 --- a/types/types.go +++ b/types/types.go @@ -16,13 +16,22 @@ package types -// GroupMeID is a WhatsApp JID. -type GroupMeID = string +// GroupMeID is a string that represents a GroupMe ID. +type GroupMeID string -// WhatsAppMessageID is the internal ID of a WhatsApp message. -type WhatsAppMessageID = string +func NewGroupMeID(id string) GroupMeID { + return GroupMeID(id) +} -//AuthToken is the authentication token -type AuthToken = string +func (gmid GroupMeID) String() string { + return string(gmid) +} -type TmpID = GroupMeID +func (gmid GroupMeID) IsEmpty() bool { + return gmid == "" +} + +type GroupMeMessageID string + +// AuthToken is the authentication token +type AuthToken string diff --git a/user.go b/user.go index e6394b8..2cf7fe8 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" @@ -46,14 +50,14 @@ 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 ConnectionErrors int @@ -79,7 +83,25 @@ type User struct { mgmtCreateLock sync.Mutex } -func (bridge *Bridge) GetUserByMXID(userID id.UserID) *User { +func (br *GMBridge) getUserByMXID(userID id.UserID, onlyIfExists bool) *User { + _, isPuppet := br.ParsePuppetMXID(userID) + if isPuppet || userID == br.Bot.UserID { + return nil + } + br.usersLock.Lock() + defer br.usersLock.Unlock() + user, ok := br.usersByMXID[userID] + if !ok { + userIDPtr := &userID + if onlyIfExists { + userIDPtr = nil + } + return br.loadDBUser(br.DB.User.GetByMXID(userID), userIDPtr) + } + return user +} + +func (bridge *GMBridge) GetUserByMXID(userID id.UserID) *User { _, isPuppet := bridge.ParsePuppetMXID(userID) if isPuppet || userID == bridge.Bot.UserID { return nil @@ -93,105 +115,126 @@ func (bridge *Bridge) GetUserByMXID(userID id.UserID) *User { return user } -func (bridge *Bridge) GetUserByJID(userID types.GroupMeID) *User { +func (br *GMBridge) GetUserByMXIDIfExists(userID id.UserID) *User { + return br.getUserByMXID(userID, true) +} + +func (bridge *GMBridge) GetUserByGMID(gmid types.GroupMeID) *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) GetManagementRoom() id.RoomID { if len(user.ManagementRoom) == 0 { user.mgmtCreateLock.Lock() @@ -618,7 +661,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 +716,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