forked from EionRobb/purple-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libdiscord.c
7732 lines (6079 loc) · 236 KB
/
libdiscord.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
/*
* Discord plugin for libpurple
* Copyright (C) 2016 Eion Robb
* Copyright (C) 2017 Alyssa Rosenzweig
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
#include <unistd.h>
#endif
#include <errno.h>
#include <assert.h>
#include <zlib.h>
#ifndef z_const
# define z_const
#endif
#ifdef ENABLE_NLS
# define GETTEXT_PACKAGE "purple-discord"
# include <glib/gi18n-lib.h>
# ifdef _WIN32
# ifdef LOCALEDIR
# unset LOCALEDIR
# endif
# define LOCALEDIR wpurple_locale_dir()
# endif
#else
# define _(a) (a)
# define N_(a) (a)
#endif
#include "glib_compat.h"
#include "json_compat.h"
#include "purple_compat.h"
#include "markdown.h"
// Prevent segfault in libpurple ssl plugins
#define purple_ssl_read(a, b, c) ((a) && (a)->private_data ? purple_ssl_read((a), (b), (c)) : 0)
#define DISCORD_PLUGIN_ID "prpl-eionrobb-discord"
#ifndef DISCORD_PLUGIN_VERSION
#define DISCORD_PLUGIN_VERSION "0.1"
#endif
#define DISCORD_PLUGIN_WEBSITE "https://github.com/EionRobb/purple-discord"
#define DISCORD_USERAGENT_VERSION "92.0.4515.159"
#define DISCORD_USERAGENT "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/" DISCORD_USERAGENT_VERSION " Safari/537.36"
#define DISCORD_BUFFER_DEFAULT_SIZE 40960
#define DISCORD_API_SERVER "discord.com"
#define DISCORD_GATEWAY_SERVER "gateway.discord.gg"
#define DISCORD_GATEWAY_PORT 443
#define DISCORD_GATEWAY_SERVER_PATH "/?encoding=json&v=9"
#define DISCORD_API_VERSION "v9"
#define DISCORD_CDN_SERVER "cdn.discordapp.com"
#define DISCORD_MESSAGE_NORMAL (0)
#define DISCORD_MESSAGE_EDITED (1)
#define DISCORD_MESSAGE_PINNED (2)
#define IGNORE_PRINTS
static GRegex *channel_mentions_regex = NULL;
static GRegex *role_mentions_regex = NULL;
static GRegex *emoji_regex = NULL;
static GRegex *emoji_natural_regex = NULL;
static GRegex *action_star_regex = NULL;
static GRegex *mention_regex = NULL;
static GRegex *natural_mention_regex = NULL;
static GRegex *discord_mention_regex = NULL;
static GRegex *discord_spaced_mention_regex = NULL;
typedef enum {
USER_ONLINE,
USER_IDLE,
USER_OFFLINE,
USER_DND
} DiscordStatus;
typedef enum {
CHANNEL_GUILD_TEXT = 0,
CHANNEL_DM = 1,
CHANNEL_VOICE = 2,
CHANNEL_GROUP_DM = 3,
CHANNEL_GUILD_CATEGORY = 4
} DiscordChannelType;
typedef enum {
NOTIFICATIONS_ALL = 0,
NOTIFICATIONS_MENTIONS = 1,
NOTIFICATIONS_NONE = 2,
NOTIFICATIONS_INHERIT = 3,
} DiscordNotificationLevel;
typedef enum {
GAME_TYPE_PLAYING = 0,
GAME_TYPE_STREAMING = 1,
GAME_TYPE_LISTENING = 2,
GAME_TYPE_WATCHING = 3,
GAME_TYPE_CUSTOM_STATUS = 4,
} DiscordGameType;
typedef struct {
guint64 id;
gchar *name;
int color;
gint64 permissions;
} DiscordGuildRole;
typedef struct {
guint64 id;
gint64 deny;
gint64 allow;
} DiscordPermissionOverride;
typedef struct {
guint64 id;
guint64 guild_id;
guint64 category_id;
gchar *name;
gchar *topic;
DiscordChannelType type;
int position;
guint64 last_message_id;
GHashTable *permission_user_overrides;
GHashTable *permission_role_overrides;
gboolean suppress_everyone;
gboolean muted;
DiscordNotificationLevel notification_level;
/* For group DMs */
GList *recipients;
GHashTable *names; /* Undiscriminated names -> count of that name */
} DiscordChannel;
typedef struct {
guint64 id;
gchar *name;
gchar *icon;
guint64 owner;
GHashTable *roles;
GHashTable *members; /* list of member ids */
GHashTable *nicknames; /* id->nick? */
GHashTable *nicknames_rev; /* reverse */
GHashTable *channels;
int afk_timeout;
const gchar *afk_voice_channel;
GHashTable *emojis;
guint64 system_channel_id; // the primary/general channel
} DiscordGuild;
typedef struct {
guint64 id;
gchar *nick;
gchar *joined_at;
GArray *roles; /* list of ids */
} DiscordGuildMembership;
typedef struct {
guint64 id;
gchar *name;
int discriminator;
DiscordStatus status;
gchar *game;
gchar *avatar;
GHashTable *guild_memberships;
gboolean bot;
gchar *custom_status;
} DiscordUser;
typedef struct {
PurpleAccount *account;
PurpleConnection *pc;
GHashTable *cookie_table;
gchar *session_token;
gchar *channel;
guint64 self_user_id;
gchar *self_username;
guint64 last_message_id;
gint64 last_load_last_message_id;
gchar *token;
gchar *session_id;
gchar *mfa_ticket;
gchar *ack_token;
PurpleSslConnection *websocket;
gboolean websocket_header_received;
gboolean sync_complete;
guchar packet_code;
gchar *frame;
guint64 frame_len;
guint64 frame_len_progress;
gint64 seq; /* incrementing counter */
guint heartbeat_timeout;
guint five_minute_restart;
GHashTable *one_to_ones; /* A store of known room_id's -> username's */
GHashTable *one_to_ones_rev; /* A store of known usernames's -> room_id's */
GHashTable *last_message_id_dm; /* A store of known room_id's -> last_message_id's */
GHashTable *sent_message_ids; /* A store of message id's that we generated from this instance */
GHashTable *result_callbacks; /* Result ID -> Callback function */
GQueue *received_message_queue; /* A store of the last 10 received message id's for de-dup */
GHashTable *new_users;
GHashTable *new_guilds;
GHashTable *group_dms; /* A store of known room_id's -> DiscordChannel's */
gint frames_since_reconnect;
GSList *pending_writes;
gint roomlist_guild_count;
gboolean compress;
z_stream *zstream;
PurpleHttpKeepalivePool *http_keepalive_pool;
} DiscordAccount;
typedef struct {
DiscordAccount *account;
DiscordGuild *guild;
} DiscordAccountGuild;
typedef struct _DiscordImgMsgContext {
gint conv_id;
gchar* from;
gchar* url;
PurpleMessageFlags flags;
time_t timestamp;
} DiscordImgMsgContext;
typedef struct {
PurpleConversation *conv;
gchar *reactor;
gchar *reaction;
} DiscordReaction;
static guint64
to_int(const gchar *id)
{
return id ? g_ascii_strtoull(id, NULL, 10) : 0;
}
static gchar *
from_int(guint64 id)
{
return g_strdup_printf("%" G_GUINT64_FORMAT, id);
}
/** libpurple requires unique chat id's per conversation.
we use a hash function to convert the 64bit conversation id
into a platform-dependent chat id (worst case 32bit).
previously we used g_int64_hash() from glib,
however libpurple requires positive integers */
static gint
discord_chat_hash(guint64 chat_id)
{
return ABS((gint) chat_id);
}
static void discord_free_guild_membership(gpointer data);
static void discord_free_guild_role(gpointer data);
static void discord_free_channel(gpointer data);
static gboolean discord_permission_is_role(JsonObject *json);
/* creating */
static DiscordUser *
discord_new_user(JsonObject *json)
{
DiscordUser *user = g_new0(DiscordUser, 1);
user->id = to_int(json_object_get_string_member(json, "id"));
user->name = g_strdup(json_object_get_string_member(json, "username"));
user->discriminator = to_int(json_object_get_string_member(json, "discriminator"));
user->bot = json_object_get_boolean_member(json, "bot");
user->avatar = g_strdup(json_object_get_string_member(json, "avatar"));
user->guild_memberships = g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, discord_free_guild_membership);
user->status = user->bot ? USER_ONLINE : USER_OFFLINE; /* Is offline the best assumption on a new user? */
return user;
}
static DiscordGuild *
discord_new_guild(JsonObject *json)
{
DiscordGuild *guild = g_new0(DiscordGuild, 1);
guild->id = to_int(json_object_get_string_member(json, "id"));
guild->name = g_strdup(json_object_get_string_member(json, "name"));
guild->icon = g_strdup(json_object_get_string_member(json, "icon"));
guild->owner = to_int(json_object_get_string_member(json, "owner_id"));
guild->roles = g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, discord_free_guild_role);
guild->members = g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, NULL);
guild->nicknames = g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, g_free);
guild->nicknames_rev = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
guild->channels = g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, discord_free_channel);
guild->afk_timeout = json_object_get_int_member(json, "afk_timeout");
guild->afk_voice_channel = g_strdup(json_object_get_string_member(json, "afk_channel_id"));
guild->emojis = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
JsonArray *emojis = json_object_get_array_member(json, "emojis");
for (int i = json_array_get_length(emojis) - 1; i >= 0; i--) {
JsonObject *emoji = json_array_get_object_element(emojis, i);
gchar *id = g_strdup(json_object_get_string_member(emoji, "id"));
gchar *name = g_strdup(json_object_get_string_member(emoji, "name"));
g_hash_table_replace(guild->emojis, name, id);
}
return guild;
}
static DiscordPermissionOverride *
discord_new_permission_override(JsonObject *json)
{
DiscordPermissionOverride *permission = g_new0(DiscordPermissionOverride, 1);
permission->id = to_int(json_object_get_string_member(json, "id"));
if (json_object_get_string_member(json, "deny")) {
// v9 and above
permission->deny = to_int(json_object_get_string_member(json, "deny"));
permission->allow = to_int(json_object_get_string_member(json, "allow"));
} else {
// v6 and below
permission->deny = json_object_get_int_member(json, "deny");
permission->allow = json_object_get_int_member(json, "allow");
}
return permission;
}
static DiscordChannel *
discord_new_channel(JsonObject *json)
{
DiscordChannel *channel = g_new0(DiscordChannel, 1);
channel->id = to_int(json_object_get_string_member(json, "id"));
channel->name = g_strdup(json_object_get_string_member(json, "name"));
channel->topic = g_strdup(json_object_get_string_member(json, "topic"));
channel->position = json_object_get_int_member(json, "position");
channel->type = json_object_get_int_member(json, "type");
channel->last_message_id = to_int(json_object_get_string_member(json, "last_message_id"));
channel->category_id = to_int(json_object_get_string_member(json, "parent_id"));
channel->permission_user_overrides = g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, g_free);
channel->permission_role_overrides = g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, g_free);
channel->recipients = NULL;
return channel;
}
static DiscordGuildMembership *
discord_new_guild_membership(guint64 id, JsonObject *json)
{
DiscordGuildMembership *guild_membership = g_new0(DiscordGuildMembership, 1);
guild_membership->id = id;
guild_membership->nick = g_strdup(json_object_get_string_member(json, "nick"));
guild_membership->joined_at = g_strdup(json_object_get_string_member(json, "joined_at"));
guild_membership->roles = g_array_new(TRUE, TRUE, sizeof(guint64));
return guild_membership;
}
static DiscordGuildRole *
discord_new_guild_role(JsonObject *json)
{
DiscordGuildRole *guild_role = g_new0(DiscordGuildRole, 1);
guild_role->id = to_int(json_object_get_string_member(json, "id"));
guild_role->name = g_strdup(json_object_get_string_member(json, "name"));
guild_role->color = json_object_get_int_member(json, "color");
if (json_object_get_string_member(json, "permissions")) {
const gchar *permissions = json_object_get_string_member(json, "permissions");
guild_role->permissions = to_int(permissions);
} else {
guild_role->permissions = json_object_get_int_member(json, "permissions");
}
return guild_role;
}
/* freeing */
static void
discord_free_guild_role(gpointer data)
{
DiscordGuildRole *guild_role = data;
g_free(guild_role->name);
g_free(guild_role);
}
static void
discord_free_guild_membership(gpointer data)
{
DiscordGuildMembership *guild_membership = data;
g_free(guild_membership->nick);
g_free(guild_membership->joined_at);
g_array_unref(guild_membership->roles);
g_free(guild_membership);
}
static void
discord_free_user(gpointer data)
{
DiscordUser *user = data;
g_free(user->name);
g_free(user->game);
g_free(user->avatar);
g_free(user->custom_status);
g_hash_table_unref(user->guild_memberships);
g_free(user);
}
static void
discord_free_guild(gpointer data)
{
DiscordGuild *guild = data;
g_free(guild->name);
g_free(guild->icon);
g_hash_table_unref(guild->roles);
g_hash_table_unref(guild->members);
g_hash_table_unref(guild->nicknames);
g_hash_table_unref(guild->nicknames_rev);
g_hash_table_unref(guild->channels);
g_hash_table_unref(guild->emojis);
g_free(guild);
}
static void
discord_free_channel(gpointer data)
{
DiscordChannel *channel = data;
g_free(channel->name);
g_free(channel->topic);
g_hash_table_unref(channel->permission_user_overrides);
g_hash_table_unref(channel->permission_role_overrides);
g_list_free_full(channel->recipients, g_free);
g_free(channel);
}
static void
discord_free_image_context(gpointer data)
{
DiscordImgMsgContext *img_context = data;
g_free(img_context->from);
g_free(img_context->url);
g_free(img_context);
}
static void
discord_free_reaction(gpointer data)
{
DiscordReaction *react = data;
g_free(react->reactor);
g_free(react->reaction);
g_free(react);
}
/* updating */
static void
discord_update_status(DiscordUser *user, JsonObject *json)
{
json_object_get_string_member(json, "id");
if (json_object_has_member(json, "status")) {
const gchar *status = json_object_get_string_member(json, "status");
if (purple_strequal("online", status)) {
user->status = USER_ONLINE;
} else if (purple_strequal("idle", status)) {
user->status = USER_IDLE;
} else if (purple_strequal("dnd", status)) {
user->status = USER_DND;
} else {
user->status = USER_OFFLINE; /* All else fails probably offline */
}
}
if (json_object_get_object_member(json, "game") != NULL) {
JsonObject *game = json_object_get_object_member(json, "game");
const gchar *game_id = json_object_get_string_member(game, "id");
g_free(user->game);
g_free(user->custom_status);
if (!purple_strequal(game_id, "custom")) {
const gchar *game_name = json_object_get_string_member(game, "name");
user->game = g_strdup(game_name);
user->custom_status = NULL;
} else {
const gchar *state = json_object_get_string_member(game, "state");
user->custom_status = g_strdup(state);
user->game = NULL;
}
}
}
static DiscordChannel *
discord_add_channel(DiscordGuild *guild, JsonObject *json, guint64 guild_id)
{
DiscordChannel *channel = discord_new_channel(json);
channel->guild_id = guild_id;
g_hash_table_replace_int64(guild->channels, channel->id, channel);
return channel;
}
static DiscordGuildRole *
discord_add_guild_role(DiscordGuild *guild, JsonObject *json)
{
DiscordGuildRole *role = discord_new_guild_role(json);
g_hash_table_replace_int64(guild->roles, role->id, role);
return role;
}
static DiscordPermissionOverride *
discord_add_permission_override(DiscordChannel *channel, JsonObject *json)
{
DiscordPermissionOverride *permission_override = discord_new_permission_override(json);
gboolean is_role = discord_permission_is_role(json);
GHashTable *overrides = is_role ? channel->permission_role_overrides : channel->permission_user_overrides;
g_hash_table_replace_int64(overrides, permission_override->id, permission_override);
return permission_override;
}
/* managing */
static gboolean
discord_permission_is_role(JsonObject *json)
{
if (json_object_get_string_member(json, "type")) {
// v6 and below
return purple_strequal(json_object_get_string_member(json, "type"), "role");
}
// v9 and above
return (json_object_get_int_member(json, "type") == 0);
}
static DiscordUser *
discord_get_user_name(DiscordAccount *da, int discriminator, gchar *name)
{
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, da->new_users);
while (g_hash_table_iter_next(&iter, &key, &value)) {
DiscordUser *user = value;
if (user->discriminator == discriminator && purple_strequal(user->name, name)) {
return value;
}
}
return NULL;
}
static DiscordUser *
discord_get_user_fullname(DiscordAccount *da, const gchar *name)
{
g_return_val_if_fail(name && *name, NULL);
gchar **split_name = g_strsplit(name, "#", 2);
DiscordUser *user = NULL;
if (split_name != NULL) {
if (split_name[0] && split_name[1]) {
user = discord_get_user_name(da, to_int(split_name[1]), split_name[0]);
}
g_strfreev(split_name);
}
return user;
}
static DiscordUser *
discord_get_user(DiscordAccount *da, guint64 id)
{
return g_hash_table_lookup_int64(da->new_users, id);
}
static DiscordUser *
discord_upsert_user(GHashTable *user_table, JsonObject *json)
{
guint64 *key = NULL, user_id = to_int(json_object_get_string_member(json, "id"));
DiscordUser *user = NULL;
if (g_hash_table_lookup_extended_int64(user_table, user_id, (gpointer) &key, (gpointer) &user)) {
return user;
} else {
user = discord_new_user(json);
g_hash_table_replace_int64(user_table, user->id, user);
return user;
}
}
static gchar *
discord_create_fullname(DiscordUser *user)
{
g_return_val_if_fail(user != NULL, NULL);
return g_strdup_printf("%s#%04d", user->name, user->discriminator);
}
static gchar *
discord_create_fullname_from_id(DiscordAccount *da, guint64 id)
{
DiscordUser *user = discord_get_user(da, id);
if (user) {
return discord_create_fullname(user);
}
return NULL;
}
static gchar *discord_create_nickname(DiscordUser *author, DiscordGuild *guild, DiscordChannel *channel);
static gchar *
discord_alloc_nickname(DiscordUser *user, DiscordGuild *guild, const gchar *suggested_nick)
{
const gchar *base_nick = suggested_nick ? suggested_nick : user->name;
gchar *nick = NULL;
if (base_nick == NULL) {
return NULL;
}
DiscordUser *existing = g_hash_table_lookup(guild->nicknames_rev, base_nick);
if (existing && existing->id != user->id) {
/* Ambiguous; try with the discriminator */
nick = g_strdup_printf("%s#%04d", base_nick, user->discriminator);
existing = g_hash_table_lookup(guild->nicknames_rev, nick);
if (existing && existing->id != user->id) {
/* Ambiguous; use the full tag */
g_free(nick);
nick = g_strdup_printf("%s (%s#%04d)", base_nick, user->name, user->discriminator);
}
}
if (!nick) {
nick = g_strdup(base_nick);
}
g_hash_table_replace_int64(guild->nicknames, user->id, g_strdup(nick));
g_hash_table_replace(guild->nicknames_rev, g_strdup(nick), g_memdup2(&user->id, sizeof(user->id)));
return nick;
}
static gchar *
discord_create_nickname_from_id(DiscordAccount *da, DiscordGuild *g, DiscordChannel *channel, guint64 id)
{
DiscordUser *user = discord_get_user(da, id);
if (user) {
return discord_create_nickname(user, g, channel);
}
return NULL;
}
static DiscordGuild *
discord_get_guild(DiscordAccount *da, guint64 id)
{
return g_hash_table_lookup_int64(da->new_guilds, id);
}
static DiscordGuild *
discord_upsert_guild(GHashTable *guild_table, JsonObject *json)
{
guint64 *key = NULL, guild_id = to_int(json_object_get_string_member(json, "id"));
DiscordGuild *guild = NULL;
if (g_hash_table_lookup_extended_int64(guild_table, guild_id, (gpointer) &key, (gpointer) &guild)) {
return guild;
} else {
guild = discord_new_guild(json);
g_hash_table_replace_int64(guild_table, guild->id, guild);
return guild;
}
}
static DiscordChannel *
discord_get_channel_global_int_guild(DiscordAccount *da, guint64 id, DiscordGuild **o_guild)
{
/* Check for group DM first to avoid iterating guilds */
DiscordChannel *group_dm = g_hash_table_lookup_int64(da->group_dms, id);
if(group_dm) return group_dm;
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, da->new_guilds);
while (g_hash_table_iter_next(&iter, &key, &value)) {
DiscordGuild *guild = value;
if (!guild) {
continue;
}
DiscordChannel *channel = g_hash_table_lookup_int64(guild->channels, id);
if (channel) {
if (o_guild) {
*o_guild = guild;
}
return channel;
}
}
return NULL;
}
static DiscordChannel *
discord_get_channel_global_int(DiscordAccount *da, guint64 id)
{
return discord_get_channel_global_int_guild(da, id, NULL);
}
static DiscordChannel *
discord_get_channel_global_name(DiscordAccount *da, const gchar *name)
{
GHashTableIter guild_iter, channel_iter;
gpointer key, value;
g_hash_table_iter_init(&guild_iter, da->new_guilds);
while (g_hash_table_iter_next(&guild_iter, &key, &value)) {
DiscordGuild *guild = value;
g_hash_table_iter_init(&channel_iter, guild->channels);
while (g_hash_table_iter_next(&channel_iter, &key, &value)) {
DiscordChannel *channel = value;
if (purple_strequal(name, channel->name)) {
return channel;
}
}
}
return NULL;
}
static DiscordChannel *
discord_get_channel_global(DiscordAccount *da, const gchar *id)
{
return discord_get_channel_global_int(da, to_int(id));
}
/* debug */
#define discord_print_append(L, B, R, M, D) \
g_string_printf((R), (M), (D)); \
discord_print_append_row((L), (B), (R));
#ifndef IGNORE_PRINTS
static void
discord_print_append_row(int level, GString *buffer, GString *row)
{
for (int i = 0; i < level; i++) {
g_string_prepend_c(row, '\t');
}
g_string_append(buffer, row->str);
g_string_append_c(buffer, '\n');
}
static void
discord_print_permission_override(GString *buffer, GHashTable *permission_overrides, const gchar *type)
{
GHashTableIter permission_override_iter;
GString *row_buffer = g_string_new("");
gpointer key, value;
type = purple_strequal("role", type) ? "Role override count: %d" : "User override count: %d";
discord_print_append(2, buffer, row_buffer, type, g_hash_table_size(permission_overrides));
g_hash_table_iter_init(&permission_override_iter, permission_overrides);
while (g_hash_table_iter_next(&permission_override_iter, &key, &value)) {
DiscordPermissionOverride *permission_override = value;
discord_print_append(3, buffer, row_buffer, "Override id: %" G_GUINT64_FORMAT, permission_override->id);
discord_print_append(4, buffer, row_buffer, "Allow: %" G_GUINT64_FORMAT, permission_override->allow);
discord_print_append(4, buffer, row_buffer, "Deny: %" G_GUINT64_FORMAT, permission_override->deny);
}
}
#endif
static void
discord_print_guilds(GHashTable *guilds)
{
#ifdef IGNORE_PRINTS
return;
#else
GString *buffer = g_string_new("\n");
GString *row_buffer = g_string_new("");
GHashTableIter guild_iter, channel_iter, role_iter;
gpointer key, value;
g_hash_table_iter_init(&guild_iter, guilds);
while (g_hash_table_iter_next(&guild_iter, &key, &value)) {
DiscordGuild *guild = value;
discord_print_append(0, buffer, row_buffer, "Guild id: %" G_GUINT64_FORMAT, guild->id);
discord_print_append(1, buffer, row_buffer, "Name: %s", guild->name);
discord_print_append(1, buffer, row_buffer, "Icon: %s", guild->icon);
discord_print_append(1, buffer, row_buffer, "Owner: %" G_GUINT64_FORMAT, guild->owner);
discord_print_append(1, buffer, row_buffer, "Afk timeout: %d", guild->afk_timeout);
discord_print_append(1, buffer, row_buffer, "Afk channel: %s", guild->afk_voice_channel);
g_hash_table_iter_init(&role_iter, guild->roles);
while (g_hash_table_iter_next(&role_iter, &key, &value)) {
DiscordGuildRole *role = value;
discord_print_append(1, buffer, row_buffer, "Role id: %" G_GUINT64_FORMAT, role->id);
discord_print_append(2, buffer, row_buffer, "Name: %s", role->name);
discord_print_append(2, buffer, row_buffer, "Color: %d", role->color);
discord_print_append(2, buffer, row_buffer, "Permissions: %" G_GUINT64_FORMAT, role->permissions);
}
discord_print_append(1, buffer, row_buffer, "Member count: %d", g_hash_table_size(guild->members));
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, guild->members);
while (g_hash_table_iter_next (&iter, &key, &value)) {
guint64 member_id = *(gint64 *) key;
discord_print_append(3, buffer, row_buffer, "Member id: %" G_GUINT64_FORMAT, member_id);
}
g_hash_table_iter_init(&channel_iter, guild->channels);
while (g_hash_table_iter_next(&channel_iter, &key, &value)) {
DiscordChannel *channel = value;
discord_print_append(1, buffer, row_buffer, "Channel id: %" G_GUINT64_FORMAT, channel->id);
discord_print_append(2, buffer, row_buffer, "Name: %s", channel->name);
discord_print_append(2, buffer, row_buffer, "Topic: %s", channel->topic);
discord_print_append(2, buffer, row_buffer, "Type: %d", channel->type);
discord_print_append(2, buffer, row_buffer, "Position: %d", channel->position);
discord_print_append(2, buffer, row_buffer, "Last message: %" G_GUINT64_FORMAT, channel->last_message_id);
discord_print_permission_override(buffer, channel->permission_role_overrides, "Role");
discord_print_permission_override(buffer, channel->permission_user_overrides, "User");
}
}
purple_debug_info("discord", "%s", buffer->str);
g_string_free(buffer, TRUE);
g_string_free(row_buffer, TRUE);
#endif
}
static void
discord_print_users(GHashTable *users)
{
#ifdef IGNORE_PRINTS
return;
#else
GString *buffer = g_string_new("\n");
GString *row_buffer = g_string_new("");
GHashTableIter user_iter, guild_membership_iter;
gpointer key, value;
g_hash_table_iter_init(&user_iter, users);
while (g_hash_table_iter_next(&user_iter, &key, &value)) {
DiscordUser *user = value;
discord_print_append(0, buffer, row_buffer, "User id: %" G_GUINT64_FORMAT, user->id);
discord_print_append(1, buffer, row_buffer, "Name: %s", user->name);
discord_print_append(1, buffer, row_buffer, "Discriminator: %d", user->discriminator);
discord_print_append(1, buffer, row_buffer, "Game: %s", user->game);
discord_print_append(1, buffer, row_buffer, "Custom Status: %s", user->custom_status);
discord_print_append(1, buffer, row_buffer, "Avatar: %s", user->avatar);
discord_print_append(1, buffer, row_buffer, "Status: %d", user->status);
g_hash_table_iter_init(&guild_membership_iter, user->guild_memberships);
while (g_hash_table_iter_next(&guild_membership_iter, &key, &value)) {
DiscordGuildMembership *guild_membership = value;
discord_print_append(1, buffer, row_buffer, "Guild membership id: %" G_GUINT64_FORMAT, guild_membership->id);
discord_print_append(2, buffer, row_buffer, "Nick: %s", guild_membership->nick);
discord_print_append(2, buffer, row_buffer, "Joined at: %s", guild_membership->joined_at);
discord_print_append(2, buffer, row_buffer, "Role count: %d", guild_membership->roles->len);
for (guint i = 0; i < guild_membership->roles->len; i++) {
guint64 role_id = g_array_index(guild_membership->roles, guint64, i);
discord_print_append(3, buffer, row_buffer, "Role id: %" G_GUINT64_FORMAT, role_id);
}
}
}
purple_debug_info("discord", "%s", buffer->str);
g_string_free(buffer, TRUE);
g_string_free(row_buffer, TRUE);
#endif
}
PurpleChatUserFlags
discord_get_user_flags_from_permissions(DiscordUser *user, guint64 permissions)
{
if (permissions & 0x8) { // Admin
return PURPLE_CHAT_USER_OP;
}
if (permissions & (0x2 | 0x4)) { // Ban or Kick
return PURPLE_CHAT_USER_HALFOP;
}
if (user == NULL) {
return PURPLE_CHAT_USER_NONE;
}
if (user->bot) {
return PURPLE_CHAT_USER_VOICE;
}
return PURPLE_CHAT_USER_NONE;
}
PurpleChatUserFlags
discord_get_user_flags(DiscordAccount *da, DiscordGuild *guild, DiscordUser *user)
{
if (user == NULL) {
return PURPLE_CHAT_USER_NONE;
}
guint64 gid = guild->id;
DiscordGuildMembership *guild_membership = g_hash_table_lookup_int64(user->guild_memberships, gid);
PurpleChatUserFlags best_flag = user->bot ? PURPLE_CHAT_USER_VOICE : PURPLE_CHAT_USER_NONE;
if (guild_membership == NULL) {
return best_flag;
}
for (guint i = 0; i < guild_membership->roles->len; i++) {
guint64 role_id = g_array_index(guild_membership->roles, guint64, i);
DiscordGuildRole *role = g_hash_table_lookup_int64(guild->roles, role_id);
PurpleChatUserFlags this_flag = PURPLE_CHAT_USER_NONE;
if (role != NULL) {
if (role->permissions & 0x8) { /* Admin */
this_flag = PURPLE_CHAT_USER_OP;
} else if (role->permissions & (0x2 | 0x4)) { /* Ban/kick */
this_flag = PURPLE_CHAT_USER_HALFOP;
}
}
if (this_flag > best_flag) {
best_flag = this_flag;
}
}