Send alerts about incoming calls

This commit is contained in:
Tulir Asokan 2019-05-30 17:00:36 +03:00
parent 1c009b96fe
commit a9fd97932b
4 changed files with 127 additions and 8 deletions

View file

@ -168,6 +168,9 @@ func (portal *Portal) handleMessageLoop() {
}
func (portal *Portal) handleMessage(msg PortalMessage) {
if len(portal.MXID) == 0 {
return
}
switch data := msg.data.(type) {
case whatsapp.TextMessage:
portal.HandleTextMessage(msg.source, data)
@ -181,6 +184,8 @@ func (portal *Portal) handleMessage(msg PortalMessage) {
portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.Type, data.Title)
case whatsappExt.MessageRevocation:
portal.HandleMessageRevoke(msg.source, data)
case FakeMessage:
portal.HandleFakeMessage(msg.source, data)
}
}
@ -764,16 +769,30 @@ func (portal *Portal) HandleMessageRevoke(user *User, message whatsappExt.Messag
msg.Delete()
}
func (portal *Portal) HandleFakeMessage(source *User, message FakeMessage) {
if portal.isRecentlyHandled(message.ID) {
return
}
_, err := portal.MainIntent().SendText(portal.MXID, message.Text)
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
}
type MessageContent struct {
*mautrix.Content
IsCustomPuppet bool `json:"net.maunium.whatsapp.puppet,omitempty"`
}
func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessage) {
if len(portal.MXID) == 0 {
return
}
if !portal.startHandling(message.Info) {
return
}
@ -801,10 +820,6 @@ func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessa
}
func (portal *Portal) HandleMediaMessage(source *User, download func() ([]byte, error), thumbnail []byte, info whatsapp.MessageInfo, mimeType, caption string) {
if len(portal.MXID) == 0 {
return
}
if !portal.startHandling(info) {
return
}

29
user.go
View file

@ -489,6 +489,35 @@ func (user *User) HandleMessageRevoke(message whatsappExt.MessageRevocation) {
user.putMessage(PortalMessage{message.RemoteJid, user, message, 0})
}
type FakeMessage struct {
Text string
ID string
}
func (user *User) HandleCallInfo(info whatsappExt.CallInfo) {
if info.Data != nil {
return
}
data := FakeMessage{
ID: info.ID,
}
switch info.Type {
case whatsappExt.CallOffer:
data.Text = "Incoming call"
case whatsappExt.CallOfferVideo:
data.Text = "Incoming video call"
case whatsappExt.CallTerminate:
data.Text = "Call ended"
data.ID += "E"
default:
return
}
portal := user.GetPortalByJID(info.From)
if portal != nil {
portal.messages <- PortalMessage{info.From, user, data, 0}
}
}
func (user *User) HandlePresence(info whatsappExt.Presence) {
puppet := user.bridge.GetPuppetByJID(info.SenderJID)
switch info.Status {

72
whatsapp-ext/call.go Normal file
View file

@ -0,0 +1,72 @@
// 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 (
"encoding/json"
"strings"
"github.com/Rhymen/go-whatsapp"
)
type CallInfoType string
const (
CallOffer CallInfoType = "offer"
CallOfferVideo CallInfoType = "offer_video"
CallTransport CallInfoType = "transport"
CallRelayLatency CallInfoType = "relaylatency"
CallTerminate CallInfoType = "terminate"
)
type CallInfo struct {
ID string `json:"id"`
Type CallInfoType `json:"type"`
From string `json:"from"`
Platform string `json:"platform"`
Version []int `json:"version"`
Data [][]interface{} `json:"data"`
}
type CallInfoHandler interface {
whatsapp.Handler
HandleCallInfo(CallInfo)
}
func (ext *ExtendedConn) handleMessageCall(message []byte) {
var event CallInfo
err := json.Unmarshal(message, &event)
if err != nil {
ext.jsonParseError(err)
return
}
event.From = strings.Replace(event.From, OldUserSuffix, NewUserSuffix, 1)
for _, handler := range ext.handlers {
callInfoHandler, ok := handler.(CallInfoHandler)
if !ok {
continue
}
if ext.shouldCallSynchronously(callInfoHandler) {
callInfoHandler.HandleCallInfo(event)
} else {
go callInfoHandler.HandleCallInfo(event)
}
}
}

View file

@ -35,6 +35,7 @@ const (
MessageProps JSONMessageType = "Props"
MessageCmd JSONMessageType = "Cmd"
MessageChat JSONMessageType = "Chat"
MessageCall JSONMessageType = "Call"
)
func (ext *ExtendedConn) HandleError(error) {}
@ -85,6 +86,8 @@ func (ext *ExtendedConn) HandleJsonMessage(message string) {
ext.handleMessageCommand(msg[1])
case MessageChat:
ext.handleMessageChatUpdate(msg[1])
case MessageCall:
ext.handleMessageCall(msg[1])
default:
for _, handler := range ext.handlers {
ujmHandler, ok := handler.(UnhandledJSONMessageHandler)