]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
Handle unknown user modes on server links
[ngircd-alex.git] / src / ngircd / irc-mode.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 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                                 goto client_exit;
257                         } else {
258                                 Log(LOG_DEBUG,
259                                     "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
260                                     set ? '+' : '-', *mode_ptr,
261                                     Client_ID(Origin), Client_ID(Target));
262                                 x[0] = *mode_ptr;
263                         }
264                 }
265
266                 if (!ok)
267                         break;
268
269                 /* Is there a valid mode change? */
270                 if (!x[0])
271                         continue;
272
273                 if (set) {
274                         if (Client_ModeAdd(Target, x[0]))
275                                 strlcat(the_modes, x, sizeof(the_modes));
276                 } else {
277                         if (Client_ModeDel(Target, x[0]))
278                                 strlcat(the_modes, x, sizeof(the_modes));
279                 }
280         }
281 client_exit:
282
283         /* Are there changed modes? */
284         if (the_modes[1]) {
285                 /* Remoce needless action modifier characters */
286                 len = strlen(the_modes) - 1;
287                 if (the_modes[len] == '+' || the_modes[len] == '-')
288                         the_modes[len] = '\0';
289
290                 if (Client_Type(Client) == CLIENT_SERVER) {
291                         /* Forward modes to other servers */
292                         if (Client_Conn(Target) != NONE) {
293                                 /* Remote server (service?) changed modes
294                                  * for one of our clients. Inform it! */
295                                 IRC_WriteStrClientPrefix(Target, Origin,
296                                                          "MODE %s :%s",
297                                                          Client_ID(Target),
298                                                          the_modes);
299                         }
300                         IRC_WriteStrServersPrefix(Client, Origin,
301                                                   "MODE %s :%s",
302                                                   Client_ID(Target),
303                                                   the_modes);
304                 } else {
305                         /* Send reply to client and inform other servers */
306                         ok = IRC_WriteStrClientPrefix(Client, Origin,
307                                                       "MODE %s :%s",
308                                                       Client_ID(Target),
309                                                       the_modes);
310                         IRC_WriteStrServersPrefix(Client, Origin,
311                                                   "MODE %s :%s",
312                                                   Client_ID(Target),
313                                                   the_modes);
314                 }
315                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
316                          Client_TypeText(Target), Client_Mask(Target),
317                          Client_Modes(Target));
318         }
319
320         IRC_SetPenalty(Client, 1);
321         return ok;
322 } /* Client_Mode */
323
324
325 static bool
326 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
327 {
328         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
329         const char *mode_ptr;
330
331         /* Member or not? -- That's the question! */
332         if (!Channel_IsMemberOf(Channel, Origin))
333                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
334                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
335
336         /* The sender is a member: generate extended reply */
337         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
338         mode_ptr = the_modes;
339         the_args[0] = '\0';
340
341         while(*mode_ptr) {
342                 switch(*mode_ptr) {
343                 case 'l':
344                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
345                         strlcat(the_args, argadd, sizeof(the_args));
346                         break;
347                 case 'k':
348                         strlcat(the_args, " ", sizeof(the_args));
349                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
350                         break;
351                 }
352                 mode_ptr++;
353         }
354         if (the_args[0])
355                 strlcat(the_modes, the_args, sizeof(the_modes));
356
357         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
358                                 Client_ID(Origin), Channel_Name(Channel),
359                                 the_modes))
360                 return DISCONNECTED;
361 #ifndef STRICT_RFC
362         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
363                                   Client_ID(Origin), Channel_Name(Channel),
364                                   Channel_CreationTime(Channel)))
365                 return DISCONNECTED;
366 #endif
367         return CONNECTED;
368 }
369
370
371 /**
372  * Handle channel mode and channel-user mode changes
373  */
374 static bool
375 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
376 {
377         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
378             argadd[CLIENT_PASS_LEN], *mode_ptr;
379         bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
380         int mode_arg, arg_arg;
381         CLIENT *client;
382         long l;
383         size_t len;
384
385         if (Channel_IsModeless(Channel))
386                 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
387                                 Client_ID(Client), Channel_Name(Channel));
388
389         /* Mode request: let's answer it :-) */
390         if (Req->argc <= 1)
391                 return Channel_Mode_Answer_Request(Origin, Channel);
392
393         Channel_CheckAdminRights(Channel, Client, Origin,
394                                  &onchannel, &modeok, &use_servermode);
395
396         if (!onchannel && !modeok)
397                 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
398                                           Client_ID(Origin),
399                                           Channel_Name(Channel));
400
401         mode_arg = 1;
402         mode_ptr = Req->argv[mode_arg];
403         if (Req->argc > mode_arg + 1)
404                 arg_arg = mode_arg + 1;
405         else
406                 arg_arg = -1;
407
408         /* Initial state: set or unset modes? */
409         skiponce = false;
410         switch (*mode_ptr) {
411         case '-':
412                 set = false;
413                 break;
414         case '+':
415                 set = true;
416                 break;
417         default:
418                 set = true;
419                 skiponce = true;
420         }
421
422         /* Prepare reply string */
423         strcpy(the_modes, set ? "+" : "-");
424         the_args[0] = '\0';
425
426         x[1] = '\0';
427         connected = CONNECTED;
428         while (mode_ptr) {
429                 if (!skiponce)
430                         mode_ptr++;
431                 if (!*mode_ptr) {
432                         /* Try next argument if there's any */
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 's': /* Secret channel */
483                 case 't': /* Topic locked */
484                 case 'z': /* Secure connections only */
485                         if (modeok)
486                                 x[0] = *mode_ptr;
487                         else
488                                 connected = IRC_WriteStrClient(Origin,
489                                         ERR_CHANOPRIVSNEEDED_MSG,
490                                         Client_ID(Origin), Channel_Name(Channel));
491                         break;
492                 case 'k': /* Channel key */
493                         if (!set) {
494                                 if (modeok)
495                                         x[0] = *mode_ptr;
496                                 else
497                                         connected = IRC_WriteStrClient(Origin,
498                                                 ERR_CHANOPRIVSNEEDED_MSG,
499                                                 Client_ID(Origin),
500                                                 Channel_Name(Channel));
501                                 break;
502                         }
503                         if (arg_arg > mode_arg) {
504                                 if (modeok) {
505                                         Channel_ModeDel(Channel, 'k');
506                                         Channel_SetKey(Channel,
507                                                        Req->argv[arg_arg]);
508                                         strlcpy(argadd, Channel_Key(Channel),
509                                                 sizeof(argadd));
510                                         x[0] = *mode_ptr;
511                                 } else {
512                                         connected = IRC_WriteStrClient(Origin,
513                                                 ERR_CHANOPRIVSNEEDED_MSG,
514                                                 Client_ID(Origin),
515                                                 Channel_Name(Channel));
516                                 }
517                                 Req->argv[arg_arg][0] = '\0';
518                                 arg_arg++;
519                         } else {
520                                 connected = IRC_WriteStrClient(Origin,
521                                         ERR_NEEDMOREPARAMS_MSG,
522                                         Client_ID(Origin), Req->command);
523                                 goto chan_exit;
524                         }
525                         break;
526                 case 'l': /* Member limit */
527                         if (!set) {
528                                 if (modeok)
529                                         x[0] = *mode_ptr;
530                                 else
531                                         connected = IRC_WriteStrClient(Origin,
532                                                 ERR_CHANOPRIVSNEEDED_MSG,
533                                                 Client_ID(Origin),
534                                                 Channel_Name(Channel));
535                                 break;
536                         }
537                         if (arg_arg > mode_arg) {
538                                 if (modeok) {
539                                         l = atol(Req->argv[arg_arg]);
540                                         if (l > 0 && l < 0xFFFF) {
541                                                 Channel_ModeDel(Channel, 'l');
542                                                 Channel_SetMaxUsers(Channel, l);
543                                                 snprintf(argadd, sizeof(argadd),
544                                                          "%ld", l);
545                                                 x[0] = *mode_ptr;
546                                         }
547                                 } else {
548                                         connected = IRC_WriteStrClient(Origin,
549                                                 ERR_CHANOPRIVSNEEDED_MSG,
550                                                 Client_ID(Origin),
551                                                 Channel_Name(Channel));
552                                 }
553                                 Req->argv[arg_arg][0] = '\0';
554                                 arg_arg++;
555                         } else {
556                                 connected = IRC_WriteStrClient(Origin,
557                                         ERR_NEEDMOREPARAMS_MSG,
558                                         Client_ID(Origin), Req->command);
559                                 goto chan_exit;
560                         }
561                         break;
562                 case 'O': /* IRC operators only */
563                         if (modeok) {
564                                 /* Only IRC operators are allowed to
565                                  * set the 'O' channel mode! */
566                                 if (set && !(Client_OperByMe(Client)
567                                     || Client_Type(Client) == CLIENT_SERVER))
568                                         connected = IRC_WriteStrClient(Origin,
569                                                 ERR_NOPRIVILEGES_MSG,
570                                                 Client_ID(Origin));
571                                 else
572                                         x[0] = 'O';
573                         } else
574                                 connected = IRC_WriteStrClient(Origin,
575                                                 ERR_CHANOPRIVSNEEDED_MSG,
576                                                 Client_ID(Origin),
577                                                 Channel_Name(Channel));
578                                 break;
579                 case 'P': /* Persistent channel */
580                         if (modeok) {
581                                 /* Only IRC operators are allowed to
582                                  * set the 'P' channel mode! */
583                                 if (set && !(Client_OperByMe(Client)
584                                     || Client_Type(Client) == CLIENT_SERVER))
585                                         connected = IRC_WriteStrClient(Origin,
586                                                 ERR_NOPRIVILEGES_MSG,
587                                                 Client_ID(Origin));
588                                 else
589                                         x[0] = 'P';
590                         } else
591                                 connected = IRC_WriteStrClient(Origin,
592                                         ERR_CHANOPRIVSNEEDED_MSG,
593                                         Client_ID(Origin),
594                                         Channel_Name(Channel));
595                         break;
596                 /* --- Channel user modes --- */
597                 case 'o': /* Channel operator */
598                 case 'v': /* Voice */
599                         if (arg_arg > mode_arg) {
600                                 if (modeok) {
601                                         client = Client_Search(Req->argv[arg_arg]);
602                                         if (client)
603                                                 x[0] = *mode_ptr;
604                                         else
605                                                 connected = IRC_WriteStrClient(Client,
606                                                         ERR_NOSUCHNICK_MSG,
607                                                         Client_ID(Client),
608                                                         Req->argv[arg_arg]);
609                                 } else {
610                                         connected = IRC_WriteStrClient(Origin,
611                                                 ERR_CHANOPRIVSNEEDED_MSG,
612                                                 Client_ID(Origin),
613                                                 Channel_Name(Channel));
614                                 }
615                                 Req->argv[arg_arg][0] = '\0';
616                                 arg_arg++;
617                         } else {
618                                 connected = IRC_WriteStrClient(Origin,
619                                         ERR_NEEDMOREPARAMS_MSG,
620                                         Client_ID(Origin), Req->command);
621                                 goto chan_exit;
622                         }
623                         break;
624                 /* --- Channel lists --- */
625                 case 'I': /* Invite lists */
626                 case 'b': /* Ban lists */
627                         if (arg_arg > mode_arg) {
628                                 /* modify list */
629                                 if (modeok) {
630                                         connected = set
631                                            ? Add_Ban_Invite(*mode_ptr, Origin,
632                                                 Client, Channel,
633                                                 Req->argv[arg_arg])
634                                            : Del_Ban_Invite(*mode_ptr, Origin,
635                                                 Client, Channel,
636                                                 Req->argv[arg_arg]);
637                                 } else {
638                                         connected = IRC_WriteStrClient(Origin,
639                                                 ERR_CHANOPRIVSNEEDED_MSG,
640                                                 Client_ID(Origin),
641                                                 Channel_Name(Channel));
642                                 }
643                                 Req->argv[arg_arg][0] = '\0';
644                                 arg_arg++;
645                         } else {
646                                 if (*mode_ptr == 'I')
647                                         Channel_ShowInvites(Origin, Channel);
648                                 else
649                                         Channel_ShowBans(Origin, Channel);
650                         }
651                         break;
652                 default:
653                         Log(LOG_DEBUG,
654                             "Unknown mode \"%c%c\" from \"%s\" on %s!?",
655                             set ? '+' : '-', *mode_ptr, Client_ID(Origin),
656                             Channel_Name(Channel));
657                         if (Client_Type(Client) != CLIENT_SERVER)
658                                 connected = IRC_WriteStrClient(Origin,
659                                         ERR_UMODEUNKNOWNFLAG2_MSG,
660                                         Client_ID(Origin),
661                                         set ? '+' : '-', *mode_ptr);
662                         x[0] = '\0';
663                         goto chan_exit;
664                 }       /* switch() */
665
666                 if (!connected)
667                         break;
668
669                 /* Is there a valid mode change? */
670                 if (!x[0])
671                         continue;
672
673                 /* Validate target client */
674                 if (client && (!Channel_IsMemberOf(Channel, client))) {
675                         if (!IRC_WriteStrClient
676                             (Origin, ERR_USERNOTINCHANNEL_MSG,
677                              Client_ID(Origin), Client_ID(client),
678                              Channel_Name(Channel)))
679                                 break;
680
681                         continue;
682                 }
683
684                 if (client) {
685                         /* Channel-User-Mode */
686                         retval = set
687                                ? Channel_UserModeAdd(Channel, client, x[0])
688                                : Channel_UserModeDel(Channel, client, x[0]);
689                         if (retval) {
690                                 strlcat(the_args, " ", sizeof(the_args));
691                                 strlcat(the_args, Client_ID(client),
692                                         sizeof(the_args));
693                                 strlcat(the_modes, x, sizeof(the_modes));
694                                 LogDebug
695                                     ("User \"%s\": Mode change on %s, now \"%s\"",
696                                      Client_Mask(client), Channel_Name(Channel),
697                                      Channel_UserModes(Channel, client));
698                         }
699                 } else {
700                         /* Channel-Mode */
701                         retval = set
702                                ? Channel_ModeAdd(Channel, x[0])
703                                : Channel_ModeDel(Channel, x[0]);
704                         if (retval) {
705                                 strlcat(the_modes, x, sizeof(the_modes));
706                                 LogDebug("Channel %s: Mode change, now \"%s\".",
707                                          Channel_Name(Channel),
708                                          Channel_Modes(Channel));
709                         }
710                 }
711
712                 /* Are there additional arguments to add? */
713                 if (argadd[0]) {
714                         strlcat(the_args, " ", sizeof(the_args));
715                         strlcat(the_args, argadd, sizeof(the_args));
716                 }
717         }
718
719       chan_exit:
720         /* Are there changed modes? */
721         if (the_modes[1]) {
722                 /* Clean up mode string */
723                 len = strlen(the_modes) - 1;
724                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
725                         the_modes[len] = '\0';
726
727                 if (Client_Type(Client) == CLIENT_SERVER) {
728                         /* MODE requests for local channels from other servers
729                          * are definitely invalid! */
730                         if (Channel_IsLocal(Channel)) {
731                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
732                                 return CONNECTED;
733                         }
734
735                         /* Forward mode changes to channel users and all the
736                          * other remote servers: */
737                         IRC_WriteStrServersPrefix(Client, Origin,
738                                 "MODE %s %s%s", Channel_Name(Channel),
739                                 the_modes, the_args);
740                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
741                                 false, "MODE %s %s%s", Channel_Name(Channel),
742                                 the_modes, the_args);
743                 } else {
744                         if (use_servermode)
745                                 Origin = Client_ThisServer();
746                         /* Send reply to client and inform other servers and channel users */
747                         connected = IRC_WriteStrClientPrefix(Client, Origin,
748                                         "MODE %s %s%s", Channel_Name(Channel),
749                                         the_modes, the_args);
750                         /* Only forward requests for non-local channels */
751                         if (!Channel_IsLocal(Channel))
752                                 IRC_WriteStrServersPrefix(Client, Origin,
753                                         "MODE %s %s%s", Channel_Name(Channel),
754                                         the_modes, the_args);
755                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
756                                 false, "MODE %s %s%s", Channel_Name(Channel),
757                                 the_modes, the_args);
758                 }
759         }
760
761         IRC_SetPenalty(Client, 1);
762         return connected;
763 } /* Channel_Mode */
764
765
766 GLOBAL bool
767 IRC_AWAY( CLIENT *Client, REQUEST *Req )
768 {
769         assert( Client != NULL );
770         assert( Req != NULL );
771
772         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
773
774         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
775         {
776                 Client_SetAway( Client, Req->argv[0] );
777                 Client_ModeAdd( Client, 'a' );
778                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
779                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
780         }
781         else
782         {
783                 Client_ModeDel( Client, 'a' );
784                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
785                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
786         }
787 } /* IRC_AWAY */
788
789
790 static bool
791 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
792 {
793         const char *mask;
794         bool already;
795         bool ret;
796
797         assert( Client != NULL );
798         assert( Channel != NULL );
799         assert( Pattern != NULL );
800         assert(what == 'I' || what == 'b');
801
802         mask = Lists_MakeMask(Pattern);
803
804         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
805         if (!already) {
806                 if (what == 'I')
807                         ret = Channel_AddInvite(Channel, mask, false);
808                 else
809                         ret = Channel_AddBan(Channel, mask);
810                 if (!ret)
811                         return CONNECTED;
812         }
813         if (already && (Client_Type(Prefix) == CLIENT_SERVER))
814                 return CONNECTED;
815
816         if (what == 'I')
817                 return Send_ListChange("+I", Prefix, Client, Channel, mask);
818         return Send_ListChange("+b", Prefix, Client, Channel, mask);
819 }
820
821
822 static bool
823 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
824 {
825         const char *mask;
826         struct list_head *list;
827
828         assert( Client != NULL );
829         assert( Channel != NULL );
830         assert( Pattern != NULL );
831         assert(what == 'I' || what == 'b');
832
833         mask = Lists_MakeMask( Pattern );
834
835         if (what == 'I')
836                 list = Channel_GetListInvites(Channel);
837         else
838                 list = Channel_GetListBans(Channel);
839
840         Lists_Del(list, mask);
841         if (what == 'I')
842                 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
843         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
844 }
845
846
847 static bool
848 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
849                 CHANNEL *Channel, const char *Mask)
850 {
851         bool ok;
852
853         if( Client_Type( Client ) == CLIENT_USER )
854         {
855                 /* send confirmation to client */
856                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
857         }
858         else ok = true;
859
860         /* to other servers */
861         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
862
863         /* and local users in channel */
864         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
865         
866         return ok;
867 } /* Send_ListChange */
868
869
870 /* -eof- */