2019-02-21 19:28:13 +00:00
|
|
|
package bwhatsapp
|
|
|
|
|
|
|
|
import (
|
2022-03-20 01:20:54 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-05-31 22:53:49 +00:00
|
|
|
"fmt"
|
2022-03-20 01:20:54 +00:00
|
|
|
"os"
|
2020-11-26 23:34:44 +00:00
|
|
|
"strings"
|
2019-02-21 19:28:13 +00:00
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
|
|
|
|
"github.com/Rhymen/go-whatsapp"
|
2019-02-21 19:28:13 +00:00
|
|
|
)
|
|
|
|
|
2019-05-31 22:53:49 +00:00
|
|
|
type ProfilePicInfo struct {
|
2020-11-26 23:34:44 +00:00
|
|
|
URL string `json:"eurl"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
Status int16 `json:"status"`
|
2019-05-31 22:53:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
func qrFromTerminal(invert bool) chan string {
|
|
|
|
qr := make(chan string)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
terminal := qrcodeTerminal.New()
|
2019-02-21 19:28:13 +00:00
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
if invert {
|
|
|
|
terminal = qrcodeTerminal.New2(qrcodeTerminal.ConsoleColors.BrightWhite, qrcodeTerminal.ConsoleColors.BrightBlack, qrcodeTerminal.QRCodeRecoveryLevels.Medium)
|
2022-01-30 23:13:37 +00:00
|
|
|
}
|
2022-03-20 01:20:54 +00:00
|
|
|
|
|
|
|
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")
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
file, err := os.Open(sessionFile)
|
|
|
|
if err != nil {
|
|
|
|
return session, err
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
2020-11-26 23:34:44 +00:00
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(sessionFile)
|
2020-11-26 23:34:44 +00:00
|
|
|
if err != nil {
|
2022-03-20 01:20:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
encoder := gob.NewEncoder(file)
|
|
|
|
|
|
|
|
return encoder.Encode(session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bwhatsapp) restoreSession() (*whatsapp.Session, error) {
|
|
|
|
session, err := b.readSession()
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Warn(err.Error())
|
2020-11-26 23:34:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
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())
|
2020-11-26 23:34:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
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
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
// 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
|
2022-03-20 01:20:54 +00:00
|
|
|
if sender.Notify != "" {
|
|
|
|
return sender.Notify
|
2020-06-24 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
if sender.Short != "" {
|
|
|
|
return sender.Short
|
2020-06-24 22:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
// try to reload this contact
|
2022-03-20 13:13:28 +00:00
|
|
|
if _, err := b.conn.Contacts(); err != nil {
|
2022-03-20 01:20:54 +00:00
|
|
|
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 ""
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
func (b *Bwhatsapp) getSenderNotify(senderJid string) string {
|
|
|
|
if sender, exists := b.users[senderJid]; exists {
|
|
|
|
return sender.Notify
|
2019-02-21 19:28:13 +00:00
|
|
|
}
|
2020-11-26 23:34:44 +00:00
|
|
|
|
2019-02-21 19:28:13 +00:00
|
|
|
return ""
|
|
|
|
}
|
2019-05-31 22:53:49 +00:00
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*ProfilePicInfo, error) {
|
|
|
|
data, err := b.conn.GetProfilePicThumb(jid)
|
2019-05-31 22:53:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get avatar: %v", err)
|
|
|
|
}
|
2020-11-26 23:34:44 +00:00
|
|
|
|
2022-03-20 01:20:54 +00:00
|
|
|
content := <-data
|
|
|
|
info := &ProfilePicInfo{}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(content), info)
|
|
|
|
if err != nil {
|
|
|
|
return info, fmt.Errorf("failed to unmarshal avatar info: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-05-31 22:53:49 +00:00
|
|
|
return info, nil
|
|
|
|
}
|
2020-11-26 23:34:44 +00:00
|
|
|
|
|
|
|
func isGroupJid(identifier string) bool {
|
|
|
|
return strings.HasSuffix(identifier, "@g.us") ||
|
|
|
|
strings.HasSuffix(identifier, "@temp") ||
|
|
|
|
strings.HasSuffix(identifier, "@broadcast")
|
|
|
|
}
|