groupme/puppet.go

180 lines
4.9 KiB
Go
Raw Normal View History

// 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 <https://www.gnu.org/licenses/>.
package main
import (
"regexp"
"sync"
2019-01-11 19:17:31 +00:00
log "maunium.net/go/maulogger/v2"
2020-05-08 19:32:22 +00:00
"gitea.watsonlabs.net/watsonb8/groupme-lib"
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
"github.com/beeper/groupme/database"
)
var userIDRegex *regexp.Regexp
type Puppet struct {
*database.Puppet
bridge *GMBridge
log log.Logger
2020-05-08 19:32:22 +00:00
typingIn id.RoomID
typingAt int64
2020-05-08 19:32:22 +00:00
MXID id.UserID
customIntent *appservice.IntentAPI
2020-05-08 19:32:22 +00:00
customTypingIn map[id.RoomID]bool
customUser *User
syncLock sync.Mutex
}
func (puppet *Puppet) ClearCustomMXID() {
//TODO implement me
panic("implement me")
}
// Public Properties
func (puppet *Puppet) GetMXID() id.UserID {
return puppet.MXID
}
func (puppet *Puppet) PhoneNumber() string {
return puppet.GMID.String()
}
// Public Methods
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
if puppet.customIntent == nil || portal.Key.GMID == puppet.GMID {
return puppet.DefaultIntent()
}
return puppet.customIntent
}
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
return puppet.customIntent
}
func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
return puppet.bridge.AS.Intent(puppet.MXID)
}
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()
}
return changed
}
err := puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
if err != nil {
puppet.log.Warnln("Failed to set avatar:", err)
} else {
puppet.AvatarSet = true
}
go puppet.updatePortalAvatar()
return true
}
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)
if err == nil {
puppet.log.Debugln("Updated name", oldName, "->", newName)
puppet.NameSet = true
go puppet.updatePortalName()
} else {
puppet.log.Warnln("Failed to set display name:", err)
}
return true
} else if forcePortalSync {
go puppet.updatePortalName()
}
return false
}
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
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()
meta(portal)
portal.roomCreateLock.Unlock()
}
}
}
func (puppet *Puppet) updatePortalAvatar() {
puppet.updatePortalMeta(func(portal *Portal) {
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)
if len(portal.MXID) > 0 {
_, err := portal.MainIntent().SetRoomAvatar(portal.MXID, puppet.AvatarURL)
if err != nil {
portal.log.Warnln("Failed to set avatar:", err)
} else {
portal.AvatarSet = true
portal.UpdateBridgeInfo()
}
}
})
}
func (puppet *Puppet) updatePortalName() {
puppet.updatePortalMeta(func(portal *Portal) {
portal.UpdateName(puppet.Displayname, groupme.ID(""), true)
})
}
2023-09-07 03:17:06 +00:00
func (puppet *Puppet) Sync(source *User, member *groupme.Member, forceAvatarSync bool, 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
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()
}
}