]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
Fix handling of channel mode sequence with/without arguments
[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((int what, CLIENT *Prefix, CLIENT *Client,
45         CHANNEL *Channel, const char *Pattern));
46 static bool Del_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
47         CHANNEL *Channel, const char *Pattern));
48
49 static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
50         CLIENT *Client, CHANNEL *Channel, const char *Mask));
51
52
53 /**
54  * Handler for the IRC "MODE" command.
55  *
56  * See RFC 2812 section 3.1.5 ("user mode message") and section 3.2.3
57  * ("channel mode message"), and RFC 2811 section 4 ("channel modes").
58  *
59  * @param Client        The client from which this command has been received.
60  * @param Req           Request structure with prefix and all parameters.
61  * @returns             CONNECTED or DISCONNECTED.
62  */
63 GLOBAL bool
64 IRC_MODE( CLIENT *Client, REQUEST *Req )
65 {
66         CLIENT *cl, *origin;
67         CHANNEL *chan;
68
69         assert(Client != NULL);
70         assert(Req != NULL);
71
72         /* No parameters? */
73         if (Req->argc < 1)
74                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
75                                           Client_ID(Client), Req->command);
76
77         /* Origin for answers */
78         if (Client_Type(Client) == CLIENT_SERVER) {
79                 origin = Client_Search(Req->prefix);
80                 if (!origin)
81                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
82                                                   Client_ID(Client),
83                                                   Req->prefix);
84         } else
85                 origin = Client;
86
87         /* Channel or user mode? */
88         cl = NULL; chan = NULL;
89         if (Client_IsValidNick(Req->argv[0]))
90                 cl = Client_Search(Req->argv[0]);
91         if (Channel_IsValidName(Req->argv[0]))
92                 chan = Channel_Search(Req->argv[0]);
93
94         if (cl)
95                 return Client_Mode(Client, Req, origin, cl);
96         if (chan)
97                 return Channel_Mode(Client, Req, origin, chan);
98
99         /* No target found! */
100         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
101                         Client_ID(Client), Req->argv[0]);
102 } /* IRC_MODE */
103
104
105 /**
106  * Handle client mode requests
107  *
108  * @param Client        The client from which this command has been received.
109  * @param Req           Request structure with prefix and all parameters.
110  * @param Origin        The originator of the MODE command (prefix).
111  * @param Target        The target (client) of this MODE command.
112  * @returns             CONNECTED or DISCONNECTED.
113  */
114 static bool
115 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
116 {
117         char the_modes[COMMAND_LEN], x[2], *mode_ptr;
118         bool ok, set;
119         int mode_arg;
120         size_t len;
121
122         /* Is the client allowed to request or change the modes? */
123         if (Client_Type(Client) == CLIENT_USER) {
124                 /* Users are only allowed to manipulate their own modes! */
125                 if (Target != Client)
126                         return IRC_WriteStrClient(Client,
127                                                   ERR_USERSDONTMATCH_MSG,
128                                                   Client_ID(Client));
129         }
130
131         /* Mode request: let's answer it :-) */
132         if (Req->argc == 1)
133                 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
134                                           Client_ID(Origin),
135                                           Client_Modes(Target));
136
137         mode_arg = 1;
138         mode_ptr = Req->argv[mode_arg];
139
140         /* Initial state: set or unset modes? */
141         if (*mode_ptr == '+') {
142                 set = true;
143                 strcpy(the_modes, "+");
144         } else if (*mode_ptr == '-') {
145                 set = false;
146                 strcpy(the_modes, "-");
147         } else
148                 return IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
149                                           Client_ID(Origin));
150
151         x[1] = '\0';
152         ok = CONNECTED;
153         while (mode_ptr) {
154                 mode_ptr++;
155                 if (!*mode_ptr) {
156                         /* Try next argument if there's any */
157                         mode_arg++;
158                         if (mode_arg < Req->argc)
159                                 mode_ptr = Req->argv[mode_arg];
160                         else
161                                 break;
162                 }
163
164                 switch(*mode_ptr) {
165                   case '+':
166                   case '-':
167                         if ((*mode_ptr == '+' && !set)
168                             || (*mode_ptr == '-' && set)) {
169                                 /* Action modifier ("+"/"-") must be changed */
170                                 len = strlen(the_modes) - 1;
171                                 if (the_modes[len] == '+'
172                                     || the_modes[len] == '-') {
173                                         /* Last character in the "result
174                                          * string" was an "action", so just
175                                          * overwrite it with the new action */
176                                         the_modes[len] = *mode_ptr;
177                                 } else {
178                                         /* Append new modifier character to
179                                          * the resulting mode string */
180                                         x[0] = *mode_ptr;
181                                         strlcat(the_modes, x,
182                                                 sizeof(the_modes));
183                                 }
184                                 if (*mode_ptr == '+')
185                                         set = true;
186                                 else
187                                         set = false;
188                         }
189                         continue;
190                 }
191
192                 /* Validate modes */
193                 x[0] = '\0';
194                 switch (*mode_ptr) {
195                 case 'i': /* Invisible */
196                 case 's': /* Server messages */
197                 case 'w': /* Wallops messages */
198                         x[0] = *mode_ptr;
199                         break;
200                 case 'a': /* Away */
201                         if (Client_Type(Client) == CLIENT_SERVER) {
202                                 x[0] = 'a';
203                                 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
204                         } else
205                                 ok = IRC_WriteStrClient(Origin,
206                                                         ERR_NOPRIVILEGES_MSG,
207                                                         Client_ID(Origin));
208                         break;
209                 case 'c': /* Receive connect notices
210                            * (only settable by IRC operators!) */
211                         if (!set || Client_Type(Client) == CLIENT_SERVER
212                             || Client_OperByMe(Origin))
213                                 x[0] = 'c';
214                         else
215                                 ok = IRC_WriteStrClient(Origin,
216                                                         ERR_NOPRIVILEGES_MSG,
217                                                         Client_ID(Origin));
218                         break;
219                 case 'o': /* IRC operator (only unsettable!) */
220                         if (!set || Client_Type(Client) == CLIENT_SERVER) {
221                                 Client_SetOperByMe(Target, false);
222                                 x[0] = 'o';
223                         } else
224                                 ok = IRC_WriteStrClient(Origin,
225                                                         ERR_NOPRIVILEGES_MSG,
226                                                         Client_ID(Origin));
227                         break;
228                 case 'r': /* Restricted (only settable) */
229                         if (set || Client_Type(Client) == CLIENT_SERVER)
230                                 x[0] = 'r';
231                         else
232                                 ok = IRC_WriteStrClient(Origin,
233                                                         ERR_RESTRICTED_MSG,
234                                                         Client_ID(Origin));
235                         break;
236                 case 'x': /* Cloak hostname */
237                         if (Client_HasMode(Client, 'r'))
238                                 ok = IRC_WriteStrClient(Origin,
239                                                         ERR_RESTRICTED_MSG,
240                                                         Client_ID(Origin));
241                         else
242                                 x[0] = 'x';
243                         break;
244                 default:
245                         if (Client_Type(Client) != CLIENT_SERVER) {
246                                 Log(LOG_DEBUG,
247                                     "Unknown mode \"%c%c\" from \"%s\"!?",
248                                     set ? '+' : '-', *mode_ptr,
249                                     Client_ID(Origin));
250                                 ok = IRC_WriteStrClient(Origin,
251                                                         ERR_UMODEUNKNOWNFLAG2_MSG,
252                                                         Client_ID(Origin),
253                                                         set ? '+' : '-',
254                                                         *mode_ptr);
255                                 x[0] = '\0';
256                         } else {
257                                 Log(LOG_DEBUG,
258                                     "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
259                                     set ? '+' : '-', *mode_ptr,
260                                     Client_ID(Origin), Client_ID(Target));
261                                 x[0] = *mode_ptr;
262                         }
263                 }
264
265                 if (!ok)
266                         break;
267
268                 /* Is there a valid mode change? */
269                 if (!x[0])
270                         continue;
271
272                 if (set) {
273                         if (Client_ModeAdd(Target, x[0]))
274                                 strlcat(the_modes, x, sizeof(the_modes));
275                 } else {
276                         if (Client_ModeDel(Target, x[0]))
277                                 strlcat(the_modes, x, sizeof(the_modes));
278                 }
279         }
280
281         /* Are there changed modes? */
282         if (the_modes[1]) {
283                 /* Remove needless action modifier characters */
284                 len = strlen(the_modes) - 1;
285                 if (the_modes[len] == '+' || the_modes[len] == '-')
286                         the_modes[len] = '\0';
287
288                 if (Client_Type(Client) == CLIENT_SERVER) {
289                         /* Forward modes to other servers */
290                         if (Client_Conn(Target) != NONE) {
291                                 /* Remote server (service?) changed modes
292                                  * for one of our clients. Inform it! */
293                                 IRC_WriteStrClientPrefix(Target, Origin,
294                                                          "MODE %s :%s",
295                                                          Client_ID(Target),
296                                                          the_modes);
297                         }
298                         IRC_WriteStrServersPrefix(Client, Origin,
299                                                   "MODE %s :%s",
300                                                   Client_ID(Target),
301                                                   the_modes);
302                 } else {
303                         /* Send reply to client and inform other servers */
304                         ok = IRC_WriteStrClientPrefix(Client, Origin,
305                                                       "MODE %s :%s",
306                                                       Client_ID(Target),
307                                                       the_modes);
308                         IRC_WriteStrServersPrefix(Client, Origin,
309                                                   "MODE %s :%s",
310                                                   Client_ID(Target),
311                                                   the_modes);
312                 }
313                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
314                          Client_TypeText(Target), Client_Mask(Target),
315                          Client_Modes(Target));
316         }
317
318         IRC_SetPenalty(Client, 1);
319         return ok;
320 } /* Client_Mode */
321
322
323 static bool
324 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
325 {
326         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
327         const char *mode_ptr;
328
329         /* Member or not? -- That's the question! */
330         if (!Channel_IsMemberOf(Channel, Origin))
331                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
332                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
333
334         /* The sender is a member: generate extended reply */
335         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
336         mode_ptr = the_modes;
337         the_args[0] = '\0';
338
339         while(*mode_ptr) {
340                 switch(*mode_ptr) {
341                 case 'l':
342                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
343                         strlcat(the_args, argadd, sizeof(the_args));
344                         break;
345                 case 'k':
346                         strlcat(the_args, " ", sizeof(the_args));
347                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
348                         break;
349                 }
350                 mode_ptr++;
351         }
352         if (the_args[0])
353                 strlcat(the_modes, the_args, sizeof(the_modes));
354
355         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
356                                 Client_ID(Origin), Channel_Name(Channel),
357                                 the_modes))
358                 return DISCONNECTED;
359 #ifndef STRICT_RFC
360         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
361                                   Client_ID(Origin), Channel_Name(Channel),
362                                   Channel_CreationTime(Channel)))
363                 return DISCONNECTED;
364 #endif
365         return CONNECTED;
366 }
367
368
369 /**
370  * Handle channel mode and channel-user mode changes
371  */
372 static bool
373 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
374 {
375         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
376             argadd[CLIENT_PASS_LEN], *mode_ptr;
377         bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
378         int mode_arg, arg_arg;
379         CLIENT *client;
380         long l;
381         size_t len;
382
383         if (Channel_IsModeless(Channel))
384                 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
385                                 Client_ID(Client), Channel_Name(Channel));
386
387         /* Mode request: let's answer it :-) */
388         if (Req->argc <= 1)
389                 return Channel_Mode_Answer_Request(Origin, Channel);
390
391         Channel_CheckAdminRights(Channel, Client, Origin,
392                                  &onchannel, &modeok, &use_servermode);
393
394         if (!onchannel && !modeok)
395                 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
396                                           Client_ID(Origin),
397                                           Channel_Name(Channel));
398
399         mode_arg = 1;
400         mode_ptr = Req->argv[mode_arg];
401         if (Req->argc > mode_arg + 1)
402                 arg_arg = mode_arg + 1;
403         else
404                 arg_arg = -1;
405
406         /* Initial state: set or unset modes? */
407         skiponce = false;
408         switch (*mode_ptr) {
409         case '-':
410                 set = false;
411                 break;
412         case '+':
413                 set = true;
414                 break;
415         default:
416                 set = true;
417                 skiponce = true;
418         }
419
420         /* Prepare reply string */
421         strcpy(the_modes, set ? "+" : "-");
422         the_args[0] = '\0';
423
424         x[1] = '\0';
425         connected = CONNECTED;
426         while (mode_ptr) {
427                 if (!skiponce)
428                         mode_ptr++;
429                 if (!*mode_ptr) {
430                         /* Try next argument if there's any */
431                         if (arg_arg < 0)
432                                 break;
433                         if (arg_arg > mode_arg)
434                                 mode_arg = arg_arg;
435                         else
436                                 mode_arg++;
437
438                         if (mode_arg >= Req->argc)
439                                 break;
440                         mode_ptr = Req->argv[mode_arg];
441
442                         if (Req->argc > mode_arg + 1)
443                                 arg_arg = mode_arg + 1;
444                         else
445                                 arg_arg = -1;
446                 }
447                 skiponce = false;
448
449                 switch (*mode_ptr) {
450                 case '+':
451                 case '-':
452                         if (((*mode_ptr == '+') && !set)
453                             || ((*mode_ptr == '-') && set)) {
454                                 /* Action modifier ("+"/"-") must be changed ... */
455                                 len = strlen(the_modes) - 1;
456                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
457                                         /* Adjust last action modifier in result */
458                                         the_modes[len] = *mode_ptr;
459                                 } else {
460                                         /* Append modifier character to result string */
461                                         x[0] = *mode_ptr;
462                                         strlcat(the_modes, x, sizeof(the_modes));
463                                 }
464                                 set = *mode_ptr == '+';
465                         }
466                         continue;
467                 }
468
469                 /* Are there arguments left? */
470                 if (arg_arg >= Req->argc)
471                         arg_arg = -1;
472
473                 /* Validate modes */
474                 x[0] = '\0';
475                 argadd[0] = '\0';
476                 client = NULL;
477                 switch (*mode_ptr) {
478                 /* --- Channel modes --- */
479                 case 'i': /* Invite only */
480                 case 'm': /* Moderated */
481                 case 'n': /* Only members can write */
482                 case 'R': /* Registered users only */
483                 case 's': /* Secret channel */
484                 case 't': /* Topic locked */
485                 case 'z': /* Secure connections only */
486                         if (modeok)
487                                 x[0] = *mode_ptr;
488                         else
489                                 connected = IRC_WriteStrClient(Origin,
490                                         ERR_CHANOPRIVSNEEDED_MSG,
491                                         Client_ID(Origin), Channel_Name(Channel));
492                         break;
493                 case 'k': /* Channel key */
494                         if (!set) {
495                                 if (modeok)
496                                         x[0] = *mode_ptr;
497                                 else
498                                         connected = IRC_WriteStrClient(Origin,
499                                                 ERR_CHANOPRIVSNEEDED_MSG,
500                                                 Client_ID(Origin),
501                                                 Channel_Name(Channel));
502                                 break;
503                         }
504                         if (arg_arg > mode_arg) {
505                                 if (modeok) {
506                                         Channel_ModeDel(Channel, 'k');
507                                         Channel_SetKey(Channel,
508                                                        Req->argv[arg_arg]);
509                                         strlcpy(argadd, Channel_Key(Channel),
510                                                 sizeof(argadd));
511                                         x[0] = *mode_ptr;
512                                 } else {
513                                         connected = IRC_WriteStrClient(Origin,
514                                                 ERR_CHANOPRIVSNEEDED_MSG,
515                                                 Client_ID(Origin),
516                                                 Channel_Name(Channel));
517                                 }
518                                 Req->argv[arg_arg][0] = '\0';
519                                 arg_arg++;
520                         } else {
521                                 connected = IRC_WriteStrClient(Origin,
522                                         ERR_NEEDMOREPARAMS_MSG,
523                                         Client_ID(Origin), Req->command);
524                                 goto chan_exit;
525                         }
526                         break;
527                 case 'l': /* Member limit */
528                         if (!set) {
529                                 if (modeok)
530                                         x[0] = *mode_ptr;
531                                 else
532                                         connected = IRC_WriteStrClient(Origin,
533                                                 ERR_CHANOPRIVSNEEDED_MSG,
534                                                 Client_ID(Origin),
535                                                 Channel_Name(Channel));
536                                 break;
537                         }
538                         if (arg_arg > mode_arg) {
539                                 if (modeok) {
540                                         l = atol(Req->argv[arg_arg]);
541                                         if (l > 0 && l < 0xFFFF) {
542                                                 Channel_ModeDel(Channel, 'l');
543                                                 Channel_SetMaxUsers(Channel, l);
544                                                 snprintf(argadd, sizeof(argadd),
545                                                          "%ld", l);
546                                                 x[0] = *mode_ptr;
547                                         }
548                                 } else {
549                                         connected = IRC_WriteStrClient(Origin,
550                                                 ERR_CHANOPRIVSNEEDED_MSG,
551                                                 Client_ID(Origin),
552                                                 Channel_Name(Channel));
553                                 }
554                                 Req->argv[arg_arg][0] = '\0';
555                                 arg_arg++;
556                         } else {
557                                 connected = IRC_WriteStrClient(Origin,
558                                         ERR_NEEDMOREPARAMS_MSG,
559                                         Client_ID(Origin), Req->command);
560                                 goto chan_exit;
561                         }
562                         break;
563                 case 'O': /* IRC operators only */
564                         if (modeok) {
565                                 /* Only IRC operators are allowed to
566                                  * set the 'O' channel mode! */
567                                 if (set && !(Client_OperByMe(Client)
568                                     || Client_Type(Client) == CLIENT_SERVER))
569                                         connected = IRC_WriteStrClient(Origin,
570                                                 ERR_NOPRIVILEGES_MSG,
571                                                 Client_ID(Origin));
572                                 else
573                                         x[0] = 'O';
574                         } else
575                                 connected = IRC_WriteStrClient(Origin,
576                                                 ERR_CHANOPRIVSNEEDED_MSG,
577                                                 Client_ID(Origin),
578                                                 Channel_Name(Channel));
579                                 break;
580                 case 'P': /* Persistent channel */
581                         if (modeok) {
582                                 /* Only IRC operators are allowed to
583                                  * set the 'P' channel mode! */
584                                 if (set && !(Client_OperByMe(Client)
585                                     || Client_Type(Client) == CLIENT_SERVER))
586                                         connected = IRC_WriteStrClient(Origin,
587                                                 ERR_NOPRIVILEGES_MSG,
588                                                 Client_ID(Origin));
589                                 else
590                                         x[0] = 'P';
591                         } else
592                                 connected = IRC_WriteStrClient(Origin,
593                                         ERR_CHANOPRIVSNEEDED_MSG,
594                                         Client_ID(Origin),
595                                         Channel_Name(Channel));
596                         break;
597                 /* --- Channel user modes --- */
598                 case 'a':
599                 case 'h':
600                 case 'q':
601                         if (Client_Type(Client) != CLIENT_SERVER) {
602                                 connected = IRC_WriteStrClient(Origin,
603                                         ERR_CHANOPRIVSNEEDED_MSG,
604                                         Client_ID(Origin),
605                                         Channel_Name(Channel));
606                                 goto chan_exit;
607                         }
608                 case 'o': /* Channel operator */
609                 case 'v': /* Voice */
610                         if (arg_arg > mode_arg) {
611                                 if (modeok) {
612                                         client = Client_Search(Req->argv[arg_arg]);
613                                         if (client)
614                                                 x[0] = *mode_ptr;
615                                         else
616                                                 connected = IRC_WriteStrClient(Client,
617                                                         ERR_NOSUCHNICK_MSG,
618                                                         Client_ID(Client),
619                                                         Req->argv[arg_arg]);
620                                 } else {
621                                         connected = IRC_WriteStrClient(Origin,
622                                                 ERR_CHANOPRIVSNEEDED_MSG,
623                                                 Client_ID(Origin),
624                                                 Channel_Name(Channel));
625                                 }
626                                 Req->argv[arg_arg][0] = '\0';
627                                 arg_arg++;
628                         } else {
629                                 connected = IRC_WriteStrClient(Origin,
630                                         ERR_NEEDMOREPARAMS_MSG,
631                                         Client_ID(Origin), Req->command);
632                                 goto chan_exit;
633                         }
634                         break;
635                 /* --- Channel lists --- */
636                 case 'I': /* Invite lists */
637                 case 'b': /* Ban lists */
638                         if (arg_arg > mode_arg) {
639                                 /* modify list */
640                                 if (modeok) {
641                                         connected = set
642                                            ? Add_Ban_Invite(*mode_ptr, Origin,
643                                                 Client, Channel,
644                                                 Req->argv[arg_arg])
645                                            : Del_Ban_Invite(*mode_ptr, Origin,
646                                                 Client, Channel,
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                                 if (*mode_ptr == 'I')
658                                         Channel_ShowInvites(Origin, Channel);
659                                 else
660                                         Channel_ShowBans(Origin, Channel);
661                         }
662                         break;
663                 default:
664                         if (Client_Type(Client) != CLIENT_SERVER) {
665                                 Log(LOG_DEBUG,
666                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
667                                     set ? '+' : '-', *mode_ptr,
668                                     Client_ID(Origin), Channel_Name(Channel));
669                                 connected = IRC_WriteStrClient(Origin,
670                                         ERR_UMODEUNKNOWNFLAG2_MSG,
671                                         Client_ID(Origin),
672                                         set ? '+' : '-', *mode_ptr);
673                                 x[0] = '\0';
674                         } else {
675                                 Log(LOG_DEBUG,
676                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
677                                     set ? '+' : '-', *mode_ptr,
678                                     Client_ID(Origin), Channel_Name(Channel));
679                                 x[0] = *mode_ptr;
680                         }
681                 }
682
683                 if (!connected)
684                         break;
685
686                 /* Is there a valid mode change? */
687                 if (!x[0])
688                         continue;
689
690                 /* Validate target client */
691                 if (client && (!Channel_IsMemberOf(Channel, client))) {
692                         if (!IRC_WriteStrClient
693                             (Origin, ERR_USERNOTINCHANNEL_MSG,
694                              Client_ID(Origin), Client_ID(client),
695                              Channel_Name(Channel)))
696                                 break;
697
698                         continue;
699                 }
700
701                 if (client) {
702                         /* Channel-User-Mode */
703                         retval = set
704                                ? Channel_UserModeAdd(Channel, client, x[0])
705                                : Channel_UserModeDel(Channel, client, x[0]);
706                         if (retval) {
707                                 strlcat(the_args, " ", sizeof(the_args));
708                                 strlcat(the_args, Client_ID(client),
709                                         sizeof(the_args));
710                                 strlcat(the_modes, x, sizeof(the_modes));
711                                 LogDebug
712                                     ("User \"%s\": Mode change on %s, now \"%s\"",
713                                      Client_Mask(client), Channel_Name(Channel),
714                                      Channel_UserModes(Channel, client));
715                         }
716                 } else {
717                         /* Channel-Mode */
718                         retval = set
719                                ? Channel_ModeAdd(Channel, x[0])
720                                : Channel_ModeDel(Channel, x[0]);
721                         if (retval) {
722                                 strlcat(the_modes, x, sizeof(the_modes));
723                                 LogDebug("Channel %s: Mode change, now \"%s\".",
724                                          Channel_Name(Channel),
725                                          Channel_Modes(Channel));
726                         }
727                 }
728
729                 /* Are there additional arguments to add? */
730                 if (argadd[0]) {
731                         strlcat(the_args, " ", sizeof(the_args));
732                         strlcat(the_args, argadd, sizeof(the_args));
733                 }
734         }
735
736       chan_exit:
737         /* Are there changed modes? */
738         if (the_modes[1]) {
739                 /* Clean up mode string */
740                 len = strlen(the_modes) - 1;
741                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
742                         the_modes[len] = '\0';
743
744                 if (Client_Type(Client) == CLIENT_SERVER) {
745                         /* MODE requests for local channels from other servers
746                          * are definitely invalid! */
747                         if (Channel_IsLocal(Channel)) {
748                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
749                                 return CONNECTED;
750                         }
751
752                         /* Forward mode changes to channel users and all the
753                          * other remote servers: */
754                         IRC_WriteStrServersPrefix(Client, Origin,
755                                 "MODE %s %s%s", Channel_Name(Channel),
756                                 the_modes, the_args);
757                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
758                                 false, "MODE %s %s%s", Channel_Name(Channel),
759                                 the_modes, the_args);
760                 } else {
761                         if (use_servermode)
762                                 Origin = Client_ThisServer();
763                         /* Send reply to client and inform other servers and channel users */
764                         connected = IRC_WriteStrClientPrefix(Client, Origin,
765                                         "MODE %s %s%s", Channel_Name(Channel),
766                                         the_modes, the_args);
767                         /* Only forward requests for non-local channels */
768                         if (!Channel_IsLocal(Channel))
769                                 IRC_WriteStrServersPrefix(Client, Origin,
770                                         "MODE %s %s%s", Channel_Name(Channel),
771                                         the_modes, the_args);
772                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
773                                 false, "MODE %s %s%s", Channel_Name(Channel),
774                                 the_modes, the_args);
775                 }
776         }
777
778         IRC_SetPenalty(Client, 1);
779         return connected;
780 } /* Channel_Mode */
781
782
783 GLOBAL bool
784 IRC_AWAY( CLIENT *Client, REQUEST *Req )
785 {
786         assert( Client != NULL );
787         assert( Req != NULL );
788
789         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
790
791         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
792         {
793                 Client_SetAway( Client, Req->argv[0] );
794                 Client_ModeAdd( Client, 'a' );
795                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
796                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
797         }
798         else
799         {
800                 Client_ModeDel( Client, 'a' );
801                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
802                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
803         }
804 } /* IRC_AWAY */
805
806
807 static bool
808 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
809 {
810         const char *mask;
811         bool already;
812         bool ret;
813
814         assert( Client != NULL );
815         assert( Channel != NULL );
816         assert( Pattern != NULL );
817         assert(what == 'I' || what == 'b');
818
819         mask = Lists_MakeMask(Pattern);
820
821         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
822         if (!already) {
823                 if (what == 'I')
824                         ret = Channel_AddInvite(Channel, mask, false);
825                 else
826                         ret = Channel_AddBan(Channel, mask);
827                 if (!ret)
828                         return CONNECTED;
829         }
830         if (already && (Client_Type(Prefix) == CLIENT_SERVER))
831                 return CONNECTED;
832
833         if (what == 'I')
834                 return Send_ListChange("+I", Prefix, Client, Channel, mask);
835         return Send_ListChange("+b", Prefix, Client, Channel, mask);
836 }
837
838
839 static bool
840 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
841 {
842         const char *mask;
843         struct list_head *list;
844
845         assert( Client != NULL );
846         assert( Channel != NULL );
847         assert( Pattern != NULL );
848         assert(what == 'I' || what == 'b');
849
850         mask = Lists_MakeMask( Pattern );
851
852         if (what == 'I')
853                 list = Channel_GetListInvites(Channel);
854         else
855                 list = Channel_GetListBans(Channel);
856
857         Lists_Del(list, mask);
858         if (what == 'I')
859                 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
860         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
861 }
862
863
864 static bool
865 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
866                 CHANNEL *Channel, const char *Mask)
867 {
868         bool ok;
869
870         if( Client_Type( Client ) == CLIENT_USER )
871         {
872                 /* send confirmation to client */
873                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
874         }
875         else ok = true;
876
877         /* to other servers */
878         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
879
880         /* and local users in channel */
881         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
882         
883         return ok;
884 } /* Send_ListChange */
885
886
887 /* -eof- */