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