From c1e1964fc560e20c1f2ab8f69a49eb2b87500edf Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 16 May 2019 01:59:36 +0300 Subject: [PATCH] Add WhatsApp<->Matrix redaction bridging --- ROADMAP.md | 5 +-- database/message.go | 7 ++++ matrix.go | 30 +++++++++++++++++ portal.go | 65 ++++++++++++++++++++++++++++++++++++ user.go | 5 +++ whatsapp-ext/protomessage.go | 54 ++++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 whatsapp-ext/protomessage.go diff --git a/ROADMAP.md b/ROADMAP.md index 9d56e79..2971d1e 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -5,7 +5,7 @@ * [x] Formatted messages * [x] Media/files * [x] Replies - * [ ] Message redactions[1] + * [x] Message redactions * [ ] Presence[4] * [ ] Typing notifications[4] * [ ] Read receipts[4] @@ -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[2] - * [ ] Message deletions[1] + * [x] Message deletions * [x] Avatars * [x] Presence * [x] Typing notifications diff --git a/database/message.go b/database/message.go index 2e5f7e0..b7910a2 100644 --- a/database/message.go +++ b/database/message.go @@ -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) + } +} diff --git a/matrix.go b/matrix.go index 96ccf92..38f1a72 100644 --- a/matrix.go +++ b/matrix.go @@ -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) + } +} diff --git a/portal.go b/portal.go index 4ffaaed..a9ecb9b 100644 --- a/portal.go +++ b/portal.go @@ -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) + } +} diff --git a/user.go b/user.go index e78da57..30d0e9e 100644 --- a/user.go +++ b/user.go @@ -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 { diff --git a/whatsapp-ext/protomessage.go b/whatsapp-ext/protomessage.go new file mode 100644 index 0000000..00520ed --- /dev/null +++ b/whatsapp-ext/protomessage.go @@ -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 . + +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) + } + } +}