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