2022-09-05 21:00:54 +02:00
|
|
|
//go:build whatsappmulti
|
2022-03-20 02:20:54 +01:00
|
|
|
// +build whatsappmulti
|
|
|
|
|
|
|
|
package bwhatsapp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-09 21:57:19 +01:00
|
|
|
goproto "google.golang.org/protobuf/proto"
|
|
|
|
|
2023-01-28 22:57:53 +01:00
|
|
|
"go.mau.fi/whatsmeow"
|
2023-03-09 21:57:19 +01:00
|
|
|
"go.mau.fi/whatsmeow/binary/proto"
|
2022-03-20 02:20:54 +01:00
|
|
|
"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"`
|
|
|
|
}
|
|
|
|
|
2023-01-28 22:57:53 +01:00
|
|
|
func (b *Bwhatsapp) reloadContacts() {
|
2022-03-20 02:20:54 +01:00
|
|
|
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
|
|
|
|
}
|
2022-12-14 23:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
if !exists || (sender.FullName == "" && sender.FirstName == "") {
|
|
|
|
b.reloadContacts() // Contacts may need to be reloaded
|
|
|
|
sender, exists = b.contacts[senderJid]
|
|
|
|
}
|
2022-03-20 02:20:54 +01:00
|
|
|
|
2022-12-14 23:47:30 +01:00
|
|
|
if exists && sender.FullName != "" {
|
|
|
|
return sender.FullName
|
|
|
|
}
|
2023-01-28 22:57:53 +01:00
|
|
|
|
2022-12-14 23:47:30 +01:00
|
|
|
if info.PushName != "" {
|
|
|
|
return info.PushName
|
2022-03-20 02:20:54 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 23:47:30 +01:00
|
|
|
if exists && sender.FirstName != "" {
|
|
|
|
return sender.FirstName
|
|
|
|
}
|
2023-01-28 22:57:53 +01:00
|
|
|
|
2022-03-20 02:20:54 +01:00
|
|
|
return "Someone"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string {
|
2022-12-14 23:47:30 +01:00
|
|
|
sender, exists := b.contacts[senderJid]
|
|
|
|
|
|
|
|
if !exists || (sender.FullName == "" && sender.PushName == "" && sender.FirstName == "") {
|
|
|
|
b.reloadContacts() // Contacts may need to be reloaded
|
|
|
|
sender, exists = b.contacts[senderJid]
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return "someone"
|
|
|
|
}
|
2023-01-28 22:57:53 +01:00
|
|
|
|
2022-12-14 23:47:30 +01:00
|
|
|
if exists && sender.FullName != "" {
|
|
|
|
return sender.FullName
|
|
|
|
}
|
2023-01-28 22:57:53 +01:00
|
|
|
|
2022-12-14 23:47:30 +01:00
|
|
|
if exists && sender.PushName != "" {
|
2022-03-20 02:20:54 +01:00
|
|
|
return sender.PushName
|
|
|
|
}
|
|
|
|
|
2022-12-14 23:47:30 +01:00
|
|
|
if exists && sender.FirstName != "" {
|
|
|
|
return sender.FirstName
|
|
|
|
}
|
2023-01-28 22:57:53 +01:00
|
|
|
|
2022-12-14 23:47:30 +01:00
|
|
|
return "someone"
|
2022-03-20 02:20:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*types.ProfilePictureInfo, error) {
|
|
|
|
pjid, _ := types.ParseJID(jid)
|
2023-01-28 22:57:53 +01:00
|
|
|
|
|
|
|
info, err := b.wc.GetProfilePictureInfo(pjid, &whatsmeow.GetProfilePictureParams{
|
|
|
|
Preview: true,
|
|
|
|
})
|
2022-03-20 02:20:54 +01:00
|
|
|
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
|
|
|
|
}
|
2023-03-09 21:57:19 +01:00
|
|
|
|
|
|
|
func (b *Bwhatsapp) getNewReplyContext(parentID string) (*proto.ContextInfo, error) {
|
|
|
|
replyInfo, err := b.parseMessageID(parentID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sender := fmt.Sprintf("%s@%s", replyInfo.Sender.User, replyInfo.Sender.Server)
|
|
|
|
ctx := &proto.ContextInfo{
|
|
|
|
StanzaId: &replyInfo.MessageID,
|
|
|
|
Participant: &sender,
|
|
|
|
QuotedMessage: &proto.Message{Conversation: goproto.String("")},
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bwhatsapp) parseMessageID(id string) (*Replyable, error) {
|
|
|
|
// 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
|
|
|
|
if id == "" {
|
|
|
|
return &Replyable{MessageID: id}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
replyInfo := strings.Split(id, "/")
|
|
|
|
|
|
|
|
if len(replyInfo) == 2 {
|
|
|
|
sender, err := types.ParseJID(replyInfo[0])
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return &Replyable{
|
|
|
|
MessageID: types.MessageID(replyInfo[1]),
|
|
|
|
Sender: sender,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := fmt.Errorf("MessageID does not match format of {senderJID}:{messageID} : \"%s\"", id)
|
|
|
|
|
|
|
|
return &Replyable{MessageID: id}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getParentIdFromCtx(ci *proto.ContextInfo) string {
|
|
|
|
if ci != nil && ci.StanzaId != nil {
|
|
|
|
senderJid, err := types.ParseJID(*ci.Participant)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return getMessageIdFormat(senderJid, *ci.StanzaId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMessageIdFormat(jid types.JID, messageID string) string {
|
|
|
|
// we're crafting our own JID str as AD JID format messes with how stuff looks on a webclient
|
|
|
|
jidStr := fmt.Sprintf("%s@%s", jid.User, jid.Server)
|
|
|
|
return fmt.Sprintf("%s/%s", jidStr, messageID)
|
|
|
|
}
|