From 496d5b4ec7f5f4afae6199f928675d14d18de015 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 20 Mar 2022 02:20:54 +0100 Subject: Add whatsappmulti buildflag for whatsapp with multidevice support (whatsapp) --- bridge/whatsappmulti/handlers.go | 344 +++++++++++++++++++++++++++++++++++++++ bridge/whatsappmulti/helpers.go | 108 ++++++++++++ bridge/whatsappmulti/whatsapp.go | 333 +++++++++++++++++++++++++++++++++++++ 3 files changed, 785 insertions(+) create mode 100644 bridge/whatsappmulti/handlers.go create mode 100644 bridge/whatsappmulti/helpers.go create mode 100644 bridge/whatsappmulti/whatsapp.go (limited to 'bridge/whatsappmulti') diff --git a/bridge/whatsappmulti/handlers.go b/bridge/whatsappmulti/handlers.go new file mode 100644 index 00000000..c6b96a5e --- /dev/null +++ b/bridge/whatsappmulti/handlers.go @@ -0,0 +1,344 @@ +// +build whatsappmulti + +package bwhatsapp + +import ( + "fmt" + "mime" + "strings" + + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" + + "go.mau.fi/whatsmeow/binary/proto" + "go.mau.fi/whatsmeow/types" + "go.mau.fi/whatsmeow/types/events" +) + +// nolint:gocritic +func (b *Bwhatsapp) eventHandler(evt interface{}) { + switch e := evt.(type) { + case *events.Message: + b.handleMessage(e) + } +} + +func (b *Bwhatsapp) handleMessage(message *events.Message) { + msg := message.Message + switch { + case msg == nil, message.Info.IsFromMe, message.Info.Timestamp.Before(b.startedAt): + return + } + + b.Log.Infof("Receiving message %#v", msg) + + 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) + } +} + +// nolint:funlen +func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.Message) { + senderJID := messageInfo.Sender + channel := messageInfo.Chat + + senderName := b.getSenderName(messageInfo.Sender) + if senderName == "" { + senderName = "Someone" // don't expose telephone number + } + + if msg.GetExtendedTextMessage() == nil && msg.GetConversation() == "" { + b.Log.Debugf("message without text content? %#v", msg) + return + } + + var text string + + // nolint:nestif + if msg.GetExtendedTextMessage() == nil { + text = msg.GetConversation() + } else { + text = msg.GetExtendedTextMessage().GetText() + ci := msg.GetExtendedTextMessage().GetContextInfo() + + if senderJID == (types.JID{}) && ci.Participant != nil { + senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer) + } + + if ci.MentionedJid != nil { + // handle user mentions + for _, mentionedJID := range ci.MentionedJid { + numberAndSuffix := strings.SplitN(mentionedJID, "@", 2) + + // mentions comes as telephone numbers and we don't want to expose it to other bridges + // replace it with something more meaninful to others + mention := b.getSenderNotify(types.NewJID(numberAndSuffix[0], types.DefaultUserServer)) + if mention == "" { + mention = "someone" + } + + text = strings.Replace(text, "@"+numberAndSuffix[0], "@"+mention, 1) + } + } + } + + rmsg := config.Message{ + UserID: senderJID.String(), + Username: senderName, + Text: text, + Channel: channel.String(), + Account: b.Account, + Protocol: b.Protocol, + Extra: make(map[string][]interface{}), + // ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string + ID: messageInfo.ID, + } + + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { + rmsg.Avatar = avatarURL + } + + b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account) + b.Log.Debugf("<= Message is %#v", rmsg) + + b.Remote <- rmsg +} + +// HandleImageMessage sent from WhatsApp, relay it to the brige +func (b *Bwhatsapp) handleImageMessage(msg *events.Message) { + imsg := msg.Message.GetImageMessage() + + senderJID := msg.Info.Sender + senderName := b.getSenderName(senderJID) + ci := imsg.GetContextInfo() + + if senderJID == (types.JID{}) && ci.Participant != nil { + senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer) + } + + rmsg := config.Message{ + UserID: senderJID.String(), + Username: senderName, + Channel: msg.Info.Chat.String(), + Account: b.Account, + Protocol: b.Protocol, + Extra: make(map[string][]interface{}), + ID: msg.Info.ID, + } + + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { + rmsg.Avatar = avatarURL + } + + fileExt, err := mime.ExtensionsByType(imsg.GetMimetype()) + if err != nil { + b.Log.Errorf("Mimetype detection error: %s", err) + + return + } + + // rename .jfif to .jpg https://github.com/42wim/matterbridge/issues/1292 + if fileExt[0] == ".jfif" { + fileExt[0] = ".jpg" + } + + // rename .jpe to .jpg https://github.com/42wim/matterbridge/issues/1463 + if fileExt[0] == ".jpe" { + fileExt[0] = ".jpg" + } + + filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[0]) + + b.Log.Debugf("Trying to download %s with type %s", filename, imsg.GetMimetype()) + + data, err := b.wc.Download(imsg) + if err != nil { + b.Log.Errorf("Download image failed: %s", err) + + return + } + + // Move file to bridge storage + helper.HandleDownloadData(b.Log, &rmsg, filename, imsg.GetCaption(), "", &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 +} + +// HandleVideoMessage downloads video messages +func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) { + imsg := msg.Message.GetVideoMessage() + + senderJID := msg.Info.Sender + senderName := b.getSenderName(senderJID) + ci := imsg.GetContextInfo() + + if senderJID == (types.JID{}) && ci.Participant != nil { + senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer) + } + + rmsg := config.Message{ + UserID: senderJID.String(), + Username: senderName, + Channel: msg.Info.Chat.String(), + Account: b.Account, + Protocol: b.Protocol, + Extra: make(map[string][]interface{}), + ID: msg.Info.ID, + } + + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { + rmsg.Avatar = avatarURL + } + + fileExt, err := mime.ExtensionsByType(imsg.GetMimetype()) + if err != nil { + b.Log.Errorf("Mimetype detection error: %s", err) + + return + } + + if len(fileExt) == 0 { + fileExt = append(fileExt, ".mp4") + } + + filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[0]) + + b.Log.Debugf("Trying to download %s with size %#v and type %s", filename, imsg.GetFileLength(), imsg.GetMimetype()) + + data, err := b.wc.Download(imsg) + if err != nil { + b.Log.Errorf("Download video failed: %s", err) + + return + } + + // Move file to bridge storage + helper.HandleDownloadData(b.Log, &rmsg, filename, imsg.GetCaption(), "", &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 +} + +// HandleAudioMessage downloads audio messages +func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) { + imsg := msg.Message.GetAudioMessage() + + senderJID := msg.Info.Sender + senderName := b.getSenderName(senderJID) + ci := imsg.GetContextInfo() + + if senderJID == (types.JID{}) && ci.Participant != nil { + senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer) + } + + rmsg := config.Message{ + UserID: senderJID.String(), + Username: senderName, + Channel: msg.Info.Chat.String(), + Account: b.Account, + Protocol: b.Protocol, + Extra: make(map[string][]interface{}), + ID: msg.Info.ID, + } + + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { + rmsg.Avatar = avatarURL + } + + fileExt, err := mime.ExtensionsByType(imsg.GetMimetype()) + if err != nil { + b.Log.Errorf("Mimetype detection error: %s", err) + + return + } + + if len(fileExt) == 0 { + fileExt = append(fileExt, ".ogg") + } + + filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[0]) + + b.Log.Debugf("Trying to download %s with size %#v and type %s", filename, imsg.GetFileLength(), imsg.GetMimetype()) + + data, err := b.wc.Download(imsg) + if err != nil { + b.Log.Errorf("Download video failed: %s", err) + + 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 +} + +// HandleDocumentMessage downloads documents +func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) { + imsg := msg.Message.GetDocumentMessage() + + senderJID := msg.Info.Sender + senderName := b.getSenderName(senderJID) + ci := imsg.GetContextInfo() + + if senderJID == (types.JID{}) && ci.Participant != nil { + senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer) + } + + rmsg := config.Message{ + UserID: senderJID.String(), + Username: senderName, + Channel: msg.Info.Chat.String(), + Account: b.Account, + Protocol: b.Protocol, + Extra: make(map[string][]interface{}), + ID: msg.Info.ID, + } + + if avatarURL, exists := b.userAvatars[senderJID.String()]; exists { + rmsg.Avatar = avatarURL + } + + fileExt, err := mime.ExtensionsByType(imsg.GetMimetype()) + if err != nil { + b.Log.Errorf("Mimetype detection error: %s", err) + + return + } + + filename := fmt.Sprintf("%v", imsg.GetFileName()) + + b.Log.Debugf("Trying to download %s with extension %s and type %s", filename, fileExt, imsg.GetMimetype()) + + data, err := b.wc.Download(imsg) + 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 +} diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go new file mode 100644 index 00000000..a7cc5c98 --- /dev/null +++ b/bridge/whatsappmulti/helpers.go @@ -0,0 +1,108 @@ +// +build whatsappmulti + +package bwhatsapp + +import ( + "fmt" + "strings" + + "go.mau.fi/whatsmeow/store" + "go.mau.fi/whatsmeow/store/sqlstore" + "go.mau.fi/whatsmeow/types" +) + +type ProfilePicInfo struct { + URL string `json:"eurl"` + Tag string `json:"tag"` + Status int16 `json:"status"` +} + +func (b *Bwhatsapp) getSenderName(senderJid types.JID) string { + if sender, exists := b.contacts[senderJid]; exists { + if sender.FullName != "" { + return sender.FullName + } + // if user is not in phone contacts + // it is the most obvious scenario unless you sync your phone contacts with some remote updated source + // users can change it in their WhatsApp settings -> profile -> click on Avatar + if sender.PushName != "" { + return sender.PushName + } + + if sender.FirstName != "" { + return sender.FirstName + } + } + + // try to reload this contact + if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil { + b.Log.Errorf("error on update of contacts: %v", err) + } + + allcontacts, err := b.wc.Store.Contacts.GetAllContacts() + if err != nil { + b.Log.Errorf("error on update of contacts: %v", err) + } + + if len(allcontacts) > 0 { + b.contacts = allcontacts + } + + if sender, exists := b.contacts[senderJid]; exists { + if sender.FullName != "" { + return sender.FullName + } + // if user is not in phone contacts + // it is the most obvious scenario unless you sync your phone contacts with some remote updated source + // users can change it in their WhatsApp settings -> profile -> click on Avatar + if sender.PushName != "" { + return sender.PushName + } + + if sender.FirstName != "" { + return sender.FirstName + } + } + + return "Someone" +} + +func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string { + if sender, exists := b.contacts[senderJid]; exists { + return sender.PushName + } + + return "" +} + +func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*types.ProfilePictureInfo, error) { + pjid, _ := types.ParseJID(jid) + info, err := b.wc.GetProfilePictureInfo(pjid, true) + if err != nil { + return nil, fmt.Errorf("failed to get avatar: %v", err) + } + + return info, nil +} + +func isGroupJid(identifier string) bool { + return strings.HasSuffix(identifier, "@g.us") || + strings.HasSuffix(identifier, "@temp") || + strings.HasSuffix(identifier, "@broadcast") +} + +func (b *Bwhatsapp) getDevice() (*store.Device, error) { + device := &store.Device{} + + storeContainer, err := sqlstore.New("sqlite", "file:"+b.Config.GetString("sessionfile")+".db?_foreign_keys=on&_pragma=busy_timeout=10000", nil) + if err != nil { + return device, fmt.Errorf("failed to connect to database: %v", err) + } + + device, err = storeContainer.GetFirstDevice() + if err != nil { + return device, fmt.Errorf("failed to get device: %v", err) + } + + return device, nil +} diff --git a/bridge/whatsappmulti/whatsapp.go b/bridge/whatsappmulti/whatsapp.go new file mode 100644 index 00000000..6b514451 --- /dev/null +++ b/bridge/whatsappmulti/whatsapp.go @@ -0,0 +1,333 @@ +// +build whatsappmulti + +package bwhatsapp + +import ( + "context" + "errors" + "fmt" + "mime" + "os" + "path/filepath" + "time" + + "github.com/42wim/matterbridge/bridge" + "github.com/42wim/matterbridge/bridge/config" + "github.com/mdp/qrterminal" + + "go.mau.fi/whatsmeow" + "go.mau.fi/whatsmeow/binary/proto" + "go.mau.fi/whatsmeow/types" + waLog "go.mau.fi/whatsmeow/util/log" + + goproto "google.golang.org/protobuf/proto" + + _ "modernc.org/sqlite" // needed for sqlite +) + +const ( + // Account config parameters + cfgNumber = "Number" +) + +// Bwhatsapp Bridge structure keeping all the information needed for relying +type Bwhatsapp struct { + *bridge.Config + + startedAt time.Time + wc *whatsmeow.Client + contacts map[types.JID]types.ContactInfo + users map[string]types.ContactInfo + userAvatars map[string]string +} + +// New Create a new WhatsApp bridge. This will be called for each [whatsapp.] entry you have in the config file +func New(cfg *bridge.Config) bridge.Bridger { + number := cfg.GetString(cfgNumber) + + if number == "" { + cfg.Log.Fatalf("Missing configuration for WhatsApp bridge: Number") + } + + b := &Bwhatsapp{ + Config: cfg, + + users: make(map[string]types.ContactInfo), + userAvatars: make(map[string]string), + } + + return b +} + +// Connect to WhatsApp. Required implementation of the Bridger interface +func (b *Bwhatsapp) Connect() error { + device, err := b.getDevice() + if err != nil { + return err + } + + number := b.GetString(cfgNumber) + if number == "" { + return errors.New("whatsapp's telephone number need to be configured") + } + + b.Log.Debugln("Connecting to WhatsApp..") + + b.wc = whatsmeow.NewClient(device, waLog.Stdout("Client", "INFO", true)) + b.wc.AddEventHandler(b.eventHandler) + + firstlogin := false + var qrChan <-chan whatsmeow.QRChannelItem + if b.wc.Store.ID == nil { + firstlogin = true + qrChan, err = b.wc.GetQRChannel(context.Background()) + if err != nil && !errors.Is(err, whatsmeow.ErrQRStoreContainsID) { + return errors.New("failed to to get QR channel:" + err.Error()) + } + } + + err = b.wc.Connect() + if err != nil { + return errors.New("failed to connect to WhatsApp: " + err.Error()) + } + + if b.wc.Store.ID == nil { + for evt := range qrChan { + if evt.Event == "code" { + qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout) + } else { + b.Log.Infof("QR channel result: %s", evt.Event) + } + } + } + + // disconnect and reconnect on our first login/pairing + // for some reason the GetJoinedGroups in JoinChannel doesn't work on first login + if firstlogin { + b.wc.Disconnect() + time.Sleep(time.Second) + + err = b.wc.Connect() + if err != nil { + return errors.New("failed to connect to WhatsApp: " + err.Error()) + } + } + + b.Log.Infoln("WhatsApp connection successful") + + b.contacts, err = b.wc.Store.Contacts.GetAllContacts() + if err != nil { + return errors.New("failed to get contacts: " + err.Error()) + } + + b.startedAt = time.Now() + + // map all the users + for id, contact := range b.contacts { + if !isGroupJid(id.String()) && id.String() != "status@broadcast" { + // it is user + b.users[id.String()] = contact + } + } + + // get user avatar asynchronously + b.Log.Info("Getting user avatars..") + + for jid := range b.users { + info, err := b.GetProfilePicThumb(jid) + if err != nil { + b.Log.Warnf("Could not get profile photo of %s: %v", jid, err) + } else { + b.Lock() + if info != nil { + b.userAvatars[jid] = info.URL + } + b.Unlock() + } + } + + b.Log.Info("Finished getting avatars..") + + return nil +} + +// Disconnect is called while reconnecting to the bridge +// Required implementation of the Bridger interface +func (b *Bwhatsapp) Disconnect() error { + b.wc.Disconnect() + + return nil +} + +// JoinChannel Join a WhatsApp group specified in gateway config as channel='number-id@g.us' or channel='Channel name' +// Required implementation of the Bridger interface +// https://github.com/42wim/matterbridge/blob/2cfd880cdb0df29771bf8f31df8d990ab897889d/bridge/bridge.go#L11-L16 +func (b *Bwhatsapp) JoinChannel(channel config.ChannelInfo) error { + byJid := isGroupJid(channel.Name) + + groups, err := b.wc.GetJoinedGroups() + if err != nil { + return err + } + + // verify if we are member of the given group + if byJid { + gJID, err := types.ParseJID(channel.Name) + if err != nil { + return err + } + + for _, group := range groups { + if group.JID == gJID { + return nil + } + } + } + + foundGroups := []string{} + + for _, group := range groups { + if group.Name == channel.Name { + foundGroups = append(foundGroups, group.Name) + } + } + + switch len(foundGroups) { + case 0: + // didn't match any group - print out possibilites + for _, group := range groups { + b.Log.Infof("%s %s", group.JID, group.Name) + } + return fmt.Errorf("please specify group's JID from the list above instead of the name '%s'", channel.Name) + case 1: + return fmt.Errorf("group name might change. Please configure gateway with channel=\"%v\" instead of channel=\"%v\"", foundGroups[0], channel.Name) + default: + return fmt.Errorf("there is more than one group with name '%s'. Please specify one of JIDs as channel name: %v", channel.Name, foundGroups) + } +} + +// Post a document message from the bridge to WhatsApp +func (b *Bwhatsapp) PostDocumentMessage(msg config.Message, filetype string) (string, error) { + groupJID, _ := types.ParseJID(msg.Channel) + + fi := msg.Extra["file"][0].(config.FileInfo) + + resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaDocument) + if err != nil { + return "", err + } + + // Post document message + var message proto.Message + + message.DocumentMessage = &proto.DocumentMessage{ + Title: &fi.Name, + FileName: &fi.Name, + Mimetype: &filetype, + MediaKey: resp.MediaKey, + FileEncSha256: resp.FileEncSHA256, + FileSha256: resp.FileSHA256, + FileLength: goproto.Uint64(resp.FileLength), + Url: &resp.URL, + } + + b.Log.Debugf("=> Sending %#v", msg) + + ID := whatsmeow.GenerateMessageID() + _, err = b.wc.SendMessage(groupJID, ID, &message) + + return ID, err +} + +// Post an image message from the bridge to WhatsApp +// Handle, for sure image/jpeg, image/png and image/gif MIME types +func (b *Bwhatsapp) PostImageMessage(msg config.Message, filetype string) (string, error) { + groupJID, _ := types.ParseJID(msg.Channel) + + fi := msg.Extra["file"][0].(config.FileInfo) + + caption := msg.Username + fi.Comment + + resp, err := b.wc.Upload(context.Background(), *fi.Data, whatsmeow.MediaImage) + if err != nil { + return "", err + } + + var message proto.Message + + message.ImageMessage = &proto.ImageMessage{ + Mimetype: &filetype, + Caption: &caption, + MediaKey: resp.MediaKey, + FileEncSha256: resp.FileEncSHA256, + FileSha256: resp.FileSHA256, + FileLength: goproto.Uint64(resp.FileLength), + Url: &resp.URL, + } + + b.Log.Debugf("=> Sending %#v", msg) + + ID := whatsmeow.GenerateMessageID() + _, err = b.wc.SendMessage(groupJID, ID, &message) + + return ID, err +} + +// Send a message from the bridge to WhatsApp +func (b *Bwhatsapp) Send(msg config.Message) (string, error) { + groupJID, _ := types.ParseJID(msg.Channel) + + b.Log.Debugf("=> Receiving %#v", msg) + + // Delete message + if msg.Event == config.EventMsgDelete { + if msg.ID == "" { + // No message ID in case action is executed on a message sent before the bridge was started + // and then the bridge cache doesn't have this message ID mapped + return "", nil + } + + _, err := b.wc.RevokeMessage(groupJID, msg.ID) + + return "", err + } + + // Edit message + if msg.ID != "" { + b.Log.Debugf("updating message with id %s", msg.ID) + + if b.GetString("editsuffix") != "" { + msg.Text += b.GetString("EditSuffix") + } else { + msg.Text += " (edited)" + } + } + + // Handle Upload a file + if msg.Extra["file"] != nil { + fi := msg.Extra["file"][0].(config.FileInfo) + filetype := mime.TypeByExtension(filepath.Ext(fi.Name)) + + b.Log.Debugf("Extra file is %#v", filetype) + + // TODO: add different types + // TODO: add webp conversion + switch filetype { + case "image/jpeg", "image/png", "image/gif": + return b.PostImageMessage(msg, filetype) + default: + return b.PostDocumentMessage(msg, filetype) + } + } + + text := msg.Username + msg.Text + + var message proto.Message + + message.Conversation = &text + + ID := whatsmeow.GenerateMessageID() + _, err := b.wc.SendMessage(groupJID, ID, &message) + + return ID, err +} -- cgit v1.2.3