2019-02-21 20:28:13 +01:00
|
|
|
package bwhatsapp
|
|
|
|
|
|
|
|
import (
|
2020-01-09 18:14:01 +01:00
|
|
|
"fmt"
|
|
|
|
"mime"
|
2019-02-21 20:28:13 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2020-01-09 18:14:01 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2019-02-21 20:28:13 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
"go.mau.fi/whatsmeow/binary/proto"
|
|
|
|
"go.mau.fi/whatsmeow/types"
|
|
|
|
"go.mau.fi/whatsmeow/types/events"
|
|
|
|
)
|
2020-02-09 22:11:46 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
// nolint:gocritic
|
|
|
|
func (b *Bwhatsapp) eventHandler(evt interface{}) {
|
|
|
|
switch e := evt.(type) {
|
|
|
|
case *events.Message:
|
|
|
|
b.handleMessage(e)
|
2020-02-09 22:11:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) handleMessage(message *events.Message) {
|
|
|
|
msg := message.Message
|
|
|
|
switch {
|
|
|
|
case msg == nil, message.Info.IsFromMe, message.Info.Timestamp.Before(b.startedAt):
|
|
|
|
return
|
2020-02-09 22:11:46 +01:00
|
|
|
}
|
2020-11-27 00:34:44 +01:00
|
|
|
|
2022-02-04 00:33:50 +01:00
|
|
|
b.Log.Infof("Receiving message %#v", msg)
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
switch {
|
|
|
|
case msg.Conversation != nil || msg.ExtendedTextMessage != nil:
|
|
|
|
b.handleTextMessage(message.Info, msg)
|
|
|
|
case msg.VideoMessage != nil:
|
|
|
|
b.handleVideoMessage(message)
|
|
|
|
case msg.AudioMessage != nil:
|
|
|
|
b.handleAudioMessage(message)
|
|
|
|
case msg.DocumentMessage != nil:
|
|
|
|
b.handleDocumentMessage(message)
|
|
|
|
case msg.ImageMessage != nil:
|
|
|
|
b.handleImageMessage(message)
|
2020-02-09 22:11:46 +01:00
|
|
|
}
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.Message) {
|
|
|
|
senderJID := messageInfo.Sender
|
|
|
|
|
|
|
|
senderName := b.getSenderName(messageInfo.Sender)
|
|
|
|
if senderName == "" {
|
|
|
|
senderName = "Someone" // don't expose telephone number
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
2022-01-31 00:13:37 +01:00
|
|
|
|
|
|
|
if msg.GetExtendedTextMessage() == nil {
|
2019-02-21 20:28:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
text := msg.GetExtendedTextMessage().GetText()
|
|
|
|
ci := msg.GetExtendedTextMessage().GetContextInfo()
|
2019-02-21 20:28:13 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
|
|
|
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if ci.MentionedJid != nil {
|
2019-02-21 20:28:13 +01:00
|
|
|
// handle user mentions
|
2022-01-31 00:13:37 +01:00
|
|
|
for _, mentionedJID := range ci.MentionedJid {
|
2020-01-09 18:14:01 +01:00
|
|
|
numberAndSuffix := strings.SplitN(mentionedJID, "@", 2)
|
2019-02-21 20:28:13 +01:00
|
|
|
|
|
|
|
// mentions comes as telephone numbers and we don't want to expose it to other bridges
|
|
|
|
// replace it with something more meaninful to others
|
2022-01-31 00:13:37 +01:00
|
|
|
mention := b.getSenderNotify(types.NewJID(numberAndSuffix[0], types.DefaultUserServer))
|
2019-02-21 20:28:13 +01:00
|
|
|
if mention == "" {
|
|
|
|
mention = "someone"
|
|
|
|
}
|
2020-11-27 00:34:44 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
text = strings.Replace(text, "@"+numberAndSuffix[0], "@"+mention, 1)
|
2019-02-21 20:28:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := config.Message{
|
2022-01-31 00:13:37 +01:00
|
|
|
UserID: senderJID.String(),
|
2020-11-29 15:26:45 +01:00
|
|
|
Username: senderName,
|
2022-01-31 00:13:37 +01:00
|
|
|
Text: msg.GetExtendedTextMessage().GetText(),
|
|
|
|
Channel: ci.GetRemoteJid(),
|
2020-11-29 15:26:45 +01:00
|
|
|
Account: b.Account,
|
|
|
|
Protocol: b.Protocol,
|
|
|
|
Extra: make(map[string][]interface{}),
|
2022-01-31 00:13:37 +01:00
|
|
|
// ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
|
|
|
|
ID: messageInfo.ID,
|
2020-11-27 00:34:44 +01:00
|
|
|
}
|
2019-02-21 20:28:13 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
2019-02-21 20:28:13 +01:00
|
|
|
rmsg.Avatar = avatarURL
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:26:45 +01:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
|
2019-02-21 20:28:13 +01:00
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
2020-11-27 00:34:44 +01:00
|
|
|
|
2019-02-21 20:28:13 +01:00
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:14:01 +01:00
|
|
|
// HandleImageMessage sent from WhatsApp, relay it to the brige
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) handleImageMessage(msg *events.Message) {
|
|
|
|
imsg := msg.Message.GetImageMessage()
|
2020-01-09 18:14:01 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
senderJID := msg.Info.Sender
|
|
|
|
senderName := b.getSenderName(senderJID)
|
|
|
|
ci := imsg.GetContextInfo()
|
2020-01-09 18:14:01 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
|
|
|
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
|
2020-01-09 18:14:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := config.Message{
|
2022-01-31 00:13:37 +01:00
|
|
|
UserID: senderJID.String(),
|
2020-11-29 15:16:26 +01:00
|
|
|
Username: senderName,
|
2022-01-31 00:13:37 +01:00
|
|
|
Channel: ci.GetRemoteJid(),
|
2020-11-29 15:16:26 +01:00
|
|
|
Account: b.Account,
|
|
|
|
Protocol: b.Protocol,
|
|
|
|
Extra: make(map[string][]interface{}),
|
2022-01-31 00:13:37 +01:00
|
|
|
ID: msg.Info.ID,
|
2020-11-27 00:34:44 +01:00
|
|
|
}
|
2020-01-09 18:14:01 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
2020-01-09 18:14:01 +01:00
|
|
|
rmsg.Avatar = avatarURL
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
fileExt, err := mime.ExtensionsByType(imsg.GetMimetype())
|
2020-01-09 18:14:01 +01:00
|
|
|
if err != nil {
|
2020-11-29 15:16:26 +01:00
|
|
|
b.Log.Errorf("Mimetype detection error: %s", err)
|
|
|
|
|
2020-01-09 18:14:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:30:21 +01:00
|
|
|
// rename .jfif to .jpg https://github.com/42wim/matterbridge/issues/1292
|
|
|
|
if fileExt[0] == ".jfif" {
|
|
|
|
fileExt[0] = ".jpg"
|
|
|
|
}
|
|
|
|
|
2021-05-23 00:17:55 +02:00
|
|
|
// rename .jpe to .jpg https://github.com/42wim/matterbridge/issues/1463
|
|
|
|
if fileExt[0] == ".jpe" {
|
|
|
|
fileExt[0] = ".jpg"
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[0])
|
2020-01-09 18:14:01 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
b.Log.Debugf("Trying to download %s with type %s", filename, imsg.GetMimetype())
|
2020-11-29 15:16:26 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
data, err := b.wc.Download(imsg)
|
2020-11-29 15:16:26 +01:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Download image failed: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-01-09 18:14:01 +01:00
|
|
|
|
|
|
|
// Move file to bridge storage
|
2022-01-31 00:13:37 +01:00
|
|
|
helper.HandleDownloadData(b.Log, &rmsg, filename, imsg.GetCaption(), "", &data, b.General)
|
2020-01-09 18:14:01 +01:00
|
|
|
|
2020-11-29 15:16:26 +01:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
2020-11-27 00:34:44 +01:00
|
|
|
|
2020-01-09 18:14:01 +01:00
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:05:57 +01:00
|
|
|
// HandleVideoMessage downloads video messages
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) {
|
|
|
|
imsg := msg.Message.GetVideoMessage()
|
2020-11-29 15:05:57 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
senderJID := msg.Info.Sender
|
|
|
|
senderName := b.getSenderName(senderJID)
|
|
|
|
ci := imsg.GetContextInfo()
|
2020-11-29 15:05:57 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
|
|
|
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
|
2020-11-29 15:05:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := config.Message{
|
2022-01-31 00:13:37 +01:00
|
|
|
UserID: senderJID.String(),
|
2020-11-29 15:05:57 +01:00
|
|
|
Username: senderName,
|
2022-01-31 00:13:37 +01:00
|
|
|
Channel: ci.GetRemoteJid(),
|
2020-11-29 15:05:57 +01:00
|
|
|
Account: b.Account,
|
|
|
|
Protocol: b.Protocol,
|
|
|
|
Extra: make(map[string][]interface{}),
|
2022-01-31 00:13:37 +01:00
|
|
|
ID: msg.Info.ID,
|
2020-11-29 15:05:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
2020-11-29 15:05:57 +01:00
|
|
|
rmsg.Avatar = avatarURL
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
fileExt, err := mime.ExtensionsByType(imsg.GetMimetype())
|
2020-11-29 15:05:57 +01:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Mimetype detection error: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-23 21:21:30 +02:00
|
|
|
if len(fileExt) == 0 {
|
|
|
|
fileExt = append(fileExt, ".mp4")
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[0])
|
2020-11-29 15:05:57 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
b.Log.Debugf("Trying to download %s with size %#v and type %s", filename, imsg.GetFileLength(), imsg.GetMimetype())
|
2020-11-29 15:05:57 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
data, err := b.wc.Download(imsg)
|
2020-11-29 15:05:57 +01:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Download video failed: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move file to bridge storage
|
2022-01-31 00:13:37 +01:00
|
|
|
helper.HandleDownloadData(b.Log, &rmsg, filename, imsg.GetCaption(), "", &data, b.General)
|
2020-11-29 15:05:57 +01:00
|
|
|
|
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:11:04 +01:00
|
|
|
// HandleAudioMessage downloads audio messages
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) {
|
|
|
|
imsg := msg.Message.GetAudioMessage()
|
2020-11-29 15:11:04 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
senderJID := msg.Info.Sender
|
|
|
|
senderName := b.getSenderName(senderJID)
|
|
|
|
ci := imsg.GetContextInfo()
|
2020-11-29 15:11:04 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
|
|
|
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
|
2020-11-29 15:11:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := config.Message{
|
2022-01-31 00:13:37 +01:00
|
|
|
UserID: senderJID.String(),
|
2020-11-29 15:11:04 +01:00
|
|
|
Username: senderName,
|
2022-01-31 00:13:37 +01:00
|
|
|
Channel: ci.GetRemoteJid(),
|
2020-11-29 15:11:04 +01:00
|
|
|
Account: b.Account,
|
|
|
|
Protocol: b.Protocol,
|
|
|
|
Extra: make(map[string][]interface{}),
|
2022-01-31 00:13:37 +01:00
|
|
|
ID: msg.Info.ID,
|
2020-11-29 15:11:04 +01:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
2020-11-29 15:11:04 +01:00
|
|
|
rmsg.Avatar = avatarURL
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
fileExt, err := mime.ExtensionsByType(imsg.GetMimetype())
|
2020-11-29 15:11:04 +01:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Mimetype detection error: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-20 23:12:59 +01:00
|
|
|
if len(fileExt) == 0 {
|
|
|
|
fileExt = append(fileExt, ".ogg")
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[0])
|
2020-11-29 15:11:04 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
b.Log.Debugf("Trying to download %s with size %#v and type %s", filename, imsg.GetFileLength(), imsg.GetMimetype())
|
2020-11-29 15:11:04 +01:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
data, err := b.wc.Download(imsg)
|
2020-11-29 15:11:04 +01:00
|
|
|
if err != nil {
|
2022-01-31 00:13:37 +01:00
|
|
|
b.Log.Errorf("Download video failed: %s", err)
|
2020-11-29 15:11:04 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move file to bridge storage
|
|
|
|
helper.HandleDownloadData(b.Log, &rmsg, filename, "audio message", "", &data, b.General)
|
|
|
|
|
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
2021-05-21 22:50:47 +02:00
|
|
|
|
|
|
|
// HandleDocumentMessage downloads documents
|
2022-01-31 00:13:37 +01:00
|
|
|
func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) {
|
|
|
|
imsg := msg.Message.GetDocumentMessage()
|
2021-05-21 22:50:47 +02:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
senderJID := msg.Info.Sender
|
|
|
|
senderName := b.getSenderName(senderJID)
|
|
|
|
ci := imsg.GetContextInfo()
|
2021-05-21 22:50:47 +02:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if senderJID == (types.JID{}) && ci.Participant != nil {
|
|
|
|
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
|
2021-05-21 22:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := config.Message{
|
2022-01-31 00:13:37 +01:00
|
|
|
UserID: senderJID.String(),
|
2021-05-21 22:50:47 +02:00
|
|
|
Username: senderName,
|
2022-01-31 00:13:37 +01:00
|
|
|
Channel: ci.GetRemoteJid(),
|
2021-05-21 22:50:47 +02:00
|
|
|
Account: b.Account,
|
|
|
|
Protocol: b.Protocol,
|
|
|
|
Extra: make(map[string][]interface{}),
|
2022-01-31 00:13:37 +01:00
|
|
|
ID: msg.Info.ID,
|
2021-05-21 22:50:47 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
|
2021-05-21 22:50:47 +02:00
|
|
|
rmsg.Avatar = avatarURL
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
fileExt, err := mime.ExtensionsByType(imsg.GetMimetype())
|
2021-05-21 22:50:47 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Mimetype detection error: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
filename := fmt.Sprintf("%v", imsg.GetFileName())
|
2021-05-21 22:50:47 +02:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
b.Log.Debugf("Trying to download %s with extension %s and type %s", filename, fileExt, imsg.GetMimetype())
|
2021-05-21 22:50:47 +02:00
|
|
|
|
2022-01-31 00:13:37 +01:00
|
|
|
data, err := b.wc.Download(imsg)
|
2021-05-21 22:50:47 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Download document message failed: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move file to bridge storage
|
|
|
|
helper.HandleDownloadData(b.Log, &rmsg, filename, "document", "", &data, b.General)
|
|
|
|
|
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|