]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
Always cloak client hostname, if needed
[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_To_List PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
45                                 CHANNEL *Channel, const char *Pattern));
46 static bool Del_From_List 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 'C': /* Only messages from clients sharing a channel */
218                 case 'i': /* Invisible */
219                 case 's': /* Server messages */
220                 case 'w': /* Wallops messages */
221                         x[0] = *mode_ptr;
222                         break;
223                 case 'a': /* Away */
224                         if (Client_Type(Client) == CLIENT_SERVER) {
225                                 x[0] = 'a';
226                                 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
227                         } else
228                                 ok = IRC_WriteStrClient(Origin,
229                                                         ERR_NOPRIVILEGES_MSG,
230                                                         Client_ID(Origin));
231                         break;
232                 case 'B': /* Bot */
233                         if (Client_HasMode(Client, 'r'))
234                                 ok = IRC_WriteStrClient(Origin,
235                                                         ERR_RESTRICTED_MSG,
236                                                         Client_ID(Origin));
237                         else
238                                 x[0] = 'B';
239                         break;
240                 case 'c': /* Receive connect notices
241                            * (only settable by IRC operators!) */
242                         if (!set || Client_Type(Client) == CLIENT_SERVER
243                             || Client_OperByMe(Origin))
244                                 x[0] = 'c';
245                         else
246                                 ok = IRC_WriteStrClient(Origin,
247                                                         ERR_NOPRIVILEGES_MSG,
248                                                         Client_ID(Origin));
249                         break;
250                 case 'o': /* IRC operator (only unsettable!) */
251                         if (!set || Client_Type(Client) == CLIENT_SERVER) {
252                                 Client_SetOperByMe(Target, false);
253                                 x[0] = 'o';
254                         } else
255                                 ok = IRC_WriteStrClient(Origin,
256                                                         ERR_NOPRIVILEGES_MSG,
257                                                         Client_ID(Origin));
258                         break;
259                 case 'r': /* Restricted (only settable) */
260                         if (set || Client_Type(Client) == CLIENT_SERVER)
261                                 x[0] = 'r';
262                         else
263                                 ok = IRC_WriteStrClient(Origin,
264                                                         ERR_RESTRICTED_MSG,
265                                                         Client_ID(Origin));
266                         break;
267                 case 'R': /* Registered (not [un]settable by clients) */
268                         if (Client_Type(Client) == CLIENT_SERVER)
269                                 x[0] = 'R';
270                         else
271                                 ok = IRC_WriteStrClient(Origin,
272                                                         ERR_NICKREGISTER_MSG,
273                                                         Client_ID(Origin));
274                         break;
275                 case 'x': /* Cloak hostname */
276                         if (Client_HasMode(Client, 'r'))
277                                 ok = IRC_WriteStrClient(Origin,
278                                                         ERR_RESTRICTED_MSG,
279                                                         Client_ID(Origin));
280                         else
281                                 x[0] = 'x';
282                         break;
283                 default:
284                         if (Client_Type(Client) != CLIENT_SERVER) {
285                                 Log(LOG_DEBUG,
286                                     "Unknown mode \"%c%c\" from \"%s\"!?",
287                                     set ? '+' : '-', *mode_ptr,
288                                     Client_ID(Origin));
289                                 ok = IRC_WriteStrClient(Origin,
290                                                         ERR_UMODEUNKNOWNFLAG2_MSG,
291                                                         Client_ID(Origin),
292                                                         set ? '+' : '-',
293                                                         *mode_ptr);
294                                 x[0] = '\0';
295                         } else {
296                                 Log(LOG_DEBUG,
297                                     "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
298                                     set ? '+' : '-', *mode_ptr,
299                                     Client_ID(Origin), Client_ID(Target));
300                                 x[0] = *mode_ptr;
301                         }
302                 }
303
304                 if (!ok)
305                         break;
306
307                 /* Is there a valid mode change? */
308                 if (!x[0])
309                         continue;
310
311                 if (set) {
312                         if (Client_ModeAdd(Target, x[0]))
313                                 strlcat(the_modes, x, sizeof(the_modes));
314                 } else {
315                         if (Client_ModeDel(Target, x[0]))
316                                 strlcat(the_modes, x, sizeof(the_modes));
317                 }
318         }
319
320         /* Are there changed modes? */
321         if (the_modes[1]) {
322                 /* Remove needless action modifier characters */
323                 len = strlen(the_modes) - 1;
324                 if (the_modes[len] == '+' || the_modes[len] == '-')
325                         the_modes[len] = '\0';
326
327                 if (Client_Type(Client) == CLIENT_SERVER) {
328                         /* Forward modes to other servers */
329                         if (Client_Conn(Target) != NONE) {
330                                 /* Remote server (service?) changed modes
331                                  * for one of our clients. Inform it! */
332                                 IRC_WriteStrClientPrefix(Target, Origin,
333                                                          "MODE %s :%s",
334                                                          Client_ID(Target),
335                                                          the_modes);
336                         }
337                         IRC_WriteStrServersPrefix(Client, Origin,
338                                                   "MODE %s :%s",
339                                                   Client_ID(Target),
340                                                   the_modes);
341                 } else {
342                         /* Send reply to client and inform other servers */
343                         ok = IRC_WriteStrClientPrefix(Client, Origin,
344                                                       "MODE %s :%s",
345                                                       Client_ID(Target),
346                                                       the_modes);
347                         IRC_WriteStrServersPrefix(Client, Origin,
348                                                   "MODE %s :%s",
349                                                   Client_ID(Target),
350                                                   the_modes);
351                 }
352                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
353                          Client_TypeText(Target), Client_Mask(Target),
354                          Client_Modes(Target));
355         }
356
357         IRC_SetPenalty(Client, 1);
358         return ok;
359 } /* Client_Mode */
360
361
362 static bool
363 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
364 {
365         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
366         const char *mode_ptr;
367
368         /* Member or not? -- That's the question! */
369         if (!Channel_IsMemberOf(Channel, Origin))
370                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
371                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
372
373         /* The sender is a member: generate extended reply */
374         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
375         mode_ptr = the_modes;
376         the_args[0] = '\0';
377
378         while(*mode_ptr) {
379                 switch(*mode_ptr) {
380                 case 'l':
381                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
382                         strlcat(the_args, argadd, sizeof(the_args));
383                         break;
384                 case 'k':
385                         strlcat(the_args, " ", sizeof(the_args));
386                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
387                         break;
388                 }
389                 mode_ptr++;
390         }
391         if (the_args[0])
392                 strlcat(the_modes, the_args, sizeof(the_modes));
393
394         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
395                                 Client_ID(Origin), Channel_Name(Channel),
396                                 the_modes))
397                 return DISCONNECTED;
398 #ifndef STRICT_RFC
399         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
400                                   Client_ID(Origin), Channel_Name(Channel),
401                                   Channel_CreationTime(Channel)))
402                 return DISCONNECTED;
403 #endif
404         return CONNECTED;
405 }
406
407
408 /**
409  * Handle channel mode and channel-user mode changes
410  */
411 static bool
412 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
413 {
414         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
415             argadd[CLIENT_PASS_LEN], *mode_ptr;
416         bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
417         int mode_arg, arg_arg, mode_arg_count = 0;
418         CLIENT *client;
419         long l;
420         size_t len;
421
422         if (Channel_IsModeless(Channel))
423                 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
424                                 Client_ID(Client), Channel_Name(Channel));
425
426         /* Mode request: let's answer it :-) */
427         if (Req->argc <= 1)
428                 return Channel_Mode_Answer_Request(Origin, Channel);
429
430         Channel_CheckAdminRights(Channel, Client, Origin,
431                                  &onchannel, &modeok, &use_servermode);
432
433         if (!onchannel && !modeok)
434                 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
435                                           Client_ID(Origin),
436                                           Channel_Name(Channel));
437
438         mode_arg = 1;
439         mode_ptr = Req->argv[mode_arg];
440         if (Req->argc > mode_arg + 1)
441                 arg_arg = mode_arg + 1;
442         else
443                 arg_arg = -1;
444
445         /* Initial state: set or unset modes? */
446         skiponce = false;
447         switch (*mode_ptr) {
448         case '-':
449                 set = false;
450                 break;
451         case '+':
452                 set = true;
453                 break;
454         default:
455                 set = true;
456                 skiponce = true;
457         }
458
459         /* Prepare reply string */
460         strcpy(the_modes, set ? "+" : "-");
461         the_args[0] = '\0';
462
463         x[1] = '\0';
464         connected = CONNECTED;
465         while (mode_ptr) {
466                 if (!skiponce)
467                         mode_ptr++;
468                 if (!*mode_ptr) {
469                         /* Try next argument if there's any */
470                         if (arg_arg < 0)
471                                 break;
472                         if (arg_arg > mode_arg)
473                                 mode_arg = arg_arg;
474                         else
475                                 mode_arg++;
476
477                         if (mode_arg >= Req->argc)
478                                 break;
479                         mode_ptr = Req->argv[mode_arg];
480
481                         if (Req->argc > mode_arg + 1)
482                                 arg_arg = mode_arg + 1;
483                         else
484                                 arg_arg = -1;
485                 }
486                 skiponce = false;
487
488                 switch (*mode_ptr) {
489                 case '+':
490                 case '-':
491                         if (((*mode_ptr == '+') && !set)
492                             || ((*mode_ptr == '-') && set)) {
493                                 /* Action modifier ("+"/"-") must be changed ... */
494                                 len = strlen(the_modes) - 1;
495                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
496                                         /* Adjust last action modifier in result */
497                                         the_modes[len] = *mode_ptr;
498                                 } else {
499                                         /* Append modifier character to result string */
500                                         x[0] = *mode_ptr;
501                                         strlcat(the_modes, x, sizeof(the_modes));
502                                 }
503                                 set = *mode_ptr == '+';
504                         }
505                         continue;
506                 }
507
508                 /* Are there arguments left? */
509                 if (arg_arg >= Req->argc)
510                         arg_arg = -1;
511
512                 /* Validate modes */
513                 x[0] = '\0';
514                 argadd[0] = '\0';
515                 client = NULL;
516                 switch (*mode_ptr) {
517                 /* --- Channel modes --- */
518                 case 'i': /* Invite only */
519                 case 'M': /* Only identified nicks can write */
520                 case 'm': /* Moderated */
521                 case 'n': /* Only members can write */
522                 case 'R': /* Registered users only */
523                 case 's': /* Secret channel */
524                 case 't': /* Topic locked */
525                 case 'z': /* Secure connections only */
526                         if (modeok)
527                                 x[0] = *mode_ptr;
528                         else
529                                 connected = IRC_WriteStrClient(Origin,
530                                         ERR_CHANOPRIVSNEEDED_MSG,
531                                         Client_ID(Origin), Channel_Name(Channel));
532                         break;
533                 case 'k': /* Channel key */
534                         if (Mode_Limit_Reached(Client, mode_arg_count++))
535                                 goto chan_exit;
536                         if (!set) {
537                                 if (modeok)
538                                         x[0] = *mode_ptr;
539                                 else
540                                         connected = IRC_WriteStrClient(Origin,
541                                                 ERR_CHANOPRIVSNEEDED_MSG,
542                                                 Client_ID(Origin),
543                                                 Channel_Name(Channel));
544                                 break;
545                         }
546                         if (arg_arg > mode_arg) {
547                                 if (modeok) {
548                                         Channel_ModeDel(Channel, 'k');
549                                         Channel_SetKey(Channel,
550                                                        Req->argv[arg_arg]);
551                                         strlcpy(argadd, Channel_Key(Channel),
552                                                 sizeof(argadd));
553                                         x[0] = *mode_ptr;
554                                 } else {
555                                         connected = IRC_WriteStrClient(Origin,
556                                                 ERR_CHANOPRIVSNEEDED_MSG,
557                                                 Client_ID(Origin),
558                                                 Channel_Name(Channel));
559                                 }
560                                 Req->argv[arg_arg][0] = '\0';
561                                 arg_arg++;
562                         } else {
563                                 connected = IRC_WriteStrClient(Origin,
564                                         ERR_NEEDMOREPARAMS_MSG,
565                                         Client_ID(Origin), Req->command);
566                                 goto chan_exit;
567                         }
568                         break;
569                 case 'l': /* Member limit */
570                         if (Mode_Limit_Reached(Client, mode_arg_count++))
571                                 goto chan_exit;
572                         if (!set) {
573                                 if (modeok)
574                                         x[0] = *mode_ptr;
575                                 else
576                                         connected = IRC_WriteStrClient(Origin,
577                                                 ERR_CHANOPRIVSNEEDED_MSG,
578                                                 Client_ID(Origin),
579                                                 Channel_Name(Channel));
580                                 break;
581                         }
582                         if (arg_arg > mode_arg) {
583                                 if (modeok) {
584                                         l = atol(Req->argv[arg_arg]);
585                                         if (l > 0 && l < 0xFFFF) {
586                                                 Channel_ModeDel(Channel, 'l');
587                                                 Channel_SetMaxUsers(Channel, l);
588                                                 snprintf(argadd, sizeof(argadd),
589                                                          "%ld", l);
590                                                 x[0] = *mode_ptr;
591                                         }
592                                 } else {
593                                         connected = IRC_WriteStrClient(Origin,
594                                                 ERR_CHANOPRIVSNEEDED_MSG,
595                                                 Client_ID(Origin),
596                                                 Channel_Name(Channel));
597                                 }
598                                 Req->argv[arg_arg][0] = '\0';
599                                 arg_arg++;
600                         } else {
601                                 connected = IRC_WriteStrClient(Origin,
602                                         ERR_NEEDMOREPARAMS_MSG,
603                                         Client_ID(Origin), Req->command);
604                                 goto chan_exit;
605                         }
606                         break;
607                 case 'O': /* IRC operators only */
608                         if (modeok) {
609                                 /* Only IRC operators are allowed to
610                                  * set the 'O' channel mode! */
611                                 if (set && !(Client_OperByMe(Client)
612                                     || Client_Type(Client) == CLIENT_SERVER))
613                                         connected = IRC_WriteStrClient(Origin,
614                                                 ERR_NOPRIVILEGES_MSG,
615                                                 Client_ID(Origin));
616                                 else
617                                         x[0] = 'O';
618                         } else
619                                 connected = IRC_WriteStrClient(Origin,
620                                                 ERR_CHANOPRIVSNEEDED_MSG,
621                                                 Client_ID(Origin),
622                                                 Channel_Name(Channel));
623                                 break;
624                 case 'P': /* Persistent channel */
625                         if (modeok) {
626                                 /* Only IRC operators are allowed to
627                                  * set the 'P' channel mode! */
628                                 if (set && !(Client_OperByMe(Client)
629                                     || Client_Type(Client) == CLIENT_SERVER))
630                                         connected = IRC_WriteStrClient(Origin,
631                                                 ERR_NOPRIVILEGES_MSG,
632                                                 Client_ID(Origin));
633                                 else
634                                         x[0] = 'P';
635                         } else
636                                 connected = IRC_WriteStrClient(Origin,
637                                         ERR_CHANOPRIVSNEEDED_MSG,
638                                         Client_ID(Origin),
639                                         Channel_Name(Channel));
640                         break;
641                 /* --- Channel user modes --- */
642                 case 'a':
643                 case 'h':
644                 case 'q':
645                         if (Client_Type(Client) != CLIENT_SERVER) {
646                                 connected = IRC_WriteStrClient(Origin,
647                                         ERR_CHANOPRIVSNEEDED_MSG,
648                                         Client_ID(Origin),
649                                         Channel_Name(Channel));
650                                 goto chan_exit;
651                         }
652                 case 'o': /* Channel operator */
653                 case 'v': /* Voice */
654                         if (arg_arg > mode_arg) {
655                                 if (modeok) {
656                                         client = Client_Search(Req->argv[arg_arg]);
657                                         if (client)
658                                                 x[0] = *mode_ptr;
659                                         else
660                                                 connected = IRC_WriteStrClient(Client,
661                                                         ERR_NOSUCHNICK_MSG,
662                                                         Client_ID(Client),
663                                                         Req->argv[arg_arg]);
664                                 } else {
665                                         connected = IRC_WriteStrClient(Origin,
666                                                 ERR_CHANOPRIVSNEEDED_MSG,
667                                                 Client_ID(Origin),
668                                                 Channel_Name(Channel));
669                                 }
670                                 Req->argv[arg_arg][0] = '\0';
671                                 arg_arg++;
672                         } else {
673                                 connected = IRC_WriteStrClient(Origin,
674                                         ERR_NEEDMOREPARAMS_MSG,
675                                         Client_ID(Origin), Req->command);
676                                 goto chan_exit;
677                         }
678                         break;
679                 /* --- Channel lists --- */
680                 case 'I': /* Invite lists */
681                 case 'b': /* Ban lists */
682                 case 'e': /* Channel exception lists */
683                         if (Mode_Limit_Reached(Client, mode_arg_count++))
684                                 goto chan_exit;
685                         if (arg_arg > mode_arg) {
686                                 /* modify list */
687                                 if (modeok) {
688                                         connected = set
689                                            ? Add_To_List(*mode_ptr, Origin,
690                                                 Client, Channel,
691                                                 Req->argv[arg_arg])
692                                            : Del_From_List(*mode_ptr, Origin,
693                                                 Client, Channel,
694                                                 Req->argv[arg_arg]);
695                                 } else {
696                                         connected = IRC_WriteStrClient(Origin,
697                                                 ERR_CHANOPRIVSNEEDED_MSG,
698                                                 Client_ID(Origin),
699                                                 Channel_Name(Channel));
700                                 }
701                                 Req->argv[arg_arg][0] = '\0';
702                                 arg_arg++;
703                         } else {
704                                 switch (*mode_ptr) {
705                                 case 'I':
706                                         Channel_ShowInvites(Origin, Channel);
707                                         break;
708                                 case 'b':
709                                         Channel_ShowBans(Origin, Channel);
710                                         break;
711                                 case 'e':
712                                         Channel_ShowExcepts(Origin, Channel);
713                                         break;
714                                 }
715                         }
716                         break;
717                 default:
718                         if (Client_Type(Client) != CLIENT_SERVER) {
719                                 Log(LOG_DEBUG,
720                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
721                                     set ? '+' : '-', *mode_ptr,
722                                     Client_ID(Origin), Channel_Name(Channel));
723                                 connected = IRC_WriteStrClient(Origin,
724                                         ERR_UNKNOWNMODE_MSG,
725                                         Client_ID(Origin), *mode_ptr,
726                                         Channel_Name(Channel));
727                                 x[0] = '\0';
728                         } else {
729                                 Log(LOG_DEBUG,
730                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
731                                     set ? '+' : '-', *mode_ptr,
732                                     Client_ID(Origin), Channel_Name(Channel));
733                                 x[0] = *mode_ptr;
734                         }
735                 }
736
737                 if (!connected)
738                         break;
739
740                 /* Is there a valid mode change? */
741                 if (!x[0])
742                         continue;
743
744                 /* Validate target client */
745                 if (client && (!Channel_IsMemberOf(Channel, client))) {
746                         if (!IRC_WriteStrClient
747                             (Origin, ERR_USERNOTINCHANNEL_MSG,
748                              Client_ID(Origin), Client_ID(client),
749                              Channel_Name(Channel)))
750                                 break;
751
752                         continue;
753                 }
754
755                 if (client) {
756                         /* Channel-User-Mode */
757                         retval = set
758                                ? Channel_UserModeAdd(Channel, client, x[0])
759                                : Channel_UserModeDel(Channel, client, x[0]);
760                         if (retval) {
761                                 strlcat(the_args, " ", sizeof(the_args));
762                                 strlcat(the_args, Client_ID(client),
763                                         sizeof(the_args));
764                                 strlcat(the_modes, x, sizeof(the_modes));
765                                 LogDebug
766                                     ("User \"%s\": Mode change on %s, now \"%s\"",
767                                      Client_Mask(client), Channel_Name(Channel),
768                                      Channel_UserModes(Channel, client));
769                         }
770                 } else {
771                         /* Channel-Mode */
772                         retval = set
773                                ? Channel_ModeAdd(Channel, x[0])
774                                : Channel_ModeDel(Channel, x[0]);
775                         if (retval) {
776                                 strlcat(the_modes, x, sizeof(the_modes));
777                                 LogDebug("Channel %s: Mode change, now \"%s\".",
778                                          Channel_Name(Channel),
779                                          Channel_Modes(Channel));
780                         }
781                 }
782
783                 /* Are there additional arguments to add? */
784                 if (argadd[0]) {
785                         strlcat(the_args, " ", sizeof(the_args));
786                         strlcat(the_args, argadd, sizeof(the_args));
787                 }
788         }
789
790       chan_exit:
791         /* Are there changed modes? */
792         if (the_modes[1]) {
793                 /* Clean up mode string */
794                 len = strlen(the_modes) - 1;
795                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
796                         the_modes[len] = '\0';
797
798                 if (Client_Type(Client) == CLIENT_SERVER) {
799                         /* MODE requests for local channels from other servers
800                          * are definitely invalid! */
801                         if (Channel_IsLocal(Channel)) {
802                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
803                                 return CONNECTED;
804                         }
805
806                         /* Forward mode changes to channel users and all the
807                          * other remote servers: */
808                         IRC_WriteStrServersPrefix(Client, Origin,
809                                 "MODE %s %s%s", Channel_Name(Channel),
810                                 the_modes, the_args);
811                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
812                                 false, "MODE %s %s%s", Channel_Name(Channel),
813                                 the_modes, the_args);
814                 } else {
815                         if (use_servermode)
816                                 Origin = Client_ThisServer();
817                         /* Send reply to client and inform other servers and channel users */
818                         connected = IRC_WriteStrClientPrefix(Client, Origin,
819                                         "MODE %s %s%s", Channel_Name(Channel),
820                                         the_modes, the_args);
821                         /* Only forward requests for non-local channels */
822                         if (!Channel_IsLocal(Channel))
823                                 IRC_WriteStrServersPrefix(Client, Origin,
824                                         "MODE %s %s%s", Channel_Name(Channel),
825                                         the_modes, the_args);
826                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
827                                 false, "MODE %s %s%s", Channel_Name(Channel),
828                                 the_modes, the_args);
829                 }
830         }
831
832         IRC_SetPenalty(Client, 1);
833         return connected;
834 } /* Channel_Mode */
835
836
837 GLOBAL bool
838 IRC_AWAY( CLIENT *Client, REQUEST *Req )
839 {
840         assert( Client != NULL );
841         assert( Req != NULL );
842
843         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
844
845         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
846         {
847                 Client_SetAway( Client, Req->argv[0] );
848                 Client_ModeAdd( Client, 'a' );
849                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
850                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
851         }
852         else
853         {
854                 Client_ModeDel( Client, 'a' );
855                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
856                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
857         }
858 } /* IRC_AWAY */
859
860
861 /**
862  * Add entries to channel invite, ban and exception lists.
863  *
864  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
865  * @param Prefix The originator of the command.
866  * @param Client The sender of the command.
867  * @param Channel The channel of which the list should be modified.
868  * @param Pattern The pattern to add to the list.
869  * @return CONNECTED or DISCONNECTED.
870  */
871 static bool
872 Add_To_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
873             const char *Pattern)
874 {
875         const char *mask;
876         struct list_head *list = NULL;
877         long int current_count;
878
879         assert(Client != NULL);
880         assert(Channel != NULL);
881         assert(Pattern != NULL);
882         assert(what == 'I' || what == 'b' || what == 'e');
883
884         mask = Lists_MakeMask(Pattern);
885         current_count = Lists_Count(Channel_GetListInvites(Channel))
886                         + Lists_Count(Channel_GetListExcepts(Channel))
887                         + Lists_Count(Channel_GetListBans(Channel));
888
889         switch(what) {
890                 case 'I':
891                         list = Channel_GetListInvites(Channel);
892                         break;
893                 case 'b':
894                         list = Channel_GetListBans(Channel);
895                         break;
896                 case 'e':
897                         list = Channel_GetListExcepts(Channel);
898                         break;
899         }
900
901         if (Lists_CheckDupeMask(list, mask))
902                 return CONNECTED;
903         if (Client_Type(Client) == CLIENT_USER &&
904             current_count >= MAX_HNDL_CHANNEL_LISTS)
905                 return IRC_WriteStrClient(Client, ERR_LISTFULL_MSG,
906                                           Client_ID(Client),
907                                           Channel_Name(Channel), mask,
908                                           MAX_HNDL_CHANNEL_LISTS);
909
910         switch (what) {
911                 case 'I':
912                         if (!Channel_AddInvite(Channel, mask, false))
913                                 return CONNECTED;
914                         break;
915                 case 'b':
916                         if (!Channel_AddBan(Channel, mask))
917                                 return CONNECTED;
918                         break;
919                 case 'e':
920                         if (!Channel_AddExcept(Channel, mask))
921                                 return CONNECTED;
922                         break;
923         }
924         return Send_ListChange(true, what, Prefix, Client, Channel, mask);
925 }
926
927
928 /**
929  * Delete entries from channel invite, ban and exeption lists.
930  *
931  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
932  * @param Prefix The originator of the command.
933  * @param Client The sender of the command.
934  * @param Channel The channel of which the list should be modified.
935  * @param Pattern The pattern to add to the list.
936  * @return CONNECTED or DISCONNECTED.
937  */
938 static bool
939 Del_From_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
940               const char *Pattern)
941 {
942         const char *mask;
943         struct list_head *list = NULL;
944
945         assert(Client != NULL);
946         assert(Channel != NULL);
947         assert(Pattern != NULL);
948         assert(what == 'I' || what == 'b' || what == 'e');
949
950         mask = Lists_MakeMask(Pattern);
951
952         switch (what) {
953                 case 'I':
954                         list = Channel_GetListInvites(Channel);
955                         break;
956                 case 'b':
957                         list = Channel_GetListBans(Channel);
958                         break;
959                 case 'e':
960                         list = Channel_GetListExcepts(Channel);
961                         break;
962         }
963
964         if (!Lists_CheckDupeMask(list, mask))
965                 return CONNECTED;
966         Lists_Del(list, mask);
967
968         return Send_ListChange(false, what, Prefix, Client, Channel, mask);
969 }
970
971
972 /**
973  * Send information about changed channel invite/ban/exception lists to clients.
974  *
975  * @param IsAdd true if the list item has been added, false otherwise.
976  * @param ModeChar The mode to use (e. g. 'b' or 'I')
977  * @param Prefix The originator of the mode list change.
978  * @param Client The sender of the command.
979  * @param Channel The channel of which the list has been modified.
980  * @param Mask The mask which has been added or removed.
981  * @return CONNECTED or DISCONNECTED.
982  */
983 static bool
984 Send_ListChange(const bool IsAdd, const char ModeChar, CLIENT *Prefix,
985                 CLIENT *Client, CHANNEL *Channel, const char *Mask)
986 {
987         bool ok = true;
988
989         /* Send confirmation to the client */
990         if (Client_Type(Client) == CLIENT_USER)
991                 ok = IRC_WriteStrClientPrefix(Client, Prefix, "MODE %s %c%c %s",
992                                               Channel_Name(Channel),
993                                               IsAdd ? '+' : '-',
994                                               ModeChar, Mask);
995
996         /* to other servers */
997         IRC_WriteStrServersPrefix(Client, Prefix, "MODE %s %c%c %s",
998                                   Channel_Name(Channel), IsAdd ? '+' : '-',
999                                   ModeChar, Mask);
1000
1001         /* and local users in channel */
1002         IRC_WriteStrChannelPrefix(Client, Channel, Prefix, false,
1003                                   "MODE %s %c%c %s", Channel_Name(Channel),
1004                                   IsAdd ? '+' : '-', ModeChar, Mask );
1005
1006         return ok;
1007 } /* Send_ListChange */
1008
1009
1010 /* -eof- */