Browse Source

Send alerts about incoming calls

Tulir Asokan 6 năm trước cách đây
mục cha
commit
a9fd97932b
4 tập tin đã thay đổi với 127 bổ sung8 xóa
  1. 23 8
      portal.go
  2. 29 0
      user.go
  3. 72 0
      whatsapp-ext/call.go
  4. 3 0
      whatsapp-ext/jsonmessage.go

+ 23 - 8
portal.go

@@ -168,6 +168,9 @@ func (portal *Portal) handleMessageLoop() {
 }
 }
 
 
 func (portal *Portal) handleMessage(msg PortalMessage) {
 func (portal *Portal) handleMessage(msg PortalMessage) {
+	if len(portal.MXID) == 0 {
+		return
+	}
 	switch data := msg.data.(type) {
 	switch data := msg.data.(type) {
 	case whatsapp.TextMessage:
 	case whatsapp.TextMessage:
 		portal.HandleTextMessage(msg.source, data)
 		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)
 		portal.HandleMediaMessage(msg.source, data.Download, data.Thumbnail, data.Info, data.Type, data.Title)
 	case whatsappExt.MessageRevocation:
 	case whatsappExt.MessageRevocation:
 		portal.HandleMessageRevoke(msg.source, data)
 		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()
 	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 {
 type MessageContent struct {
 	*mautrix.Content
 	*mautrix.Content
 	IsCustomPuppet bool `json:"net.maunium.whatsapp.puppet,omitempty"`
 	IsCustomPuppet bool `json:"net.maunium.whatsapp.puppet,omitempty"`
 }
 }
 
 
 func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessage) {
 func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessage) {
-	if len(portal.MXID) == 0 {
-		return
-	}
-
 	if !portal.startHandling(message.Info) {
 	if !portal.startHandling(message.Info) {
 		return
 		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) {
 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) {
 	if !portal.startHandling(info) {
 		return
 		return
 	}
 	}

+ 29 - 0
user.go

@@ -489,6 +489,35 @@ func (user *User) HandleMessageRevoke(message whatsappExt.MessageRevocation) {
 	user.putMessage(PortalMessage{message.RemoteJid, user, message, 0})
 	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) {
 func (user *User) HandlePresence(info whatsappExt.Presence) {
 	puppet := user.bridge.GetPuppetByJID(info.SenderJID)
 	puppet := user.bridge.GetPuppetByJID(info.SenderJID)
 	switch info.Status {
 	switch info.Status {

+ 72 - 0
whatsapp-ext/call.go

@@ -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)
+		}
+	}
+}

+ 3 - 0
whatsapp-ext/jsonmessage.go

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