-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-services.c
3787 lines (3381 loc) · 106 KB
/
no-services.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
/*
Licence: GPLv3
Copyright Ⓒ 2024 Valerie Pond
*/
#define NOSERVICES_VERSION "1.3.1.3"
/*** <<<MODULE MANAGER START>>>
module
{
documentation "https://github.com/DalekIRC/no-services/wiki";
troubleshooting "In case of problems, documentation or e-mail me at v.a.pond@outlook.com";
min-unrealircd-version "6.1.3";
max-unrealircd-version "6.*";
post-install-text {
"The module is installed. Now all you need to do is add a loadmodule line:";
"loadmodule \"third/no-services\";";
"And /REHASH the IRCd.";
"The module does not need any other configuration.";
}
}
*** <<<MODULE MANAGER END>>>
*/
#include "unrealircd.h"
/* Defines */
#define END_OF_HELP NULL
#define NO_SERVICES_CONF "no-services"
#define REGCAP_NAME "draft/account-registration"
#define CERTFP_DEL 0
#define CERTFP_ADD 1
#define CERTFP_LIST 2
#define SR_FAIL 0
#define SR_WARN 1
#define SR_NOTE 2
#define MAX_BUDDYLIST_SIZE 100
// Runs both when a fully-connected user authenticates or an authenticated user fully-connects lol
#define HOOKTYPE_NOSERV_CONNECT_AND_LOGIN 159
/** Called when a local user quits or otherwise disconnects (function prototype for HOOKTYPE_PRE_LOCAL_QUIT).
* @param client The client
* @param result A JSON object about the user
*
* {
* "id": 29,
* "account_name": "bob",
* "first_name": null,
* "last_name": null,
* "password": "$argon2id$v=19$m=6144,t=2,p=2$P5bUJuJSarijl8abOiV1Lw$GIbxA06zp3Kk1pjWjcSc8E0MwgfBUAuO5MjfhYpksUI",
* "email": "bob@example.cn",
* "activated": null,
* "last_login": null,
* "registered_at": "2023-12-10 23:38:06",
* "roles": null,
* "meta": {
* "0": {
* "id": 11,
* "user_id": "bob",
* "meta_name": "ajoin",
* "meta_value": "#services"
* },
* "1": {
* "id": 12,
* "user_id": "bob",
* "meta_name": "ajoin",
* "meta_value": "#opers"
* }
* }
*
* For an actual example, see `void do_ajoin(){...}`
*/
void hooktype_noserv_connect_and_login(Client *client, json_t *result);
// sasl stuff
#define SASL_TYPE_NONE 0
#define SASL_TYPE_PLAIN 1
#define SASL_TYPE_EXTERNAL 2
#define MTAG_ISO_LANG "+valware.uk/iso-lang"
#define MTAG_RESPONDER "valware.uk/responder"
#define SetLanguage(x, y) do { moddata_client_set(x, "language", y); } while (0)
#define GetLanguage(x) moddata_client_get(x, "language")
#define GetSaslType(x) (moddata_client(x, sasl_md).i)
#define SetSaslType(x, y) do { moddata_client(x, sasl_md).i = y; } while (0)
#define DelSaslType(x) do { moddata_client(x, sasl_md).i = SASL_TYPE_NONE; } while (0)
ModDataInfo *sasl_md;
// nick enforcement
#define UserRequiresLogin(x) (moddata_client(x, need_login).i)
#define RequireLogin(x) do { moddata_client(x, need_login).i = 1; } while (0)
#define UnrequireLogin(x) do { moddata_client(x, need_login).i = 0; } while (0)
ModDataInfo *need_login;
// password for login/sasl
#define GetPassword(x) moddata_client_get(x, "login_cred")
#define SetPassword(x, y) do { moddata_client(x, login_cred).str = strdup(y); } while (0)
#define UnsetPassword(x) do { moddata_client_set(x, "login_cred", NULL); } while (0)
ModDataInfo *login_cred;
// nick drop key
#define WantsToDrop(x) do { moddata_client_set(x, need_drop, "1"); } while (0)
#define NoLongerWantsToDrop(x) do { moddata_client(x, need_drop, "0"); } while (0)
ModDataInfo *need_drop;
// nick drop key
#define GetDropKey(x) (moddata_client(x, drop_key_md).i)
#define SetDropKey(x, y) do { moddata_client(x, drop_key_md).i = y; } while(0)
#define UnsetDropKey(x) do { moddata_client(x, drop_key_md).i = 0; } while(0)
ModDataInfo *drop_key_md;
void setcfg(void);
void freecfg(void);
int noservices_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs);
int noservices_configrun(ConfigFile *cf, ConfigEntry *ce, int type);
void connect_query_user(Client *client);
void account_enforce_check(Client *client);
long CAP_ACCOUNTREGISTRATION = 0L;
long CAP_SASL_OVR = 0L;
char *construct_url(const char *base_url, const char *extra_params);
const char *accreg_capability_parameter(Client *client);
int accreg_capability_visible(Client *client);
void register_account(OutgoingWebRequest *request, OutgoingWebResponse *response);
void register_channel_callback(OutgoingWebRequest *request, OutgoingWebResponse *response);
void ns_account_login(OutgoingWebRequest *request, OutgoingWebResponse *response);
void ajoin_callback(OutgoingWebRequest *request, OutgoingWebResponse *response);
void connect_query_user_response(OutgoingWebRequest *request, OutgoingWebResponse *response);
void certfp_callback(OutgoingWebRequest *request, OutgoingWebResponse *response);
void nick_account_enforce(OutgoingWebRequest *request, OutgoingWebResponse *response);
void setpassword_callback(OutgoingWebRequest *request, OutgoingWebResponse *response);
void drop_callback(OutgoingWebRequest *request, OutgoingWebResponse *response);
void ns_account_identify(OutgoingWebRequest *request, OutgoingWebResponse *response);
void translate_callback(OutgoingWebRequest *request, OutgoingWebResponse *response);
// draft/account-registration= parameter MD
void regkeylist_free(ModData *m);
const char *regkeylist_serialize(ModData *m);
void regkeylist_unserialize(const char *str, ModData *m);
// If someone wants to drop their nick
void wtdrop_free(ModData *m);
const char *wtdrop_serialize(ModData *m);
void wtdrop_unserialize(const char *str, ModData *m);
// A key if someone wants to drop their account
void dropkey_free(ModData *m);
const char *dropkey_serialize(ModData *m);
void dropkey_unserialize(const char *str, ModData *m);
// Who's SASLing how
void sat_free(ModData *m);
const char *sat_serialize(ModData *m);
void sat_unserialize(const char *str, ModData *m);
// Who's SASLing how
void need_login_free(ModData *m);
const char *need_login_serialize(ModData *m);
void need_login_unserialize(const char *str, ModData *m);
void login_cred_free(ModData *m);
const char *login_cred_serialize(ModData *m);
void login_cred_unserialize(const char *str, ModData *m);
int noservices_hook_local_connect(Client *client);
int noservices_hook_post_local_nickchange(Client *client, MessageTag *mtags, const char *oldnick);
int auto_translate(Client *from, Client *to, MessageTag *mtags, const char *text, SendType sendtype);
void do_ajoin(Client *client, json_t *result);
void do_account_drop(const char *account, const char *callback_uid);
int sasl_capability_visible(Client *client);
const char *sasl_capability_parameter(Client *client);
CMD_OVERRIDE_FUNC(cmd_authenticate_ovr);
CMD_OVERRIDE_FUNC(cmd_nick_ovr);
CMD_OVERRIDE_FUNC(cmd_privmsg_ovr);
CMD_FUNC(cmd_register);
CMD_FUNC(cmd_cregister);
CMD_FUNC(cmd_ajoin);
CMD_FUNC(cmd_identify);
CMD_FUNC(cmd_logout);
CMD_FUNC(cmd_salogout);
CMD_FUNC(cmd_certfp);
CMD_FUNC(cmd_release);
CMD_FUNC(cmd_drop);
CMD_FUNC(cmd_setpassword);
CMD_FUNC(cmd_sadrop);
CMD_FUNC(cmd_tban);
CMD_FUNC(cmd_bot);
CMD_FUNC(cmd_access);
EVENT(nick_enforce);
int responder_is_ok(Client *client, const char *name, const char *value);
// case insensitive version of strstr()
char *strcasestr(const char* haystack, const char* needle) {
if (*needle == '\0') {
return (char*) haystack; // Empty needle, return the haystack
}
while (*haystack != '\0') {
// Find the first character of the needle in the haystack
while (toupper(*haystack) != toupper(*needle)) {
if (*haystack == '\0') {
return NULL; // Reached the end of the haystack without finding the needle
}
haystack++;
}
// Compare the rest of the needle and the corresponding part of the haystack
const char* h = haystack;
const char* n = needle;
while (toupper(*h) == toupper(*n)) {
if (*n == '\0') {
return (char*) haystack; // Found the needle in the haystack
}
h++;
n++;
}
// Continue searching in the haystack
haystack++;
}
return NULL; // Needle not found
}
char *json_user_object_get_property(json_t *user_obj, const char *property_name)
{
const char *key;
char *ret = NULL;
json_t *value;
json_object_foreach(user_obj, key, value)
{
if (!strcasecmp(key, property_name))
ret = strdup(json_string_value(value));
}
return ret;
}
char *json_user_object_get_meta(json_t *user_obj, const char *meta_name)
{
const char *key, *key2;
char *ret = NULL;
json_t *value, *value2;
json_object_foreach(user_obj, key, value)
{
if (strcasecmp(key, "meta") != 0)
continue;
json_object_foreach(value, key2, value2)
{
if (!strcasecmp(key, meta_name))
ret = strdup(json_string_value(value2));
}
}
return ret;
}
int random_number(int low, int high)
{
int number = rand() % ((high+1) - low) + low;
return number;
}
char *uppercaseString(const char* input)
{
size_t length = strlen(input);
// Allocate memory for the new string
char* result = (char*)malloc(length + 1);
if (result == NULL)
{
// Handle memory allocation error
exit(EXIT_FAILURE);
}
// Copy the input string and convert each character to uppercase
for (size_t i = 0; i < length; ++i)
{
result[i] = toupper(input[i]);
}
// Null-terminate the new string
result[length] = '\0';
return result;
}
/* These are two local (static) buffers used by the various send functions */
static char sendbuf[MAXLINELENGTH];
static char sendbuf2[MAXLINELENGTH];
/** Send a message to a single client - va_list variant.
* This function is similar to sendto_one() except that it
* doesn't use varargs but uses a va_list instead.
* Generally this function is NOT used outside send.c, so not by modules.
* @param to The client to send to
* @param mtags Any message tags associated with this message (can be NULL)
* @param pattern The format string / pattern to use.
* @param vl Format string parameters.
*/
void vsendto_one(Client *to, MessageTag *mtags, const char *pattern, va_list vl)
{
const char *mtags_str = mtags ? mtags_to_string(mtags, to) : NULL;
/* Need to ignore -Wformat-nonliteral here */
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
ircvsnprintf(sendbuf, sizeof(sendbuf)-3, pattern, vl);
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
if (BadPtr(mtags_str))
{
/* Simple message without message tags */
sendbufto_one(to, sendbuf, 0);
} else {
/* Message tags need to be prepended */
snprintf(sendbuf2, sizeof(sendbuf2)-3, "@%s %s", mtags_str, sendbuf);
sendbufto_one(to, sendbuf2, 0);
}
}
/*** TRANSLATION THINGS */
#define IsAutoTranslate(x) (x->umodes & UMODE_TRANSLATE)
/* Global variables */
long UMODE_TRANSLATE = 0L;
// Define a structure to hold ISO language information
typedef struct {
char code[8];
char name[70];
} ISO_Language;
const ISO_Language iso_language_list[] = {
{ "AR", "Arabic" },
{ "BG", "Bulgarian" },
{ "CS", "Czech" },
{ "DA", "Danish" },
{ "DE", "German" },
{ "EL", "Greek" },
{ "EN-GB", "English (British)" },
{ "EN-US", "English (American)" },
{ "ES", "Spanish" },
{ "ET", "Estonian" },
{ "FI", "Finnish" },
{ "FR", "French" },
{ "HU", "Hungarian" },
{ "ID", "Indonesian" },
{ "IT", "Italian" },
{ "JA", "Japanese" },
{ "KO", "Korean" },
{ "LT", "Lithuanian" },
{ "LV", "Latvian" },
{ "NB", "Norwegian (Bokmål)" },
{ "NL", "Dutch" },
{ "PL", "Polish" },
{ "PT-BR", "Portuguese (Brazilian)" },
{ "PT-PT", "Portuguese (all Portuguese varieties excluding Brazilian Portuguese)" },
{ "RO", "Romanian" },
{ "RU", "Russian" },
{ "SK", "Slovak" },
{ "SL", "Slovenian" },
{ "SV", "Swedish" },
{ "TR", "Turkish" },
{ "UK", "Ukrainian" },
{ "ZH", "Chinese (simplified)" }
};
// Function to check if a given string is a valid ISO language code
bool isValidISOLanguageCode(const char *code) {
int i;
for (i = 0; i < sizeof(iso_language_list) / sizeof(iso_language_list[0]); i++) {
if (strcasecmp(code, iso_language_list[i].code) == 0)
return true;
}
return false;
}
int iso_lang_mtag_is_ok(Client *client, const char *name, const char *value)
{
if (isValidISOLanguageCode(value))
return 1;
sendto_one(client, NULL, ":%s NOTE * INVALID_MTAG_VALUE %s :Value for tag \"%s\" must be a valid ISO language code, for example EN-GB (British English)", me.name, value, name);
return 0;
}
void mtag_add_iso_lang(Client *client, MessageTag *recv_mtags, MessageTag **mtag_list, const char *signature)
{
MessageTag *m;
if (IsUser(client))
{
m = find_mtag(recv_mtags, MTAG_ISO_LANG);
if (m)
{
m = duplicate_mtag(m);
AddListItem(m, *mtag_list);
}
}
}
void language_free(ModData *m)
{
safe_free(m->str);
}
const char *language_serialize(ModData *m)
{
if (!m->str)
return NULL;
return m->str;
}
void language_unserialize(const char *str, ModData *m)
{
safe_strdup(m->str, str);
}
CMD_FUNC(cmd_setlang);
/*** BOT THINGS AND STUFF */
typedef struct BotCommand BotCommand;
struct BotCommand {
char command[50];
char helptext[250];
struct BotCommand *prev, *next;
};
// Function to create a new bot command node
BotCommand* create_bot_command(const char* command, const char* helptext)
{
BotCommand* newCommand = (BotCommand*)malloc(sizeof(BotCommand));
if (newCommand == NULL)
{
// Handle memory allocation failure
exit(EXIT_FAILURE);
}
strcpy(newCommand->command, command);
strcpy(newCommand->helptext, helptext);
newCommand->prev = NULL;
newCommand->next = NULL;
return newCommand;
}
// Function to insert a new bot command at the beginning of the doubly-linked list
BotCommand* insert_bot_command(BotCommand* head, const char* command, const char* helptext)
{
BotCommand* newCommand = create_bot_command(command, helptext);
newCommand->next = head;
if (head != NULL)
{
head->prev = newCommand;
}
return newCommand;
}
// Function to display all bot commands in the doubly-linked list
void displayBotCommands(BotCommand* head)
{
BotCommand* current = head;
while (current != NULL)
{
printf("Command: %s\n", current->command);
printf("HelpText: %s\n", current->helptext);
printf("\n");
current = current->next;
}
}
// Function to handle bot commands received from IRC
void handle_bot_command(const char* botName, const char* command)
{
// You can implement logic here to process the bot command
printf("Bot %s received command: %s\n", botName, command);
}
// Function to free the memory allocated for the doubly-linked list
void free_bot_commands(BotCommand* head)
{
BotCommand* current = head;
while (current != NULL)
{
BotCommand* next = current->next;
free(current);
current = next;
}
}
// Function to delete a specific bot command from the doubly-linked list
BotCommand* delete_bot_command(BotCommand* head, const char* commandToDelete)
{
BotCommand* current = head;
// Search for the command to delete
while (current != NULL)
{
if (strcmp(current->command, commandToDelete) == 0)
{
if (current->prev != NULL)
{
current->prev->next = current->next;
} else {
// Deleting the first node, update head
head = current->next;
}
if (current->next != NULL)
{
current->next->prev = current->prev;
}
free(current);
break;
}
current = current->next;
}
return head; // Return the updated head of the list
}
typedef struct Bot Bot;
struct Bot {
char name[NICKLEN + 1];
BotCommand *cmdslist;
MultiLine *helptext;
struct Bot *prev, *next;
};
// Function to add a new command to a bot
void add_command_to_bot(Bot* bot, const char* command, const char* helpText)
{
BotCommand* newCommand = (BotCommand*)malloc(sizeof(BotCommand));
if (newCommand == NULL)
{
// Handle memory allocation error
exit(EXIT_FAILURE);
}
strncpy(newCommand->command, uppercaseString(command), sizeof(newCommand->command));
strncpy(newCommand->helptext, helpText, sizeof(newCommand->helptext));
newCommand->next = bot->cmdslist;
bot->cmdslist = newCommand;
}
// Function to remove a command from a bot
void remove_command_from_bot(Bot* bot, const char* command)
{
BotCommand* current = bot->cmdslist;
BotCommand* prev = NULL;
while (current != NULL)
{
if (strcasecmp(current->command, command) == 0)
{
if (prev == NULL)
{
bot->cmdslist = current->next;
} else {
prev->next = current->next;
}
free(current);
return;
}
prev = current;
current = current->next;
}
}
bool BotHasCommand(Bot *bot, const char *cmd)
{
BotCommand* current = bot->cmdslist;
while (current != NULL)
{
if (strcasecmp(current->command, cmd) == 0)
{
return true;
}
current = current->next;
}
return false;
}
/* Config struct*/
struct cfgstruct {
char *url;
char *key;
unsigned short int got_url;
unsigned short int got_key;
unsigned short int got_password_strength_requirement;
unsigned short int got_guest_nick;
unsigned short int got_nick_enforcement_timeout;
// account registration
int register_before_connect;
int register_custom_account;
int register_email_required;
int password_strength_requirement;
long nick_enforce_timeout;
char *guest_nick;
Bot *bot_node; // head of linked list
};
static struct cfgstruct cfg;
void bot_sendnotice(Bot *from, Client *target, FORMAT_STRING(const char *pattern), ...)
{
if (!from)
return;
va_list vl;
char buffer[512];
// Use snprintf to modify the string in the buffer
snprintf(buffer, sizeof(buffer), ":%s!%s@%s NOTICE %s :%s", from->name, from->name, me.name, target->name, pattern);
// Use the same buffer for the new const char*
const char* newString = buffer;
va_start(vl, pattern);
vsendto_one(target, NULL, newString, vl);
va_end(vl);
}
Bot *AddBot(const char *name)
{
Bot *newBot = (Bot *)malloc(sizeof(Bot));
if (newBot != NULL)
{
strncpy(newBot->name, name, NICKLEN);
newBot->cmdslist = NULL;
newBot->next = cfg.bot_node;
newBot->prev = NULL;
if (cfg.bot_node != NULL)
{
cfg.bot_node->prev = newBot;
}
cfg.bot_node = newBot;
}
return newBot;
}
void DelBot(const char *name)
{
Bot *currentBot = cfg.bot_node;
while (currentBot != NULL)
{
if (strcasecmp(currentBot->name, name) == 0)
{
if (currentBot->prev != NULL)
{
currentBot->prev->next = currentBot->next;
} else {
cfg.bot_node = currentBot->next;
}
if (currentBot->next != NULL)
{
currentBot->next->prev = currentBot->prev;
}
free(currentBot);
break;
}
currentBot = currentBot->next;
}
}
Bot *find_bot(const char *name)
{
Bot *currentBot = cfg.bot_node;
while (currentBot != NULL)
{
if (strcasecmp(currentBot->name, name) == 0)
{
return currentBot;
}
currentBot = currentBot->next;
}
return NULL; // Bot not found
}
void free_bot_node()
{
Bot *currentBot = cfg.bot_node;
Bot *nextBot;
while (currentBot != NULL)
{
nextBot = currentBot->next;
free_bot_commands(currentBot->cmdslist);
free(currentBot);
currentBot = nextBot;
}
cfg.bot_node = NULL;
}
/* adds the responder message-tag to the given &list */
void mtag_add_responder(MessageTag **mtags, Bot *bot)
{
MessageTag *m;
if (!bot)
return;
m = safe_alloc(sizeof(MessageTag));
safe_strdup(m->name, MTAG_RESPONDER);
safe_strdup(m->value, bot->name);
AddListItem(m, *mtags);
}
void rehash_check_bline(void)
{
Client *cptr;
Bot *bot;
list_for_each_entry(cptr, &client_list, client_node)
{
if ((bot = find_bot(cptr->name)))
{
const char *comment = "Overridden";
int n;
/* for new_message() we use target here, makes sense for the exit_client, right? */
if (!MyConnect(cptr))
sendto_server(NULL, 0, 0, NULL, ":%s SVSKILL %s :%s", me.name, cptr->id, comment);
SetKilled(cptr);
dead_socket(cptr, comment);
}
}
}
/** "Test" the "validity" of emails*/
int IsValidEmail(const char *str)
{
if (!strstr(str, "@"))
return 0;
if (!strstr(str, "."))
return 0;
return 1;
}
// copied from source: src/rpc/name_bans.c
TKL *my_find_tkl_nameban(const char *name)
{
TKL *tkl;
for (tkl = tklines[tkl_hash('Q')]; tkl; tkl = tkl->next)
{
if (!TKLIsNameBan(tkl))
continue;
if (!strcasecmp(name, tkl->ptr.nameban->name))
return tkl;
}
return NULL;
}
/** Checks the strength of a given password
* @param password The password to check
* @param int Value between 0 and 4, 0 = no validation, 4 = require a very strong password
*/
int check_password_strength_dalek(const char *password, int password_strength_requirement)
{
int length = strlen(password);
int uppercase = 0, lowercase = 0, digits = 0, symbols = 0;
for (int i = 0; i < length; i++)
{
if (isupper(password[i]))
{
uppercase = 1;
} else if (islower(password[i]))
{
lowercase = 1;
} else if (isdigit(password[i]))
{
digits = 1;
} else {
symbols = 1;
}
}
int strength = uppercase + lowercase + digits + symbols;
if (length >= 8 && strength >= password_strength_requirement)
{
return 1; // Password meets strength requirement
} else {
return 0; // Password does not meet strength requirement
}
}
/** special send()
* This function lets you send one thing to a user in two different manners depending on whether
* there is a bot required to reply to the user, the latter being a fallback for the former:
* 1) The first way is via a bot which is defined by config. This sends a notice to the user just like classic services.
* 2) The second way is via IRCv3 Standard Replies.
*
* @param from The Bot struct to send the notice from. (Can be NULL for forced Standard Replies.)
* @param to The client to send the message to.
* @param success 0 = Fail, 1 = Warn, 2 = NOTE (Not shown in bot notices)
* @param cmd The command the user tried to issue (Not shown in bot notices)
* @param msg The associated error code/message (NOT_ON_CHANNEL, INVALID_EMAIL, etc)
* @param extra Any extra data to show to the client, such as any parameters they might have passed
* @param pattern The human-readible format string/pattern to use (e.g.: "That email address is invalid :%s")
* @returns Returns nothing
*/
void special_send(Bot *from, Client *to, int success, const char *cmd, const char *msg, const char *extra, FORMAT_STRING(const char *pattern), ...)
{
va_list vl;
char buffer[512];
char sreply[5];
if (strstr(cmd, " ") || strstr(msg, " ")) // no spaces allowed
return;
if (!success)
snprintf(sreply, sizeof(sreply), "%s", "FAIL");
else if (success == 1)
snprintf(sreply, sizeof(sreply), "%s", "WARN");
else if (success == 2)
snprintf(sreply, sizeof(sreply), "%s", "NOTE");
// Use snprintf to modify the string in the buffer
if (from)
snprintf(buffer, sizeof(buffer), ":%s!%s@%s NOTICE %s :%s (%s: %s)", from->name, from->name, me.name, to->name, pattern, msg, (extra) ? extra : "*");
else
snprintf(buffer, sizeof(buffer), ":%s %s %s %s %s :%s", me.name, sreply, cmd, msg, ((extra)) ? extra : "*", pattern);
// Use the same buffer for the new const char*
const char* newString = buffer;
va_start(vl, pattern);
vsendto_one(to, NULL, newString, vl);
va_end(vl);
}
/** Check a client's stored password against a hash
* @param client The client whose password to check
* @param hash The hash to check against
* @returns Boolean
*/
bool is_correct_password(Client *client, const char *hash)
{
if (!client)
{
return false;
}
const char *local_pass = GetPassword(client);
if (!local_pass)
{
return false;
}
if (argon2_verify(hash, local_pass, strlen(local_pass), Argon2_id) == ARGON2_OK)
{
return true;
}
return false;
}
/** checks the validity of nicks
* @param nick The nick to check
* @returns Boolean
*/
bool is_valid_nick(const char *nick)
{
// Check if the nickname is not NULL
if (nick == NULL)
{
return false;
}
int length = strlen(nick);
// Check if the length is within the valid range
if (length < 1 || length > NICKLEN)
{
return false;
}
// Check if the first character is a letter
if (!isalpha(nick[0]))
{
return false;
}
// Check if the remaining characters are letters, numbers, underscores, or hyphens
for (int i = 1; i < length; ++i)
{
if (!isalnum(nick[i]) && nick[i] != '_' && nick[i] != '-')
{
return false;
}
}
// If all checks pass, the nickname is valid
return true;
}
/** Query the No-Services API
@param endpoint The endpoint of the API
@param body The body to POST, typically JSON
@param callback The callback function
@returns Void
*/
void query_api(const char *endpoint, char *body, const char *callback)
{
OutgoingWebRequest *w = safe_alloc(sizeof(OutgoingWebRequest));
json_t *j;
NameValuePrioList *headers = NULL;
add_nvplist(&headers, 0, "Content-Type", "application/json; charset=utf-8");
add_nvplist(&headers, 0, "X-API-Key", cfg.key);
/* Do the web request */
char *our_url = construct_url(cfg.url, endpoint);
safe_strdup(w->url, our_url);
w->http_method = HTTP_METHOD_POST;
w->body = body;
w->headers = headers;
w->max_redirects = 1;
safe_strdup(w->apicallback, callback);
url_start_async(w);
free(our_url);
}
// basically strncat but with a new char in memorayyy
char *construct_url(const char *base_url, const char *extra_params)
{
size_t base_len = strlen(base_url) +1;
size_t params_len = strlen(extra_params)+1;
// Calculate the length of the resulting URL (+1 for the null terminator)
size_t url_len = base_len+params_len+1;
// Allocate memory for the URL
char *url = (char *)safe_alloc(url_len);
if (url != NULL)
{
// Copy the base URL into the constructed URL
strncpy(url, base_url, base_len);
url[base_len] = '\0'; // Null-terminate the base URL in the new string
// Concatenate the extra parameters
strncat(url, extra_params, params_len);
url[url_len - 1] = '\0'; // Ensure null termination at the end
}
return url;
}
/** Fetches a Bot struct of the intended responder to a command from MessageTag list
* @param recv_mtags A MessageTag linked list to search for a responder
* @returns A bot struct of the bot agent found in the MessageTag list
*/
Bot *fetch_responder(MessageTag *recv_mtags)
{
MessageTag *m = find_mtag(recv_mtags, MTAG_RESPONDER);
return (m) ? find_bot(m->value) : NULL;
}
/** Sets a new password for the account
* @param client The client issuing the password change
* @param account The name of the account to change in case it's an admin changing someones password for them or something
* @param password The hash of the password (not the actual password itself) to update with.
* @param responder The bot agent responsible for replying to this request after our callback later
* @returns 1 on success, 0 on failure
*/