summaryrefslogtreecommitdiffstats
path: root/bridge/whatsapp/helpers.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2022-01-31 00:13:37 +0100
committerWim <wim@42.be>2022-03-20 14:57:48 +0100
commite7b193788a56ee7cdb02a87a9db0ad6724ef66d5 (patch)
tree085f8363ddb76d1caf47eea19bcb2ca1400747e5 /bridge/whatsapp/helpers.go
parent17da95b094e4bb433e5fe240fa42067d94d908c1 (diff)
downloadmatterbridge-msglm-e7b193788a56ee7cdb02a87a9db0ad6724ef66d5.tar.gz
matterbridge-msglm-e7b193788a56ee7cdb02a87a9db0ad6724ef66d5.tar.bz2
matterbridge-msglm-e7b193788a56ee7cdb02a87a9db0ad6724ef66d5.zip
Rewrite whatsapp bridge to use whatsmeow
Diffstat (limited to 'bridge/whatsapp/helpers.go')
-rw-r--r--bridge/whatsapp/helpers.go163
1 files changed, 53 insertions, 110 deletions
diff --git a/bridge/whatsapp/helpers.go b/bridge/whatsapp/helpers.go
index e424387d..030dc2b4 100644
--- a/bridge/whatsapp/helpers.go
+++ b/bridge/whatsapp/helpers.go
@@ -1,15 +1,12 @@
package bwhatsapp
import (
- "encoding/gob"
- "encoding/json"
- "errors"
"fmt"
- "os"
"strings"
- qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
- "github.com/Rhymen/go-whatsapp"
+ "go.mau.fi/whatsmeow/store"
+ "go.mau.fi/whatsmeow/store/sqlstore"
+ "go.mau.fi/whatsmeow/types"
)
type ProfilePicInfo struct {
@@ -18,141 +15,71 @@ type ProfilePicInfo struct {
Status int16 `json:"status"`
}
-func qrFromTerminal(invert bool) chan string {
- qr := make(chan string)
-
- go func() {
- terminal := qrcodeTerminal.New()
-
- if invert {
- terminal = qrcodeTerminal.New2(qrcodeTerminal.ConsoleColors.BrightWhite, qrcodeTerminal.ConsoleColors.BrightBlack, qrcodeTerminal.QRCodeRecoveryLevels.Medium)
+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
}
- terminal.Get(<-qr).Print()
- }()
-
- return qr
-}
-
-func (b *Bwhatsapp) readSession() (whatsapp.Session, error) {
- session := whatsapp.Session{}
- sessionFile := b.Config.GetString(sessionFile)
-
- if sessionFile == "" {
- return session, errors.New("if you won't set SessionFile then you will need to scan QR code on every restart")
- }
-
- file, err := os.Open(sessionFile)
- if err != nil {
- return session, err
- }
-
- defer file.Close()
-
- decoder := gob.NewDecoder(file)
-
- return session, decoder.Decode(&session)
-}
-
-func (b *Bwhatsapp) writeSession(session whatsapp.Session) error {
- sessionFile := b.Config.GetString(sessionFile)
-
- if sessionFile == "" {
- // we already sent a warning while starting the bridge, so let's be quiet here
- return nil
+ if sender.FirstName != "" {
+ return sender.FirstName
+ }
}
- file, err := os.Create(sessionFile)
- if err != nil {
- return err
+ // try to reload this contact
+ if _, err := b.wc.Store.Contacts.GetAllContacts(); err != nil {
+ b.Log.Errorf("error on update of contacts: %v", err)
}
- defer file.Close()
-
- encoder := gob.NewEncoder(file)
-
- return encoder.Encode(session)
-}
-
-func (b *Bwhatsapp) restoreSession() (*whatsapp.Session, error) {
- session, err := b.readSession()
+ allcontacts, err := b.wc.Store.Contacts.GetAllContacts()
if err != nil {
- b.Log.Warn(err.Error())
+ b.Log.Errorf("error on update of contacts: %v", err)
}
- b.Log.Debugln("Restoring WhatsApp session..")
-
- session, err = b.conn.RestoreWithSession(session)
- if err != nil {
- // restore session connection timed out (I couldn't get over it without logging in again)
- return nil, errors.New("failed to restore session: " + err.Error())
+ if len(allcontacts) > 0 {
+ b.contacts = allcontacts
}
- b.Log.Debugln("Session restored successfully!")
-
- return &session, nil
-}
-
-func (b *Bwhatsapp) getSenderName(senderJid string) string {
- if sender, exists := b.users[senderJid]; exists {
- if sender.Name != "" {
- return sender.Name
+ 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.Notify != "" {
- return sender.Notify
+ if sender.PushName != "" {
+ return sender.PushName
}
- if sender.Short != "" {
- return sender.Short
+ if sender.FirstName != "" {
+ return sender.FirstName
}
}
- // try to reload this contact
- _, err := b.conn.Contacts()
- if err != nil {
- b.Log.Errorf("error on update of contacts: %v", err)
- }
-
- if contact, exists := b.conn.Store.Contacts[senderJid]; exists {
- // Add it to the user map
- b.users[senderJid] = contact
-
- if contact.Name != "" {
- return contact.Name
- }
- // if user is not in phone contacts
- // same as above
- return contact.Notify
- }
-
- return ""
+ return "Someone"
}
-func (b *Bwhatsapp) getSenderNotify(senderJid string) string {
- if sender, exists := b.users[senderJid]; exists {
- return sender.Notify
+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) (*ProfilePicInfo, error) {
- data, err := b.conn.GetProfilePicThumb(jid)
+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)
}
- content := <-data
- info := &ProfilePicInfo{}
-
- err = json.Unmarshal([]byte(content), info)
- if err != nil {
- return info, fmt.Errorf("failed to unmarshal avatar info: %v", err)
- }
-
return info, nil
}
@@ -161,3 +88,19 @@ func isGroupJid(identifier string) bool {
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", 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
+}