forked from tulir/whatsmeow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
852 lines (801 loc) · 25.9 KB
/
user.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// Copyright (c) 2021 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package whatsmeow
import (
"context"
"errors"
"fmt"
"strings"
"google.golang.org/protobuf/proto"
waBinary "go.mau.fi/whatsmeow/binary"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
const BusinessMessageLinkPrefix = "https://wa.me/message/"
const ContactQRLinkPrefix = "https://wa.me/qr/"
const BusinessMessageLinkDirectPrefix = "https://api.whatsapp.com/message/"
const ContactQRLinkDirectPrefix = "https://api.whatsapp.com/qr/"
const NewsletterLinkPrefix = "https://whatsapp.com/channel/"
// ResolveBusinessMessageLink resolves a business message short link and returns the target JID, business name and
// text to prefill in the input field (if any).
//
// The links look like https://wa.me/message/<code> or https://api.whatsapp.com/message/<code>. You can either provide
// the full link, or just the <code> part.
func (cli *Client) ResolveBusinessMessageLink(code string) (*types.BusinessMessageLinkTarget, error) {
code = strings.TrimPrefix(code, BusinessMessageLinkPrefix)
code = strings.TrimPrefix(code, BusinessMessageLinkDirectPrefix)
resp, err := cli.sendIQ(infoQuery{
Namespace: "w:qr",
Type: iqGet,
// WhatsApp android doesn't seem to have a "to" field for this one at all, not sure why but it works
Content: []waBinary.Node{{
Tag: "qr",
Attrs: waBinary.Attrs{
"code": code,
},
}},
})
if errors.Is(err, ErrIQNotFound) {
return nil, wrapIQError(ErrBusinessMessageLinkNotFound, err)
} else if err != nil {
return nil, err
}
qrChild, ok := resp.GetOptionalChildByTag("qr")
if !ok {
return nil, &ElementMissingError{Tag: "qr", In: "response to business message link query"}
}
var target types.BusinessMessageLinkTarget
ag := qrChild.AttrGetter()
target.JID = ag.JID("jid")
target.PushName = ag.String("notify")
messageChild, ok := qrChild.GetOptionalChildByTag("message")
if ok {
messageBytes, _ := messageChild.Content.([]byte)
target.Message = string(messageBytes)
}
businessChild, ok := qrChild.GetOptionalChildByTag("business")
if ok {
bag := businessChild.AttrGetter()
target.IsSigned = bag.OptionalBool("is_signed")
target.VerifiedName = bag.OptionalString("verified_name")
target.VerifiedLevel = bag.OptionalString("verified_level")
}
return &target, ag.Error()
}
// ResolveContactQRLink resolves a link from a contact share QR code and returns the target JID and push name.
//
// The links look like https://wa.me/qr/<code> or https://api.whatsapp.com/qr/<code>. You can either provide
// the full link, or just the <code> part.
func (cli *Client) ResolveContactQRLink(code string) (*types.ContactQRLinkTarget, error) {
code = strings.TrimPrefix(code, ContactQRLinkPrefix)
code = strings.TrimPrefix(code, ContactQRLinkDirectPrefix)
resp, err := cli.sendIQ(infoQuery{
Namespace: "w:qr",
Type: iqGet,
Content: []waBinary.Node{{
Tag: "qr",
Attrs: waBinary.Attrs{
"code": code,
},
}},
})
if errors.Is(err, ErrIQNotFound) {
return nil, wrapIQError(ErrContactQRLinkNotFound, err)
} else if err != nil {
return nil, err
}
qrChild, ok := resp.GetOptionalChildByTag("qr")
if !ok {
return nil, &ElementMissingError{Tag: "qr", In: "response to contact link query"}
}
var target types.ContactQRLinkTarget
ag := qrChild.AttrGetter()
target.JID = ag.JID("jid")
target.PushName = ag.OptionalString("notify")
target.Type = ag.String("type")
return &target, ag.Error()
}
// GetContactQRLink gets your own contact share QR link that can be resolved using ResolveContactQRLink
// (or scanned with the official apps when encoded as a QR code).
//
// If the revoke parameter is set to true, it will ask the server to revoke the previous link and generate a new one.
func (cli *Client) GetContactQRLink(revoke bool) (string, error) {
action := "get"
if revoke {
action = "revoke"
}
resp, err := cli.sendIQ(infoQuery{
Namespace: "w:qr",
Type: iqSet,
Content: []waBinary.Node{{
Tag: "qr",
Attrs: waBinary.Attrs{
"type": "contact",
"action": action,
},
}},
})
if err != nil {
return "", err
}
qrChild, ok := resp.GetOptionalChildByTag("qr")
if !ok {
return "", &ElementMissingError{Tag: "qr", In: "response to own contact link fetch"}
}
ag := qrChild.AttrGetter()
return ag.String("code"), ag.Error()
}
// SetStatusMessage updates the current user's status text, which is shown in the "About" section in the user profile.
//
// This is different from the ephemeral status broadcast messages. Use SendMessage to types.StatusBroadcastJID to send
// such messages.
func (cli *Client) SetStatusMessage(msg string) error {
_, err := cli.sendIQ(infoQuery{
Namespace: "status",
Type: iqSet,
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "status",
Content: msg,
}},
})
return err
}
// IsOnWhatsApp checks if the given phone numbers are registered on WhatsApp.
// The phone numbers should be in international format, including the `+` prefix.
func (cli *Client) IsOnWhatsApp(phones []string) ([]types.IsOnWhatsAppResponse, error) {
jids := make([]types.JID, len(phones))
for i := range jids {
jids[i] = types.NewJID(phones[i], types.LegacyUserServer)
}
list, err := cli.usync(context.TODO(), jids, "query", "interactive", []waBinary.Node{
{Tag: "business", Content: []waBinary.Node{{Tag: "verified_name"}}},
{Tag: "contact"},
})
if err != nil {
return nil, err
}
output := make([]types.IsOnWhatsAppResponse, 0, len(jids))
querySuffix := "@" + types.LegacyUserServer
for _, child := range list.GetChildren() {
jid, jidOK := child.Attrs["jid"].(types.JID)
if child.Tag != "user" || !jidOK {
continue
}
var info types.IsOnWhatsAppResponse
info.JID = jid
info.VerifiedName, err = parseVerifiedName(child.GetChildByTag("business"))
if err != nil {
cli.Log.Warnf("Failed to parse %s's verified name details: %v", jid, err)
}
contactNode := child.GetChildByTag("contact")
info.IsIn = contactNode.AttrGetter().String("type") == "in"
contactQuery, _ := contactNode.Content.([]byte)
info.Query = strings.TrimSuffix(string(contactQuery), querySuffix)
output = append(output, info)
}
return output, nil
}
// GetUserInfo gets basic user info (avatar, status, verified business name, device list).
func (cli *Client) GetUserInfo(jids []types.JID) (map[types.JID]types.UserInfo, error) {
list, err := cli.usync(context.TODO(), jids, "full", "background", []waBinary.Node{
{Tag: "business", Content: []waBinary.Node{{Tag: "verified_name"}}},
{Tag: "status"},
{Tag: "picture"},
{Tag: "devices", Attrs: waBinary.Attrs{"version": "2"}},
})
if err != nil {
return nil, err
}
respData := make(map[types.JID]types.UserInfo, len(jids))
for _, child := range list.GetChildren() {
jid, jidOK := child.Attrs["jid"].(types.JID)
if child.Tag != "user" || !jidOK {
continue
}
var info types.UserInfo
verifiedName, err := parseVerifiedName(child.GetChildByTag("business"))
if err != nil {
cli.Log.Warnf("Failed to parse %s's verified name details: %v", jid, err)
}
status, _ := child.GetChildByTag("status").Content.([]byte)
info.Status = string(status)
info.PictureID, _ = child.GetChildByTag("picture").Attrs["id"].(string)
info.Devices = parseDeviceList(jid.User, child.GetChildByTag("devices"))
if verifiedName != nil {
cli.updateBusinessName(jid, nil, verifiedName.Details.GetVerifiedName())
}
respData[jid] = info
}
return respData, nil
}
func (cli *Client) GetBotListV2() ([]types.BotListInfo, error) {
resp, err := cli.sendIQ(infoQuery{
To: types.ServerJID,
Namespace: "bot",
Type: iqGet,
Content: []waBinary.Node{
{Tag: "bot", Attrs: waBinary.Attrs{"v": "2"}},
},
})
if err != nil {
return nil, err
}
botNode, ok := resp.GetOptionalChildByTag("bot")
if !ok {
return nil, &ElementMissingError{Tag: "bot", In: "response to bot list query"}
}
var list []types.BotListInfo
for _, section := range botNode.GetChildrenByTag("section") {
if section.AttrGetter().String("type") == "all" {
for _, bot := range section.GetChildrenByTag("bot") {
ag := bot.AttrGetter()
list = append(list, types.BotListInfo{
PersonaID: ag.String("persona_id"),
BotJID: ag.JID("jid"),
})
}
}
}
return list, nil
}
func (cli *Client) GetBotProfiles(botInfo []types.BotListInfo) ([]types.BotProfileInfo, error) {
jids := make([]types.JID, len(botInfo))
for i, bot := range botInfo {
jids[i] = bot.BotJID
}
list, err := cli.usync(context.TODO(), jids, "query", "interactive", []waBinary.Node{
{Tag: "bot", Content: []waBinary.Node{{Tag: "profile", Attrs: waBinary.Attrs{"v": "1"}}}},
}, UsyncQueryExtras{
BotListInfo: botInfo,
})
if err != nil {
return nil, err
}
var profiles []types.BotProfileInfo
for _, user := range list.GetChildren() {
jid := user.AttrGetter().JID("jid")
bot := user.GetChildByTag("bot")
profile := bot.GetChildByTag("profile")
name := string(profile.GetChildByTag("name").Content.([]byte))
attributes := string(profile.GetChildByTag("attributes").Content.([]byte))
description := string(profile.GetChildByTag("description").Content.([]byte))
category := string(profile.GetChildByTag("category").Content.([]byte))
_, isDefault := profile.GetOptionalChildByTag("default")
personaID := profile.AttrGetter().String("persona_id")
commandsNode := profile.GetChildByTag("commands")
commandDescription := string(commandsNode.GetChildByTag("description").Content.([]byte))
var commands []types.BotProfileCommand
for _, commandNode := range commandsNode.GetChildrenByTag("command") {
commands = append(commands, types.BotProfileCommand{
Name: string(commandNode.GetChildByTag("name").Content.([]byte)),
Description: string(commandNode.GetChildByTag("description").Content.([]byte)),
})
}
promptsNode := profile.GetChildByTag("prompts")
var prompts []string
for _, promptNode := range promptsNode.GetChildrenByTag("prompt") {
prompts = append(
prompts,
fmt.Sprintf(
"%s %s",
string(promptNode.GetChildByTag("emoji").Content.([]byte)),
string(promptNode.GetChildByTag("text").Content.([]byte)),
),
)
}
profiles = append(profiles, types.BotProfileInfo{
JID: jid,
Name: name,
Attributes: attributes,
Description: description,
Category: category,
IsDefault: isDefault,
Prompts: prompts,
PersonaID: personaID,
Commands: commands,
CommandsDescription: commandDescription,
})
}
return profiles, nil
}
func (cli *Client) parseBusinessProfile(node *waBinary.Node) (*types.BusinessProfile, error) {
profileNode := node.GetChildByTag("profile")
jid, ok := profileNode.AttrGetter().GetJID("jid", true)
if !ok {
return nil, errors.New("missing jid in business profile")
}
address := string(profileNode.GetChildByTag("address").Content.([]byte))
email := string(profileNode.GetChildByTag("email").Content.([]byte))
businessHour := profileNode.GetChildByTag("business_hours")
businessHourTimezone := businessHour.AttrGetter().String("timezone")
businessHoursConfigs := businessHour.GetChildren()
businessHours := make([]types.BusinessHoursConfig, 0)
for _, config := range businessHoursConfigs {
if config.Tag != "business_hours_config" {
continue
}
dow := config.AttrGetter().String("dow")
mode := config.AttrGetter().String("mode")
openTime := config.AttrGetter().String("open_time")
closeTime := config.AttrGetter().String("close_time")
businessHours = append(businessHours, types.BusinessHoursConfig{
DayOfWeek: dow,
Mode: mode,
OpenTime: openTime,
CloseTime: closeTime,
})
}
categoriesNode := profileNode.GetChildByTag("categories")
categories := make([]types.Category, 0)
for _, category := range categoriesNode.GetChildren() {
if category.Tag != "category" {
continue
}
id := category.AttrGetter().String("id")
name := string(category.Content.([]byte))
categories = append(categories, types.Category{
ID: id,
Name: name,
})
}
profileOptionsNode := profileNode.GetChildByTag("profile_options")
profileOptions := make(map[string]string)
for _, option := range profileOptionsNode.GetChildren() {
profileOptions[option.Tag] = string(option.Content.([]byte))
}
return &types.BusinessProfile{
JID: jid,
Email: email,
Address: address,
Categories: categories,
ProfileOptions: profileOptions,
BusinessHoursTimeZone: businessHourTimezone,
BusinessHours: businessHours,
}, nil
}
// GetBusinessProfile gets the profile info of a WhatsApp business account
func (cli *Client) GetBusinessProfile(jid types.JID) (*types.BusinessProfile, error) {
resp, err := cli.sendIQ(infoQuery{
Type: iqGet,
To: types.ServerJID,
Namespace: "w:biz",
Content: []waBinary.Node{{
Tag: "business_profile",
Attrs: waBinary.Attrs{
"v": "244",
},
Content: []waBinary.Node{{
Tag: "profile",
Attrs: waBinary.Attrs{
"jid": jid,
},
}},
}},
})
if err != nil {
return nil, err
}
node, ok := resp.GetOptionalChildByTag("business_profile")
if !ok {
return nil, &ElementMissingError{Tag: "business_profile", In: "response to business profile query"}
}
return cli.parseBusinessProfile(&node)
}
// GetUserDevices gets the list of devices that the given user has. The input should be a list of
// regular JIDs, and the output will be a list of AD JIDs. The local device will not be included in
// the output even if the user's JID is included in the input. All other devices will be included.
func (cli *Client) GetUserDevices(jids []types.JID) ([]types.JID, error) {
return cli.GetUserDevicesContext(context.Background(), jids)
}
func (cli *Client) GetUserDevicesContext(ctx context.Context, jids []types.JID) ([]types.JID, error) {
cli.userDevicesCacheLock.Lock()
defer cli.userDevicesCacheLock.Unlock()
var devices, jidsToSync, fbJIDsToSync []types.JID
for _, jid := range jids {
cached, ok := cli.userDevicesCache[jid]
if ok && len(cached.devices) > 0 {
devices = append(devices, cached.devices...)
} else if jid.Server == types.MessengerServer {
fbJIDsToSync = append(fbJIDsToSync, jid)
} else if jid.IsBot() {
// Bot JIDs do not have devices, the usync query is empty
devices = append(devices, jid)
} else {
jidsToSync = append(jidsToSync, jid)
}
}
if len(jidsToSync) > 0 {
list, err := cli.usync(ctx, jidsToSync, "query", "message", []waBinary.Node{
{Tag: "devices", Attrs: waBinary.Attrs{"version": "2"}},
})
if err != nil {
return nil, err
}
for _, user := range list.GetChildren() {
jid, jidOK := user.Attrs["jid"].(types.JID)
if user.Tag != "user" || !jidOK {
continue
}
userDevices := parseDeviceList(jid.User, user.GetChildByTag("devices"))
cli.userDevicesCache[jid] = deviceCache{devices: userDevices, dhash: participantListHashV2(userDevices)}
devices = append(devices, userDevices...)
}
}
if len(fbJIDsToSync) > 0 {
list, err := cli.getFBIDDevices(ctx, fbJIDsToSync)
if err != nil {
return nil, err
}
for _, user := range list.GetChildren() {
jid, jidOK := user.Attrs["jid"].(types.JID)
if user.Tag != "user" || !jidOK {
continue
}
userDevices := parseFBDeviceList(jid, user.GetChildByTag("devices"))
cli.userDevicesCache[jid] = userDevices
devices = append(devices, userDevices.devices...)
}
}
return devices, nil
}
type GetProfilePictureParams struct {
Preview bool
ExistingID string
IsCommunity bool
}
// GetProfilePictureInfo gets the URL where you can download a WhatsApp user's profile picture or group's photo.
//
// Optionally, you can pass the last known profile picture ID.
// If the profile picture hasn't changed, this will return nil with no error.
//
// To get a community photo, you should pass `IsCommunity: true`, as otherwise you may get a 401 error.
func (cli *Client) GetProfilePictureInfo(jid types.JID, params *GetProfilePictureParams) (*types.ProfilePictureInfo, error) {
attrs := waBinary.Attrs{
"query": "url",
}
var target, to types.JID
if params == nil {
params = &GetProfilePictureParams{}
}
if params.Preview {
attrs["type"] = "preview"
} else {
attrs["type"] = "image"
}
if params.ExistingID != "" {
attrs["id"] = params.ExistingID
}
var expectWrapped bool
var content []waBinary.Node
namespace := "w:profile:picture"
if params.IsCommunity {
target = types.EmptyJID
namespace = "w:g2"
to = jid
attrs["parent_group_jid"] = jid
expectWrapped = true
content = []waBinary.Node{{
Tag: "pictures",
Content: []waBinary.Node{{
Tag: "picture",
Attrs: attrs,
}},
}}
} else {
to = types.ServerJID
target = jid
content = []waBinary.Node{{
Tag: "picture",
Attrs: attrs,
}}
}
resp, err := cli.sendIQ(infoQuery{
Namespace: namespace,
Type: "get",
To: to,
Target: target,
Content: content,
})
if errors.Is(err, ErrIQNotAuthorized) {
return nil, wrapIQError(ErrProfilePictureUnauthorized, err)
} else if errors.Is(err, ErrIQNotFound) {
return nil, wrapIQError(ErrProfilePictureNotSet, err)
} else if err != nil {
return nil, err
}
if expectWrapped {
pics, ok := resp.GetOptionalChildByTag("pictures")
if !ok {
return nil, &ElementMissingError{Tag: "pictures", In: "response to profile picture query"}
}
resp = &pics
}
picture, ok := resp.GetOptionalChildByTag("picture")
if !ok {
if params.ExistingID != "" {
return nil, nil
}
return nil, &ElementMissingError{Tag: "picture", In: "response to profile picture query"}
}
var info types.ProfilePictureInfo
ag := picture.AttrGetter()
if ag.OptionalInt("status") == 304 {
return nil, nil
}
info.ID = ag.String("id")
info.URL = ag.String("url")
info.Type = ag.String("type")
info.DirectPath = ag.String("direct_path")
if !ag.OK() {
return &info, ag.Error()
}
return &info, nil
}
func (cli *Client) handleHistoricalPushNames(names []*waProto.Pushname) {
if cli.Store.Contacts == nil {
return
}
cli.Log.Infof("Updating contact store with %d push names from history sync", len(names))
for _, user := range names {
if user.GetPushname() == "-" {
continue
}
var changed bool
if jid, err := types.ParseJID(user.GetId()); err != nil {
cli.Log.Warnf("Failed to parse user ID '%s' in push name history sync: %v", user.GetId(), err)
} else if changed, _, err = cli.Store.Contacts.PutPushName(jid, user.GetPushname()); err != nil {
cli.Log.Warnf("Failed to store push name of %s from history sync: %v", err)
} else if changed {
cli.Log.Debugf("Got push name %s for %s in history sync", user.GetPushname(), jid)
}
}
}
func (cli *Client) updatePushName(user types.JID, messageInfo *types.MessageInfo, name string) {
if cli.Store.Contacts == nil {
return
}
user = user.ToNonAD()
changed, previousName, err := cli.Store.Contacts.PutPushName(user, name)
if err != nil {
cli.Log.Errorf("Failed to save push name of %s in device store: %v", user, err)
} else if changed {
cli.Log.Debugf("Push name of %s changed from %s to %s, dispatching event", user, previousName, name)
cli.dispatchEvent(&events.PushName{
JID: user,
Message: messageInfo,
OldPushName: previousName,
NewPushName: name,
})
}
}
func (cli *Client) updateBusinessName(user types.JID, messageInfo *types.MessageInfo, name string) {
if cli.Store.Contacts == nil {
return
}
changed, previousName, err := cli.Store.Contacts.PutBusinessName(user, name)
if err != nil {
cli.Log.Errorf("Failed to save business name of %s in device store: %v", user, err)
} else if changed {
cli.Log.Debugf("Business name of %s changed from %s to %s, dispatching event", user, previousName, name)
cli.dispatchEvent(&events.BusinessName{
JID: user,
Message: messageInfo,
OldBusinessName: previousName,
NewBusinessName: name,
})
}
}
func parseVerifiedName(businessNode waBinary.Node) (*types.VerifiedName, error) {
if businessNode.Tag != "business" {
return nil, nil
}
verifiedNameNode, ok := businessNode.GetOptionalChildByTag("verified_name")
if !ok {
return nil, nil
}
return parseVerifiedNameContent(verifiedNameNode)
}
func parseVerifiedNameContent(verifiedNameNode waBinary.Node) (*types.VerifiedName, error) {
rawCert, ok := verifiedNameNode.Content.([]byte)
if !ok {
return nil, nil
}
var cert waProto.VerifiedNameCertificate
err := proto.Unmarshal(rawCert, &cert)
if err != nil {
return nil, err
}
var certDetails waProto.VerifiedNameCertificate_Details
err = proto.Unmarshal(cert.GetDetails(), &certDetails)
if err != nil {
return nil, err
}
return &types.VerifiedName{
Certificate: &cert,
Details: &certDetails,
}, nil
}
func parseDeviceList(user string, deviceNode waBinary.Node) []types.JID {
deviceList := deviceNode.GetChildByTag("device-list")
if deviceNode.Tag != "devices" || deviceList.Tag != "device-list" {
return nil
}
children := deviceList.GetChildren()
devices := make([]types.JID, 0, len(children))
for _, device := range children {
deviceID, ok := device.AttrGetter().GetInt64("id", true)
if device.Tag != "device" || !ok {
continue
}
devices = append(devices, types.NewADJID(user, 0, byte(deviceID)))
}
return devices
}
func parseFBDeviceList(user types.JID, deviceList waBinary.Node) deviceCache {
children := deviceList.GetChildren()
devices := make([]types.JID, 0, len(children))
for _, device := range children {
deviceID, ok := device.AttrGetter().GetInt64("id", true)
if device.Tag != "device" || !ok {
continue
}
user.Device = uint16(deviceID)
devices = append(devices, user)
// TODO take identities here too?
}
// TODO do something with the icdc blob?
return deviceCache{
devices: devices,
dhash: deviceList.AttrGetter().String("dhash"),
}
}
func (cli *Client) getFBIDDevices(ctx context.Context, jids []types.JID) (*waBinary.Node, error) {
users := make([]waBinary.Node, len(jids))
for i, jid := range jids {
users[i].Tag = "user"
users[i].Attrs = waBinary.Attrs{"jid": jid}
// TODO include dhash for users
}
resp, err := cli.sendIQ(infoQuery{
Context: ctx,
Namespace: "fbid:devices",
Type: iqGet,
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "users",
Content: users,
}},
})
if err != nil {
return nil, fmt.Errorf("failed to send usync query: %w", err)
} else if list, ok := resp.GetOptionalChildByTag("users"); !ok {
return nil, &ElementMissingError{Tag: "users", In: "response to fbid devices query"}
} else {
return &list, err
}
}
type UsyncQueryExtras struct {
BotListInfo []types.BotListInfo
}
func (cli *Client) usync(ctx context.Context, jids []types.JID, mode, context string, query []waBinary.Node, extra ...UsyncQueryExtras) (*waBinary.Node, error) {
var extras UsyncQueryExtras
if len(extra) > 1 {
return nil, errors.New("only one extra parameter may be provided to usync()")
} else if len(extra) == 1 {
extras = extra[0]
}
userList := make([]waBinary.Node, len(jids))
for i, jid := range jids {
userList[i].Tag = "user"
jid = jid.ToNonAD()
switch jid.Server {
case types.LegacyUserServer:
userList[i].Content = []waBinary.Node{{
Tag: "contact",
Content: jid.String(),
}}
case types.DefaultUserServer:
userList[i].Attrs = waBinary.Attrs{"jid": jid}
if jid.IsBot() {
var personaId string
for _, bot := range extras.BotListInfo {
if bot.BotJID.User == jid.User {
personaId = bot.PersonaID
}
}
userList[i].Content = []waBinary.Node{{
Tag: "bot",
Content: []waBinary.Node{{
Tag: "profile",
Attrs: waBinary.Attrs{"persona_id": personaId},
}},
}}
}
default:
return nil, fmt.Errorf("unknown user server '%s'", jid.Server)
}
}
resp, err := cli.sendIQ(infoQuery{
Context: ctx,
Namespace: "usync",
Type: "get",
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "usync",
Attrs: waBinary.Attrs{
"sid": cli.generateRequestID(),
"mode": mode,
"last": "true",
"index": "0",
"context": context,
},
Content: []waBinary.Node{
{Tag: "query", Content: query},
{Tag: "list", Content: userList},
},
}},
})
if err != nil {
return nil, fmt.Errorf("failed to send usync query: %w", err)
} else if list, ok := resp.GetOptionalChildByTag("usync", "list"); !ok {
return nil, &ElementMissingError{Tag: "list", In: "response to usync query"}
} else {
return &list, err
}
}
func (cli *Client) parseBlocklist(node *waBinary.Node) *types.Blocklist {
output := &types.Blocklist{
DHash: node.AttrGetter().String("dhash"),
}
for _, child := range node.GetChildren() {
ag := child.AttrGetter()
blockedJID := ag.JID("jid")
if !ag.OK() {
cli.Log.Debugf("Ignoring contact blocked data with unexpected attributes: %v", ag.Error())
continue
}
output.JIDs = append(output.JIDs, blockedJID)
}
return output
}
// GetBlocklist gets the list of users that this user has blocked.
func (cli *Client) GetBlocklist() (*types.Blocklist, error) {
resp, err := cli.sendIQ(infoQuery{
Namespace: "blocklist",
Type: iqGet,
To: types.ServerJID,
})
if err != nil {
return nil, err
}
list, ok := resp.GetOptionalChildByTag("list")
if !ok {
return nil, &ElementMissingError{Tag: "list", In: "response to blocklist query"}
}
return cli.parseBlocklist(&list), nil
}
// UpdateBlocklist updates the user's block list and returns the updated list.
func (cli *Client) UpdateBlocklist(jid types.JID, action events.BlocklistChangeAction) (*types.Blocklist, error) {
resp, err := cli.sendIQ(infoQuery{
Namespace: "blocklist",
Type: iqSet,
To: types.ServerJID,
Content: []waBinary.Node{{
Tag: "item",
Attrs: waBinary.Attrs{
"jid": jid,
"action": string(action),
},
}},
})
list, ok := resp.GetOptionalChildByTag("list")
if !ok {
return nil, &ElementMissingError{Tag: "list", In: "response to blocklist update"}
}
return cli.parseBlocklist(&list), err
}