2018-08-16 23:11:28 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2020-05-08 21:32:22 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-08-16 23:11:28 +02: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"
|
2018-08-24 18:46:14 +02:00
|
|
|
"net/http"
|
2018-08-16 23:11:28 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-03-19 19:55:08 +01:00
|
|
|
"sync"
|
2018-08-24 18:46:14 +02:00
|
|
|
|
2021-02-09 22:41:14 +01:00
|
|
|
"github.com/Rhymen/go-whatsapp"
|
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2020-05-09 13:31:06 +02:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2018-08-24 18:46:14 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
2018-08-16 23:11:28 +02:00
|
|
|
)
|
|
|
|
|
2020-06-25 22:33:11 +02:00
|
|
|
var userIDRegex *regexp.Regexp
|
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (whatsapp.JID, bool) {
|
2020-06-25 22:33:11 +02:00
|
|
|
if userIDRegex == nil {
|
|
|
|
userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
|
|
|
|
bridge.Config.Bridge.FormatUsername("([0-9]+)"),
|
|
|
|
bridge.Config.Homeserver.Domain))
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
match := userIDRegex.FindStringSubmatch(string(mxid))
|
2018-08-28 23:40:54 +02:00
|
|
|
if match == nil || len(match) != 2 {
|
|
|
|
return "", false
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
jid := whatsapp.JID(match[1] + whatsapp.NewUserSuffix)
|
2018-08-28 23:40:54 +02:00
|
|
|
return jid, true
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (bridge *Bridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
|
2018-08-28 23:40:54 +02:00
|
|
|
jid, ok := bridge.ParsePuppetMXID(mxid)
|
2018-08-16 23:11:28 +02:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
return bridge.GetPuppetByJID(jid)
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
func (bridge *Bridge) GetPuppetByJID(jid whatsapp.JID) *Puppet {
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.puppetsLock.Lock()
|
|
|
|
defer bridge.puppetsLock.Unlock()
|
|
|
|
puppet, ok := bridge.puppets[jid]
|
2018-08-16 23:11:28 +02:00
|
|
|
if !ok {
|
2018-08-28 23:40:54 +02:00
|
|
|
dbPuppet := bridge.DB.Puppet.Get(jid)
|
2018-08-16 23:11:28 +02:00
|
|
|
if dbPuppet == nil {
|
2018-08-28 23:40:54 +02:00
|
|
|
dbPuppet = bridge.DB.Puppet.New()
|
2018-08-18 21:57:08 +02:00
|
|
|
dbPuppet.JID = jid
|
|
|
|
dbPuppet.Insert()
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
puppet = bridge.NewPuppet(dbPuppet)
|
|
|
|
bridge.puppets[puppet.JID] = puppet
|
2019-05-24 01:33:26 +02:00
|
|
|
if len(puppet.CustomMXID) > 0 {
|
|
|
|
bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (bridge *Bridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
|
2019-05-24 01:33:26 +02: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)
|
|
|
|
bridge.puppets[puppet.JID] = puppet
|
|
|
|
bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
2019-05-24 01:33:26 +02:00
|
|
|
func (bridge *Bridge) GetAllPuppetsWithCustomMXID() []*Puppet {
|
|
|
|
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID())
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) GetAllPuppets() []*Puppet {
|
2019-05-24 01:33:26 +02:00
|
|
|
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.puppetsLock.Lock()
|
|
|
|
defer bridge.puppetsLock.Unlock()
|
2018-08-16 23:11:28 +02:00
|
|
|
output := make([]*Puppet, len(dbPuppets))
|
|
|
|
for index, dbPuppet := range dbPuppets {
|
2019-06-13 20:30:38 +02:00
|
|
|
if dbPuppet == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
puppet, ok := bridge.puppets[dbPuppet.JID]
|
2018-08-16 23:11:28 +02:00
|
|
|
if !ok {
|
2018-08-28 23:40:54 +02:00
|
|
|
puppet = bridge.NewPuppet(dbPuppet)
|
|
|
|
bridge.puppets[dbPuppet.JID] = puppet
|
2019-05-30 16:22:03 +02:00
|
|
|
if len(dbPuppet.CustomMXID) > 0 {
|
2019-05-24 01:33:26 +02:00
|
|
|
bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
|
|
|
|
}
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
output[index] = puppet
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
func (bridge *Bridge) FormatPuppetMXID(jid whatsapp.JID) id.UserID {
|
2020-08-22 12:07:55 +02:00
|
|
|
return id.NewUserID(
|
|
|
|
bridge.Config.Bridge.FormatUsername(
|
|
|
|
strings.Replace(
|
|
|
|
jid,
|
2021-02-17 00:21:30 +01:00
|
|
|
whatsapp.NewUserSuffix, "", 1)),
|
2020-08-22 12:07:55 +02:00
|
|
|
bridge.Config.Homeserver.Domain)
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
|
2018-08-16 23:11:28 +02:00
|
|
|
return &Puppet{
|
|
|
|
Puppet: dbPuppet,
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge: bridge,
|
|
|
|
log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.JID)),
|
2018-08-18 21:57:08 +02:00
|
|
|
|
2020-08-22 12:07:55 +02:00
|
|
|
MXID: bridge.FormatPuppetMXID(dbPuppet.JID),
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Puppet struct {
|
|
|
|
*database.Puppet
|
|
|
|
|
|
|
|
bridge *Bridge
|
|
|
|
log log.Logger
|
2018-08-18 21:57:08 +02:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
typingIn id.RoomID
|
2018-08-24 21:06:17 +02:00
|
|
|
typingAt int64
|
2018-08-24 19:02:38 +02:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
MXID id.UserID
|
2019-05-24 01:33:26 +02:00
|
|
|
|
2019-05-30 16:22:03 +02:00
|
|
|
customIntent *appservice.IntentAPI
|
2020-05-08 21:32:22 +02:00
|
|
|
customTypingIn map[id.RoomID]bool
|
2019-05-30 16:22:03 +02:00
|
|
|
customUser *User
|
2021-03-19 19:55:08 +01:00
|
|
|
|
|
|
|
syncLock sync.Mutex
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 23:26:24 +02:00
|
|
|
func (puppet *Puppet) PhoneNumber() string {
|
2021-02-17 00:21:30 +01:00
|
|
|
return strings.Replace(puppet.JID, whatsapp.NewUserSuffix, "", 1)
|
2018-08-25 23:26:24 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 01:33:26 +02:00
|
|
|
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
|
2020-05-21 19:00:00 +02:00
|
|
|
if (!portal.IsPrivateChat() && puppet.customIntent == nil) ||
|
|
|
|
(portal.backfilling && portal.bridge.Config.Bridge.InviteOwnPuppetForBackfilling) ||
|
|
|
|
portal.Key.JID == puppet.JID {
|
2019-05-24 01:33:26 +02: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 23:40:54 +02:00
|
|
|
return puppet.bridge.AS.Intent(puppet.MXID)
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
2018-08-19 17:21:38 +02:00
|
|
|
|
2021-02-17 00:21:30 +01:00
|
|
|
func (puppet *Puppet) UpdateAvatar(source *User, avatar *whatsapp.ProfilePicInfo) bool {
|
2018-08-25 23:39:36 +02:00
|
|
|
if avatar == nil {
|
|
|
|
var err error
|
2018-08-28 23:40:54 +02:00
|
|
|
avatar, err = source.Conn.GetProfilePicThumb(puppet.JID)
|
2018-08-25 23:39:36 +02:00
|
|
|
if err != nil {
|
2019-01-21 22:55:16 +01:00
|
|
|
puppet.log.Warnln("Failed to get avatar:", err)
|
2018-08-25 23:39:36 +02:00
|
|
|
return false
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 22:41:14 +01:00
|
|
|
if avatar.Status == 404 {
|
|
|
|
avatar.Tag = "remove"
|
|
|
|
avatar.Status = 0
|
|
|
|
} else if avatar.Status == 401 && puppet.Avatar != "unauthorized" {
|
|
|
|
puppet.Avatar = "unauthorized"
|
|
|
|
return true
|
2019-05-23 18:07:34 +02:00
|
|
|
}
|
2021-02-09 22:41:14 +01:00
|
|
|
if avatar.Status != 0 || avatar.Tag == puppet.Avatar {
|
2018-08-23 00:12:26 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:41:14 +01:00
|
|
|
if avatar.Tag == "remove" || len(avatar.URL) == 0 {
|
2020-05-08 21:32:22 +02:00
|
|
|
err := puppet.DefaultIntent().SetAvatarURL(id.ContentURI{})
|
2019-01-21 22:55:16 +01:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Warnln("Failed to remove avatar:", err)
|
|
|
|
}
|
2020-05-08 21:32:22 +02:00
|
|
|
puppet.AvatarURL = id.ContentURI{}
|
2018-08-25 23:39:36 +02:00
|
|
|
puppet.Avatar = avatar.Tag
|
2019-06-01 19:03:29 +02:00
|
|
|
go puppet.updatePortalAvatar()
|
2018-08-25 23:39:36 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
data, err := avatar.DownloadBytes()
|
|
|
|
if err != nil {
|
2019-01-21 22:55:16 +01:00
|
|
|
puppet.log.Warnln("Failed to download avatar:", err)
|
2018-08-23 00:12:26 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
mime := http.DetectContentType(data)
|
2019-05-24 01:33:26 +02:00
|
|
|
resp, err := puppet.DefaultIntent().UploadBytes(data, mime)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
2019-01-21 22:55:16 +01:00
|
|
|
puppet.log.Warnln("Failed to upload avatar:", err)
|
2018-08-23 00:12:26 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-01 19:03:29 +02:00
|
|
|
puppet.AvatarURL = resp.ContentURI
|
|
|
|
err = puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
|
2019-01-21 22:55:16 +01:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Warnln("Failed to set avatar:", err)
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
puppet.Avatar = avatar.Tag
|
2019-06-01 19:03:29 +02:00
|
|
|
go puppet.updatePortalAvatar()
|
2018-08-23 00:12:26 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-01 19:03:29 +02:00
|
|
|
func (puppet *Puppet) UpdateName(source *User, contact whatsapp.Contact) bool {
|
2018-09-01 22:38:03 +02:00
|
|
|
newName, quality := puppet.bridge.Config.Bridge.FormatDisplayname(contact)
|
|
|
|
if puppet.Displayname != newName && quality >= puppet.NameQuality {
|
2019-05-24 01:33:26 +02:00
|
|
|
err := puppet.DefaultIntent().SetDisplayName(newName)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err == nil {
|
|
|
|
puppet.Displayname = newName
|
2018-09-01 22:38:03 +02:00
|
|
|
puppet.NameQuality = quality
|
2019-06-01 19:03:29 +02:00
|
|
|
go puppet.updatePortalName()
|
2018-08-23 00:12:26 +02:00
|
|
|
puppet.Update()
|
|
|
|
} else {
|
|
|
|
puppet.log.Warnln("Failed to set display name:", err)
|
|
|
|
}
|
2019-06-01 19:03:29 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
|
|
|
|
if puppet.bridge.Config.Bridge.PrivateChatPortalMeta {
|
|
|
|
for _, portal := range puppet.bridge.GetAllPortalsByJID(puppet.JID) {
|
|
|
|
meta(portal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) updatePortalAvatar() {
|
|
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
_, err := portal.MainIntent().SetRoomAvatar(portal.MXID, puppet.AvatarURL)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to set avatar:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
portal.AvatarURL = puppet.AvatarURL
|
|
|
|
portal.Avatar = puppet.Avatar
|
|
|
|
portal.Update()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) updatePortalName() {
|
|
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
_, err := portal.MainIntent().SetRoomName(portal.MXID, puppet.Displayname)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to set name:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
portal.Name = puppet.Displayname
|
|
|
|
portal.Update()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:55:08 +01:00
|
|
|
func (puppet *Puppet) SyncContactIfNecessary(source *User) {
|
|
|
|
if len(puppet.Displayname) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-19 01:22:01 +02:00
|
|
|
source.Conn.Store.ContactsLock.RLock()
|
2021-03-19 19:55:08 +01:00
|
|
|
contact, ok := source.Conn.Store.Contacts[puppet.JID]
|
2021-06-19 01:22:01 +02:00
|
|
|
source.Conn.Store.ContactsLock.RUnlock()
|
2021-03-19 19:55:08 +01:00
|
|
|
if !ok {
|
2021-04-05 18:24:37 +02:00
|
|
|
puppet.log.Warnfln("No contact info found through %s in SyncContactIfNecessary", source.MXID)
|
2021-03-22 19:49:17 +01:00
|
|
|
contact.JID = puppet.JID
|
|
|
|
// Sync anyway to set a phone number name
|
|
|
|
} else {
|
|
|
|
puppet.log.Debugfln("Syncing contact info through %s / %s because puppet has no displayname", source.MXID, source.JID)
|
2021-03-19 19:55:08 +01:00
|
|
|
}
|
|
|
|
puppet.Sync(source, contact)
|
|
|
|
}
|
|
|
|
|
2019-06-01 19:03:29 +02:00
|
|
|
func (puppet *Puppet) Sync(source *User, contact whatsapp.Contact) {
|
2021-03-19 19:55:08 +01:00
|
|
|
puppet.syncLock.Lock()
|
|
|
|
defer puppet.syncLock.Unlock()
|
2019-06-01 19:03:29 +02:00
|
|
|
err := puppet.DefaultIntent().EnsureRegistered()
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Errorln("Failed to ensure registered:", err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 22:36:14 +01:00
|
|
|
if contact.JID == source.JID {
|
|
|
|
contact.Notify = source.pushName
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:03:29 +02:00
|
|
|
update := false
|
|
|
|
update = puppet.UpdateName(source, contact) || update
|
2021-02-09 22:41:14 +01:00
|
|
|
// TODO figure out how to update avatars after being offline
|
|
|
|
if len(puppet.Avatar) == 0 || puppet.bridge.Config.Bridge.UserAvatarSync {
|
|
|
|
update = puppet.UpdateAvatar(source, nil) || update
|
|
|
|
}
|
2019-06-01 19:03:29 +02:00
|
|
|
if update {
|
2018-08-19 17:21:38 +02:00
|
|
|
puppet.Update()
|
|
|
|
}
|
|
|
|
}
|