diff --git a/commands.go b/commands.go index 68f5d63..7d72d0e 100644 --- a/commands.go +++ b/commands.go @@ -119,8 +119,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandReconnect(ce) case "disconnect": handler.CommandDisconnect(ce) - case "ping": - handler.CommandPing(ce) + // case "ping": + // handler.CommandPing(ce) case "delete-connection": handler.CommandDeleteConnection(ce) case "delete-session": @@ -379,7 +379,7 @@ func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) { } } -const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client` +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) { @@ -492,10 +492,9 @@ func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) { // ce.Reply("Session information purged") } -const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp` +const cmdReconnectHelp = `reconnect - Reconnect to GroupMe` func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { - fmt.Println(ce.User.Conn) if ce.User.Conn == nil { if len(ce.User.Token) == 0 { ce.Reply("No existing connection and no token. Did you mean `login`?") @@ -506,8 +505,11 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { return } - // wasConnected := true - // sess, err := ce.User.Conn.Disconnect() + 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 { @@ -553,14 +555,19 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { // 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) + var msg string + if wasConnected { + msg = "Reconnected successfully." + } else { + msg = "Connected successfully." + } + ce.Reply(msg) // ce.User.PostLogin() } @@ -635,35 +642,36 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { ce.Reply("* " + strings.Join([]string{ cmdPrefix + cmdHelpHelp, cmdPrefix + cmdLoginHelp, - cmdPrefix + cmdLogoutHelp, - cmdPrefix + cmdDeleteSessionHelp, + // cmdPrefix + cmdLogoutHelp, + // cmdPrefix + cmdDeleteSessionHelp, cmdPrefix + cmdReconnectHelp, - cmdPrefix + cmdDisconnectHelp, - cmdPrefix + cmdDeleteConnectionHelp, - cmdPrefix + cmdPingHelp, - cmdPrefix + cmdLoginMatrixHelp, - cmdPrefix + cmdLogoutMatrixHelp, - cmdPrefix + cmdToggleHelp, + // 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 + cmdPMHelp, + // cmdPrefix + cmdInviteLinkHelp, + // cmdPrefix + cmdJoinHelp, + // cmdPrefix + cmdCreateHelp, + // cmdPrefix + cmdSetPowerLevelHelp, cmdPrefix + cmdDeletePortalHelp, cmdPrefix + cmdDeleteAllPortalsHelp, }, "\n* ")) } -const cmdSyncHelp = `sync [--create-all] - Synchronize contacts from phone and optionally create portals for group chats.` +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" + 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() @@ -685,7 +693,7 @@ func (handler *CommandHandler) CommandSync(ce *CommandEvent) { // ce.Reply("Syncing chats...") // user.syncPortals(nil, create) - // ce.Reply("Sync complete.") + 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.` @@ -754,7 +762,9 @@ func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) { }() } -const cmdListHelp = `list [page] [items per page] - Get a list of all contacts and groups.` +const cmdListHelp = `list groups [page] [items per page] - Get a list of all contacts and groups.` + +// //TODO func formatContacts(contacts bool, input map[string]string) (result []string) { for jid, contact := range input { diff --git a/database/portal.go b/database/portal.go index 2837a64..c091d47 100644 --- a/database/portal.go +++ b/database/portal.go @@ -173,13 +173,12 @@ func (portal *Portal) Delete() { } func (portal *Portal) GetUserIDs() []id.UserID { - println("HI AAAAAAAAAAAAAAAAAAAa") - rows, err := portal.db.Raw(`SELECT "user".mxid FROM "user", user_portal - WHERE "user".jid=user_portal.user_jid - AND user_portal.portal_jid=$1 - AND user_portal.portal_receiver=$2`, + //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() - print("maybe maybe sql 760476084") if err != nil { portal.log.Debugln("Failed to get portal user ids:", err) return nil diff --git a/database/user.go b/database/user.go index f15866a..08e397c 100644 --- a/database/user.go +++ b/database/user.go @@ -222,7 +222,7 @@ func (user *User) GetPortalKeys() []PortalKey { var keys []PortalKey for _, i := range up { key := PortalKey{ - JID: i.UserJID, + JID: i.PortalJID, Receiver: i.PortalReceiver, } keys = append(keys, key) diff --git a/portal.go b/portal.go index 14a7249..65be844 100644 --- a/portal.go +++ b/portal.go @@ -19,7 +19,6 @@ package main import ( "bytes" "context" - "encoding/gob" "encoding/hex" "errors" "fmt" @@ -43,7 +42,6 @@ import ( "maunium.net/go/mautrix/crypto/attachment" "github.com/Rhymen/go-whatsapp" - waProto "github.com/Rhymen/go-whatsapp/binary/proto" "github.com/karmanyaahm/groupme" "maunium.net/go/mautrix" @@ -131,7 +129,7 @@ func (bridge *Bridge) NewManualPortal(key database.PortalKey) *Portal { bridge: bridge, log: bridge.Log.Sub(fmt.Sprintf("Portal/%s", key)), - recentlyHandled: [recentlyHandledLength]string{}, + recentlyHandled: make([]string, recentlyHandledLength), messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer), } @@ -146,7 +144,7 @@ func (bridge *Bridge) NewPortal(dbPortal *database.Portal) *Portal { bridge: bridge, log: bridge.Log.Sub(fmt.Sprintf("Portal/%s", dbPortal.Key)), - recentlyHandled: [recentlyHandledLength]string{}, + recentlyHandled: make([]string, recentlyHandledLength), messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer), } @@ -172,7 +170,7 @@ type Portal struct { roomCreateLock sync.Mutex - recentlyHandled [recentlyHandledLength]string + recentlyHandled []string recentlyHandledLock sync.Mutex recentlyHandledIndex uint8 @@ -266,12 +264,8 @@ func (portal *Portal) handleMessage(msg PortalMessage) { } func (portal *Portal) isRecentlyHandled(id groupme.ID) bool { - start := int(portal.recentlyHandledIndex) //int bc -1 loops over to 255 in uint8 idStr := id.String() - for i := (start - 1) % recentlyHandledLength; i != start; i = (i - 1) % recentlyHandledLength { - if i < 0 { //do this to get modulo not remainder - i += recentlyHandledLength - } + for i := recentlyHandledLength - 1; i >= 0; i-- { if portal.recentlyHandled[i] == idStr { return true } @@ -289,7 +283,6 @@ func (portal *Portal) isDuplicate(id groupme.ID) bool { } func init() { - gob.Register(&waProto.Message{}) } func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid id.EventID) { @@ -313,10 +306,10 @@ func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid i msg.Insert() portal.recentlyHandledLock.Lock() - index := portal.recentlyHandledIndex - portal.recentlyHandledIndex = (portal.recentlyHandledIndex + 1) % recentlyHandledLength + portal.recentlyHandled[0] = "" //FIFO queue being implemented here //TODO: is this efficent + portal.recentlyHandled = portal.recentlyHandled[1:] + portal.recentlyHandled = append(portal.recentlyHandled, message.ID.String()) portal.recentlyHandledLock.Unlock() - portal.recentlyHandled[index] = message.ID.String() } func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appservice.IntentAPI { @@ -393,7 +386,7 @@ func (portal *Portal) SyncParticipants(metadata *groupme.Group) { if user != nil { changed = levels.EnsureUserLevel(user.MXID, expectedLevel) || changed } - puppet.Sync(nil, *participant) //why nil whynot + go puppet.Sync(nil, *participant) //why nil whynot } if changed { _, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels) @@ -423,7 +416,7 @@ func (portal *Portal) SyncParticipants(metadata *groupme.Group) { } } -func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInfo, updateInfo bool) bool { +func (portal *Portal) UpdateAvatar(user *User, avatar string, updateInfo bool) bool { // if avatar == nil { // var err error // avatar, err = user.Conn.GetProfilePicThumb(portal.Key.JID) @@ -599,7 +592,7 @@ func (portal *Portal) Sync(user *User, group groupme.Group) { update := false update = portal.UpdateMetadata(user) || update if !portal.IsStatusBroadcastRoom() { - update = portal.UpdateAvatar(user, nil, false) || update + update = portal.UpdateAvatar(user, "", false) || update } if update { portal.Update() @@ -984,7 +977,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { portal.Name = metadata.Name portal.Topic = metadata.Description } - portal.UpdateAvatar(user, nil, false) + portal.UpdateAvatar(user, "", false) } bridgeInfoStateKey, bridgeInfo := portal.getBridgeInfo() diff --git a/user.go b/user.go index a2a1cf9..471e233 100644 --- a/user.go +++ b/user.go @@ -362,6 +362,10 @@ func (user *User) Login(ce *CommandEvent) { // // Also between the two logout methods (commands.go and provisioning.go) // user.ConnectionErrors = 0 // user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) + if len(ce.Args) == 0 { + ce.Reply(`Get your access token from https://dev.groupme.com/ which should be the first argument to login`) + return + } user.Token = ce.Args[0] user.addToJIDMap()