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 (
|
|
|
|
"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
|
|
|
|
2023-09-19 03:15:46 +00:00
|
|
|
"gitea.watsonlabs.net/watsonb8/groupme-lib"
|
2022-10-21 21:05:58 +00:00
|
|
|
|
2020-05-09 11:31:06 +00:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
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
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-11 21:44:58 +00:00
|
|
|
func (puppet *Puppet) ClearCustomMXID() {
|
|
|
|
//TODO implement me
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Public Properties
|
|
|
|
|
|
|
|
func (puppet *Puppet) GetMXID() id.UserID {
|
|
|
|
return puppet.MXID
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Public Methods
|
|
|
|
|
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
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-07 03:17:06 +00:00
|
|
|
func (puppet *Puppet) Sync(source *User, member *groupme.Member, forceAvatarSync bool, forcePortalSync bool) {
|
2022-10-21 22:04:21 +00:00
|
|
|
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
|
|
|
|
2023-09-07 03:17:06 +00:00
|
|
|
update := false
|
|
|
|
update = puppet.UpdateName(*member, forcePortalSync) || update
|
|
|
|
update = puppet.UpdateAvatar(source, forcePortalSync) || update
|
|
|
|
if update {
|
|
|
|
puppet.Update()
|
|
|
|
}
|
2018-08-19 15:21:38 +00:00
|
|
|
}
|