summaryrefslogtreecommitdiffstats
path: root/bridge/whatsapp/helpers.go
blob: aa5f3188a6023b68aa102ea04b9cbfb5ccc0eaba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package bwhatsapp

import (
	"encoding/gob"
	"encoding/json"
	"errors"
	"fmt"
	"os"

	qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
	"github.com/Rhymen/go-whatsapp"
)

type ProfilePicInfo struct {
	URL string `json:"eurl"`
	Tag string `json:"tag"`

	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)
		}

		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)
	err = decoder.Decode(&session)
	if err != nil {
		return session, err
	}
	return session, nil
}

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)
	if err != nil {
		return err
	}
	defer file.Close()
	encoder := gob.NewEncoder(file)
	err = encoder.Encode(session)

	return err
}

func (b *Bwhatsapp) getSenderName(senderJid string) string {
	if sender, exists := b.users[senderJid]; exists {
		if sender.Name != "" {
			return sender.Name
		}
		// 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.Short != "" {
			return sender.Short
		}
	}

	// 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 ""
}

func (b *Bwhatsapp) getSenderNotify(senderJid string) string {
	if sender, exists := b.users[senderJid]; exists {
		return sender.Notify
	}
	return ""
}

func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*ProfilePicInfo, error) {
	data, err := b.conn.GetProfilePicThumb(jid)
	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
}