groupme/puppet.go

314 lines
8.2 KiB
Go
Raw Normal View History

// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
2020-05-08 19:32:22 +00:00
// Copyright (C) 2020 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 (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
2021-02-22 03:46:17 +00:00
"github.com/karmanyaahm/groupme"
2019-01-11 19:17:31 +00:00
log "maunium.net/go/maulogger/v2"
2020-05-08 19:32:22 +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
2021-03-03 00:46:53 +00:00
"github.com/karmanyaahm/matrix-groupme-go/database"
"github.com/karmanyaahm/matrix-groupme-go/types"
whatsappExt "github.com/karmanyaahm/matrix-groupme-go/whatsapp-ext"
)
var userIDRegex *regexp.Regexp
2021-02-13 05:53:35 +00:00
func (bridge *Bridge) ParsePuppetMXID(mxid id.UserID) (types.GroupMeID, bool) {
if userIDRegex == nil {
userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
bridge.Config.Bridge.FormatUsername("([0-9]+)"),
bridge.Config.Homeserver.Domain))
}
match := userIDRegex.FindStringSubmatch(string(mxid))
if match == nil || len(match) != 2 {
return "", false
}
2021-02-21 05:58:50 +00:00
jid := types.GroupMeID(match[1])
return jid, true
}
2020-05-08 19:32:22 +00:00
func (bridge *Bridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
jid, ok := bridge.ParsePuppetMXID(mxid)
if !ok {
return nil
}
return bridge.GetPuppetByJID(jid)
}
2021-02-13 05:53:35 +00:00
func (bridge *Bridge) GetPuppetByJID(jid types.GroupMeID) *Puppet {
bridge.puppetsLock.Lock()
defer bridge.puppetsLock.Unlock()
puppet, ok := bridge.puppets[jid]
if !ok {
dbPuppet := bridge.DB.Puppet.Get(jid)
if dbPuppet == nil {
dbPuppet = bridge.DB.Puppet.New()
dbPuppet.JID = jid
dbPuppet.Insert()
}
puppet = bridge.NewPuppet(dbPuppet)
bridge.puppets[puppet.JID] = puppet
if len(puppet.CustomMXID) > 0 {
bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
}
}
return puppet
}
2020-05-08 19:32:22 +00:00
func (bridge *Bridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
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
}
return puppet
}
func (bridge *Bridge) GetAllPuppetsWithCustomMXID() []*Puppet {
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID())
}
func (bridge *Bridge) GetAllPuppets() []*Puppet {
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll())
}
func (bridge *Bridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
bridge.puppetsLock.Lock()
defer bridge.puppetsLock.Unlock()
output := make([]*Puppet, len(dbPuppets))
for index, dbPuppet := range dbPuppets {
2019-06-13 18:30:38 +00:00
if dbPuppet == nil {
continue
}
puppet, ok := bridge.puppets[dbPuppet.JID]
if !ok {
puppet = bridge.NewPuppet(dbPuppet)
bridge.puppets[dbPuppet.JID] = puppet
if len(dbPuppet.CustomMXID) > 0 {
bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
}
}
output[index] = puppet
}
return output
}
2021-02-13 05:53:35 +00:00
func (bridge *Bridge) FormatPuppetMXID(jid types.GroupMeID) id.UserID {
return id.NewUserID(
bridge.Config.Bridge.FormatUsername(
strings.Replace(
jid,
whatsappExt.NewUserSuffix, "", 1)),
bridge.Config.Homeserver.Domain)
}
func (bridge *Bridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
return &Puppet{
Puppet: dbPuppet,
bridge: bridge,
log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.JID)),
MXID: bridge.FormatPuppetMXID(dbPuppet.JID),
}
}
type Puppet struct {
*database.Puppet
bridge *Bridge
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
}
func (puppet *Puppet) PhoneNumber() string {
2021-02-21 05:58:50 +00:00
println("phone num")
return strings.Replace(puppet.JID, whatsappExt.NewUserSuffix, "", 1)
}
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
if (!portal.IsPrivateChat() && puppet.customIntent == nil) ||
(portal.backfilling && portal.bridge.Config.Bridge.InviteOwnPuppetForBackfilling) ||
portal.Key.JID == puppet.JID {
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, avatar string) bool {
if len(avatar) == 0 {
err := puppet.DefaultIntent().SetAvatarURL(id.ContentURI{})
if err != nil {
puppet.log.Warnln("Failed to remove avatar:", err)
}
puppet.AvatarURL = types.ContentURI{}
puppet.Avatar = avatar
go puppet.updatePortalAvatar()
return true
}
if puppet.Avatar == avatar {
return false // up to date
}
//TODO check its actually groupme?
response, err := http.Get(avatar + ".large")
if err != nil {
puppet.log.Warnln("Failed to download avatar:", err)
return false
}
defer response.Body.Close()
image, err := ioutil.ReadAll(response.Body)
if err != nil {
puppet.log.Warnln("Failed to read downloaded avatar:", err)
return false
}
mime := response.Header.Get("Content-Type")
if len(mime) == 0 {
mime = http.DetectContentType(image)
}
resp, err := puppet.DefaultIntent().UploadBytes(image, mime)
if err != nil {
puppet.log.Warnln("Failed to upload avatar:", err)
return false
}
puppet.AvatarURL = types.ContentURI{resp.ContentURI}
err = puppet.DefaultIntent().SetAvatarURL(resp.ContentURI)
if err != nil {
puppet.log.Warnln("Failed to set avatar:", err)
}
puppet.Avatar = avatar
go puppet.updatePortalAvatar()
return true
}
2021-02-27 22:38:47 +00:00
func (puppet *Puppet) UpdateName(source *User, contact groupme.Member) bool {
newName, quality := puppet.bridge.Config.Bridge.FormatDisplayname(contact)
if puppet.Displayname != newName && quality >= puppet.NameQuality {
err := puppet.DefaultIntent().SetDisplayName(newName)
if err == nil {
puppet.Displayname = newName
puppet.NameQuality = quality
go puppet.updatePortalName()
puppet.Update()
} else {
puppet.log.Warnln("Failed to set display name:", err)
}
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.ContentURI)
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-02-27 22:38:47 +00:00
func (puppet *Puppet) Sync(source *User, contact groupme.Member) {
if contact.UserID.String() == "system" {
puppet.log.Warnln("Trying to sync system puppet")
2021-02-28 00:57:42 +00:00
// portal.Sync(puppet.bridge.GetUserByJID(portal.Key.Receiver), groupme.Group{})
2021-02-27 22:38:47 +00:00
//TODO permissoins idk if its fine to use portal owner
return
}
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
2021-02-27 22:38:47 +00:00
//if contact.ID.String() == source.JID {
//TODO What is this
// contact.Notify = source.Conn.Info.Pushname
//}
2021-02-13 05:53:35 +00:00
2021-02-22 03:46:17 +00:00
update := false
update = puppet.UpdateName(source, contact) || update
2021-02-27 22:38:47 +00:00
update = puppet.UpdateAvatar(source, contact.ImageURL) || update
2021-02-22 03:46:17 +00:00
if update {
puppet.Update()
}
}