-
Notifications
You must be signed in to change notification settings - Fork 65
/
irc-mode.c
1115 lines (1031 loc) · 30.3 KB
/
irc-mode.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
/*
* ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* Please read the file COPYING, README and AUTHORS for more information.
*/
#include "portab.h"
/**
* @file
* IRC commands for mode changes (like MODE, AWAY, etc.)
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "conn.h"
#include "channel.h"
#include "irc-macros.h"
#include "irc-write.h"
#include "lists.h"
#include "log.h"
#include "parse.h"
#include "messages.h"
#include "conf.h"
#include "irc-mode.h"
static bool Client_Mode PARAMS((CLIENT *Client, REQUEST *Req, CLIENT *Origin,
CLIENT *Target));
static bool Channel_Mode PARAMS((CLIENT *Client, REQUEST *Req, CLIENT *Origin,
CHANNEL *Channel));
static bool Add_To_List PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
CHANNEL *Channel, const char *Pattern));
static bool Del_From_List PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
CHANNEL *Channel, const char *Pattern));
static bool Send_ListChange PARAMS((const bool IsAdd, const char ModeChar,
CLIENT *Prefix, CLIENT *Client,
CHANNEL *Channel, const char *Mask));
/**
* Handler for the IRC "MODE" command.
*
* This function detects whether user or channel modes should be modified
* and calls the appropriate sub-functions.
*
* @param Client The client from which this command has been received.
* @param Req Request structure with prefix and all parameters.
* @return CONNECTED or DISCONNECTED.
*/
GLOBAL bool
IRC_MODE( CLIENT *Client, REQUEST *Req )
{
CLIENT *cl, *origin;
CHANNEL *chan;
assert(Client != NULL);
assert(Req != NULL);
_IRC_GET_SENDER_OR_RETURN_(origin, Req, Client)
/* Test for "fake" MODE commands injected by this local instance,
* for example when handling the "DefaultUserModes" settings.
* This doesn't harm real commands, because prefixes of regular
* clients are checked in Validate_Prefix() and can't be faked. */
if (Req->prefix && Client_Search(Req->prefix) == Client_ThisServer())
Client = Client_Search(Req->prefix);
/* Channel or user mode? */
cl = NULL; chan = NULL;
if (Client_IsValidNick(Req->argv[0]))
cl = Client_Search(Req->argv[0]);
if (Channel_IsValidName(Req->argv[0]))
chan = Channel_Search(Req->argv[0]);
if (cl)
return Client_Mode(Client, Req, origin, cl);
if (chan)
return Channel_Mode(Client, Req, origin, chan);
/* No target found! */
return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
Client_ID(Client), Req->argv[0]);
} /* IRC_MODE */
/**
* Check if the "mode limit" for a client has been reached.
*
* This limit doesn't apply for servers or services!
*
* @param Client The client to check.
* @param Count The number of modes already handled.
* @return true if the limit has been reached.
*/
static bool
Mode_Limit_Reached(CLIENT *Client, int Count)
{
if (Client_Type(Client) == CLIENT_SERVER
|| Client_Type(Client) == CLIENT_SERVICE)
return false;
if (Count < MAX_HNDL_MODES_ARG)
return false;
return true;
}
/**
* Handle client mode requests
*
* @param Client The client from which this command has been received.
* @param Req Request structure with prefix and all parameters.
* @param Origin The originator of the MODE command (prefix).
* @param Target The target (client) of this MODE command.
* @return CONNECTED or DISCONNECTED.
*/
static bool
Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
{
char the_modes[COMMAND_LEN], x[2], *mode_ptr;
bool ok, set;
bool send_RPL_HOSTHIDDEN_MSG = false;
int mode_arg;
size_t len;
/* Is the client allowed to request or change the modes? */
if (Client_Type(Client) == CLIENT_USER) {
/* Users are only allowed to manipulate their own modes! */
if (Target != Client)
return IRC_WriteErrClient(Client,
ERR_USERSDONTMATCH_MSG,
Client_ID(Client));
}
/* Mode request: let's answer it :-) */
if (Req->argc == 1)
return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
Client_ID(Target),
Client_Modes(Target));
mode_arg = 1;
mode_ptr = Req->argv[mode_arg];
/* Initial state: set or unset modes? */
if (*mode_ptr == '+') {
set = true;
strcpy(the_modes, "+");
} else if (*mode_ptr == '-') {
set = false;
strcpy(the_modes, "-");
} else
return IRC_WriteErrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
Client_ID(Origin));
x[1] = '\0';
ok = CONNECTED;
while (mode_ptr) {
mode_ptr++;
if (!*mode_ptr) {
/* Try next argument if there's any */
mode_arg++;
if (mode_arg < Req->argc)
mode_ptr = Req->argv[mode_arg];
else
break;
}
switch(*mode_ptr) {
case '+':
case '-':
if ((*mode_ptr == '+' && !set)
|| (*mode_ptr == '-' && set)) {
/* Action modifier ("+"/"-") must be changed */
len = strlen(the_modes) - 1;
if (the_modes[len] == '+'
|| the_modes[len] == '-') {
/* Last character in the "result
* string" was an "action", so just
* overwrite it with the new action */
the_modes[len] = *mode_ptr;
} else {
/* Append new modifier character to
* the resulting mode string */
x[0] = *mode_ptr;
strlcat(the_modes, x,
sizeof(the_modes));
}
if (*mode_ptr == '+')
set = true;
else
set = false;
}
continue;
}
/* Validate modes */
x[0] = '\0';
switch (*mode_ptr) {
case 'b': /* Block private msgs */
case 'C': /* Only messages from clients sharing a channel */
case 'i': /* Invisible */
case 'I': /* Hide channel list from WHOIS */
case 's': /* Server messages */
case 'w': /* Wallops messages */
x[0] = *mode_ptr;
break;
case 'a': /* Away */
if (Client_Type(Client) == CLIENT_SERVER) {
x[0] = 'a';
Client_SetAway(Origin, DEFAULT_AWAY_MSG);
} else
ok = IRC_WriteErrClient(Origin,
ERR_NOPRIVILEGES_MSG,
Client_ID(Origin));
break;
case 'B': /* Bot */
if (Client_HasMode(Client, 'r'))
ok = IRC_WriteErrClient(Origin,
ERR_RESTRICTED_MSG,
Client_ID(Origin));
else
x[0] = 'B';
break;
case 'c': /* Receive connect notices */
case 'q': /* KICK-protected user */
case 'F': /* disable flood protection */
/* (only settable by IRC operators!) */
if (!set || Client_Type(Client) == CLIENT_SERVER
|| Client_HasMode(Origin, 'o'))
x[0] = *mode_ptr;
else
ok = IRC_WriteErrClient(Origin,
ERR_NOPRIVILEGES_MSG,
Client_ID(Origin));
break;
case 'o': /* IRC operator (only unsettable!) */
if (!set || Client_Type(Client) == CLIENT_SERVER) {
x[0] = 'o';
} else
ok = IRC_WriteErrClient(Origin,
ERR_NOPRIVILEGES_MSG,
Client_ID(Origin));
break;
case 'r': /* Restricted (only settable) */
if (set || Client_Type(Client) == CLIENT_SERVER)
x[0] = 'r';
else
ok = IRC_WriteErrClient(Origin,
ERR_RESTRICTED_MSG,
Client_ID(Origin));
break;
case 'R': /* Registered (not [un]settable by clients) */
if (Client_Type(Client) == CLIENT_SERVER)
x[0] = 'R';
else
ok = IRC_WriteErrClient(Origin,
ERR_NICKREGISTER_MSG,
Client_ID(Origin));
break;
case 'x': /* Cloak hostname */
if (Client_HasMode(Client, 'r'))
ok = IRC_WriteErrClient(Origin,
ERR_RESTRICTED_MSG,
Client_ID(Origin));
else if (!set || Conf_CloakHostModeX[0]
|| Client_Type(Client) == CLIENT_SERVER
|| Client_HasMode(Origin, 'o')) {
x[0] = 'x';
send_RPL_HOSTHIDDEN_MSG = true;
} else
ok = IRC_WriteErrClient(Origin,
ERR_NOPRIVILEGES_MSG,
Client_ID(Origin));
break;
default:
if (Client_Type(Client) != CLIENT_SERVER) {
Log(LOG_DEBUG,
"Unknown mode \"%c%c\" from \"%s\"!?",
set ? '+' : '-', *mode_ptr,
Client_ID(Origin));
ok = IRC_WriteErrClient(Origin,
ERR_UMODEUNKNOWNFLAG2_MSG,
Client_ID(Origin),
set ? '+' : '-',
*mode_ptr);
x[0] = '\0';
} else {
Log(LOG_DEBUG,
"Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
set ? '+' : '-', *mode_ptr,
Client_ID(Origin), Client_ID(Target));
x[0] = *mode_ptr;
}
}
if (!ok)
break;
/* Is there a valid mode change? */
if (!x[0])
continue;
if (set) {
if (Client_ModeAdd(Target, x[0]))
strlcat(the_modes, x, sizeof(the_modes));
} else {
if (Client_ModeDel(Target, x[0]))
strlcat(the_modes, x, sizeof(the_modes));
}
}
/* Are there changed modes? */
if (the_modes[1]) {
/* Remove needless action modifier characters */
len = strlen(the_modes) - 1;
if (the_modes[len] == '+' || the_modes[len] == '-')
the_modes[len] = '\0';
if (Client_Type(Client) == CLIENT_SERVER) {
/* Forward modes to other servers */
if (Client_Conn(Target) != NONE) {
/* Remote server (service?) changed modes
* for one of our clients. Inform it! */
IRC_WriteStrClientPrefix(Target, Origin,
"MODE %s :%s",
Client_ID(Target),
the_modes);
}
IRC_WriteStrServersPrefix(Client, Origin,
"MODE %s :%s",
Client_ID(Target),
the_modes);
} else {
/* Send reply to client and inform other servers */
ok = IRC_WriteStrClientPrefix(Client, Origin,
"MODE %s :%s",
Client_ID(Target),
the_modes);
IRC_WriteStrServersPrefix(Client, Origin,
"MODE %s :%s",
Client_ID(Target),
the_modes);
}
if (send_RPL_HOSTHIDDEN_MSG && Client_Conn(Target) > NONE) {
/* A new (cloaked) hostname must be announced */
IRC_WriteStrClientPrefix(Target, Origin,
RPL_HOSTHIDDEN_MSG,
Client_ID(Target),
Client_HostnameDisplayed(Target));
}
LogDebug("%s \"%s\": Mode change, now \"%s\".",
Client_TypeText(Target), Client_Mask(Target),
Client_Modes(Target));
}
return ok;
} /* Client_Mode */
/*
* Reply to a channel mode request.
*
* @param Origin The originator of the MODE command (prefix).
* @param Channel The channel of which the modes should be sent.
* @return CONNECTED or DISCONNECTED.
*/
static bool
Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
{
char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
const char *mode_ptr;
if (!Channel_IsMemberOf(Channel, Origin)) {
/* Not a member: "simple" mode reply */
if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
Client_ID(Origin), Channel_Name(Channel),
Channel_Modes(Channel)))
return DISCONNECTED;
} else {
/* The sender is a member: generate extended reply */
strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
mode_ptr = the_modes;
the_args[0] = '\0';
while(*mode_ptr) {
switch(*mode_ptr) {
case 'l':
snprintf(argadd, sizeof(argadd), " %lu",
Channel_MaxUsers(Channel));
strlcat(the_args, argadd, sizeof(the_args));
break;
case 'k':
strlcat(the_args, " ", sizeof(the_args));
strlcat(the_args, Channel_Key(Channel),
sizeof(the_args));
break;
}
mode_ptr++;
}
if (the_args[0])
strlcat(the_modes, the_args, sizeof(the_modes));
if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
Client_ID(Origin), Channel_Name(Channel),
the_modes))
return DISCONNECTED;
}
#ifndef STRICT_RFC
/* Channel creation time */
if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
Client_ID(Origin), Channel_Name(Channel),
Channel_CreationTime(Channel)))
return DISCONNECTED;
#endif
return CONNECTED;
}
/**
* Handle channel mode and channel-user mode changes
*
* @param Client The client from which this command has been received.
* @param Req Request structure with prefix and all parameters.
* @param Origin The originator of the MODE command (prefix).
* @param Channel The target channel of this MODE command.
* @return CONNECTED or DISCONNECTED.
*/
static bool
Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
{
char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
argadd[CLIENT_PASS_LEN], *mode_ptr;
bool connected, set, skiponce, retval, use_servermode,
is_halfop, is_op, is_admin, is_owner, is_machine, is_oper;
int mode_arg, arg_arg, mode_arg_count = 0;
CLIENT *client;
long l;
size_t len;
is_halfop = is_op = is_admin = is_owner = is_machine = is_oper = false;
if (Channel_IsModeless(Channel))
return IRC_WriteErrClient(Client, ERR_NOCHANMODES_MSG,
Client_ID(Client), Channel_Name(Channel));
/* Mode request: let's answer it :-) */
if (Req->argc <= 1)
return Channel_Mode_Answer_Request(Origin, Channel);
/* Check if origin is oper and opers can use mode */
use_servermode = Conf_OperServerMode;
if(Client_HasMode(Client, 'o') && Conf_OperCanMode) {
is_oper = true;
}
/* Check if client is a server/service */
if(Client_Type(Client) == CLIENT_SERVER ||
Client_Type(Client) == CLIENT_SERVICE) {
is_machine = true;
}
/* Check if client is member of channel or an oper or an server/service */
if(!Channel_IsMemberOf(Channel, Client) && !is_oper && !is_machine)
return IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
Client_ID(Origin),
Channel_Name(Channel));
mode_arg = 1;
mode_ptr = Req->argv[mode_arg];
if (Req->argc > mode_arg + 1)
arg_arg = mode_arg + 1;
else
arg_arg = -1;
/* Initial state: set or unset modes? */
skiponce = false;
switch (*mode_ptr) {
case '-':
set = false;
break;
case '+':
set = true;
break;
default:
set = true;
skiponce = true;
}
/* Prepare reply string */
strcpy(the_modes, set ? "+" : "-");
the_args[0] = '\0';
x[1] = '\0';
connected = CONNECTED;
while (mode_ptr) {
if (!skiponce)
mode_ptr++;
if (!*mode_ptr) {
/* Try next argument if there's any */
if (arg_arg < 0)
break;
if (arg_arg > mode_arg)
mode_arg = arg_arg;
else
mode_arg++;
if (mode_arg >= Req->argc)
break;
mode_ptr = Req->argv[mode_arg];
if (Req->argc > mode_arg + 1)
arg_arg = mode_arg + 1;
else
arg_arg = -1;
}
skiponce = false;
switch (*mode_ptr) {
case '+':
case '-':
if (((*mode_ptr == '+') && !set)
|| ((*mode_ptr == '-') && set)) {
/* Action modifier ("+"/"-") must be changed ... */
len = strlen(the_modes) - 1;
if (the_modes[len] == '+' || the_modes[len] == '-') {
/* Adjust last action modifier in result */
the_modes[len] = *mode_ptr;
} else {
/* Append modifier character to result string */
x[0] = *mode_ptr;
strlcat(the_modes, x, sizeof(the_modes));
}
set = *mode_ptr == '+';
}
continue;
}
/* Are there arguments left? */
if (arg_arg >= Req->argc)
arg_arg = -1;
if(!is_machine && !is_oper) {
if (Channel_UserHasMode(Channel, Client, 'q'))
is_owner = true;
if (Channel_UserHasMode(Channel, Client, 'a'))
is_admin = true;
if (Channel_UserHasMode(Channel, Client, 'o'))
is_op = true;
if (Channel_UserHasMode(Channel, Client, 'h'))
is_halfop = true;
}
/* Validate modes */
x[0] = '\0';
argadd[0] = '\0';
client = NULL;
switch (*mode_ptr) {
/* --- Channel modes --- */
case 'R': /* Registered users only */
case 's': /* Secret channel */
case 'z': /* Secure connections only */
if(!is_oper && !is_machine && !is_owner &&
!is_admin && !is_op) {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin), Channel_Name(Channel));
goto chan_exit;
}
case 'i': /* Invite only */
case 'V': /* Invite disallow */
case 'M': /* Only identified nicks can write */
case 'm': /* Moderated */
case 'n': /* Only members can write */
case 'Q': /* No kicks */
case 't': /* Topic locked */
if(is_oper || is_machine || is_owner ||
is_admin || is_op || is_halfop)
x[0] = *mode_ptr;
else
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin), Channel_Name(Channel));
break;
case 'k': /* Channel key */
if (Mode_Limit_Reached(Client, mode_arg_count++))
goto chan_exit;
if (!set) {
if (is_oper || is_machine || is_owner ||
is_admin || is_op || is_halfop) {
x[0] = *mode_ptr;
if (Channel_HasMode(Channel, 'k'))
strlcpy(argadd, "*", sizeof(argadd));
if (arg_arg > mode_arg)
arg_arg++;
} else
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
break;
}
if (arg_arg > mode_arg) {
if (is_oper || is_machine || is_owner ||
is_admin || is_op || is_halfop) {
Channel_ModeDel(Channel, 'k');
Channel_SetKey(Channel,
Req->argv[arg_arg]);
strlcpy(argadd, Channel_Key(Channel),
sizeof(argadd));
x[0] = *mode_ptr;
} else {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
}
Req->argv[arg_arg][0] = '\0';
arg_arg++;
} else {
#ifdef STRICT_RFC
/* Only send error message in "strict" mode,
* this is how ircd2.11 and others behave ... */
connected = IRC_WriteErrClient(Origin,
ERR_NEEDMOREPARAMS_MSG,
Client_ID(Origin), Req->command);
#endif
goto chan_exit;
}
break;
case 'l': /* Member limit */
if (Mode_Limit_Reached(Client, mode_arg_count++))
goto chan_exit;
if (!set) {
if (is_oper || is_machine || is_owner ||
is_admin || is_op || is_halfop)
x[0] = *mode_ptr;
else
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
break;
}
if (arg_arg > mode_arg) {
if (is_oper || is_machine || is_owner ||
is_admin || is_op || is_halfop) {
l = atol(Req->argv[arg_arg]);
if (l > 0 && l < 0xFFFF) {
Channel_ModeDel(Channel, 'l');
Channel_SetMaxUsers(Channel, l);
snprintf(argadd, sizeof(argadd),
"%ld", l);
x[0] = *mode_ptr;
}
} else {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
}
Req->argv[arg_arg][0] = '\0';
arg_arg++;
} else {
#ifdef STRICT_RFC
/* Only send error message in "strict" mode,
* this is how ircd2.11 and others behave ... */
connected = IRC_WriteErrClient(Origin,
ERR_NEEDMOREPARAMS_MSG,
Client_ID(Origin), Req->command);
#endif
goto chan_exit;
}
break;
case 'O': /* IRC operators only */
if (set) {
/* Only IRC operators are allowed to
* set the 'O' channel mode! */
if(is_oper || is_machine)
x[0] = 'O';
else
connected = IRC_WriteErrClient(Origin,
ERR_NOPRIVILEGES_MSG,
Client_ID(Origin));
} else if(is_oper || is_machine || is_owner ||
is_admin || is_op)
x[0] = 'O';
else
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
break;
case 'P': /* Persistent channel */
if (set) {
/* Only IRC operators are allowed to
* set the 'P' channel mode! */
if(is_oper || is_machine)
x[0] = 'P';
else
connected = IRC_WriteErrClient(Origin,
ERR_NOPRIVILEGES_MSG,
Client_ID(Origin));
} else if(is_oper || is_machine || is_owner ||
is_admin || is_op)
x[0] = 'P';
else
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
break;
/* --- Channel user modes --- */
case 'q': /* Owner */
case 'a': /* Channel admin */
if(!is_oper && !is_machine && !is_owner && !is_admin) {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPPRIVTOOLOW_MSG,
Client_ID(Origin),
Channel_Name(Channel));
goto chan_exit;
}
case 'o': /* Channel operator */
if(!is_oper && !is_machine && !is_owner &&
!is_admin && !is_op) {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
goto chan_exit;
}
case 'h': /* Half Op */
if(!is_oper && !is_machine && !is_owner &&
!is_admin && !is_op) {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
goto chan_exit;
}
case 'v': /* Voice */
if (arg_arg > mode_arg) {
if (is_oper || is_machine || is_owner ||
is_admin || is_op || is_halfop) {
client = Client_Search(Req->argv[arg_arg]);
if (client)
x[0] = *mode_ptr;
else
connected = IRC_WriteErrClient(Origin,
ERR_NOSUCHNICK_MSG,
Client_ID(Origin),
Req->argv[arg_arg]);
} else {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
}
Req->argv[arg_arg][0] = '\0';
arg_arg++;
} else {
#ifdef STRICT_RFC
/* Report an error to the client, when a user
* mode should be changed but no nickname is
* given. But don't do it when not in "strict"
* mode, because most other servers don't do
* it as well and some clients send "wired"
* MODE commands like "MODE #chan -ooo nick". */
connected = IRC_WriteErrClient(Origin,
ERR_NEEDMOREPARAMS_MSG,
Client_ID(Origin), Req->command);
#endif
goto chan_exit;
}
break;
/* --- Channel lists --- */
case 'I': /* Invite lists */
case 'b': /* Ban lists */
case 'e': /* Channel exception lists */
if (Mode_Limit_Reached(Client, mode_arg_count++))
goto chan_exit;
if (arg_arg > mode_arg) {
/* modify list */
if (is_oper || is_machine || is_owner ||
is_admin || is_op || is_halfop) {
connected = set
? Add_To_List(*mode_ptr, Origin,
Client, Channel,
Req->argv[arg_arg])
: Del_From_List(*mode_ptr, Origin,
Client, Channel,
Req->argv[arg_arg]);
} else {
connected = IRC_WriteErrClient(Origin,
ERR_CHANOPRIVSNEEDED_MSG,
Client_ID(Origin),
Channel_Name(Channel));
}
Req->argv[arg_arg][0] = '\0';
arg_arg++;
} else {
switch (*mode_ptr) {
case 'I':
Channel_ShowInvites(Origin, Channel);
break;
case 'b':
Channel_ShowBans(Origin, Channel);
break;
case 'e':
Channel_ShowExcepts(Origin, Channel);
break;
}
}
break;
default:
if (Client_Type(Client) != CLIENT_SERVER) {
Log(LOG_DEBUG,
"Unknown mode \"%c%c\" from \"%s\" on %s!?",
set ? '+' : '-', *mode_ptr,
Client_ID(Origin), Channel_Name(Channel));
connected = IRC_WriteErrClient(Origin,
ERR_UNKNOWNMODE_MSG,
Client_ID(Origin), *mode_ptr,
Channel_Name(Channel));
x[0] = '\0';
} else {
Log(LOG_DEBUG,
"Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
set ? '+' : '-', *mode_ptr,
Client_ID(Origin), Channel_Name(Channel));
x[0] = *mode_ptr;
}
}
if (!connected)
break;
/* Is there a valid mode change? */
if (!x[0])
continue;
/* Validate target client */
if (client && (!Channel_IsMemberOf(Channel, client))) {
if (!IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
Client_ID(Origin),
Client_ID(client),
Channel_Name(Channel)))
break;
continue;
}
if (client) {
/* Channel-User-Mode */
retval = set
? Channel_UserModeAdd(Channel, client, x[0])
: Channel_UserModeDel(Channel, client, x[0]);
if (retval) {
strlcat(the_args, " ", sizeof(the_args));
strlcat(the_args, Client_ID(client),
sizeof(the_args));
strlcat(the_modes, x, sizeof(the_modes));
LogDebug
("User \"%s\": Mode change on %s, now \"%s\"",
Client_Mask(client), Channel_Name(Channel),
Channel_UserModes(Channel, client));
}
} else {
/* Channel-Mode */
retval = set
? Channel_ModeAdd(Channel, x[0])
: Channel_ModeDel(Channel, x[0]);
if (retval) {
strlcat(the_modes, x, sizeof(the_modes));
LogDebug("Channel %s: Mode change, now \"%s\".",
Channel_Name(Channel),
Channel_Modes(Channel));
}
}
/* Are there additional arguments to add? */
if (argadd[0]) {
strlcat(the_args, " ", sizeof(the_args));
strlcat(the_args, argadd, sizeof(the_args));
}
}
chan_exit:
/* Are there changed modes? */
if (the_modes[1]) {
/* Clean up mode string */
len = strlen(the_modes) - 1;
if ((the_modes[len] == '+') || (the_modes[len] == '-'))
the_modes[len] = '\0';
if (Client_Type(Client) == CLIENT_SERVER) {
/* MODE requests for local channels from other servers
* are definitely invalid! */
if (Channel_IsLocal(Channel)) {
Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
return CONNECTED;
}
/* Forward mode changes to channel users and all the
* other remote servers: */
IRC_WriteStrServersPrefix(Client, Origin,
"MODE %s %s%s", Channel_Name(Channel),
the_modes, the_args);
IRC_WriteStrChannelPrefix(Client, Channel, Origin,
false, "MODE %s %s%s", Channel_Name(Channel),
the_modes, the_args);
} else {
if (use_servermode)
Origin = Client_ThisServer();
/* Send reply to client and inform other servers and channel users */
connected = IRC_WriteStrClientPrefix(Client, Origin,
"MODE %s %s%s", Channel_Name(Channel),
the_modes, the_args);
/* Only forward requests for non-local channels */
if (!Channel_IsLocal(Channel))
IRC_WriteStrServersPrefix(Client, Origin,
"MODE %s %s%s", Channel_Name(Channel),
the_modes, the_args);
IRC_WriteStrChannelPrefix(Client, Channel, Origin,
false, "MODE %s %s%s", Channel_Name(Channel),
the_modes, the_args);
}
}
return connected;
} /* Channel_Mode */
/**
* Handler for the IRC "AWAY" command.
*
* @param Client The client from which this command has been received.
* @param Req Request structure with prefix and all parameters.
* @return CONNECTED or DISCONNECTED.
*/
GLOBAL bool
IRC_AWAY( CLIENT *Client, REQUEST *Req )
{
assert (Client != NULL);
assert (Req != NULL);
if (Req->argc == 1 && Req->argv[0][0]) {
Client_SetAway(Client, Req->argv[0]);
Client_ModeAdd(Client, 'a');
IRC_WriteStrServersPrefix(Client, Client, "MODE %s :+a",
Client_ID( Client));
return IRC_WriteStrClient(Client, RPL_NOWAWAY_MSG,
Client_ID( Client));
} else {
Client_ModeDel(Client, 'a');
IRC_WriteStrServersPrefix(Client, Client, "MODE %s :-a",
Client_ID( Client));
return IRC_WriteStrClient(Client, RPL_UNAWAY_MSG,
Client_ID( Client));
}
} /* IRC_AWAY */
/**
* Add entries to channel invite, ban and exception lists.
*
* @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
* @param Prefix The originator of the command.
* @param Client The sender of the command.
* @param Channel The channel of which the list should be modified.
* @param Pattern The pattern to add to the list.
* @return CONNECTED or DISCONNECTED.
*/
static bool
Add_To_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
const char *Pattern)
{
char mask[MASK_LEN];
struct list_head *list = NULL;
long int current_count;
assert(Client != NULL);
assert(Channel != NULL);
assert(Pattern != NULL);
assert(what == 'I' || what == 'b' || what == 'e');
Lists_MakeMask(Pattern, mask, sizeof(mask));
current_count = Lists_Count(Channel_GetListInvites(Channel))
+ Lists_Count(Channel_GetListExcepts(Channel))
+ Lists_Count(Channel_GetListBans(Channel));
switch(what) {
case 'I':
list = Channel_GetListInvites(Channel);
break;