forked from EionRobb/funyahoo-plusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libyahoo-plusplus.c
2183 lines (1770 loc) · 72 KB
/
libyahoo-plusplus.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
/*
* FunYahoo++ - A replacement Yahoo prpl for Pidgin
* Copyright (C) 2016 Eion Robb
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
// Glib
#include <glib.h>
#if !GLIB_CHECK_VERSION(2, 32, 0)
#define g_hash_table_contains(hash_table, key) g_hash_table_lookup_extended(hash_table, key, NULL, NULL)
#endif /* 2.32.0 */
static gboolean
g_str_insensitive_equal(gconstpointer v1, gconstpointer v2)
{
return (g_ascii_strcasecmp(v1, v2) == 0);
}
static guint
g_str_insensitive_hash(gconstpointer v)
{
guint hash;
gchar *lower_str = g_ascii_strdown(v, -1);
hash = g_str_hash(lower_str);
g_free(lower_str);
return hash;
}
// GNU C libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
#include <unistd.h>
#endif
#include <errno.h>
#include <json-glib/json-glib.h>
// Supress overzealous json-glib 'critical errors'
#define json_object_get_int_member(JSON_OBJECT, MEMBER) \
(json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_int_member(JSON_OBJECT, MEMBER) : 0)
#define json_object_get_string_member(JSON_OBJECT, MEMBER) \
(json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_string_member(JSON_OBJECT, MEMBER) : NULL)
#define json_object_get_array_member(JSON_OBJECT, MEMBER) \
(json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_array_member(JSON_OBJECT, MEMBER) : NULL)
#define json_object_get_object_member(JSON_OBJECT, MEMBER) \
(json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_object_member(JSON_OBJECT, MEMBER) : NULL)
#define json_object_get_boolean_member(JSON_OBJECT, MEMBER) \
(json_object_has_member(JSON_OBJECT, MEMBER) ? json_object_get_boolean_member(JSON_OBJECT, MEMBER) : FALSE)
static void
json_array_foreach_element_reverse (JsonArray *array,
JsonArrayForeach func,
gpointer data)
{
gint i;
g_return_if_fail (array != NULL);
g_return_if_fail (func != NULL);
for (i = json_array_get_length(array) - 1; i >= 0; i--)
{
JsonNode *element_node;
element_node = json_array_get_element(array, i);
(* func) (array, i, element_node, data);
}
}
#include <purple.h>
#if PURPLE_VERSION_CHECK(3, 0, 0)
#include <http.h>
#endif
#ifndef PURPLE_PLUGINS
# define PURPLE_PLUGINS
#endif
#ifndef _
# define _(a) (a)
# define N_(a) (a)
#endif
#define YAHOO_PLUGIN_ID "prpl-eionrobb-funyahoo-plusplus"
#define YAHOO_PLUGIN_VERSION "0.9"
#define YAHOO_PLUGIN_WEBSITE "https://github.com/EionRobb/funyahoo-plusplus"
#define YAHOO_USERAGENT "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
#define YAHOO_BUFFER_DEFAULT_SIZE 40960
#define YAHOO_PRETEND_VERSION "929"
// Purple2 compat functions
#if !PURPLE_VERSION_CHECK(3, 0, 0)
#define purple_connection_error purple_connection_error_reason
#define PURPLE_CONNECTION_CONNECTING PURPLE_CONNECTING
#define PURPLE_CONNECTION_CONNECTED PURPLE_CONNECTED
#define purple_blist_find_group purple_find_group
#define PurpleProtocolChatEntry struct proto_chat_entry
#define PurpleChatConversation PurpleConvChat
#define PurpleIMConversation PurpleConvIm
#define purple_conversations_find_chat_with_account(id, account) \
PURPLE_CONV_CHAT(purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, id, account))
#define purple_chat_conversation_has_left purple_conv_chat_has_left
#define PURPLE_CONVERSATION(chatorim) (chatorim == NULL ? NULL : chatorim->conv)
#define PURPLE_IM_CONVERSATION(conv) PURPLE_CONV_IM(conv)
#define PURPLE_CHAT_CONVERSATION(conv) PURPLE_CONV_CHAT(conv)
#define purple_serv_got_joined_chat(pc, id, name) PURPLE_CONV_CHAT(serv_got_joined_chat(pc, id, name))
#define purple_conversations_find_chat(pc, id) PURPLE_CONV_CHAT(purple_find_chat(pc, id))
#define purple_serv_got_chat_in serv_got_chat_in
#define purple_chat_conversation_add_user purple_conv_chat_add_user
#define purple_chat_conversation_remove_user purple_conv_chat_remove_user
#define PurpleChatUserFlags PurpleConvChatBuddyFlags
#define PURPLE_CHAT_USER_NONE PURPLE_CBFLAGS_NONE
#define PURPLE_CHAT_USER_OP PURPLE_CBFLAGS_OP
#define purple_conversation_get_connection purple_conversation_get_gc
#define purple_chat_conversation_get_id purple_conv_chat_get_id
#define PURPLE_CMD_FLAG_PROTOCOL_ONLY PURPLE_CMD_FLAG_PRPL_ONLY
#define PURPLE_IS_BUDDY PURPLE_BLIST_NODE_IS_BUDDY
#define PURPLE_IS_CHAT PURPLE_BLIST_NODE_IS_CHAT
#define purple_chat_get_name_only purple_chat_get_name
#define purple_blist_find_buddy purple_find_buddy
#define purple_serv_got_alias serv_got_alias
#define purple_account_set_private_alias purple_account_set_alias
#define purple_account_get_private_alias purple_account_get_alias
#define purple_protocol_got_user_status purple_prpl_got_user_status
#define purple_serv_got_im serv_got_im
#define purple_conversations_find_im_with_account(name, account) \
PURPLE_CONV_IM(purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, account))
#define purple_im_conversation_new(account, from) PURPLE_CONV_IM(purple_conversation_new(PURPLE_CONV_TYPE_IM, account, from))
#define PurpleMessage PurpleConvMessage
#define purple_message_set_time(msg, time) ((msg)->when = (time))
#define purple_conversation_write_message(conv, msg) purple_conversation_write(conv, msg->who, msg->what, msg->flags, msg->when)
static inline PurpleMessage *
purple_message_new_outgoing(const gchar *who, const gchar *contents, PurpleMessageFlags flags)
{
PurpleMessage *message = g_new0(PurpleMessage, 1);
message->who = g_strdup(who);
message->what = g_strdup(contents);
message->flags = flags;
message->when = time(NULL);
return message;
}
static inline void
purple_message_destroy(PurpleMessage *message)
{
g_free(message->who);
g_free(message->what);
g_free(message);
}
#define purple_account_privacy_deny_add purple_privacy_deny_add
#define purple_account_privacy_deny_remove purple_privacy_deny_remove
#define PurpleHttpConnection PurpleUtilFetchUrlData
#define purple_buddy_set_name purple_blist_rename_buddy
#else
// Purple3 helper functions
#define purple_conversation_set_data(conv, key, value) g_object_set_data(G_OBJECT(conv), key, value)
#define purple_conversation_get_data(conv, key) g_object_get_data(G_OBJECT(conv), key)
#define purple_message_destroy g_object_unref
#endif
typedef struct {
PurpleAccount *account;
PurpleConnection *pc;
GHashTable *cookie_table;
gchar *session_token;
gchar *channel;
gchar *self_user;
PurpleSslConnection *websocket;
gboolean websocket_header_received;
guchar packet_code;
gchar *frame;
guint64 frame_len;
guint64 frame_len_progress;
gint64 seq;
gint64 ack;
gint64 opid;
gint64 last_event_timestamp;
GHashTable *one_to_ones; // A store of known groupId's->userId's
GHashTable *one_to_ones_rev; // A store of known userId's->groupId's
GHashTable *group_chats; // A store of known multi-user groupId's
GHashTable *sent_message_ids;// A store of message id's that we generated from this instance
GHashTable *media_urls; // MediaId -> URL
} YahooAccount;
typedef void (*YahooProxyCallbackFunc)(YahooAccount *ya, JsonNode *node, gpointer user_data);
typedef struct {
YahooAccount *ya;
YahooProxyCallbackFunc callback;
gpointer user_data;
} YahooProxyConnection;
static gchar *
purple_base32_encode(const guchar *data, gsize len)
{
static const char base32_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
char *out, *rv;
guchar work[5];
g_return_val_if_fail(data != NULL, NULL);
g_return_val_if_fail(len > 0, NULL);
rv = out = g_malloc(((len / 5) + 1) * 8 + 1);
for (; len; len -= MIN(5, len))
{
memset(work, 0, 5);
memcpy(work, data, MIN(5, len));
*out++ = base32_alphabet[work[0] >> 3];
*out++ = base32_alphabet[((work[0] & 0x07) << 2) | (work[1] >> 6)];
*out++ = base32_alphabet[(work[1] >> 1) & 0x1f];
*out++ = base32_alphabet[((work[1] & 0x01) << 4) | (work[2] >> 4)];
*out++ = base32_alphabet[((work[2] & 0x0f) << 1) | (work[3] >> 7)];
*out++ = base32_alphabet[(work[3] >> 2) & 0x1f];
*out++ = base32_alphabet[((work[3] & 0x03) << 3) | (work[4] >> 5)];
*out++ = base32_alphabet[work[4] & 0x1f];
data += MIN(5, len);
}
*out = '\0';
return rv;
}
gchar *
yahoo_string_get_chunk(const gchar *haystack, gsize len, const gchar *start, const gchar *end)
{
const gchar *chunk_start, *chunk_end;
g_return_val_if_fail(haystack && start && end, NULL);
if (len > 0) {
chunk_start = g_strstr_len(haystack, len, start);
} else {
chunk_start = strstr(haystack, start);
}
g_return_val_if_fail(chunk_start, NULL);
chunk_start += strlen(start);
if (len > 0) {
chunk_end = g_strstr_len(chunk_start, len - (chunk_start - haystack), end);
} else {
chunk_end = strstr(chunk_start, end);
}
g_return_val_if_fail(chunk_end, NULL);
return g_strndup(chunk_start, chunk_end - chunk_start);
}
#if PURPLE_VERSION_CHECK(3, 0, 0)
static void
yahoo_update_cookies(YahooAccount *ya, const GList *cookie_headers)
{
const gchar *cookie_start;
const gchar *cookie_end;
gchar *cookie_name;
gchar *cookie_value;
const GList *cur;
for (cur = cookie_headers; cur != NULL; cur = g_list_next(cur))
{
cookie_start = cur->data;
cookie_end = strchr(cookie_start, '=');
cookie_name = g_strndup(cookie_start, cookie_end-cookie_start);
cookie_start = cookie_end + 1;
cookie_end = strchr(cookie_start, ';');
cookie_value= g_strndup(cookie_start, cookie_end-cookie_start);
cookie_start = cookie_end;
g_hash_table_replace(ya->cookie_table, cookie_name, cookie_value);
}
}
#else
static void
yahoo_update_cookies(YahooAccount *ya, const gchar *headers)
{
const gchar *cookie_start;
const gchar *cookie_end;
gchar *cookie_name;
gchar *cookie_value;
int header_len;
g_return_if_fail(headers != NULL);
header_len = strlen(headers);
/* look for the next "Set-Cookie: " */
/* grab the data up until ';' */
cookie_start = headers;
while ((cookie_start = strstr(cookie_start, "\r\nSet-Cookie: ")) && (cookie_start - headers) < header_len)
{
cookie_start += 14;
cookie_end = strchr(cookie_start, '=');
cookie_name = g_strndup(cookie_start, cookie_end-cookie_start);
cookie_start = cookie_end + 1;
cookie_end = strchr(cookie_start, ';');
cookie_value= g_strndup(cookie_start, cookie_end-cookie_start);
cookie_start = cookie_end;
g_hash_table_replace(ya->cookie_table, cookie_name, cookie_value);
}
}
#endif
static void
yahoo_cookie_foreach_cb(gchar *cookie_name, gchar *cookie_value, GString *str)
{
g_string_append_printf(str, "%s=%s;", cookie_name, cookie_value);
}
static gchar *
yahoo_cookies_to_string(YahooAccount *ya)
{
GString *str;
str = g_string_new(NULL);
g_hash_table_foreach(ya->cookie_table, (GHFunc)yahoo_cookie_foreach_cb, str);
return g_string_free(str, FALSE);
}
static void
yahoo_response_callback(PurpleHttpConnection *http_conn,
#if PURPLE_VERSION_CHECK(3, 0, 0)
PurpleHttpResponse *response, gpointer user_data)
{
gsize len;
const gchar *url_text = purple_http_response_get_data(response, &len);
#else
gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message)
{
#endif
const gchar *body;
gsize body_len;
YahooProxyConnection *conn = user_data;
JsonParser *parser = json_parser_new();
#if !PURPLE_VERSION_CHECK(3, 0, 0)
yahoo_update_cookies(conn->ya, url_text);
body = g_strstr_len(url_text, len, "\r\n\r\n");
body_len = len - (body - url_text);
#else
yahoo_update_cookies(conn->ya, purple_http_response_get_headers_by_name(response, "Set-Cookie"));
body = url_text;
body_len = len;
#endif
if (!json_parser_load_from_data(parser, body, body_len, NULL))
{
purple_debug_error("yahoo", "Error parsing response: %s\n", body);
if (conn->callback) {
conn->callback(conn->ya, NULL, conn->user_data);
}
} else {
JsonNode *root = json_parser_get_root(parser);
purple_debug_misc("yahoo", "Got response: %s\n", body);
if (conn->callback) {
conn->callback(conn->ya, root, conn->user_data);
}
}
g_object_unref(parser);
g_free(conn);
}
static void
yahoo_fetch_url(YahooAccount *ya, const gchar *url, const gchar *postdata, YahooProxyCallbackFunc callback, gpointer user_data)
{
PurpleAccount *account;
YahooProxyConnection *conn;
gchar *cookies;
account = ya->account;
if (purple_account_is_disconnected(account)) return;
conn = g_new0(YahooProxyConnection, 1);
conn->ya = ya;
conn->callback = callback;
conn->user_data = user_data;
cookies = yahoo_cookies_to_string(ya);
purple_debug_info("yahoo", "Fetching url %s\n", url);
#if PURPLE_VERSION_CHECK(3, 0, 0)
PurpleHttpRequest *request = purple_http_request_new(url);
purple_http_request_header_set(request, "Accept", "*/*");
purple_http_request_header_set(request, "User-Agent", YAHOO_USERAGENT);
purple_http_request_header_set(request, "Cookie", cookies);
if (postdata) {
purple_debug_info("yahoo", "With postdata %s\n", postdata);
if (postdata[0] == '{') {
purple_http_request_header_set(request, "Content-Type", "application/json");
} else {
purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded");
}
purple_http_request_set_contents(request, postdata, -1);
}
purple_http_request(ya->pc, request, yahoo_response_callback, conn);
purple_http_request_unref(request);
#else
GString *headers;
gchar *host = NULL, *path = NULL, *user = NULL, *password = NULL;
int port;
purple_url_parse(url, &host, &port, &path, &user, &password);
headers = g_string_new(NULL);
//Use the full 'url' until libpurple can handle path's longer than 256 chars
g_string_append_printf(headers, "%s /%s HTTP/1.0\r\n", (postdata ? "POST" : "GET"), path);
//g_string_append_printf(headers, "%s %s HTTP/1.0\r\n", (postdata ? "POST" : "GET"), url);
g_string_append_printf(headers, "Connection: close\r\n");
g_string_append_printf(headers, "Host: %s\r\n", host);
g_string_append_printf(headers, "Accept: */*\r\n");
g_string_append_printf(headers, "User-Agent: " YAHOO_USERAGENT "\r\n");
g_string_append_printf(headers, "Cookie: %s\r\n", cookies);
if (postdata) {
purple_debug_info("yahoo", "With postdata %s\n", postdata);
if (postdata[0] == '{') {
g_string_append(headers, "Content-Type: application/json\r\n");
} else {
g_string_append(headers, "Content-Type: application/x-www-form-urlencoded\r\n");
}
g_string_append_printf(headers, "Content-Length: %" G_GSIZE_FORMAT "\r\n", strlen(postdata));
g_string_append(headers, "\r\n");
g_string_append(headers, postdata);
} else {
g_string_append(headers, "\r\n");
}
g_free(host);
g_free(path);
g_free(user);
g_free(password);
purple_util_fetch_url_request_len_with_account(ya->account, url, FALSE, YAHOO_USERAGENT, TRUE, headers->str, TRUE, 6553500, yahoo_response_callback, conn);
g_string_free(headers, TRUE);
#endif
g_free(cookies);
}
static void
yahoo_process_mutation_op_entity(JsonArray *array, guint index_, JsonNode *element_node, gpointer user_data)
{
YahooAccount *ya = user_data;
JsonArray *change_array = json_node_get_array(element_node);
JsonArray *entity;
gint entity_length;
if (json_array_get_length(change_array) < 3) {
return;
}
entity = json_array_get_array_element(change_array, 0);
entity_length = json_array_get_length(entity);
if (entity_length == 4 &&
purple_strequal(json_array_get_string_element(entity, 0), "Group") &&
purple_strequal(json_array_get_string_element(entity, 2), "items")) {
const gchar *oldItemId = json_array_get_string_element(change_array, 2);
if (g_hash_table_contains(ya->sent_message_ids, oldItemId)) {
const gchar *newItemId = json_array_get_string_element(entity, 3);
g_hash_table_remove(ya->sent_message_ids, oldItemId);
g_hash_table_replace(ya->sent_message_ids, g_strdup(newItemId), NULL);
}
} else if (entity_length == 2 &&
purple_strequal(json_array_get_string_element(entity, 0), "Group")) {
const gchar *oldItemId = json_array_get_string_element(change_array, 2);
if (g_hash_table_contains(ya->group_chats, oldItemId)) {
const gchar *newItemId = json_array_get_string_element(entity, 1);
PurpleChat *chat;
g_hash_table_remove(ya->group_chats, oldItemId);
g_hash_table_replace(ya->group_chats, g_strdup(newItemId), NULL);
while ((chat = purple_blist_find_chat(ya->account, oldItemId))) {
purple_blist_node_set_string(PURPLE_BLIST_NODE(chat), "groupId", newItemId);
g_hash_table_replace(purple_chat_get_components(chat), g_strdup("groupId"), g_strdup(newItemId));
}
} else if (g_hash_table_contains(ya->one_to_ones, oldItemId)) {
const gchar *newItemId = json_array_get_string_element(entity, 1);
gchar *userId = g_strdup(g_hash_table_lookup(ya->one_to_ones, oldItemId));
PurpleBuddy *buddy = purple_blist_find_buddy(ya->account, userId);
if (buddy != NULL) {
purple_blist_node_set_string(PURPLE_BLIST_NODE(buddy), "groupId", newItemId);
}
g_hash_table_remove(ya->one_to_ones, oldItemId);
g_hash_table_remove(ya->one_to_ones_rev, userId);
g_hash_table_replace(ya->one_to_ones, g_strdup(newItemId), g_strdup(userId));
g_hash_table_replace(ya->one_to_ones_rev, g_strdup(userId), g_strdup(newItemId));
g_free(userId);
}
} else if (entity_length == 2 &&
purple_strequal(json_array_get_string_element(entity, 0), "User")) {
const gchar *oldItemId = json_array_get_string_element(change_array, 2);
if (g_hash_table_contains(ya->one_to_ones_rev, oldItemId)) {
const gchar *newItemId = json_array_get_string_element(entity, 1);
gchar *groupId = g_strdup(g_hash_table_lookup(ya->one_to_ones_rev, oldItemId));
PurpleBuddy *bad_buddy;
while ((bad_buddy = purple_blist_find_buddy(ya->account, oldItemId))) {
purple_buddy_set_name(bad_buddy, newItemId);
}
g_hash_table_remove(ya->one_to_ones_rev, oldItemId);
g_hash_table_remove(ya->one_to_ones, groupId);
g_hash_table_replace(ya->one_to_ones_rev, g_strdup(newItemId), g_strdup(groupId));
g_hash_table_replace(ya->one_to_ones, g_strdup(groupId), g_strdup(newItemId));
g_free(groupId);
purple_protocol_got_user_status(ya->account, newItemId, "online", NULL);
}
}
}
static void
yahoo_process_mutation_op(JsonArray *array, guint index_, JsonNode *element_node, gpointer user_data)
{
YahooAccount *ya = user_data;
JsonObject *op = json_node_get_object(element_node);
json_array_foreach_element_reverse(json_object_get_array_member(op, "entities"), yahoo_process_mutation_op_entity, ya);
}
void yahoo_block_user(PurpleConnection *pc, const char *who);
static void yahoo_socket_write_json(YahooAccount *ya, JsonObject *data);
static GHashTable *yahoo_chat_info_defaults(PurpleConnection *pc, const char *chatname);
static void
yahoo_auth_allow(
#if PURPLE_VERSION_CHECK(3, 0, 0)
const gchar *message,
#endif
gpointer userdata)
{
PurpleBuddy *buddy = userdata;
PurpleAccount *account = purple_buddy_get_account(buddy);
PurpleConnection *pc = purple_account_get_connection(account);
YahooAccount *ya = purple_connection_get_protocol_data(pc);
const gchar *userId = purple_buddy_get_name(buddy);
JsonObject *data = json_object_new();
json_object_set_string_member(data, "msg", "AcceptUser");
json_object_set_string_member(data, "userId", userId);
json_object_set_int_member(data, "opId", ya->opid++);
yahoo_socket_write_json(ya, data);
}
static void
yahoo_auth_deny(
#if PURPLE_VERSION_CHECK(3, 0, 0)
const gchar *message,
#endif
gpointer userdata)
{
PurpleBuddy *buddy = userdata;
PurpleAccount *account = purple_buddy_get_account(buddy);
PurpleConnection *pc = purple_account_get_connection(account);
const gchar *userId = purple_buddy_get_name(buddy);
yahoo_block_user(pc, userId);
}
static void
yahoo_process_msg(JsonArray *array, guint index_, JsonNode *element_node, gpointer user_data)
{
JsonObject *response = NULL;
YahooAccount *ya = user_data;
JsonObject *obj = json_node_get_object(element_node);
PurpleGroup *yahoo_group = NULL;
const gchar *msg = json_object_get_string_member(obj, "msg");
gint64 createdTime = json_object_get_int_member(obj, "createdTime");
if (createdTime != 0 && createdTime < ya->last_event_timestamp) {
return;
}
if (purple_strequal(msg, "NewEntity")) {
JsonArray *key_array = json_object_get_array_member(obj, "key");
const gchar *key = json_array_get_string_element(key_array, 0);
const gchar *subkey = json_array_get_length(key_array) > 2 ? json_array_get_string_element(key_array, 2) : NULL;
if (purple_strequal(key, "User")) {
if (subkey == NULL) {
// New buddy
const gchar *userId = json_object_get_string_member(obj, "userId");
const gchar *fullName = json_object_get_string_member(obj, "fullName");
PurpleBuddy *buddy = purple_blist_find_buddy(ya->account, userId);
// Check that we didn't try to add a funky buddy to the buddy list
if (json_object_has_member(obj, "inviteIdentifier")) {
const gchar *inviteIdentifier = json_object_get_string_member(obj, "inviteIdentifier");
PurpleBuddy *bad_buddy;
while ((bad_buddy = purple_blist_find_buddy(ya->account, inviteIdentifier))) {
purple_buddy_set_name(bad_buddy, userId);
}
buddy = purple_blist_find_buddy(ya->account, userId);
}
if (buddy == NULL) {
buddy = purple_buddy_new(ya->account, userId, fullName);
if (yahoo_group == NULL) {
yahoo_group = purple_blist_find_group(_("Yahoo"));
if (!yahoo_group)
{
yahoo_group = purple_group_new(_("Yahoo"));
purple_blist_add_group(yahoo_group, NULL);
}
}
purple_blist_add_buddy(buddy, NULL, yahoo_group, NULL);
}
if (purple_buddy_get_server_alias(buddy) == NULL || !purple_strequal(purple_buddy_get_server_alias(buddy), fullName)) {
purple_serv_got_alias(ya->pc, userId, fullName);
}
if (G_UNLIKELY(purple_strequal(userId, ya->self_user))) {
const gchar *account_alias = purple_account_get_private_alias(ya->account);
if (G_UNLIKELY(!account_alias || !*account_alias)) {
purple_account_set_private_alias(ya->account, fullName);
}
}
purple_protocol_got_user_status(ya->account, userId, "online", NULL);
if (json_object_has_member(obj, "picture")) {
const gchar *mediaId = json_array_get_string_element(json_object_get_array_member(obj, "picture"), 3);
const gchar *media_url = g_hash_table_lookup(ya->media_urls, mediaId);
if (!purple_strequal(purple_buddy_icons_get_checksum_for_user(buddy), media_url)) {
//TODO
//yahoo_fetch_url(ya, url, NULL, yahoo_got_buddy_icon, buddy);
}
}
} else if (purple_strequal(subkey, "media")) {
const gchar *mediaId = json_array_get_string_element(key_array, 3);
const gchar *originalUrl = json_object_get_string_member(obj, "originalUrl");
purple_debug_info("yahoo", "Received media id %s with url %s\n", mediaId, originalUrl);
g_hash_table_replace(ya->media_urls, g_strdup(mediaId), g_strdup(originalUrl));
}
} else if (purple_strequal(key, "Group")) {
if (subkey == NULL) {
// New group
const gchar *groupId = json_object_get_string_member(obj, "groupId");
if (json_object_get_boolean_member(obj, "defaultGroup")) {
const gchar *otherUser = json_array_get_string_element(json_object_get_array_member(obj, "defaultGroupOtherUser"), 1);
PurpleBuddy *buddy = purple_blist_find_buddy(ya->account, otherUser);
// This is a one-to-one IM
g_hash_table_replace(ya->one_to_ones, g_strdup(groupId), g_strdup(otherUser));
g_hash_table_replace(ya->one_to_ones_rev, g_strdup(otherUser), g_strdup(groupId));
if (buddy != NULL) {
purple_blist_node_set_string(PURPLE_BLIST_NODE(buddy), "groupId", groupId);
}
} else {
PurpleChat *chat = purple_blist_find_chat(ya->account, groupId);
const gchar *name = json_object_get_string_member(obj, "name");
// This is a group chat
g_hash_table_replace(ya->group_chats, g_strdup(groupId), NULL);
if (chat == NULL) {
if (yahoo_group == NULL) {
yahoo_group = purple_blist_find_group(_("Yahoo"));
if (!yahoo_group)
{
yahoo_group = purple_group_new(_("Yahoo"));
purple_blist_add_group(yahoo_group, NULL);
}
}
purple_blist_add_chat(purple_chat_new(ya->account, name?name:groupId, yahoo_chat_info_defaults(ya->pc, groupId)), yahoo_group, NULL);
}
}
} else {
if (purple_strequal(json_object_get_string_member(obj, "type"), "post")) {
gchar *message = purple_markup_escape_text(json_object_get_string_member(obj, "message"), -1);
const gchar *user = json_array_get_string_element(json_object_get_array_member(obj, "user"), 1);
const gchar *group = json_array_get_string_element(json_object_get_array_member(obj, "group"), 1);
gint64 timestamp = createdTime / 1000;
PurpleMessageFlags msg_flags = (purple_strequal(user, ya->self_user) ? PURPLE_MESSAGE_SEND : PURPLE_MESSAGE_RECV);
const gchar *itemId = json_object_get_string_member(obj, "itemId");
//check we didn't send this
if (msg_flags == PURPLE_MESSAGE_RECV || !g_hash_table_remove(ya->sent_message_ids, itemId)) {
if (g_hash_table_contains(ya->group_chats, group)) {
//Group chat message
PurpleChatConversation *chatconv = purple_conversations_find_chat_with_account(group, ya->account);
if (chatconv == NULL) {
chatconv = purple_serv_got_joined_chat(ya->pc, g_str_hash(group), group);
purple_conversation_set_data(PURPLE_CONVERSATION(chatconv), "group", g_strdup(group));
}
purple_serv_got_chat_in(ya->pc, g_str_hash(group), user, msg_flags, message, timestamp);
} else {
if (msg_flags == PURPLE_MESSAGE_RECV) {
purple_serv_got_im(ya->pc, user, message, msg_flags, timestamp);
//sometimes we get chat messages before we get the list of groups
// if (!g_hash_table_contains(ya->one_to_ones, group)) {
// g_hash_table_replace(ya->one_to_ones, g_strdup(group), g_strdup(user));
// g_hash_table_replace(ya->one_to_ones_rev, g_strdup(user), g_strdup(group));
// }
} else {
const gchar *other_user = g_hash_table_lookup(ya->one_to_ones, group);
//TODO null check
PurpleIMConversation *imconv = purple_conversations_find_im_with_account(other_user, ya->account);
PurpleMessage *msg = purple_message_new_outgoing(other_user, message, msg_flags);
if (imconv == NULL) {
imconv = purple_im_conversation_new(ya->account, other_user);
}
purple_message_set_time(msg, timestamp);
purple_conversation_write_message(PURPLE_CONVERSATION(imconv), msg);
purple_message_destroy(msg);
}
}
}
g_free(message);
} else if (purple_strequal(subkey, "members")) {
if (purple_strequal(json_object_get_string_member(obj, "invitationState"), "joined")) {
const gchar *message = NULL; //"%s has invited %s to the chat room %s\n"
const gchar *userId = json_array_get_string_element(json_object_get_array_member(obj, "user"), 1);
const gchar *groupId = json_array_get_string_element(json_object_get_array_member(obj, "group"), 1);
//const gchar *invitedBy = json_array_get_string_element(json_object_get_array_member(obj, "invitedBy"), 1);
PurpleChatUserFlags cbflags = json_object_get_boolean_member(obj, "admin") ? PURPLE_CHAT_USER_OP : PURPLE_CHAT_USER_NONE;
PurpleChatConversation *chatconv = purple_conversations_find_chat_with_account(groupId, ya->account);
if (chatconv == NULL) {
if (g_hash_table_contains(ya->group_chats, groupId)) {
chatconv = purple_serv_got_joined_chat(ya->pc, g_str_hash(groupId), groupId);
purple_conversation_set_data(PURPLE_CONVERSATION(chatconv), "groupId", g_strdup(groupId));
}
}
if (chatconv != NULL) {
purple_chat_conversation_add_user(chatconv, userId, message, cbflags, TRUE);
}
}
} else if (purple_strequal(json_object_get_string_member(obj, "type"), "memberRemoved")) {
const gchar *message = NULL; //"%s left the room."
const gchar *groupId = json_array_get_string_element(json_object_get_array_member(obj, "group"), 1);
const gchar *userId = json_array_get_string_element(json_object_get_array_member(obj, "user"), 1);
PurpleChatConversation *chatconv = purple_conversations_find_chat_with_account(groupId, ya->account);
if (chatconv != NULL) {
purple_chat_conversation_remove_user(chatconv, userId, message);
}
} else if (purple_strequal(subkey, "Item") && purple_strequal(json_array_get_string_element(key_array, 4), "media")) {
//TODO split this out for the regular message receive handling code
// Received a message with a photo
JsonArray *media_array = json_object_get_array_member(obj, "media");
const gchar *mediaId = json_array_get_string_element(media_array, 3);
const gchar *media_url = g_hash_table_lookup(ya->media_urls, mediaId);
const gchar *userId = json_array_get_string_element(media_array, 1);
const gchar *groupId = json_array_get_string_element(json_object_get_array_member(obj, "group"), 1);
gint64 timestamp = createdTime / 1000;
PurpleMessageFlags msg_flags = (purple_strequal(userId, ya->self_user) ? PURPLE_MESSAGE_SEND : PURPLE_MESSAGE_RECV);
if (g_hash_table_contains(ya->group_chats, groupId)) {
//Group chat message
PurpleChatConversation *chatconv = purple_conversations_find_chat_with_account(groupId, ya->account);
if (chatconv == NULL) {
chatconv = purple_serv_got_joined_chat(ya->pc, g_str_hash(groupId), groupId);
purple_conversation_set_data(PURPLE_CONVERSATION(chatconv), "groupId", g_strdup(groupId));
}
purple_serv_got_chat_in(ya->pc, g_str_hash(groupId), userId, msg_flags, media_url, timestamp);
} else {
if (msg_flags == PURPLE_MESSAGE_RECV) {
purple_serv_got_im(ya->pc, userId, media_url, msg_flags, timestamp);
} else {
const gchar *other_user = g_hash_table_lookup(ya->one_to_ones, groupId);
//TODO null check
PurpleIMConversation *imconv = purple_conversations_find_im_with_account(other_user, ya->account);
PurpleMessage *msg = purple_message_new_outgoing(other_user, media_url, msg_flags);
if (imconv == NULL) {
imconv = purple_im_conversation_new(ya->account, other_user);
}
purple_message_set_time(msg, timestamp);
purple_conversation_write_message(PURPLE_CONVERSATION(imconv), msg);
purple_message_destroy(msg);
}
}
}
}
} else if (purple_strequal(key, "BlockedUser")) {
const gchar *userId = json_array_get_string_element(key_array, 1);
purple_account_privacy_deny_add(ya->account, userId, TRUE);
} else if (purple_strequal(key, "GroupPrivate")) {
if (json_object_has_member(obj, "unknownInviter")) {
JsonArray *unknownInviter = json_object_get_array_member(obj, "unknownInviter");
const gchar *userId = json_array_get_string_element(unknownInviter, 1);
PurpleBuddy *buddy = purple_blist_find_buddy(ya->account, userId);
const gchar *alias = purple_buddy_get_server_alias(buddy);
if (buddy == NULL) {
buddy = purple_buddy_new(ya->account, userId, NULL);
}
purple_account_request_authorization(ya->account, userId, NULL, alias, NULL, TRUE, yahoo_auth_allow, yahoo_auth_deny, buddy);
}
}
} else if (purple_strequal(msg, "SyncBatch")) {
response = json_object_new();
json_object_set_string_member(response, "msg", "SyncAck");
json_object_set_string_member(response, "pushId", json_object_get_string_member(obj, "pushId"));
} else if (purple_strequal(msg, "MutationResponse")) {
json_array_foreach_element_reverse(json_object_get_array_member(obj, "ops"), yahoo_process_mutation_op, ya);
response = json_object_new();
json_object_set_string_member(response, "msg", "MutationResponseAck");
json_object_set_string_member(response, "ackId", json_object_get_string_member(obj, "ackId"));
} else if (purple_strequal(msg, "DropEntity")) {
JsonArray *key_array = json_object_get_array_member(obj, "key");
const gchar *key = json_array_get_string_element(key_array, 0);
if (purple_strequal(key, "BlockedUser")) {
const gchar *userId = json_array_get_string_element(key_array, 1);
purple_account_privacy_deny_remove(ya->account, userId, TRUE);
}
}
yahoo_socket_write_json(ya, response);
}
static void yahoo_start_socket(YahooAccount *ya);
static void
yahoo_rpc_callback(YahooAccount *ya, JsonNode *node, gpointer user_data)
{
JsonObject *obj = json_node_get_object(node);
const gchar *msg = json_object_get_string_member(obj, "msg");
if (purple_strequal(msg, "SessionOpened")) {
//connected
ya->session_token = g_strdup(json_object_get_string_member(obj, "sessionToken"));
ya->channel = g_strdup(json_object_get_string_member(obj, "channelId"));
ya->self_user = g_strdup(json_object_get_string_member(obj, "userId"));
purple_connection_set_display_name(ya->pc, ya->self_user);
purple_connection_set_state(ya->pc, PURPLE_CONNECTION_CONNECTED);
//process batch
json_array_foreach_element_reverse(json_object_get_array_member(obj, "batch"), yahoo_process_msg, ya);
yahoo_start_socket(ya);
} else if (purple_strequal(msg, "UserMustActivate")) {
purple_notify_uri(ya->pc, "https://messenger.yahoo.com/");
purple_connection_error(ya->pc, PURPLE_CONNECTION_ERROR_OTHER_ERROR, _("Please login to the Yahoo messenger website once, to continue"));
} else if (purple_strequal(msg, "InvalidCredentials")) {
purple_connection_error(ya->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, "Session expired");
} else {
purple_connection_error(ya->pc, PURPLE_CONNECTION_ERROR_OTHER_ERROR, json_object_get_string_member(obj, "reason"));
}
}
static void
yahoo_auth_callback(YahooAccount *ya, JsonNode *node, gpointer user_data)
{
JsonObject *obj = json_node_get_object(node);
if (purple_strequal(json_object_get_string_member(obj, "status"), "error")) {
if (purple_strequal(json_object_get_string_member(obj, "code"), "1212")) {
purple_connection_error(ya->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, json_object_get_string_member(obj, "message"));
} else {
purple_connection_error(ya->pc, PURPLE_CONNECTION_ERROR_OTHER_ERROR, json_object_get_string_member(obj, "message"));
}
} else {
const gchar *rpcdata = "{\"msg\":\"OpenSession\",\"device\":{\"kind\":\"mobile\"},\"auth\":{\"provider\":\"signin\"},\"version\":{\"platform\":\"web\",\"app\":\"iris/dogfood\",\"appVersion\":" YAHOO_PRETEND_VERSION "},\"batch\":[]}";
purple_connection_set_state(ya->pc, PURPLE_CONNECTION_CONNECTING);
yahoo_fetch_url(ya, "https://prod.iris.yahoo.com/prod/rpc?wait=1&v=1", rpcdata, yahoo_rpc_callback, NULL);
}
}
static void
yahoo_restart_channel(YahooAccount *ya)
{
gchar *rpcdata = g_strdup_printf("{\"msg\":\"ReopenSession\",\"sessionToken\":\"%s\",\"batch\":[],\"version\":{\"platform\":\"web\",\"app\":\"iris/dogfood\",\"appVersion\":" YAHOO_PRETEND_VERSION "}}", ya->session_token);
purple_connection_set_state(ya->pc, PURPLE_CONNECTION_CONNECTING);
yahoo_fetch_url(ya, "https://prod.iris.yahoo.com/prod/rpc?wait=1&v=1", rpcdata, yahoo_rpc_callback, NULL);
g_free(rpcdata);
}
static void
yahoo_preauth_callback(PurpleHttpConnection *http_conn,
#if PURPLE_VERSION_CHECK(3, 0, 0)
PurpleHttpResponse *response, gpointer user_data)
{
gsize len;
const gchar *url_text = purple_http_response_get_data(response, &len);
#else
gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message)
{
#endif
YahooAccount *ya = user_data;
GString *postdata = g_string_new("");
gchar *crumb = yahoo_string_get_chunk(url_text, len, "<input name=\"_crumb\" type=\"hidden\" value=\"", "\"");
#if PURPLE_VERSION_CHECK(3, 0, 0)
yahoo_update_cookies(ya, purple_http_response_get_headers_by_name(response, "Set-Cookie"));
#else
yahoo_update_cookies(ya, url_text);
#endif
if (g_hash_table_lookup(ya->cookie_table, "B") == NULL) {
purple_connection_error(ya->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, "Couldn't get login cookies");
return;
}
g_string_append_printf(postdata, "username=%s&", purple_url_encode(purple_account_get_username(ya->account)));
g_string_append_printf(postdata, "passwd=%s&", purple_url_encode(purple_connection_get_password(ya->pc)));
g_string_append_printf(postdata, "_crumb=%s&", purple_url_encode(crumb));
g_string_append(postdata, "countrycode=1&");
g_string_append(postdata, "signin=&");
g_string_append(postdata, "otp_channel=&");
g_string_append(postdata, ".persistent=y&");
g_string_append(postdata, "_format=json&");
g_string_append(postdata, "_seqid=1&");
purple_connection_set_state(ya->pc, PURPLE_CONNECTION_CONNECTING);
yahoo_fetch_url(ya, "https://login.yahoo.com/?.pd=&.src=messenger&.done=https%3A%2F%2Fmessenger.yahoo.com%2F", postdata->str, yahoo_auth_callback, NULL);