2018-08-16 14:59:18 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2019-01-12 14:54:04 +01:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-08-16 14:59:18 +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 (
|
2018-08-24 18:46:14 +02:00
|
|
|
"bytes"
|
2018-09-01 22:38:03 +02:00
|
|
|
"encoding/gob"
|
2018-08-24 18:46:14 +02:00
|
|
|
"encoding/hex"
|
2018-08-16 14:59:18 +02:00
|
|
|
"fmt"
|
2018-08-24 18:46:14 +02:00
|
|
|
"image"
|
2018-08-25 23:26:24 +02:00
|
|
|
"image/gif"
|
|
|
|
"image/jpeg"
|
|
|
|
"image/png"
|
2018-08-24 18:46:14 +02:00
|
|
|
"math/rand"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
2019-05-31 21:30:57 +02:00
|
|
|
"reflect"
|
2018-08-18 21:57:08 +02:00
|
|
|
"strings"
|
2018-08-23 00:12:26 +02:00
|
|
|
"sync"
|
2019-05-22 15:46:18 +02:00
|
|
|
"time"
|
2018-08-24 18:46:14 +02:00
|
|
|
|
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2018-08-25 23:26:24 +02:00
|
|
|
waProto "github.com/Rhymen/go-whatsapp/binary/proto"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
2019-05-16 00:59:36 +02:00
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2018-08-24 18:46:14 +02:00
|
|
|
"maunium.net/go/mautrix-appservice"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2018-08-24 18:46:14 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
|
|
|
"maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
2018-08-16 14:59:18 +02:00
|
|
|
)
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) GetPortalByMXID(mxid types.MatrixRoomID) *Portal {
|
|
|
|
bridge.portalsLock.Lock()
|
|
|
|
defer bridge.portalsLock.Unlock()
|
|
|
|
portal, ok := bridge.portalsByMXID[mxid]
|
2018-08-16 14:59:18 +02:00
|
|
|
if !ok {
|
2019-05-28 20:31:25 +02:00
|
|
|
return bridge.loadDBPortal(bridge.DB.Portal.GetByMXID(mxid), nil)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
return portal
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) GetPortalByJID(key database.PortalKey) *Portal {
|
|
|
|
bridge.portalsLock.Lock()
|
|
|
|
defer bridge.portalsLock.Unlock()
|
|
|
|
portal, ok := bridge.portalsByJID[key]
|
2018-08-16 14:59:18 +02:00
|
|
|
if !ok {
|
2019-05-28 20:31:25 +02:00
|
|
|
return bridge.loadDBPortal(bridge.DB.Portal.GetByJID(key), &key)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
return portal
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) GetAllPortals() []*Portal {
|
2019-06-01 19:03:29 +02:00
|
|
|
return bridge.dbPortalsToPortals(bridge.DB.Portal.GetAll())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) GetAllPortalsByJID(jid types.WhatsAppID) []*Portal {
|
|
|
|
return bridge.dbPortalsToPortals(bridge.DB.Portal.GetAllByJID(jid))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal {
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.portalsLock.Lock()
|
|
|
|
defer bridge.portalsLock.Unlock()
|
2018-08-16 14:59:18 +02:00
|
|
|
output := make([]*Portal, len(dbPortals))
|
|
|
|
for index, dbPortal := range dbPortals {
|
2019-06-13 20:30:38 +02:00
|
|
|
if dbPortal == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
portal, ok := bridge.portalsByJID[dbPortal.Key]
|
2018-08-16 14:59:18 +02:00
|
|
|
if !ok {
|
2019-05-28 20:31:25 +02:00
|
|
|
portal = bridge.loadDBPortal(dbPortal, nil)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
output[index] = portal
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:31:25 +02:00
|
|
|
func (bridge *Bridge) loadDBPortal(dbPortal *database.Portal, key *database.PortalKey) *Portal {
|
|
|
|
if dbPortal == nil {
|
|
|
|
if key == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dbPortal = bridge.DB.Portal.New()
|
|
|
|
dbPortal.Key = *key
|
|
|
|
dbPortal.Insert()
|
|
|
|
}
|
|
|
|
portal := bridge.NewPortal(dbPortal)
|
|
|
|
bridge.portalsByJID[portal.Key] = portal
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
bridge.portalsByMXID[portal.MXID] = portal
|
|
|
|
}
|
|
|
|
return portal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) GetUsers() []*User {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) NewPortal(dbPortal *database.Portal) *Portal {
|
2019-05-21 22:44:14 +02:00
|
|
|
portal := &Portal{
|
2018-08-16 14:59:18 +02:00
|
|
|
Portal: dbPortal,
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge: bridge,
|
|
|
|
log: bridge.Log.Sub(fmt.Sprintf("Portal/%s", dbPortal.Key)),
|
2018-08-30 23:13:08 +02:00
|
|
|
|
2018-09-01 23:01:22 +02:00
|
|
|
recentlyHandled: [recentlyHandledLength]types.WhatsAppMessageID{},
|
2019-05-21 22:44:14 +02:00
|
|
|
|
|
|
|
messages: make(chan PortalMessage, 128),
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2019-05-21 22:44:14 +02:00
|
|
|
go portal.handleMessageLoop()
|
|
|
|
return portal
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
|
2018-09-01 23:01:22 +02:00
|
|
|
const recentlyHandledLength = 100
|
|
|
|
|
2019-05-21 22:44:14 +02:00
|
|
|
type PortalMessage struct {
|
2019-05-22 22:05:58 +02:00
|
|
|
chat string
|
2019-05-22 15:46:18 +02:00
|
|
|
source *User
|
|
|
|
data interface{}
|
|
|
|
timestamp uint64
|
2019-05-21 22:44:14 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:59:18 +02:00
|
|
|
type Portal struct {
|
|
|
|
*database.Portal
|
|
|
|
|
|
|
|
bridge *Bridge
|
2018-08-16 18:20:07 +02:00
|
|
|
log log.Logger
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2019-05-28 13:12:35 +02:00
|
|
|
roomCreateLock sync.Mutex
|
2018-08-30 23:13:08 +02:00
|
|
|
|
2018-09-01 23:01:22 +02:00
|
|
|
recentlyHandled [recentlyHandledLength]types.WhatsAppMessageID
|
2018-08-30 23:13:08 +02:00
|
|
|
recentlyHandledLock sync.Mutex
|
|
|
|
recentlyHandledIndex uint8
|
2018-09-01 22:38:03 +02:00
|
|
|
|
2019-05-22 15:46:18 +02:00
|
|
|
backfillLock sync.Mutex
|
2019-05-30 16:22:03 +02:00
|
|
|
backfilling bool
|
2019-05-21 22:44:14 +02:00
|
|
|
lastMessageTs uint64
|
|
|
|
|
2019-05-30 16:48:22 +02:00
|
|
|
privateChatBackfillInvitePuppet func()
|
|
|
|
|
2019-05-21 22:44:14 +02:00
|
|
|
messages chan PortalMessage
|
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
isPrivate *bool
|
2018-08-30 23:13:08 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 15:46:18 +02:00
|
|
|
const MaxMessageAgeToCreatePortal = 5 * 60 // 5 minutes
|
|
|
|
|
2019-05-21 22:44:14 +02:00
|
|
|
func (portal *Portal) handleMessageLoop() {
|
|
|
|
for msg := range portal.messages {
|
|
|
|
if len(portal.MXID) == 0 {
|
2019-05-22 15:46:18 +02:00
|
|
|
if msg.timestamp+MaxMessageAgeToCreatePortal < uint64(time.Now().Unix()) {
|
|
|
|
portal.log.Debugln("Not creating portal room for incoming message as the message is too old.")
|
2019-05-21 22:44:14 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := portal.CreateMatrixRoom(msg.source)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 22:05:58 +02:00
|
|
|
portal.backfillLock.Lock()
|
2019-05-21 22:44:14 +02:00
|
|
|
portal.handleMessage(msg)
|
2019-05-22 22:05:58 +02:00
|
|
|
portal.backfillLock.Unlock()
|
2019-05-21 22:44:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) handleMessage(msg PortalMessage) {
|
2019-05-30 16:00:36 +02:00
|
|
|
if len(portal.MXID) == 0 {
|
2019-05-31 21:30:57 +02:00
|
|
|
portal.log.Warnln("handleMessage called even though portal.MXID is empty")
|
2019-05-30 16:00:36 +02:00
|
|
|
return
|
|
|
|
}
|
2019-05-21 22:44:14 +02:00
|
|
|
switch data := msg.data.(type) {
|
|
|
|
case whatsapp.TextMessage:
|
|
|
|
portal.HandleTextMessage(msg.source, data)
|
|
|
|
case whatsapp.ImageMessage:
|
|
|
|
portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.Type, data.Caption)
|
|
|
|
case whatsapp.VideoMessage:
|
|
|
|
portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.Type, data.Caption)
|
|
|
|
case whatsapp.AudioMessage:
|
|
|
|
portal.HandleMediaMessage(msg.source, data.Download, nil, data.Info, data.Type, "")
|
|
|
|
case whatsapp.DocumentMessage:
|
|
|
|
portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.Type, data.Title)
|
|
|
|
case whatsappExt.MessageRevocation:
|
|
|
|
portal.HandleMessageRevoke(msg.source, data)
|
2019-05-30 16:00:36 +02:00
|
|
|
case FakeMessage:
|
|
|
|
portal.HandleFakeMessage(msg.source, data)
|
2019-05-31 21:30:57 +02:00
|
|
|
default:
|
|
|
|
portal.log.Warnln("Unknown message type:", reflect.TypeOf(msg.data))
|
2019-05-21 22:44:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 23:13:08 +02:00
|
|
|
func (portal *Portal) isRecentlyHandled(id types.WhatsAppMessageID) bool {
|
|
|
|
start := portal.recentlyHandledIndex
|
2018-09-01 23:01:22 +02:00
|
|
|
for i := start; i != start; i = (i - 1) % recentlyHandledLength {
|
2018-08-30 23:13:08 +02:00
|
|
|
if portal.recentlyHandled[i] == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) isDuplicate(id types.WhatsAppMessageID) bool {
|
|
|
|
msg := portal.bridge.DB.Message.GetByJID(portal.Key, id)
|
|
|
|
if msg != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
func init() {
|
|
|
|
gob.Register(&waProto.Message{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) markHandled(source *User, message *waProto.WebMessageInfo, mxid types.MatrixEventID) {
|
2018-08-30 23:13:08 +02:00
|
|
|
msg := portal.bridge.DB.Message.New()
|
|
|
|
msg.Chat = portal.Key
|
2018-09-01 22:38:03 +02:00
|
|
|
msg.JID = message.GetKey().GetId()
|
2018-08-30 23:13:08 +02:00
|
|
|
msg.MXID = mxid
|
2019-05-22 15:46:18 +02:00
|
|
|
msg.Timestamp = message.GetMessageTimestamp()
|
2018-09-01 22:38:03 +02:00
|
|
|
if message.GetKey().GetFromMe() {
|
|
|
|
msg.Sender = source.JID
|
|
|
|
} else if portal.IsPrivateChat() {
|
|
|
|
msg.Sender = portal.Key.JID
|
|
|
|
} else {
|
|
|
|
msg.Sender = message.GetKey().GetParticipant()
|
|
|
|
if len(msg.Sender) == 0 {
|
|
|
|
msg.Sender = message.GetParticipant()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg.Content = message.Message
|
2018-08-30 23:13:08 +02:00
|
|
|
msg.Insert()
|
|
|
|
|
|
|
|
portal.recentlyHandledLock.Lock()
|
|
|
|
index := portal.recentlyHandledIndex
|
2018-09-01 23:01:22 +02:00
|
|
|
portal.recentlyHandledIndex = (portal.recentlyHandledIndex + 1) % recentlyHandledLength
|
2018-08-30 23:13:08 +02:00
|
|
|
portal.recentlyHandledLock.Unlock()
|
2018-09-01 22:38:03 +02:00
|
|
|
portal.recentlyHandled[index] = msg.JID
|
2018-08-30 23:13:08 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:12:35 +02:00
|
|
|
func (portal *Portal) startHandling(info whatsapp.MessageInfo) bool {
|
|
|
|
if portal.lastMessageTs > info.Timestamp+1 ||
|
|
|
|
portal.isRecentlyHandled(info.Id) ||
|
|
|
|
portal.isDuplicate(info.Id) {
|
|
|
|
return false
|
2018-08-30 23:13:08 +02:00
|
|
|
}
|
2019-05-21 22:44:14 +02:00
|
|
|
portal.lastMessageTs = info.Timestamp
|
2019-05-28 13:12:35 +02:00
|
|
|
return true
|
2018-08-30 23:13:08 +02:00
|
|
|
}
|
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
func (portal *Portal) finishHandling(source *User, message *waProto.WebMessageInfo, mxid types.MatrixEventID) {
|
|
|
|
portal.markHandled(source, message, mxid)
|
2019-05-28 13:12:35 +02:00
|
|
|
portal.log.Debugln("Handled message", message.GetKey().GetId(), "->", mxid)
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
|
2018-08-26 00:55:21 +02:00
|
|
|
func (portal *Portal) SyncParticipants(metadata *whatsappExt.GroupInfo) {
|
2018-08-26 15:11:48 +02:00
|
|
|
changed := false
|
|
|
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
levels = portal.GetBasePowerLevels()
|
|
|
|
changed = true
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
for _, participant := range metadata.Participants {
|
2018-08-28 23:40:54 +02:00
|
|
|
user := portal.bridge.GetUserByJID(participant.JID)
|
2018-08-30 00:10:26 +02:00
|
|
|
if user != nil && !portal.bridge.AS.StateStore.IsInvited(portal.MXID, user.MXID) {
|
2019-01-21 22:55:16 +01:00
|
|
|
_, err = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{
|
2018-08-28 23:40:54 +02:00
|
|
|
UserID: user.MXID,
|
|
|
|
})
|
2019-01-21 22:55:16 +01:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to invite %s to %s: %v", user.MXID, portal.MXID, err)
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 01:33:26 +02:00
|
|
|
puppet := portal.bridge.GetPuppetByJID(participant.JID)
|
|
|
|
err := puppet.IntentFor(portal).EnsureJoined(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to make puppet of %s join %s: %v", participant.JID, portal.MXID, err)
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:11:48 +02:00
|
|
|
expectedLevel := 0
|
|
|
|
if participant.IsSuperAdmin {
|
|
|
|
expectedLevel = 95
|
|
|
|
} else if participant.IsAdmin {
|
|
|
|
expectedLevel = 50
|
|
|
|
}
|
2018-08-26 15:19:50 +02:00
|
|
|
changed = levels.EnsureUserLevel(puppet.MXID, expectedLevel) || changed
|
2018-08-28 23:40:54 +02:00
|
|
|
if user != nil {
|
|
|
|
changed = levels.EnsureUserLevel(user.MXID, expectedLevel) || changed
|
2018-08-26 15:11:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if changed {
|
2018-08-30 00:10:26 +02:00
|
|
|
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to change power levels:", err)
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 17:21:38 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInfo) bool {
|
2018-08-26 00:55:21 +02:00
|
|
|
if avatar == nil {
|
|
|
|
var err error
|
2018-08-28 23:40:54 +02:00
|
|
|
avatar, err = user.Conn.GetProfilePicThumb(portal.Key.JID)
|
2018-08-26 00:55:21 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln(err)
|
|
|
|
return false
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2018-08-26 00:55:21 +02:00
|
|
|
|
2019-05-23 19:09:13 +02:00
|
|
|
if avatar.Status != 0 {
|
2018-12-05 10:30:07 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
if portal.Avatar == avatar.Tag {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := avatar.DownloadBytes()
|
|
|
|
if err != nil {
|
2019-01-21 22:55:16 +01:00
|
|
|
portal.log.Warnln("Failed to download avatar:", err)
|
2018-08-23 00:12:26 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-23 23:52:06 +02:00
|
|
|
mimeType := http.DetectContentType(data)
|
|
|
|
resp, err := portal.MainIntent().UploadBytes(data, mimeType)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
2019-01-21 22:55:16 +01:00
|
|
|
portal.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
|
|
|
portal.AvatarURL = resp.ContentURI
|
2019-05-22 22:27:58 +02:00
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
_, err = portal.MainIntent().SetRoomAvatar(portal.MXID, resp.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to set room topic:", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
portal.Avatar = avatar.Tag
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:55:21 +02:00
|
|
|
func (portal *Portal) UpdateName(name string, setBy types.WhatsAppID) bool {
|
|
|
|
if portal.Name != name {
|
2019-03-13 23:38:11 +01:00
|
|
|
intent := portal.MainIntent()
|
|
|
|
if len(setBy) > 0 {
|
2019-05-24 01:33:26 +02:00
|
|
|
intent = portal.bridge.GetPuppetByJID(setBy).IntentFor(portal)
|
2019-03-13 23:38:11 +01:00
|
|
|
}
|
2018-08-26 00:55:21 +02:00
|
|
|
_, err := intent.SetRoomName(portal.MXID, name)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err == nil {
|
2018-08-26 00:55:21 +02:00
|
|
|
portal.Name = name
|
2018-08-23 00:12:26 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
portal.log.Warnln("Failed to set room name:", err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:55:21 +02:00
|
|
|
func (portal *Portal) UpdateTopic(topic string, setBy types.WhatsAppID) bool {
|
|
|
|
if portal.Topic != topic {
|
2019-03-13 23:38:11 +01:00
|
|
|
intent := portal.MainIntent()
|
|
|
|
if len(setBy) > 0 {
|
2019-05-24 01:33:26 +02:00
|
|
|
intent = portal.bridge.GetPuppetByJID(setBy).IntentFor(portal)
|
2019-03-13 23:38:11 +01:00
|
|
|
}
|
2018-08-26 00:55:21 +02:00
|
|
|
_, err := intent.SetRoomTopic(portal.MXID, topic)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err == nil {
|
2018-08-26 00:55:21 +02:00
|
|
|
portal.Topic = topic
|
2018-08-23 00:12:26 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
portal.log.Warnln("Failed to set room topic:", err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) UpdateMetadata(user *User) bool {
|
2019-03-13 23:38:11 +01:00
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
return false
|
|
|
|
} else if portal.IsStatusBroadcastRoom() {
|
|
|
|
update := false
|
|
|
|
update = portal.UpdateName("WhatsApp Status Broadcast", "") || update
|
|
|
|
update = portal.UpdateTopic("WhatsApp status updates from your contacts", "") || update
|
|
|
|
return update
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
metadata, err := user.Conn.GetGroupMetaData(portal.Key.JID)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln(err)
|
|
|
|
return false
|
|
|
|
}
|
2018-12-05 09:20:39 +01:00
|
|
|
if metadata.Status != 0 {
|
|
|
|
// 401: access denied
|
|
|
|
// 404: group does (no longer) exist
|
|
|
|
// 500: ??? happens with status@broadcast
|
|
|
|
|
|
|
|
// TODO: update the room, e.g. change priority level
|
|
|
|
// to send messages to moderator
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
portal.SyncParticipants(metadata)
|
|
|
|
update := false
|
2018-08-26 00:55:21 +02:00
|
|
|
update = portal.UpdateName(metadata.Name, metadata.NameSetBy) || update
|
|
|
|
update = portal.UpdateTopic(metadata.Topic, metadata.TopicSetBy) || update
|
2018-08-23 00:12:26 +02:00
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:22:03 +02:00
|
|
|
func (portal *Portal) ensureUserInvited(user *User) {
|
|
|
|
err := portal.MainIntent().EnsureInvited(portal.MXID, user.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to ensure %s is invited to %s: %v", user.MXID, portal.MXID, err)
|
|
|
|
}
|
|
|
|
customPuppet := portal.bridge.GetPuppetByCustomMXID(user.MXID)
|
2019-05-30 19:25:04 +02:00
|
|
|
if customPuppet != nil && customPuppet.CustomIntent() != nil {
|
2019-05-30 16:22:03 +02:00
|
|
|
_ = customPuppet.CustomIntent().EnsureJoined(portal.MXID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) Sync(user *User, contact whatsapp.Contact) {
|
2019-05-22 15:46:18 +02:00
|
|
|
portal.log.Infoln("Syncing portal for", user.MXID)
|
2018-08-28 23:40:54 +02:00
|
|
|
|
2018-08-19 17:21:38 +02:00
|
|
|
if len(portal.MXID) == 0 {
|
2019-05-31 18:33:18 +02:00
|
|
|
if !portal.IsPrivateChat() {
|
|
|
|
portal.Name = contact.Name
|
|
|
|
}
|
2019-03-13 23:38:11 +01:00
|
|
|
err := portal.CreateMatrixRoom(user)
|
2018-08-19 17:21:38 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-30 23:13:08 +02:00
|
|
|
} else {
|
2019-05-30 16:22:03 +02:00
|
|
|
portal.ensureUserInvited(user)
|
2018-08-19 17:21:38 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 18:33:18 +02:00
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
update := false
|
2018-08-28 23:40:54 +02:00
|
|
|
update = portal.UpdateMetadata(user) || update
|
2019-03-13 23:38:11 +01:00
|
|
|
if !portal.IsStatusBroadcastRoom() {
|
|
|
|
update = portal.UpdateAvatar(user, nil) || update
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
if update {
|
2018-08-19 17:21:38 +02:00
|
|
|
portal.Update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
func (portal *Portal) GetBasePowerLevels() *mautrix.PowerLevels {
|
2018-08-26 15:11:48 +02:00
|
|
|
anyone := 0
|
|
|
|
nope := 99
|
2019-07-16 11:16:17 +02:00
|
|
|
invite := 99
|
|
|
|
if portal.bridge.Config.Bridge.AllowUserInvite {
|
|
|
|
invite = 0
|
|
|
|
}
|
2019-01-11 20:17:31 +01:00
|
|
|
return &mautrix.PowerLevels{
|
2018-08-26 15:11:48 +02:00
|
|
|
UsersDefault: anyone,
|
|
|
|
EventsDefault: anyone,
|
|
|
|
RedactPtr: &anyone,
|
|
|
|
StateDefaultPtr: &nope,
|
|
|
|
BanPtr: &nope,
|
2019-07-16 11:16:17 +02:00
|
|
|
InvitePtr: &invite,
|
2018-08-26 15:11:48 +02:00
|
|
|
Users: map[string]int{
|
|
|
|
portal.MainIntent().UserID: 100,
|
|
|
|
},
|
2018-08-30 00:10:26 +02:00
|
|
|
Events: map[string]int{
|
2019-01-11 20:17:31 +01:00
|
|
|
mautrix.StateRoomName.Type: anyone,
|
|
|
|
mautrix.StateRoomAvatar.Type: anyone,
|
|
|
|
mautrix.StateTopic.Type: anyone,
|
2018-08-26 15:11:48 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) ChangeAdminStatus(jids []string, setAdmin bool) {
|
|
|
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
levels = portal.GetBasePowerLevels()
|
|
|
|
}
|
|
|
|
newLevel := 0
|
|
|
|
if setAdmin {
|
|
|
|
newLevel = 50
|
|
|
|
}
|
|
|
|
changed := false
|
|
|
|
for _, jid := range jids {
|
2018-08-28 23:40:54 +02:00
|
|
|
puppet := portal.bridge.GetPuppetByJID(jid)
|
2018-08-26 15:11:48 +02:00
|
|
|
changed = levels.EnsureUserLevel(puppet.MXID, newLevel) || changed
|
2018-08-26 15:19:50 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
user := portal.bridge.GetUserByJID(jid)
|
|
|
|
if user != nil {
|
|
|
|
changed = levels.EnsureUserLevel(user.MXID, newLevel) || changed
|
2018-08-26 15:19:50 +02:00
|
|
|
}
|
2018-08-26 15:11:48 +02:00
|
|
|
}
|
|
|
|
if changed {
|
2018-08-30 00:10:26 +02:00
|
|
|
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to change power levels:", err)
|
|
|
|
}
|
2018-08-26 15:11:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) RestrictMessageSending(restrict bool) {
|
|
|
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
levels = portal.GetBasePowerLevels()
|
|
|
|
}
|
|
|
|
if restrict {
|
|
|
|
levels.EventsDefault = 50
|
|
|
|
} else {
|
|
|
|
levels.EventsDefault = 0
|
|
|
|
}
|
2018-08-30 00:10:26 +02:00
|
|
|
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to change power levels:", err)
|
|
|
|
}
|
2018-08-26 15:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) RestrictMetadataChanges(restrict bool) {
|
|
|
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
levels = portal.GetBasePowerLevels()
|
|
|
|
}
|
|
|
|
newLevel := 0
|
|
|
|
if restrict {
|
|
|
|
newLevel = 50
|
|
|
|
}
|
|
|
|
changed := false
|
2019-01-11 20:17:31 +01:00
|
|
|
changed = levels.EnsureEventLevel(mautrix.StateRoomName, newLevel) || changed
|
|
|
|
changed = levels.EnsureEventLevel(mautrix.StateRoomAvatar, newLevel) || changed
|
|
|
|
changed = levels.EnsureEventLevel(mautrix.StateTopic, newLevel) || changed
|
2018-08-26 15:11:48 +02:00
|
|
|
if changed {
|
2018-08-30 00:10:26 +02:00
|
|
|
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to change power levels:", err)
|
|
|
|
}
|
2018-08-26 15:11:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 22:05:58 +02:00
|
|
|
func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error {
|
2019-05-22 15:46:18 +02:00
|
|
|
if !portal.bridge.Config.Bridge.RecoverHistory {
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-30 16:25:56 +02:00
|
|
|
|
|
|
|
endBackfill := portal.beginBackfill()
|
|
|
|
defer endBackfill()
|
|
|
|
|
2019-05-22 15:46:18 +02:00
|
|
|
lastMessage := portal.bridge.DB.Message.GetLastInChat(portal.Key)
|
|
|
|
if lastMessage == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-28 20:30:39 +02:00
|
|
|
if lastMessage.Timestamp >= lastMessageTime {
|
2019-05-22 22:05:58 +02:00
|
|
|
portal.log.Debugln("Not backfilling: no new messages")
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-22 15:46:18 +02:00
|
|
|
|
|
|
|
lastMessageID := lastMessage.JID
|
2019-05-28 20:30:39 +02:00
|
|
|
lastMessageFromMe := lastMessage.Sender == user.JID
|
2019-05-22 15:46:18 +02:00
|
|
|
portal.log.Infoln("Backfilling history since", lastMessageID, "for", user.MXID)
|
|
|
|
for len(lastMessageID) > 0 {
|
|
|
|
portal.log.Debugln("Backfilling history: 50 messages after", lastMessageID)
|
2019-05-28 20:30:39 +02:00
|
|
|
resp, err := user.Conn.LoadMessagesAfter(portal.Key.JID, lastMessageID, lastMessageFromMe, 50)
|
2019-05-22 15:46:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-22 21:05:28 +02:00
|
|
|
messages, ok := resp.Content.([]interface{})
|
2019-05-28 20:30:39 +02:00
|
|
|
if !ok || len(messages) == 0 {
|
2019-05-22 21:05:28 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
portal.handleHistory(user, messages)
|
|
|
|
|
|
|
|
lastMessageProto, ok := messages[len(messages)-1].(*waProto.WebMessageInfo)
|
|
|
|
if ok {
|
|
|
|
lastMessageID = lastMessageProto.GetKey().GetId()
|
2019-05-28 20:30:39 +02:00
|
|
|
lastMessageFromMe = lastMessageProto.GetKey().GetFromMe()
|
2019-05-22 15:46:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
portal.log.Infoln("Backfilling finished")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:25:56 +02:00
|
|
|
func (portal *Portal) beginBackfill() func() {
|
2019-05-28 13:12:35 +02:00
|
|
|
portal.backfillLock.Lock()
|
2019-05-30 16:22:03 +02:00
|
|
|
portal.backfilling = true
|
2019-05-30 16:48:22 +02:00
|
|
|
var privateChatPuppetInvited bool
|
2019-05-30 16:22:03 +02:00
|
|
|
var privateChatPuppet *Puppet
|
2019-06-01 19:03:29 +02:00
|
|
|
if portal.IsPrivateChat() && portal.bridge.Config.Bridge.InviteOwnPuppetForBackfilling {
|
2019-05-30 16:22:03 +02:00
|
|
|
privateChatPuppet = portal.bridge.GetPuppetByJID(portal.Key.Receiver)
|
2019-05-30 16:48:22 +02:00
|
|
|
portal.privateChatBackfillInvitePuppet = func() {
|
|
|
|
if privateChatPuppetInvited {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
privateChatPuppetInvited = true
|
|
|
|
_, _ = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{UserID: privateChatPuppet.MXID})
|
|
|
|
_ = privateChatPuppet.DefaultIntent().EnsureJoined(portal.MXID)
|
|
|
|
}
|
2019-05-30 16:22:03 +02:00
|
|
|
}
|
2019-05-30 16:25:56 +02:00
|
|
|
return func() {
|
|
|
|
portal.backfilling = false
|
2019-05-30 16:48:22 +02:00
|
|
|
portal.privateChatBackfillInvitePuppet = nil
|
2019-05-30 16:25:56 +02:00
|
|
|
portal.backfillLock.Unlock()
|
2019-05-30 16:48:22 +02:00
|
|
|
if privateChatPuppet != nil && privateChatPuppetInvited {
|
2019-05-30 16:25:56 +02:00
|
|
|
_, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) FillInitialHistory(user *User) error {
|
|
|
|
if portal.bridge.Config.Bridge.InitialHistoryFill == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
endBackfill := portal.beginBackfill()
|
|
|
|
defer endBackfill()
|
2019-05-30 16:48:22 +02:00
|
|
|
if portal.privateChatBackfillInvitePuppet != nil {
|
|
|
|
portal.privateChatBackfillInvitePuppet()
|
|
|
|
}
|
2019-05-30 16:25:56 +02:00
|
|
|
|
2019-05-22 21:05:28 +02:00
|
|
|
n := portal.bridge.Config.Bridge.InitialHistoryFill
|
|
|
|
portal.log.Infoln("Filling initial history, maximum", n, "messages")
|
|
|
|
var messages []interface{}
|
|
|
|
before := ""
|
2019-05-28 20:30:39 +02:00
|
|
|
fromMe := true
|
2019-05-22 21:05:28 +02:00
|
|
|
chunkNum := 1
|
|
|
|
for n > 0 {
|
2019-05-28 20:48:37 +02:00
|
|
|
count := 50
|
2019-05-22 21:05:28 +02:00
|
|
|
if n < count {
|
|
|
|
count = n
|
|
|
|
}
|
|
|
|
portal.log.Debugfln("Fetching chunk %d (%d messages / %d cap) before message %s", chunkNum, count, n, before)
|
2019-05-28 20:30:39 +02:00
|
|
|
resp, err := user.Conn.LoadMessagesBefore(portal.Key.JID, before, fromMe, count)
|
2019-05-22 21:05:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chunk, ok := resp.Content.([]interface{})
|
2019-05-31 21:30:57 +02:00
|
|
|
if !ok || len(chunk) == 0 {
|
2019-05-22 21:05:28 +02:00
|
|
|
portal.log.Infoln("Chunk empty, starting handling of loaded messages")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-05-31 21:30:57 +02:00
|
|
|
messages = append(chunk, messages...)
|
2019-05-22 21:05:28 +02:00
|
|
|
|
|
|
|
portal.log.Debugfln("Fetched chunk and received %d messages", len(chunk))
|
|
|
|
|
|
|
|
n -= len(chunk)
|
2019-05-28 20:30:39 +02:00
|
|
|
key := chunk[0].(*waProto.WebMessageInfo).GetKey()
|
|
|
|
before = key.GetId()
|
|
|
|
fromMe = key.GetFromMe()
|
2019-05-22 21:05:28 +02:00
|
|
|
if len(before) == 0 {
|
|
|
|
portal.log.Infoln("No message ID for first message, starting handling of loaded messages")
|
|
|
|
break
|
|
|
|
}
|
2019-05-21 22:44:14 +02:00
|
|
|
}
|
2019-05-22 21:05:28 +02:00
|
|
|
portal.handleHistory(user, messages)
|
2019-05-30 16:48:22 +02:00
|
|
|
portal.log.Infoln("Initial history fill complete")
|
2019-05-22 21:05:28 +02:00
|
|
|
return nil
|
2019-05-22 15:46:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:05:28 +02:00
|
|
|
func (portal *Portal) handleHistory(user *User, messages []interface{}) {
|
|
|
|
portal.log.Infoln("Handling", len(messages), "messages of history")
|
2019-05-21 22:44:14 +02:00
|
|
|
for _, rawMessage := range messages {
|
|
|
|
message, ok := rawMessage.(*waProto.WebMessageInfo)
|
|
|
|
if !ok {
|
|
|
|
portal.log.Warnln("Unexpected non-WebMessageInfo item in history response:", rawMessage)
|
|
|
|
continue
|
|
|
|
}
|
2019-05-22 16:39:27 +02:00
|
|
|
data := whatsapp.ParseProtoMessage(message)
|
2019-05-30 16:48:22 +02:00
|
|
|
if portal.privateChatBackfillInvitePuppet != nil && message.GetKey().GetFromMe() && portal.IsPrivateChat() {
|
|
|
|
portal.privateChatBackfillInvitePuppet()
|
|
|
|
}
|
2019-05-22 22:05:58 +02:00
|
|
|
portal.handleMessage(PortalMessage{portal.Key.JID, user, data, message.GetMessageTimestamp()})
|
2019-05-21 22:44:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 23:38:11 +01:00
|
|
|
func (portal *Portal) CreateMatrixRoom(user *User) error {
|
2018-08-23 00:12:26 +02:00
|
|
|
portal.roomCreateLock.Lock()
|
|
|
|
defer portal.roomCreateLock.Unlock()
|
2018-08-18 21:57:08 +02:00
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 10:45:14 +01:00
|
|
|
intent := portal.MainIntent()
|
|
|
|
if err := intent.EnsureRegistered(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:46:18 +02:00
|
|
|
portal.log.Infoln("Creating Matrix room. Info source:", user.MXID)
|
|
|
|
|
2019-05-22 22:27:58 +02:00
|
|
|
var metadata *whatsappExt.GroupInfo
|
2018-08-18 21:57:08 +02:00
|
|
|
isPrivateChat := false
|
2018-08-24 23:45:50 +02:00
|
|
|
if portal.IsPrivateChat() {
|
2019-06-01 19:03:29 +02:00
|
|
|
puppet := portal.bridge.GetPuppetByJID(portal.Key.JID)
|
|
|
|
if portal.bridge.Config.Bridge.PrivateChatPortalMeta {
|
|
|
|
portal.Name = puppet.Displayname
|
|
|
|
portal.AvatarURL = puppet.AvatarURL
|
|
|
|
portal.Avatar = puppet.Avatar
|
|
|
|
} else {
|
|
|
|
portal.Name = ""
|
|
|
|
}
|
2019-03-13 23:38:11 +01:00
|
|
|
portal.Topic = "WhatsApp private chat"
|
2018-08-18 21:57:08 +02:00
|
|
|
isPrivateChat = true
|
2019-03-13 23:38:11 +01:00
|
|
|
} else if portal.IsStatusBroadcastRoom() {
|
|
|
|
portal.Name = "WhatsApp Status Broadcast"
|
|
|
|
portal.Topic = "WhatsApp status updates from your contacts"
|
|
|
|
} else {
|
2019-05-22 22:27:58 +02:00
|
|
|
var err error
|
|
|
|
metadata, err = user.Conn.GetGroupMetaData(portal.Key.JID)
|
2019-03-13 23:38:11 +01:00
|
|
|
if err == nil && metadata.Status == 0 {
|
|
|
|
portal.Name = metadata.Name
|
|
|
|
portal.Topic = metadata.Topic
|
|
|
|
}
|
2019-05-22 22:27:58 +02:00
|
|
|
portal.UpdateAvatar(user, nil)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-08-26 15:11:48 +02:00
|
|
|
|
2019-05-22 22:27:58 +02:00
|
|
|
initialState := []*mautrix.Event{{
|
|
|
|
Type: mautrix.StatePowerLevels,
|
|
|
|
Content: mautrix.Content{
|
|
|
|
PowerLevels: portal.GetBasePowerLevels(),
|
|
|
|
},
|
|
|
|
}}
|
2019-06-01 19:03:29 +02:00
|
|
|
if len(portal.AvatarURL) > 0 {
|
2019-05-22 22:27:58 +02:00
|
|
|
initialState = append(initialState, &mautrix.Event{
|
|
|
|
Type: mautrix.StateRoomAvatar,
|
2019-01-11 20:17:31 +01:00
|
|
|
Content: mautrix.Content{
|
2019-06-01 19:03:29 +02:00
|
|
|
URL: portal.AvatarURL,
|
2018-08-26 15:11:48 +02:00
|
|
|
},
|
2019-05-22 22:27:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := intent.CreateRoom(&mautrix.ReqCreateRoom{
|
|
|
|
Visibility: "private",
|
|
|
|
Name: portal.Name,
|
|
|
|
Topic: portal.Topic,
|
|
|
|
Invite: []string{user.MXID},
|
|
|
|
Preset: "private_chat",
|
|
|
|
IsDirect: isPrivateChat,
|
|
|
|
InitialState: initialState,
|
2018-08-18 21:57:08 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
portal.MXID = resp.RoomID
|
|
|
|
portal.Update()
|
2019-05-22 22:27:58 +02:00
|
|
|
if metadata != nil {
|
|
|
|
portal.SyncParticipants(metadata)
|
2019-05-30 16:22:03 +02:00
|
|
|
} else {
|
|
|
|
customPuppet := portal.bridge.GetPuppetByCustomMXID(user.MXID)
|
2019-06-01 18:20:06 +02:00
|
|
|
if customPuppet != nil && customPuppet.CustomIntent() != nil {
|
2019-05-30 16:22:03 +02:00
|
|
|
_ = customPuppet.CustomIntent().EnsureJoined(portal.MXID)
|
|
|
|
}
|
2019-05-22 22:27:58 +02:00
|
|
|
}
|
2019-08-10 14:44:05 +02:00
|
|
|
user.addPortalToCommunity(portal)
|
2019-05-22 15:46:18 +02:00
|
|
|
err = portal.FillInitialHistory(user)
|
2019-05-21 22:44:14 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to fill history:", err)
|
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) IsPrivateChat() bool {
|
2018-09-01 22:38:03 +02:00
|
|
|
if portal.isPrivate == nil {
|
|
|
|
val := strings.HasSuffix(portal.Key.JID, whatsappExt.NewUserSuffix)
|
|
|
|
portal.isPrivate = &val
|
|
|
|
}
|
|
|
|
return *portal.isPrivate
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 23:38:11 +01:00
|
|
|
func (portal *Portal) IsStatusBroadcastRoom() bool {
|
|
|
|
return portal.Key.JID == "status@broadcast"
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
func (portal *Portal) MainIntent() *appservice.IntentAPI {
|
|
|
|
if portal.IsPrivateChat() {
|
2019-05-24 01:33:26 +02:00
|
|
|
return portal.bridge.GetPuppetByJID(portal.Key.JID).DefaultIntent()
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-08-30 00:10:26 +02:00
|
|
|
return portal.bridge.Bot
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 00:10:26 +02:00
|
|
|
func (portal *Portal) GetMessageIntent(user *User, info whatsapp.MessageInfo) *appservice.IntentAPI {
|
2018-08-23 00:12:26 +02:00
|
|
|
if info.FromMe {
|
2019-05-24 01:33:26 +02:00
|
|
|
return portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal)
|
2018-08-23 00:12:26 +02:00
|
|
|
} else if portal.IsPrivateChat() {
|
|
|
|
return portal.MainIntent()
|
2018-08-26 23:37:54 +02:00
|
|
|
} else if len(info.SenderJid) == 0 {
|
2018-08-27 00:06:27 +02:00
|
|
|
if len(info.Source.GetParticipant()) != 0 {
|
|
|
|
info.SenderJid = info.Source.GetParticipant()
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
return portal.bridge.GetPuppetByJID(info.SenderJid).IntentFor(portal)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
func (portal *Portal) SetReply(content *mautrix.Content, info whatsapp.MessageInfo) {
|
2018-08-23 23:52:06 +02:00
|
|
|
if len(info.QuotedMessageID) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
message := portal.bridge.DB.Message.GetByJID(portal.Key, info.QuotedMessageID)
|
2018-08-23 23:52:06 +02:00
|
|
|
if message != nil {
|
2018-08-24 18:46:14 +02:00
|
|
|
event, err := portal.MainIntent().GetEvent(portal.MXID, message.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to get reply target:", err)
|
|
|
|
return
|
|
|
|
}
|
2019-02-20 13:46:37 +01:00
|
|
|
event.Content.RemoveReplyFallback()
|
2018-08-24 18:46:14 +02:00
|
|
|
content.SetReply(event)
|
2018-08-23 23:52:06 +02:00
|
|
|
}
|
|
|
|
return
|
2018-08-24 21:05:38 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 00:59:36 +02:00
|
|
|
func (portal *Portal) HandleMessageRevoke(user *User, message whatsappExt.MessageRevocation) {
|
|
|
|
msg := portal.bridge.DB.Message.GetByJID(portal.Key, message.Id)
|
|
|
|
if msg == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
var intent *appservice.IntentAPI
|
2019-05-16 00:59:36 +02:00
|
|
|
if message.FromMe {
|
|
|
|
if portal.IsPrivateChat() {
|
2019-05-24 01:33:26 +02:00
|
|
|
intent = portal.bridge.GetPuppetByJID(user.JID).CustomIntent()
|
2019-05-16 00:59:36 +02:00
|
|
|
} else {
|
2019-05-24 01:33:26 +02:00
|
|
|
intent = portal.bridge.GetPuppetByJID(user.JID).IntentFor(portal)
|
2019-05-16 00:59:36 +02:00
|
|
|
}
|
|
|
|
} else if len(message.Participant) > 0 {
|
2019-05-24 01:33:26 +02:00
|
|
|
intent = portal.bridge.GetPuppetByJID(message.Participant).IntentFor(portal)
|
|
|
|
}
|
|
|
|
if intent == nil {
|
|
|
|
intent = portal.MainIntent()
|
2019-05-16 00:59:36 +02:00
|
|
|
}
|
|
|
|
_, err := intent.RedactEvent(portal.MXID, msg.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to redact %s: %v", msg.JID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg.Delete()
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:00:36 +02:00
|
|
|
func (portal *Portal) HandleFakeMessage(source *User, message FakeMessage) {
|
|
|
|
if portal.isRecentlyHandled(message.ID) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-24 21:40:09 +02:00
|
|
|
_, err := portal.MainIntent().SendNotice(portal.MXID, message.Text)
|
2019-05-30 16:00:36 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to handle fake message %s: %v", message.ID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
portal.recentlyHandledLock.Lock()
|
|
|
|
index := portal.recentlyHandledIndex
|
|
|
|
portal.recentlyHandledIndex = (portal.recentlyHandledIndex + 1) % recentlyHandledLength
|
|
|
|
portal.recentlyHandledLock.Unlock()
|
|
|
|
portal.recentlyHandled[index] = message.ID
|
|
|
|
}
|
|
|
|
|
2019-05-24 01:33:26 +02:00
|
|
|
type MessageContent struct {
|
|
|
|
*mautrix.Content
|
|
|
|
IsCustomPuppet bool `json:"net.maunium.whatsapp.puppet,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessage) {
|
2019-05-28 13:12:35 +02:00
|
|
|
if !portal.startHandling(message.Info) {
|
2018-08-24 18:46:14 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2018-08-30 00:10:26 +02:00
|
|
|
intent := portal.GetMessageIntent(source, message.Info)
|
2018-08-23 00:12:26 +02:00
|
|
|
if intent == nil {
|
2018-08-19 17:21:38 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
content := &mautrix.Content{
|
2018-08-24 18:46:14 +02:00
|
|
|
Body: message.Text,
|
2019-01-11 20:17:31 +01:00
|
|
|
MsgType: mautrix.MsgText,
|
2018-08-24 18:46:14 +02:00
|
|
|
}
|
2018-08-24 21:05:38 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.bridge.Formatter.ParseWhatsApp(content)
|
2018-08-28 19:09:37 +02:00
|
|
|
portal.SetReply(content, message.Info)
|
2018-08-24 18:46:14 +02:00
|
|
|
|
2019-01-21 22:55:16 +01:00
|
|
|
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
2019-05-24 01:33:26 +02:00
|
|
|
resp, err := intent.SendMassagedMessageEvent(portal.MXID, mautrix.EventMessage, &MessageContent{content, intent.IsCustomPuppet}, int64(message.Info.Timestamp*1000))
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err)
|
|
|
|
return
|
|
|
|
}
|
2018-09-01 22:38:03 +02:00
|
|
|
portal.finishHandling(source, message.Info.Source, resp.EventID)
|
2018-08-19 17:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) HandleMediaMessage(source *User, download func() ([]byte, error), thumbnail []byte, info whatsapp.MessageInfo, mimeType, caption string) {
|
2019-05-28 13:12:35 +02:00
|
|
|
if !portal.startHandling(info) {
|
2018-08-24 18:46:14 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2018-08-30 00:10:26 +02:00
|
|
|
intent := portal.GetMessageIntent(source, info)
|
2018-08-23 00:12:26 +02:00
|
|
|
if intent == nil {
|
2018-08-19 17:21:38 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2018-08-23 23:52:06 +02:00
|
|
|
data, err := download()
|
2018-08-19 17:21:38 +02:00
|
|
|
if err != nil {
|
2019-05-15 23:35:04 +02:00
|
|
|
portal.log.Errorfln("Failed to download media for %s: %v", info.Id, err)
|
2018-08-19 17:21:38 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-23 23:52:06 +02:00
|
|
|
|
|
|
|
uploaded, err := intent.UploadBytes(data, mimeType)
|
2018-08-19 17:21:38 +02:00
|
|
|
if err != nil {
|
2019-05-15 23:35:04 +02:00
|
|
|
portal.log.Errorfln("Failed to upload media for %s: %v", err)
|
2018-08-19 17:21:38 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-27 22:15:05 +02:00
|
|
|
|
|
|
|
fileName := info.Id
|
|
|
|
exts, _ := mime.ExtensionsByType(mimeType)
|
|
|
|
if exts != nil && len(exts) > 0 {
|
|
|
|
fileName += exts[0]
|
2018-08-23 23:52:06 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
content := &mautrix.Content{
|
2018-08-27 22:15:05 +02:00
|
|
|
Body: fileName,
|
2018-08-23 23:52:06 +02:00
|
|
|
URL: uploaded.ContentURI,
|
2019-01-11 20:17:31 +01:00
|
|
|
Info: &mautrix.FileInfo{
|
2018-08-23 23:52:06 +02:00
|
|
|
Size: len(data),
|
|
|
|
MimeType: mimeType,
|
|
|
|
},
|
|
|
|
}
|
2018-08-27 22:15:05 +02:00
|
|
|
portal.SetReply(content, info)
|
2018-08-23 23:52:06 +02:00
|
|
|
|
|
|
|
if thumbnail != nil {
|
|
|
|
thumbnailMime := http.DetectContentType(thumbnail)
|
|
|
|
uploadedThumbnail, _ := intent.UploadBytes(thumbnail, thumbnailMime)
|
|
|
|
if uploadedThumbnail != nil {
|
|
|
|
content.Info.ThumbnailURL = uploadedThumbnail.ContentURI
|
|
|
|
cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
|
2019-01-11 20:17:31 +01:00
|
|
|
content.Info.ThumbnailInfo = &mautrix.FileInfo{
|
2018-08-23 23:52:06 +02:00
|
|
|
Size: len(thumbnail),
|
|
|
|
Width: cfg.Width,
|
|
|
|
Height: cfg.Height,
|
|
|
|
MimeType: thumbnailMime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(strings.Split(mimeType, "/")[0]) {
|
|
|
|
case "image":
|
2019-01-11 20:17:31 +01:00
|
|
|
content.MsgType = mautrix.MsgImage
|
2018-08-23 23:52:06 +02:00
|
|
|
cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
|
|
|
|
content.Info.Width = cfg.Width
|
|
|
|
content.Info.Height = cfg.Height
|
|
|
|
case "video":
|
2019-01-11 20:17:31 +01:00
|
|
|
content.MsgType = mautrix.MsgVideo
|
2018-08-23 23:52:06 +02:00
|
|
|
case "audio":
|
2019-01-11 20:17:31 +01:00
|
|
|
content.MsgType = mautrix.MsgAudio
|
2018-08-23 23:52:06 +02:00
|
|
|
default:
|
2019-01-11 20:17:31 +01:00
|
|
|
content.MsgType = mautrix.MsgFile
|
2018-08-23 23:52:06 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:55:16 +01:00
|
|
|
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
2018-08-27 22:15:05 +02:00
|
|
|
ts := int64(info.Timestamp * 1000)
|
2019-05-24 01:33:26 +02:00
|
|
|
resp, err := intent.SendMassagedMessageEvent(portal.MXID, mautrix.EventMessage, &MessageContent{content, intent.IsCustomPuppet}, ts)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to handle message %s: %v", info.Id, err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-27 22:15:05 +02:00
|
|
|
|
|
|
|
if len(caption) > 0 {
|
2019-01-11 20:17:31 +01:00
|
|
|
captionContent := &mautrix.Content{
|
2018-08-27 22:15:05 +02:00
|
|
|
Body: caption,
|
2019-01-11 20:17:31 +01:00
|
|
|
MsgType: mautrix.MsgNotice,
|
2018-08-27 22:15:05 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.bridge.Formatter.ParseWhatsApp(captionContent)
|
2018-08-27 22:15:05 +02:00
|
|
|
|
2019-05-24 01:33:26 +02:00
|
|
|
_, err := intent.SendMassagedMessageEvent(portal.MXID, mautrix.EventMessage, &MessageContent{captionContent, intent.IsCustomPuppet}, ts)
|
2018-08-27 22:15:05 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to handle caption of message %s: %v", info.Id, err)
|
|
|
|
}
|
|
|
|
// TODO store caption mxid?
|
|
|
|
}
|
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
portal.finishHandling(source, info.Source, resp.EventID)
|
2018-08-19 17:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 23:26:24 +02:00
|
|
|
func makeMessageID() *string {
|
2018-08-24 18:46:14 +02:00
|
|
|
b := make([]byte, 10)
|
|
|
|
rand.Read(b)
|
2018-08-25 23:26:24 +02:00
|
|
|
str := strings.ToUpper(hex.EncodeToString(b))
|
|
|
|
return &str
|
2018-08-24 18:46:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
func (portal *Portal) downloadThumbnail(evt *mautrix.Event) []byte {
|
2018-08-25 23:26:24 +02:00
|
|
|
if evt.Content.Info == nil || len(evt.Content.Info.ThumbnailURL) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbnail, err := portal.MainIntent().DownloadBytes(evt.Content.Info.ThumbnailURL)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to download thumbnail in %s: %v", evt.ID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
thumbnailType := http.DetectContentType(thumbnail)
|
|
|
|
var img image.Image
|
|
|
|
switch thumbnailType {
|
|
|
|
case "image/png":
|
|
|
|
img, err = png.Decode(bytes.NewReader(thumbnail))
|
|
|
|
case "image/gif":
|
|
|
|
img, err = gif.Decode(bytes.NewReader(thumbnail))
|
|
|
|
case "image/jpeg":
|
|
|
|
return thumbnail
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = jpeg.Encode(&buf, img, &jpeg.Options{
|
|
|
|
Quality: jpeg.DefaultQuality,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to re-encode thumbnail in %s: %v", evt.ID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
func (portal *Portal) preprocessMatrixMedia(sender *User, evt *mautrix.Event, mediaType whatsapp.MediaType) *MediaUpload {
|
2018-08-24 21:31:18 +02:00
|
|
|
if evt.Content.Info == nil {
|
2019-01-11 20:17:31 +01:00
|
|
|
evt.Content.Info = &mautrix.FileInfo{}
|
2018-08-24 21:31:18 +02:00
|
|
|
}
|
|
|
|
caption := evt.Content.Body
|
|
|
|
exts, err := mime.ExtensionsByType(evt.Content.Info.MimeType)
|
|
|
|
for _, ext := range exts {
|
|
|
|
if strings.HasSuffix(caption, ext) {
|
|
|
|
caption = ""
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-08-25 23:26:24 +02:00
|
|
|
content, err := portal.MainIntent().DownloadBytes(evt.Content.URL)
|
2018-08-24 21:31:18 +02:00
|
|
|
if err != nil {
|
2018-08-26 23:37:54 +02:00
|
|
|
portal.log.Errorfln("Failed to download media in %s: %v", evt.ID, err)
|
2018-08-25 23:26:24 +02:00
|
|
|
return nil
|
2018-08-24 21:31:18 +02:00
|
|
|
}
|
2018-08-25 23:26:24 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
url, mediaKey, fileEncSHA256, fileSHA256, fileLength, err := sender.Conn.Upload(bytes.NewReader(content), mediaType)
|
2018-08-25 23:26:24 +02:00
|
|
|
if err != nil {
|
2018-08-26 23:37:54 +02:00
|
|
|
portal.log.Errorfln("Failed to upload media in %s: %v", evt.ID, err)
|
2018-08-25 23:26:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MediaUpload{
|
|
|
|
Caption: caption,
|
|
|
|
URL: url,
|
|
|
|
MediaKey: mediaKey,
|
|
|
|
FileEncSHA256: fileEncSHA256,
|
|
|
|
FileSHA256: fileSHA256,
|
|
|
|
FileLength: fileLength,
|
|
|
|
Thumbnail: portal.downloadThumbnail(evt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MediaUpload struct {
|
|
|
|
Caption string
|
|
|
|
URL string
|
|
|
|
MediaKey []byte
|
|
|
|
FileEncSHA256 []byte
|
|
|
|
FileSHA256 []byte
|
|
|
|
FileLength uint64
|
|
|
|
Thumbnail []byte
|
|
|
|
}
|
|
|
|
|
2019-01-11 20:17:31 +01:00
|
|
|
func (portal *Portal) HandleMatrixMessage(sender *User, evt *mautrix.Event) {
|
2018-08-28 23:40:54 +02:00
|
|
|
if portal.IsPrivateChat() && sender.JID != portal.Key.Receiver {
|
|
|
|
return
|
|
|
|
}
|
2019-01-21 22:55:16 +01:00
|
|
|
portal.log.Debugfln("Received event %s", evt.ID)
|
2018-08-28 23:40:54 +02:00
|
|
|
|
2018-08-25 23:26:24 +02:00
|
|
|
ts := uint64(evt.Timestamp / 1000)
|
|
|
|
status := waProto.WebMessageInfo_ERROR
|
|
|
|
fromMe := true
|
|
|
|
info := &waProto.WebMessageInfo{
|
|
|
|
Key: &waProto.MessageKey{
|
|
|
|
FromMe: &fromMe,
|
|
|
|
Id: makeMessageID(),
|
2018-08-28 23:40:54 +02:00
|
|
|
RemoteJid: &portal.Key.JID,
|
2018-08-25 23:26:24 +02:00
|
|
|
},
|
|
|
|
MessageTimestamp: &ts,
|
|
|
|
Message: &waProto.Message{},
|
|
|
|
Status: &status,
|
|
|
|
}
|
|
|
|
ctxInfo := &waProto.ContextInfo{}
|
|
|
|
replyToID := evt.Content.GetReplyTo()
|
|
|
|
if len(replyToID) > 0 {
|
|
|
|
evt.Content.RemoveReplyFallback()
|
|
|
|
msg := portal.bridge.DB.Message.GetByMXID(replyToID)
|
2018-09-01 22:38:03 +02:00
|
|
|
if msg != nil && msg.Content != nil {
|
|
|
|
ctxInfo.StanzaId = &msg.JID
|
|
|
|
ctxInfo.Participant = &msg.Sender
|
2019-05-23 21:57:19 +02:00
|
|
|
ctxInfo.QuotedMessage = msg.Content
|
2018-08-25 23:26:24 +02:00
|
|
|
}
|
2018-08-24 21:31:18 +02:00
|
|
|
}
|
2018-08-19 17:21:38 +02:00
|
|
|
var err error
|
|
|
|
switch evt.Content.MsgType {
|
2019-01-11 20:17:31 +01:00
|
|
|
case mautrix.MsgText, mautrix.MsgEmote:
|
2018-08-23 23:52:06 +02:00
|
|
|
text := evt.Content.Body
|
2019-01-11 20:17:31 +01:00
|
|
|
if evt.Content.Format == mautrix.FormatHTML {
|
2018-08-28 23:40:54 +02:00
|
|
|
text = portal.bridge.Formatter.ParseMatrix(evt.Content.FormattedBody)
|
2018-08-23 23:52:06 +02:00
|
|
|
}
|
2019-01-11 20:17:31 +01:00
|
|
|
if evt.Content.MsgType == mautrix.MsgEmote {
|
2018-08-24 21:31:18 +02:00
|
|
|
text = "/me " + text
|
|
|
|
}
|
2018-08-25 23:26:24 +02:00
|
|
|
ctxInfo.MentionedJid = mentionRegex.FindAllString(text, -1)
|
|
|
|
for index, mention := range ctxInfo.MentionedJid {
|
2018-08-26 00:55:21 +02:00
|
|
|
ctxInfo.MentionedJid[index] = mention[1:] + whatsappExt.NewUserSuffix
|
2018-08-25 23:26:24 +02:00
|
|
|
}
|
|
|
|
if ctxInfo.StanzaId != nil || ctxInfo.MentionedJid != nil {
|
|
|
|
info.Message.ExtendedTextMessage = &waProto.ExtendedTextMessage{
|
|
|
|
Text: &text,
|
|
|
|
ContextInfo: ctxInfo,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info.Message.Conversation = &text
|
|
|
|
}
|
2019-01-11 20:17:31 +01:00
|
|
|
case mautrix.MsgImage:
|
2018-08-28 23:40:54 +02:00
|
|
|
media := portal.preprocessMatrixMedia(sender, evt, whatsapp.MediaImage)
|
2018-08-25 23:26:24 +02:00
|
|
|
if media == nil {
|
2018-08-24 21:31:18 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-25 23:26:24 +02:00
|
|
|
info.Message.ImageMessage = &waProto.ImageMessage{
|
|
|
|
Caption: &media.Caption,
|
|
|
|
JpegThumbnail: media.Thumbnail,
|
|
|
|
Url: &media.URL,
|
|
|
|
MediaKey: media.MediaKey,
|
|
|
|
Mimetype: &evt.Content.GetInfo().MimeType,
|
|
|
|
FileEncSha256: media.FileEncSHA256,
|
|
|
|
FileSha256: media.FileSHA256,
|
|
|
|
FileLength: &media.FileLength,
|
|
|
|
}
|
2019-01-11 20:17:31 +01:00
|
|
|
case mautrix.MsgVideo:
|
2018-08-28 23:40:54 +02:00
|
|
|
media := portal.preprocessMatrixMedia(sender, evt, whatsapp.MediaVideo)
|
2018-08-25 23:26:24 +02:00
|
|
|
if media == nil {
|
2018-08-24 21:31:18 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-25 23:26:24 +02:00
|
|
|
duration := uint32(evt.Content.GetInfo().Duration)
|
|
|
|
info.Message.VideoMessage = &waProto.VideoMessage{
|
|
|
|
Caption: &media.Caption,
|
|
|
|
JpegThumbnail: media.Thumbnail,
|
|
|
|
Url: &media.URL,
|
|
|
|
MediaKey: media.MediaKey,
|
|
|
|
Mimetype: &evt.Content.GetInfo().MimeType,
|
|
|
|
Seconds: &duration,
|
|
|
|
FileEncSha256: media.FileEncSHA256,
|
|
|
|
FileSha256: media.FileSHA256,
|
|
|
|
FileLength: &media.FileLength,
|
|
|
|
}
|
2019-01-11 20:17:31 +01:00
|
|
|
case mautrix.MsgAudio:
|
2018-08-28 23:40:54 +02:00
|
|
|
media := portal.preprocessMatrixMedia(sender, evt, whatsapp.MediaAudio)
|
2018-08-25 23:26:24 +02:00
|
|
|
if media == nil {
|
2018-08-24 21:31:18 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-25 23:26:24 +02:00
|
|
|
duration := uint32(evt.Content.GetInfo().Duration)
|
|
|
|
info.Message.AudioMessage = &waProto.AudioMessage{
|
|
|
|
Url: &media.URL,
|
|
|
|
MediaKey: media.MediaKey,
|
|
|
|
Mimetype: &evt.Content.GetInfo().MimeType,
|
|
|
|
Seconds: &duration,
|
|
|
|
FileEncSha256: media.FileEncSHA256,
|
|
|
|
FileSha256: media.FileSHA256,
|
|
|
|
FileLength: &media.FileLength,
|
|
|
|
}
|
2019-01-11 20:17:31 +01:00
|
|
|
case mautrix.MsgFile:
|
2018-08-28 23:40:54 +02:00
|
|
|
media := portal.preprocessMatrixMedia(sender, evt, whatsapp.MediaDocument)
|
2018-08-25 23:26:24 +02:00
|
|
|
if media == nil {
|
2018-08-24 21:31:18 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-25 23:26:24 +02:00
|
|
|
info.Message.DocumentMessage = &waProto.DocumentMessage{
|
|
|
|
Url: &media.URL,
|
|
|
|
MediaKey: media.MediaKey,
|
|
|
|
Mimetype: &evt.Content.GetInfo().MimeType,
|
|
|
|
FileEncSha256: media.FileEncSHA256,
|
|
|
|
FileSha256: media.FileSHA256,
|
|
|
|
FileLength: &media.FileLength,
|
|
|
|
}
|
2018-08-19 17:21:38 +02:00
|
|
|
default:
|
|
|
|
portal.log.Debugln("Unhandled Matrix event:", evt)
|
|
|
|
return
|
|
|
|
}
|
2018-09-01 22:38:03 +02:00
|
|
|
portal.markHandled(sender, info, evt.ID)
|
2019-01-21 22:55:16 +01:00
|
|
|
portal.log.Debugln("Sending event", evt.ID, "to WhatsApp")
|
2019-05-15 22:04:09 +02:00
|
|
|
_, err = sender.Conn.Send(info)
|
2018-08-19 17:21:38 +02:00
|
|
|
if err != nil {
|
2018-08-24 18:46:14 +02:00
|
|
|
portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err)
|
2018-08-19 17:21:38 +02:00
|
|
|
} else {
|
|
|
|
portal.log.Debugln("Handled Matrix event:", evt)
|
|
|
|
}
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2019-05-16 00:59:36 +02:00
|
|
|
|
|
|
|
func (portal *Portal) HandleMatrixRedaction(sender *User, evt *mautrix.Event) {
|
|
|
|
if portal.IsPrivateChat() && sender.JID != portal.Key.Receiver {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := portal.bridge.DB.Message.GetByMXID(evt.Redacts)
|
2019-06-18 07:24:17 +02:00
|
|
|
if msg == nil || msg.Sender != sender.JID {
|
2019-05-16 00:59:36 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := uint64(evt.Timestamp / 1000)
|
|
|
|
status := waProto.WebMessageInfo_PENDING
|
|
|
|
protoMsgType := waProto.ProtocolMessage_REVOKE
|
|
|
|
fromMe := true
|
|
|
|
info := &waProto.WebMessageInfo{
|
|
|
|
Key: &waProto.MessageKey{
|
|
|
|
FromMe: &fromMe,
|
|
|
|
Id: makeMessageID(),
|
|
|
|
RemoteJid: &portal.Key.JID,
|
|
|
|
},
|
|
|
|
MessageTimestamp: &ts,
|
|
|
|
Message: &waProto.Message{
|
|
|
|
ProtocolMessage: &waProto.ProtocolMessage{
|
|
|
|
Type: &protoMsgType,
|
|
|
|
Key: &waProto.MessageKey{
|
|
|
|
FromMe: &fromMe,
|
|
|
|
Id: &msg.JID,
|
|
|
|
RemoteJid: &portal.Key.JID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Status: &status,
|
|
|
|
}
|
|
|
|
_, err := sender.Conn.Send(info)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Error handling Matrix redaction: %s: %v", evt.ID, err)
|
|
|
|
} else {
|
|
|
|
portal.log.Debugln("Handled Matrix redaction:", evt)
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 19:14:32 +02:00
|
|
|
|
|
|
|
func (portal *Portal) Delete() {
|
|
|
|
portal.Portal.Delete()
|
|
|
|
delete(portal.bridge.portalsByJID, portal.Key)
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
delete(portal.bridge.portalsByMXID, portal.MXID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Cleanup(puppetsOnly bool) {
|
|
|
|
if len(portal.MXID) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
_, err := portal.MainIntent().LeaveRoom(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to leave private chat portal with main intent:", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
intent := portal.MainIntent()
|
|
|
|
members, err := intent.JoinedMembers(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to get portal members for cleanup:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for member, _ := range members.Joined {
|
2019-05-21 22:44:14 +02:00
|
|
|
if member == intent.UserID {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-16 19:14:32 +02:00
|
|
|
puppet := portal.bridge.GetPuppetByMXID(member)
|
|
|
|
if puppet != nil {
|
2019-05-24 01:33:26 +02:00
|
|
|
_, err = puppet.DefaultIntent().LeaveRoom(portal.MXID)
|
2019-05-21 20:06:27 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Error leaving as puppet while cleaning up portal:", err)
|
|
|
|
}
|
2019-05-16 19:14:32 +02:00
|
|
|
} else if !puppetsOnly {
|
|
|
|
_, err = intent.KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: member, Reason: "Deleting portal"})
|
2019-05-21 20:06:27 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Error kicking user while cleaning up portal:", err)
|
|
|
|
}
|
2019-05-16 19:14:32 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-21 22:44:14 +02:00
|
|
|
_, err = intent.LeaveRoom(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Error leaving with main intent while cleaning up portal:", err)
|
|
|
|
}
|
2019-05-16 19:14:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) HandleMatrixLeave(sender *User) {
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
portal.log.Debugln("User left private chat portal, cleaning up and deleting...")
|
|
|
|
portal.Delete()
|
|
|
|
portal.Cleanup(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) HandleMatrixKick(sender *User, event *mautrix.Event) {
|
|
|
|
// TODO
|
|
|
|
}
|