forked from unrealircd/unrealircd-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata2.c
1482 lines (1352 loc) · 41.3 KB
/
metadata2.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
/* Copyright (C) 2020-2021, 2023 k4be
** Copyright (C) 2023 Valentin Lorentz
** License: GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
*/
/*** <<<MODULE MANAGER START>>>
module
{
documentation "Implements the draft IRCv3 metadata-2 specification https://github.com/ircv3/ircv3-specifications/pull/501"
troubleshooting "In case of problems, contact val on irc.unrealircd.org.";
min-unrealircd-version "6.*";
post-install-text {
"The module is installed. Now all you need to do is add a loadmodule line:";
"loadmodule \"third/metadata2\";";
"And /REHASH the IRCd.";
"The module may be additionaly configured to change the defaults.";
"See documentation for help.";
"Please note that the implemented feature is still \"Work In Progress\".";
}
}
*** <<<MODULE MANAGER END>>>
*/
#include "unrealircd.h"
/* this should go into include/numeric.h (was there at one point of time) */
#define RPL_WHOISKEYVALUE 760
#define RPL_KEYVALUE 761
#define RPL_KEYNOTSET 766
#define RPL_METADATASUBOK 770
#define RPL_METADATAUNSUBOK 771
#define RPL_METADATASUBS 772
#define RPL_METADATASYNCLATER 774
#define STR_RPL_WHOISKEYVALUE /* 760 */ "%s %s %s :%s"
#define STR_RPL_KEYVALUE /* 761 */ "%s %s %s :%s"
#define STR_RPL_KEYNOTSET /* 766 */ "%s %s :no matching key"
#define STR_RPL_METADATASUBOK /* 770 */ ":%s"
#define STR_RPL_METADATAUNSUBOK /* 771 */ ":%s"
#define STR_RPL_METADATASUBS /* 772 */ ":%s"
#define STR_RPL_METADATASYNCLATER /* 774 */ "%s %s"
#define STR_FAIL_INVALID_TARGET ":%s FAIL METADATA INVALID_TARGET %s :invalid metadata target"
#define STR_FAIL_KEY_INVALID ":%s FAIL METADATA KEY_INVALID %s :invalid key"
#define STR_FAIL_SUBCOMMAND_INVALID ":%s FAIL METADATA SUBCOMMAND_INVALID %s :invalid metadata subcommand"
#define STR_FAIL_KEY_NO_PERMISSION ":%s FAIL METADATA KEY_NO_PERMISSION %s %s :permission denied"
#define STR_FAIL_KEY_NOT_SET ":%s FAIL METADATA KEY_NOT_SET %s :key not set"
#define STR_FAIL_LIMIT_REACHED ":%s FAIL METADATA LIMIT_REACHED %s :metadata limit reached"
#define STR_FAIL_RATE_LIMITED ":%s FAIL METADATA RATE_LIMITED %s %s %s :rate limited"
#define STR_FAIL_TOO_MANY_SUBS ":%s FAIL METADATA TOO_MANY_SUBS %s :too many subscriptions"
#define STR_FAIL_VALUE_INVALID_UTF8 ":%s FAIL METADATA VALUE_INVALID :value contains invalid UTF8"
#define STR_FAIL_VALUE_INVALID_SIZE ":%s FAIL METADATA VALUE_INVALID :value is too long"
/* actual METADATA code */
/* get or set for perms */
#define MODE_SET 0
#define MODE_GET 1
/* TODO: pick a less arbitrary value; to allow it to be as large as possible without
* overflowing IRC line length */
#define MAX_VALUE_BYTES 300
#define MYCONF "metadata2"
#define CHECKPARAMSCNT_OR_DIE(count, return) \
{ \
if (parc < count+1 || BadPtr(parv[count])) \
{ \
sendnumeric(client, ERR_NEEDMOREPARAMS, "METADATA"); \
return; \
} \
}
/* target "*" is always the user issuing the command */
#define PROCESS_TARGET_OR_DIE(target, user, channel, return) \
{ \
char *channame; \
channame = strchr(target, '#'); \
if (channame) \
{ \
channel = find_channel(channame); \
if (!channel) \
{ \
sendto_one(client, NULL, STR_FAIL_INVALID_TARGET, me.name, channame); \
return; \
} \
} else \
{ \
if (strcmp(target, "*")) \
{ \
user = hash_find_nickatserver(target, NULL); \
if (!user) \
{ \
sendto_one(client, NULL, STR_FAIL_INVALID_TARGET, me.name, target); \
return; \
} \
} else \
{ \
user = client; \
} \
} \
}
#define FOR_EACH_KEY(keyindex, parc, parv) while(keyindex++, key = parv[keyindex], (!BadPtr(key) && keyindex < parc))
#define IsSendable(x) (DBufLength(&x->local->sendQ) < 2048)
#define CHECKREGISTERED_OR_DIE(client, return) \
{ \
if (!IsUser(client)) \
{ \
sendnumeric(client, ERR_NOTREGISTERED); \
return; \
} \
}
#define USER_METADATA(client) moddata_client(client, metadataUser).ptr
#define CHANNEL_METADATA(channel) moddata_channel(channel, metadataChannel).ptr
#if defined(__GNUC__)
#define PUSH_IGNORE_ADDRESS \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Waddress\"")
#define POP_IGNORE_ADDRESS \
_Pragma("GCC diagnostic pop")
#else
#define PUSH_IGNORE_ADDRESS
#define POP_IGNORE_ADDRESS
#endif
#define batched(func, client, batch_id, ...) \
{ \
MessageTag *mtags = NULL; \
MessageTag *m; \
PUSH_IGNORE_ADDRESS /* -Waddress warns when batch_id is an array */ \
if (!BadPtr(batch_id)) { \
POP_IGNORE_ADDRESS \
mtags = safe_alloc(sizeof(MessageTag)); \
mtags->name = strdup("batch"); \
mtags->value = strdup(batchid); \
} \
func(client, mtags, ##__VA_ARGS__); \
if (mtags) \
free_message_tags(mtags); \
}
struct metadata {
char *name;
char *value;
struct metadata *next;
};
struct metadata_subscriptions {
char *name;
struct metadata_subscriptions *next;
};
struct metadata_moddata_user {
struct metadata *metadata;
struct metadata_subscriptions *subs;
struct metadata_unsynced *us;
};
struct metadata_unsynced { /* we're listing users (UIDs) that should be synced but were not */
char *id;
char *key;
struct metadata_unsynced *next;
};
CMD_FUNC(cmd_metadata);
CMD_FUNC(cmd_metadata_remote);
CMD_FUNC(cmd_metadata_local);
EVENT(metadata_queue_evt);
const char *metadata_cap_param(Client *client);
char *metadata_isupport_param(void);
int metadata_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs);
int metadata_configposttest(int *errs);
int metadata_configrun(ConfigFile *cf, ConfigEntry *ce, int type);
int metadata_server_sync(Client *client);
int metadata_join(Client *client, Channel *channel, MessageTag *mtags);
int metadata_user_registered(Client *client);
void metadata_user_free(ModData *md);
void metadata_channel_free(ModData *md);
void metadata_free(struct metadata *metadata);
void metadata_free_subs(struct metadata_subscriptions *subs);
int metadata_is_subscribed(Client *user, const char *key);
const char *metadata_get_user_key_value(Client *user, const char *key);
const char *metadata_get_channel_key_value(Channel *channel, const char *key);
void user_metadata_changed(Client *user, const char *key, const char *value, Client *changer);
void channel_metadata_changed(Channel *channel, const char *key, const char *value, Client *changer);
void metadata_free_list(struct metadata *metadata, const char *whose, Client *client);
struct metadata_moddata_user *metadata_prepare_user_moddata(Client *user);
void metadata_set_channel(Channel *channel, const char *key, const char *value, Client *client);
void metadata_set_user(Client *user, const char *key, const char *value, Client *client);
void metadata_send_channel(Channel *channel, const char *key, Client *client, const char *batchid);
void metadata_send_user(Client *user, const char *key, Client *client, const char *batchid);
int metadata_subscribe(const char *key, Client *client, int remove);
void metadata_clear_channel(Channel *channel, Client *client);
void metadata_clear_user(Client *user, Client *client);
void metadata_send_subscribtions(Client *client);
void metadata_send_all_for_channel(Channel *channel, Client *client);
void metadata_send_all_for_user(Client *user, Client *client);
void metadata_sync(Client *client);
int metadata_key_valid(const char *key);
int metadata_check_perms(Client *user, Channel *channel, Client *client, const char *key, int mode);
void metadata_send_change(Client *client, const char *batchid, const char *who, const char *key, const char *value, Client *changer);
int metadata_notify_or_queue(Client *client, const char *who, const char *key, const char *value, Client *changer);
ModDataInfo *metadataUser;
ModDataInfo *metadataChannel;
long CAP_METADATA = 0L;
long CAP_METADATA_NOTIFY = 0L;
struct metadata_settings_s {
int max_user_metadata;
int max_channel_metadata;
int max_subscriptions;
} metadata_settings;
ModuleHeader MOD_HEADER = {
"third/metadata2",
"6.0",
"draft/metadata2 and draft/metadata-notify-2 cap",
"k4be & val",
"unrealircd-6"
};
/*
metadata {
max-user-metadata 10;
max-channel-metadata 10;
max-subscriptions 10;
};
*/
int metadata_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs) {
ConfigEntry *cep;
int errors = 0;
int i;
if (type != CONFIG_MAIN)
return 0;
if (!ce || !ce->name)
return 0;
if (strcmp(ce->name, MYCONF))
return 0;
for (cep = ce->items; cep; cep = cep->next)
{
if (!cep->name)
{
config_error("%s:%i: blank %s item", cep->file->filename, cep->line_number, MYCONF);
errors++;
continue;
}
if (!cep->value || !strlen(cep->value))
{
config_error("%s:%i: %s::%s must be non-empty", cep->file->filename, cep->line_number, MYCONF, cep->name);
errors++;
continue;
}
if (!strcmp(cep->name, "max-user-metadata"))
{
for (i = 0; cep->value[i]; i++)
{
if (!isdigit(cep->value[i]))
{
config_error("%s:%i: %s::%s must be an integer between 1 and 100", cep->file->filename, cep->line_number, MYCONF, cep->name);
errors++;
break;
}
}
if (!errors && (atoi(cep->value) < 1 || atoi(cep->value) > 100))
{
config_error("%s:%i: %s::%s must be an integer between 1 and 100", cep->file->filename, cep->line_number, MYCONF, cep->name);
errors++;
}
continue;
}
if (!strcmp(cep->name, "max-channel-metadata"))
{
for (i = 0; cep->value[i]; i++)
{
if (!isdigit(cep->value[i]))
{
config_error("%s:%i: %s::%s must be an integer between 0 and 100", cep->file->filename, cep->line_number, MYCONF, cep->name);
errors++;
break;
}
}
if (!errors && (atoi(cep->value) < 0 || atoi(cep->value) > 100))
{
config_error("%s:%i: %s::%s must be an integer between 0 and 100", cep->file->filename, cep->line_number, MYCONF, cep->name);
errors++;
}
continue;
}
if (!strcmp(cep->name, "max-subscriptions"))
{
for (i = 0; cep->value[i]; i++)
{
if (!isdigit(cep->value[i]))
{
config_error("%s:%i: %s::%s must be an integer between 1 and 100", cep->file->filename, cep->line_number, MYCONF, cep->name);
errors++;
break;
}
}
if (!errors && (atoi(cep->value) < 0 || atoi(cep->value) > 100))
{
config_error("%s:%i: %s::%s must be an integer between 1 and 100", cep->file->filename, cep->line_number, MYCONF, cep->name);
errors++;
}
continue;
}
config_warn("%s:%i: unknown item %s::%s", cep->file->filename, cep->line_number, MYCONF, cep->name);
}
*errs = errors;
return errors ? -1 : 1;
}
int metadata_configposttest(int *errs) {
/* null the settings to avoid keeping old value if none is set in config */
metadata_settings.max_user_metadata = 0;
metadata_settings.max_channel_metadata = 0;
metadata_settings.max_subscriptions = 0;
return 1;
}
int metadata_configrun(ConfigFile *cf, ConfigEntry *ce, int type) {
ConfigEntry *cep;
if (type != CONFIG_MAIN)
return 0;
if (!ce || !ce->name)
return 0;
if (strcmp(ce->name, MYCONF))
return 0;
for (cep = ce->items; cep; cep = cep->next)
{
if (!cep->name)
continue;
if (!strcmp(cep->name, "max-user-metadata"))
{
metadata_settings.max_user_metadata = atoi(cep->value);
continue;
}
if (!strcmp(cep->name, "max-channel-metadata"))
{
metadata_settings.max_channel_metadata = atoi(cep->value);
continue;
}
if (!strcmp(cep->name, "max-subscriptions"))
{
metadata_settings.max_subscriptions = atoi(cep->value);
continue;
}
}
return 1;
}
MOD_TEST(){
MARK_AS_OFFICIAL_MODULE(modinfo);
HookAdd(modinfo->handle, HOOKTYPE_CONFIGTEST, 0, metadata_configtest);
HookAdd(modinfo->handle, HOOKTYPE_CONFIGPOSTTEST, 0, metadata_configposttest);
return MOD_SUCCESS;
}
MOD_INIT() {
ClientCapabilityInfo cap;
ClientCapability *c;
ModDataInfo mreq;
MARK_AS_OFFICIAL_MODULE(modinfo);
MARK_AS_GLOBAL_MODULE(modinfo);
memset(&cap, 0, sizeof(cap));
cap.name = "draft/metadata-2";
cap.parameter = metadata_cap_param;
c = ClientCapabilityAdd(modinfo->handle, &cap, &CAP_METADATA);
memset(&cap, 0, sizeof(cap));
cap.name = "draft/metadata-notify-2"; /* for irccloud compatibility */
c = ClientCapabilityAdd(modinfo->handle, &cap, &CAP_METADATA_NOTIFY);
CommandAdd(modinfo->handle, "METADATA", cmd_metadata, MAXPARA, CMD_USER|CMD_SERVER|CMD_UNREGISTERED);
memset(&mreq, 0 , sizeof(mreq));
mreq.type = MODDATATYPE_CLIENT;
mreq.name = "metadata_user",
mreq.free = metadata_user_free;
metadataUser = ModDataAdd(modinfo->handle, mreq);
if (!metadataUser)
{
config_error("[%s] Failed to request metadata_user moddata: %s", MOD_HEADER.name, ModuleGetErrorStr(modinfo->handle));
return MOD_FAILED;
}
memset(&mreq, 0 , sizeof(mreq));
mreq.type = MODDATATYPE_CHANNEL;
mreq.name = "metadata_channel",
mreq.free = metadata_channel_free;
metadataChannel = ModDataAdd(modinfo->handle, mreq);
if (!metadataChannel)
{
config_error("[%s] Failed to request metadata_channel moddata: %s", MOD_HEADER.name, ModuleGetErrorStr(modinfo->handle));
return MOD_FAILED;
}
HookAdd(modinfo->handle, HOOKTYPE_SERVER_SYNC, 0, metadata_server_sync);
HookAdd(modinfo->handle, HOOKTYPE_LOCAL_JOIN, -2, metadata_join);
HookAdd(modinfo->handle, HOOKTYPE_REMOTE_JOIN, -2, metadata_join);
HookAdd(modinfo->handle, HOOKTYPE_LOCAL_CONNECT, 0, metadata_user_registered);
HookAdd(modinfo->handle, HOOKTYPE_CONFIGRUN, 0, metadata_configrun);
return MOD_SUCCESS;
}
MOD_LOAD() {
/* setting default values if not configured */
if(metadata_settings.max_user_metadata == 0)
metadata_settings.max_user_metadata = 10;
if(metadata_settings.max_channel_metadata == 0)
metadata_settings.max_channel_metadata = 10;
if(metadata_settings.max_subscriptions == 0)
metadata_settings.max_subscriptions = 10;
EventAdd(modinfo->handle, "metadata_queue", metadata_queue_evt, NULL, 1500, 0);
ISupportAdd(modinfo->handle, "METADATA", metadata_isupport_param());
return MOD_SUCCESS;
}
MOD_UNLOAD() {
return MOD_SUCCESS;
}
const char *metadata_cap_param(Client *client)
{
static char buf[20];
ircsnprintf(buf, sizeof(buf), "max-sub=%d,max-value-bytes=%d", metadata_settings.max_subscriptions, MAX_VALUE_BYTES);
return buf;
}
char *metadata_isupport_param(void)
{
static char buf[20];
ircsnprintf(buf, sizeof(buf), "%d", metadata_settings.max_user_metadata);
return buf;
}
void metadata_free(struct metadata *metadata)
{
safe_free(metadata->name);
safe_free(metadata->value);
safe_free(metadata);
}
void metadata_free_subs(struct metadata_subscriptions *subs)
{
safe_free(subs->name);
safe_free(subs);
}
int metadata_is_subscribed(Client *user, const char *key)
{
struct metadata_moddata_user *moddata = USER_METADATA(user);
if (!moddata)
return 0;
struct metadata_subscriptions *subs;
for (subs = moddata->subs; subs; subs = subs->next)
{
if (!strcasecmp(subs->name, key))
return 1;
}
return 0;
}
const char *metadata_get_user_key_value(Client *user, const char *key)
{
struct metadata_moddata_user *moddata = USER_METADATA(user);
struct metadata *metadata = NULL;
if (!moddata)
return NULL;
for (metadata = moddata->metadata; metadata; metadata = metadata->next)
{
if (!strcasecmp(key, metadata->name))
return metadata->value;
}
return NULL;
}
const char *metadata_get_channel_key_value(Channel *channel, const char *key)
{
struct metadata *metadata;
for (metadata = CHANNEL_METADATA(channel); metadata; metadata = metadata->next)
{
if (!strcasecmp(key, metadata->name))
return metadata->value;
}
return NULL;
}
/* returns 1 if something remains to sync */
int metadata_notify_or_queue(Client *client, const char *who, const char *key, const char *value, Client *changer)
{
int trylater = 0;
if (!who)
{
unreal_log(ULOG_DEBUG, "metadata2", "METADATA_DEBUG", changer, "metadata_notify_or_queue called with null who!");
return 0;
}
if (!key)
{
unreal_log(ULOG_DEBUG, "metadata2", "METADATA_DEBUG", changer, "metadata_notify_or_queue called with null key!");
return 0;
}
if (!client)
{
unreal_log(ULOG_DEBUG, "metadata2", "METADATA_DEBUG", changer, "metadata_notify_or_queue called with null client!");
return 0;
}
struct metadata_moddata_user *moddata = USER_METADATA(client);
if (!moddata)
moddata = metadata_prepare_user_moddata(client);
struct metadata_unsynced **us = &moddata->us;
if (IsSendable(client))
{
metadata_send_change(client, NULL, who, key, value, changer);
} else
{ /* store for the SYNC */
Client *who_client;
const char *uid_or_channel;
if (*who == '#') {
uid_or_channel = who;
} else {
who_client = find_client(who, NULL); /* FIXME the caller should already have this figured out */
if (!who_client) {
unreal_log(ULOG_DEBUG, "metadata", "METADATA_DEBUG", changer, "metadata_notify_or_queue called with nonexistent client!");
return 0; /* shouldn't happen */
}
uid_or_channel = who_client->id;
}
trylater = 1;
while (*us)
us = &(*us)->next; /* find last list element */
*us = safe_alloc(sizeof(struct metadata_unsynced));
(*us)->id = strdup(uid_or_channel);
(*us)->key = strdup(key);
(*us)->next = NULL;
}
return trylater;
}
void metadata_send_change(Client *client, const char *batchid, const char *who, const char *key, const char *value, Client *changer)
{
char *sender = NULL;
if (!key)
{
unreal_log(ULOG_DEBUG, "metadata2", "METADATA_DEBUG", changer, "metadata_send_change called with null key!");
return;
}
if (!who)
{
unreal_log(ULOG_DEBUG, "metadata2", "METADATA_DEBUG", changer, "metadata_send_change called with null who!");
return;
}
if (!client)
{
unreal_log(ULOG_DEBUG, "metadata2", "METADATA_DEBUG", changer, "metadata_send_change called with null client!");
return;
}
if (changer)
{
if (IsServer(client))
sender = changer->id;
else
sender = changer->name;
}
if (!sender)
sender = me.name;
if (changer && IsUser(changer) && MyUser(client))
{
if (!value) {
batched(sendto_one, client, batchid, ":%s!%s@%s METADATA %s %s %s", sender, changer->user->username, GetHost(changer), who, key, "*");
}
else {
batched(sendto_one, client, batchid, ":%s!%s@%s METADATA %s %s %s :%s", sender, changer->user->username, GetHost(changer), who, key, "*", value);
}
} else
{ /* sending S2S (sender is id) or receiving S2S (sender is servername) */
if (!value) {
batched(sendto_one, client, batchid, ":%s METADATA %s %s %s", sender, who, key, "*");
}
else {
batched(sendto_one, client, batchid, ":%s METADATA %s %s %s :%s", sender, who, key, "*", value);
}
}
}
/* used for broadcasting changes to subscribed users and linked servers */
void user_metadata_changed(Client *user, const char *key, const char *value, Client *changer){
Client *acptr;
if (!user || !key)
return; /* sanity check */
list_for_each_entry(acptr, &lclient_list, lclient_node)
{ /* notifications for local subscribers */
if(IsUser(acptr) && IsUser(user) && metadata_is_subscribed(acptr, key) && has_common_channels(user, acptr))
metadata_notify_or_queue(acptr, user->name, key, value, changer);
}
list_for_each_entry(acptr, &server_list, special_node)
{ /* notifications for linked servers, TODO change to sendto_server */
if (acptr == &me)
continue;
metadata_send_change(acptr, NULL, user->name, key, value, changer);
}
}
void channel_metadata_changed(Channel *channel, const char *key, const char *value, Client *changer)
{
Client *acptr;
if (!channel || !key)
return; /* sanity check */
list_for_each_entry(acptr, &lclient_list, lclient_node)
{ /* notifications for local subscribers */
if (metadata_is_subscribed(acptr, key) && IsMember(acptr, channel))
metadata_send_change(acptr, NULL, channel->name, key, value, changer);
}
list_for_each_entry(acptr, &server_list, special_node)
{ /* notifications for linked servers, TODO change to sendto_server */
if(acptr == &me)
continue;
metadata_send_change(acptr, NULL, channel->name, key, value, changer);
}
}
void metadata_free_list(struct metadata *metadata, const char *whose, Client *client)
{
struct metadata *prev_metadata = metadata;
char *name;
while(metadata)
{
name = metadata->name;
safe_free(metadata->value);
metadata = metadata->next;
safe_free(prev_metadata);
prev_metadata = metadata;
if(client && whose && *whose)
{ /* send out the data being removed, unless we're unloading the module */
sendnumeric(client, RPL_KEYVALUE, whose, name, "*", "");
if(*whose == '#')
channel_metadata_changed(find_channel(whose), name, NULL, client);
else
user_metadata_changed(hash_find_nickatserver(whose, NULL), name, NULL, client);
}
safe_free(name);
}
}
void metadata_channel_free(ModData *md)
{
if (!md->ptr)
return; /* was not set */
struct metadata *metadata = md->ptr;
metadata_free_list(metadata, NULL, NULL);
}
void metadata_user_free(ModData *md)
{
struct metadata_moddata_user *moddata = md->ptr;
if (!moddata)
return; /* was not set */
struct metadata_subscriptions *sub = moddata->subs;
struct metadata_subscriptions *prev_sub = sub;
struct metadata_unsynced *us = moddata->us;
struct metadata_unsynced *prev_us;
while (sub)
{
safe_free(sub->name);
sub = sub->next;
safe_free(prev_sub);
prev_sub = sub;
}
struct metadata *metadata = moddata->metadata;
metadata_free_list(metadata, NULL, NULL);
while (us)
{
safe_free(us->id);
safe_free(us->key);
prev_us = us;
us = us->next;
safe_free(prev_us);
}
safe_free(moddata);
}
struct metadata_moddata_user *metadata_prepare_user_moddata(Client *user)
{
USER_METADATA(user) = safe_alloc(sizeof(struct metadata_moddata_user));
struct metadata_moddata_user *ptr = USER_METADATA(user);
ptr->metadata = NULL;
ptr->subs = NULL;
return ptr;
}
void metadata_set_user(Client *user, const char *key, const char *value, Client *client)
{
int changed = 0;
Client *target;
char *target_name;
int removed = 0;
int set = 0;
int count = 0;
if (user)
{
target = user;
target_name = user->name;
} else
{
target = client;
target_name = "*";
}
struct metadata_moddata_user *moddata = USER_METADATA(target);
if (!moddata) /* first call for this user */
moddata = metadata_prepare_user_moddata(target);
struct metadata **metadata = &moddata->metadata;
struct metadata *prev;
if (BadPtr(value) || strlen(value) == 0)
{ /* unset */
value = NULL; /* just to make sure */
removed = 0;
while (*metadata)
{
if (!strcasecmp(key, (*metadata)->name))
break;
metadata = &(*metadata)->next;
}
if (*metadata)
{
prev = *metadata;
*metadata = prev->next;
metadata_free(prev);
removed = 1;
changed = 1;
}
if (!removed)
{
if(client) sendnumeric(client, RPL_KEYNOTSET, target_name, key); // not set so can't remove
return;
}
} else
{ /* set */
while (*metadata)
{
if (!strcasecmp(key, (*metadata)->name))
{
set = 1;
if (strcmp(value, (*metadata)->value))
{
safe_free((*metadata)->value);
(*metadata)->value = strdup(value);
changed = 1;
}
}
metadata = &(*metadata)->next;
count++;
}
if (!set)
{
if (!client || count < metadata_settings.max_user_metadata)
{ /* add new entry for user */
*metadata = safe_alloc(sizeof(struct metadata));
(*metadata)->next = NULL;
(*metadata)->name = strdup(key);
(*metadata)->value = strdup(value);
changed = 1;
} else
{ /* no more allowed */
if (client)
sendto_one(client, NULL, STR_FAIL_LIMIT_REACHED, me.name, target_name);
return;
}
}
}
if (!IsServer(client) && MyConnect(client)) {
/* all OK */
if (value)
sendnumeric(client, RPL_KEYVALUE, target_name, key, "*", value);
else
sendnumeric(client, RPL_KEYNOTSET, target_name, key);
}
if (changed && (client == &me || IsUser(client) || IsServer(client)))
user_metadata_changed(target, key, value, client);
}
void metadata_set_channel(Channel *channel, const char *key, const char *value, Client *client)
{
int changed = 0;
int set = 0;
int count = 0;
struct metadata **metadata = (struct metadata **)&CHANNEL_METADATA(channel);
struct metadata *prev;
if(BadPtr(value) || strlen(value) == 0)
{ /* unset */
value = NULL; /* just to make sure */
int removed = 0;
while (*metadata)
{
if (!strcasecmp(key, (*metadata)->name))
break;
metadata = &(*metadata)->next;
}
if (*metadata)
{
prev = *metadata;
*metadata = prev->next;
metadata_free(prev);
removed = 1;
changed = 1;
}
if (!removed)
{
if (client)
sendto_one(client, NULL, STR_FAIL_KEY_NOT_SET, me.name, key);
return;
}
} else { /* set */
while (*metadata)
{
if (!strcasecmp(key, (*metadata)->name))
{
set = 1;
if (strcmp(value, (*metadata)->value))
{
safe_free((*metadata)->value);
(*metadata)->value = strdup(value);
changed = 1;
}
}
metadata = &(*metadata)->next;
count++;
}
if (!set)
{
if (!client || count < metadata_settings.max_channel_metadata)
{ /* add new entry for user */
*metadata = safe_alloc(sizeof(struct metadata));
(*metadata)->next = NULL;
(*metadata)->name = strdup(key);
(*metadata)->value = strdup(value);
changed = 1;
} else
{ /* no more allowed */
if (client)
sendto_one(client, NULL, STR_FAIL_LIMIT_REACHED, me.name, channel->name);
return;
}
}
}
if (IsUser(client) && MyUser(client)) {
/* all OK */
if (value)
sendnumeric(client, RPL_KEYVALUE, channel->name, key, "*", value);
else
sendnumeric(client, RPL_KEYNOTSET, channel->name, key);
}
if (changed && (IsUser(client) || IsServer(client)))
channel_metadata_changed(channel, key, value, client);
}
int metadata_subscribe(const char *key, Client *client, int remove)
{
struct metadata_moddata_user *moddata = USER_METADATA(client);
struct metadata_subscriptions **subs;
struct metadata_subscriptions *prev_subs;
int found = 0;
int count = 0;
int trylater = 0;
const char *value;
unsigned int hashnum;
Channel *channel;
Client *acptr;
if (!client)
return 0;
if (!moddata) /* first call for this user */
moddata = metadata_prepare_user_moddata(client);
subs = &moddata->subs;
while (*subs)
{
count++;
if (!strcasecmp(key, (*subs)->name))
{
found = 1;
if (remove)
{
prev_subs = *subs;
*subs = prev_subs->next;
metadata_free_subs(prev_subs);
}
break;
}
subs = &(*subs)->next;
}
if (!remove && !found)
{
if (count < metadata_settings.max_subscriptions)
{
*subs = safe_alloc(sizeof(struct metadata_subscriptions));
(*subs)->next = NULL;
(*subs)->name = strdup(key);
} else
{ /* no more allowed */
sendto_one(client, NULL, STR_FAIL_TOO_MANY_SUBS, me.name, key);
return 0;
}
}
if (!remove)
{
sendnumeric(client, RPL_METADATASUBOK, key);
if(!IsUser(client))
return 0; /* unregistered user is not getting any keys yet */
/* we have to send out all subscribed data now */
trylater = 0;
list_for_each_entry(acptr, &client_list, client_node)
{
value = NULL;
if (IsUser(client) && IsUser(acptr) && has_common_channels(acptr, client))
value = metadata_get_user_key_value(acptr, key);
if (value)
trylater |= metadata_notify_or_queue(client, acptr->name, key, value, NULL);
}
for (hashnum = 0; hashnum < CHAN_HASH_TABLE_SIZE; hashnum++)
{
for (channel = hash_get_chan_bucket(hashnum); channel; channel = channel->hnextch)
{
if (IsMember(client, channel))
{
value = metadata_get_channel_key_value(channel, key);
if (value)
trylater |= metadata_notify_or_queue(client, channel->name, key, value, NULL);
}
}
}
if (trylater)
return 1;
} else
{
sendnumeric(client, RPL_METADATAUNSUBOK, key);
}
return 0;
}
void metadata_send_channel(Channel *channel, const char *key, Client *client, const char *batchid)
{
struct metadata *metadata;
int found = 0;
for (metadata = CHANNEL_METADATA(channel); metadata; metadata = metadata->next)
{
if (!strcasecmp(key, metadata->name))
{
found = 1;
batched(sendtaggednumeric, client, batchid, RPL_KEYVALUE, channel->name, key, "*", metadata->value);
break;
}
}
if (!found)
batched(sendtaggednumeric, client, batchid, RPL_KEYNOTSET, channel->name, key);
}
void metadata_send_user(Client *user, const char *key, Client *client, const char *batchid)
{
if (!user)
user = client;
struct metadata_moddata_user *moddata = USER_METADATA(user);
struct metadata *metadata = NULL;