]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
d8329b1655e2a5f66b88277dd5a0274d86b09ab7
[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                         } 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                 /* Remoce 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 > mode_arg)
432                                 mode_arg = arg_arg;
433                         else
434                                 mode_arg++;
435
436                         if (mode_arg >= Req->argc)
437                                 break;
438                         mode_ptr = Req->argv[mode_arg];
439
440                         if (Req->argc > mode_arg + 1)
441                                 arg_arg = mode_arg + 1;
442                         else
443                                 arg_arg = -1;
444                 }
445                 skiponce = false;
446
447                 switch (*mode_ptr) {
448                 case '+':
449                 case '-':
450                         if (((*mode_ptr == '+') && !set)
451                             || ((*mode_ptr == '-') && set)) {
452                                 /* Action modifier ("+"/"-") must be changed ... */
453                                 len = strlen(the_modes) - 1;
454                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
455                                         /* Adjust last action modifier in result */
456                                         the_modes[len] = *mode_ptr;
457                                 } else {
458                                         /* Append modifier character to result string */
459                                         x[0] = *mode_ptr;
460                                         strlcat(the_modes, x, sizeof(the_modes));
461                                 }
462                                 set = *mode_ptr == '+';
463                         }
464                         continue;
465                 }
466
467                 /* Are there arguments left? */
468                 if (arg_arg >= Req->argc)
469                         arg_arg = -1;
470
471                 /* Validate modes */
472                 x[0] = '\0';
473                 argadd[0] = '\0';
474                 client = NULL;
475                 switch (*mode_ptr) {
476                 /* --- Channel modes --- */
477                 case 'i': /* Invite only */
478                 case 'm': /* Moderated */
479                 case 'n': /* Only members can write */
480                 case 'R': /* Registered users only */
481                 case 's': /* Secret channel */
482                 case 't': /* Topic locked */
483                 case 'z': /* Secure connections only */
484                         if (modeok)
485                                 x[0] = *mode_ptr;
486                         else
487                                 connected = IRC_WriteStrClient(Origin,
488                                         ERR_CHANOPRIVSNEEDED_MSG,
489                                         Client_ID(Origin), Channel_Name(Channel));
490                         break;
491                 case 'k': /* Channel key */
492                         if (!set) {
493                                 if (modeok)
494                                         x[0] = *mode_ptr;
495                                 else
496                                         connected = IRC_WriteStrClient(Origin,
497                                                 ERR_CHANOPRIVSNEEDED_MSG,
498                                                 Client_ID(Origin),
499                                                 Channel_Name(Channel));
500                                 break;
501                         }
502                         if (arg_arg > mode_arg) {
503                                 if (modeok) {
504                                         Channel_ModeDel(Channel, 'k');
505                                         Channel_SetKey(Channel,
506                                                        Req->argv[arg_arg]);
507                                         strlcpy(argadd, Channel_Key(Channel),
508                                                 sizeof(argadd));
509                                         x[0] = *mode_ptr;
510                                 } else {
511                                         connected = IRC_WriteStrClient(Origin,
512                                                 ERR_CHANOPRIVSNEEDED_MSG,
513                                                 Client_ID(Origin),
514                                                 Channel_Name(Channel));
515                                 }
516                                 Req->argv[arg_arg][0] = '\0';
517                                 arg_arg++;
518                         } else {
519                                 connected = IRC_WriteStrClient(Origin,
520                                         ERR_NEEDMOREPARAMS_MSG,
521                                         Client_ID(Origin), Req->command);
522                                 goto chan_exit;
523                         }
524                         break;
525                 case 'l': /* Member limit */
526                         if (!set) {
527                                 if (modeok)
528                                         x[0] = *mode_ptr;
529                                 else
530                                         connected = IRC_WriteStrClient(Origin,
531                                                 ERR_CHANOPRIVSNEEDED_MSG,
532                                                 Client_ID(Origin),
533                                                 Channel_Name(Channel));
534                                 break;
535                         }
536                         if (arg_arg > mode_arg) {
537                                 if (modeok) {
538                                         l = atol(Req->argv[arg_arg]);
539                                         if (l > 0 && l < 0xFFFF) {
540                                                 Channel_ModeDel(Channel, 'l');
541                                                 Channel_SetMaxUsers(Channel, l);
542                                                 snprintf(argadd, sizeof(argadd),
543                                                          "%ld", l);
544                                                 x[0] = *mode_ptr;
545                                         }
546                                 } else {
547                                         connected = IRC_WriteStrClient(Origin,
548                                                 ERR_CHANOPRIVSNEEDED_MSG,
549                                                 Client_ID(Origin),
550                                                 Channel_Name(Channel));
551                                 }
552                                 Req->argv[arg_arg][0] = '\0';
553                                 arg_arg++;
554                         } else {
555                                 connected = IRC_WriteStrClient(Origin,
556                                         ERR_NEEDMOREPARAMS_MSG,
557                                         Client_ID(Origin), Req->command);
558                                 goto chan_exit;
559                         }
560                         break;
561                 case 'O': /* IRC operators only */
562                         if (modeok) {
563                                 /* Only IRC operators are allowed to
564                                  * set the 'O' channel mode! */
565                                 if (set && !(Client_OperByMe(Client)
566                                     || Client_Type(Client) == CLIENT_SERVER))
567                                         connected = IRC_WriteStrClient(Origin,
568                                                 ERR_NOPRIVILEGES_MSG,
569                                                 Client_ID(Origin));
570                                 else
571                                         x[0] = 'O';
572                         } else
573                                 connected = IRC_WriteStrClient(Origin,
574                                                 ERR_CHANOPRIVSNEEDED_MSG,
575                                                 Client_ID(Origin),
576                                                 Channel_Name(Channel));
577                                 break;
578                 case 'P': /* Persistent channel */
579                         if (modeok) {
580                                 /* Only IRC operators are allowed to
581                                  * set the 'P' channel mode! */
582                                 if (set && !(Client_OperByMe(Client)
583                                     || Client_Type(Client) == CLIENT_SERVER))
584                                         connected = IRC_WriteStrClient(Origin,
585                                                 ERR_NOPRIVILEGES_MSG,
586                                                 Client_ID(Origin));
587                                 else
588                                         x[0] = 'P';
589                         } else
590                                 connected = IRC_WriteStrClient(Origin,
591                                         ERR_CHANOPRIVSNEEDED_MSG,
592                                         Client_ID(Origin),
593                                         Channel_Name(Channel));
594                         break;
595                 /* --- Channel user modes --- */
596                 case 'a':
597                 case 'h':
598                 case 'q':
599                         if (Client_Type(Client) != CLIENT_SERVER) {
600                                 connected = IRC_WriteStrClient(Origin,
601                                         ERR_CHANOPRIVSNEEDED_MSG,
602                                         Client_ID(Origin),
603                                         Channel_Name(Channel));
604                                 goto chan_exit;
605                         }
606                 case 'o': /* Channel operator */
607                 case 'v': /* Voice */
608                         if (arg_arg > mode_arg) {
609                                 if (modeok) {
610                                         client = Client_Search(Req->argv[arg_arg]);
611                                         if (client)
612                                                 x[0] = *mode_ptr;
613                                         else
614                                                 connected = IRC_WriteStrClient(Client,
615                                                         ERR_NOSUCHNICK_MSG,
616                                                         Client_ID(Client),
617                                                         Req->argv[arg_arg]);
618                                 } else {
619                                         connected = IRC_WriteStrClient(Origin,
620                                                 ERR_CHANOPRIVSNEEDED_MSG,
621                                                 Client_ID(Origin),
622                                                 Channel_Name(Channel));
623                                 }
624                                 Req->argv[arg_arg][0] = '\0';
625                                 arg_arg++;
626                         } else {
627                                 connected = IRC_WriteStrClient(Origin,
628                                         ERR_NEEDMOREPARAMS_MSG,
629                                         Client_ID(Origin), Req->command);
630                                 goto chan_exit;
631                         }
632                         break;
633                 /* --- Channel lists --- */
634                 case 'I': /* Invite lists */
635                 case 'b': /* Ban lists */
636                         if (arg_arg > mode_arg) {
637                                 /* modify list */
638                                 if (modeok) {
639                                         connected = set
640                                            ? Add_Ban_Invite(*mode_ptr, Origin,
641                                                 Client, Channel,
642                                                 Req->argv[arg_arg])
643                                            : Del_Ban_Invite(*mode_ptr, Origin,
644                                                 Client, Channel,
645                                                 Req->argv[arg_arg]);
646                                 } else {
647                                         connected = IRC_WriteStrClient(Origin,
648                                                 ERR_CHANOPRIVSNEEDED_MSG,
649                                                 Client_ID(Origin),
650                                                 Channel_Name(Channel));
651                                 }
652                                 Req->argv[arg_arg][0] = '\0';
653                                 arg_arg++;
654                         } else {
655                                 if (*mode_ptr == 'I')
656                                         Channel_ShowInvites(Origin, Channel);
657                                 else
658                                         Channel_ShowBans(Origin, Channel);
659                         }
660                         break;
661                 default:
662                         if (Client_Type(Client) != CLIENT_SERVER) {
663                                 Log(LOG_DEBUG,
664                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
665                                     set ? '+' : '-', *mode_ptr,
666                                     Client_ID(Origin), Channel_Name(Channel));
667                                 connected = IRC_WriteStrClient(Origin,
668                                         ERR_UMODEUNKNOWNFLAG2_MSG,
669                                         Client_ID(Origin),
670                                         set ? '+' : '-', *mode_ptr);
671                                 x[0] = '\0';
672                         } else {
673                                 Log(LOG_DEBUG,
674                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
675                                     set ? '+' : '-', *mode_ptr,
676                                     Client_ID(Origin), Channel_Name(Channel));
677                                 x[0] = *mode_ptr;
678                         }
679                 }
680
681                 if (!connected)
682                         break;
683
684                 /* Is there a valid mode change? */
685                 if (!x[0])
686                         continue;
687
688                 /* Validate target client */
689                 if (client && (!Channel_IsMemberOf(Channel, client))) {
690                         if (!IRC_WriteStrClient
691                             (Origin, ERR_USERNOTINCHANNEL_MSG,
692                              Client_ID(Origin), Client_ID(client),
693                              Channel_Name(Channel)))
694                                 break;
695
696                         continue;
697                 }
698
699                 if (client) {
700                         /* Channel-User-Mode */
701                         retval = set
702                                ? Channel_UserModeAdd(Channel, client, x[0])
703                                : Channel_UserModeDel(Channel, client, x[0]);
704                         if (retval) {
705                                 strlcat(the_args, " ", sizeof(the_args));
706                                 strlcat(the_args, Client_ID(client),
707                                         sizeof(the_args));
708                                 strlcat(the_modes, x, sizeof(the_modes));
709                                 LogDebug
710                                     ("User \"%s\": Mode change on %s, now \"%s\"",
711                                      Client_Mask(client), Channel_Name(Channel),
712                                      Channel_UserModes(Channel, client));
713                         }
714                 } else {
715                         /* Channel-Mode */
716                         retval = set
717                                ? Channel_ModeAdd(Channel, x[0])
718                                : Channel_ModeDel(Channel, x[0]);
719                         if (retval) {
720                                 strlcat(the_modes, x, sizeof(the_modes));
721                                 LogDebug("Channel %s: Mode change, now \"%s\".",
722                                          Channel_Name(Channel),
723                                          Channel_Modes(Channel));
724                         }
725                 }
726
727                 /* Are there additional arguments to add? */
728                 if (argadd[0]) {
729                         strlcat(the_args, " ", sizeof(the_args));
730                         strlcat(the_args, argadd, sizeof(the_args));
731                 }
732         }
733
734       chan_exit:
735         /* Are there changed modes? */
736         if (the_modes[1]) {
737                 /* Clean up mode string */
738                 len = strlen(the_modes) - 1;
739                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
740                         the_modes[len] = '\0';
741
742                 if (Client_Type(Client) == CLIENT_SERVER) {
743                         /* MODE requests for local channels from other servers
744                          * are definitely invalid! */
745                         if (Channel_IsLocal(Channel)) {
746                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
747                                 return CONNECTED;
748                         }
749
750                         /* Forward mode changes to channel users and all the
751                          * other remote servers: */
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                 } else {
759                         if (use_servermode)
760                                 Origin = Client_ThisServer();
761                         /* Send reply to client and inform other servers and channel users */
762                         connected = IRC_WriteStrClientPrefix(Client, Origin,
763                                         "MODE %s %s%s", Channel_Name(Channel),
764                                         the_modes, the_args);
765                         /* Only forward requests for non-local channels */
766                         if (!Channel_IsLocal(Channel))
767                                 IRC_WriteStrServersPrefix(Client, Origin,
768                                         "MODE %s %s%s", Channel_Name(Channel),
769                                         the_modes, the_args);
770                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
771                                 false, "MODE %s %s%s", Channel_Name(Channel),
772                                 the_modes, the_args);
773                 }
774         }
775
776         IRC_SetPenalty(Client, 1);
777         return connected;
778 } /* Channel_Mode */
779
780
781 GLOBAL bool
782 IRC_AWAY( CLIENT *Client, REQUEST *Req )
783 {
784         assert( Client != NULL );
785         assert( Req != NULL );
786
787         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
788
789         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
790         {
791                 Client_SetAway( Client, Req->argv[0] );
792                 Client_ModeAdd( Client, 'a' );
793                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
794                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
795         }
796         else
797         {
798                 Client_ModeDel( Client, 'a' );
799                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
800                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
801         }
802 } /* IRC_AWAY */
803
804
805 static bool
806 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
807 {
808         const char *mask;
809         bool already;
810         bool ret;
811
812         assert( Client != NULL );
813         assert( Channel != NULL );
814         assert( Pattern != NULL );
815         assert(what == 'I' || what == 'b');
816
817         mask = Lists_MakeMask(Pattern);
818
819         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
820         if (!already) {
821                 if (what == 'I')
822                         ret = Channel_AddInvite(Channel, mask, false);
823                 else
824                         ret = Channel_AddBan(Channel, mask);
825                 if (!ret)
826                         return CONNECTED;
827         }
828         if (already && (Client_Type(Prefix) == CLIENT_SERVER))
829                 return CONNECTED;
830
831         if (what == 'I')
832                 return Send_ListChange("+I", Prefix, Client, Channel, mask);
833         return Send_ListChange("+b", Prefix, Client, Channel, mask);
834 }
835
836
837 static bool
838 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
839 {
840         const char *mask;
841         struct list_head *list;
842
843         assert( Client != NULL );
844         assert( Channel != NULL );
845         assert( Pattern != NULL );
846         assert(what == 'I' || what == 'b');
847
848         mask = Lists_MakeMask( Pattern );
849
850         if (what == 'I')
851                 list = Channel_GetListInvites(Channel);
852         else
853                 list = Channel_GetListBans(Channel);
854
855         Lists_Del(list, mask);
856         if (what == 'I')
857                 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
858         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
859 }
860
861
862 static bool
863 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
864                 CHANNEL *Channel, const char *Mask)
865 {
866         bool ok;
867
868         if( Client_Type( Client ) == CLIENT_USER )
869         {
870                 /* send confirmation to client */
871                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
872         }
873         else ok = true;
874
875         /* to other servers */
876         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
877
878         /* and local users in channel */
879         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
880         
881         return ok;
882 } /* Send_ListChange */
883
884
885 /* -eof- */