2018-08-16 14:59:18 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// Copyright (C) 2018 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 (
|
2018-08-24 18:46:14 +02:00
|
|
|
"bytes"
|
|
|
|
"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"
|
2018-08-18 21:57:08 +02:00
|
|
|
"strings"
|
2018-08-23 00:12:26 +02:00
|
|
|
"sync"
|
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"
|
2018-08-24 18:46:14 +02:00
|
|
|
"maunium.net/go/gomatrix"
|
|
|
|
log "maunium.net/go/maulogger"
|
|
|
|
"maunium.net/go/mautrix-appservice"
|
|
|
|
"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 {
|
2018-08-28 23:40:54 +02:00
|
|
|
dbPortal := bridge.DB.Portal.GetByMXID(mxid)
|
|
|
|
if dbPortal == nil {
|
2018-08-16 14:59:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
portal = bridge.NewPortal(dbPortal)
|
|
|
|
bridge.portalsByJID[portal.Key] = portal
|
2018-08-16 14:59:18 +02:00
|
|
|
if len(portal.MXID) > 0 {
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.portalsByMXID[portal.MXID] = portal
|
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 {
|
2018-08-28 23:40:54 +02:00
|
|
|
dbPortal := bridge.DB.Portal.GetByJID(key)
|
2018-08-16 14:59:18 +02:00
|
|
|
if dbPortal == nil {
|
2018-08-28 23:40:54 +02:00
|
|
|
dbPortal = bridge.DB.Portal.New()
|
|
|
|
dbPortal.Key = key
|
2018-08-18 21:57:08 +02:00
|
|
|
dbPortal.Insert()
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
portal = bridge.NewPortal(dbPortal)
|
|
|
|
bridge.portalsByJID[portal.Key] = portal
|
2018-08-16 14:59:18 +02:00
|
|
|
if len(portal.MXID) > 0 {
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.portalsByMXID[portal.MXID] = portal
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return portal
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) GetAllPortals() []*Portal {
|
|
|
|
bridge.portalsLock.Lock()
|
|
|
|
defer bridge.portalsLock.Unlock()
|
|
|
|
dbPortals := bridge.DB.Portal.GetAll()
|
2018-08-16 14:59:18 +02:00
|
|
|
output := make([]*Portal, len(dbPortals))
|
|
|
|
for index, dbPortal := range dbPortals {
|
2018-08-28 23:40:54 +02:00
|
|
|
portal, ok := bridge.portalsByJID[dbPortal.Key]
|
2018-08-16 14:59:18 +02:00
|
|
|
if !ok {
|
2018-08-28 23:40:54 +02:00
|
|
|
portal = bridge.NewPortal(dbPortal)
|
|
|
|
bridge.portalsByJID[portal.Key] = portal
|
2018-08-16 14:59:18 +02:00
|
|
|
if len(dbPortal.MXID) > 0 {
|
2018-08-28 23:40:54 +02:00
|
|
|
bridge.portalsByMXID[dbPortal.MXID] = portal
|
2018-08-16 14:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
output[index] = portal
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (bridge *Bridge) NewPortal(dbPortal *database.Portal) *Portal {
|
2018-08-16 14:59:18 +02:00
|
|
|
return &Portal{
|
|
|
|
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
|
|
|
|
|
|
|
messageLocks: make(map[types.WhatsAppMessageID]sync.Mutex),
|
|
|
|
recentlyHandled: [20]types.WhatsAppMessageID{},
|
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
|
|
|
|
2018-08-30 23:13:08 +02:00
|
|
|
roomCreateLock sync.Mutex
|
|
|
|
messageLocksLock sync.Mutex
|
|
|
|
messageLocks map[types.WhatsAppMessageID]sync.Mutex
|
|
|
|
|
|
|
|
recentlyHandled [20]types.WhatsAppMessageID
|
|
|
|
recentlyHandledLock sync.Mutex
|
|
|
|
recentlyHandledIndex uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) getMessageLock(messageID types.WhatsAppMessageID) sync.Mutex {
|
|
|
|
portal.messageLocksLock.Lock()
|
|
|
|
defer portal.messageLocksLock.Unlock()
|
|
|
|
lock, ok := portal.messageLocks[messageID]
|
|
|
|
if !ok {
|
|
|
|
portal.messageLocks[messageID] = lock
|
|
|
|
}
|
|
|
|
return lock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) deleteMessageLock(messageID types.WhatsAppMessageID) {
|
|
|
|
portal.messageLocksLock.Lock()
|
|
|
|
delete(portal.messageLocks, messageID)
|
|
|
|
portal.messageLocksLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) isRecentlyHandled(id types.WhatsAppMessageID) bool {
|
|
|
|
start := portal.recentlyHandledIndex
|
|
|
|
for i := start; i != start; i = (i - 1) % 20 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) markHandled(jid types.WhatsAppMessageID, mxid types.MatrixEventID) {
|
|
|
|
msg := portal.bridge.DB.Message.New()
|
|
|
|
msg.Chat = portal.Key
|
|
|
|
msg.JID = jid
|
|
|
|
msg.MXID = mxid
|
|
|
|
msg.Insert()
|
|
|
|
|
|
|
|
portal.recentlyHandledLock.Lock()
|
|
|
|
index := portal.recentlyHandledIndex
|
|
|
|
portal.recentlyHandledIndex = (portal.recentlyHandledIndex + 1) % 20
|
|
|
|
portal.recentlyHandledLock.Unlock()
|
|
|
|
portal.recentlyHandled[index] = jid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) startHandling(id types.WhatsAppMessageID) (*sync.Mutex, bool) {
|
|
|
|
if portal.isRecentlyHandled(id) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
lock := portal.getMessageLock(id)
|
|
|
|
lock.Lock()
|
|
|
|
if portal.isDuplicate(id) {
|
|
|
|
lock.Unlock()
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return &lock, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) finishHandling(id types.WhatsAppMessageID, mxid types.MatrixEventID) {
|
|
|
|
portal.markHandled(id, mxid)
|
|
|
|
portal.deleteMessageLock(id)
|
|
|
|
portal.log.Debugln("Handled message", id, "->", 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
|
|
|
puppet := portal.bridge.GetPuppetByJID(participant.JID)
|
2018-08-26 15:11:48 +02:00
|
|
|
puppet.Intent().EnsureJoined(portal.MXID)
|
2018-08-26 15:19:50 +02:00
|
|
|
|
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) {
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.MainIntent().InviteUser(portal.MXID, &gomatrix.ReqInviteUser{
|
|
|
|
UserID: user.MXID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
if portal.Avatar == avatar.Tag {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := avatar.DownloadBytes()
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to download avatar:", err)
|
|
|
|
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 {
|
|
|
|
portal.log.Errorln("Failed to upload avatar:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = portal.MainIntent().SetRoomAvatar(portal.MXID, resp.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to set room topic:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
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 {
|
2018-08-28 23:40:54 +02:00
|
|
|
intent := portal.bridge.GetPuppetByJID(setBy).Intent()
|
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 {
|
2018-08-28 23:40:54 +02:00
|
|
|
intent := portal.bridge.GetPuppetByJID(setBy).Intent()
|
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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) Sync(user *User, contact whatsapp.Contact) {
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-19 17:21:38 +02:00
|
|
|
if len(portal.MXID) == 0 {
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.Name = contact.Name
|
|
|
|
err := portal.CreateMatrixRoom([]string{user.MXID})
|
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 {
|
|
|
|
portal.MainIntent().EnsureInvited(portal.MXID, user.MXID)
|
2018-08-19 17:21:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
update := false
|
2018-08-28 23:40:54 +02:00
|
|
|
update = portal.UpdateMetadata(user) || update
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 15:11:48 +02:00
|
|
|
func (portal *Portal) GetBasePowerLevels() *gomatrix.PowerLevels {
|
|
|
|
anyone := 0
|
|
|
|
nope := 99
|
|
|
|
return &gomatrix.PowerLevels{
|
|
|
|
UsersDefault: anyone,
|
|
|
|
EventsDefault: anyone,
|
|
|
|
RedactPtr: &anyone,
|
|
|
|
StateDefaultPtr: &nope,
|
|
|
|
BanPtr: &nope,
|
|
|
|
InvitePtr: &nope,
|
|
|
|
Users: map[string]int{
|
|
|
|
portal.MainIntent().UserID: 100,
|
|
|
|
},
|
2018-08-30 00:10:26 +02:00
|
|
|
Events: map[string]int{
|
|
|
|
gomatrix.StateRoomName.Type: anyone,
|
|
|
|
gomatrix.StateRoomAvatar.Type: anyone,
|
|
|
|
gomatrix.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
|
2018-08-28 23:40:54 +02:00
|
|
|
changed = levels.EnsureEventLevel(gomatrix.StateRoomName, newLevel) || changed
|
|
|
|
changed = levels.EnsureEventLevel(gomatrix.StateRoomAvatar, newLevel) || changed
|
|
|
|
changed = levels.EnsureEventLevel(gomatrix.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) CreateMatrixRoom(invite []string) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
name := portal.Name
|
2018-08-23 00:12:26 +02:00
|
|
|
topic := portal.Topic
|
2018-08-18 21:57:08 +02:00
|
|
|
isPrivateChat := false
|
2018-08-24 23:45:50 +02:00
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
name = ""
|
2018-08-18 21:57:08 +02:00
|
|
|
topic = "WhatsApp private chat"
|
|
|
|
isPrivateChat = true
|
|
|
|
}
|
2018-08-26 15:11:48 +02:00
|
|
|
|
2018-08-18 21:57:08 +02:00
|
|
|
resp, err := portal.MainIntent().CreateRoom(&gomatrix.ReqCreateRoom{
|
|
|
|
Visibility: "private",
|
|
|
|
Name: name,
|
|
|
|
Topic: topic,
|
2018-08-24 23:45:50 +02:00
|
|
|
Invite: invite,
|
2018-08-18 21:57:08 +02:00
|
|
|
Preset: "private_chat",
|
|
|
|
IsDirect: isPrivateChat,
|
2018-08-26 15:11:48 +02:00
|
|
|
InitialState: []*gomatrix.Event{{
|
|
|
|
Type: gomatrix.StatePowerLevels,
|
|
|
|
Content: gomatrix.Content{
|
2018-08-26 16:02:32 +02:00
|
|
|
PowerLevels: *portal.GetBasePowerLevels(),
|
2018-08-26 15:11:48 +02:00
|
|
|
},
|
|
|
|
}},
|
2018-08-18 21:57:08 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
portal.MXID = resp.RoomID
|
|
|
|
portal.Update()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) IsPrivateChat() bool {
|
2018-08-28 23:40:54 +02:00
|
|
|
return strings.HasSuffix(portal.Key.JID, whatsappExt.NewUserSuffix)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) MainIntent() *appservice.IntentAPI {
|
|
|
|
if portal.IsPrivateChat() {
|
2018-08-28 23:40:54 +02:00
|
|
|
return portal.bridge.GetPuppetByJID(portal.Key.JID).Intent()
|
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 {
|
2018-08-26 21:50:28 +02:00
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
// TODO handle own messages in private chats properly
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-30 00:10:26 +02:00
|
|
|
return portal.bridge.GetPuppetByJID(user.JID).Intent()
|
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
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
return portal.bridge.GetPuppetByJID(info.SenderJid).Intent()
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:46:14 +02:00
|
|
|
func (portal *Portal) SetReply(content *gomatrix.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
|
|
|
|
}
|
|
|
|
content.SetReply(event)
|
2018-08-23 23:52:06 +02:00
|
|
|
}
|
|
|
|
return
|
2018-08-24 21:05:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessage) {
|
2018-08-30 23:13:08 +02:00
|
|
|
lock, ok := portal.startHandling(message.Info.Id)
|
|
|
|
if !ok {
|
2018-08-23 00:12:26 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-30 23:13:08 +02:00
|
|
|
defer lock.Unlock()
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
err := portal.CreateMatrixRoom([]string{source.MXID})
|
2018-08-24 18:46:14 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
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
|
|
|
|
2018-08-28 19:09:37 +02:00
|
|
|
content := &gomatrix.Content{
|
2018-08-24 18:46:14 +02:00
|
|
|
Body: message.Text,
|
|
|
|
MsgType: gomatrix.MsgText,
|
|
|
|
}
|
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
|
|
|
|
2018-08-24 21:05:38 +02:00
|
|
|
intent.UserTyping(portal.MXID, false, 0)
|
2018-08-24 18:46:14 +02:00
|
|
|
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, 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-08-30 23:13:08 +02:00
|
|
|
portal.finishHandling(message.Info.Id, 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) {
|
2018-08-30 23:13:08 +02:00
|
|
|
lock, ok := portal.startHandling(info.Id)
|
|
|
|
if !ok {
|
2018-08-23 00:12:26 +02:00
|
|
|
return
|
|
|
|
}
|
2018-08-30 23:13:08 +02:00
|
|
|
defer lock.Unlock()
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
err := portal.CreateMatrixRoom([]string{source.MXID})
|
2018-08-24 18:46:14 +02:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
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 {
|
|
|
|
portal.log.Errorln("Failed to download media:", err)
|
|
|
|
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 {
|
|
|
|
portal.log.Errorln("Failed to upload media:", err)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-27 22:15:05 +02:00
|
|
|
content := &gomatrix.Content{
|
|
|
|
Body: fileName,
|
2018-08-23 23:52:06 +02:00
|
|
|
URL: uploaded.ContentURI,
|
2018-08-24 18:46:14 +02:00
|
|
|
Info: &gomatrix.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))
|
|
|
|
content.Info.ThumbnailInfo = &gomatrix.FileInfo{
|
|
|
|
Size: len(thumbnail),
|
|
|
|
Width: cfg.Width,
|
|
|
|
Height: cfg.Height,
|
|
|
|
MimeType: thumbnailMime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(strings.Split(mimeType, "/")[0]) {
|
|
|
|
case "image":
|
|
|
|
content.MsgType = gomatrix.MsgImage
|
|
|
|
cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
|
|
|
|
content.Info.Width = cfg.Width
|
|
|
|
content.Info.Height = cfg.Height
|
|
|
|
case "video":
|
|
|
|
content.MsgType = gomatrix.MsgVideo
|
|
|
|
case "audio":
|
|
|
|
content.MsgType = gomatrix.MsgAudio
|
|
|
|
default:
|
|
|
|
content.MsgType = gomatrix.MsgFile
|
|
|
|
}
|
|
|
|
|
2018-08-24 21:05:38 +02:00
|
|
|
intent.UserTyping(portal.MXID, false, 0)
|
2018-08-27 22:15:05 +02:00
|
|
|
ts := int64(info.Timestamp * 1000)
|
|
|
|
resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, 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 {
|
|
|
|
captionContent := &gomatrix.Content{
|
|
|
|
Body: caption,
|
|
|
|
MsgType: gomatrix.MsgNotice,
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.bridge.Formatter.ParseWhatsApp(captionContent)
|
2018-08-27 22:15:05 +02:00
|
|
|
|
|
|
|
_, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, captionContent, ts)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to handle caption of message %s: %v", info.Id, err)
|
|
|
|
}
|
|
|
|
// TODO store caption mxid?
|
|
|
|
}
|
|
|
|
|
2018-08-30 23:13:08 +02:00
|
|
|
portal.finishHandling(info.Id, 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
|
|
|
}
|
|
|
|
|
2018-08-25 23:26:24 +02:00
|
|
|
func (portal *Portal) downloadThumbnail(evt *gomatrix.Event) []byte {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) preprocessMatrixMedia(sender *User, evt *gomatrix.Event, mediaType whatsapp.MediaType) *MediaUpload {
|
2018-08-24 21:31:18 +02:00
|
|
|
if evt.Content.Info == nil {
|
|
|
|
evt.Content.Info = &gomatrix.FileInfo{}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) GetMessage(user *User, jid types.WhatsAppMessageID) *waProto.WebMessageInfo {
|
|
|
|
node, err := user.Conn.LoadMessagesBefore(portal.Key.JID, jid, 1)
|
2018-08-25 23:26:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
msgs, ok := node.Content.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
msg, ok := msgs[0].(*waProto.WebMessageInfo)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
node, err = user.Conn.LoadMessagesAfter(portal.Key.JID, msg.GetKey().GetId(), 1)
|
2018-08-25 23:26:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
msgs, ok = node.Content.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
msg, _ = msgs[0].(*waProto.WebMessageInfo)
|
|
|
|
return msg
|
2018-08-24 21:31:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) HandleMatrixMessage(sender *User, evt *gomatrix.Event) {
|
|
|
|
if portal.IsPrivateChat() && sender.JID != portal.Key.Receiver {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if msg != nil {
|
2018-08-28 23:40:54 +02:00
|
|
|
origMsg := portal.GetMessage(sender, msg.JID)
|
2018-08-25 23:26:24 +02:00
|
|
|
if origMsg != nil {
|
|
|
|
ctxInfo.StanzaId = &msg.JID
|
|
|
|
replyMsgSender := origMsg.GetParticipant()
|
|
|
|
if origMsg.GetKey().GetFromMe() {
|
2018-08-28 23:40:54 +02:00
|
|
|
replyMsgSender = sender.JID
|
2018-08-25 23:26:24 +02:00
|
|
|
}
|
|
|
|
ctxInfo.Participant = &replyMsgSender
|
|
|
|
ctxInfo.QuotedMessage = []*waProto.Message{origMsg.Message}
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 21:31:18 +02:00
|
|
|
}
|
2018-08-19 17:21:38 +02:00
|
|
|
var err error
|
|
|
|
switch evt.Content.MsgType {
|
2018-08-24 21:31:18 +02:00
|
|
|
case gomatrix.MsgText, gomatrix.MsgEmote:
|
2018-08-23 23:52:06 +02:00
|
|
|
text := evt.Content.Body
|
|
|
|
if evt.Content.Format == gomatrix.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
|
|
|
}
|
2018-08-24 21:31:18 +02:00
|
|
|
if evt.Content.MsgType == gomatrix.MsgEmote {
|
|
|
|
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
|
|
|
|
}
|
2018-08-24 21:31:18 +02:00
|
|
|
case gomatrix.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,
|
|
|
|
}
|
2018-08-24 21:31:18 +02:00
|
|
|
case gomatrix.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,
|
|
|
|
}
|
2018-08-24 21:31:18 +02:00
|
|
|
case gomatrix.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,
|
|
|
|
}
|
2018-08-24 21:31:18 +02:00
|
|
|
case gomatrix.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-08-30 23:13:08 +02:00
|
|
|
portal.markHandled(info.GetKey().GetId(), evt.ID)
|
2018-08-28 23:40:54 +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
|
|
|
}
|