2019-05-24 01:33:26 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2021-10-27 18:30:34 +02:00
|
|
|
// Copyright (C) 2021 Tulir Asokan
|
2019-05-24 01:33:26 +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 (
|
2019-12-30 19:21:04 +01:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/hex"
|
2020-10-05 21:38:34 +02:00
|
|
|
"errors"
|
2021-11-06 12:57:35 +01:00
|
|
|
"fmt"
|
2019-05-24 01:33:26 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
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/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2019-05-24 01:33:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-05-24 13:09:48 +02:00
|
|
|
ErrNoCustomMXID = errors.New("no custom mxid set")
|
2019-05-24 01:33:26 +02:00
|
|
|
ErrMismatchingMXID = errors.New("whoami result does not match custom mxid")
|
|
|
|
)
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error {
|
2019-05-24 01:33:26 +02:00
|
|
|
prevCustomMXID := puppet.CustomMXID
|
|
|
|
if puppet.customIntent != nil {
|
|
|
|
puppet.stopSyncing()
|
|
|
|
}
|
|
|
|
puppet.CustomMXID = mxid
|
|
|
|
puppet.AccessToken = accessToken
|
|
|
|
|
2021-02-11 19:47:10 +01:00
|
|
|
err := puppet.StartCustomMXID(false)
|
2019-05-24 01:33:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(prevCustomMXID) > 0 {
|
|
|
|
delete(puppet.bridge.puppetsByCustomMXID, prevCustomMXID)
|
|
|
|
}
|
|
|
|
if len(puppet.CustomMXID) > 0 {
|
|
|
|
puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
|
|
|
}
|
2020-07-10 15:26:55 +02:00
|
|
|
puppet.EnablePresence = puppet.bridge.Config.Bridge.DefaultBridgePresence
|
|
|
|
puppet.EnableReceipts = puppet.bridge.Config.Bridge.DefaultBridgeReceipts
|
2019-12-30 19:40:20 +01:00
|
|
|
puppet.bridge.AS.StateStore.MarkRegistered(puppet.CustomMXID)
|
2019-05-24 01:33:26 +02:00
|
|
|
puppet.Update()
|
|
|
|
// TODO leave rooms with default puppet
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (puppet *Puppet) loginWithSharedSecret(mxid id.UserID) (string, error) {
|
2021-11-06 12:57:35 +01:00
|
|
|
_, homeserver, _ := mxid.Parse()
|
2020-11-06 01:29:14 +01:00
|
|
|
puppet.log.Debugfln("Logging into %s with shared secret", mxid)
|
2022-08-16 16:10:15 +02:00
|
|
|
loginSecret := puppet.bridge.Config.Bridge.LoginSharedSecretMap[homeserver]
|
2021-11-06 12:57:35 +01:00
|
|
|
client, err := puppet.bridge.newDoublePuppetClient(mxid, "")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to create mautrix client to log in: %v", err)
|
|
|
|
}
|
2022-08-16 16:10:15 +02:00
|
|
|
req := mautrix.ReqLogin{
|
2020-08-22 12:07:55 +02:00
|
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(mxid)},
|
2022-06-21 21:28:57 +02:00
|
|
|
DeviceID: "WhatsApp Bridge",
|
|
|
|
InitialDeviceDisplayName: "WhatsApp Bridge",
|
2022-08-16 16:10:15 +02:00
|
|
|
}
|
|
|
|
if loginSecret == "appservice" {
|
|
|
|
client.AccessToken = puppet.bridge.AS.Registration.AppToken
|
|
|
|
req.Type = mautrix.AuthTypeAppservice
|
|
|
|
} else {
|
|
|
|
mac := hmac.New(sha512.New, []byte(loginSecret))
|
|
|
|
mac.Write([]byte(mxid))
|
|
|
|
req.Password = hex.EncodeToString(mac.Sum(nil))
|
|
|
|
req.Type = mautrix.AuthTypePassword
|
|
|
|
}
|
|
|
|
resp, err := client.Login(&req)
|
2019-12-30 19:21:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) newDoublePuppetClient(mxid id.UserID, accessToken string) (*mautrix.Client, error) {
|
2021-11-06 12:57:35 +01:00
|
|
|
_, homeserver, err := mxid.Parse()
|
2021-11-02 00:17:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-22 00:06:30 +02:00
|
|
|
homeserverURL, found := br.Config.Bridge.DoublePuppetServerMap[homeserver]
|
2021-11-02 00:17:44 +01:00
|
|
|
if !found {
|
2022-05-22 00:06:30 +02:00
|
|
|
if homeserver == br.AS.HomeserverDomain {
|
|
|
|
homeserverURL = br.AS.HomeserverURL
|
|
|
|
} else if br.Config.Bridge.DoublePuppetAllowDiscovery {
|
2021-11-06 12:57:35 +01:00
|
|
|
resp, err := mautrix.DiscoverClientAPI(homeserver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to find homeserver URL for %s: %v", homeserver, err)
|
|
|
|
}
|
|
|
|
homeserverURL = resp.Homeserver.BaseURL
|
2022-05-22 00:06:30 +02:00
|
|
|
br.Log.Debugfln("Discovered URL %s for %s to enable double puppeting for %s", homeserverURL, homeserver, mxid)
|
2021-11-06 12:57:35 +01:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("double puppeting from %s is not allowed", homeserver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client, err := mautrix.NewClient(homeserverURL, mxid, accessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-24 13:45:27 +01:00
|
|
|
client.Log = br.AS.Log.With().Str("as_user_id", mxid.String()).Logger()
|
2022-05-22 00:06:30 +02:00
|
|
|
client.Client = br.AS.HTTPClient
|
|
|
|
client.DefaultHTTPRetries = br.AS.DefaultHTTPRetries
|
2021-11-06 12:57:35 +01:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) newCustomIntent() (*appservice.IntentAPI, error) {
|
|
|
|
if len(puppet.CustomMXID) == 0 {
|
|
|
|
return nil, ErrNoCustomMXID
|
2021-11-02 00:17:44 +01:00
|
|
|
}
|
2021-11-06 12:57:35 +01:00
|
|
|
client, err := puppet.bridge.newDoublePuppetClient(puppet.CustomMXID, puppet.AccessToken)
|
2019-05-24 01:33:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-24 13:09:48 +02:00
|
|
|
client.Syncer = puppet
|
2019-05-24 01:33:26 +02:00
|
|
|
client.Store = puppet
|
|
|
|
|
|
|
|
ia := puppet.bridge.AS.NewIntentAPI("custom")
|
|
|
|
ia.Client = client
|
2020-05-08 21:32:22 +02:00
|
|
|
ia.Localpart, _, _ = puppet.CustomMXID.Parse()
|
2019-05-24 01:33:26 +02:00
|
|
|
ia.UserID = puppet.CustomMXID
|
|
|
|
ia.IsCustomPuppet = true
|
|
|
|
return ia, nil
|
|
|
|
}
|
|
|
|
|
2019-05-27 12:46:04 +02:00
|
|
|
func (puppet *Puppet) clearCustomMXID() {
|
|
|
|
puppet.CustomMXID = ""
|
|
|
|
puppet.AccessToken = ""
|
|
|
|
puppet.customIntent = nil
|
|
|
|
puppet.customUser = nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:47:10 +01:00
|
|
|
func (puppet *Puppet) StartCustomMXID(reloginOnFail bool) error {
|
2019-05-24 01:33:26 +02:00
|
|
|
if len(puppet.CustomMXID) == 0 {
|
2019-05-27 12:46:04 +02:00
|
|
|
puppet.clearCustomMXID()
|
2019-05-24 01:33:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
intent, err := puppet.newCustomIntent()
|
|
|
|
if err != nil {
|
2019-05-27 12:46:04 +02:00
|
|
|
puppet.clearCustomMXID()
|
2019-05-24 01:33:26 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-05-08 21:32:22 +02:00
|
|
|
resp, err := intent.Whoami()
|
2019-05-24 01:33:26 +02:00
|
|
|
if err != nil {
|
2021-02-11 19:47:10 +01:00
|
|
|
if !reloginOnFail || (errors.Is(err, mautrix.MUnknownToken) && !puppet.tryRelogin(err, "initializing double puppeting")) {
|
|
|
|
puppet.clearCustomMXID()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
intent.AccessToken = puppet.AccessToken
|
|
|
|
} else if resp.UserID != puppet.CustomMXID {
|
2019-05-27 12:46:04 +02:00
|
|
|
puppet.clearCustomMXID()
|
2019-05-24 01:33:26 +02:00
|
|
|
return ErrMismatchingMXID
|
|
|
|
}
|
|
|
|
puppet.customIntent = intent
|
2019-05-27 12:46:04 +02:00
|
|
|
puppet.customUser = puppet.bridge.GetUserByMXID(puppet.CustomMXID)
|
2019-05-24 01:33:26 +02:00
|
|
|
puppet.startSyncing()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) startSyncing() {
|
|
|
|
if !puppet.bridge.Config.Bridge.SyncWithCustomPuppets {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
puppet.log.Debugln("Starting syncing...")
|
2019-05-27 12:46:04 +02:00
|
|
|
puppet.customIntent.SyncPresence = "offline"
|
2019-05-24 01:33:26 +02:00
|
|
|
err := puppet.customIntent.Sync()
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Errorln("Fatal error syncing:", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) stopSyncing() {
|
|
|
|
if !puppet.bridge.Config.Bridge.SyncWithCustomPuppets {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
puppet.customIntent.StopSync()
|
|
|
|
}
|
|
|
|
|
2020-07-10 13:53:18 +02:00
|
|
|
func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, _ string) error {
|
2021-10-22 19:14:34 +02:00
|
|
|
if !puppet.customUser.IsLoggedIn() {
|
2019-08-24 21:39:12 +02:00
|
|
|
puppet.log.Debugln("Skipping sync processing: custom user not connected to whatsapp")
|
2019-05-27 13:01:30 +02:00
|
|
|
return nil
|
2019-05-27 12:46:04 +02:00
|
|
|
}
|
|
|
|
for roomID, events := range resp.Rooms.Join {
|
2020-05-08 21:32:22 +02:00
|
|
|
for _, evt := range events.Ephemeral.Events {
|
2021-12-09 13:17:13 +01:00
|
|
|
evt.RoomID = roomID
|
2020-05-08 21:32:22 +02:00
|
|
|
err := evt.Content.ParseRaw(evt.Type)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch evt.Type {
|
|
|
|
case event.EphemeralEventReceipt:
|
2020-07-10 15:26:55 +02:00
|
|
|
if puppet.EnableReceipts {
|
2021-12-07 15:02:51 +01:00
|
|
|
go puppet.bridge.MatrixHandler.HandleReceipt(evt)
|
2020-07-10 13:53:18 +02:00
|
|
|
}
|
2020-05-08 21:32:22 +02:00
|
|
|
case event.EphemeralEventTyping:
|
2021-12-07 15:02:51 +01:00
|
|
|
go puppet.bridge.MatrixHandler.HandleTyping(evt)
|
2019-05-27 12:46:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 13:53:18 +02:00
|
|
|
if puppet.EnablePresence {
|
|
|
|
for _, evt := range resp.Presence.Events {
|
|
|
|
if evt.Sender != puppet.CustomMXID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := evt.Content.ParseRaw(evt.Type)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-22 15:15:54 +02:00
|
|
|
go puppet.bridge.HandlePresence(evt)
|
2020-05-08 21:32:22 +02:00
|
|
|
}
|
2019-05-27 12:46:04 +02:00
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:47:10 +01:00
|
|
|
func (puppet *Puppet) tryRelogin(cause error, action string) bool {
|
2021-11-06 12:57:35 +01:00
|
|
|
if !puppet.bridge.Config.CanAutoDoublePuppet(puppet.CustomMXID) {
|
2021-02-11 19:47:10 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
puppet.log.Debugfln("Trying to relogin after '%v' while %s", cause, action)
|
|
|
|
accessToken, err := puppet.loginWithSharedSecret(puppet.CustomMXID)
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Errorfln("Failed to relogin after '%v' while %s: %v", cause, action, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
puppet.log.Infofln("Successfully relogined after '%v' while %s", cause, action)
|
|
|
|
puppet.AccessToken = accessToken
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-10 13:53:18 +02:00
|
|
|
func (puppet *Puppet) OnFailedSync(_ *mautrix.RespSync, err error) (time.Duration, error) {
|
2019-05-24 01:33:26 +02:00
|
|
|
puppet.log.Warnln("Sync error:", err)
|
2021-02-11 19:47:10 +01:00
|
|
|
if errors.Is(err, mautrix.MUnknownToken) {
|
|
|
|
if !puppet.tryRelogin(err, "syncing") {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
puppet.customIntent.AccessToken = puppet.AccessToken
|
|
|
|
return 0, nil
|
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
return 10 * time.Second, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
func (puppet *Puppet) GetFilterJSON(_ id.UserID) *mautrix.Filter {
|
|
|
|
everything := []event.Type{{Type: "*"}}
|
|
|
|
return &mautrix.Filter{
|
|
|
|
Presence: mautrix.FilterPart{
|
|
|
|
Senders: []id.UserID{puppet.CustomMXID},
|
|
|
|
Types: []event.Type{event.EphemeralEventPresence},
|
|
|
|
},
|
|
|
|
AccountData: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
Room: mautrix.RoomFilter{
|
|
|
|
Ephemeral: mautrix.FilterPart{Types: []event.Type{event.EphemeralEventTyping, event.EphemeralEventReceipt}},
|
|
|
|
IncludeLeave: false,
|
|
|
|
AccountData: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
State: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
Timeline: mautrix.FilterPart{NotTypes: everything},
|
|
|
|
},
|
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 13:53:18 +02:00
|
|
|
func (puppet *Puppet) SaveFilterID(_ id.UserID, _ string) {}
|
|
|
|
func (puppet *Puppet) SaveNextBatch(_ id.UserID, nbt string) { puppet.NextBatch = nbt; puppet.Update() }
|
|
|
|
func (puppet *Puppet) SaveRoom(_ *mautrix.Room) {}
|
|
|
|
func (puppet *Puppet) LoadFilterID(_ id.UserID) string { return "" }
|
|
|
|
func (puppet *Puppet) LoadNextBatch(_ id.UserID) string { return puppet.NextBatch }
|
|
|
|
func (puppet *Puppet) LoadRoom(_ id.RoomID) *mautrix.Room { return nil }
|