]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
Implement channel mode "M"
[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': /* Only identified nicks can write */
504                 case 'm': /* Moderated */
505                 case 'n': /* Only members can write */
506                 case 'R': /* Registered users only */
507                 case 's': /* Secret channel */
508                 case 't': /* Topic locked */
509                 case 'z': /* Secure connections only */
510                         if (modeok)
511                                 x[0] = *mode_ptr;
512                         else
513                                 connected = IRC_WriteStrClient(Origin,
514                                         ERR_CHANOPRIVSNEEDED_MSG,
515                                         Client_ID(Origin), Channel_Name(Channel));
516                         break;
517                 case 'k': /* Channel key */
518                         if (Mode_Limit_Reached(Client, mode_arg_count++))
519                                 goto chan_exit;
520                         if (!set) {
521                                 if (modeok)
522                                         x[0] = *mode_ptr;
523                                 else
524                                         connected = IRC_WriteStrClient(Origin,
525                                                 ERR_CHANOPRIVSNEEDED_MSG,
526                                                 Client_ID(Origin),
527                                                 Channel_Name(Channel));
528                                 break;
529                         }
530                         if (arg_arg > mode_arg) {
531                                 if (modeok) {
532                                         Channel_ModeDel(Channel, 'k');
533                                         Channel_SetKey(Channel,
534                                                        Req->argv[arg_arg]);
535                                         strlcpy(argadd, Channel_Key(Channel),
536                                                 sizeof(argadd));
537                                         x[0] = *mode_ptr;
538                                 } else {
539                                         connected = IRC_WriteStrClient(Origin,
540                                                 ERR_CHANOPRIVSNEEDED_MSG,
541                                                 Client_ID(Origin),
542                                                 Channel_Name(Channel));
543                                 }
544                                 Req->argv[arg_arg][0] = '\0';
545                                 arg_arg++;
546                         } else {
547                                 connected = IRC_WriteStrClient(Origin,
548                                         ERR_NEEDMOREPARAMS_MSG,
549                                         Client_ID(Origin), Req->command);
550                                 goto chan_exit;
551                         }
552                         break;
553                 case 'l': /* Member limit */
554                         if (Mode_Limit_Reached(Client, mode_arg_count++))
555                                 goto chan_exit;
556                         if (!set) {
557                                 if (modeok)
558                                         x[0] = *mode_ptr;
559                                 else
560                                         connected = IRC_WriteStrClient(Origin,
561                                                 ERR_CHANOPRIVSNEEDED_MSG,
562                                                 Client_ID(Origin),
563                                                 Channel_Name(Channel));
564                                 break;
565                         }
566                         if (arg_arg > mode_arg) {
567                                 if (modeok) {
568                                         l = atol(Req->argv[arg_arg]);
569                                         if (l > 0 && l < 0xFFFF) {
570                                                 Channel_ModeDel(Channel, 'l');
571                                                 Channel_SetMaxUsers(Channel, l);
572                                                 snprintf(argadd, sizeof(argadd),
573                                                          "%ld", l);
574                                                 x[0] = *mode_ptr;
575                                         }
576                                 } else {
577                                         connected = IRC_WriteStrClient(Origin,
578                                                 ERR_CHANOPRIVSNEEDED_MSG,
579                                                 Client_ID(Origin),
580                                                 Channel_Name(Channel));
581                                 }
582                                 Req->argv[arg_arg][0] = '\0';
583                                 arg_arg++;
584                         } else {
585                                 connected = IRC_WriteStrClient(Origin,
586                                         ERR_NEEDMOREPARAMS_MSG,
587                                         Client_ID(Origin), Req->command);
588                                 goto chan_exit;
589                         }
590                         break;
591                 case 'O': /* IRC operators only */
592                         if (modeok) {
593                                 /* Only IRC operators are allowed to
594                                  * set the 'O' channel mode! */
595                                 if (set && !(Client_OperByMe(Client)
596                                     || Client_Type(Client) == CLIENT_SERVER))
597                                         connected = IRC_WriteStrClient(Origin,
598                                                 ERR_NOPRIVILEGES_MSG,
599                                                 Client_ID(Origin));
600                                 else
601                                         x[0] = 'O';
602                         } else
603                                 connected = IRC_WriteStrClient(Origin,
604                                                 ERR_CHANOPRIVSNEEDED_MSG,
605                                                 Client_ID(Origin),
606                                                 Channel_Name(Channel));
607                                 break;
608                 case 'P': /* Persistent channel */
609                         if (modeok) {
610                                 /* Only IRC operators are allowed to
611                                  * set the 'P' channel mode! */
612                                 if (set && !(Client_OperByMe(Client)
613                                     || Client_Type(Client) == CLIENT_SERVER))
614                                         connected = IRC_WriteStrClient(Origin,
615                                                 ERR_NOPRIVILEGES_MSG,
616                                                 Client_ID(Origin));
617                                 else
618                                         x[0] = 'P';
619                         } else
620                                 connected = IRC_WriteStrClient(Origin,
621                                         ERR_CHANOPRIVSNEEDED_MSG,
622                                         Client_ID(Origin),
623                                         Channel_Name(Channel));
624                         break;
625                 /* --- Channel user modes --- */
626                 case 'a':
627                 case 'h':
628                 case 'q':
629                         if (Client_Type(Client) != CLIENT_SERVER) {
630                                 connected = IRC_WriteStrClient(Origin,
631                                         ERR_CHANOPRIVSNEEDED_MSG,
632                                         Client_ID(Origin),
633                                         Channel_Name(Channel));
634                                 goto chan_exit;
635                         }
636                 case 'o': /* Channel operator */
637                 case 'v': /* Voice */
638                         if (arg_arg > mode_arg) {
639                                 if (modeok) {
640                                         client = Client_Search(Req->argv[arg_arg]);
641                                         if (client)
642                                                 x[0] = *mode_ptr;
643                                         else
644                                                 connected = IRC_WriteStrClient(Client,
645                                                         ERR_NOSUCHNICK_MSG,
646                                                         Client_ID(Client),
647                                                         Req->argv[arg_arg]);
648                                 } else {
649                                         connected = IRC_WriteStrClient(Origin,
650                                                 ERR_CHANOPRIVSNEEDED_MSG,
651                                                 Client_ID(Origin),
652                                                 Channel_Name(Channel));
653                                 }
654                                 Req->argv[arg_arg][0] = '\0';
655                                 arg_arg++;
656                         } else {
657                                 connected = IRC_WriteStrClient(Origin,
658                                         ERR_NEEDMOREPARAMS_MSG,
659                                         Client_ID(Origin), Req->command);
660                                 goto chan_exit;
661                         }
662                         break;
663                 /* --- Channel lists --- */
664                 case 'I': /* Invite lists */
665                 case 'b': /* Ban lists */
666                 case 'e': /* Channel exception lists */
667                         if (Mode_Limit_Reached(Client, mode_arg_count++))
668                                 goto chan_exit;
669                         if (arg_arg > mode_arg) {
670                                 /* modify list */
671                                 if (modeok) {
672                                         connected = set
673                                            ? Add_To_List(*mode_ptr, Origin,
674                                                 Client, Channel,
675                                                 Req->argv[arg_arg])
676                                            : Del_From_List(*mode_ptr, Origin,
677                                                 Client, Channel,
678                                                 Req->argv[arg_arg]);
679                                 } else {
680                                         connected = IRC_WriteStrClient(Origin,
681                                                 ERR_CHANOPRIVSNEEDED_MSG,
682                                                 Client_ID(Origin),
683                                                 Channel_Name(Channel));
684                                 }
685                                 Req->argv[arg_arg][0] = '\0';
686                                 arg_arg++;
687                         } else {
688                                 switch (*mode_ptr) {
689                                 case 'I':
690                                         Channel_ShowInvites(Origin, Channel);
691                                         break;
692                                 case 'b':
693                                         Channel_ShowBans(Origin, Channel);
694                                         break;
695                                 case 'e':
696                                         Channel_ShowExcepts(Origin, Channel);
697                                         break;
698                                 }
699                         }
700                         break;
701                 default:
702                         if (Client_Type(Client) != CLIENT_SERVER) {
703                                 Log(LOG_DEBUG,
704                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
705                                     set ? '+' : '-', *mode_ptr,
706                                     Client_ID(Origin), Channel_Name(Channel));
707                                 connected = IRC_WriteStrClient(Origin,
708                                         ERR_UNKNOWNMODE_MSG,
709                                         Client_ID(Origin), *mode_ptr,
710                                         Channel_Name(Channel));
711                                 x[0] = '\0';
712                         } else {
713                                 Log(LOG_DEBUG,
714                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
715                                     set ? '+' : '-', *mode_ptr,
716                                     Client_ID(Origin), Channel_Name(Channel));
717                                 x[0] = *mode_ptr;
718                         }
719                 }
720
721                 if (!connected)
722                         break;
723
724                 /* Is there a valid mode change? */
725                 if (!x[0])
726                         continue;
727
728                 /* Validate target client */
729                 if (client && (!Channel_IsMemberOf(Channel, client))) {
730                         if (!IRC_WriteStrClient
731                             (Origin, ERR_USERNOTINCHANNEL_MSG,
732                              Client_ID(Origin), Client_ID(client),
733                              Channel_Name(Channel)))
734                                 break;
735
736                         continue;
737                 }
738
739                 if (client) {
740                         /* Channel-User-Mode */
741                         retval = set
742                                ? Channel_UserModeAdd(Channel, client, x[0])
743                                : Channel_UserModeDel(Channel, client, x[0]);
744                         if (retval) {
745                                 strlcat(the_args, " ", sizeof(the_args));
746                                 strlcat(the_args, Client_ID(client),
747                                         sizeof(the_args));
748                                 strlcat(the_modes, x, sizeof(the_modes));
749                                 LogDebug
750                                     ("User \"%s\": Mode change on %s, now \"%s\"",
751                                      Client_Mask(client), Channel_Name(Channel),
752                                      Channel_UserModes(Channel, client));
753                         }
754                 } else {
755                         /* Channel-Mode */
756                         retval = set
757                                ? Channel_ModeAdd(Channel, x[0])
758                                : Channel_ModeDel(Channel, x[0]);
759                         if (retval) {
760                                 strlcat(the_modes, x, sizeof(the_modes));
761                                 LogDebug("Channel %s: Mode change, now \"%s\".",
762                                          Channel_Name(Channel),
763                                          Channel_Modes(Channel));
764                         }
765                 }
766
767                 /* Are there additional arguments to add? */
768                 if (argadd[0]) {
769                         strlcat(the_args, " ", sizeof(the_args));
770                         strlcat(the_args, argadd, sizeof(the_args));
771                 }
772         }
773
774       chan_exit:
775         /* Are there changed modes? */
776         if (the_modes[1]) {
777                 /* Clean up mode string */
778                 len = strlen(the_modes) - 1;
779                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
780                         the_modes[len] = '\0';
781
782                 if (Client_Type(Client) == CLIENT_SERVER) {
783                         /* MODE requests for local channels from other servers
784                          * are definitely invalid! */
785                         if (Channel_IsLocal(Channel)) {
786                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
787                                 return CONNECTED;
788                         }
789
790                         /* Forward mode changes to channel users and all the
791                          * other remote servers: */
792                         IRC_WriteStrServersPrefix(Client, Origin,
793                                 "MODE %s %s%s", Channel_Name(Channel),
794                                 the_modes, the_args);
795                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
796                                 false, "MODE %s %s%s", Channel_Name(Channel),
797                                 the_modes, the_args);
798                 } else {
799                         if (use_servermode)
800                                 Origin = Client_ThisServer();
801                         /* Send reply to client and inform other servers and channel users */
802                         connected = IRC_WriteStrClientPrefix(Client, Origin,
803                                         "MODE %s %s%s", Channel_Name(Channel),
804                                         the_modes, the_args);
805                         /* Only forward requests for non-local channels */
806                         if (!Channel_IsLocal(Channel))
807                                 IRC_WriteStrServersPrefix(Client, Origin,
808                                         "MODE %s %s%s", Channel_Name(Channel),
809                                         the_modes, the_args);
810                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
811                                 false, "MODE %s %s%s", Channel_Name(Channel),
812                                 the_modes, the_args);
813                 }
814         }
815
816         IRC_SetPenalty(Client, 1);
817         return connected;
818 } /* Channel_Mode */
819
820
821 GLOBAL bool
822 IRC_AWAY( CLIENT *Client, REQUEST *Req )
823 {
824         assert( Client != NULL );
825         assert( Req != NULL );
826
827         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
828
829         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
830         {
831                 Client_SetAway( Client, Req->argv[0] );
832                 Client_ModeAdd( Client, 'a' );
833                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
834                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
835         }
836         else
837         {
838                 Client_ModeDel( Client, 'a' );
839                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
840                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
841         }
842 } /* IRC_AWAY */
843
844
845 /**
846  * Add entries to channel invite, ban and exception lists.
847  *
848  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
849  * @param Prefix The originator of the command.
850  * @param Client The sender of the command.
851  * @param Channel The channel of which the list should be modified.
852  * @param Pattern The pattern to add to the list.
853  * @return CONNECTED or DISCONNECTED.
854  */
855 static bool
856 Add_To_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
857             const char *Pattern)
858 {
859         const char *mask;
860         struct list_head *list = NULL;
861         long int current_count;
862
863         assert(Client != NULL);
864         assert(Channel != NULL);
865         assert(Pattern != NULL);
866         assert(what == 'I' || what == 'b' || what == 'e');
867
868         mask = Lists_MakeMask(Pattern);
869         current_count = Lists_Count(Channel_GetListInvites(Channel))
870                         + Lists_Count(Channel_GetListExcepts(Channel))
871                         + Lists_Count(Channel_GetListBans(Channel));
872
873         switch(what) {
874                 case 'I':
875                         list = Channel_GetListInvites(Channel);
876                         break;
877                 case 'b':
878                         list = Channel_GetListBans(Channel);
879                         break;
880                 case 'e':
881                         list = Channel_GetListExcepts(Channel);
882                         break;
883         }
884
885         if (Lists_CheckDupeMask(list, mask))
886                 return CONNECTED;
887         if (Client_Type(Client) == CLIENT_USER &&
888             current_count >= MAX_HNDL_CHANNEL_LISTS)
889                 return IRC_WriteStrClient(Client, ERR_LISTFULL_MSG,
890                                           Client_ID(Client),
891                                           Channel_Name(Channel), mask,
892                                           MAX_HNDL_CHANNEL_LISTS);
893
894         switch (what) {
895                 case 'I':
896                         if (!Channel_AddInvite(Channel, mask, false))
897                                 return CONNECTED;
898                         break;
899                 case 'b':
900                         if (!Channel_AddBan(Channel, mask))
901                                 return CONNECTED;
902                         break;
903                 case 'e':
904                         if (!Channel_AddExcept(Channel, mask))
905                                 return CONNECTED;
906                         break;
907         }
908         return Send_ListChange(true, what, Prefix, Client, Channel, mask);
909 }
910
911
912 /**
913  * Delete entries from channel invite, ban and exeption lists.
914  *
915  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
916  * @param Prefix The originator of the command.
917  * @param Client The sender of the command.
918  * @param Channel The channel of which the list should be modified.
919  * @param Pattern The pattern to add to the list.
920  * @return CONNECTED or DISCONNECTED.
921  */
922 static bool
923 Del_From_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
924               const char *Pattern)
925 {
926         const char *mask;
927         struct list_head *list = NULL;
928
929         assert(Client != NULL);
930         assert(Channel != NULL);
931         assert(Pattern != NULL);
932         assert(what == 'I' || what == 'b' || what == 'e');
933
934         mask = Lists_MakeMask(Pattern);
935
936         switch (what) {
937                 case 'I':
938                         list = Channel_GetListInvites(Channel);
939                         break;
940                 case 'b':
941                         list = Channel_GetListBans(Channel);
942                         break;
943                 case 'e':
944                         list = Channel_GetListExcepts(Channel);
945                         break;
946         }
947
948         if (!Lists_CheckDupeMask(list, mask))
949                 return CONNECTED;
950         Lists_Del(list, mask);
951
952         return Send_ListChange(false, what, Prefix, Client, Channel, mask);
953 }
954
955
956 /**
957  * Send information about changed channel invite/ban/exception lists to clients.
958  *
959  * @param IsAdd true if the list item has been added, false otherwise.
960  * @param ModeChar The mode to use (e. g. 'b' or 'I')
961  * @param Prefix The originator of the mode list change.
962  * @param Client The sender of the command.
963  * @param Channel The channel of which the list has been modified.
964  * @param Mask The mask which has been added or removed.
965  * @return CONNECTED or DISCONNECTED.
966  */
967 static bool
968 Send_ListChange(const bool IsAdd, const char ModeChar, CLIENT *Prefix,
969                 CLIENT *Client, CHANNEL *Channel, const char *Mask)
970 {
971         bool ok = true;
972
973         /* Send confirmation to the client */
974         if (Client_Type(Client) == CLIENT_USER)
975                 ok = IRC_WriteStrClientPrefix(Client, Prefix, "MODE %s %c%c %s",
976                                               Channel_Name(Channel),
977                                               IsAdd ? '+' : '-',
978                                               ModeChar, Mask);
979
980         /* to other servers */
981         IRC_WriteStrServersPrefix(Client, Prefix, "MODE %s %c%c %s",
982                                   Channel_Name(Channel), IsAdd ? '+' : '-',
983                                   ModeChar, Mask);
984
985         /* and local users in channel */
986         IRC_WriteStrChannelPrefix(Client, Channel, Prefix, false,
987                                   "MODE %s %c%c %s", Channel_Name(Channel),
988                                   IsAdd ? '+' : '-', ModeChar, Mask );
989
990         return ok;
991 } /* Send_ListChange */
992
993
994 /* -eof- */