forked from davidgfnet/whatsapp-purple
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhatsapp-protocol.cc
2863 lines (2499 loc) · 89.4 KB
/
whatsapp-protocol.cc
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* WhatsApp API implementation in C++ for libpurple.
* Written by David Guillen Fandos ([email protected]) based
* on the sources of WhatsAPI PHP implementation.
* v1.4 changes based on WP7 sources
*
* Share and enjoy!
*
*/
#include <iostream>
#include <map>
#include <vector>
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#ifdef ENABLE_OPENSSL
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#else
#include "wa_api.h"
#endif
#include "wadict.h"
std::string temp_thumbnail = "/9j/4AAQSkZJRgABAQEASABIAAD/4QCURXhpZgAASUkqAAgAAAADADEBAgAcAAAAMgAAADIBAgAUAAAATgAAAGmHBAABAAAAYgAAAAAAAABBZG9iZSBQaG90b3Nob3AgQ1MyIFdpbmRvd3MAMjAwNzoxMDoyMCAyMDo1NDo1OQADAAGgAwABAAAA//8SAAKgBAABAAAAvBIAAAOgBAABAAAAoA8AAAAAAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAABtbnRyUkdCIFhZWiAHzgACAAkABgAxAABhY3NwTVNGVAAAAABJRUMgc1JHQgAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLUhQICAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABFjcHJ0AAABUAAAADNkZXNjAAABhAAAAGx3dHB0AAAB8AAAABRia3B0AAACBAAAABRyWFlaAAACGAAAABRnWFlaAAACLAAAABRiWFlaAAACQAAAABRkbW5kAAACVAAAAHBkbWRkAAACxAAAAIh2dWVkAAADTAAAAIZ2aWV3AAAD1AAAACRsdW1pAAAD+AAAABRtZWFzAAAEDAAAACR0ZWNoAAAEMAAAAAxyVFJDAAAEPAAACAxnVFJDAAAEPAAACAxiVFJDAAAEPAAACAx0ZXh0AAAAAENvcHlyaWdodCAoYykgMTk5OCBIZXdsZX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DlMRygkfcutkGK8NrW94zu99YmTr6ChHtYqb5ESnlVCnBVtPWNoqACVkQQIvDwbgnUsYE5KWJ2bMSTrqHXtPI/w1lyN2EHjnjg+/3GtBuEeU5lDig5juWuMriErslTsAQ2Q6DOYlrcwZJvN5iGgVOsM4GbeRwKnfwmPGkzFLPyoSiLKUtQkbIos0YJdZy6tfFDYAAR3H3H03H3Hb23H2DsHoHbXOwf39fvprwUMXVxz3JIAxlv2XtWZXILSSOSQPQACRg9ka/wAq/qWLM1v3XvnPbxr7cp5aWIUdqYSpgcJSroyQVKVaKJHfhmdns3JIxYtzseZJTwojhjhijaaaakdU7TTTTTTTTTTTTTTTTTTTTTTTTXkbnBGHnV7VyerjushfF0Ct31jQYA1dSpUVSKt1JtBqZFjOOmp0k/KPphq9eNCkBNsukn8uvXNNNNNNNNNNNNNNNNNNNNNNNf/Z";
class RC4Decoder;
class DataBuffer;
class Tree;
class WhatsappConnection;
#define MESSAGE_CHAT 0
#define MESSAGE_IMAGE 1
#define MESSAGE_LOCATION 2
enum SessionStatus { SessionNone = 0, SessionConnecting = 1, SessionWaitingChallenge = 2, SessionWaitingAuthOK = 3, SessionConnected = 4 };
enum ErrorCode { errorAuth, errorUnknown };
struct t_fileupload {
std::string to, from;
std::string file, hash;
int rid;
std::string type;
std::string uploadurl, host;
bool uploading;
int totalsize;
};
std::string base64_decode(std::string const &encoded_string);
unsigned long long str2lng(std::string s);
std::string int2str(unsigned int num);
int str2int(std::string s);
double str2dbl(std::string s);
unsigned short lookupDecoded(std::string value);
bool getDecoded(int n, std::string & res);
std::string getDecodedExtended(int n, int n2);
unsigned long long str2lng(std::string s)
{
unsigned long long r;
#ifdef _WIN32
sscanf(s.c_str(), "%I64u", &r);
#else
sscanf(s.c_str(), "%llu", &r);
#endif
return r;
}
std::string int2str(unsigned int num)
{
char temp[512];
sprintf(temp, "%d", num);
return std::string(temp);
}
int str2int(std::string s)
{
int d;
sscanf(s.c_str(), "%d", &d);
return d;
}
double str2dbl(std::string s)
{
// Manual parsing. we assume dot as separator :)
int i1,i2;
sscanf(s.c_str(), "%d.%d", &i1, &i2);
double d;
int i3 = i2;
int n = 1;
while (i3 > 0) {
i3 /= 10;
n *= 10;
}
d = i1 + ((double)i2)/n;
return d;
}
std::string getusername(std::string user)
{
size_t pos = user.find('@');
if (pos != std::string::npos)
return user.substr(0, pos);
else
return user;
}
inline std::map < std::string, std::string > makeAttr1(std::string k1, std::string v1)
{
std::map < std::string, std::string > at;
at[k1] = v1;
return at;
}
inline std::map < std::string, std::string > makeAttr2(std::string k1, std::string v1, std::string k2, std::string v2)
{
std::map < std::string, std::string > at;
at[k1] = v1;
at[k2] = v2;
return at;
}
inline std::map < std::string, std::string > makeAttr3(std::string k1, std::string v1, std::string k2, std::string v2, std::string k3, std::string v3)
{
std::map < std::string, std::string > at;
at[k1] = v1;
at[k2] = v2;
at[k3] = v3;
return at;
}
inline std::map < std::string, std::string > makeAttr4(std::string k1, std::string v1, std::string k2, std::string v2, std::string k3, std::string v3, std::string k4, std::string v4)
{
std::map < std::string, std::string > at;
at[k1] = v1;
at[k2] = v2;
at[k3] = v3;
at[k4] = v4;
return at;
}
inline std::map < std::string, std::string > makeAttr5(std::string k1, std::string v1, std::string k2, std::string v2, std::string k3, std::string v3, std::string k4, std::string v4, std::string k5, std::string v5)
{
std::map < std::string, std::string > at;
at[k1] = v1;
at[k2] = v2;
at[k3] = v3;
at[k4] = v4;
at[k5] = v5;
return at;
}
// This changed to a huffman-like encoding. Therefore we can use one or two bytes...
bool getDecoded(int n, std::string & res)
{
if (n < 236) {
res = std::string(main_dict[n]);
return true;
}
return false; // Extended code!
}
std::string getDecodedExtended(int n, int n2)
{
if (n == 236) {
return std::string(sec_dict[n2]);
} // In the future add more secondary dictionaries here
return ""; // This should never happen
}
// Return a token (or extended token) for a given value
unsigned short lookupDecoded(std::string value)
{
for (unsigned int i = 0; i < sizeof(main_dict)/sizeof(main_dict[0]); i++) {
if (strcmp(main_dict[i], value.c_str()) == 0)
return i;
}
// Not found in the main dict, search the secondary
for (unsigned int i = 0; i < sizeof(sec_dict)/sizeof(sec_dict[0]); i++) {
if (strcmp(sec_dict[i], value.c_str()) == 0)
return i | 0x0100;
}
return 0;
}
const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
const char hexmap2[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
class KeyGenerator {
public:
static void generateKeyImei(const char *imei, const char *salt, int saltlen, char *out)
{
char imeir[strlen(imei)];
for (unsigned int i = 0; i < strlen(imei); i++)
imeir[i] = imei[strlen(imei) - i - 1];
char hash[16];
MD5((unsigned char *)imeir, strlen(imei), (unsigned char *)hash);
/* Convert to hex */
char hashhex[32];
for (int i = 0; i < 16; i++) {
hashhex[2 * i] = hexmap[(hash[i] >> 4) & 0xF];
hashhex[2 * i + 1] = hexmap[hash[i] & 0xF];
}
PKCS5_PBKDF2_HMAC_SHA1(hashhex, 32, (unsigned char *)salt, saltlen, 16, 20, (unsigned char *)out);
}
static void generateKeyV2(const std::string pw, const char *salt, int saltlen, char *out)
{
std::string dec = base64_decode(pw);
PKCS5_PBKDF2_HMAC_SHA1(dec.c_str(), 20, (unsigned char *)salt, saltlen, 16, 20, (unsigned char *)out);
}
// New way to derive key.
static void generateKeysV14(const std::string pw, const char *salt, int saltlen, char *out)
{
std::string dec = base64_decode(pw);
char salt_[saltlen+1]; memcpy(salt_,salt,saltlen);
for (int i = 0; i < 4; i++) {
salt_[saltlen] = (i+1);
PKCS5_PBKDF2_HMAC_SHA1(dec.c_str(), 20, (unsigned char *)salt_, saltlen+1, 2, 20, (unsigned char *)&out[20*i]);
}
}
static void generateKeyMAC(std::string macaddr, const char *salt, int saltlen, char *out)
{
macaddr = macaddr + macaddr;
char hash[16];
MD5((unsigned char *)macaddr.c_str(), 34, (unsigned char *)hash);
/* Convert to hex */
char hashhex[32];
for (int i = 0; i < 16; i++) {
hashhex[2 * i] = hexmap[(hash[i] >> 4) & 0xF];
hashhex[2 * i + 1] = hexmap[hash[i] & 0xF];
}
PKCS5_PBKDF2_HMAC_SHA1(hashhex, 32, (unsigned char *)salt, saltlen, 16, 20, (unsigned char *)out);
}
static void calc_hmac_v12(const unsigned char *data, int l, const unsigned char *key, unsigned char *hmac)
{
unsigned char temp[20];
HMAC_SHA1(data, l, key, 20, temp);
memcpy(hmac, temp, 4);
}
static void calc_hmac(const unsigned char *data, int l, const unsigned char *key, unsigned char * hmac, unsigned int seq){
unsigned char temp[20];
unsigned char data_temp[l+4];
memcpy(data_temp, data, l);
data_temp[l ] = (seq >> 24);
data_temp[l+1] = (seq >> 16);
data_temp[l+2] = (seq >> 8);
data_temp[l+3] = (seq );
HMAC_SHA1 (data_temp,l+4,key,20,temp);
memcpy(hmac,temp,4);
}
private:
static void HMAC_SHA1(const unsigned char *text, int text_len, const unsigned char *key, int key_len, unsigned char *digest)
{
unsigned char SHA1_Key[4096], AppendBuf2[4096], szReport[4096];
unsigned char *AppendBuf1 = new unsigned char[text_len + 64];
unsigned char m_ipad[64], m_opad[64];
memset(SHA1_Key, 0, 64);
memset(m_ipad, 0x36, sizeof(m_ipad));
memset(m_opad, 0x5c, sizeof(m_opad));
if (key_len > 64)
SHA1(key, key_len, SHA1_Key);
else
memcpy(SHA1_Key, key, key_len);
for (unsigned int i = 0; i < sizeof(m_ipad); i++)
m_ipad[i] ^= SHA1_Key[i];
memcpy(AppendBuf1, m_ipad, sizeof(m_ipad));
memcpy(AppendBuf1 + sizeof(m_ipad), text, text_len);
SHA1(AppendBuf1, sizeof(m_ipad) + text_len, szReport);
for (unsigned int j = 0; j < sizeof(m_opad); j++)
m_opad[j] ^= SHA1_Key[j];
memcpy(AppendBuf2, m_opad, sizeof(m_opad));
memcpy(AppendBuf2 + sizeof(m_opad), szReport, 20);
SHA1(AppendBuf2, sizeof(m_opad) + 20, digest);
delete[]AppendBuf1;
}
};
class RC4Decoder {
public:
unsigned char s[256];
unsigned char i, j;
inline void swap(unsigned char i, unsigned char j)
{
unsigned char t = s[i];
s[i] = s[j];
s[j] = t;
}
public:
RC4Decoder(const unsigned char *key, int keylen, int drop)
{
for (unsigned int k = 0; k < 256; k++)
s[k] = k;
i = j = 0;
do {
unsigned char k = key[i % keylen];
j = (j + k + s[i]) & 0xFF;
swap(i, j);
} while (++i != 0);
i = j = 0;
unsigned char temp[drop];
for (int k = 0; k < drop; k++)
temp[k] = 0;
cipher(temp, drop);
}
void cipher(unsigned char *data, int len)
{
while (len--) {
i++;
j += s[i];
swap(i, j);
unsigned char idx = s[i] + s[j];
*data++ ^= s[idx];
}
}
};
class DataBuffer {
private:
unsigned char *buffer;
int blen;
public:
DataBuffer(const void *ptr = 0, int size = 0)
{
if (ptr != NULL and size > 0) {
buffer = (unsigned char *)malloc(size + 1);
memcpy(buffer, ptr, size);
blen = size;
} else {
blen = 0;
buffer = (unsigned char *)malloc(1024);
}
}
~DataBuffer()
{
free(buffer);
}
DataBuffer(const DataBuffer & other)
{
blen = other.blen;
buffer = (unsigned char *)malloc(blen + 1024);
memcpy(buffer, other.buffer, blen);
}
DataBuffer operator+(const DataBuffer & other) const
{
DataBuffer result = *this;
result.addData(other.buffer, other.blen);
return result;
}
DataBuffer & operator =(const DataBuffer & other)
{
if (this != &other) {
free(buffer);
this->blen = other.blen;
buffer = (unsigned char *)malloc(blen + 1024);
memcpy(buffer, other.buffer, blen);
}
return *this;
}
DataBuffer(const DataBuffer * d)
{
blen = d->blen;
buffer = (unsigned char *)malloc(blen + 1024);
memcpy(buffer, d->buffer, blen);
}
DataBuffer *decodedBuffer(RC4Decoder * decoder, int clength, bool dout)
{
DataBuffer *deco = new DataBuffer(this->buffer, clength);
decoder->cipher(&deco->buffer[0], clength - 4);
return deco;
}
DataBuffer encodedBuffer(RC4Decoder * decoder, unsigned char *key, bool dout, unsigned int seq)
{
DataBuffer deco = *this;
decoder->cipher(&deco.buffer[0], blen);
unsigned char hmacint[4];
DataBuffer hmac;
KeyGenerator::calc_hmac(deco.buffer, blen, key, (unsigned char *)&hmacint, seq);
hmac.addData(hmacint, 4);
if (dout)
deco = deco + hmac;
else
deco = hmac + deco;
return deco;
}
void clear()
{
blen = 0;
free(buffer);
buffer = (unsigned char *)malloc(1);
}
void *getPtr()
{
return buffer;
}
void addData(const void *ptr, int size)
{
if (ptr != NULL and size > 0) {
buffer = (unsigned char *)realloc(buffer, blen + size);
memcpy(&buffer[blen], ptr, size);
blen += size;
}
}
void popData(int size)
{
if (size > blen) {
throw 0;
} else {
memmove(&buffer[0], &buffer[size], blen - size);
blen -= size;
if (blen + size > blen * 2 and blen > 8 * 1024)
buffer = (unsigned char *)realloc(buffer, blen + 1);
}
}
void crunchData(int size)
{
if (size > blen) {
throw 0;
} else {
blen -= size;
}
}
int getInt(int nbytes, int offset = 0)
{
if (nbytes > blen)
throw 0;
int ret = 0;
for (int i = 0; i < nbytes; i++) {
ret <<= 8;
ret |= buffer[i + offset];
}
return ret;
}
void putInt(int value, int nbytes)
{
assert(nbytes > 0);
unsigned char out[nbytes];
for (int i = 0; i < nbytes; i++) {
out[nbytes - i - 1] = (value >> (i << 3)) & 0xFF;
}
this->addData(out, nbytes);
}
int readInt(int nbytes)
{
if (nbytes > blen)
throw 0;
int ret = getInt(nbytes);
popData(nbytes);
return ret;
}
int readListSize()
{
if (blen == 0)
throw 0;
int ret;
if (buffer[0] == 0xf8 or buffer[0] == 0xf3) {
ret = buffer[1];
popData(2);
} else if (buffer[0] == 0xf9) {
ret = getInt(2, 1);
popData(3);
} else {
/* FIXME throw 0 error */
ret = -1;
printf("Parse error!!\n");
}
return ret;
}
void writeListSize(int size)
{
if (size == 0) {
putInt(0, 1);
} else if (size < 256) {
putInt(0xf8, 1);
putInt(size, 1);
} else {
putInt(0xf9, 1);
putInt(size, 2);
}
}
std::string readRawString(int size)
{
if (size < 0 or size > blen)
throw 0;
std::string st(size, ' ');
memcpy(&st[0], buffer, size);
popData(size);
return st;
}
std::string readString()
{
if (blen == 0)
throw 0;
int type = readInt(1);
if (type > 2 and type <= 236) {
std::string r;
if (getDecoded(type,r))
return r;
return getDecodedExtended(type, readInt(1));
} else if (type == 0) {
return "";
} else if (type == 252) {
int slen = readInt(1);
return readRawString(slen);
} else if (type == 253) {
int slen = readInt(3);
return readRawString(slen);
} else if (type == 250) {
std::string u = readString();
std::string s = readString();
if (u.size() > 0 and s.size() > 0)
return u + "@" + s;
else if (s.size() > 0)
return s;
return "";
}
return "";
}
void putRawString(std::string s)
{
if (s.size() < 256) {
putInt(0xfc, 1);
putInt(s.size(), 1);
addData(s.c_str(), s.size());
} else {
putInt(0xfd, 1);
putInt(s.size(), 3);
addData(s.c_str(), s.size());
}
}
void putString(std::string s)
{
unsigned short lu = lookupDecoded(s);
int sub_dict = (lu >> 8);
if (sub_dict != 0)
putInt(sub_dict + 236 - 1, 1); // Put dict byte first!
if (lu != 0) {
putInt(lu & 0xFF, 1); // Now put second byte
} else if (s.find('@') != std::string::npos) {
std::string p1 = s.substr(0, s.find('@'));
std::string p2 = s.substr(s.find('@') + 1);
putInt(250, 1);
putString(p1);
putString(p2);
} else if (s.size() < 256) {
putInt(252, 1);
putInt(s.size(), 1);
addData(s.c_str(), s.size());
} else {
putInt(253, 1);
putInt(s.size(), 3);
addData(s.c_str(), s.size());
}
}
bool isList()
{
if (blen == 0)
throw 0;
return (buffer[0] == 248 or buffer[0] == 0 or buffer[0] == 249);
}
std::vector < Tree > readList(WhatsappConnection * c);
int size()
{
return blen;
}
std::string toString()
{
std::string r(blen, ' ');
memcpy(&r[0], buffer, blen);
return r;
}
};
class Tree {
private:
std::map < std::string, std::string > attributes;
std::vector < Tree > children;
std::string tag, data;
bool forcedata;
public:
Tree(std::string tag = "")
{
this->tag = tag;
forcedata = false;
}
Tree(std::string tag, std::map < std::string, std::string > attributes)
{
this->tag = tag;
this->attributes = attributes;
forcedata = false;
}
~Tree()
{
}
void forceDataWrite()
{
forcedata = true;
}
bool forcedData() const
{
return forcedata;
}
void addChild(Tree t)
{
children.push_back(t);
}
void setTag(std::string tag)
{
this->tag = tag;
}
void setAttributes(std::map < std::string, std::string > attributes)
{
this->attributes = attributes;
}
void readAttributes(DataBuffer * data, int size)
{
int count = (size - 2 + (size % 2)) / 2;
while (count--) {
std::string key = data->readString();
std::string value = data->readString();
attributes[key] = value;
}
}
void writeAttributes(DataBuffer * data)
{
for (std::map < std::string, std::string >::iterator iter = attributes.begin(); iter != attributes.end(); iter++) {
data->putString(iter->first);
data->putString(iter->second);
}
}
void setData(const std::string d)
{
data = d;
}
std::string getData()
{
return data;
}
std::string getTag()
{
return tag;
}
void setChildren(std::vector < Tree > c)
{
children = c;
}
std::vector < Tree > getChildren()
{
return children;
}
std::map < std::string, std::string > &getAttributes()
{
return attributes;
}
bool hasAttributeValue(std::string at, std::string val)
{
if (hasAttribute(at)) {
return (attributes[at] == val);
}
return false;
}
bool hasAttribute(std::string at)
{
return (attributes.find(at) != attributes.end());
}
std::string getAttribute(std::string at)
{
if (hasAttribute(at))
return (attributes[at]);
return "";
}
Tree getChild(std::string tag)
{
for (unsigned int i = 0; i < children.size(); i++) {
if (children[i].getTag() == tag)
return children[i];
Tree t = children[i].getChild(tag);
if (t.getTag() != "treeerr")
return t;
}
return Tree("treeerr");
}
bool hasChild(std::string tag)
{
for (unsigned int i = 0; i < children.size(); i++) {
if (children[i].getTag() == tag)
return true;
if (children[i].hasChild(tag))
return true;
}
return false;
}
std::string toString(int sp = 0)
{
std::string ret;
std::string spacing(' ', sp);
ret += spacing + "Tag: " + tag + "\n";
for (std::map < std::string, std::string >::iterator iter = attributes.begin(); iter != attributes.end(); iter++) {
ret += spacing + "at[" + iter->first + "]=" + iter->second + "\n";
}
std::string piece = data.substr(0,10) + " ...";
ret += spacing + "Data: " + piece + "\n";
for (unsigned int i = 0; i < children.size(); i++) {
ret += children[i].toString(sp + 1);
}
return ret;
}
};
class Group {
public:
Group(std::string id, std::string subject, std::string owner)
{
this->id = id;
this->subject = subject;
this->owner = owner;
}
~Group() {
}
std::string id, subject, owner;
std::vector < std::string > participants;
};
class Contact {
public:
Contact()
{
}
Contact(std::string phone, bool myc)
{
this->phone = phone;
this->mycontact = myc;
this->last_seen = 0;
this->subscribed = false;
this->typing = "paused";
this->status = "";
}
std::string phone, name;
std::string presence, typing;
std::string status;
unsigned long long last_seen, last_status;
bool mycontact;
std::string ppprev, pppicture;
bool subscribed;
};
class Message {
public:
Message(const WhatsappConnection * wc, const std::string from, const unsigned long long time, const std::string id, const std::string author)
{
size_t pos = from.find('@');
if (pos != std::string::npos) {
this->from = from.substr(0, pos);
this->server = from.substr(pos + 1);
} else
this->from = from;
this->t = time;
this->wc = const_cast < WhatsappConnection * >(wc);
this->id = id;
this->author = getusername(author);
}
virtual ~ Message()
{
}
std::string from, server, author;
unsigned long long t;
std::string id;
WhatsappConnection *wc;
virtual int type() const = 0;
virtual Message *copy() const = 0;
};
class WhatsappConnection {
friend class ChatMessage;
friend class ImageMessage;
friend class Message;
private:
/* Current dissection classes */
RC4Decoder * in, *out;
unsigned char session_key[20*4]; // V1.4 update
unsigned int frame_seq;
DataBuffer inbuffer, outbuffer;
DataBuffer sslbuffer, sslbuffer_in;
std::string challenge_data, challenge_response;
std::string phone, password;
SessionStatus conn_status;
/* State stuff */
unsigned int msgcounter, iqid;
std::string nickname;
std::string whatsappserver, whatsappservergroup;
std::string mypresence, mymessage;
/* Various account info */
std::string account_type, account_status, account_expiration, account_creation;
/* Groups stuff */
std::map < std::string, Group > groups;
int gq_stat;
int gw1, gw2, gw3;
bool groups_updated;
/* Contacts & msg */
std::map < std::string, Contact > contacts;
std::vector < Message * >recv_messages, recv_messages_delay;
std::vector < std::string > user_changes, user_icons, user_typing;
void processIncomingData();
void processSSLIncomingData();
DataBuffer serialize_tree(Tree * tree, bool crypt = true);
DataBuffer write_tree(Tree * tree);
Tree parse_tree(DataBuffer * data);
/* Upload */
std::vector < t_fileupload > uploadfile_queue;
/* HTTP interface */
std::string generateHttpAuth(std::string nonce);
/* SSL / HTTPS interface */
std::string sslnonce;
int sslstatus; /* 0 none, 1/2 requesting A, 3/4 requesting Q */
/* 5/6 for image upload */
void receiveMessage(const Message & m);
void notifyPresence(std::string from, std::string presence);
void notifyLastSeen(std::string from, std::string seconds);
void addPreviewPicture(std::string from, std::string picture);
void addFullsizePicture(std::string from, std::string picture);
void sendResponse();
void doPong(std::string id, std::string from);
void subscribePresence(std::string user);
void getLast(std::string user);
void queryPreview(std::string user);
void queryFullSize(std::string user);
void gotTyping(std::string who, std::string tstat);
void updateGroups();
void queryStatuses();
void notifyMyMessage();
void notifyMyPresence();
void sendInitial();
void notifyError(ErrorCode err);
DataBuffer generateResponse(std::string from, std::string type, std::string id);
DataBuffer notificationResponse(std::string from, std::string type, std::string id);
std::string generateUploadPOST(t_fileupload * fu);
void processUploadQueue();
void generateSyncARequest();
void generateSyncQRequest();
void updateContactStatuses(std::string json);
void updateFileUpload(std::string);
public:
Tree read_tree(DataBuffer * data);
WhatsappConnection(std::string phone, std::string password, std::string nick);
~WhatsappConnection();
void doLogin(std::string);
void receiveCallback(const char *data, int len);
int sendCallback(char *data, int len);
void sentCallback(int len);
bool hasDataToSend();
void addContacts(std::vector < std::string > clist);
void sendChat(std::string to, std::string message);
void sendGroupChat(std::string to, std::string message);
bool query_chat(std::string & from, std::string & message, std::string & author, unsigned long &t);
bool query_chatimages(std::string & from, std::string & preview, std::string & url, std::string & author, unsigned long &t);
bool query_chatsounds(std::string & from, std::string & url, std::string & author, unsigned long &t);
bool query_chatvideos(std::string & from, std::string & url, std::string & author, unsigned long &t);
bool query_chatlocations(std::string & from, double &lat, double &lng, std::string & prev, std::string & author, unsigned long &t);
int query_next();
bool query_status(std::string & from, int &status);
bool query_icon(std::string & from, std::string & icon, std::string & hash);
bool query_avatar(std::string user, std::string & icon);
bool query_typing(std::string & from, int &status);
void send_avatar(const std::string & avatar);
void account_info(unsigned long long &creation, unsigned long long &freeexp, std::string & status);
int getuserstatus(const std::string & who);
std::string getuserstatusstring(const std::string & who);
unsigned long long getlastseen(const std::string & who);
void manageParticipant(std::string group, std::string participant, std::string command);
void leaveGroup(std::string group);
void notifyTyping(std::string who, int status);
void setMyPresence(std::string s, std::string msg);
std::map < std::string, Group > getGroups();
bool groupsUpdated();
void addGroup(std::string subject);
int loginStatus() const
{
return ((int)conn_status) - 1;
}
int sendImage(std::string to, int w, int h, unsigned int size, const char *fp);
int sendSSLCallback(char *buffer, int maxbytes);
int sentSSLCallback(int bytessent);
void receiveSSLCallback(char *buffer, int bytesrecv);
bool hasSSLDataToSend();
bool closeSSLConnection();
void SSLCloseCallback();
bool hasSSLConnection(std::string & host, int *port);
int uploadProgress(int &rid, int &bs);
int uploadComplete(int);
std::string generateHeaders(std::string auth, int content_length);
};
class ChatMessage:public Message {
public:
ChatMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time, const std::string id, const std::string message, const std::string author):Message(wc, from, time, id, author)
{
this->message = message;
}
int type() const
{
return 0;
}
std::string message; /* Message */
DataBuffer serialize() const
{
Tree tbody("body");
tbody.setData(this->message);
std::string stime = int2str(t);
std::map < std::string, std::string > attrs;
if (server.size())
attrs["to"] = from + "@" + server;
else
attrs["to"] = from + "@" + wc->whatsappserver;
attrs["type"] = "text";
attrs["id"] = stime + "-" + id;
attrs["t"] = stime;
Tree mes("message", attrs);
mes.addChild(tbody);
return wc->serialize_tree(&mes);
}
Message *copy() const
{
return new ChatMessage(wc, from, t, id, message, author);
}
};
class ImageMessage:public Message {
public:
ImageMessage(const WhatsappConnection * wc, const std::string from, const unsigned long long time, const std::string id, const std::string author, const std::string url, const unsigned int width, const unsigned int height, const unsigned int size, const std::string encoding, const std::string hash, const std::string filetype, const std::string preview):Message(wc, from, time, id, author)
{
this->url = url;
this->width = width;
this->height = height;
this->size = size;