]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
ba44a3ba286f1e592058b2fced15e262183c747d
[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 '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                                 goto chan_exit;
675                         } else {
676                                 Log(LOG_DEBUG,
677                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
678                                     set ? '+' : '-', *mode_ptr,
679                                     Client_ID(Origin), Channel_Name(Channel));
680                                 x[0] = *mode_ptr;
681                         }
682                 }
683
684                 if (!connected)
685                         break;
686
687                 /* Is there a valid mode change? */
688                 if (!x[0])
689                         continue;
690
691                 /* Validate target client */
692                 if (client && (!Channel_IsMemberOf(Channel, client))) {
693                         if (!IRC_WriteStrClient
694                             (Origin, ERR_USERNOTINCHANNEL_MSG,
695                              Client_ID(Origin), Client_ID(client),
696                              Channel_Name(Channel)))
697                                 break;
698
699                         continue;
700                 }
701
702                 if (client) {
703                         /* Channel-User-Mode */
704                         retval = set
705                                ? Channel_UserModeAdd(Channel, client, x[0])
706                                : Channel_UserModeDel(Channel, client, x[0]);
707                         if (retval) {
708                                 strlcat(the_args, " ", sizeof(the_args));
709                                 strlcat(the_args, Client_ID(client),
710                                         sizeof(the_args));
711                                 strlcat(the_modes, x, sizeof(the_modes));
712                                 LogDebug
713                                     ("User \"%s\": Mode change on %s, now \"%s\"",
714                                      Client_Mask(client), Channel_Name(Channel),
715                                      Channel_UserModes(Channel, client));
716                         }
717                 } else {
718                         /* Channel-Mode */
719                         retval = set
720                                ? Channel_ModeAdd(Channel, x[0])
721                                : Channel_ModeDel(Channel, x[0]);
722                         if (retval) {
723                                 strlcat(the_modes, x, sizeof(the_modes));
724                                 LogDebug("Channel %s: Mode change, now \"%s\".",
725                                          Channel_Name(Channel),
726                                          Channel_Modes(Channel));
727                         }
728                 }
729
730                 /* Are there additional arguments to add? */
731                 if (argadd[0]) {
732                         strlcat(the_args, " ", sizeof(the_args));
733                         strlcat(the_args, argadd, sizeof(the_args));
734                 }
735         }
736
737       chan_exit:
738         /* Are there changed modes? */
739         if (the_modes[1]) {
740                 /* Clean up mode string */
741                 len = strlen(the_modes) - 1;
742                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
743                         the_modes[len] = '\0';
744
745                 if (Client_Type(Client) == CLIENT_SERVER) {
746                         /* MODE requests for local channels from other servers
747                          * are definitely invalid! */
748                         if (Channel_IsLocal(Channel)) {
749                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
750                                 return CONNECTED;
751                         }
752
753                         /* Forward mode changes to channel users and all the
754                          * other remote servers: */
755                         IRC_WriteStrServersPrefix(Client, Origin,
756                                 "MODE %s %s%s", Channel_Name(Channel),
757                                 the_modes, the_args);
758                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
759                                 false, "MODE %s %s%s", Channel_Name(Channel),
760                                 the_modes, the_args);
761                 } else {
762                         if (use_servermode)
763                                 Origin = Client_ThisServer();
764                         /* Send reply to client and inform other servers and channel users */
765                         connected = IRC_WriteStrClientPrefix(Client, Origin,
766                                         "MODE %s %s%s", Channel_Name(Channel),
767                                         the_modes, the_args);
768                         /* Only forward requests for non-local channels */
769                         if (!Channel_IsLocal(Channel))
770                                 IRC_WriteStrServersPrefix(Client, Origin,
771                                         "MODE %s %s%s", Channel_Name(Channel),
772                                         the_modes, the_args);
773                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
774                                 false, "MODE %s %s%s", Channel_Name(Channel),
775                                 the_modes, the_args);
776                 }
777         }
778
779         IRC_SetPenalty(Client, 1);
780         return connected;
781 } /* Channel_Mode */
782
783
784 GLOBAL bool
785 IRC_AWAY( CLIENT *Client, REQUEST *Req )
786 {
787         assert( Client != NULL );
788         assert( Req != NULL );
789
790         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
791
792         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
793         {
794                 Client_SetAway( Client, Req->argv[0] );
795                 Client_ModeAdd( Client, 'a' );
796                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
797                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
798         }
799         else
800         {
801                 Client_ModeDel( Client, 'a' );
802                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
803                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
804         }
805 } /* IRC_AWAY */
806
807
808 static bool
809 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
810 {
811         const char *mask;
812         bool already;
813         bool ret;
814
815         assert( Client != NULL );
816         assert( Channel != NULL );
817         assert( Pattern != NULL );
818         assert(what == 'I' || what == 'b');
819
820         mask = Lists_MakeMask(Pattern);
821
822         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
823         if (!already) {
824                 if (what == 'I')
825                         ret = Channel_AddInvite(Channel, mask, false);
826                 else
827                         ret = Channel_AddBan(Channel, mask);
828                 if (!ret)
829                         return CONNECTED;
830         }
831         if (already && (Client_Type(Prefix) == CLIENT_SERVER))
832                 return CONNECTED;
833
834         if (what == 'I')
835                 return Send_ListChange("+I", Prefix, Client, Channel, mask);
836         return Send_ListChange("+b", Prefix, Client, Channel, mask);
837 }
838
839
840 static bool
841 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
842 {
843         const char *mask;
844         struct list_head *list;
845
846         assert( Client != NULL );
847         assert( Channel != NULL );
848         assert( Pattern != NULL );
849         assert(what == 'I' || what == 'b');
850
851         mask = Lists_MakeMask( Pattern );
852
853         if (what == 'I')
854                 list = Channel_GetListInvites(Channel);
855         else
856                 list = Channel_GetListBans(Channel);
857
858         Lists_Del(list, mask);
859         if (what == 'I')
860                 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
861         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
862 }
863
864
865 static bool
866 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
867                 CHANNEL *Channel, const char *Mask)
868 {
869         bool ok;
870
871         if( Client_Type( Client ) == CLIENT_USER )
872         {
873                 /* send confirmation to client */
874                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
875         }
876         else ok = true;
877
878         /* to other servers */
879         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
880
881         /* and local users in channel */
882         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
883         
884         return ok;
885 } /* Send_ListChange */
886
887
888 /* -eof- */