]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
MODE command: Always report channel creation time
[ngircd-alex.git] / src / ngircd / irc-mode.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2014 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 <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "conn.h"
25 #include "channel.h"
26 #include "irc-macros.h"
27 #include "irc-write.h"
28 #include "lists.h"
29 #include "log.h"
30 #include "parse.h"
31 #include "messages.h"
32 #include "conf.h"
33
34 #include "irc-mode.h"
35
36 static bool Client_Mode PARAMS((CLIENT *Client, REQUEST *Req, CLIENT *Origin,
37                                 CLIENT *Target));
38 static bool Channel_Mode PARAMS((CLIENT *Client, REQUEST *Req, CLIENT *Origin,
39                                  CHANNEL *Channel));
40
41 static bool Add_To_List PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
42                                 CHANNEL *Channel, const char *Pattern));
43 static bool Del_From_List PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
44                                   CHANNEL *Channel, const char *Pattern));
45
46 static bool Send_ListChange PARAMS((const bool IsAdd, const char ModeChar,
47                                     CLIENT *Prefix, CLIENT *Client,
48                                     CHANNEL *Channel, const char *Mask));
49
50 /**
51  * Handler for the IRC "MODE" command.
52  *
53  * This function detects whether user or channel modes should be modified
54  * and calls the appropriate sub-functions.
55  *
56  * @param Client The client from which this command has been received.
57  * @param Req Request structure with prefix and all parameters.
58  * @return CONNECTED or DISCONNECTED.
59  */
60 GLOBAL bool
61 IRC_MODE( CLIENT *Client, REQUEST *Req )
62 {
63         CLIENT *cl, *origin;
64         CHANNEL *chan;
65
66         assert(Client != NULL);
67         assert(Req != NULL);
68
69         _IRC_GET_SENDER_OR_RETURN_(origin, Req, Client)
70
71         /* Test for "fake" MODE commands injected by this local instance,
72          * for example when handling the "DefaultUserModes" settings.
73          * This doesn't harm real commands, because prefixes of regular
74          * clients are checked in Validate_Prefix() and can't be faked. */
75         if (Req->prefix && Client_Search(Req->prefix) == Client_ThisServer())
76                 Client = Client_Search(Req->prefix);
77
78         /* Channel or user mode? */
79         cl = NULL; chan = NULL;
80         if (Client_IsValidNick(Req->argv[0]))
81                 cl = Client_Search(Req->argv[0]);
82         if (Channel_IsValidName(Req->argv[0]))
83                 chan = Channel_Search(Req->argv[0]);
84
85         if (cl)
86                 return Client_Mode(Client, Req, origin, cl);
87         if (chan)
88                 return Channel_Mode(Client, Req, origin, chan);
89
90         /* No target found! */
91         return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
92                         Client_ID(Client), Req->argv[0]);
93 } /* IRC_MODE */
94
95 /**
96  * Check if the "mode limit" for a client has been reached.
97  *
98  * This limit doesn't apply for servers or services!
99  *
100  * @param Client The client to check.
101  * @param Count The number of modes already handled.
102  * @return true if the limit has been reached.
103  */
104 static bool
105 Mode_Limit_Reached(CLIENT *Client, int Count)
106 {
107         if (Client_Type(Client) == CLIENT_SERVER
108             || Client_Type(Client) == CLIENT_SERVICE)
109                 return false;
110         if (Count < MAX_HNDL_MODES_ARG)
111                 return false;
112         return true;
113 }
114
115 /**
116  * Handle client mode requests
117  *
118  * @param Client The client from which this command has been received.
119  * @param Req Request structure with prefix and all parameters.
120  * @param Origin The originator of the MODE command (prefix).
121  * @param Target The target (client) of this MODE command.
122  * @return CONNECTED or DISCONNECTED.
123  */
124 static bool
125 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
126 {
127         char the_modes[COMMAND_LEN], x[2], *mode_ptr;
128         bool ok, set;
129         bool send_RPL_HOSTHIDDEN_MSG = false;
130         int mode_arg;
131         size_t len;
132
133         /* Is the client allowed to request or change the modes? */
134         if (Client_Type(Client) == CLIENT_USER) {
135                 /* Users are only allowed to manipulate their own modes! */
136                 if (Target != Client)
137                         return IRC_WriteErrClient(Client,
138                                                   ERR_USERSDONTMATCH_MSG,
139                                                   Client_ID(Client));
140         }
141
142         /* Mode request: let's answer it :-) */
143         if (Req->argc == 1)
144                 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
145                                           Client_ID(Target),
146                                           Client_Modes(Target));
147
148         mode_arg = 1;
149         mode_ptr = Req->argv[mode_arg];
150
151         /* Initial state: set or unset modes? */
152         if (*mode_ptr == '+') {
153                 set = true;
154                 strcpy(the_modes, "+");
155         } else if (*mode_ptr == '-') {
156                 set = false;
157                 strcpy(the_modes, "-");
158         } else
159                 return IRC_WriteErrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
160                                           Client_ID(Origin));
161
162         x[1] = '\0';
163         ok = CONNECTED;
164         while (mode_ptr) {
165                 mode_ptr++;
166                 if (!*mode_ptr) {
167                         /* Try next argument if there's any */
168                         mode_arg++;
169                         if (mode_arg < Req->argc)
170                                 mode_ptr = Req->argv[mode_arg];
171                         else
172                                 break;
173                 }
174
175                 switch(*mode_ptr) {
176                   case '+':
177                   case '-':
178                         if ((*mode_ptr == '+' && !set)
179                             || (*mode_ptr == '-' && set)) {
180                                 /* Action modifier ("+"/"-") must be changed */
181                                 len = strlen(the_modes) - 1;
182                                 if (the_modes[len] == '+'
183                                     || the_modes[len] == '-') {
184                                         /* Last character in the "result
185                                          * string" was an "action", so just
186                                          * overwrite it with the new action */
187                                         the_modes[len] = *mode_ptr;
188                                 } else {
189                                         /* Append new modifier character to
190                                          * the resulting mode string */
191                                         x[0] = *mode_ptr;
192                                         strlcat(the_modes, x,
193                                                 sizeof(the_modes));
194                                 }
195                                 if (*mode_ptr == '+')
196                                         set = true;
197                                 else
198                                         set = false;
199                         }
200                         continue;
201                 }
202
203                 /* Validate modes */
204                 x[0] = '\0';
205                 switch (*mode_ptr) {
206                 case 'b': /* Block private msgs */
207                 case 'C': /* Only messages from clients sharing a channel */
208                 case 'i': /* Invisible */
209                 case 's': /* Server messages */
210                 case 'w': /* Wallops messages */
211                         x[0] = *mode_ptr;
212                         break;
213                 case 'a': /* Away */
214                         if (Client_Type(Client) == CLIENT_SERVER) {
215                                 x[0] = 'a';
216                                 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
217                         } else
218                                 ok = IRC_WriteErrClient(Origin,
219                                                         ERR_NOPRIVILEGES_MSG,
220                                                         Client_ID(Origin));
221                         break;
222                 case 'B': /* Bot */
223                         if (Client_HasMode(Client, 'r'))
224                                 ok = IRC_WriteErrClient(Origin,
225                                                         ERR_RESTRICTED_MSG,
226                                                         Client_ID(Origin));
227                         else
228                                 x[0] = 'B';
229                         break;
230                 case 'c': /* Receive connect notices */
231                 case 'q': /* KICK-protected user */
232                 case 'F': /* disable flood protection */
233                           /* (only settable by IRC operators!) */
234                         if (!set || Client_Type(Client) == CLIENT_SERVER
235                             || Client_HasMode(Origin, 'o'))
236                                 x[0] = *mode_ptr;
237                         else
238                                 ok = IRC_WriteErrClient(Origin,
239                                                         ERR_NOPRIVILEGES_MSG,
240                                                         Client_ID(Origin));
241                         break;
242                 case 'o': /* IRC operator (only unsettable!) */
243                         if (!set || Client_Type(Client) == CLIENT_SERVER) {
244                                 x[0] = 'o';
245                         } else
246                                 ok = IRC_WriteErrClient(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_WriteErrClient(Origin,
255                                                         ERR_RESTRICTED_MSG,
256                                                         Client_ID(Origin));
257                         break;
258                 case 'R': /* Registered (not [un]settable by clients) */
259                         if (Client_Type(Client) == CLIENT_SERVER)
260                                 x[0] = 'R';
261                         else
262                                 ok = IRC_WriteErrClient(Origin,
263                                                         ERR_NICKREGISTER_MSG,
264                                                         Client_ID(Origin));
265                         break;
266                 case 'x': /* Cloak hostname */
267                         if (Client_HasMode(Client, 'r'))
268                                 ok = IRC_WriteErrClient(Origin,
269                                                         ERR_RESTRICTED_MSG,
270                                                         Client_ID(Origin));
271                         else if (!set || Conf_CloakHostModeX[0]
272                                  || Client_Type(Client) == CLIENT_SERVER
273                                  || Client_HasMode(Origin, 'o')) {
274                                 x[0] = 'x';
275                                 send_RPL_HOSTHIDDEN_MSG = true;
276                         } else
277                                 ok = IRC_WriteErrClient(Origin,
278                                                         ERR_NOPRIVILEGES_MSG,
279                                                         Client_ID(Origin));
280                         break;
281                 default:
282                         if (Client_Type(Client) != CLIENT_SERVER) {
283                                 Log(LOG_DEBUG,
284                                     "Unknown mode \"%c%c\" from \"%s\"!?",
285                                     set ? '+' : '-', *mode_ptr,
286                                     Client_ID(Origin));
287                                 ok = IRC_WriteErrClient(Origin,
288                                                         ERR_UMODEUNKNOWNFLAG2_MSG,
289                                                         Client_ID(Origin),
290                                                         set ? '+' : '-',
291                                                         *mode_ptr);
292                                 x[0] = '\0';
293                         } else {
294                                 Log(LOG_DEBUG,
295                                     "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
296                                     set ? '+' : '-', *mode_ptr,
297                                     Client_ID(Origin), Client_ID(Target));
298                                 x[0] = *mode_ptr;
299                         }
300                 }
301
302                 if (!ok)
303                         break;
304
305                 /* Is there a valid mode change? */
306                 if (!x[0])
307                         continue;
308
309                 if (set) {
310                         if (Client_ModeAdd(Target, x[0]))
311                                 strlcat(the_modes, x, sizeof(the_modes));
312                 } else {
313                         if (Client_ModeDel(Target, x[0]))
314                                 strlcat(the_modes, x, sizeof(the_modes));
315                 }
316         }
317
318         /* Are there changed modes? */
319         if (the_modes[1]) {
320                 /* Remove needless action modifier characters */
321                 len = strlen(the_modes) - 1;
322                 if (the_modes[len] == '+' || the_modes[len] == '-')
323                         the_modes[len] = '\0';
324
325                 if (Client_Type(Client) == CLIENT_SERVER) {
326                         /* Forward modes to other servers */
327                         if (Client_Conn(Target) != NONE) {
328                                 /* Remote server (service?) changed modes
329                                  * for one of our clients. Inform it! */
330                                 IRC_WriteStrClientPrefix(Target, Origin,
331                                                          "MODE %s :%s",
332                                                          Client_ID(Target),
333                                                          the_modes);
334                         }
335                         IRC_WriteStrServersPrefix(Client, Origin,
336                                                   "MODE %s :%s",
337                                                   Client_ID(Target),
338                                                   the_modes);
339                 } else {
340                         /* Send reply to client and inform other servers */
341                         ok = IRC_WriteStrClientPrefix(Client, Origin,
342                                                       "MODE %s :%s",
343                                                       Client_ID(Target),
344                                                       the_modes);
345                         IRC_WriteStrServersPrefix(Client, Origin,
346                                                   "MODE %s :%s",
347                                                   Client_ID(Target),
348                                                   the_modes);
349                 }
350
351                 if (send_RPL_HOSTHIDDEN_MSG && Client_Conn(Target) > NONE) {
352                         /* A new (cloaked) hostname must be announced */
353                         IRC_WriteStrClientPrefix(Target, Origin,
354                                                  RPL_HOSTHIDDEN_MSG,
355                                                  Client_ID(Target),
356                                                  Client_HostnameDisplayed(Target));
357
358                 }
359
360                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
361                          Client_TypeText(Target), Client_Mask(Target),
362                          Client_Modes(Target));
363         }
364
365         return ok;
366 } /* Client_Mode */
367
368 /*
369  * Reply to a channel mode request.
370  *
371  * @param Origin The originator of the MODE command (prefix).
372  * @param Channel The channel of which the modes should be sent.
373  * @return CONNECTED or DISCONNECTED.
374  */
375 static bool
376 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
377 {
378         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
379         const char *mode_ptr;
380
381         if (!Channel_IsMemberOf(Channel, Origin)) {
382                 /* Not a member: "simple" mode reply */
383                 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
384                                         Client_ID(Origin), Channel_Name(Channel),
385                                         Channel_Modes(Channel)))
386                         return DISCONNECTED;
387         } else {
388                 /* The sender is a member: generate extended reply */
389                 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
390                 mode_ptr = the_modes;
391                 the_args[0] = '\0';
392
393                 while(*mode_ptr) {
394                         switch(*mode_ptr) {
395                         case 'l':
396                                 snprintf(argadd, sizeof(argadd), " %lu",
397                                          Channel_MaxUsers(Channel));
398                                 strlcat(the_args, argadd, sizeof(the_args));
399                                 break;
400                         case 'k':
401                                 strlcat(the_args, " ", sizeof(the_args));
402                                 strlcat(the_args, Channel_Key(Channel),
403                                         sizeof(the_args));
404                                 break;
405                         }
406                         mode_ptr++;
407                 }
408                 if (the_args[0])
409                         strlcat(the_modes, the_args, sizeof(the_modes));
410
411                 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
412                                         Client_ID(Origin), Channel_Name(Channel),
413                                         the_modes))
414                         return DISCONNECTED;
415         }
416
417 #ifndef STRICT_RFC
418         /* Channel creation time */
419         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
420                                   Client_ID(Origin), Channel_Name(Channel),
421                                   Channel_CreationTime(Channel)))
422                 return DISCONNECTED;
423 #endif
424         return CONNECTED;
425 }
426
427 /**
428  * Handle channel mode and channel-user mode changes
429  *
430  * @param Client The client from which this command has been received.
431  * @param Req Request structure with prefix and all parameters.
432  * @param Origin The originator of the MODE command (prefix).
433  * @param Channel The target channel of this MODE command.
434  * @return CONNECTED or DISCONNECTED.
435  */
436 static bool
437 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
438 {
439         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
440             argadd[CLIENT_PASS_LEN], *mode_ptr;
441         bool connected, set, skiponce, retval, use_servermode,
442              is_halfop, is_op, is_admin, is_owner, is_machine, is_oper;
443         int mode_arg, arg_arg, mode_arg_count = 0;
444         CLIENT *client;
445         long l;
446         size_t len;
447
448         is_halfop = is_op = is_admin = is_owner = is_machine = is_oper = false;
449
450         if (Channel_IsModeless(Channel))
451                 return IRC_WriteErrClient(Client, ERR_NOCHANMODES_MSG,
452                                 Client_ID(Client), Channel_Name(Channel));
453
454         /* Mode request: let's answer it :-) */
455         if (Req->argc <= 1)
456                 return Channel_Mode_Answer_Request(Origin, Channel);
457
458         /* Check if origin is oper and opers can use mode */
459         use_servermode = Conf_OperServerMode;
460         if(Client_HasMode(Client, 'o') && Conf_OperCanMode) {
461                 is_oper = true;
462         }
463
464         /* Check if client is a server/service */
465         if(Client_Type(Client) == CLIENT_SERVER ||
466            Client_Type(Client) == CLIENT_SERVICE) {
467                 is_machine = true;
468         }
469
470         /* Check if client is member of channel or an oper or an server/service */
471         if(!Channel_IsMemberOf(Channel, Client) && !is_oper && !is_machine)
472                 return IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
473                                           Client_ID(Origin),
474                                           Channel_Name(Channel));
475
476         mode_arg = 1;
477         mode_ptr = Req->argv[mode_arg];
478         if (Req->argc > mode_arg + 1)
479                 arg_arg = mode_arg + 1;
480         else
481                 arg_arg = -1;
482
483         /* Initial state: set or unset modes? */
484         skiponce = false;
485         switch (*mode_ptr) {
486         case '-':
487                 set = false;
488                 break;
489         case '+':
490                 set = true;
491                 break;
492         default:
493                 set = true;
494                 skiponce = true;
495         }
496
497         /* Prepare reply string */
498         strcpy(the_modes, set ? "+" : "-");
499         the_args[0] = '\0';
500
501         x[1] = '\0';
502         connected = CONNECTED;
503         while (mode_ptr) {
504                 if (!skiponce)
505                         mode_ptr++;
506                 if (!*mode_ptr) {
507                         /* Try next argument if there's any */
508                         if (arg_arg < 0)
509                                 break;
510                         if (arg_arg > mode_arg)
511                                 mode_arg = arg_arg;
512                         else
513                                 mode_arg++;
514
515                         if (mode_arg >= Req->argc)
516                                 break;
517                         mode_ptr = Req->argv[mode_arg];
518
519                         if (Req->argc > mode_arg + 1)
520                                 arg_arg = mode_arg + 1;
521                         else
522                                 arg_arg = -1;
523                 }
524                 skiponce = false;
525
526                 switch (*mode_ptr) {
527                 case '+':
528                 case '-':
529                         if (((*mode_ptr == '+') && !set)
530                             || ((*mode_ptr == '-') && set)) {
531                                 /* Action modifier ("+"/"-") must be changed ... */
532                                 len = strlen(the_modes) - 1;
533                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
534                                         /* Adjust last action modifier in result */
535                                         the_modes[len] = *mode_ptr;
536                                 } else {
537                                         /* Append modifier character to result string */
538                                         x[0] = *mode_ptr;
539                                         strlcat(the_modes, x, sizeof(the_modes));
540                                 }
541                                 set = *mode_ptr == '+';
542                         }
543                         continue;
544                 }
545
546                 /* Are there arguments left? */
547                 if (arg_arg >= Req->argc)
548                         arg_arg = -1;
549
550                 if(!is_machine && !is_oper) {
551                         if (Channel_UserHasMode(Channel, Client, 'q'))
552                                 is_owner = true;
553                         if (Channel_UserHasMode(Channel, Client, 'a'))
554                                 is_admin = true;
555                         if (Channel_UserHasMode(Channel, Client, 'o'))
556                                 is_op = true;
557                         if (Channel_UserHasMode(Channel, Client, 'h'))
558                                 is_halfop = true;
559                 }
560
561                 /* Validate modes */
562                 x[0] = '\0';
563                 argadd[0] = '\0';
564                 client = NULL;
565                 switch (*mode_ptr) {
566                 /* --- Channel modes --- */
567                 case 'R': /* Registered users only */
568                 case 's': /* Secret channel */
569                 case 'z': /* Secure connections only */
570                         if(!is_oper && !is_machine && !is_owner &&
571                            !is_admin && !is_op) {
572                                 connected = IRC_WriteErrClient(Origin,
573                                         ERR_CHANOPRIVSNEEDED_MSG,
574                                         Client_ID(Origin), Channel_Name(Channel));
575                                 goto chan_exit;
576                         }
577                 case 'i': /* Invite only */
578                 case 'V': /* Invite disallow */
579                 case 'M': /* Only identified nicks can write */
580                 case 'm': /* Moderated */
581                 case 'n': /* Only members can write */
582                 case 'Q': /* No kicks */
583                 case 't': /* Topic locked */
584                         if(is_oper || is_machine || is_owner ||
585                            is_admin || is_op || is_halfop)
586                                 x[0] = *mode_ptr;
587                         else
588                                 connected = IRC_WriteErrClient(Origin,
589                                         ERR_CHANOPRIVSNEEDED_MSG,
590                                         Client_ID(Origin), Channel_Name(Channel));
591                         break;
592                 case 'k': /* Channel key */
593                         if (Mode_Limit_Reached(Client, mode_arg_count++))
594                                 goto chan_exit;
595                         if (!set) {
596                                 if (is_oper || is_machine || is_owner ||
597                                     is_admin || is_op || is_halfop) {
598                                         x[0] = *mode_ptr;
599                                         if (Channel_HasMode(Channel, 'k'))
600                                                 strlcpy(argadd, "*", sizeof(argadd));
601                                         if (arg_arg > mode_arg)
602                                                 arg_arg++;
603                                 } else
604                                         connected = IRC_WriteErrClient(Origin,
605                                                 ERR_CHANOPRIVSNEEDED_MSG,
606                                                 Client_ID(Origin),
607                                                 Channel_Name(Channel));
608                                 break;
609                         }
610                         if (arg_arg > mode_arg) {
611                                 if (is_oper || is_machine || is_owner ||
612                                     is_admin || is_op || is_halfop) {
613                                         Channel_ModeDel(Channel, 'k');
614                                         Channel_SetKey(Channel,
615                                                        Req->argv[arg_arg]);
616                                         strlcpy(argadd, Channel_Key(Channel),
617                                                 sizeof(argadd));
618                                         x[0] = *mode_ptr;
619                                 } else {
620                                         connected = IRC_WriteErrClient(Origin,
621                                                 ERR_CHANOPRIVSNEEDED_MSG,
622                                                 Client_ID(Origin),
623                                                 Channel_Name(Channel));
624                                 }
625                                 Req->argv[arg_arg][0] = '\0';
626                                 arg_arg++;
627                         } else {
628 #ifdef STRICT_RFC
629                                 /* Only send error message in "strict" mode,
630                                  * this is how ircd2.11 and others behave ... */
631                                 connected = IRC_WriteErrClient(Origin,
632                                         ERR_NEEDMOREPARAMS_MSG,
633                                         Client_ID(Origin), Req->command);
634 #endif
635                                 goto chan_exit;
636                         }
637                         break;
638                 case 'l': /* Member limit */
639                         if (Mode_Limit_Reached(Client, mode_arg_count++))
640                                 goto chan_exit;
641                         if (!set) {
642                                 if (is_oper || is_machine || is_owner ||
643                                     is_admin || is_op || is_halfop)
644                                         x[0] = *mode_ptr;
645                                 else
646                                         connected = IRC_WriteErrClient(Origin,
647                                                 ERR_CHANOPRIVSNEEDED_MSG,
648                                                 Client_ID(Origin),
649                                                 Channel_Name(Channel));
650                                 break;
651                         }
652                         if (arg_arg > mode_arg) {
653                                 if (is_oper || is_machine || is_owner ||
654                                     is_admin || is_op || is_halfop) {
655                                         l = atol(Req->argv[arg_arg]);
656                                         if (l > 0 && l < 0xFFFF) {
657                                                 Channel_ModeDel(Channel, 'l');
658                                                 Channel_SetMaxUsers(Channel, l);
659                                                 snprintf(argadd, sizeof(argadd),
660                                                          "%ld", l);
661                                                 x[0] = *mode_ptr;
662                                         }
663                                 } else {
664                                         connected = IRC_WriteErrClient(Origin,
665                                                 ERR_CHANOPRIVSNEEDED_MSG,
666                                                 Client_ID(Origin),
667                                                 Channel_Name(Channel));
668                                 }
669                                 Req->argv[arg_arg][0] = '\0';
670                                 arg_arg++;
671                         } else {
672 #ifdef STRICT_RFC
673                                 /* Only send error message in "strict" mode,
674                                  * this is how ircd2.11 and others behave ... */
675                                 connected = IRC_WriteErrClient(Origin,
676                                         ERR_NEEDMOREPARAMS_MSG,
677                                         Client_ID(Origin), Req->command);
678 #endif
679                                 goto chan_exit;
680                         }
681                         break;
682                 case 'O': /* IRC operators only */
683                         if (set) {
684                                 /* Only IRC operators are allowed to
685                                  * set the 'O' channel mode! */
686                                 if(is_oper || is_machine)
687                                         x[0] = 'O';
688                                 else
689                                         connected = IRC_WriteErrClient(Origin,
690                                                 ERR_NOPRIVILEGES_MSG,
691                                                 Client_ID(Origin));
692                         } else if(is_oper || is_machine || is_owner ||
693                                   is_admin || is_op)
694                                 x[0] = 'O';
695                         else
696                                 connected = IRC_WriteErrClient(Origin,
697                                         ERR_CHANOPRIVSNEEDED_MSG,
698                                         Client_ID(Origin),
699                                         Channel_Name(Channel));
700                         break;
701                 case 'P': /* Persistent channel */
702                         if (set) {
703                                 /* Only IRC operators are allowed to
704                                  * set the 'P' channel mode! */
705                                 if(is_oper || is_machine)
706                                         x[0] = 'P';
707                                 else
708                                         connected = IRC_WriteErrClient(Origin,
709                                                 ERR_NOPRIVILEGES_MSG,
710                                                 Client_ID(Origin));
711                         } else if(is_oper || is_machine || is_owner ||
712                                   is_admin || is_op)
713                                 x[0] = 'P';
714                         else
715                                 connected = IRC_WriteErrClient(Origin,
716                                         ERR_CHANOPRIVSNEEDED_MSG,
717                                         Client_ID(Origin),
718                                         Channel_Name(Channel));
719                         break;
720                 /* --- Channel user modes --- */
721                 case 'q': /* Owner */
722                 case 'a': /* Channel admin */
723                         if(!is_oper && !is_machine && !is_owner && !is_admin) {
724                                 connected = IRC_WriteErrClient(Origin,
725                                         ERR_CHANOPPRIVTOOLOW_MSG,
726                                         Client_ID(Origin),
727                                         Channel_Name(Channel));
728                                 goto chan_exit;
729                         }
730                 case 'o': /* Channel operator */
731                         if(!is_oper && !is_machine && !is_owner &&
732                            !is_admin && !is_op) {
733                                 connected = IRC_WriteErrClient(Origin,
734                                         ERR_CHANOPRIVSNEEDED_MSG,
735                                         Client_ID(Origin),
736                                         Channel_Name(Channel));
737                                 goto chan_exit;
738                         }
739                 case 'h': /* Half Op */
740                         if(!is_oper && !is_machine && !is_owner &&
741                            !is_admin && !is_op) {
742                                 connected = IRC_WriteErrClient(Origin,
743                                         ERR_CHANOPRIVSNEEDED_MSG,
744                                         Client_ID(Origin),
745                                         Channel_Name(Channel));
746                                 goto chan_exit;
747                         }
748                 case 'v': /* Voice */
749                         if (arg_arg > mode_arg) {
750                                 if (is_oper || is_machine || is_owner ||
751                                     is_admin || is_op || is_halfop) {
752                                         client = Client_Search(Req->argv[arg_arg]);
753                                         if (client)
754                                                 x[0] = *mode_ptr;
755                                         else
756                                                 connected = IRC_WriteErrClient(Origin,
757                                                         ERR_NOSUCHNICK_MSG,
758                                                         Client_ID(Origin),
759                                                         Req->argv[arg_arg]);
760                                 } else {
761                                         connected = IRC_WriteErrClient(Origin,
762                                                 ERR_CHANOPRIVSNEEDED_MSG,
763                                                 Client_ID(Origin),
764                                                 Channel_Name(Channel));
765                                 }
766                                 Req->argv[arg_arg][0] = '\0';
767                                 arg_arg++;
768                         } else {
769 #ifdef STRICT_RFC
770                                 /* Report an error to the client, when a user
771                                  * mode should be changed but no nickname is
772                                  * given. But don't do it when not in "strict"
773                                  * mode, because most other servers don't do
774                                  * it as well and some clients send "wired"
775                                  * MODE commands like "MODE #chan -ooo nick". */
776                                 connected = IRC_WriteErrClient(Origin,
777                                         ERR_NEEDMOREPARAMS_MSG,
778                                         Client_ID(Origin), Req->command);
779 #endif
780                                 goto chan_exit;
781                         }
782                         break;
783                 /* --- Channel lists --- */
784                 case 'I': /* Invite lists */
785                 case 'b': /* Ban lists */
786                 case 'e': /* Channel exception lists */
787                         if (Mode_Limit_Reached(Client, mode_arg_count++))
788                                 goto chan_exit;
789                         if (arg_arg > mode_arg) {
790                                 /* modify list */
791                                 if (is_oper || is_machine || is_owner ||
792                                     is_admin || is_op || is_halfop) {
793                                         connected = set
794                                            ? Add_To_List(*mode_ptr, Origin,
795                                                 Client, Channel,
796                                                 Req->argv[arg_arg])
797                                            : Del_From_List(*mode_ptr, Origin,
798                                                 Client, Channel,
799                                                 Req->argv[arg_arg]);
800                                 } else {
801                                         connected = IRC_WriteErrClient(Origin,
802                                                 ERR_CHANOPRIVSNEEDED_MSG,
803                                                 Client_ID(Origin),
804                                                 Channel_Name(Channel));
805                                 }
806                                 Req->argv[arg_arg][0] = '\0';
807                                 arg_arg++;
808                         } else {
809                                 switch (*mode_ptr) {
810                                 case 'I':
811                                         Channel_ShowInvites(Origin, Channel);
812                                         break;
813                                 case 'b':
814                                         Channel_ShowBans(Origin, Channel);
815                                         break;
816                                 case 'e':
817                                         Channel_ShowExcepts(Origin, Channel);
818                                         break;
819                                 }
820                         }
821                         break;
822                 default:
823                         if (Client_Type(Client) != CLIENT_SERVER) {
824                                 Log(LOG_DEBUG,
825                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
826                                     set ? '+' : '-', *mode_ptr,
827                                     Client_ID(Origin), Channel_Name(Channel));
828                                 connected = IRC_WriteErrClient(Origin,
829                                         ERR_UNKNOWNMODE_MSG,
830                                         Client_ID(Origin), *mode_ptr,
831                                         Channel_Name(Channel));
832                                 x[0] = '\0';
833                         } else {
834                                 Log(LOG_DEBUG,
835                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
836                                     set ? '+' : '-', *mode_ptr,
837                                     Client_ID(Origin), Channel_Name(Channel));
838                                 x[0] = *mode_ptr;
839                         }
840                 }
841
842                 if (!connected)
843                         break;
844
845                 /* Is there a valid mode change? */
846                 if (!x[0])
847                         continue;
848
849                 /* Validate target client */
850                 if (client && (!Channel_IsMemberOf(Channel, client))) {
851                         if (!IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
852                                                 Client_ID(Origin),
853                                                 Client_ID(client),
854                                                 Channel_Name(Channel)))
855                                 break;
856                         continue;
857                 }
858
859                 if (client) {
860                         /* Channel-User-Mode */
861                         retval = set
862                                ? Channel_UserModeAdd(Channel, client, x[0])
863                                : Channel_UserModeDel(Channel, client, x[0]);
864                         if (retval) {
865                                 strlcat(the_args, " ", sizeof(the_args));
866                                 strlcat(the_args, Client_ID(client),
867                                         sizeof(the_args));
868                                 strlcat(the_modes, x, sizeof(the_modes));
869                                 LogDebug
870                                     ("User \"%s\": Mode change on %s, now \"%s\"",
871                                      Client_Mask(client), Channel_Name(Channel),
872                                      Channel_UserModes(Channel, client));
873                         }
874                 } else {
875                         /* Channel-Mode */
876                         retval = set
877                                ? Channel_ModeAdd(Channel, x[0])
878                                : Channel_ModeDel(Channel, x[0]);
879                         if (retval) {
880                                 strlcat(the_modes, x, sizeof(the_modes));
881                                 LogDebug("Channel %s: Mode change, now \"%s\".",
882                                          Channel_Name(Channel),
883                                          Channel_Modes(Channel));
884                         }
885                 }
886
887                 /* Are there additional arguments to add? */
888                 if (argadd[0]) {
889                         strlcat(the_args, " ", sizeof(the_args));
890                         strlcat(the_args, argadd, sizeof(the_args));
891                 }
892         }
893
894       chan_exit:
895         /* Are there changed modes? */
896         if (the_modes[1]) {
897                 /* Clean up mode string */
898                 len = strlen(the_modes) - 1;
899                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
900                         the_modes[len] = '\0';
901
902                 if (Client_Type(Client) == CLIENT_SERVER) {
903                         /* MODE requests for local channels from other servers
904                          * are definitely invalid! */
905                         if (Channel_IsLocal(Channel)) {
906                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
907                                 return CONNECTED;
908                         }
909
910                         /* Forward mode changes to channel users and all the
911                          * other remote servers: */
912                         IRC_WriteStrServersPrefix(Client, Origin,
913                                 "MODE %s %s%s", Channel_Name(Channel),
914                                 the_modes, the_args);
915                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
916                                 false, "MODE %s %s%s", Channel_Name(Channel),
917                                 the_modes, the_args);
918                 } else {
919                         if (use_servermode)
920                                 Origin = Client_ThisServer();
921                         /* Send reply to client and inform other servers and channel users */
922                         connected = IRC_WriteStrClientPrefix(Client, Origin,
923                                         "MODE %s %s%s", Channel_Name(Channel),
924                                         the_modes, the_args);
925                         /* Only forward requests for non-local channels */
926                         if (!Channel_IsLocal(Channel))
927                                 IRC_WriteStrServersPrefix(Client, Origin,
928                                         "MODE %s %s%s", Channel_Name(Channel),
929                                         the_modes, the_args);
930                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
931                                 false, "MODE %s %s%s", Channel_Name(Channel),
932                                 the_modes, the_args);
933                 }
934         }
935
936         return connected;
937 } /* Channel_Mode */
938
939 /**
940  * Handler for the IRC "AWAY" command.
941  *
942  * @param Client The client from which this command has been received.
943  * @param Req Request structure with prefix and all parameters.
944  * @return CONNECTED or DISCONNECTED.
945  */
946 GLOBAL bool
947 IRC_AWAY( CLIENT *Client, REQUEST *Req )
948 {
949         assert (Client != NULL);
950         assert (Req != NULL);
951
952         if (Req->argc == 1 && Req->argv[0][0]) {
953                 Client_SetAway(Client, Req->argv[0]);
954                 Client_ModeAdd(Client, 'a');
955                 IRC_WriteStrServersPrefix(Client, Client, "MODE %s :+a",
956                                           Client_ID( Client));
957                 return IRC_WriteStrClient(Client, RPL_NOWAWAY_MSG,
958                                           Client_ID( Client));
959         } else {
960                 Client_ModeDel(Client, 'a');
961                 IRC_WriteStrServersPrefix(Client, Client, "MODE %s :-a",
962                                           Client_ID( Client));
963                 return IRC_WriteStrClient(Client, RPL_UNAWAY_MSG,
964                                           Client_ID( Client));
965         }
966 } /* IRC_AWAY */
967
968 /**
969  * Add entries to channel invite, ban and exception lists.
970  *
971  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
972  * @param Prefix The originator of the command.
973  * @param Client The sender of the command.
974  * @param Channel The channel of which the list should be modified.
975  * @param Pattern The pattern to add to the list.
976  * @return CONNECTED or DISCONNECTED.
977  */
978 static bool
979 Add_To_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
980             const char *Pattern)
981 {
982         char mask[MASK_LEN];
983         struct list_head *list = NULL;
984         long int current_count;
985
986         assert(Client != NULL);
987         assert(Channel != NULL);
988         assert(Pattern != NULL);
989         assert(what == 'I' || what == 'b' || what == 'e');
990
991         Lists_MakeMask(Pattern, mask, sizeof(mask));
992         current_count = Lists_Count(Channel_GetListInvites(Channel))
993                         + Lists_Count(Channel_GetListExcepts(Channel))
994                         + Lists_Count(Channel_GetListBans(Channel));
995
996         switch(what) {
997                 case 'I':
998                         list = Channel_GetListInvites(Channel);
999                         break;
1000                 case 'b':
1001                         list = Channel_GetListBans(Channel);
1002                         break;
1003                 case 'e':
1004                         list = Channel_GetListExcepts(Channel);
1005                         break;
1006         }
1007
1008         if (Lists_CheckDupeMask(list, mask))
1009                 return CONNECTED;
1010         if (Client_Type(Client) == CLIENT_USER &&
1011             current_count >= MAX_HNDL_CHANNEL_LISTS)
1012                 return IRC_WriteErrClient(Client, ERR_LISTFULL_MSG,
1013                                           Client_ID(Client),
1014                                           Channel_Name(Channel), mask,
1015                                           MAX_HNDL_CHANNEL_LISTS);
1016
1017         switch (what) {
1018                 case 'I':
1019                         if (!Channel_AddInvite(Channel, mask, false))
1020                                 return CONNECTED;
1021                         break;
1022                 case 'b':
1023                         if (!Channel_AddBan(Channel, mask))
1024                                 return CONNECTED;
1025                         break;
1026                 case 'e':
1027                         if (!Channel_AddExcept(Channel, mask))
1028                                 return CONNECTED;
1029                         break;
1030         }
1031         return Send_ListChange(true, what, Prefix, Client, Channel, mask);
1032 }
1033
1034 /**
1035  * Delete entries from channel invite, ban and exception lists.
1036  *
1037  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
1038  * @param Prefix The originator of the command.
1039  * @param Client The sender of the command.
1040  * @param Channel The channel of which the list should be modified.
1041  * @param Pattern The pattern to add to the list.
1042  * @return CONNECTED or DISCONNECTED.
1043  */
1044 static bool
1045 Del_From_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
1046               const char *Pattern)
1047 {
1048         char mask[MASK_LEN];
1049         struct list_head *list = NULL;
1050
1051         assert(Client != NULL);
1052         assert(Channel != NULL);
1053         assert(Pattern != NULL);
1054         assert(what == 'I' || what == 'b' || what == 'e');
1055
1056         Lists_MakeMask(Pattern, mask, sizeof(mask));
1057
1058         switch (what) {
1059                 case 'I':
1060                         list = Channel_GetListInvites(Channel);
1061                         break;
1062                 case 'b':
1063                         list = Channel_GetListBans(Channel);
1064                         break;
1065                 case 'e':
1066                         list = Channel_GetListExcepts(Channel);
1067                         break;
1068         }
1069
1070         if (!Lists_CheckDupeMask(list, mask))
1071                 return CONNECTED;
1072         Lists_Del(list, mask);
1073
1074         return Send_ListChange(false, what, Prefix, Client, Channel, mask);
1075 }
1076
1077 /**
1078  * Send information about changed channel invite/ban/exception lists to clients.
1079  *
1080  * @param IsAdd true if the list item has been added, false otherwise.
1081  * @param ModeChar The mode to use (e. g. 'b' or 'I')
1082  * @param Prefix The originator of the mode list change.
1083  * @param Client The sender of the command.
1084  * @param Channel The channel of which the list has been modified.
1085  * @param Mask The mask which has been added or removed.
1086  * @return CONNECTED or DISCONNECTED.
1087  */
1088 static bool
1089 Send_ListChange(const bool IsAdd, const char ModeChar, CLIENT *Prefix,
1090                 CLIENT *Client, CHANNEL *Channel, const char *Mask)
1091 {
1092         bool ok = true;
1093
1094         /* Send confirmation to the client */
1095         if (Client_Type(Client) == CLIENT_USER)
1096                 ok = IRC_WriteStrClientPrefix(Client, Prefix, "MODE %s %c%c %s",
1097                                               Channel_Name(Channel),
1098                                               IsAdd ? '+' : '-',
1099                                               ModeChar, Mask);
1100
1101         /* to other servers */
1102         IRC_WriteStrServersPrefix(Client, Prefix, "MODE %s %c%c %s",
1103                                   Channel_Name(Channel), IsAdd ? '+' : '-',
1104                                   ModeChar, Mask);
1105
1106         /* and local users in channel */
1107         IRC_WriteStrChannelPrefix(Client, Channel, Prefix, false,
1108                                   "MODE %s %c%c %s", Channel_Name(Channel),
1109                                   IsAdd ? '+' : '-', ModeChar, Mask );
1110
1111         return ok;
1112 } /* Send_ListChange */
1113
1114 /* -eof- */