3dd61e7ca2031ad6727c64579da300b86cd9a038
[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_Ban_Invite PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
45                                    CHANNEL *Channel, const char *Pattern));
46 static bool Del_Ban_Invite 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                         if (Mode_Limit_Reached(Client, mode_arg_count++))
666                                 goto chan_exit;
667                         if (arg_arg > mode_arg) {
668                                 /* modify list */
669                                 if (modeok) {
670                                         connected = set
671                                            ? Add_Ban_Invite(*mode_ptr, Origin,
672                                                 Client, Channel,
673                                                 Req->argv[arg_arg])
674                                            : Del_Ban_Invite(*mode_ptr, Origin,
675                                                 Client, Channel,
676                                                 Req->argv[arg_arg]);
677                                 } else {
678                                         connected = IRC_WriteStrClient(Origin,
679                                                 ERR_CHANOPRIVSNEEDED_MSG,
680                                                 Client_ID(Origin),
681                                                 Channel_Name(Channel));
682                                 }
683                                 Req->argv[arg_arg][0] = '\0';
684                                 arg_arg++;
685                         } else {
686                                 if (*mode_ptr == 'I')
687                                         Channel_ShowInvites(Origin, Channel);
688                                 else
689                                         Channel_ShowBans(Origin, Channel);
690                         }
691                         break;
692                 default:
693                         if (Client_Type(Client) != CLIENT_SERVER) {
694                                 Log(LOG_DEBUG,
695                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
696                                     set ? '+' : '-', *mode_ptr,
697                                     Client_ID(Origin), Channel_Name(Channel));
698                                 connected = IRC_WriteStrClient(Origin,
699                                         ERR_UNKNOWNMODE_MSG,
700                                         Client_ID(Origin), *mode_ptr,
701                                         Channel_Name(Channel));
702                                 x[0] = '\0';
703                         } else {
704                                 Log(LOG_DEBUG,
705                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
706                                     set ? '+' : '-', *mode_ptr,
707                                     Client_ID(Origin), Channel_Name(Channel));
708                                 x[0] = *mode_ptr;
709                         }
710                 }
711
712                 if (!connected)
713                         break;
714
715                 /* Is there a valid mode change? */
716                 if (!x[0])
717                         continue;
718
719                 /* Validate target client */
720                 if (client && (!Channel_IsMemberOf(Channel, client))) {
721                         if (!IRC_WriteStrClient
722                             (Origin, ERR_USERNOTINCHANNEL_MSG,
723                              Client_ID(Origin), Client_ID(client),
724                              Channel_Name(Channel)))
725                                 break;
726
727                         continue;
728                 }
729
730                 if (client) {
731                         /* Channel-User-Mode */
732                         retval = set
733                                ? Channel_UserModeAdd(Channel, client, x[0])
734                                : Channel_UserModeDel(Channel, client, x[0]);
735                         if (retval) {
736                                 strlcat(the_args, " ", sizeof(the_args));
737                                 strlcat(the_args, Client_ID(client),
738                                         sizeof(the_args));
739                                 strlcat(the_modes, x, sizeof(the_modes));
740                                 LogDebug
741                                     ("User \"%s\": Mode change on %s, now \"%s\"",
742                                      Client_Mask(client), Channel_Name(Channel),
743                                      Channel_UserModes(Channel, client));
744                         }
745                 } else {
746                         /* Channel-Mode */
747                         retval = set
748                                ? Channel_ModeAdd(Channel, x[0])
749                                : Channel_ModeDel(Channel, x[0]);
750                         if (retval) {
751                                 strlcat(the_modes, x, sizeof(the_modes));
752                                 LogDebug("Channel %s: Mode change, now \"%s\".",
753                                          Channel_Name(Channel),
754                                          Channel_Modes(Channel));
755                         }
756                 }
757
758                 /* Are there additional arguments to add? */
759                 if (argadd[0]) {
760                         strlcat(the_args, " ", sizeof(the_args));
761                         strlcat(the_args, argadd, sizeof(the_args));
762                 }
763         }
764
765       chan_exit:
766         /* Are there changed modes? */
767         if (the_modes[1]) {
768                 /* Clean up mode string */
769                 len = strlen(the_modes) - 1;
770                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
771                         the_modes[len] = '\0';
772
773                 if (Client_Type(Client) == CLIENT_SERVER) {
774                         /* MODE requests for local channels from other servers
775                          * are definitely invalid! */
776                         if (Channel_IsLocal(Channel)) {
777                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
778                                 return CONNECTED;
779                         }
780
781                         /* Forward mode changes to channel users and all the
782                          * other remote servers: */
783                         IRC_WriteStrServersPrefix(Client, Origin,
784                                 "MODE %s %s%s", Channel_Name(Channel),
785                                 the_modes, the_args);
786                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
787                                 false, "MODE %s %s%s", Channel_Name(Channel),
788                                 the_modes, the_args);
789                 } else {
790                         if (use_servermode)
791                                 Origin = Client_ThisServer();
792                         /* Send reply to client and inform other servers and channel users */
793                         connected = IRC_WriteStrClientPrefix(Client, Origin,
794                                         "MODE %s %s%s", Channel_Name(Channel),
795                                         the_modes, the_args);
796                         /* Only forward requests for non-local channels */
797                         if (!Channel_IsLocal(Channel))
798                                 IRC_WriteStrServersPrefix(Client, Origin,
799                                         "MODE %s %s%s", Channel_Name(Channel),
800                                         the_modes, the_args);
801                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
802                                 false, "MODE %s %s%s", Channel_Name(Channel),
803                                 the_modes, the_args);
804                 }
805         }
806
807         IRC_SetPenalty(Client, 1);
808         return connected;
809 } /* Channel_Mode */
810
811
812 GLOBAL bool
813 IRC_AWAY( CLIENT *Client, REQUEST *Req )
814 {
815         assert( Client != NULL );
816         assert( Req != NULL );
817
818         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
819
820         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
821         {
822                 Client_SetAway( Client, Req->argv[0] );
823                 Client_ModeAdd( Client, 'a' );
824                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
825                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
826         }
827         else
828         {
829                 Client_ModeDel( Client, 'a' );
830                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
831                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
832         }
833 } /* IRC_AWAY */
834
835
836 /**
837  * Add entries to channel ban and invite lists.
838  *
839  * @param what Can be 'I' for invite or 'b' for ban list.
840  * @param Prefix The originator of the command.
841  * @param Client The sender of the command.
842  * @param Channel The channel of which the list should be modified.
843  * @param Pattern The pattern to add to the list.
844  * @return CONNECTED or DISCONNECTED.
845  */
846 static bool
847 Add_Ban_Invite(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
848                const char *Pattern)
849 {
850         const char *mask;
851         struct list_head *list;
852
853         assert(Client != NULL);
854         assert(Channel != NULL);
855         assert(Pattern != NULL);
856         assert(what == 'I' || what == 'b');
857
858         mask = Lists_MakeMask(Pattern);
859
860         if (what == 'I')
861                 list = Channel_GetListInvites(Channel);
862         else
863                 list = Channel_GetListBans(Channel);
864
865         if (Lists_CheckDupeMask(list, mask))
866                 return CONNECTED;
867         if (Client_Type(Client) == CLIENT_USER &&
868             Lists_Count(list) >= MAX_HNDL_CHANNEL_LISTS)
869                 return IRC_WriteStrClient(Client, ERR_LISTFULL_MSG,
870                                           Client_ID(Client),
871                                           Channel_Name(Channel), mask,
872                                           MAX_HNDL_CHANNEL_LISTS);
873
874         if (what == 'I') {
875                 if (!Channel_AddInvite(Channel, mask, false))
876                         return CONNECTED;
877         } else {
878                 if (!Channel_AddBan(Channel, mask))
879                         return CONNECTED;
880         }
881         return Send_ListChange(true, what, Prefix, Client, Channel, mask);
882 }
883
884
885 /**
886  * Delete entries from channel ban and invite lists.
887  *
888  * @param what Can be 'I' for invite or 'b' for ban list.
889  * @param Prefix The originator of the command.
890  * @param Client The sender of the command.
891  * @param Channel The channel of which the list should be modified.
892  * @param Pattern The pattern to add to the list.
893  * @return CONNECTED or DISCONNECTED.
894  */
895 static bool
896 Del_Ban_Invite(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
897                const char *Pattern)
898 {
899         const char *mask;
900         struct list_head *list;
901
902         assert(Client != NULL);
903         assert(Channel != NULL);
904         assert(Pattern != NULL);
905         assert(what == 'I' || what == 'b');
906
907         mask = Lists_MakeMask(Pattern);
908
909         if (what == 'I')
910                 list = Channel_GetListInvites(Channel);
911         else
912                 list = Channel_GetListBans(Channel);
913
914         if (!Lists_CheckDupeMask(list, mask))
915                 return CONNECTED;
916         Lists_Del(list, mask);
917
918         return Send_ListChange(false, what, Prefix, Client, Channel, mask);
919 }
920
921
922 /**
923  * Send information about changed channel ban/invite lists to clients.
924  *
925  * @param IsAdd true if the list item has been added, false otherwise.
926  * @param ModeChar The mode to use (e. g. 'b' or 'I')
927  * @param Prefix The originator of the mode list change.
928  * @param Client The sender of the command.
929  * @param Channel The channel of which the list has been modified.
930  * @param Mask The mask which has been added or removed.
931  * @return CONNECTED or DISCONNECTED.
932  */
933 static bool
934 Send_ListChange(const bool IsAdd, const char ModeChar, CLIENT *Prefix,
935                 CLIENT *Client, CHANNEL *Channel, const char *Mask)
936 {
937         bool ok = true;
938
939         /* Send confirmation to the client */
940         if (Client_Type(Client) == CLIENT_USER)
941                 ok = IRC_WriteStrClientPrefix(Client, Prefix, "MODE %s %c%c %s",
942                                               Channel_Name(Channel),
943                                               IsAdd ? '+' : '-',
944                                               ModeChar, Mask);
945
946         /* to other servers */
947         IRC_WriteStrServersPrefix(Client, Prefix, "MODE %s %c%c %s",
948                                   Channel_Name(Channel), IsAdd ? '+' : '-',
949                                   ModeChar, Mask);
950
951         /* and local users in channel */
952         IRC_WriteStrChannelPrefix(Client, Channel, Prefix, false,
953                                   "MODE %s %c%c %s", Channel_Name(Channel),
954                                   IsAdd ? '+' : '-', ModeChar, Mask );
955
956         return ok;
957 } /* Send_ListChange */
958
959
960 /* -eof- */