2022-10-21 14:18:02 +00:00
|
|
|
// mautrix-groupme - A Matrix-GroupMe puppeting bridge.
|
|
|
|
// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra
|
2018-08-16 21:11:28 +00:00
|
|
|
//
|
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2022-10-21 22:04:21 +00:00
|
|
|
"sync"
|
2018-08-24 16:46:14 +00:00
|
|
|
|
2019-01-11 19:17:31 +00:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2020-05-08 19:32:22 +00:00
|
|
|
|
2022-10-21 21:05:58 +00:00
|
|
|
"github.com/beeper/groupme-lib"
|
|
|
|
|
2020-05-09 11:31:06 +00:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
2022-10-21 19:02:33 +00:00
|
|
|
"maunium.net/go/mautrix/bridge"
|
2020-05-08 19:32:22 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
2019-01-11 19:17:31 +00:00
|
|
|
|
2022-10-21 14:48:03 +00:00
|
|
|
"github.com/beeper/groupme/database"
|
2018-08-16 21:11:28 +00:00
|
|
|
)
|
|
|
|
|
2020-06-25 20:33:11 +00:00
|
|
|
var userIDRegex *regexp.Regexp
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) ParsePuppetMXID(mxid id.UserID) (groupme.ID, bool) {
|
2020-06-25 20:33:11 +00:00
|
|
|
if userIDRegex == nil {
|
|
|
|
userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
|
|
|
|
bridge.Config.Bridge.FormatUsername("([0-9]+)"),
|
|
|
|
bridge.Config.Homeserver.Domain))
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
|
|
|
match := userIDRegex.FindStringSubmatch(string(mxid))
|
2018-08-28 21:40:54 +00:00
|
|
|
if match == nil || len(match) != 2 {
|
|
|
|
return "", false
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
return groupme.ID(match[1]), true
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
|
|
|
|
gmid, ok := bridge.ParsePuppetMXID(mxid)
|
2018-08-16 21:11:28 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
return bridge.GetPuppetByGMID(gmid)
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) GetPuppetByGMID(gmid groupme.ID) *Puppet {
|
2018-08-28 21:40:54 +00:00
|
|
|
bridge.puppetsLock.Lock()
|
|
|
|
defer bridge.puppetsLock.Unlock()
|
2022-10-21 19:02:33 +00:00
|
|
|
puppet, ok := bridge.puppets[gmid]
|
2018-08-16 21:11:28 +00:00
|
|
|
if !ok {
|
2022-10-21 19:02:33 +00:00
|
|
|
dbPuppet := bridge.DB.Puppet.Get(gmid)
|
2018-08-16 21:11:28 +00:00
|
|
|
if dbPuppet == nil {
|
2018-08-28 21:40:54 +00:00
|
|
|
dbPuppet = bridge.DB.Puppet.New()
|
2022-10-21 19:02:33 +00:00
|
|
|
dbPuppet.GMID = gmid
|
2018-08-18 19:57:08 +00:00
|
|
|
dbPuppet.Insert()
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
2018-08-28 21:40:54 +00:00
|
|
|
puppet = bridge.NewPuppet(dbPuppet)
|
2022-10-21 19:02:33 +00:00
|
|
|
bridge.puppets[puppet.GMID] = puppet
|
2019-05-23 23:33:26 +00:00
|
|
|
if len(puppet.CustomMXID) > 0 {
|
|
|
|
bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
|
2019-05-23 23:33:26 +00:00
|
|
|
bridge.puppetsLock.Lock()
|
|
|
|
defer bridge.puppetsLock.Unlock()
|
|
|
|
puppet, ok := bridge.puppetsByCustomMXID[mxid]
|
|
|
|
if !ok {
|
|
|
|
dbPuppet := bridge.DB.Puppet.GetByCustomMXID(mxid)
|
|
|
|
if dbPuppet == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
puppet = bridge.NewPuppet(dbPuppet)
|
2022-10-21 19:02:33 +00:00
|
|
|
bridge.puppets[puppet.GMID] = puppet
|
2019-05-23 23:33:26 +00:00
|
|
|
bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (user *User) GetIDoublePuppet() bridge.DoublePuppet {
|
|
|
|
p := user.bridge.GetPuppetByCustomMXID(user.MXID)
|
|
|
|
if p == nil || p.CustomIntent() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) GetIGhost() bridge.Ghost {
|
|
|
|
if user.GMID.String() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p := user.bridge.GetPuppetByGMID(user.GMID)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *GMBridge) IsGhost(id id.UserID) bool {
|
|
|
|
_, ok := br.ParsePuppetMXID(id)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *GMBridge) GetIGhost(id id.UserID) bridge.Ghost {
|
|
|
|
p := br.GetPuppetByMXID(id)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) GetMXID() id.UserID {
|
|
|
|
return puppet.MXID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *GMBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
|
2019-05-23 23:33:26 +00:00
|
|
|
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID())
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) GetAllPuppets() []*Puppet {
|
2019-05-23 23:33:26 +00:00
|
|
|
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll())
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
|
2018-08-28 21:40:54 +00:00
|
|
|
bridge.puppetsLock.Lock()
|
|
|
|
defer bridge.puppetsLock.Unlock()
|
2018-08-16 21:11:28 +00:00
|
|
|
output := make([]*Puppet, len(dbPuppets))
|
|
|
|
for index, dbPuppet := range dbPuppets {
|
2019-06-13 18:30:38 +00:00
|
|
|
if dbPuppet == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-21 19:02:33 +00:00
|
|
|
puppet, ok := bridge.puppets[dbPuppet.GMID]
|
2018-08-16 21:11:28 +00:00
|
|
|
if !ok {
|
2018-08-28 21:40:54 +00:00
|
|
|
puppet = bridge.NewPuppet(dbPuppet)
|
2022-10-21 19:02:33 +00:00
|
|
|
bridge.puppets[dbPuppet.GMID] = puppet
|
2019-05-30 14:22:03 +00:00
|
|
|
if len(dbPuppet.CustomMXID) > 0 {
|
2019-05-23 23:33:26 +00:00
|
|
|
bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
|
|
|
|
}
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
|
|
|
output[index] = puppet
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) FormatPuppetMXID(gmid groupme.ID) id.UserID {
|
2020-08-22 10:07:55 +00:00
|
|
|
return id.NewUserID(
|
2022-10-21 22:04:21 +00:00
|
|
|
bridge.Config.Bridge.FormatUsername(gmid.String()),
|
2020-08-22 10:07:55 +00:00
|
|
|
bridge.Config.Homeserver.Domain)
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (bridge *GMBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
|
2018-08-16 21:11:28 +00:00
|
|
|
return &Puppet{
|
|
|
|
Puppet: dbPuppet,
|
2018-08-28 21:40:54 +00:00
|
|
|
bridge: bridge,
|
2022-10-21 19:02:33 +00:00
|
|
|
log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.GMID)),
|
2018-08-18 19:57:08 +00:00
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
MXID: bridge.FormatPuppetMXID(dbPuppet.GMID),
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Puppet struct {
|
|
|
|
*database.Puppet
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
bridge *GMBridge
|
2018-08-16 21:11:28 +00:00
|
|
|
log log.Logger
|
2018-08-18 19:57:08 +00:00
|
|
|
|
2020-05-08 19:32:22 +00:00
|
|
|
typingIn id.RoomID
|
2018-08-24 19:06:17 +00:00
|
|
|
typingAt int64
|
2018-08-24 17:02:38 +00:00
|
|
|
|
2020-05-08 19:32:22 +00:00
|
|
|
MXID id.UserID
|
2019-05-23 23:33:26 +00:00
|
|
|
|
2019-05-30 14:22:03 +00:00
|
|
|
customIntent *appservice.IntentAPI
|
2020-05-08 19:32:22 +00:00
|
|
|
customTypingIn map[id.RoomID]bool
|
2019-05-30 14:22:03 +00:00
|
|
|
customUser *User
|
2022-10-21 22:04:21 +00:00
|
|
|
|
|
|
|
syncLock sync.Mutex
|
2018-08-18 19:57:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 21:26:24 +00:00
|
|
|
func (puppet *Puppet) PhoneNumber() string {
|
2022-10-21 22:04:21 +00:00
|
|
|
return puppet.GMID.String()
|
2018-08-25 21:26:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 23:33:26 +00:00
|
|
|
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
|
2022-10-21 22:04:21 +00:00
|
|
|
if puppet.customIntent == nil || portal.Key.GMID == puppet.GMID {
|
2019-05-23 23:33:26 +00:00
|
|
|
return puppet.DefaultIntent()
|
|
|
|
}
|
|
|
|
return puppet.customIntent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
|
|
|
|
return puppet.customIntent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
|
2018-08-28 21:40:54 +00:00
|
|
|
return puppet.bridge.AS.Intent(puppet.MXID)
|
2018-08-16 21:11:28 +00:00
|
|
|
}
|
2018-08-19 15:21:38 +00:00
|
|
|
|
2021-04-18 00:57:16 +00:00
|
|
|
//func (puppet *Puppet) SetRoomMetadata(name, avatarURL string) bool {
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
|
2022-10-21 22:04:21 +00:00
|
|
|
func (puppet *Puppet) UpdateAvatar(source *User, forcePortalSync bool) bool {
|
|
|
|
changed := source.updateAvatar(puppet.GMID, &puppet.Avatar, &puppet.AvatarURL, &puppet.AvatarSet, puppet.log, puppet.DefaultIntent())
|
|
|
|
if !changed || puppet.Avatar == "unauthorized" {
|
|
|
|
if forcePortalSync {
|
|
|
|
go puppet.updatePortalAvatar()
|
2021-02-23 05:22:16 +00:00
|
|
|
}
|
2022-10-21 22:04:21 +00:00
|
|
|
return changed
|
2021-02-23 05:22:16 +00:00
|
|
|
}
|
2022-10-21 22:04:21 +00:00
|
|
|
err := puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
|
2021-02-23 05:22:16 +00:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Warnln("Failed to set avatar:", err)
|
2022-10-21 22:04:21 +00:00
|
|
|
} else {
|
|
|
|
puppet.AvatarSet = true
|
2021-02-23 05:22:16 +00:00
|
|
|
}
|
|
|
|
go puppet.updatePortalAvatar()
|
2018-08-22 22:12:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-21 22:04:21 +00:00
|
|
|
func (puppet *Puppet) UpdateName(member groupme.Member, forcePortalSync bool) bool {
|
|
|
|
newName := puppet.bridge.Config.Bridge.FormatDisplayname(puppet.GMID, member)
|
|
|
|
if puppet.Displayname != newName || !puppet.NameSet {
|
|
|
|
oldName := puppet.Displayname
|
|
|
|
puppet.Displayname = newName
|
|
|
|
puppet.NameSet = false
|
|
|
|
err := puppet.DefaultIntent().SetDisplayName(newName)
|
2018-08-22 22:12:26 +00:00
|
|
|
if err == nil {
|
2022-10-21 22:04:21 +00:00
|
|
|
puppet.log.Debugln("Updated name", oldName, "->", newName)
|
|
|
|
puppet.NameSet = true
|
2019-06-01 17:03:29 +00:00
|
|
|
go puppet.updatePortalName()
|
2018-08-22 22:12:26 +00:00
|
|
|
} else {
|
|
|
|
puppet.log.Warnln("Failed to set display name:", err)
|
|
|
|
}
|
2019-06-01 17:03:29 +00:00
|
|
|
return true
|
2022-10-21 22:04:21 +00:00
|
|
|
} else if forcePortalSync {
|
|
|
|
go puppet.updatePortalName()
|
2019-06-01 17:03:29 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
|
2022-10-21 22:04:21 +00:00
|
|
|
if puppet.bridge.Config.Bridge.PrivateChatPortalMeta || puppet.bridge.Config.Bridge.Encryption.Allow {
|
|
|
|
for _, portal := range puppet.bridge.GetAllPortalsByGMID(puppet.GMID) {
|
|
|
|
if !puppet.bridge.Config.Bridge.PrivateChatPortalMeta && !portal.Encrypted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Get room create lock to prevent races between receiving contact info and room creation.
|
|
|
|
portal.roomCreateLock.Lock()
|
2019-06-01 17:03:29 +00:00
|
|
|
meta(portal)
|
2022-10-21 22:04:21 +00:00
|
|
|
portal.roomCreateLock.Unlock()
|
2019-06-01 17:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) updatePortalAvatar() {
|
|
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
2022-10-21 22:04:21 +00:00
|
|
|
if portal.Avatar == puppet.Avatar && portal.AvatarURL == puppet.AvatarURL && portal.AvatarSet {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
portal.AvatarURL = puppet.AvatarURL
|
|
|
|
portal.Avatar = puppet.Avatar
|
|
|
|
portal.AvatarSet = false
|
|
|
|
defer portal.Update(nil)
|
2019-06-01 17:03:29 +00:00
|
|
|
if len(portal.MXID) > 0 {
|
2022-10-21 22:04:21 +00:00
|
|
|
_, err := portal.MainIntent().SetRoomAvatar(portal.MXID, puppet.AvatarURL)
|
2019-06-01 17:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to set avatar:", err)
|
2022-10-21 22:04:21 +00:00
|
|
|
} else {
|
|
|
|
portal.AvatarSet = true
|
|
|
|
portal.UpdateBridgeInfo()
|
2019-06-01 17:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) updatePortalName() {
|
|
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
2022-10-21 22:04:21 +00:00
|
|
|
portal.UpdateName(puppet.Displayname, groupme.ID(""), true)
|
2019-06-01 17:03:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-21 22:04:21 +00:00
|
|
|
func (puppet *Puppet) Sync(source *User, member *groupme.Member, forceAvatarSync, forcePortalSync bool) {
|
|
|
|
puppet.syncLock.Lock()
|
|
|
|
defer puppet.syncLock.Unlock()
|
2021-02-22 03:46:17 +00:00
|
|
|
err := puppet.DefaultIntent().EnsureRegistered()
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Errorln("Failed to ensure registered:", err)
|
|
|
|
}
|
2021-02-13 05:53:35 +00:00
|
|
|
|
2022-10-21 22:04:21 +00:00
|
|
|
puppet.log.Debugfln("Syncing info through %s", source.GMID)
|
|
|
|
|
|
|
|
// TODO
|
2018-08-19 15:21:38 +00:00
|
|
|
}
|