forked from davidgfnet/whatsapp-purple
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwa_purple.c
1438 lines (1201 loc) · 43.6 KB
/
wa_purple.c
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
/**
* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* Whatsapp is a free implementation of the WhatsApp protocol for libpurple.
* The implementation is not 100% complete. Currently supported features include
* message send and receive and profile pictures. In order to be able to login
* you need your WhatsApp password, which is not easy to know. As of Jun. 2012
* the password was either the IMEI or the MAC addres, but latest versions
* of the protocol changed the password so now it's server-generated.
* For more info check WhatsAPI (https://github.com/venomous0x/WhatsAPI)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include "account.h"
#include "accountopt.h"
#include "blist.h"
#include "cmds.h"
#include "conversation.h"
#include "connection.h"
#include "debug.h"
#include "notify.h"
#include "privacy.h"
#include "prpl.h"
#include "roomlist.h"
#include "status.h"
#include "util.h"
#include "version.h"
#include "wa_api.h"
#ifdef _WIN32
#define sys_read wpurple_read
#define sys_write wpurple_write
#else
#include <unistd.h>
#define sys_read read
#define sys_write write
#endif
const char default_resource[] = "Android-2.11.151-443";
#define WHATSAPP_ID "prpl-whatsapp"
static PurplePlugin *_whatsapp_protocol = NULL;
#define WHATSAPP_STATUS_ONLINE "online"
#define WHATSAPP_STATUS_AWAY "away"
#define WHATSAPP_STATUS_OFFLINE "offline"
#define WHATSAPP_DEFAULT_SERVER "c3.whatsapp.net"
#define WHATSAPP_DEFAULT_PORT 443
typedef struct {
unsigned int file_size;
char *to;
void *wconn;
PurpleConnection *gc;
int ref_id;
int done, started;
} wa_file_upload;
typedef struct {
PurpleAccount *account;
int fd; /* File descriptor of the socket */
guint rh, wh; /* Read/write handlers */
int connected; /* Connection status */
void *waAPI; /* Pointer to the C++ class which actually implements the protocol */
int conv_id; /* Combo id counter */
/* HTTPS interface for status query */
guint sslrh, sslwh; /* Read/write handlers */
int sslfd;
PurpleSslConnection *gsc; /* SSL handler */
} whatsapp_connection;
static void waprpl_check_output(PurpleConnection * gc);
static void waprpl_process_incoming_events(PurpleConnection * gc);
static void waprpl_insert_contacts(PurpleConnection * gc);
char *last_seen_text(unsigned long long t);
static void waprpl_chat_join(PurpleConnection * gc, GHashTable * data);
void check_ssl_requests(PurpleAccount * acct);
void waprpl_ssl_cerr_cb(PurpleSslConnection * gsc, PurpleSslErrorType error, gpointer data);
void waprpl_check_ssl_output(PurpleConnection * gc);
void waprpl_ssl_input_cb(gpointer data, gint source, PurpleInputCondition cond);
static void waprpl_set_status(PurpleAccount * acct, PurpleStatus * status);
static void waprpl_check_complete_uploads(PurpleConnection * gc);
unsigned int chatid_to_convo(const char *id)
{
/* Get the chat number to use as combo id */
int unused, cid;
sscanf(id, "%d-%d", &unused, &cid);
return cid;
}
static void waprpl_tooltip_text(PurpleBuddy * buddy, PurpleNotifyUserInfo * info, gboolean full)
{
const char *status;
whatsapp_connection *wconn = purple_connection_get_protocol_data(purple_account_get_connection(purple_buddy_get_account(buddy)));
int st = waAPI_getuserstatus(wconn->waAPI, purple_buddy_get_name(buddy));
if (st < 0)
status = "Unknown";
else if (st == 0)
status = "Unavailable";
else
status = "Available";
unsigned long long lseen = waAPI_getlastseen(wconn->waAPI, purple_buddy_get_name(buddy));
char * statusmsg = waAPI_getuserstatusstring(wconn->waAPI, purple_buddy_get_name(buddy));
purple_notify_user_info_add_pair_plaintext(info, "Status", status);
purple_notify_user_info_add_pair_plaintext(info, "Last seen on WhatsApp", purple_str_seconds_to_string(lseen));
purple_notify_user_info_add_pair_plaintext(info, "Status message", statusmsg);
}
static char *waprpl_status_text(PurpleBuddy * buddy)
{
char *statusmsg;
whatsapp_connection *wconn = purple_connection_get_protocol_data(purple_account_get_connection(purple_buddy_get_account(buddy)));
if (!wconn)
return 0;
statusmsg = waAPI_getuserstatusstring(wconn->waAPI, purple_buddy_get_name(buddy));
if (!statusmsg || strlen(statusmsg) == 0)
return NULL;
return statusmsg;
}
static const char *waprpl_list_icon(PurpleAccount * acct, PurpleBuddy * buddy)
{
return "whatsapp";
}
/* Show the account information received at the login such as expiration,
* creation, etc. */
static void waprpl_show_accountinfo(PurplePluginAction * action)
{
PurpleConnection *gc = (PurpleConnection *) action->context;
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
if (!wconn)
return;
unsigned long long creation, freeexpires;
char *status;
waAPI_accountinfo(wconn->waAPI, &creation, &freeexpires, &status);
time_t creationtime = creation;
time_t freeexpirestime = freeexpires;
char *cr = g_strdup(asctime(localtime(&creationtime)));
char *ex = g_strdup(asctime(localtime(&freeexpirestime)));
char *text = g_strdup_printf("Account status: %s<br />Created on: %s<br />Free expires on: %s\n", status, cr, ex);
purple_notify_formatted(gc, "Account information", "Account information", "", text, NULL, NULL);
g_free(text);
g_free(ex);
g_free(cr);
}
static GList *waprpl_actions(PurplePlugin * plugin, gpointer context)
{
PurplePluginAction *act;
act = purple_plugin_action_new("Show account information ...", waprpl_show_accountinfo);
return g_list_append(NULL, act);
}
static int isgroup(const char *user)
{
return (strchr(user, '-') != NULL);
}
static void waprpl_blist_node_removed(PurpleBlistNode * node)
{
if (!PURPLE_BLIST_NODE_IS_CHAT(node))
return;
PurpleChat *ch = PURPLE_CHAT(node);
PurpleConnection *gc = purple_account_get_connection(purple_chat_get_account(ch));
if (purple_connection_get_prpl(gc) != _whatsapp_protocol)
return;
char *gid = g_hash_table_lookup(purple_chat_get_components(ch), "id");
if (gid == 0)
return; /* Group is not created yet... */
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
waAPI_deletegroup(wconn->waAPI, gid);
waprpl_check_output(purple_account_get_connection(purple_chat_get_account(ch)));
}
static void waprpl_blist_node_added(PurpleBlistNode * node)
{
if (!PURPLE_BLIST_NODE_IS_CHAT(node))
return;
PurpleChat *ch = PURPLE_CHAT(node);
PurpleConnection *gc = purple_account_get_connection(purple_chat_get_account(ch));
if (purple_connection_get_prpl(gc) != _whatsapp_protocol)
return;
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
GHashTable *hasht = purple_chat_get_components(ch);
const char *groupname = g_hash_table_lookup(hasht, "subject");
const char *gid = g_hash_table_lookup(hasht, "id");
if (gid != 0)
return; /* Already created */
purple_debug_info(WHATSAPP_ID, "Creating group %s\n", groupname);
waAPI_creategroup(wconn->waAPI, groupname);
waprpl_check_output(purple_account_get_connection(purple_chat_get_account(ch)));
/* Remove it, it will get added at the moment the chat list gets refreshed */
purple_blist_remove_chat(ch);
}
static PurpleChat *blist_find_chat_by_hasht_cond(PurpleConnection *gc, int (*fn)(GHashTable *hasht, void *data), void *data)
{
PurpleAccount *account = purple_connection_get_account(gc);
PurpleBlistNode *node = purple_blist_get_root();
GHashTable *hasht;
while (node) {
if (PURPLE_BLIST_NODE_IS_CHAT(node)) {
PurpleChat *ch = PURPLE_CHAT(node);
if (purple_chat_get_account(ch) == account) {
hasht = purple_chat_get_components(ch);
if (fn(hasht, data))
return ch;
}
}
node = purple_blist_node_next(node, FALSE);
}
return NULL;
}
static int hasht_cmp_id(GHashTable *hasht, void *data)
{
return !strcmp(g_hash_table_lookup(hasht, "id"), *((char **)data));
}
static int hasht_cmp_convo(GHashTable *hasht, void *data)
{
return (chatid_to_convo(g_hash_table_lookup(hasht, "id")) == *((int *)data));
}
static PurpleChat *blist_find_chat_by_id(PurpleConnection *gc, const char *id)
{
return blist_find_chat_by_hasht_cond(gc, hasht_cmp_id, &id);
}
static PurpleChat *blist_find_chat_by_convo(PurpleConnection *gc, int convo)
{
return blist_find_chat_by_hasht_cond(gc, hasht_cmp_convo, &convo);
}
PurpleConversation *get_open_combo(const char *who, PurpleConnection * gc)
{
PurpleAccount *acc = purple_connection_get_account(gc);
if (isgroup(who)) {
/* Search fot the combo */
PurpleChat *ch = blist_find_chat_by_id(gc, who);
GHashTable *hasht = purple_chat_get_components(ch);
int convo_id = chatid_to_convo(who);
PurpleConversation *convo = purple_find_chat(gc, convo_id);
/* Create a window if it's not open yet */
if (!convo) {
waprpl_chat_join(gc, hasht);
convo = purple_find_chat(gc, convo_id);
}
return convo;
} else {
/* Search for the combo */
PurpleConversation *convo = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, acc);
if (!convo)
convo = purple_conversation_new(PURPLE_CONV_TYPE_IM, acc, who);
return convo;
}
}
static void conv_add_participants(PurpleConversation * conv, const char *part, const char *owner)
{
gchar **plist = g_strsplit(part, ",", 0);
gchar **p;
purple_conv_chat_clear_users(purple_conversation_get_chat_data(conv));
for (p = plist; *p; p++)
purple_conv_chat_add_user(purple_conversation_get_chat_data(conv), *p, "", PURPLE_CBFLAGS_NONE | (!strcmp(owner, *p) ? PURPLE_CBFLAGS_FOUNDER : 0), FALSE);
g_strfreev(plist);
}
static void conv_add_message(PurpleConnection * gc, const char *who, const char *msg, const char *author, unsigned long timestamp)
{
if (isgroup(who)) {
PurpleConversation *convo = get_open_combo(who, gc);
if (convo)
serv_got_chat_in(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(convo)), author, PURPLE_MESSAGE_RECV, msg, timestamp);
} else {
serv_got_im(gc, who, msg, PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_IMAGES, timestamp);
}
}
static char * dbl2str(double num) {
double a,b;
b = modf (num, &a);
return g_strdup_printf("%d.%d", (int)a, (int)(b*100000000.0f));
}
static int str_array_find(gchar **haystack, const gchar *needle)
{
int i;
for (i = 0; haystack[i]; i++) {
if (!strcmp(haystack[i], needle))
return i;
}
return -1;
}
static void query_chat_message(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char *msg, *who, *author;
unsigned long timestamp;
if (waAPI_querychat(wconn->waAPI, &who, &msg, &author, ×tamp)) {
purple_debug_info(WHATSAPP_ID, "Got chat message from %s: %s\n", who, msg);
conv_add_message(gc, who, msg, author, timestamp);
}
}
static void query_chat_image(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char *who, *prev, *url, *author;
int size;
unsigned long timestamp;
if (waAPI_querychatimage(wconn->waAPI, &who, &prev, &size, &url, &author, ×tamp)) {
purple_debug_info(WHATSAPP_ID, "Got image from %s: %s\n", who, url);
int imgid = purple_imgstore_add_with_id(g_memdup(prev, size), size, NULL);
char *msg = g_strdup_printf("<a href=\"%s\"><img id=\"%u\"></a><br/><a href=\"%s\">%s</a>", url, imgid, url, url);
conv_add_message(gc, who, msg, author, timestamp);
g_free(msg);
}
}
static void query_chat_location(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char *who, *prev, *author;
int size;
double lat, lng;
unsigned long timestamp;
if (waAPI_querychatlocation(wconn->waAPI, &who, &prev, &size, &lat, &lng, &author, ×tamp)) {
purple_debug_info(WHATSAPP_ID, "Got geomessage from: %s Coordinates (%f %f)\n", who, (float)lat, (float)lng);
char *msg = g_strdup_printf("<a href=\"http://openstreetmap.org/?lat=%s&lon=%s&zoom=20\">http://openstreetmap.org/?lat=%s&lon=%s&zoom=20</a>", dbl2str(lat), dbl2str(lng), dbl2str(lat), dbl2str(lng));
conv_add_message(gc, who, msg, author, timestamp);
g_free(msg);
}
}
static void query_chat_sound(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char *who, *url, *author;
unsigned long timestamp;
if (waAPI_querychatsound(wconn->waAPI, &who, &url, &author, ×tamp)) {
purple_debug_info(WHATSAPP_ID, "Got chat sound from %s: %s\n", who, url);
char *msg = g_strdup_printf("<a href=\"%s\">%s</a>", url, url);
conv_add_message(gc, who, msg, author, timestamp);
g_free(msg);
}
}
static void query_chat_video(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char *who, *url, *author;
unsigned long timestamp;
if (waAPI_querychatvideo(wconn->waAPI, &who, &url, &author, ×tamp)) {
purple_debug_info(WHATSAPP_ID, "Got chat video from %s: %s\n", who, url);
char *msg = g_strdup_printf("<a href=\"%s\">%s</a>", url, url);
conv_add_message(gc, who, msg, author, timestamp);
g_free(msg);
}
}
static void query_status(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
PurpleAccount *acc = purple_connection_get_account(gc);
char *who;
int status;
while (waAPI_querystatus(wconn->waAPI, &who, &status)) {
if (status == 1) {
purple_prpl_got_user_status(acc, who, "available", "message", "", NULL);
} else {
purple_prpl_got_user_status(acc, who, "unavailable", "message", "", NULL);
}
}
}
static void query_typing(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char *who;
int status;
while (waAPI_querytyping(wconn->waAPI, &who, &status)) {
if (status == 1) {
purple_debug_info(WHATSAPP_ID, "%s is typing\n", who);
serv_got_typing(gc, who, 0, PURPLE_TYPING);
} else {
purple_debug_info(WHATSAPP_ID, "%s is not typing\n", who);
serv_got_typing(gc, who, 0, PURPLE_NOT_TYPING);
serv_got_typing_stopped(gc, who);
}
}
}
static void query_icon(PurpleConnection *gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
PurpleAccount *acc = purple_connection_get_account(gc);
char *who, *icon, *hash;
int len;
while (waAPI_queryicon(wconn->waAPI, &who, &icon, &len, &hash)) {
purple_buddy_icons_set_for_user(acc, who, g_memdup(icon, len), len, hash);
}
}
static void waprpl_process_incoming_events(PurpleConnection * gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
PurpleAccount *acc = purple_connection_get_account(gc);
switch (waAPI_loginstatus(wconn->waAPI)) {
case 0:
purple_connection_update_progress(gc, "Connecting", 0, 4);
break;
case 1:
purple_connection_update_progress(gc, "Sending authorization", 1, 4);
break;
case 2:
purple_connection_update_progress(gc, "Awaiting response", 2, 4);
break;
case 3:
purple_connection_update_progress(gc, "Connection established", 3, 4);
purple_connection_set_state(gc, PURPLE_CONNECTED);
if (!wconn->connected)
waprpl_insert_contacts(gc);
wconn->connected = 1;
PurpleAccount *account = purple_connection_get_account(gc);
PurpleStatus *status = purple_account_get_active_status(account);
waprpl_set_status(account, status);
break;
default:
break;
};
/* Incoming messages. */
for (;;) {
int r = waAPI_querynext(wconn->waAPI);
if (r < 0)
break;
switch (r) {
case 0:
query_chat_message(gc);
break;
case 1:
query_chat_image(gc);
break;
case 2:
query_chat_location(gc);
break;
case 3:
query_chat_sound(gc);
break;
case 4:
query_chat_video(gc);
break;
default:
/* Unsupported message type. */
break;
};
}
/* Status changes, typing notices and profile pictures. */
query_status(gc);
query_typing(gc);
query_icon(gc);
/* Groups update */
if (waAPI_getgroupsupdated(wconn->waAPI)) {
/* Delete/update the chats that are in our list */
PurpleBlistNode *node;
for (node = purple_blist_get_root(); node; node = purple_blist_node_next(node, FALSE)) {
if (!PURPLE_BLIST_NODE_IS_CHAT(node))
continue;
PurpleChat *ch = PURPLE_CHAT(node);
if (purple_chat_get_account(ch) != acc)
continue;
GHashTable *hasht = purple_chat_get_components(ch);
char *grid = g_hash_table_lookup(hasht, "id");
char *glist = waAPI_getgroups(wconn->waAPI);
gchar **gplist = g_strsplit(glist, ",", 0);
if (str_array_find(gplist, grid) >= 0) {
/* The group is in the system, update the fields */
char *sub, *own;
waAPI_getgroupinfo(wconn->waAPI, grid, &sub, &own, 0);
g_hash_table_replace(hasht, g_strdup("subject"), g_strdup(sub));
g_hash_table_replace(hasht, g_strdup("owner"), g_strdup(own));
purple_blist_alias_chat(ch, g_strdup(sub));
} else {
/* The group was deleted */
PurpleChat *del = (PurpleChat *) node;
node = purple_blist_node_next(node, FALSE);
purple_blist_remove_chat(del);
}
g_strfreev(gplist);
}
/* Add new groups */
char *glist = waAPI_getgroups(wconn->waAPI);
gchar **gplist = g_strsplit(glist, ",", 0);
gchar **p;
for (p = gplist; *p; p++) {
gchar *gpid = *p;
PurpleChat *ch = blist_find_chat_by_id(gc, gpid);
if (!ch) {
char *sub, *own;
waAPI_getgroupinfo(wconn->waAPI, gpid, &sub, &own, 0);
purple_debug_info("waprpl", "New group found %s %s\n", gpid, sub);
GHashTable *htable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(htable, g_strdup("subject"), g_strdup(sub));
g_hash_table_insert(htable, g_strdup("id"), g_strdup(gpid));
g_hash_table_insert(htable, g_strdup("owner"), g_strdup(own));
ch = purple_chat_new(acc, sub, htable);
purple_blist_add_chat(ch, NULL, NULL);
}
/* Now update the open conversation that may exist */
char *id = g_hash_table_lookup(purple_chat_get_components(ch), "id");
int prplid = chatid_to_convo(id);
PurpleConversation *conv = purple_find_chat(gc, prplid);
char *subject, *owner, *part;
if (conv && waAPI_getgroupinfo(wconn->waAPI, id, &subject, &owner, &part))
conv_add_participants(conv, part, owner);
}
g_strfreev(gplist);
}
}
static void waprpl_output_cb(gpointer data, gint source, PurpleInputCondition cond)
{
PurpleConnection *gc = data;
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char tempbuff[1024];
int ret;
do {
int datatosend = waAPI_sendcb(wconn->waAPI, tempbuff, sizeof(tempbuff));
if (datatosend == 0)
break;
ret = sys_write(wconn->fd, tempbuff, datatosend);
if (ret > 0) {
waAPI_senddone(wconn->waAPI, ret);
} else if (ret == 0 || (ret < 0 && errno == EAGAIN)) {
/* Check later */
} else {
gchar *tmp = g_strdup_printf("Lost connection with server (out cb): %s", g_strerror(errno));
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp);
g_free(tmp);
break;
}
} while (ret > 0);
/* Check if we need to callback again or not */
waprpl_check_output(gc);
}
/* Try to read some data and push it to the WhatsApp API */
static void waprpl_input_cb(gpointer data, gint source, PurpleInputCondition cond)
{
PurpleConnection *gc = data;
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char tempbuff[1024];
int ret;
do {
ret = sys_read(wconn->fd, tempbuff, sizeof(tempbuff));
if (ret > 0)
waAPI_input(wconn->waAPI, tempbuff, ret);
else if (ret < 0 && errno == EAGAIN)
break;
else if (ret < 0) {
gchar *tmp = g_strdup_printf("Lost connection with server (in cb): %s", g_strerror(errno));
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp);
g_free(tmp);
break;
} else {
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, "Server closed the connection");
}
} while (ret > 0);
waprpl_process_incoming_events(gc);
waprpl_check_output(gc); /* The input data may generate responses! */
}
/* Checks if the WA protocol has data to output and schedules a write handler */
static void waprpl_check_output(PurpleConnection * gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
if (wconn->fd < 0)
return;
if (waAPI_hasoutdata(wconn->waAPI) > 0) {
/* Need to watch for output data (if we are not doing it already) */
if (wconn->wh == 0)
wconn->wh = purple_input_add(wconn->fd, PURPLE_INPUT_WRITE, waprpl_output_cb, gc);
} else {
if (wconn->wh != 0)
purple_input_remove(wconn->wh);
wconn->wh = 0;
}
check_ssl_requests(purple_connection_get_account(gc));
waprpl_check_complete_uploads(gc);
}
static void waprpl_connect_cb(gpointer data, gint source, const gchar * error_message)
{
PurpleConnection *gc = data;
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
PurpleAccount *acct = purple_connection_get_account(gc);
const char *resource = purple_account_get_string(acct, "resource", default_resource);
if (source < 0) {
gchar *tmp = g_strdup_printf("Unable to connect: %s", error_message);
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp);
g_free(tmp);
} else {
wconn->fd = source;
waAPI_login(wconn->waAPI, resource);
wconn->rh = purple_input_add(wconn->fd, PURPLE_INPUT_READ, waprpl_input_cb, gc);
waprpl_check_output(gc);
}
}
static void waprpl_login(PurpleAccount * acct)
{
PurpleConnection *gc = purple_account_get_connection(acct);
purple_debug_info("waprpl", "logging in %s\n", purple_account_get_username(acct));
purple_connection_update_progress(gc, "Connecting", 0, 4);
whatsapp_connection *wconn = g_new0(whatsapp_connection, 1);
wconn->fd = -1;
wconn->sslfd = -1;
wconn->account = acct;
wconn->rh = 0;
wconn->wh = 0;
wconn->connected = 0;
wconn->conv_id = 1;
wconn->gsc = 0;
wconn->sslrh = 0;
wconn->sslwh = 0;
const char *username = purple_account_get_username(acct);
const char *password = purple_account_get_password(acct);
const char *nickname = purple_account_get_string(acct, "nick", "");
wconn->waAPI = waAPI_create(username, password, nickname);
purple_connection_set_protocol_data(gc, wconn);
const char *hostname = purple_account_get_string(acct, "server", WHATSAPP_DEFAULT_SERVER);
int port = purple_account_get_int(acct, "port", WHATSAPP_DEFAULT_PORT);
if (purple_proxy_connect(gc, acct, hostname, port, waprpl_connect_cb, gc) == NULL) {
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, "Unable to connect");
}
static int sig_con = 0;
if (!sig_con) {
sig_con = 1;
purple_signal_connect(purple_blist_get_handle(), "blist-node-removed", _whatsapp_protocol, PURPLE_CALLBACK(waprpl_blist_node_removed), NULL);
purple_signal_connect(purple_blist_get_handle(), "blist-node-added", _whatsapp_protocol, PURPLE_CALLBACK(waprpl_blist_node_added), NULL);
}
}
static void waprpl_close(PurpleConnection * gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
if (wconn->rh)
purple_input_remove(wconn->rh);
if (wconn->wh)
purple_input_remove(wconn->wh);
if (wconn->fd >= 0)
close(wconn->fd);
if (wconn->waAPI)
waAPI_delete(wconn->waAPI);
wconn->waAPI = NULL;
g_free(wconn);
purple_connection_set_protocol_data(gc, 0);
}
static int waprpl_send_im(PurpleConnection * gc, const char *who, const char *message, PurpleMessageFlags flags)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
char *plain;
purple_markup_html_to_xhtml(message, NULL, &plain);
waAPI_sendim(wconn->waAPI, who, plain);
g_free(plain);
waprpl_check_output(gc);
return 1;
}
static int waprpl_send_chat(PurpleConnection * gc, int id, const char *message, PurpleMessageFlags flags)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
PurpleAccount *account = purple_connection_get_account(gc);
PurpleConversation *convo = purple_find_chat(gc, id);
PurpleChat *ch = blist_find_chat_by_convo(gc, id);
GHashTable *hasht = purple_chat_get_components(ch);
char *chat_id = g_hash_table_lookup(hasht, "id");
char *plain;
purple_markup_html_to_xhtml(message, NULL, &plain);
waAPI_sendchat(wconn->waAPI, chat_id, plain);
g_free(plain);
waprpl_check_output(gc);
const char *me = purple_account_get_string(account, "nick", "");
purple_conv_chat_write(PURPLE_CONV_CHAT(convo), me, message, PURPLE_MESSAGE_SEND, time(NULL));
return 1;
}
static void waprpl_add_buddy(PurpleConnection * gc, PurpleBuddy * buddy, PurpleGroup * group)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
const char *name = purple_buddy_get_name(buddy);
waAPI_addcontact(wconn->waAPI, name);
waprpl_check_output(gc);
}
static void waprpl_add_buddies(PurpleConnection * gc, GList * buddies, GList * groups)
{
GList *buddy = buddies;
GList *group = groups;
while (buddy && group) {
waprpl_add_buddy(gc, (PurpleBuddy *) buddy->data, (PurpleGroup *) group->data);
buddy = g_list_next(buddy);
group = g_list_next(group);
}
}
static void waprpl_remove_buddy(PurpleConnection * gc, PurpleBuddy * buddy, PurpleGroup * group)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
const char *name = purple_buddy_get_name(buddy);
waAPI_delcontact(wconn->waAPI, name);
waprpl_check_output(gc);
}
static void waprpl_remove_buddies(PurpleConnection * gc, GList * buddies, GList * groups)
{
GList *buddy = buddies;
GList *group = groups;
while (buddy && group) {
waprpl_remove_buddy(gc, (PurpleBuddy *) buddy->data, (PurpleGroup *) group->data);
buddy = g_list_next(buddy);
group = g_list_next(group);
}
}
static void waprpl_convo_closed(PurpleConnection * gc, const char *who)
{
/* TODO */
}
static void waprpl_add_deny(PurpleConnection * gc, const char *name)
{
/* TODO Do we need to implement deny? Or purple provides it? */
}
static void waprpl_rem_deny(PurpleConnection * gc, const char *name)
{
/* TODO Do we need to implement deny? Or purple provides it? */
}
static unsigned int waprpl_send_typing(PurpleConnection * gc, const char *who, PurpleTypingState typing)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
int status = 0;
if (typing == PURPLE_TYPING)
status = 1;
purple_debug_info(WHATSAPP_ID, "purple: %s typing status: %d\n", who, typing);
waAPI_sendtyping(wconn->waAPI, who, status);
waprpl_check_output(gc);
return 1;
}
static void waprpl_set_buddy_icon(PurpleConnection * gc, PurpleStoredImage * img)
{
/* Send the picture the user has selected! */
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
size_t size = purple_imgstore_get_size(img);
const void *data = purple_imgstore_get_data(img);
waAPI_setavatar(wconn->waAPI, data, size);
waprpl_check_output(gc);
}
static gboolean waprpl_can_receive_file(PurpleConnection * gc, const char *who)
{
return TRUE;
}
static gboolean waprpl_offline_message(const PurpleBuddy * buddy)
{
return FALSE;
}
static GList *waprpl_status_types(PurpleAccount * acct)
{
GList *types = NULL;
PurpleStatusType *type;
type = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE, "available", NULL, TRUE, TRUE, FALSE, "message", "Message", purple_value_new(PURPLE_TYPE_STRING), NULL);
types = g_list_prepend(types, type);
type = purple_status_type_new_with_attrs(PURPLE_STATUS_AWAY, "unavailable", NULL, TRUE, TRUE, FALSE, "message", "Message", purple_value_new(PURPLE_TYPE_STRING), NULL);
types = g_list_prepend(types, type);
type = purple_status_type_new(PURPLE_STATUS_OFFLINE, NULL, NULL, TRUE);
types = g_list_append(types, type);
return g_list_reverse(types);
}
static void waprpl_set_status(PurpleAccount * acct, PurpleStatus * status)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(purple_account_get_connection(acct));
const char *sid = purple_status_get_id(status);
const char *mid = purple_status_get_attr_string(status, "message");
if (mid == 0)
mid = "";
waAPI_setmypresence(wconn->waAPI, sid, mid);
waprpl_check_output(purple_account_get_connection(acct));
}
static void waprpl_get_info(PurpleConnection * gc, const char *username)
{
PurpleNotifyUserInfo *info = purple_notify_user_info_new();
purple_debug_info(WHATSAPP_ID, "Fetching %s's user info for %s\n", username, gc->account->username);
/* Get user status */
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
const char *status_string = waAPI_getuserstatusstring(wconn->waAPI, username);
/* Get user picture (big one) */
char *profile_image = "";
char *icon;
int len;
int res = waAPI_queryavatar(wconn->waAPI, username, &icon, &len);
if (res) {
int iid = purple_imgstore_add_with_id(g_memdup(icon, len), len, NULL);
profile_image = g_strdup_printf("<img id=\"%u\">", iid);
}
purple_notify_user_info_add_pair(info, "Status", status_string);
purple_notify_user_info_add_pair(info, "Profile image", profile_image);
if (res)
g_free(profile_image);
purple_notify_userinfo(gc, username, info, NULL, NULL);
waprpl_check_output(gc);
}
static void waprpl_group_buddy(PurpleConnection * gc, const char *who, const char *old_group, const char *new_group)
{
/* TODO implement local groups */
}
static void waprpl_rename_group(PurpleConnection * gc, const char *old_name, PurpleGroup * group, GList * moved_buddies)
{
/* TODO implement local groups */
}
static void waprpl_insert_contacts(PurpleConnection * gc)
{
whatsapp_connection *wconn = purple_connection_get_protocol_data(gc);
GSList *buddies = purple_find_buddies(purple_connection_get_account(gc), NULL);
GSList *l;
for (l = buddies; l; l = l->next) {
PurpleBuddy *b = l->data;
const char *name = purple_buddy_get_name(b);
waAPI_addcontact(wconn->waAPI, name);
}
waprpl_check_output(gc);
g_slist_free(buddies);
}
/* WA group support as chats */
static GList *waprpl_chat_join_info(PurpleConnection * gc)
{
struct proto_chat_entry *pce;
pce = g_new0(struct proto_chat_entry, 1);
pce->label = "_Subject:";
pce->identifier = "subject";
pce->required = TRUE;
return g_list_append(NULL, pce);
}
static GHashTable *waprpl_chat_info_defaults(PurpleConnection * gc, const char *chat_name)
{
GHashTable *defaults = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
if (chat_name != NULL)
g_hash_table_insert(defaults, g_strdup("subject"), g_strdup(chat_name));