2023-07-02 14:21:55 +00:00
|
|
|
// mautrix-gmessages - A Matrix-Google Messages puppeting bridge.
|
|
|
|
// Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-04 22:18:01 +00:00
|
|
|
"crypto/sha256"
|
2023-07-02 14:21:55 +00:00
|
|
|
"fmt"
|
2023-09-04 22:18:01 +00:00
|
|
|
"net/http"
|
2023-07-02 14:21:55 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2023-09-04 22:18:01 +00:00
|
|
|
"time"
|
2023-07-02 14:21:55 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/appservice"
|
|
|
|
"maunium.net/go/mautrix/bridge"
|
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
|
|
|
|
"go.mau.fi/mautrix-gmessages/database"
|
2023-07-17 13:51:31 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-07-02 14:21:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var userIDRegex *regexp.Regexp
|
|
|
|
|
|
|
|
func (br *GMBridge) ParsePuppetMXID(mxid id.UserID) (key database.Key, ok bool) {
|
|
|
|
if userIDRegex == nil {
|
|
|
|
userIDRegex = br.Config.MakeUserIDRegex(`([0-9]+)\.([0-9]+)`)
|
|
|
|
}
|
|
|
|
match := userIDRegex.FindStringSubmatch(string(mxid))
|
|
|
|
if len(match) == 3 {
|
|
|
|
var err error
|
|
|
|
key.Receiver, err = strconv.Atoi(match[1])
|
|
|
|
ok = err == nil
|
|
|
|
key.ID = match[2]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *GMBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
|
|
|
|
key, ok := br.ParsePuppetMXID(mxid)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return br.GetPuppetByKey(key, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *GMBridge) GetPuppetByKey(key database.Key, phone string) *Puppet {
|
|
|
|
br.puppetsLock.Lock()
|
|
|
|
defer br.puppetsLock.Unlock()
|
|
|
|
puppet, ok := br.puppetsByKey[key]
|
|
|
|
if !ok {
|
|
|
|
dbPuppet, err := br.DB.Puppet.Get(context.TODO(), key)
|
|
|
|
if err != nil {
|
|
|
|
br.ZLog.Err(err).Object("puppet_key", key).Msg("Failed to get puppet from database")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if dbPuppet == nil {
|
2023-07-03 13:16:53 +00:00
|
|
|
if phone == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-02 14:21:55 +00:00
|
|
|
dbPuppet = br.DB.Puppet.New()
|
|
|
|
dbPuppet.Key = key
|
|
|
|
dbPuppet.Phone = phone
|
|
|
|
err = dbPuppet.Insert(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
br.ZLog.Err(err).Object("puppet_key", key).Msg("Failed to insert puppet into database")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppetsByKey[puppet.Key] = puppet
|
|
|
|
}
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (br *GMBridge) loadManyPuppets(query func(ctx context.Context) ([]*database.Puppet, error)) []*Puppet {
|
|
|
|
br.puppetsLock.Lock()
|
|
|
|
defer br.puppetsLock.Unlock()
|
|
|
|
dbPuppets, err := query(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
br.ZLog.Err(err).Msg("Failed to load all puppets from database")
|
|
|
|
return []*Puppet{}
|
|
|
|
}
|
|
|
|
output := make([]*Puppet, len(dbPuppets))
|
|
|
|
for index, dbPuppet := range dbPuppets {
|
|
|
|
if dbPuppet == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
puppet, ok := br.puppetsByKey[dbPuppet.Key]
|
|
|
|
if !ok {
|
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppetsByKey[puppet.Key] = puppet
|
|
|
|
}
|
|
|
|
output[index] = puppet
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *GMBridge) FormatPuppetMXID(key database.Key) id.UserID {
|
|
|
|
return id.NewUserID(
|
|
|
|
br.Config.Bridge.FormatUsername(key.String()),
|
|
|
|
br.Config.Homeserver.Domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *GMBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
|
|
|
|
return &Puppet{
|
|
|
|
Puppet: dbPuppet,
|
|
|
|
bridge: br,
|
2023-07-19 22:54:30 +00:00
|
|
|
log: br.ZLog.With().
|
|
|
|
Str("phone", dbPuppet.Phone).
|
|
|
|
Str("puppet_id", dbPuppet.ID).
|
|
|
|
Int("puppet_receiver", dbPuppet.Receiver).
|
|
|
|
Logger(),
|
|
|
|
MXID: br.FormatPuppetMXID(dbPuppet.Key),
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Puppet struct {
|
|
|
|
*database.Puppet
|
|
|
|
bridge *GMBridge
|
|
|
|
log zerolog.Logger
|
|
|
|
MXID id.UserID
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ bridge.GhostWithProfile = (*Puppet)(nil)
|
|
|
|
|
|
|
|
func (puppet *Puppet) GetDisplayname() string {
|
|
|
|
return puppet.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) GetAvatarURL() id.ContentURI {
|
|
|
|
return puppet.AvatarMXC
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) SwitchCustomMXID(_ string, _ id.UserID) error {
|
|
|
|
return fmt.Errorf("puppets don't support custom MXIDs here")
|
|
|
|
}
|
|
|
|
|
2023-08-21 10:52:03 +00:00
|
|
|
func (puppet *Puppet) ClearCustomMXID() {}
|
|
|
|
|
2023-07-02 14:21:55 +00:00
|
|
|
func (puppet *Puppet) IntentFor(_ *Portal) *appservice.IntentAPI {
|
|
|
|
return puppet.DefaultIntent()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
|
|
|
|
return puppet.bridge.AS.Intent(puppet.MXID)
|
|
|
|
}
|
|
|
|
|
2023-09-04 22:18:01 +00:00
|
|
|
const MinAvatarUpdateInterval = 24 * time.Hour
|
|
|
|
|
2024-02-23 19:10:31 +00:00
|
|
|
func (puppet *Puppet) UpdateAvatar(ctx context.Context, source *User) bool {
|
2024-03-18 18:23:43 +00:00
|
|
|
if (puppet.AvatarSet && time.Since(puppet.AvatarUpdateTS) < MinAvatarUpdateInterval) || (puppet.ContactID == "" && puppet.AvatarMXC.IsEmpty()) {
|
2023-07-02 14:21:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-03-18 18:23:43 +00:00
|
|
|
var resp *gmproto.GetThumbnailResponse
|
|
|
|
var err error
|
|
|
|
if puppet.ContactID != "" {
|
|
|
|
resp, err = source.Client.GetParticipantThumbnail(puppet.ID)
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Err(err).Msg("Failed to get avatar thumbnail")
|
|
|
|
return false
|
|
|
|
}
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
2023-09-04 22:18:01 +00:00
|
|
|
puppet.AvatarUpdateTS = time.Now()
|
2024-03-18 18:23:43 +00:00
|
|
|
if resp == nil || len(resp.Thumbnail) == 0 {
|
2024-03-01 12:43:25 +00:00
|
|
|
if puppet.AvatarHash == [32]byte{} && puppet.AvatarMXC.IsEmpty() && puppet.AvatarSet {
|
2023-09-04 22:18:01 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
puppet.AvatarHash = [32]byte{}
|
|
|
|
puppet.AvatarMXC = id.ContentURI{}
|
|
|
|
puppet.AvatarSet = false
|
2024-03-01 12:43:25 +00:00
|
|
|
puppet.log.Debug().Msg("Clearing user avatar")
|
2023-09-04 22:18:01 +00:00
|
|
|
} else {
|
|
|
|
thumbData := resp.Thumbnail[0].GetData()
|
|
|
|
hash := sha256.Sum256(thumbData.GetImageBuffer())
|
2023-09-04 22:34:47 +00:00
|
|
|
if hash == puppet.AvatarHash && puppet.AvatarSet {
|
2023-09-04 22:18:01 +00:00
|
|
|
return true
|
|
|
|
}
|
2024-03-01 12:43:25 +00:00
|
|
|
puppet.log.Debug().Hex("avatar_hash", hash[:]).Msg("Uploading new user avatar")
|
2023-09-04 22:18:01 +00:00
|
|
|
puppet.AvatarHash = hash
|
|
|
|
puppet.AvatarSet = false
|
|
|
|
avatarBytes := thumbData.GetImageBuffer()
|
2024-02-23 19:10:31 +00:00
|
|
|
uploadResp, err := puppet.DefaultIntent().UploadMedia(ctx, mautrix.ReqUploadMedia{
|
2023-09-04 22:18:01 +00:00
|
|
|
ContentBytes: avatarBytes,
|
|
|
|
ContentType: http.DetectContentType(avatarBytes),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Err(err).Msg("Failed to upload avatar")
|
|
|
|
return true
|
|
|
|
}
|
2024-03-01 12:43:25 +00:00
|
|
|
puppet.log.Debug().
|
|
|
|
Hex("avatar_hash", hash[:]).
|
|
|
|
Stringer("mxc", uploadResp.ContentURI).
|
|
|
|
Msg("Uploaded new user avatar")
|
2023-09-04 22:18:01 +00:00
|
|
|
puppet.AvatarMXC = uploadResp.ContentURI
|
|
|
|
}
|
2024-02-23 19:10:31 +00:00
|
|
|
err = puppet.DefaultIntent().SetAvatarURL(ctx, puppet.AvatarMXC)
|
2023-07-02 14:21:55 +00:00
|
|
|
if err != nil {
|
2023-09-04 22:18:01 +00:00
|
|
|
puppet.log.Err(err).Msg("Failed to set avatar")
|
2023-07-02 14:21:55 +00:00
|
|
|
} else {
|
|
|
|
puppet.AvatarSet = true
|
2024-03-01 12:43:25 +00:00
|
|
|
puppet.log.Debug().Stringer("mxc", puppet.AvatarMXC).Msg("Updated avatar")
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
2024-02-23 19:10:31 +00:00
|
|
|
go puppet.updatePortalAvatar(ctx)
|
2023-07-02 14:21:55 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-02-23 19:10:31 +00:00
|
|
|
func (puppet *Puppet) updatePortalAvatar(ctx context.Context) {
|
2023-09-04 22:34:47 +00:00
|
|
|
portal := puppet.bridge.GetPortalByOtherUser(puppet.Key)
|
|
|
|
if portal == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
portal.roomCreateLock.Lock()
|
|
|
|
defer portal.roomCreateLock.Unlock()
|
|
|
|
if portal.MXID == "" || !portal.shouldSetDMRoomMetadata() {
|
|
|
|
return
|
|
|
|
}
|
2024-02-23 19:10:31 +00:00
|
|
|
_, err := portal.MainIntent().SetRoomAvatar(ctx, portal.MXID, puppet.AvatarMXC)
|
2023-09-04 22:34:47 +00:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Err(err).Str("room_id", portal.MXID.String()).Msg("Failed to update DM room avatar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 19:10:31 +00:00
|
|
|
func (puppet *Puppet) UpdateName(ctx context.Context, formattedPhone, fullName, firstName string) bool {
|
2023-07-02 14:21:55 +00:00
|
|
|
newName := puppet.bridge.Config.Bridge.FormatDisplayname(formattedPhone, fullName, firstName)
|
|
|
|
if puppet.Name != newName || !puppet.NameSet {
|
|
|
|
oldName := puppet.Name
|
|
|
|
puppet.Name = newName
|
|
|
|
puppet.NameSet = false
|
2024-02-23 19:10:31 +00:00
|
|
|
err := puppet.DefaultIntent().SetDisplayName(ctx, newName)
|
2023-07-02 14:21:55 +00:00
|
|
|
if err == nil {
|
|
|
|
puppet.log.Debug().Str("old_name", oldName).Str("new_name", newName).Msg("Updated displayname")
|
|
|
|
puppet.NameSet = true
|
|
|
|
} else {
|
|
|
|
puppet.log.Warn().Err(err).Msg("Failed to set displayname")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-23 19:10:31 +00:00
|
|
|
func (puppet *Puppet) UpdateContactInfo(ctx context.Context) bool {
|
2023-07-02 14:21:55 +00:00
|
|
|
if !puppet.bridge.SpecVersions.Supports(mautrix.BeeperFeatureArbitraryProfileMeta) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if puppet.ContactInfoSet {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-03-18 13:12:56 +00:00
|
|
|
idents := make([]string, 0, 2)
|
|
|
|
if puppet.Phone != "" {
|
|
|
|
idents = append(idents, fmt.Sprintf("tel:%s", puppet.Phone))
|
2024-02-29 13:50:19 +00:00
|
|
|
}
|
|
|
|
if puppet.ContactID != "" {
|
|
|
|
idents = append(idents, fmt.Sprintf("gmsg-contact:%s", puppet.ContactID))
|
|
|
|
}
|
|
|
|
|
2023-07-02 14:21:55 +00:00
|
|
|
contactInfo := map[string]any{
|
2024-02-29 13:50:19 +00:00
|
|
|
"com.beeper.bridge.identifiers": idents,
|
|
|
|
"com.beeper.bridge.remote_id": puppet.Key.String(),
|
|
|
|
"com.beeper.bridge.service": "gmessages",
|
|
|
|
"com.beeper.bridge.network": "gmessages",
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
2024-02-23 19:10:31 +00:00
|
|
|
err := puppet.DefaultIntent().BeeperUpdateProfile(ctx, contactInfo)
|
2023-07-02 14:21:55 +00:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Warn().Err(err).Msg("Failed to store custom contact info in profile")
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
puppet.ContactInfoSet = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 19:10:31 +00:00
|
|
|
func (puppet *Puppet) Sync(ctx context.Context, source *User, contact *gmproto.Participant) {
|
|
|
|
err := puppet.DefaultIntent().EnsureRegistered(ctx)
|
2023-07-02 14:21:55 +00:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Err(err).Msg("Failed to ensure registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
update := false
|
2023-09-04 22:18:01 +00:00
|
|
|
if contact.ID.Number != "" && puppet.Phone != contact.ID.Number {
|
2024-03-18 13:12:56 +00:00
|
|
|
oldPhone := puppet.Phone
|
2023-09-04 22:18:01 +00:00
|
|
|
puppet.Phone = contact.ID.Number
|
2024-03-01 12:44:29 +00:00
|
|
|
puppet.ContactInfoSet = false
|
2024-03-18 13:12:56 +00:00
|
|
|
puppet.log = puppet.bridge.ZLog.With().
|
|
|
|
Str("phone", puppet.Phone).
|
|
|
|
Str("puppet_id", puppet.ID).
|
|
|
|
Int("puppet_receiver", puppet.Receiver).
|
|
|
|
Logger()
|
|
|
|
puppet.log.Debug().Str("old_phone", oldPhone).Msg("Phone number changed")
|
2023-09-04 22:18:01 +00:00
|
|
|
update = true
|
|
|
|
}
|
|
|
|
if contact.ContactID != puppet.ContactID {
|
|
|
|
puppet.ContactID = contact.ContactID
|
2024-03-01 12:44:29 +00:00
|
|
|
puppet.ContactInfoSet = false
|
2023-09-04 22:18:01 +00:00
|
|
|
update = true
|
2023-07-02 14:21:55 +00:00
|
|
|
}
|
2024-02-23 19:10:31 +00:00
|
|
|
update = puppet.UpdateName(ctx, contact.GetFormattedNumber(), contact.GetFullName(), contact.GetFirstName()) || update
|
|
|
|
update = puppet.UpdateAvatar(ctx, source) || update
|
|
|
|
update = puppet.UpdateContactInfo(ctx) || update
|
2023-07-02 14:21:55 +00:00
|
|
|
if update {
|
2024-02-23 19:10:31 +00:00
|
|
|
err = puppet.Update(ctx)
|
2023-07-02 14:21:55 +00:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Err(err).Msg("Failed to save puppet to database after sync")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|