Add WhatsApp<->Matrix redaction bridging

This commit is contained in:
Tulir Asokan 2019-05-16 01:59:36 +03:00
parent ae92d71d5a
commit c1e1964fc5
6 changed files with 164 additions and 2 deletions

View file

@ -5,7 +5,7 @@
* [x] Formatted messages
* [x] Media/files
* [x] Replies
* [ ] Message redactions<sup>[1]</sup>
* [x] Message redactions
* [ ] Presence<sup>[4]</sup>
* [ ] Typing notifications<sup>[4]</sup>
* [ ] Read receipts<sup>[4]</sup>
@ -25,12 +25,13 @@
* [x] Plain text
* [x] Formatted messages
* [x] Media/files
* [ ] Location messages
* [x] Replies
* [ ] Chat types
* [x] Private chat
* [x] Group chat
* [ ] Broadcast list<sup>[2]</sup>
* [ ] Message deletions<sup>[1]</sup>
* [x] Message deletions
* [x] Avatars
* [x] Presence
* [x] Typing notifications

View file

@ -151,3 +151,10 @@ func (msg *Message) Insert() {
msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err)
}
}
func (msg *Message) Delete() {
_, err := msg.db.Exec("DELETE FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", msg.Chat.JID, msg.Chat.Receiver, msg.JID)
if err != nil {
msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, err)
}
}

View file

@ -43,6 +43,7 @@ func NewMatrixHandler(bridge *Bridge) *MatrixHandler {
cmd: NewCommandHandler(bridge),
}
bridge.EventProcessor.On(mautrix.EventMessage, handler.HandleMessage)
bridge.EventProcessor.On(mautrix.EventRedaction, handler.HandleRedaction)
bridge.EventProcessor.On(mautrix.StateMember, handler.HandleMembership)
bridge.EventProcessor.On(mautrix.StateRoomName, handler.HandleRoomMetadata)
bridge.EventProcessor.On(mautrix.StateRoomAvatar, handler.HandleRoomMetadata)
@ -180,3 +181,32 @@ func (mx *MatrixHandler) HandleMessage(evt *mautrix.Event) {
portal.HandleMatrixMessage(user, evt)
}
}
func (mx *MatrixHandler) HandleRedaction(evt *mautrix.Event) {
if _, isPuppet := mx.bridge.ParsePuppetMXID(evt.Sender); evt.Sender == mx.bridge.Bot.UserID || isPuppet {
return
}
roomID := types.MatrixRoomID(evt.RoomID)
user := mx.bridge.GetUserByMXID(types.MatrixUserID(evt.Sender))
if !user.Whitelisted {
return
}
if !user.IsLoggedIn() {
return
} else if !user.Connected {
msg := format.RenderMarkdown(fmt.Sprintf("[%[1]s](https://matrix.to/#/%[1]s): \u26a0 " +
"You are not connected to WhatsApp, so your redaction was not bridged. " +
"Use `%[2]s reconnect` to reconnect.", user.MXID, mx.bridge.Config.Bridge.CommandPrefix))
msg.MsgType = mautrix.MsgNotice
_, _ = mx.bridge.Bot.SendMessageEvent(roomID, mautrix.EventMessage, msg)
return
}
portal := mx.bridge.GetPortalByMXID(roomID)
if portal != nil {
portal.HandleMatrixRedaction(user, evt)
}
}

View file

@ -35,6 +35,7 @@ import (
waProto "github.com/Rhymen/go-whatsapp/binary/proto"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix-appservice"
@ -580,6 +581,29 @@ func (portal *Portal) SetReply(content *mautrix.Content, info whatsapp.MessageIn
return
}
func (portal *Portal) HandleMessageRevoke(user *User, message whatsappExt.MessageRevocation) {
msg := portal.bridge.DB.Message.GetByJID(portal.Key, message.Id)
if msg == nil {
return
}
intent := portal.MainIntent()
if message.FromMe {
if portal.IsPrivateChat() {
// TODO handle
} else {
intent = portal.bridge.GetPuppetByJID(user.JID).Intent()
}
} else if len(message.Participant) > 0 {
intent = portal.bridge.GetPuppetByJID(message.Participant).Intent()
}
_, err := intent.RedactEvent(portal.MXID, msg.MXID)
if err != nil {
portal.log.Errorln("Failed to redact %s: %v", msg.JID, err)
return
}
msg.Delete()
}
func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessage) {
lock, ok := portal.startHandling(message.Info.Id)
if !ok {
@ -926,3 +950,44 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *mautrix.Event) {
portal.log.Debugln("Handled Matrix event:", evt)
}
}
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)
if msg.Sender != sender.JID {
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)
}
}

View file

@ -286,6 +286,11 @@ func (user *User) HandleDocumentMessage(message whatsapp.DocumentMessage) {
portal.HandleMediaMessage(user, message.Download, message.Thumbnail, message.Info, message.Type, message.Title)
}
func (user *User) HandleMessageRevoke(message whatsappExt.MessageRevocation) {
portal := user.GetPortalByJID(message.RemoteJid)
portal.HandleMessageRevoke(user, message)
}
func (user *User) HandlePresence(info whatsappExt.Presence) {
puppet := user.bridge.GetPuppetByJID(info.SenderJID)
switch info.Status {

View file

@ -0,0 +1,54 @@
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2019 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 whatsappExt
import (
"github.com/Rhymen/go-whatsapp"
"github.com/Rhymen/go-whatsapp/binary/proto"
)
type MessageRevokeHandler interface {
whatsapp.Handler
HandleMessageRevoke(key MessageRevocation)
}
type MessageRevocation struct {
Id string
RemoteJid string
FromMe bool
Participant string
}
func (ext *ExtendedConn) HandleRawMessage(message *proto.WebMessageInfo) {
protoMsg := message.GetMessage().GetProtocolMessage()
if protoMsg.GetType() == proto.ProtocolMessage_REVOKE {
key := protoMsg.GetKey()
deletedMessage := MessageRevocation{
Id: key.GetId(),
RemoteJid: key.GetRemoteJid(),
FromMe: key.GetFromMe(),
Participant: key.GetParticipant(),
}
for _, handler := range ext.handlers {
mrHandler, ok := handler.(MessageRevokeHandler)
if !ok {
continue
}
mrHandler.HandleMessageRevoke(deletedMessage)
}
}
}