]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
e2fa59a2df67aa5f306629e0b3db9030a26d7c1d
[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_GET_SENDER_OR_RETURN_(origin, Req, Client)
73
74         /* Channel or user mode? */
75         cl = NULL; chan = NULL;
76         if (Client_IsValidNick(Req->argv[0]))
77                 cl = Client_Search(Req->argv[0]);
78         if (Channel_IsValidName(Req->argv[0]))
79                 chan = Channel_Search(Req->argv[0]);
80
81         if (cl)
82                 return Client_Mode(Client, Req, origin, cl);
83         if (chan)
84                 return Channel_Mode(Client, Req, origin, chan);
85
86         /* No target found! */
87         return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
88                         Client_ID(Client), Req->argv[0]);
89 } /* IRC_MODE */
90
91 /**
92  * Check if the "mode limit" for a client has been reached.
93  *
94  * This limit doesn't apply for servers or services!
95  *
96  * @param Client The client to check.
97  * @param Count The number of modes already handled.
98  * @return true if the limit has been reached.
99  */
100 static bool
101 Mode_Limit_Reached(CLIENT *Client, int Count)
102 {
103         if (Client_Type(Client) == CLIENT_SERVER
104             || Client_Type(Client) == CLIENT_SERVICE)
105                 return false;
106         if (Count < MAX_HNDL_MODES_ARG)
107                 return false;
108         return true;
109 }
110
111 /**
112  * Handle client mode requests
113  *
114  * @param Client The client from which this command has been received.
115  * @param Req Request structure with prefix and all parameters.
116  * @param Origin The originator of the MODE command (prefix).
117  * @param Target The target (client) of this MODE command.
118  * @return CONNECTED or DISCONNECTED.
119  */
120 static bool
121 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
122 {
123         char the_modes[COMMAND_LEN], x[2], *mode_ptr;
124         bool ok, set;
125         bool send_RPL_HOSTHIDDEN_MSG = false;
126         int mode_arg;
127         size_t len;
128
129         /* Is the client allowed to request or change the modes? */
130         if (Client_Type(Client) == CLIENT_USER) {
131                 /* Users are only allowed to manipulate their own modes! */
132                 if (Target != Client)
133                         return IRC_WriteErrClient(Client,
134                                                   ERR_USERSDONTMATCH_MSG,
135                                                   Client_ID(Client));
136         }
137
138         /* Mode request: let's answer it :-) */
139         if (Req->argc == 1)
140                 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
141                                           Client_ID(Target),
142                                           Client_Modes(Target));
143
144         mode_arg = 1;
145         mode_ptr = Req->argv[mode_arg];
146
147         /* Initial state: set or unset modes? */
148         if (*mode_ptr == '+') {
149                 set = true;
150                 strcpy(the_modes, "+");
151         } else if (*mode_ptr == '-') {
152                 set = false;
153                 strcpy(the_modes, "-");
154         } else
155                 return IRC_WriteErrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
156                                           Client_ID(Origin));
157
158         x[1] = '\0';
159         ok = CONNECTED;
160         while (mode_ptr) {
161                 mode_ptr++;
162                 if (!*mode_ptr) {
163                         /* Try next argument if there's any */
164                         mode_arg++;
165                         if (mode_arg < Req->argc)
166                                 mode_ptr = Req->argv[mode_arg];
167                         else
168                                 break;
169                 }
170
171                 switch(*mode_ptr) {
172                   case '+':
173                   case '-':
174                         if ((*mode_ptr == '+' && !set)
175                             || (*mode_ptr == '-' && set)) {
176                                 /* Action modifier ("+"/"-") must be changed */
177                                 len = strlen(the_modes) - 1;
178                                 if (the_modes[len] == '+'
179                                     || the_modes[len] == '-') {
180                                         /* Last character in the "result
181                                          * string" was an "action", so just
182                                          * overwrite it with the new action */
183                                         the_modes[len] = *mode_ptr;
184                                 } else {
185                                         /* Append new modifier character to
186                                          * the resulting mode string */
187                                         x[0] = *mode_ptr;
188                                         strlcat(the_modes, x,
189                                                 sizeof(the_modes));
190                                 }
191                                 if (*mode_ptr == '+')
192                                         set = true;
193                                 else
194                                         set = false;
195                         }
196                         continue;
197                 }
198
199                 /* Validate modes */
200                 x[0] = '\0';
201                 switch (*mode_ptr) {
202                 case 'b': /* Block private msgs */
203                 case 'C': /* Only messages from clients sharing a channel */
204                 case 'i': /* Invisible */
205                 case 's': /* Server messages */
206                 case 'w': /* Wallops messages */
207                         x[0] = *mode_ptr;
208                         break;
209                 case 'a': /* Away */
210                         if (Client_Type(Client) == CLIENT_SERVER) {
211                                 x[0] = 'a';
212                                 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
213                         } else
214                                 ok = IRC_WriteErrClient(Origin,
215                                                         ERR_NOPRIVILEGES_MSG,
216                                                         Client_ID(Origin));
217                         break;
218                 case 'B': /* Bot */
219                         if (Client_HasMode(Client, 'r'))
220                                 ok = IRC_WriteErrClient(Origin,
221                                                         ERR_RESTRICTED_MSG,
222                                                         Client_ID(Origin));
223                         else
224                                 x[0] = 'B';
225                         break;
226                 case 'c': /* Receive connect notices
227                            * (only settable by IRC operators!) */
228                         if (!set || Client_Type(Client) == CLIENT_SERVER
229                             || Client_OperByMe(Origin))
230                                 x[0] = 'c';
231                         else
232                                 ok = IRC_WriteErrClient(Origin,
233                                                         ERR_NOPRIVILEGES_MSG,
234                                                         Client_ID(Origin));
235                         break;
236                 case 'o': /* IRC operator (only unsettable!) */
237                         if (!set || Client_Type(Client) == CLIENT_SERVER) {
238                                 Client_SetOperByMe(Target, false);
239                                 x[0] = 'o';
240                         } else
241                                 ok = IRC_WriteErrClient(Origin,
242                                                         ERR_NOPRIVILEGES_MSG,
243                                                         Client_ID(Origin));
244                         break;
245                 case 'q': /* KICK-protected user */
246                         if (!set || Client_Type(Client) == CLIENT_SERVER
247                             || Client_OperByMe(Origin))
248                                 x[0] = 'q';
249                         else
250                                 ok = IRC_WriteErrClient(Origin,
251                                                         ERR_NOPRIVILEGES_MSG,
252                                                         Client_ID(Origin));
253                         break;
254                 case 'r': /* Restricted (only settable) */
255                         if (set || Client_Type(Client) == CLIENT_SERVER)
256                                 x[0] = 'r';
257                         else
258                                 ok = IRC_WriteErrClient(Origin,
259                                                         ERR_RESTRICTED_MSG,
260                                                         Client_ID(Origin));
261                         break;
262                 case 'R': /* Registered (not [un]settable by clients) */
263                         if (Client_Type(Client) == CLIENT_SERVER)
264                                 x[0] = 'R';
265                         else
266                                 ok = IRC_WriteErrClient(Origin,
267                                                         ERR_NICKREGISTER_MSG,
268                                                         Client_ID(Origin));
269                         break;
270                 case 'x': /* Cloak hostname */
271                         if (Client_HasMode(Client, 'r'))
272                                 ok = IRC_WriteErrClient(Origin,
273                                                         ERR_RESTRICTED_MSG,
274                                                         Client_ID(Origin));
275                         else if (!set || Conf_CloakHostModeX[0]
276                                  || Client_Type(Client) == CLIENT_SERVER
277                                  || Client_OperByMe(Client)) {
278                                 x[0] = 'x';
279                                 send_RPL_HOSTHIDDEN_MSG = true;
280                         } else
281                                 ok = IRC_WriteErrClient(Origin,
282                                                         ERR_NOPRIVILEGES_MSG,
283                                                         Client_ID(Origin));
284                         break;
285                 default:
286                         if (Client_Type(Client) != CLIENT_SERVER) {
287                                 Log(LOG_DEBUG,
288                                     "Unknown mode \"%c%c\" from \"%s\"!?",
289                                     set ? '+' : '-', *mode_ptr,
290                                     Client_ID(Origin));
291                                 ok = IRC_WriteErrClient(Origin,
292                                                         ERR_UMODEUNKNOWNFLAG2_MSG,
293                                                         Client_ID(Origin),
294                                                         set ? '+' : '-',
295                                                         *mode_ptr);
296                                 x[0] = '\0';
297                         } else {
298                                 Log(LOG_DEBUG,
299                                     "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
300                                     set ? '+' : '-', *mode_ptr,
301                                     Client_ID(Origin), Client_ID(Target));
302                                 x[0] = *mode_ptr;
303                         }
304                 }
305
306                 if (!ok)
307                         break;
308
309                 /* Is there a valid mode change? */
310                 if (!x[0])
311                         continue;
312
313                 if (set) {
314                         if (Client_ModeAdd(Target, x[0]))
315                                 strlcat(the_modes, x, sizeof(the_modes));
316                 } else {
317                         if (Client_ModeDel(Target, x[0]))
318                                 strlcat(the_modes, x, sizeof(the_modes));
319                 }
320         }
321
322         /* Are there changed modes? */
323         if (the_modes[1]) {
324                 /* Remove needless action modifier characters */
325                 len = strlen(the_modes) - 1;
326                 if (the_modes[len] == '+' || the_modes[len] == '-')
327                         the_modes[len] = '\0';
328
329                 if (Client_Type(Client) == CLIENT_SERVER) {
330                         /* Forward modes to other servers */
331                         if (Client_Conn(Target) != NONE) {
332                                 /* Remote server (service?) changed modes
333                                  * for one of our clients. Inform it! */
334                                 IRC_WriteStrClientPrefix(Target, Origin,
335                                                          "MODE %s :%s",
336                                                          Client_ID(Target),
337                                                          the_modes);
338                         }
339                         IRC_WriteStrServersPrefix(Client, Origin,
340                                                   "MODE %s :%s",
341                                                   Client_ID(Target),
342                                                   the_modes);
343                 } else {
344                         /* Send reply to client and inform other servers */
345                         ok = IRC_WriteStrClientPrefix(Client, Origin,
346                                                       "MODE %s :%s",
347                                                       Client_ID(Target),
348                                                       the_modes);
349                         IRC_WriteStrServersPrefix(Client, Origin,
350                                                   "MODE %s :%s",
351                                                   Client_ID(Target),
352                                                   the_modes);
353                 }
354
355                 if (send_RPL_HOSTHIDDEN_MSG && Client_Conn(Target) > NONE) {
356                         /* A new (cloaked) hostname must be announced */
357                         IRC_WriteStrClientPrefix(Target, Origin,
358                                                  RPL_HOSTHIDDEN_MSG,
359                                                  Client_ID(Target),
360                                                  Client_HostnameDisplayed(Target));
361
362                 }
363
364                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
365                          Client_TypeText(Target), Client_Mask(Target),
366                          Client_Modes(Target));
367         }
368
369         IRC_SetPenalty(Client, 1);
370         return ok;
371 } /* Client_Mode */
372
373 /*
374  * Reply to a channel mode request.
375  *
376  * @param Origin The originator of the MODE command (prefix).
377  * @param Channel The channel of which the modes should be sent.
378  * @return CONNECTED or DISCONNECTED.
379  */
380 static bool
381 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
382 {
383         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
384         const char *mode_ptr;
385
386         /* Member or not? -- That's the question! */
387         if (!Channel_IsMemberOf(Channel, Origin))
388                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
389                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
390
391         /* The sender is a member: generate extended reply */
392         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
393         mode_ptr = the_modes;
394         the_args[0] = '\0';
395
396         while(*mode_ptr) {
397                 switch(*mode_ptr) {
398                 case 'l':
399                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
400                         strlcat(the_args, argadd, sizeof(the_args));
401                         break;
402                 case 'k':
403                         strlcat(the_args, " ", sizeof(the_args));
404                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
405                         break;
406                 }
407                 mode_ptr++;
408         }
409         if (the_args[0])
410                 strlcat(the_modes, the_args, sizeof(the_modes));
411
412         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
413                                 Client_ID(Origin), Channel_Name(Channel),
414                                 the_modes))
415                 return DISCONNECTED;
416 #ifndef STRICT_RFC
417         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
418                                   Client_ID(Origin), Channel_Name(Channel),
419                                   Channel_CreationTime(Channel)))
420                 return DISCONNECTED;
421 #endif
422         return CONNECTED;
423 }
424
425 /**
426  * Handle channel mode and channel-user mode changes
427  *
428  * @param Client The client from which this command has been received.
429  * @param Req Request structure with prefix and all parameters.
430  * @param Origin The originator of the MODE command (prefix).
431  * @param Channel The target channel of this MODE command.
432  * @return CONNECTED or DISCONNECTED.
433  */
434 static bool
435 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
436 {
437         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
438             argadd[CLIENT_PASS_LEN], *mode_ptr;
439         bool connected, set, skiponce, retval, use_servermode,
440              is_halfop, is_op, is_admin, is_owner, is_machine, is_oper;
441         int mode_arg, arg_arg, mode_arg_count = 0;
442         CLIENT *client;
443         long l;
444         size_t len;
445
446         is_halfop = is_op = is_admin = is_owner = is_machine = is_oper = false;
447
448         if (Channel_IsModeless(Channel))
449                 return IRC_WriteErrClient(Client, ERR_NOCHANMODES_MSG,
450                                 Client_ID(Client), Channel_Name(Channel));
451
452         /* Mode request: let's answer it :-) */
453         if (Req->argc <= 1)
454                 return Channel_Mode_Answer_Request(Origin, Channel);
455
456         /* Check if origin is oper and opers can use mode */
457         use_servermode = Conf_OperServerMode;
458         if(Client_OperByMe(Client) && Conf_OperCanMode) {
459                 is_oper = true;
460         }
461
462         /* Check if client is a server/service */
463         if(Client_Type(Client) == CLIENT_SERVER ||
464            Client_Type(Client) == CLIENT_SERVICE) {
465                 is_machine = true;
466         }
467
468         /* Check if client is member of channel or an oper or an server/service */
469         if(!Channel_IsMemberOf(Channel, Client) && !is_oper && !is_machine)
470                 return IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
471                                           Client_ID(Origin),
472                                           Channel_Name(Channel));
473
474         mode_arg = 1;
475         mode_ptr = Req->argv[mode_arg];
476         if (Req->argc > mode_arg + 1)
477                 arg_arg = mode_arg + 1;
478         else
479                 arg_arg = -1;
480
481         /* Initial state: set or unset modes? */
482         skiponce = false;
483         switch (*mode_ptr) {
484         case '-':
485                 set = false;
486                 break;
487         case '+':
488                 set = true;
489                 break;
490         default:
491                 set = true;
492                 skiponce = true;
493         }
494
495         /* Prepare reply string */
496         strcpy(the_modes, set ? "+" : "-");
497         the_args[0] = '\0';
498
499         x[1] = '\0';
500         connected = CONNECTED;
501         while (mode_ptr) {
502                 if (!skiponce)
503                         mode_ptr++;
504                 if (!*mode_ptr) {
505                         /* Try next argument if there's any */
506                         if (arg_arg < 0)
507                                 break;
508                         if (arg_arg > mode_arg)
509                                 mode_arg = arg_arg;
510                         else
511                                 mode_arg++;
512
513                         if (mode_arg >= Req->argc)
514                                 break;
515                         mode_ptr = Req->argv[mode_arg];
516
517                         if (Req->argc > mode_arg + 1)
518                                 arg_arg = mode_arg + 1;
519                         else
520                                 arg_arg = -1;
521                 }
522                 skiponce = false;
523
524                 switch (*mode_ptr) {
525                 case '+':
526                 case '-':
527                         if (((*mode_ptr == '+') && !set)
528                             || ((*mode_ptr == '-') && set)) {
529                                 /* Action modifier ("+"/"-") must be changed ... */
530                                 len = strlen(the_modes) - 1;
531                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
532                                         /* Adjust last action modifier in result */
533                                         the_modes[len] = *mode_ptr;
534                                 } else {
535                                         /* Append modifier character to result string */
536                                         x[0] = *mode_ptr;
537                                         strlcat(the_modes, x, sizeof(the_modes));
538                                 }
539                                 set = *mode_ptr == '+';
540                         }
541                         continue;
542                 }
543
544                 /* Are there arguments left? */
545                 if (arg_arg >= Req->argc)
546                         arg_arg = -1;
547
548                 if(!is_machine && !is_oper) {
549                         if (Channel_UserHasMode(Channel, Client, 'q'))
550                                 is_owner = true;
551                         if (Channel_UserHasMode(Channel, Client, 'a'))
552                                 is_admin = true;
553                         if (Channel_UserHasMode(Channel, Client, 'o'))
554                                 is_op = true;
555                         if (Channel_UserHasMode(Channel, Client, 'h'))
556                                 is_halfop = true;
557                 }
558
559                 /* Validate modes */
560                 x[0] = '\0';
561                 argadd[0] = '\0';
562                 client = NULL;
563                 switch (*mode_ptr) {
564                 /* --- Channel modes --- */
565                 case 'R': /* Registered users only */
566                 case 's': /* Secret channel */
567                 case 'z': /* Secure connections only */
568                         if(!is_oper && !is_machine && !is_owner &&
569                            !is_admin && !is_op) {
570                                 connected = IRC_WriteErrClient(Origin,
571                                         ERR_CHANOPRIVSNEEDED_MSG,
572                                         Client_ID(Origin), Channel_Name(Channel));
573                                 goto chan_exit;
574                         }
575                 case 'i': /* Invite only */
576                 case 'V': /* Invite disallow */
577                 case 'M': /* Only identified nicks can write */
578                 case 'm': /* Moderated */
579                 case 'n': /* Only members can write */
580                 case 'Q': /* No kicks */
581                 case 't': /* Topic locked */
582                         if(is_oper || is_machine || is_owner ||
583                            is_admin || is_op || is_halfop)
584                                 x[0] = *mode_ptr;
585                         else
586                                 connected = IRC_WriteErrClient(Origin,
587                                         ERR_CHANOPRIVSNEEDED_MSG,
588                                         Client_ID(Origin), Channel_Name(Channel));
589                         break;
590                 case 'k': /* Channel key */
591                         if (Mode_Limit_Reached(Client, mode_arg_count++))
592                                 goto chan_exit;
593                         if (!set) {
594                                 if (is_oper || is_machine || is_owner ||
595                                     is_admin || is_op || is_halfop)
596                                         x[0] = *mode_ptr;
597                                 else
598                                         connected = IRC_WriteErrClient(Origin,
599                                                 ERR_CHANOPRIVSNEEDED_MSG,
600                                                 Client_ID(Origin),
601                                                 Channel_Name(Channel));
602                                 break;
603                         }
604                         if (arg_arg > mode_arg) {
605                                 if (is_oper || is_machine || is_owner ||
606                                     is_admin || is_op || is_halfop) {
607                                         Channel_ModeDel(Channel, 'k');
608                                         Channel_SetKey(Channel,
609                                                        Req->argv[arg_arg]);
610                                         strlcpy(argadd, Channel_Key(Channel),
611                                                 sizeof(argadd));
612                                         x[0] = *mode_ptr;
613                                 } else {
614                                         connected = IRC_WriteErrClient(Origin,
615                                                 ERR_CHANOPRIVSNEEDED_MSG,
616                                                 Client_ID(Origin),
617                                                 Channel_Name(Channel));
618                                 }
619                                 Req->argv[arg_arg][0] = '\0';
620                                 arg_arg++;
621                         } else {
622 #ifdef STRICT_RFC
623                                 /* Only send error message in "strict" mode,
624                                  * this is how ircd2.11 and others behave ... */
625                                 IRC_SetPenalty(Origin, 2);
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                                 IRC_SetPenalty(Origin, 2);
671                                 connected = IRC_WriteErrClient(Origin,
672                                         ERR_NEEDMOREPARAMS_MSG,
673                                         Client_ID(Origin), Req->command);
674 #endif
675                                 goto chan_exit;
676                         }
677                         break;
678                 case 'O': /* IRC operators only */
679                         if (set) {
680                                 /* Only IRC operators are allowed to
681                                  * set the 'O' channel mode! */
682                                 if(is_oper || is_machine)
683                                         x[0] = 'O';
684                                 else
685                                         connected = IRC_WriteErrClient(Origin,
686                                                 ERR_NOPRIVILEGES_MSG,
687                                                 Client_ID(Origin));
688                         } else if(is_oper || is_machine || is_owner ||
689                                   is_admin || is_op)
690                                 x[0] = 'O';
691                         else
692                                 connected = IRC_WriteErrClient(Origin,
693                                         ERR_CHANOPRIVSNEEDED_MSG,
694                                         Client_ID(Origin),
695                                         Channel_Name(Channel));
696                         break;
697                 case 'P': /* Persistent channel */
698                         if (set) {
699                                 /* Only IRC operators are allowed to
700                                  * set the 'P' channel mode! */
701                                 if(is_oper || is_machine)
702                                         x[0] = 'P';
703                                 else
704                                         connected = IRC_WriteErrClient(Origin,
705                                                 ERR_NOPRIVILEGES_MSG,
706                                                 Client_ID(Origin));
707                         } else if(is_oper || is_machine || is_owner ||
708                                   is_admin || is_op)
709                                 x[0] = 'P';
710                         else
711                                 connected = IRC_WriteErrClient(Origin,
712                                         ERR_CHANOPRIVSNEEDED_MSG,
713                                         Client_ID(Origin),
714                                         Channel_Name(Channel));
715                         break;
716                 /* --- Channel user modes --- */
717                 case 'q': /* Owner */
718                 case 'a': /* Channel admin */
719                         if(!is_oper && !is_machine && !is_owner && !is_admin) {
720                                 connected = IRC_WriteErrClient(Origin,
721                                         ERR_CHANOPPRIVTOOLOW_MSG,
722                                         Client_ID(Origin),
723                                         Channel_Name(Channel));
724                                 goto chan_exit;
725                         }
726                 case 'o': /* Channel operator */
727                         if(!is_oper && !is_machine && !is_owner &&
728                            !is_admin && !is_op) {
729                                 connected = IRC_WriteErrClient(Origin,
730                                         ERR_CHANOPRIVSNEEDED_MSG,
731                                         Client_ID(Origin),
732                                         Channel_Name(Channel));
733                                 goto chan_exit;
734                         }
735                 case 'h': /* Half Op */
736                         if(!is_oper && !is_machine && !is_owner &&
737                            !is_admin && !is_op) {
738                                 connected = IRC_WriteErrClient(Origin,
739                                         ERR_CHANOPRIVSNEEDED_MSG,
740                                         Client_ID(Origin),
741                                         Channel_Name(Channel));
742                                 goto chan_exit;
743                         }
744                 case 'v': /* Voice */
745                         if (arg_arg > mode_arg) {
746                                 if (is_oper || is_machine || is_owner ||
747                                     is_admin || is_op || is_halfop) {
748                                         client = Client_Search(Req->argv[arg_arg]);
749                                         if (client)
750                                                 x[0] = *mode_ptr;
751                                         else
752                                                 connected = IRC_WriteErrClient(Origin,
753                                                         ERR_NOSUCHNICK_MSG,
754                                                         Client_ID(Origin),
755                                                         Req->argv[arg_arg]);
756                                 } else {
757                                         connected = IRC_WriteErrClient(Origin,
758                                                 ERR_CHANOPRIVSNEEDED_MSG,
759                                                 Client_ID(Origin),
760                                                 Channel_Name(Channel));
761                                 }
762                                 Req->argv[arg_arg][0] = '\0';
763                                 arg_arg++;
764                         } else {
765 #ifdef STRICT_RFC
766                                 /* Report an error to the client, when a user
767                                  * mode should be changed but no nickname is
768                                  * given. But don't do it when not in "strict"
769                                  * mode, because most other servers don't do
770                                  * it as well and some clients send "wired"
771                                  * MODE commands like "MODE #chan -ooo nick". */
772                                 IRC_SetPenalty(Origin, 2);
773                                 connected = IRC_WriteErrClient(Origin,
774                                         ERR_NEEDMOREPARAMS_MSG,
775                                         Client_ID(Origin), Req->command);
776 #endif
777                                 goto chan_exit;
778                         }
779                         break;
780                 /* --- Channel lists --- */
781                 case 'I': /* Invite lists */
782                 case 'b': /* Ban lists */
783                 case 'e': /* Channel exception lists */
784                         if (Mode_Limit_Reached(Client, mode_arg_count++))
785                                 goto chan_exit;
786                         if (arg_arg > mode_arg) {
787                                 /* modify list */
788                                 if (is_oper || is_machine || is_owner ||
789                                     is_admin || is_op || is_halfop) {
790                                         connected = set
791                                            ? Add_To_List(*mode_ptr, Origin,
792                                                 Client, Channel,
793                                                 Req->argv[arg_arg])
794                                            : Del_From_List(*mode_ptr, Origin,
795                                                 Client, Channel,
796                                                 Req->argv[arg_arg]);
797                                 } else {
798                                         connected = IRC_WriteErrClient(Origin,
799                                                 ERR_CHANOPRIVSNEEDED_MSG,
800                                                 Client_ID(Origin),
801                                                 Channel_Name(Channel));
802                                 }
803                                 Req->argv[arg_arg][0] = '\0';
804                                 arg_arg++;
805                         } else {
806                                 switch (*mode_ptr) {
807                                 case 'I':
808                                         Channel_ShowInvites(Origin, Channel);
809                                         break;
810                                 case 'b':
811                                         Channel_ShowBans(Origin, Channel);
812                                         break;
813                                 case 'e':
814                                         Channel_ShowExcepts(Origin, Channel);
815                                         break;
816                                 }
817                         }
818                         break;
819                 default:
820                         if (Client_Type(Client) != CLIENT_SERVER) {
821                                 Log(LOG_DEBUG,
822                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
823                                     set ? '+' : '-', *mode_ptr,
824                                     Client_ID(Origin), Channel_Name(Channel));
825                                 connected = IRC_WriteErrClient(Origin,
826                                         ERR_UNKNOWNMODE_MSG,
827                                         Client_ID(Origin), *mode_ptr,
828                                         Channel_Name(Channel));
829                                 x[0] = '\0';
830                         } else {
831                                 Log(LOG_DEBUG,
832                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
833                                     set ? '+' : '-', *mode_ptr,
834                                     Client_ID(Origin), Channel_Name(Channel));
835                                 x[0] = *mode_ptr;
836                         }
837                 }
838
839                 if (!connected)
840                         break;
841
842                 /* Is there a valid mode change? */
843                 if (!x[0])
844                         continue;
845
846                 /* Validate target client */
847                 if (client && (!Channel_IsMemberOf(Channel, client))) {
848                         if (!IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
849                                                 Client_ID(Origin),
850                                                 Client_ID(client),
851                                                 Channel_Name(Channel)))
852                                 break;
853                         continue;
854                 }
855
856                 if (client) {
857                         /* Channel-User-Mode */
858                         retval = set
859                                ? Channel_UserModeAdd(Channel, client, x[0])
860                                : Channel_UserModeDel(Channel, client, x[0]);
861                         if (retval) {
862                                 strlcat(the_args, " ", sizeof(the_args));
863                                 strlcat(the_args, Client_ID(client),
864                                         sizeof(the_args));
865                                 strlcat(the_modes, x, sizeof(the_modes));
866                                 LogDebug
867                                     ("User \"%s\": Mode change on %s, now \"%s\"",
868                                      Client_Mask(client), Channel_Name(Channel),
869                                      Channel_UserModes(Channel, client));
870                         }
871                 } else {
872                         /* Channel-Mode */
873                         retval = set
874                                ? Channel_ModeAdd(Channel, x[0])
875                                : Channel_ModeDel(Channel, x[0]);
876                         if (retval) {
877                                 strlcat(the_modes, x, sizeof(the_modes));
878                                 LogDebug("Channel %s: Mode change, now \"%s\".",
879                                          Channel_Name(Channel),
880                                          Channel_Modes(Channel));
881                         }
882                 }
883
884                 /* Are there additional arguments to add? */
885                 if (argadd[0]) {
886                         strlcat(the_args, " ", sizeof(the_args));
887                         strlcat(the_args, argadd, sizeof(the_args));
888                 }
889         }
890
891       chan_exit:
892         /* Are there changed modes? */
893         if (the_modes[1]) {
894                 /* Clean up mode string */
895                 len = strlen(the_modes) - 1;
896                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
897                         the_modes[len] = '\0';
898
899                 if (Client_Type(Client) == CLIENT_SERVER) {
900                         /* MODE requests for local channels from other servers
901                          * are definitely invalid! */
902                         if (Channel_IsLocal(Channel)) {
903                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
904                                 return CONNECTED;
905                         }
906
907                         /* Forward mode changes to channel users and all the
908                          * other remote servers: */
909                         IRC_WriteStrServersPrefix(Client, Origin,
910                                 "MODE %s %s%s", Channel_Name(Channel),
911                                 the_modes, the_args);
912                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
913                                 false, "MODE %s %s%s", Channel_Name(Channel),
914                                 the_modes, the_args);
915                 } else {
916                         if (use_servermode)
917                                 Origin = Client_ThisServer();
918                         /* Send reply to client and inform other servers and channel users */
919                         connected = IRC_WriteStrClientPrefix(Client, Origin,
920                                         "MODE %s %s%s", Channel_Name(Channel),
921                                         the_modes, the_args);
922                         /* Only forward requests for non-local channels */
923                         if (!Channel_IsLocal(Channel))
924                                 IRC_WriteStrServersPrefix(Client, Origin,
925                                         "MODE %s %s%s", Channel_Name(Channel),
926                                         the_modes, the_args);
927                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
928                                 false, "MODE %s %s%s", Channel_Name(Channel),
929                                 the_modes, the_args);
930                 }
931         }
932
933         IRC_SetPenalty(Client, 1);
934         return connected;
935 } /* Channel_Mode */
936
937 /**
938  * Handler for the IRC "AWAY" command.
939  *
940  * @param Client The client from which this command has been received.
941  * @param Req Request structure with prefix and all parameters.
942  * @return CONNECTED or DISCONNECTED.
943  */
944 GLOBAL bool
945 IRC_AWAY( CLIENT *Client, REQUEST *Req )
946 {
947         assert (Client != NULL);
948         assert (Req != NULL);
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- */