| 1 | package bwhatsapp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | |
| 8 | goproto "google.golang.org/protobuf/proto" |
| 9 | |
| 10 | "go.mau.fi/whatsmeow" |
| 11 | "go.mau.fi/whatsmeow/binary/proto" |
| 12 | "go.mau.fi/whatsmeow/store" |
| 13 | "go.mau.fi/whatsmeow/store/sqlstore" |
| 14 | "go.mau.fi/whatsmeow/types" |
| 15 | ) |
| 16 | |
| 17 | type ProfilePicInfo struct { |
| 18 | URL string `json:"eurl"` |
| 19 | Tag string `json:"tag"` |
| 20 | Status int16 `json:"status"` |
| 21 | } |
| 22 | |
| 23 | func (b *Bwhatsapp) reloadContacts() { |
| 24 | if _, err := b.wc.Store.Contacts.GetAllContacts(context.Background()); err != nil { |
| 25 | b.Log.Errorf("error on update of contacts: %v", err) |
| 26 | } |
| 27 | |
| 28 | allcontacts, err := b.wc.Store.Contacts.GetAllContacts(context.Background()) |
| 29 | if err != nil { |
| 30 | b.Log.Errorf("error on update of contacts: %v", err) |
| 31 | } |
| 32 | |
| 33 | if len(allcontacts) > 0 { |
| 34 | b.contacts = allcontacts |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func (b *Bwhatsapp) getSenderName(info types.MessageInfo) string { |
| 39 | // Parse AD JID |
| 40 | var senderJid types.JID |
| 41 | senderJid.User, senderJid.Server = info.Sender.User, info.Sender.Server |
| 42 | |
| 43 | sender, exists := b.contacts[senderJid] |
| 44 | |
| 45 | if !exists || (sender.FullName == "" && sender.FirstName == "") { |
| 46 | b.reloadContacts() // Contacts may need to be reloaded |
| 47 | sender, exists = b.contacts[senderJid] |
| 48 | } |
| 49 | |
| 50 | if exists && sender.FullName != "" { |
| 51 | return sender.FullName |
| 52 | } |
| 53 | |
| 54 | if info.PushName != "" { |
| 55 | return info.PushName |
| 56 | } |
| 57 | |
| 58 | if exists && sender.FirstName != "" { |
| 59 | return sender.FirstName |
| 60 | } |
| 61 | |
| 62 | return "Someone" |
| 63 | } |
| 64 | |
| 65 | func (b *Bwhatsapp) getSenderNameFromJID(senderJid types.JID) string { |
| 66 | sender, exists := b.contacts[senderJid] |
| 67 | |
| 68 | if !exists || (sender.FullName == "" && sender.FirstName == "") { |
| 69 | b.reloadContacts() // Contacts may need to be reloaded |
| 70 | sender, exists = b.contacts[senderJid] |
| 71 | } |
| 72 | |
| 73 | if exists && sender.FullName != "" { |
| 74 | return sender.FullName |
| 75 | } |
| 76 | |
| 77 | if exists && sender.FirstName != "" { |
| 78 | return sender.FirstName |
| 79 | } |
| 80 | |
| 81 | if sender.PushName != "" { |
| 82 | return sender.PushName |
| 83 | } |
| 84 | |
| 85 | return "Someone" |
| 86 | } |
| 87 | |
| 88 | func (b *Bwhatsapp) getSenderNotify(senderJid types.JID) string { |
| 89 | sender, exists := b.contacts[senderJid] |
| 90 | |
| 91 | if !exists || (sender.FullName == "" && sender.PushName == "" && sender.FirstName == "") { |
| 92 | b.reloadContacts() // Contacts may need to be reloaded |
| 93 | sender, exists = b.contacts[senderJid] |
| 94 | } |
| 95 | |
| 96 | if !exists { |
| 97 | return "someone" |
| 98 | } |
| 99 | |
| 100 | if exists && sender.FullName != "" { |
| 101 | return sender.FullName |
| 102 | } |
| 103 | |
| 104 | if exists && sender.PushName != "" { |
| 105 | return sender.PushName |
| 106 | } |
| 107 | |
| 108 | if exists && sender.FirstName != "" { |
| 109 | return sender.FirstName |
| 110 | } |
| 111 | |
| 112 | return "someone" |
| 113 | } |
| 114 | |
| 115 | func (b *Bwhatsapp) GetProfilePicThumb(jid string) (*types.ProfilePictureInfo, error) { |
| 116 | pjid, _ := types.ParseJID(jid) |
| 117 | |
| 118 | info, err := b.wc.GetProfilePictureInfo(context.Background(), pjid, &whatsmeow.GetProfilePictureParams{ |
| 119 | Preview: true, |
| 120 | }) |
| 121 | if err != nil { |
| 122 | return nil, fmt.Errorf("failed to get avatar: %v", err) |
| 123 | } |
| 124 | |
| 125 | return info, nil |
| 126 | } |
| 127 | |
| 128 | func isGroupJid(identifier string) bool { |
| 129 | return strings.HasSuffix(identifier, "@g.us") || |
| 130 | strings.HasSuffix(identifier, "@temp") || |
| 131 | strings.HasSuffix(identifier, "@broadcast") |
| 132 | } |
| 133 | |
| 134 | func (b *Bwhatsapp) getDevice() (*store.Device, error) { |
| 135 | device := &store.Device{} |
| 136 | |
| 137 | storeContainer, err := sqlstore.New(context.Background(), "sqlite", "file:"+b.Config.GetString("sessionfile")+".db?_pragma=foreign_keys(1)&_pragma=busy_timeout=10000", nil) |
| 138 | if err != nil { |
| 139 | return device, fmt.Errorf("failed to connect to database: %v", err) |
| 140 | } |
| 141 | |
| 142 | device, err = storeContainer.GetFirstDevice(context.Background()) |
| 143 | if err != nil { |
| 144 | return device, fmt.Errorf("failed to get device: %v", err) |
| 145 | } |
| 146 | |
| 147 | return device, nil |
| 148 | } |
| 149 | |
| 150 | func (b *Bwhatsapp) getNewReplyContext(parentID string) (*proto.ContextInfo, error) { |
| 151 | replyInfo, err := b.parseMessageID(parentID) |
| 152 | if err != nil { |
| 153 | return nil, err |
| 154 | } |
| 155 | |
| 156 | sender := fmt.Sprintf("%s@%s", replyInfo.Sender.User, replyInfo.Sender.Server) |
| 157 | ctx := &proto.ContextInfo{ |
| 158 | StanzaID: &replyInfo.MessageID, |
| 159 | Participant: &sender, |
| 160 | QuotedMessage: &proto.Message{Conversation: goproto.String("")}, |
| 161 | } |
| 162 | |
| 163 | return ctx, nil |
| 164 | } |
| 165 | |
| 166 | func (b *Bwhatsapp) parseMessageID(id string) (*Replyable, error) { |
| 167 | // No message ID in case action is executed on a message sent before the bridge was started |
| 168 | // and then the bridge cache doesn't have this message ID mapped |
| 169 | if id == "" { |
| 170 | return &Replyable{MessageID: id}, nil |
| 171 | } |
| 172 | |
| 173 | replyInfo := strings.Split(id, "/") |
| 174 | |
| 175 | if len(replyInfo) == 2 { |
| 176 | sender, err := types.ParseJID(replyInfo[0]) |
| 177 | |
| 178 | if err == nil { |
| 179 | return &Replyable{ |
| 180 | MessageID: types.MessageID(replyInfo[1]), |
| 181 | Sender: sender, |
| 182 | }, nil |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | err := fmt.Errorf("MessageID does not match format of {senderJID}:{messageID} : \"%s\"", id) |
| 187 | |
| 188 | return &Replyable{MessageID: id}, err |
| 189 | } |
| 190 | |
| 191 | func getParentIdFromCtx(ci *proto.ContextInfo) string { |
| 192 | if ci != nil && ci.StanzaID != nil { |
| 193 | senderJid, err := types.ParseJID(*ci.Participant) |
| 194 | |
| 195 | if err == nil { |
| 196 | return getMessageIdFormat(senderJid, *ci.StanzaID) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return "" |
| 201 | } |
| 202 | |
| 203 | func getMessageIdFormat(jid types.JID, messageID string) string { |
| 204 | // we're crafting our own JID str as AD JID format messes with how stuff looks on a webclient |
| 205 | jidStr := fmt.Sprintf("%s@%s", jid.User, jid.Server) |
| 206 | return fmt.Sprintf("%s/%s", jidStr, messageID) |
| 207 | } |
| 208 | |