From ffd9ec1e05e7f4d2cf07fc5d489ca60c76e9e8d8 Mon Sep 17 00:00:00 2001 From: Iiro Laiho Date: Tue, 29 Nov 2022 12:06:53 +0200 Subject: [PATCH 1/6] Apply patch to work around the "Someone" nickname problem --- bridge/whatsappmulti/handlers.go | 10 +++++----- bridge/whatsappmulti/helpers.go | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/bridge/whatsappmulti/handlers.go b/bridge/whatsappmulti/handlers.go index c6b96a5ec8..dd7e87bd01 100644 --- a/bridge/whatsappmulti/handlers.go +++ b/bridge/whatsappmulti/handlers.go @@ -51,7 +51,7 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto. senderJID := messageInfo.Sender channel := messageInfo.Chat - senderName := b.getSenderName(messageInfo.Sender) + senderName := b.getSenderName(messageInfo.Sender, messageInfo.PushName) if senderName == "" { senderName = "Someone" // don't expose telephone number } @@ -118,7 +118,7 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) { imsg := msg.Message.GetImageMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID) + senderName := b.getSenderName(senderJID, msg.Info.PushName) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { @@ -181,7 +181,7 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) { imsg := msg.Message.GetVideoMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID) + senderName := b.getSenderName(senderJID, msg.Info.PushName) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { @@ -238,7 +238,7 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) { imsg := msg.Message.GetAudioMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID) + senderName := b.getSenderName(senderJID, msg.Info.PushName) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { @@ -295,7 +295,7 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) { imsg := msg.Message.GetDocumentMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID) + senderName := b.getSenderName(senderJID, msg.Info.PushName) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index 5b1ec86ca7..767a5086e3 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -18,7 +18,7 @@ type ProfilePicInfo struct { Status int16 `json:"status"` } -func (b *Bwhatsapp) getSenderName(senderJid types.JID) string { +func (b *Bwhatsapp) getSenderName(senderJid types.JID, AltName string) string { if sender, exists := b.contacts[senderJid]; exists { if sender.FullName != "" { return sender.FullName @@ -65,6 +65,10 @@ func (b *Bwhatsapp) getSenderName(senderJid types.JID) string { } } + if AltName != "" { + return AltName + } + return "Someone" } From a975b145daf3ccd1e418490a54f340e2d94386c7 Mon Sep 17 00:00:00 2001 From: Iiro Laiho Date: Tue, 29 Nov 2022 18:14:26 +0200 Subject: [PATCH 2/6] Code cleanup (whatsapp) --- bridge/whatsappmulti/handlers.go | 10 ++--- bridge/whatsappmulti/helpers.go | 64 ++++++++++++++------------------ 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/bridge/whatsappmulti/handlers.go b/bridge/whatsappmulti/handlers.go index dd7e87bd01..ddeb22eff8 100644 --- a/bridge/whatsappmulti/handlers.go +++ b/bridge/whatsappmulti/handlers.go @@ -51,7 +51,7 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto. senderJID := messageInfo.Sender channel := messageInfo.Chat - senderName := b.getSenderName(messageInfo.Sender, messageInfo.PushName) + senderName := b.getSenderName(messageInfo) if senderName == "" { senderName = "Someone" // don't expose telephone number } @@ -118,7 +118,7 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) { imsg := msg.Message.GetImageMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID, msg.Info.PushName) + senderName := b.getSenderName(msg.Info) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { @@ -181,7 +181,7 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) { imsg := msg.Message.GetVideoMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID, msg.Info.PushName) + senderName := b.getSenderName(msg.Info) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { @@ -238,7 +238,7 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) { imsg := msg.Message.GetAudioMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID, msg.Info.PushName) + senderName := b.getSenderName(msg.Info) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { @@ -295,7 +295,7 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) { imsg := msg.Message.GetDocumentMessage() senderJID := msg.Info.Sender - senderName := b.getSenderName(senderJID, msg.Info.PushName) + senderName := b.getSenderName(msg.Info) ci := imsg.GetContextInfo() if senderJID == (types.JID{}) && ci.Participant != nil { diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index 767a5086e3..e9ee24ca43 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -18,55 +18,47 @@ type ProfilePicInfo struct { Status int16 `json:"status"` } -func (b *Bwhatsapp) getSenderName(senderJid types.JID, AltName string) string { - if sender, exists := b.contacts[senderJid]; exists { - if sender.FullName != "" { - return sender.FullName +func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { + // Parse AD JID + var senderJid types.JID + senderJid.User, senderJid.Server = info.Sender.User, info.Sender.Server + + for i := 0; i < 2; i++ { + 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 info.PushName != "" { + return info.PushName } - if sender.FirstName != "" { - return sender.FirstName + if sender, exists := b.contacts[senderJid]; exists { + 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 i > 0 { + break } - // 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 + + // try to reload this contact + if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil { + b.Log.Errorf("error on update of contacts: %v", err) } - if sender.FirstName != "" { - return sender.FirstName + allcontacts, err := b.wc.Store.Contacts.GetAllContacts() + if err != nil { + b.Log.Errorf("error on update of contacts: %v", err) } - } - if AltName != "" { - return AltName + if len(allcontacts) > 0 { + b.contacts = allcontacts + } } return "Someone" From fe39c5a9d581e32107e938b75f7de965429e6d91 Mon Sep 17 00:00:00 2001 From: Iiro Laiho Date: Fri, 2 Dec 2022 22:43:37 +0200 Subject: [PATCH 3/6] Cleanup --- bridge/whatsappmulti/handlers.go | 6 ------ bridge/whatsappmulti/helpers.go | 20 +++++++++----------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/bridge/whatsappmulti/handlers.go b/bridge/whatsappmulti/handlers.go index ddeb22eff8..0c0b0bf78e 100644 --- a/bridge/whatsappmulti/handlers.go +++ b/bridge/whatsappmulti/handlers.go @@ -52,9 +52,6 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto. channel := messageInfo.Chat senderName := b.getSenderName(messageInfo) - if senderName == "" { - senderName = "Someone" // don't expose telephone number - } if msg.GetExtendedTextMessage() == nil && msg.GetConversation() == "" { b.Log.Debugf("message without text content? %#v", msg) @@ -82,9 +79,6 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto. // 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) } diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index e9ee24ca43..d7e4736684 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -18,17 +18,19 @@ type ProfilePicInfo struct { Status int16 `json:"status"` } + func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { // Parse AD JID var senderJid types.JID senderJid.User, senderJid.Server = info.Sender.User, info.Sender.Server + sender, exists := b.contacts[senderJid] + for i := 0; i < 2; i++ { - if sender, exists := b.contacts[senderJid]; exists { - if sender.FullName != "" { - return sender.FullName - } + if exists && ( 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 @@ -36,10 +38,8 @@ func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { return info.PushName } - if sender, exists := b.contacts[senderJid]; exists { - if sender.FirstName != "" { - return sender.FirstName - } + if exists && ( sender.FirstName != "" ) { + return sender.FirstName } if i > 0 { @@ -60,7 +60,6 @@ func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { b.contacts = allcontacts } } - return "Someone" } @@ -68,8 +67,7 @@ func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string { if sender, exists := b.contacts[senderJid]; exists { return sender.PushName } - - return "" + return "someone" } func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*types.ProfilePictureInfo, error) { From 7cf4e4b786a9f6d76d80f99b9b4c122b5527a46f Mon Sep 17 00:00:00 2001 From: Iiro Laiho Date: Fri, 2 Dec 2022 23:43:58 +0200 Subject: [PATCH 4/6] Code cleanup (whatsapp) --- bridge/whatsappmulti/helpers.go | 83 +++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index d7e4736684..08716f06b4 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -18,6 +18,20 @@ type ProfilePicInfo struct { Status int16 `json:"status"` } +func (b *Bwhatsapp) reloadContacts(){ + 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 + } +} func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { // Parse AD JID @@ -26,47 +40,48 @@ func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { sender, exists := b.contacts[senderJid] - for i := 0; i < 2; i++ { - if exists && ( 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 info.PushName != "" { - return info.PushName - } - - if exists && ( sender.FirstName != "" ) { - return sender.FirstName - } - - if i > 0 { - break - } - - // 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 !exists || (sender.FullName == "" && sender.FirstName == "") { + b.reloadContacts() // Contacts may need to be reloaded + } + + if exists && ( sender.FullName != "" ) { + return sender.FullName + } + + if info.PushName != "" { + return info.PushName + } + + if exists && ( sender.FirstName != "" ) { + return sender.FirstName } + return "Someone" } func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string { - if sender, exists := b.contacts[senderJid]; exists { + sender, exists := b.contacts[senderJid] + + if !exists || (sender.FullName == "" && sender.PushName == "" && sender.FirstName == "") { + b.reloadContacts() // Contacts may need to be reloaded + } + + if !exists { + return "someone" + } + + if sender.FullName != "" { + return sender.FullName + } + + if sender.PushName != "" { return sender.PushName } + + if exists && sender.FirstName != "" { + return sender.FirstName + } + return "someone" } From 0b14df521f019fab6ac39a188dec888e927283d0 Mon Sep 17 00:00:00 2001 From: Iiro Laiho Date: Sat, 3 Dec 2022 00:04:18 +0200 Subject: [PATCH 5/6] Re-examine if sender exists --- bridge/whatsappmulti/helpers.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index 08716f06b4..08043d2d0b 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -42,6 +42,7 @@ func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { if !exists || (sender.FullName == "" && sender.FirstName == "") { b.reloadContacts() // Contacts may need to be reloaded + sender, exists = b.contacts[senderJid] } if exists && ( sender.FullName != "" ) { @@ -64,6 +65,7 @@ func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string { if !exists || (sender.FullName == "" && sender.PushName == "" && sender.FirstName == "") { b.reloadContacts() // Contacts may need to be reloaded + sender, exists = b.contacts[senderJid] } if !exists { From 2200a9c1520f986616123427665b3b204f985ccb Mon Sep 17 00:00:00 2001 From: Iiro Laiho Date: Wed, 7 Dec 2022 00:35:15 +0200 Subject: [PATCH 6/6] Cleanup (whatsapp) --- bridge/whatsappmulti/helpers.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bridge/whatsappmulti/helpers.go b/bridge/whatsappmulti/helpers.go index 08043d2d0b..c3c9d01816 100644 --- a/bridge/whatsappmulti/helpers.go +++ b/bridge/whatsappmulti/helpers.go @@ -45,7 +45,7 @@ func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { sender, exists = b.contacts[senderJid] } - if exists && ( sender.FullName != "" ) { + if exists && sender.FullName != "" { return sender.FullName } @@ -53,7 +53,7 @@ func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { return info.PushName } - if exists && ( sender.FirstName != "" ) { + if exists && sender.FirstName != "" { return sender.FirstName } @@ -72,11 +72,11 @@ func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string { return "someone" } - if sender.FullName != "" { + if exists && sender.FullName != "" { return sender.FullName } - if sender.PushName != "" { + if exists && sender.PushName != "" { return sender.PushName }