]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
Reorder checks
[ngircd-alex.git] / src / ngircd / irc-mode.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC commands for mode changes (like MODE, AWAY, etc.)
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "defines.h"
26 #include "conn.h"
27 #include "channel.h"
28 #include "irc-macros.h"
29 #include "irc-write.h"
30 #include "lists.h"
31 #include "log.h"
32 #include "parse.h"
33 #include "messages.h"
34 #include "conf.h"
35
36 #include "exp.h"
37 #include "irc-mode.h"
38
39 static bool Client_Mode PARAMS((CLIENT *Client, REQUEST *Req, CLIENT *Origin,
40                                 CLIENT *Target));
41 static bool Channel_Mode PARAMS((CLIENT *Client, REQUEST *Req, CLIENT *Origin,
42                                  CHANNEL *Channel));
43
44 static bool Add_To_List PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
45                                 CHANNEL *Channel, const char *Pattern));
46 static bool Del_From_List PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
47                                   CHANNEL *Channel, const char *Pattern));
48
49 static bool Send_ListChange PARAMS((const bool IsAdd, const char ModeChar,
50                                     CLIENT *Prefix, CLIENT *Client,
51                                     CHANNEL *Channel, const char *Mask));
52
53 /**
54  * Handler for the IRC "MODE" command.
55  *
56  * This function detects whether user or channel modes should be modified
57  * and calls the appropriate sub-functions.
58  *
59  * @param Client The client from which this command has been received.
60  * @param Req Request structure with prefix and all parameters.
61  * @return CONNECTED or DISCONNECTED.
62  */
63 GLOBAL bool
64 IRC_MODE( CLIENT *Client, REQUEST *Req )
65 {
66         CLIENT *cl, *origin;
67         CHANNEL *chan;
68
69         assert(Client != NULL);
70         assert(Req != NULL);
71
72         _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
73         _IRC_GET_SENDER_OR_RETURN_(origin, Req, Client)
74
75         /* Channel or user mode? */
76         cl = NULL; chan = NULL;
77         if (Client_IsValidNick(Req->argv[0]))
78                 cl = Client_Search(Req->argv[0]);
79         if (Channel_IsValidName(Req->argv[0]))
80                 chan = Channel_Search(Req->argv[0]);
81
82         if (cl)
83                 return Client_Mode(Client, Req, origin, cl);
84         if (chan)
85                 return Channel_Mode(Client, Req, origin, chan);
86
87         /* No target found! */
88         return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
89                         Client_ID(Client), Req->argv[0]);
90 } /* IRC_MODE */
91
92 /**
93  * Check if the "mode limit" for a client has been reached.
94  *
95  * This limit doesn't apply for servers or services!
96  *
97  * @param Client The client to check.
98  * @param Count The number of modes already handled.
99  * @return true if the limit has been reached.
100  */
101 static bool
102 Mode_Limit_Reached(CLIENT *Client, int Count)
103 {
104         if (Client_Type(Client) == CLIENT_SERVER
105             || Client_Type(Client) == CLIENT_SERVICE)
106                 return false;
107         if (Count < MAX_HNDL_MODES_ARG)
108                 return false;
109         return true;
110 }
111
112 /**
113  * Handle client mode requests
114  *
115  * @param Client The client from which this command has been received.
116  * @param Req Request structure with prefix and all parameters.
117  * @param Origin The originator of the MODE command (prefix).
118  * @param Target The target (client) of this MODE command.
119  * @return CONNECTED or DISCONNECTED.
120  */
121 static bool
122 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
123 {
124         char the_modes[COMMAND_LEN], x[2], *mode_ptr;
125         bool ok, set;
126         bool send_RPL_HOSTHIDDEN_MSG = false;
127         int mode_arg;
128         size_t len;
129
130         /* Is the client allowed to request or change the modes? */
131         if (Client_Type(Client) == CLIENT_USER) {
132                 /* Users are only allowed to manipulate their own modes! */
133                 if (Target != Client)
134                         return IRC_WriteErrClient(Client,
135                                                   ERR_USERSDONTMATCH_MSG,
136                                                   Client_ID(Client));
137         }
138
139         /* Mode request: let's answer it :-) */
140         if (Req->argc == 1)
141                 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
142                                           Client_ID(Target),
143                                           Client_Modes(Target));
144
145         mode_arg = 1;
146         mode_ptr = Req->argv[mode_arg];
147
148         /* Initial state: set or unset modes? */
149         if (*mode_ptr == '+') {
150                 set = true;
151                 strcpy(the_modes, "+");
152         } else if (*mode_ptr == '-') {
153                 set = false;
154                 strcpy(the_modes, "-");
155         } else
156                 return IRC_WriteErrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
157                                           Client_ID(Origin));
158
159         x[1] = '\0';
160         ok = CONNECTED;
161         while (mode_ptr) {
162                 mode_ptr++;
163                 if (!*mode_ptr) {
164                         /* Try next argument if there's any */
165                         mode_arg++;
166                         if (mode_arg < Req->argc)
167                                 mode_ptr = Req->argv[mode_arg];
168                         else
169                                 break;
170                 }
171
172                 switch(*mode_ptr) {
173                   case '+':
174                   case '-':
175                         if ((*mode_ptr == '+' && !set)
176                             || (*mode_ptr == '-' && set)) {
177                                 /* Action modifier ("+"/"-") must be changed */
178                                 len = strlen(the_modes) - 1;
179                                 if (the_modes[len] == '+'
180                                     || the_modes[len] == '-') {
181                                         /* Last character in the "result
182                                          * string" was an "action", so just
183                                          * overwrite it with the new action */
184                                         the_modes[len] = *mode_ptr;
185                                 } else {
186                                         /* Append new modifier character to
187                                          * the resulting mode string */
188                                         x[0] = *mode_ptr;
189                                         strlcat(the_modes, x,
190                                                 sizeof(the_modes));
191                                 }
192                                 if (*mode_ptr == '+')
193                                         set = true;
194                                 else
195                                         set = false;
196                         }
197                         continue;
198                 }
199
200                 /* Validate modes */
201                 x[0] = '\0';
202                 switch (*mode_ptr) {
203                 case 'b': /* Block private msgs */
204                 case 'C': /* Only messages from clients sharing a channel */
205                 case 'i': /* Invisible */
206                 case 's': /* Server messages */
207                 case 'w': /* Wallops messages */
208                         x[0] = *mode_ptr;
209                         break;
210                 case 'a': /* Away */
211                         if (Client_Type(Client) == CLIENT_SERVER) {
212                                 x[0] = 'a';
213                                 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
214                         } else
215                                 ok = IRC_WriteErrClient(Origin,
216                                                         ERR_NOPRIVILEGES_MSG,
217                                                         Client_ID(Origin));
218                         break;
219                 case 'B': /* Bot */
220                         if (Client_HasMode(Client, 'r'))
221                                 ok = IRC_WriteErrClient(Origin,
222                                                         ERR_RESTRICTED_MSG,
223                                                         Client_ID(Origin));
224                         else
225                                 x[0] = 'B';
226                         break;
227                 case 'c': /* Receive connect notices
228                            * (only settable by IRC operators!) */
229                         if (!set || Client_Type(Client) == CLIENT_SERVER
230                             || Client_OperByMe(Origin))
231                                 x[0] = 'c';
232                         else
233                                 ok = IRC_WriteErrClient(Origin,
234                                                         ERR_NOPRIVILEGES_MSG,
235                                                         Client_ID(Origin));
236                         break;
237                 case 'o': /* IRC operator (only unsettable!) */
238                         if (!set || Client_Type(Client) == CLIENT_SERVER) {
239                                 Client_SetOperByMe(Target, false);
240                                 x[0] = 'o';
241                         } else
242                                 ok = IRC_WriteErrClient(Origin,
243                                                         ERR_NOPRIVILEGES_MSG,
244                                                         Client_ID(Origin));
245                         break;
246                 case 'q': /* KICK-protected user */
247                         if (!set || Client_Type(Client) == CLIENT_SERVER
248                             || Client_OperByMe(Origin))
249                                 x[0] = 'q';
250                         else
251                                 ok = IRC_WriteErrClient(Origin,
252                                                         ERR_NOPRIVILEGES_MSG,
253                                                         Client_ID(Origin));
254                         break;
255                 case 'r': /* Restricted (only settable) */
256                         if (set || Client_Type(Client) == CLIENT_SERVER)
257                                 x[0] = 'r';
258                         else
259                                 ok = IRC_WriteErrClient(Origin,
260                                                         ERR_RESTRICTED_MSG,
261                                                         Client_ID(Origin));
262                         break;
263                 case 'R': /* Registered (not [un]settable by clients) */
264                         if (Client_Type(Client) == CLIENT_SERVER)
265                                 x[0] = 'R';
266                         else
267                                 ok = IRC_WriteErrClient(Origin,
268                                                         ERR_NICKREGISTER_MSG,
269                                                         Client_ID(Origin));
270                         break;
271                 case 'x': /* Cloak hostname */
272                         if (Client_HasMode(Client, 'r'))
273                                 ok = IRC_WriteErrClient(Origin,
274                                                         ERR_RESTRICTED_MSG,
275                                                         Client_ID(Origin));
276                         else if (!set || Conf_CloakHostModeX[0]
277                                  || Client_Type(Client) == CLIENT_SERVER
278                                  || Client_OperByMe(Client)) {
279                                 x[0] = 'x';
280                                 send_RPL_HOSTHIDDEN_MSG = true;
281                         } else
282                                 ok = IRC_WriteErrClient(Origin,
283                                                         ERR_NOPRIVILEGES_MSG,
284                                                         Client_ID(Origin));
285                         break;
286                 default:
287                         if (Client_Type(Client) != CLIENT_SERVER) {
288                                 Log(LOG_DEBUG,
289                                     "Unknown mode \"%c%c\" from \"%s\"!?",
290                                     set ? '+' : '-', *mode_ptr,
291                                     Client_ID(Origin));
292                                 ok = IRC_WriteErrClient(Origin,
293                                                         ERR_UMODEUNKNOWNFLAG2_MSG,
294                                                         Client_ID(Origin),
295                                                         set ? '+' : '-',
296                                                         *mode_ptr);
297                                 x[0] = '\0';
298                         } else {
299                                 Log(LOG_DEBUG,
300                                     "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
301                                     set ? '+' : '-', *mode_ptr,
302                                     Client_ID(Origin), Client_ID(Target));
303                                 x[0] = *mode_ptr;
304                         }
305                 }
306
307                 if (!ok)
308                         break;
309
310                 /* Is there a valid mode change? */
311                 if (!x[0])
312                         continue;
313
314                 if (set) {
315                         if (Client_ModeAdd(Target, x[0]))
316                                 strlcat(the_modes, x, sizeof(the_modes));
317                 } else {
318                         if (Client_ModeDel(Target, x[0]))
319                                 strlcat(the_modes, x, sizeof(the_modes));
320                 }
321         }
322
323         /* Are there changed modes? */
324         if (the_modes[1]) {
325                 /* Remove needless action modifier characters */
326                 len = strlen(the_modes) - 1;
327                 if (the_modes[len] == '+' || the_modes[len] == '-')
328                         the_modes[len] = '\0';
329
330                 if (Client_Type(Client) == CLIENT_SERVER) {
331                         /* Forward modes to other servers */
332                         if (Client_Conn(Target) != NONE) {
333                                 /* Remote server (service?) changed modes
334                                  * for one of our clients. Inform it! */
335                                 IRC_WriteStrClientPrefix(Target, Origin,
336                                                          "MODE %s :%s",
337                                                          Client_ID(Target),
338                                                          the_modes);
339                         }
340                         IRC_WriteStrServersPrefix(Client, Origin,
341                                                   "MODE %s :%s",
342                                                   Client_ID(Target),
343                                                   the_modes);
344                 } else {
345                         /* Send reply to client and inform other servers */
346                         ok = IRC_WriteStrClientPrefix(Client, Origin,
347                                                       "MODE %s :%s",
348                                                       Client_ID(Target),
349                                                       the_modes);
350                         IRC_WriteStrServersPrefix(Client, Origin,
351                                                   "MODE %s :%s",
352                                                   Client_ID(Target),
353                                                   the_modes);
354                 }
355
356                 if (send_RPL_HOSTHIDDEN_MSG && Client_Conn(Target) > NONE) {
357                         /* A new (cloaked) hostname must be announced */
358                         IRC_WriteStrClientPrefix(Target, Origin,
359                                                  RPL_HOSTHIDDEN_MSG,
360                                                  Client_ID(Target),
361                                                  Client_HostnameDisplayed(Target));
362
363                 }
364
365                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
366                          Client_TypeText(Target), Client_Mask(Target),
367                          Client_Modes(Target));
368         }
369
370         IRC_SetPenalty(Client, 1);
371         return ok;
372 } /* Client_Mode */
373
374 /*
375  * Reply to a channel mode request.
376  *
377  * @param Origin The originator of the MODE command (prefix).
378  * @param Channel The channel of which the modes should be sent.
379  * @return CONNECTED or DISCONNECTED.
380  */
381 static bool
382 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
383 {
384         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
385         const char *mode_ptr;
386
387         /* Member or not? -- That's the question! */
388         if (!Channel_IsMemberOf(Channel, Origin))
389                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
390                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
391
392         /* The sender is a member: generate extended reply */
393         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
394         mode_ptr = the_modes;
395         the_args[0] = '\0';
396
397         while(*mode_ptr) {
398                 switch(*mode_ptr) {
399                 case 'l':
400                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
401                         strlcat(the_args, argadd, sizeof(the_args));
402                         break;
403                 case 'k':
404                         strlcat(the_args, " ", sizeof(the_args));
405                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
406                         break;
407                 }
408                 mode_ptr++;
409         }
410         if (the_args[0])
411                 strlcat(the_modes, the_args, sizeof(the_modes));
412
413         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
414                                 Client_ID(Origin), Channel_Name(Channel),
415                                 the_modes))
416                 return DISCONNECTED;
417 #ifndef STRICT_RFC
418         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
419                                   Client_ID(Origin), Channel_Name(Channel),
420                                   Channel_CreationTime(Channel)))
421                 return DISCONNECTED;
422 #endif
423         return CONNECTED;
424 }
425
426 /**
427  * Handle channel mode and channel-user mode changes
428  *
429  * @param Client The client from which this command has been received.
430  * @param Req Request structure with prefix and all parameters.
431  * @param Origin The originator of the MODE command (prefix).
432  * @param Channel The target channel of this MODE command.
433  * @return CONNECTED or DISCONNECTED.
434  */
435 static bool
436 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
437 {
438         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
439             argadd[CLIENT_PASS_LEN], *mode_ptr;
440         bool connected, set, skiponce, retval, use_servermode,
441              is_halfop, is_op, is_admin, is_owner, is_machine, is_oper;
442         int mode_arg, arg_arg, mode_arg_count = 0;
443         CLIENT *client;
444         long l;
445         size_t len;
446
447         is_halfop = is_op = is_admin = is_owner = is_machine = is_oper = false;
448
449         if (Channel_IsModeless(Channel))
450                 return IRC_WriteErrClient(Client, ERR_NOCHANMODES_MSG,
451                                 Client_ID(Client), Channel_Name(Channel));
452
453         /* Mode request: let's answer it :-) */
454         if (Req->argc <= 1)
455                 return Channel_Mode_Answer_Request(Origin, Channel);
456
457         /* Check if origin is oper and opers can use mode */
458         use_servermode = Conf_OperServerMode;
459         if(Client_OperByMe(Client) && Conf_OperCanMode) {
460                 is_oper = true;
461         }
462
463         /* Check if client is a server/service */
464         if(Client_Type(Client) == CLIENT_SERVER ||
465            Client_Type(Client) == CLIENT_SERVICE) {
466                 is_machine = true;
467         }
468
469         /* Check if client is member of channel or an oper or an server/service */
470         if(!Channel_IsMemberOf(Channel, Client) && !is_oper && !is_machine)
471                 return IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
472                                           Client_ID(Origin),
473                                           Channel_Name(Channel));
474
475         mode_arg = 1;
476         mode_ptr = Req->argv[mode_arg];
477         if (Req->argc > mode_arg + 1)
478                 arg_arg = mode_arg + 1;
479         else
480                 arg_arg = -1;
481
482         /* Initial state: set or unset modes? */
483         skiponce = false;
484         switch (*mode_ptr) {
485         case '-':
486                 set = false;
487                 break;
488         case '+':
489                 set = true;
490                 break;
491         default:
492                 set = true;
493                 skiponce = true;
494         }
495
496         /* Prepare reply string */
497         strcpy(the_modes, set ? "+" : "-");
498         the_args[0] = '\0';
499
500         x[1] = '\0';
501         connected = CONNECTED;
502         while (mode_ptr) {
503                 if (!skiponce)
504                         mode_ptr++;
505                 if (!*mode_ptr) {
506                         /* Try next argument if there's any */
507                         if (arg_arg < 0)
508                                 break;
509                         if (arg_arg > mode_arg)
510                                 mode_arg = arg_arg;
511                         else
512                                 mode_arg++;
513
514                         if (mode_arg >= Req->argc)
515                                 break;
516                         mode_ptr = Req->argv[mode_arg];
517
518                         if (Req->argc > mode_arg + 1)
519                                 arg_arg = mode_arg + 1;
520                         else
521                                 arg_arg = -1;
522                 }
523                 skiponce = false;
524
525                 switch (*mode_ptr) {
526                 case '+':
527                 case '-':
528                         if (((*mode_ptr == '+') && !set)
529                             || ((*mode_ptr == '-') && set)) {
530                                 /* Action modifier ("+"/"-") must be changed ... */
531                                 len = strlen(the_modes) - 1;
532                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
533                                         /* Adjust last action modifier in result */
534                                         the_modes[len] = *mode_ptr;
535                                 } else {
536                                         /* Append modifier character to result string */
537                                         x[0] = *mode_ptr;
538                                         strlcat(the_modes, x, sizeof(the_modes));
539                                 }
540                                 set = *mode_ptr == '+';
541                         }
542                         continue;
543                 }
544
545                 /* Are there arguments left? */
546                 if (arg_arg >= Req->argc)
547                         arg_arg = -1;
548
549                 if(!is_machine && !is_oper) {
550                         if (Channel_UserHasMode(Channel, Client, 'q'))
551                                 is_owner = true;
552                         if (Channel_UserHasMode(Channel, Client, 'a'))
553                                 is_admin = true;
554                         if (Channel_UserHasMode(Channel, Client, 'o'))
555                                 is_op = true;
556                         if (Channel_UserHasMode(Channel, Client, 'h'))
557                                 is_halfop = true;
558                 }
559
560                 /* Validate modes */
561                 x[0] = '\0';
562                 argadd[0] = '\0';
563                 client = NULL;
564                 switch (*mode_ptr) {
565                 /* --- Channel modes --- */
566                 case 'R': /* Registered users only */
567                 case 's': /* Secret channel */
568                 case 'z': /* Secure connections only */
569                         if(!is_oper && !is_machine && !is_owner &&
570                            !is_admin && !is_op) {
571                                 connected = IRC_WriteErrClient(Origin,
572                                         ERR_CHANOPRIVSNEEDED_MSG,
573                                         Client_ID(Origin), Channel_Name(Channel));
574                                 goto chan_exit;
575                         }
576                 case 'i': /* Invite only */
577                 case 'V': /* Invite disallow */
578                 case 'M': /* Only identified nicks can write */
579                 case 'm': /* Moderated */
580                 case 'n': /* Only members can write */
581                 case 'Q': /* No kicks */
582                 case 't': /* Topic locked */
583                         if(is_oper || is_machine || is_owner ||
584                            is_admin || is_op || is_halfop)
585                                 x[0] = *mode_ptr;
586                         else
587                                 connected = IRC_WriteErrClient(Origin,
588                                         ERR_CHANOPRIVSNEEDED_MSG,
589                                         Client_ID(Origin), Channel_Name(Channel));
590                         break;
591                 case 'k': /* Channel key */
592                         if (Mode_Limit_Reached(Client, mode_arg_count++))
593                                 goto chan_exit;
594                         if (!set) {
595                                 if (is_oper || is_machine || is_owner ||
596                                     is_admin || is_op || is_halfop)
597                                         x[0] = *mode_ptr;
598                                 else
599                                         connected = IRC_WriteErrClient(Origin,
600                                                 ERR_CHANOPRIVSNEEDED_MSG,
601                                                 Client_ID(Origin),
602                                                 Channel_Name(Channel));
603                                 break;
604                         }
605                         if (arg_arg > mode_arg) {
606                                 if (is_oper || is_machine || is_owner ||
607                                     is_admin || is_op || is_halfop) {
608                                         Channel_ModeDel(Channel, 'k');
609                                         Channel_SetKey(Channel,
610                                                        Req->argv[arg_arg]);
611                                         strlcpy(argadd, Channel_Key(Channel),
612                                                 sizeof(argadd));
613                                         x[0] = *mode_ptr;
614                                 } else {
615                                         connected = IRC_WriteErrClient(Origin,
616                                                 ERR_CHANOPRIVSNEEDED_MSG,
617                                                 Client_ID(Origin),
618                                                 Channel_Name(Channel));
619                                 }
620                                 Req->argv[arg_arg][0] = '\0';
621                                 arg_arg++;
622                         } else {
623 #ifdef STRICT_RFC
624                                 /* Only send error message in "strict" mode,
625                                  * this is how ircd2.11 and others behave ... */
626                                 connected = IRC_WriteErrClient(Origin,
627                                         ERR_NEEDMOREPARAMS_MSG,
628                                         Client_ID(Origin), Req->command);
629 #endif
630                                 goto chan_exit;
631                         }
632                         break;
633                 case 'l': /* Member limit */
634                         if (Mode_Limit_Reached(Client, mode_arg_count++))
635                                 goto chan_exit;
636                         if (!set) {
637                                 if (is_oper || is_machine || is_owner ||
638                                     is_admin || is_op || is_halfop)
639                                         x[0] = *mode_ptr;
640                                 else
641                                         connected = IRC_WriteErrClient(Origin,
642                                                 ERR_CHANOPRIVSNEEDED_MSG,
643                                                 Client_ID(Origin),
644                                                 Channel_Name(Channel));
645                                 break;
646                         }
647                         if (arg_arg > mode_arg) {
648                                 if (is_oper || is_machine || is_owner ||
649                                     is_admin || is_op || is_halfop) {
650                                         l = atol(Req->argv[arg_arg]);
651                                         if (l > 0 && l < 0xFFFF) {
652                                                 Channel_ModeDel(Channel, 'l');
653                                                 Channel_SetMaxUsers(Channel, l);
654                                                 snprintf(argadd, sizeof(argadd),
655                                                          "%ld", l);
656                                                 x[0] = *mode_ptr;
657                                         }
658                                 } else {
659                                         connected = IRC_WriteErrClient(Origin,
660                                                 ERR_CHANOPRIVSNEEDED_MSG,
661                                                 Client_ID(Origin),
662                                                 Channel_Name(Channel));
663                                 }
664                                 Req->argv[arg_arg][0] = '\0';
665                                 arg_arg++;
666                         } else {
667 #ifdef STRICT_RFC
668                                 /* Only send error message in "strict" mode,
669                                  * this is how ircd2.11 and others behave ... */
670                                 connected = IRC_WriteErrClient(Origin,
671                                         ERR_NEEDMOREPARAMS_MSG,
672                                         Client_ID(Origin), Req->command);
673 #endif
674                                 goto chan_exit;
675                         }
676                         break;
677                 case 'O': /* IRC operators only */
678                         if (set) {
679                                 /* Only IRC operators are allowed to
680                                  * set the 'O' channel mode! */
681                                 if(is_oper || is_machine)
682                                         x[0] = 'O';
683                                 else
684                                         connected = IRC_WriteErrClient(Origin,
685                                                 ERR_NOPRIVILEGES_MSG,
686                                                 Client_ID(Origin));
687                         } else if(is_oper || is_machine || is_owner ||
688                                   is_admin || is_op)
689                                 x[0] = 'O';
690                         else
691                                 connected = IRC_WriteErrClient(Origin,
692                                         ERR_CHANOPRIVSNEEDED_MSG,
693                                         Client_ID(Origin),
694                                         Channel_Name(Channel));
695                         break;
696                 case 'P': /* Persistent channel */
697                         if (set) {
698                                 /* Only IRC operators are allowed to
699                                  * set the 'P' channel mode! */
700                                 if(is_oper || is_machine)
701                                         x[0] = 'P';
702                                 else
703                                         connected = IRC_WriteErrClient(Origin,
704                                                 ERR_NOPRIVILEGES_MSG,
705                                                 Client_ID(Origin));
706                         } else if(is_oper || is_machine || is_owner ||
707                                   is_admin || is_op)
708                                 x[0] = 'P';
709                         else
710                                 connected = IRC_WriteErrClient(Origin,
711                                         ERR_CHANOPRIVSNEEDED_MSG,
712                                         Client_ID(Origin),
713                                         Channel_Name(Channel));
714                         break;
715                 /* --- Channel user modes --- */
716                 case 'q': /* Owner */
717                 case 'a': /* Channel admin */
718                         if(!is_oper && !is_machine && !is_owner && !is_admin) {
719                                 connected = IRC_WriteErrClient(Origin,
720                                         ERR_CHANOPPRIVTOOLOW_MSG,
721                                         Client_ID(Origin),
722                                         Channel_Name(Channel));
723                                 goto chan_exit;
724                         }
725                 case 'o': /* Channel operator */
726                         if(!is_oper && !is_machine && !is_owner &&
727                            !is_admin && !is_op) {
728                                 connected = IRC_WriteErrClient(Origin,
729                                         ERR_CHANOPRIVSNEEDED_MSG,
730                                         Client_ID(Origin),
731                                         Channel_Name(Channel));
732                                 goto chan_exit;
733                         }
734                 case 'h': /* Half Op */
735                         if(!is_oper && !is_machine && !is_owner &&
736                            !is_admin && !is_op) {
737                                 connected = IRC_WriteErrClient(Origin,
738                                         ERR_CHANOPRIVSNEEDED_MSG,
739                                         Client_ID(Origin),
740                                         Channel_Name(Channel));
741                                 goto chan_exit;
742                         }
743                 case 'v': /* Voice */
744                         if (arg_arg > mode_arg) {
745                                 if (is_oper || is_machine || is_owner ||
746                                     is_admin || is_op || is_halfop) {
747                                         client = Client_Search(Req->argv[arg_arg]);
748                                         if (client)
749                                                 x[0] = *mode_ptr;
750                                         else
751                                                 connected = IRC_WriteErrClient(Origin,
752                                                         ERR_NOSUCHNICK_MSG,
753                                                         Client_ID(Origin),
754                                                         Req->argv[arg_arg]);
755                                 } else {
756                                         connected = IRC_WriteErrClient(Origin,
757                                                 ERR_CHANOPRIVSNEEDED_MSG,
758                                                 Client_ID(Origin),
759                                                 Channel_Name(Channel));
760                                 }
761                                 Req->argv[arg_arg][0] = '\0';
762                                 arg_arg++;
763                         } else {
764 #ifdef STRICT_RFC
765                                 /* Report an error to the client, when a user
766                                  * mode should be changed but no nickname is
767                                  * given. But don't do it when not in "strict"
768                                  * mode, because most other servers don't do
769                                  * it as well and some clients send "wired"
770                                  * MODE commands like "MODE #chan -ooo nick". */
771                                 connected = IRC_WriteErrClient(Origin,
772                                         ERR_NEEDMOREPARAMS_MSG,
773                                         Client_ID(Origin), Req->command);
774 #endif
775                                 goto chan_exit;
776                         }
777                         break;
778                 /* --- Channel lists --- */
779                 case 'I': /* Invite lists */
780                 case 'b': /* Ban lists */
781                 case 'e': /* Channel exception lists */
782                         if (Mode_Limit_Reached(Client, mode_arg_count++))
783                                 goto chan_exit;
784                         if (arg_arg > mode_arg) {
785                                 /* modify list */
786                                 if (is_oper || is_machine || is_owner ||
787                                     is_admin || is_op || is_halfop) {
788                                         connected = set
789                                            ? Add_To_List(*mode_ptr, Origin,
790                                                 Client, Channel,
791                                                 Req->argv[arg_arg])
792                                            : Del_From_List(*mode_ptr, Origin,
793                                                 Client, Channel,
794                                                 Req->argv[arg_arg]);
795                                 } else {
796                                         connected = IRC_WriteErrClient(Origin,
797                                                 ERR_CHANOPRIVSNEEDED_MSG,
798                                                 Client_ID(Origin),
799                                                 Channel_Name(Channel));
800                                 }
801                                 Req->argv[arg_arg][0] = '\0';
802                                 arg_arg++;
803                         } else {
804                                 switch (*mode_ptr) {
805                                 case 'I':
806                                         Channel_ShowInvites(Origin, Channel);
807                                         break;
808                                 case 'b':
809                                         Channel_ShowBans(Origin, Channel);
810                                         break;
811                                 case 'e':
812                                         Channel_ShowExcepts(Origin, Channel);
813                                         break;
814                                 }
815                         }
816                         break;
817                 default:
818                         if (Client_Type(Client) != CLIENT_SERVER) {
819                                 Log(LOG_DEBUG,
820                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
821                                     set ? '+' : '-', *mode_ptr,
822                                     Client_ID(Origin), Channel_Name(Channel));
823                                 connected = IRC_WriteErrClient(Origin,
824                                         ERR_UNKNOWNMODE_MSG,
825                                         Client_ID(Origin), *mode_ptr,
826                                         Channel_Name(Channel));
827                                 x[0] = '\0';
828                         } else {
829                                 Log(LOG_DEBUG,
830                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
831                                     set ? '+' : '-', *mode_ptr,
832                                     Client_ID(Origin), Channel_Name(Channel));
833                                 x[0] = *mode_ptr;
834                         }
835                 }
836
837                 if (!connected)
838                         break;
839
840                 /* Is there a valid mode change? */
841                 if (!x[0])
842                         continue;
843
844                 /* Validate target client */
845                 if (client && (!Channel_IsMemberOf(Channel, client))) {
846                         if (!IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
847                                                 Client_ID(Origin),
848                                                 Client_ID(client),
849                                                 Channel_Name(Channel)))
850                                 break;
851                         continue;
852                 }
853
854                 if (client) {
855                         /* Channel-User-Mode */
856                         retval = set
857                                ? Channel_UserModeAdd(Channel, client, x[0])
858                                : Channel_UserModeDel(Channel, client, x[0]);
859                         if (retval) {
860                                 strlcat(the_args, " ", sizeof(the_args));
861                                 strlcat(the_args, Client_ID(client),
862                                         sizeof(the_args));
863                                 strlcat(the_modes, x, sizeof(the_modes));
864                                 LogDebug
865                                     ("User \"%s\": Mode change on %s, now \"%s\"",
866                                      Client_Mask(client), Channel_Name(Channel),
867                                      Channel_UserModes(Channel, client));
868                         }
869                 } else {
870                         /* Channel-Mode */
871                         retval = set
872                                ? Channel_ModeAdd(Channel, x[0])
873                                : Channel_ModeDel(Channel, x[0]);
874                         if (retval) {
875                                 strlcat(the_modes, x, sizeof(the_modes));
876                                 LogDebug("Channel %s: Mode change, now \"%s\".",
877                                          Channel_Name(Channel),
878                                          Channel_Modes(Channel));
879                         }
880                 }
881
882                 /* Are there additional arguments to add? */
883                 if (argadd[0]) {
884                         strlcat(the_args, " ", sizeof(the_args));
885                         strlcat(the_args, argadd, sizeof(the_args));
886                 }
887         }
888
889       chan_exit:
890         /* Are there changed modes? */
891         if (the_modes[1]) {
892                 /* Clean up mode string */
893                 len = strlen(the_modes) - 1;
894                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
895                         the_modes[len] = '\0';
896
897                 if (Client_Type(Client) == CLIENT_SERVER) {
898                         /* MODE requests for local channels from other servers
899                          * are definitely invalid! */
900                         if (Channel_IsLocal(Channel)) {
901                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
902                                 return CONNECTED;
903                         }
904
905                         /* Forward mode changes to channel users and all the
906                          * other remote servers: */
907                         IRC_WriteStrServersPrefix(Client, Origin,
908                                 "MODE %s %s%s", Channel_Name(Channel),
909                                 the_modes, the_args);
910                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
911                                 false, "MODE %s %s%s", Channel_Name(Channel),
912                                 the_modes, the_args);
913                 } else {
914                         if (use_servermode)
915                                 Origin = Client_ThisServer();
916                         /* Send reply to client and inform other servers and channel users */
917                         connected = IRC_WriteStrClientPrefix(Client, Origin,
918                                         "MODE %s %s%s", Channel_Name(Channel),
919                                         the_modes, the_args);
920                         /* Only forward requests for non-local channels */
921                         if (!Channel_IsLocal(Channel))
922                                 IRC_WriteStrServersPrefix(Client, Origin,
923                                         "MODE %s %s%s", Channel_Name(Channel),
924                                         the_modes, the_args);
925                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
926                                 false, "MODE %s %s%s", Channel_Name(Channel),
927                                 the_modes, the_args);
928                 }
929         }
930
931         IRC_SetPenalty(Client, 1);
932         return connected;
933 } /* Channel_Mode */
934
935 /**
936  * Handler for the IRC "AWAY" command.
937  *
938  * @param Client The client from which this command has been received.
939  * @param Req Request structure with prefix and all parameters.
940  * @return CONNECTED or DISCONNECTED.
941  */
942 GLOBAL bool
943 IRC_AWAY( CLIENT *Client, REQUEST *Req )
944 {
945         assert (Client != NULL);
946         assert (Req != NULL);
947
948         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
949
950         if (Req->argc == 1 && Req->argv[0][0]) {
951                 Client_SetAway(Client, Req->argv[0]);
952                 Client_ModeAdd(Client, 'a');
953                 IRC_WriteStrServersPrefix(Client, Client, "MODE %s :+a",
954                                           Client_ID( Client));
955                 return IRC_WriteStrClient(Client, RPL_NOWAWAY_MSG,
956                                           Client_ID( Client));
957         } else {
958                 Client_ModeDel(Client, 'a');
959                 IRC_WriteStrServersPrefix(Client, Client, "MODE %s :-a",
960                                           Client_ID( Client));
961                 return IRC_WriteStrClient(Client, RPL_UNAWAY_MSG,
962                                           Client_ID( Client));
963         }
964 } /* IRC_AWAY */
965
966 /**
967  * Add entries to channel invite, ban and exception lists.
968  *
969  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
970  * @param Prefix The originator of the command.
971  * @param Client The sender of the command.
972  * @param Channel The channel of which the list should be modified.
973  * @param Pattern The pattern to add to the list.
974  * @return CONNECTED or DISCONNECTED.
975  */
976 static bool
977 Add_To_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
978             const char *Pattern)
979 {
980         char mask[MASK_LEN];
981         struct list_head *list = NULL;
982         long int current_count;
983
984         assert(Client != NULL);
985         assert(Channel != NULL);
986         assert(Pattern != NULL);
987         assert(what == 'I' || what == 'b' || what == 'e');
988
989         Lists_MakeMask(Pattern, mask, sizeof(mask));
990         current_count = Lists_Count(Channel_GetListInvites(Channel))
991                         + Lists_Count(Channel_GetListExcepts(Channel))
992                         + Lists_Count(Channel_GetListBans(Channel));
993
994         switch(what) {
995                 case 'I':
996                         list = Channel_GetListInvites(Channel);
997                         break;
998                 case 'b':
999                         list = Channel_GetListBans(Channel);
1000                         break;
1001                 case 'e':
1002                         list = Channel_GetListExcepts(Channel);
1003                         break;
1004         }
1005
1006         if (Lists_CheckDupeMask(list, mask))
1007                 return CONNECTED;
1008         if (Client_Type(Client) == CLIENT_USER &&
1009             current_count >= MAX_HNDL_CHANNEL_LISTS)
1010                 return IRC_WriteErrClient(Client, ERR_LISTFULL_MSG,
1011                                           Client_ID(Client),
1012                                           Channel_Name(Channel), mask,
1013                                           MAX_HNDL_CHANNEL_LISTS);
1014
1015         switch (what) {
1016                 case 'I':
1017                         if (!Channel_AddInvite(Channel, mask, false))
1018                                 return CONNECTED;
1019                         break;
1020                 case 'b':
1021                         if (!Channel_AddBan(Channel, mask))
1022                                 return CONNECTED;
1023                         break;
1024                 case 'e':
1025                         if (!Channel_AddExcept(Channel, mask))
1026                                 return CONNECTED;
1027                         break;
1028         }
1029         return Send_ListChange(true, what, Prefix, Client, Channel, mask);
1030 }
1031
1032 /**
1033  * Delete entries from channel invite, ban and exception lists.
1034  *
1035  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
1036  * @param Prefix The originator of the command.
1037  * @param Client The sender of the command.
1038  * @param Channel The channel of which the list should be modified.
1039  * @param Pattern The pattern to add to the list.
1040  * @return CONNECTED or DISCONNECTED.
1041  */
1042 static bool
1043 Del_From_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
1044               const char *Pattern)
1045 {
1046         char mask[MASK_LEN];
1047         struct list_head *list = NULL;
1048
1049         assert(Client != NULL);
1050         assert(Channel != NULL);
1051         assert(Pattern != NULL);
1052         assert(what == 'I' || what == 'b' || what == 'e');
1053
1054         Lists_MakeMask(Pattern, mask, sizeof(mask));
1055
1056         switch (what) {
1057                 case 'I':
1058                         list = Channel_GetListInvites(Channel);
1059                         break;
1060                 case 'b':
1061                         list = Channel_GetListBans(Channel);
1062                         break;
1063                 case 'e':
1064                         list = Channel_GetListExcepts(Channel);
1065                         break;
1066         }
1067
1068         if (!Lists_CheckDupeMask(list, mask))
1069                 return CONNECTED;
1070         Lists_Del(list, mask);
1071
1072         return Send_ListChange(false, what, Prefix, Client, Channel, mask);
1073 }
1074
1075 /**
1076  * Send information about changed channel invite/ban/exception lists to clients.
1077  *
1078  * @param IsAdd true if the list item has been added, false otherwise.
1079  * @param ModeChar The mode to use (e. g. 'b' or 'I')
1080  * @param Prefix The originator of the mode list change.
1081  * @param Client The sender of the command.
1082  * @param Channel The channel of which the list has been modified.
1083  * @param Mask The mask which has been added or removed.
1084  * @return CONNECTED or DISCONNECTED.
1085  */
1086 static bool
1087 Send_ListChange(const bool IsAdd, const char ModeChar, CLIENT *Prefix,
1088                 CLIENT *Client, CHANNEL *Channel, const char *Mask)
1089 {
1090         bool ok = true;
1091
1092         /* Send confirmation to the client */
1093         if (Client_Type(Client) == CLIENT_USER)
1094                 ok = IRC_WriteStrClientPrefix(Client, Prefix, "MODE %s %c%c %s",
1095                                               Channel_Name(Channel),
1096                                               IsAdd ? '+' : '-',
1097                                               ModeChar, Mask);
1098
1099         /* to other servers */
1100         IRC_WriteStrServersPrefix(Client, Prefix, "MODE %s %c%c %s",
1101                                   Channel_Name(Channel), IsAdd ? '+' : '-',
1102                                   ModeChar, Mask);
1103
1104         /* and local users in channel */
1105         IRC_WriteStrChannelPrefix(Client, Channel, Prefix, false,
1106                                   "MODE %s %c%c %s", Channel_Name(Channel),
1107                                   IsAdd ? '+' : '-', ModeChar, Mask );
1108
1109         return ok;
1110 } /* Send_ListChange */
1111
1112 /* -eof- */