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