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