From e4a160e6501d86c957a87979704859e483998b02 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 10 Jun 2020 14:06:36 +0300 Subject: [PATCH] Add basic bridging of contact messages to Matrix --- go.mod | 2 +- go.sum | 2 ++ portal.go | 41 +++++++++++++++++++++++++++++++++++++++++ user.go | 4 ++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5750e03..301de6d 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( gopkg.in/yaml.v2 v2.3.0 maunium.net/go/mauflag v1.0.0 maunium.net/go/maulogger/v2 v2.1.1 - maunium.net/go/mautrix v0.4.11 + maunium.net/go/mautrix v0.5.0-rc.2 ) replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.2.8 diff --git a/go.sum b/go.sum index afb23d8..aa1b9ae 100644 --- a/go.sum +++ b/go.sum @@ -65,3 +65,5 @@ maunium.net/go/mautrix v0.4.9 h1:zp4ihCuSfonsTbwOmBH5/akWwp4QEii+SU5QRAC9Foo= maunium.net/go/mautrix v0.4.9/go.mod h1:8Y+NqmROJyWYvvP4yPfX9tLM59VCfgE/kcQ0SeX68ho= maunium.net/go/mautrix v0.4.11 h1:cONVoAkD7AOvtzEMvuuq79Y+2vRrNKfpKZoR8HdyPAw= maunium.net/go/mautrix v0.4.11/go.mod h1:LnkFnB1yjCbb8V+upoEHDGvI/F38NHSTWYCe2RRJgSY= +maunium.net/go/mautrix v0.5.0-rc.2 h1:ohx+dprvMS6Txm+suMx5pbjl0rjDpfftFxgXhx/+Usc= +maunium.net/go/mautrix v0.5.0-rc.2/go.mod h1:LnkFnB1yjCbb8V+upoEHDGvI/F38NHSTWYCe2RRJgSY= diff --git a/portal.go b/portal.go index 1da167b..14d6350 100644 --- a/portal.go +++ b/portal.go @@ -205,6 +205,8 @@ func (portal *Portal) handleMessage(msg PortalMessage) { portal.HandleMediaMessage(msg.source, data.Download, nil, data.Info, data.ContextInfo, data.Type, "", false) case whatsapp.DocumentMessage: portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.ContextInfo, data.Type, data.Title, false) + case whatsapp.ContactMessage: + portal.HandleContactMessage(msg.source, data) case whatsappExt.MessageRevocation: portal.HandleMessageRevoke(msg.source, data) case FakeMessage: @@ -1087,6 +1089,45 @@ func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessa portal.finishHandling(source, message.Info.Source, resp.EventID) } +func (portal *Portal) HandleContactMessage(source *User, message whatsapp.ContactMessage) { + if !portal.startHandling(message.Info) { + return + } + + intent := portal.GetMessageIntent(source, message.Info) + if intent == nil { + return + } + + fileName := fmt.Sprintf("%s.vcf", message.DisplayName) + + uploadResp, err := intent.UploadBytesWithName([]byte(message.Vcard), "text/vcard", fileName) + if err != nil { + portal.log.Errorfln("Failed to upload vcard of %s: %v", message.DisplayName, err) + return + } + + content := &event.MessageEventContent{ + Body: fileName, + MsgType: event.MsgFile, + URL: uploadResp.ContentURI.CUString(), + Info: &event.FileInfo{ + MimeType: "text/vcard", + Size: len(message.Vcard), + }, + } + + portal.SetReply(content, message.ContextInfo) + + _, _ = intent.UserTyping(portal.MXID, false, 0) + resp, err := portal.sendMessage(intent, event.EventMessage, content, int64(message.Info.Timestamp*1000)) + if err != nil { + portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err) + return + } + portal.finishHandling(source, message.Info.Source, resp.EventID) +} + func (portal *Portal) sendMediaBridgeFailure(source *User, intent *appservice.IntentAPI, info whatsapp.MessageInfo, downloadErr error) { portal.log.Errorfln("Failed to download media for %s: %v", info.Id, downloadErr) resp, err := portal.sendMessage(intent, event.EventMessage, &event.MessageEventContent{ diff --git a/user.go b/user.go index d084bba..90ae503 100644 --- a/user.go +++ b/user.go @@ -694,6 +694,10 @@ func (user *User) HandleDocumentMessage(message whatsapp.DocumentMessage) { user.putMessage(PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}) } +func (user *User) HandleContactMessage(message whatsapp.ContactMessage) { + user.putMessage(PortalMessage{message.Info.RemoteJid, user, message, message.Info.Timestamp}) +} + func (user *User) HandleMessageRevoke(message whatsappExt.MessageRevocation) { user.putMessage(PortalMessage{message.RemoteJid, user, message, 0}) }