]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
79ab2ebe94beaa41b904b01b1493a83d125e5e25
[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         /* Member or not? -- That's the question! */
382         if (!Channel_IsMemberOf(Channel, Origin))
383                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
384                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
385
386         /* The sender is a member: generate extended reply */
387         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
388         mode_ptr = the_modes;
389         the_args[0] = '\0';
390
391         while(*mode_ptr) {
392                 switch(*mode_ptr) {
393                 case 'l':
394                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
395                         strlcat(the_args, argadd, sizeof(the_args));
396                         break;
397                 case 'k':
398                         strlcat(the_args, " ", sizeof(the_args));
399                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
400                         break;
401                 }
402                 mode_ptr++;
403         }
404         if (the_args[0])
405                 strlcat(the_modes, the_args, sizeof(the_modes));
406
407         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
408                                 Client_ID(Origin), Channel_Name(Channel),
409                                 the_modes))
410                 return DISCONNECTED;
411 #ifndef STRICT_RFC
412         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
413                                   Client_ID(Origin), Channel_Name(Channel),
414                                   Channel_CreationTime(Channel)))
415                 return DISCONNECTED;
416 #endif
417         return CONNECTED;
418 }
419
420 /**
421  * Handle channel mode and channel-user mode changes
422  *
423  * @param Client The client from which this command has been received.
424  * @param Req Request structure with prefix and all parameters.
425  * @param Origin The originator of the MODE command (prefix).
426  * @param Channel The target channel of this MODE command.
427  * @return CONNECTED or DISCONNECTED.
428  */
429 static bool
430 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
431 {
432         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
433             argadd[CLIENT_PASS_LEN], *mode_ptr;
434         bool connected, set, skiponce, retval, use_servermode,
435              is_halfop, is_op, is_admin, is_owner, is_machine, is_oper;
436         int mode_arg, arg_arg, mode_arg_count = 0;
437         CLIENT *client;
438         long l;
439         size_t len;
440
441         is_halfop = is_op = is_admin = is_owner = is_machine = is_oper = false;
442
443         if (Channel_IsModeless(Channel))
444                 return IRC_WriteErrClient(Client, ERR_NOCHANMODES_MSG,
445                                 Client_ID(Client), Channel_Name(Channel));
446
447         /* Mode request: let's answer it :-) */
448         if (Req->argc <= 1)
449                 return Channel_Mode_Answer_Request(Origin, Channel);
450
451         /* Check if origin is oper and opers can use mode */
452         use_servermode = Conf_OperServerMode;
453         if(Client_HasMode(Client, 'o') && Conf_OperCanMode) {
454                 is_oper = true;
455         }
456
457         /* Check if client is a server/service */
458         if(Client_Type(Client) == CLIENT_SERVER ||
459            Client_Type(Client) == CLIENT_SERVICE) {
460                 is_machine = true;
461         }
462
463         /* Check if client is member of channel or an oper or an server/service */
464         if(!Channel_IsMemberOf(Channel, Client) && !is_oper && !is_machine)
465                 return IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
466                                           Client_ID(Origin),
467                                           Channel_Name(Channel));
468
469         mode_arg = 1;
470         mode_ptr = Req->argv[mode_arg];
471         if (Req->argc > mode_arg + 1)
472                 arg_arg = mode_arg + 1;
473         else
474                 arg_arg = -1;
475
476         /* Initial state: set or unset modes? */
477         skiponce = false;
478         switch (*mode_ptr) {
479         case '-':
480                 set = false;
481                 break;
482         case '+':
483                 set = true;
484                 break;
485         default:
486                 set = true;
487                 skiponce = true;
488         }
489
490         /* Prepare reply string */
491         strcpy(the_modes, set ? "+" : "-");
492         the_args[0] = '\0';
493
494         x[1] = '\0';
495         connected = CONNECTED;
496         while (mode_ptr) {
497                 if (!skiponce)
498                         mode_ptr++;
499                 if (!*mode_ptr) {
500                         /* Try next argument if there's any */
501                         if (arg_arg < 0)
502                                 break;
503                         if (arg_arg > mode_arg)
504                                 mode_arg = arg_arg;
505                         else
506                                 mode_arg++;
507
508                         if (mode_arg >= Req->argc)
509                                 break;
510                         mode_ptr = Req->argv[mode_arg];
511
512                         if (Req->argc > mode_arg + 1)
513                                 arg_arg = mode_arg + 1;
514                         else
515                                 arg_arg = -1;
516                 }
517                 skiponce = false;
518
519                 switch (*mode_ptr) {
520                 case '+':
521                 case '-':
522                         if (((*mode_ptr == '+') && !set)
523                             || ((*mode_ptr == '-') && set)) {
524                                 /* Action modifier ("+"/"-") must be changed ... */
525                                 len = strlen(the_modes) - 1;
526                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
527                                         /* Adjust last action modifier in result */
528                                         the_modes[len] = *mode_ptr;
529                                 } else {
530                                         /* Append modifier character to result string */
531                                         x[0] = *mode_ptr;
532                                         strlcat(the_modes, x, sizeof(the_modes));
533                                 }
534                                 set = *mode_ptr == '+';
535                         }
536                         continue;
537                 }
538
539                 /* Are there arguments left? */
540                 if (arg_arg >= Req->argc)
541                         arg_arg = -1;
542
543                 if(!is_machine && !is_oper) {
544                         if (Channel_UserHasMode(Channel, Client, 'q'))
545                                 is_owner = true;
546                         if (Channel_UserHasMode(Channel, Client, 'a'))
547                                 is_admin = true;
548                         if (Channel_UserHasMode(Channel, Client, 'o'))
549                                 is_op = true;
550                         if (Channel_UserHasMode(Channel, Client, 'h'))
551                                 is_halfop = true;
552                 }
553
554                 /* Validate modes */
555                 x[0] = '\0';
556                 argadd[0] = '\0';
557                 client = NULL;
558                 switch (*mode_ptr) {
559                 /* --- Channel modes --- */
560                 case 'R': /* Registered users only */
561                 case 's': /* Secret channel */
562                 case 'z': /* Secure connections only */
563                         if(!is_oper && !is_machine && !is_owner &&
564                            !is_admin && !is_op) {
565                                 connected = IRC_WriteErrClient(Origin,
566                                         ERR_CHANOPRIVSNEEDED_MSG,
567                                         Client_ID(Origin), Channel_Name(Channel));
568                                 goto chan_exit;
569                         }
570                 case 'i': /* Invite only */
571                 case 'V': /* Invite disallow */
572                 case 'M': /* Only identified nicks can write */
573                 case 'm': /* Moderated */
574                 case 'n': /* Only members can write */
575                 case 'Q': /* No kicks */
576                 case 't': /* Topic locked */
577                         if(is_oper || is_machine || is_owner ||
578                            is_admin || is_op || is_halfop)
579                                 x[0] = *mode_ptr;
580                         else
581                                 connected = IRC_WriteErrClient(Origin,
582                                         ERR_CHANOPRIVSNEEDED_MSG,
583                                         Client_ID(Origin), Channel_Name(Channel));
584                         break;
585                 case 'k': /* Channel key */
586                         if (Mode_Limit_Reached(Client, mode_arg_count++))
587                                 goto chan_exit;
588                         if (!set) {
589                                 if (is_oper || is_machine || is_owner ||
590                                     is_admin || is_op || is_halfop) {
591                                         x[0] = *mode_ptr;
592                                         if (Channel_HasMode(Channel, 'k'))
593                                                 strlcpy(argadd, "*", sizeof(argadd));
594                                         if (arg_arg > mode_arg)
595                                                 arg_arg++;
596                                 } else
597                                         connected = IRC_WriteErrClient(Origin,
598                                                 ERR_CHANOPRIVSNEEDED_MSG,
599                                                 Client_ID(Origin),
600                                                 Channel_Name(Channel));
601                                 break;
602                         }
603                         if (arg_arg > mode_arg) {
604                                 if (is_oper || is_machine || is_owner ||
605                                     is_admin || is_op || is_halfop) {
606                                         Channel_ModeDel(Channel, 'k');
607                                         Channel_SetKey(Channel,
608                                                        Req->argv[arg_arg]);
609                                         strlcpy(argadd, Channel_Key(Channel),
610                                                 sizeof(argadd));
611                                         x[0] = *mode_ptr;
612                                 } else {
613                                         connected = IRC_WriteErrClient(Origin,
614                                                 ERR_CHANOPRIVSNEEDED_MSG,
615                                                 Client_ID(Origin),
616                                                 Channel_Name(Channel));
617                                 }
618                                 Req->argv[arg_arg][0] = '\0';
619                                 arg_arg++;
620                         } else {
621 #ifdef STRICT_RFC
622                                 /* Only send error message in "strict" mode,
623                                  * this is how ircd2.11 and others behave ... */
624                                 connected = IRC_WriteErrClient(Origin,
625                                         ERR_NEEDMOREPARAMS_MSG,
626                                         Client_ID(Origin), Req->command);
627 #endif
628                                 goto chan_exit;
629                         }
630                         break;
631                 case 'l': /* Member limit */
632                         if (Mode_Limit_Reached(Client, mode_arg_count++))
633                                 goto chan_exit;
634                         if (!set) {
635                                 if (is_oper || is_machine || is_owner ||
636                                     is_admin || is_op || is_halfop)
637                                         x[0] = *mode_ptr;
638                                 else
639                                         connected = IRC_WriteErrClient(Origin,
640                                                 ERR_CHANOPRIVSNEEDED_MSG,
641                                                 Client_ID(Origin),
642                                                 Channel_Name(Channel));
643                                 break;
644                         }
645                         if (arg_arg > mode_arg) {
646                                 if (is_oper || is_machine || is_owner ||
647                                     is_admin || is_op || is_halfop) {
648                                         l = atol(Req->argv[arg_arg]);
649                                         if (l > 0 && l < 0xFFFF) {
650                                                 Channel_ModeDel(Channel, 'l');
651                                                 Channel_SetMaxUsers(Channel, l);
652                                                 snprintf(argadd, sizeof(argadd),
653                                                          "%ld", l);
654                                                 x[0] = *mode_ptr;
655                                         }
656                                 } else {
657                                         connected = IRC_WriteErrClient(Origin,
658                                                 ERR_CHANOPRIVSNEEDED_MSG,
659                                                 Client_ID(Origin),
660                                                 Channel_Name(Channel));
661                                 }
662                                 Req->argv[arg_arg][0] = '\0';
663                                 arg_arg++;
664                         } else {
665 #ifdef STRICT_RFC
666                                 /* Only send error message in "strict" mode,
667                                  * this is how ircd2.11 and others behave ... */
668                                 connected = IRC_WriteErrClient(Origin,
669                                         ERR_NEEDMOREPARAMS_MSG,
670                                         Client_ID(Origin), Req->command);
671 #endif
672                                 goto chan_exit;
673                         }
674                         break;
675                 case 'O': /* IRC operators only */
676                         if (set) {
677                                 /* Only IRC operators are allowed to
678                                  * set the 'O' channel mode! */
679                                 if(is_oper || is_machine)
680                                         x[0] = 'O';
681                                 else
682                                         connected = IRC_WriteErrClient(Origin,
683                                                 ERR_NOPRIVILEGES_MSG,
684                                                 Client_ID(Origin));
685                         } else if(is_oper || is_machine || is_owner ||
686                                   is_admin || is_op)
687                                 x[0] = 'O';
688                         else
689                                 connected = IRC_WriteErrClient(Origin,
690                                         ERR_CHANOPRIVSNEEDED_MSG,
691                                         Client_ID(Origin),
692                                         Channel_Name(Channel));
693                         break;
694                 case 'P': /* Persistent channel */
695                         if (set) {
696                                 /* Only IRC operators are allowed to
697                                  * set the 'P' channel mode! */
698                                 if(is_oper || is_machine)
699                                         x[0] = 'P';
700                                 else
701                                         connected = IRC_WriteErrClient(Origin,
702                                                 ERR_NOPRIVILEGES_MSG,
703                                                 Client_ID(Origin));
704                         } else if(is_oper || is_machine || is_owner ||
705                                   is_admin || is_op)
706                                 x[0] = 'P';
707                         else
708                                 connected = IRC_WriteErrClient(Origin,
709                                         ERR_CHANOPRIVSNEEDED_MSG,
710                                         Client_ID(Origin),
711                                         Channel_Name(Channel));
712                         break;
713                 /* --- Channel user modes --- */
714                 case 'q': /* Owner */
715                 case 'a': /* Channel admin */
716                         if(!is_oper && !is_machine && !is_owner && !is_admin) {
717                                 connected = IRC_WriteErrClient(Origin,
718                                         ERR_CHANOPPRIVTOOLOW_MSG,
719                                         Client_ID(Origin),
720                                         Channel_Name(Channel));
721                                 goto chan_exit;
722                         }
723                 case 'o': /* Channel operator */
724                         if(!is_oper && !is_machine && !is_owner &&
725                            !is_admin && !is_op) {
726                                 connected = IRC_WriteErrClient(Origin,
727                                         ERR_CHANOPRIVSNEEDED_MSG,
728                                         Client_ID(Origin),
729                                         Channel_Name(Channel));
730                                 goto chan_exit;
731                         }
732                 case 'h': /* Half Op */
733                         if(!is_oper && !is_machine && !is_owner &&
734                            !is_admin && !is_op) {
735                                 connected = IRC_WriteErrClient(Origin,
736                                         ERR_CHANOPRIVSNEEDED_MSG,
737                                         Client_ID(Origin),
738                                         Channel_Name(Channel));
739                                 goto chan_exit;
740                         }
741                 case 'v': /* Voice */
742                         if (arg_arg > mode_arg) {
743                                 if (is_oper || is_machine || is_owner ||
744                                     is_admin || is_op || is_halfop) {
745                                         client = Client_Search(Req->argv[arg_arg]);
746                                         if (client)
747                                                 x[0] = *mode_ptr;
748                                         else
749                                                 connected = IRC_WriteErrClient(Origin,
750                                                         ERR_NOSUCHNICK_MSG,
751                                                         Client_ID(Origin),
752                                                         Req->argv[arg_arg]);
753                                 } else {
754                                         connected = IRC_WriteErrClient(Origin,
755                                                 ERR_CHANOPRIVSNEEDED_MSG,
756                                                 Client_ID(Origin),
757                                                 Channel_Name(Channel));
758                                 }
759                                 Req->argv[arg_arg][0] = '\0';
760                                 arg_arg++;
761                         } else {
762 #ifdef STRICT_RFC
763                                 /* Report an error to the client, when a user
764                                  * mode should be changed but no nickname is
765                                  * given. But don't do it when not in "strict"
766                                  * mode, because most other servers don't do
767                                  * it as well and some clients send "wired"
768                                  * MODE commands like "MODE #chan -ooo nick". */
769                                 connected = IRC_WriteErrClient(Origin,
770                                         ERR_NEEDMOREPARAMS_MSG,
771                                         Client_ID(Origin), Req->command);
772 #endif
773                                 goto chan_exit;
774                         }
775                         break;
776                 /* --- Channel lists --- */
777                 case 'I': /* Invite lists */
778                 case 'b': /* Ban lists */
779                 case 'e': /* Channel exception lists */
780                         if (Mode_Limit_Reached(Client, mode_arg_count++))
781                                 goto chan_exit;
782                         if (arg_arg > mode_arg) {
783                                 /* modify list */
784                                 if (is_oper || is_machine || is_owner ||
785                                     is_admin || is_op || is_halfop) {
786                                         connected = set
787                                            ? Add_To_List(*mode_ptr, Origin,
788                                                 Client, Channel,
789                                                 Req->argv[arg_arg])
790                                            : Del_From_List(*mode_ptr, Origin,
791                                                 Client, Channel,
792                                                 Req->argv[arg_arg]);
793                                 } else {
794                                         connected = IRC_WriteErrClient(Origin,
795                                                 ERR_CHANOPRIVSNEEDED_MSG,
796                                                 Client_ID(Origin),
797                                                 Channel_Name(Channel));
798                                 }
799                                 Req->argv[arg_arg][0] = '\0';
800                                 arg_arg++;
801                         } else {
802                                 switch (*mode_ptr) {
803                                 case 'I':
804                                         Channel_ShowInvites(Origin, Channel);
805                                         break;
806                                 case 'b':
807                                         Channel_ShowBans(Origin, Channel);
808                                         break;
809                                 case 'e':
810                                         Channel_ShowExcepts(Origin, Channel);
811                                         break;
812                                 }
813                         }
814                         break;
815                 default:
816                         if (Client_Type(Client) != CLIENT_SERVER) {
817                                 Log(LOG_DEBUG,
818                                     "Unknown mode \"%c%c\" from \"%s\" on %s!?",
819                                     set ? '+' : '-', *mode_ptr,
820                                     Client_ID(Origin), Channel_Name(Channel));
821                                 connected = IRC_WriteErrClient(Origin,
822                                         ERR_UNKNOWNMODE_MSG,
823                                         Client_ID(Origin), *mode_ptr,
824                                         Channel_Name(Channel));
825                                 x[0] = '\0';
826                         } else {
827                                 Log(LOG_DEBUG,
828                                     "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
829                                     set ? '+' : '-', *mode_ptr,
830                                     Client_ID(Origin), Channel_Name(Channel));
831                                 x[0] = *mode_ptr;
832                         }
833                 }
834
835                 if (!connected)
836                         break;
837
838                 /* Is there a valid mode change? */
839                 if (!x[0])
840                         continue;
841
842                 /* Validate target client */
843                 if (client && (!Channel_IsMemberOf(Channel, client))) {
844                         if (!IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
845                                                 Client_ID(Origin),
846                                                 Client_ID(client),
847                                                 Channel_Name(Channel)))
848                                 break;
849                         continue;
850                 }
851
852                 if (client) {
853                         /* Channel-User-Mode */
854                         retval = set
855                                ? Channel_UserModeAdd(Channel, client, x[0])
856                                : Channel_UserModeDel(Channel, client, x[0]);
857                         if (retval) {
858                                 strlcat(the_args, " ", sizeof(the_args));
859                                 strlcat(the_args, Client_ID(client),
860                                         sizeof(the_args));
861                                 strlcat(the_modes, x, sizeof(the_modes));
862                                 LogDebug
863                                     ("User \"%s\": Mode change on %s, now \"%s\"",
864                                      Client_Mask(client), Channel_Name(Channel),
865                                      Channel_UserModes(Channel, client));
866                         }
867                 } else {
868                         /* Channel-Mode */
869                         retval = set
870                                ? Channel_ModeAdd(Channel, x[0])
871                                : Channel_ModeDel(Channel, x[0]);
872                         if (retval) {
873                                 strlcat(the_modes, x, sizeof(the_modes));
874                                 LogDebug("Channel %s: Mode change, now \"%s\".",
875                                          Channel_Name(Channel),
876                                          Channel_Modes(Channel));
877                         }
878                 }
879
880                 /* Are there additional arguments to add? */
881                 if (argadd[0]) {
882                         strlcat(the_args, " ", sizeof(the_args));
883                         strlcat(the_args, argadd, sizeof(the_args));
884                 }
885         }
886
887       chan_exit:
888         /* Are there changed modes? */
889         if (the_modes[1]) {
890                 /* Clean up mode string */
891                 len = strlen(the_modes) - 1;
892                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
893                         the_modes[len] = '\0';
894
895                 if (Client_Type(Client) == CLIENT_SERVER) {
896                         /* MODE requests for local channels from other servers
897                          * are definitely invalid! */
898                         if (Channel_IsLocal(Channel)) {
899                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
900                                 return CONNECTED;
901                         }
902
903                         /* Forward mode changes to channel users and all the
904                          * other remote servers: */
905                         IRC_WriteStrServersPrefix(Client, Origin,
906                                 "MODE %s %s%s", Channel_Name(Channel),
907                                 the_modes, the_args);
908                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
909                                 false, "MODE %s %s%s", Channel_Name(Channel),
910                                 the_modes, the_args);
911                 } else {
912                         if (use_servermode)
913                                 Origin = Client_ThisServer();
914                         /* Send reply to client and inform other servers and channel users */
915                         connected = IRC_WriteStrClientPrefix(Client, Origin,
916                                         "MODE %s %s%s", Channel_Name(Channel),
917                                         the_modes, the_args);
918                         /* Only forward requests for non-local channels */
919                         if (!Channel_IsLocal(Channel))
920                                 IRC_WriteStrServersPrefix(Client, Origin,
921                                         "MODE %s %s%s", Channel_Name(Channel),
922                                         the_modes, the_args);
923                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
924                                 false, "MODE %s %s%s", Channel_Name(Channel),
925                                 the_modes, the_args);
926                 }
927         }
928
929         return connected;
930 } /* Channel_Mode */
931
932 /**
933  * Handler for the IRC "AWAY" command.
934  *
935  * @param Client The client from which this command has been received.
936  * @param Req Request structure with prefix and all parameters.
937  * @return CONNECTED or DISCONNECTED.
938  */
939 GLOBAL bool
940 IRC_AWAY( CLIENT *Client, REQUEST *Req )
941 {
942         assert (Client != NULL);
943         assert (Req != NULL);
944
945         if (Req->argc == 1 && Req->argv[0][0]) {
946                 Client_SetAway(Client, Req->argv[0]);
947                 Client_ModeAdd(Client, 'a');
948                 IRC_WriteStrServersPrefix(Client, Client, "MODE %s :+a",
949                                           Client_ID( Client));
950                 return IRC_WriteStrClient(Client, RPL_NOWAWAY_MSG,
951                                           Client_ID( Client));
952         } else {
953                 Client_ModeDel(Client, 'a');
954                 IRC_WriteStrServersPrefix(Client, Client, "MODE %s :-a",
955                                           Client_ID( Client));
956                 return IRC_WriteStrClient(Client, RPL_UNAWAY_MSG,
957                                           Client_ID( Client));
958         }
959 } /* IRC_AWAY */
960
961 /**
962  * Add entries to channel invite, ban and exception lists.
963  *
964  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
965  * @param Prefix The originator of the command.
966  * @param Client The sender of the command.
967  * @param Channel The channel of which the list should be modified.
968  * @param Pattern The pattern to add to the list.
969  * @return CONNECTED or DISCONNECTED.
970  */
971 static bool
972 Add_To_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
973             const char *Pattern)
974 {
975         char mask[MASK_LEN];
976         struct list_head *list = NULL;
977         long int current_count;
978
979         assert(Client != NULL);
980         assert(Channel != NULL);
981         assert(Pattern != NULL);
982         assert(what == 'I' || what == 'b' || what == 'e');
983
984         Lists_MakeMask(Pattern, mask, sizeof(mask));
985         current_count = Lists_Count(Channel_GetListInvites(Channel))
986                         + Lists_Count(Channel_GetListExcepts(Channel))
987                         + Lists_Count(Channel_GetListBans(Channel));
988
989         switch(what) {
990                 case 'I':
991                         list = Channel_GetListInvites(Channel);
992                         break;
993                 case 'b':
994                         list = Channel_GetListBans(Channel);
995                         break;
996                 case 'e':
997                         list = Channel_GetListExcepts(Channel);
998                         break;
999         }
1000
1001         if (Lists_CheckDupeMask(list, mask))
1002                 return CONNECTED;
1003         if (Client_Type(Client) == CLIENT_USER &&
1004             current_count >= MAX_HNDL_CHANNEL_LISTS)
1005                 return IRC_WriteErrClient(Client, ERR_LISTFULL_MSG,
1006                                           Client_ID(Client),
1007                                           Channel_Name(Channel), mask,
1008                                           MAX_HNDL_CHANNEL_LISTS);
1009
1010         switch (what) {
1011                 case 'I':
1012                         if (!Channel_AddInvite(Channel, mask, false))
1013                                 return CONNECTED;
1014                         break;
1015                 case 'b':
1016                         if (!Channel_AddBan(Channel, mask))
1017                                 return CONNECTED;
1018                         break;
1019                 case 'e':
1020                         if (!Channel_AddExcept(Channel, mask))
1021                                 return CONNECTED;
1022                         break;
1023         }
1024         return Send_ListChange(true, what, Prefix, Client, Channel, mask);
1025 }
1026
1027 /**
1028  * Delete entries from channel invite, ban and exception lists.
1029  *
1030  * @param what Can be 'I' for invite, 'b' for ban, and 'e' for exception list.
1031  * @param Prefix The originator of the command.
1032  * @param Client The sender of the command.
1033  * @param Channel The channel of which the list should be modified.
1034  * @param Pattern The pattern to add to the list.
1035  * @return CONNECTED or DISCONNECTED.
1036  */
1037 static bool
1038 Del_From_List(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
1039               const char *Pattern)
1040 {
1041         char mask[MASK_LEN];
1042         struct list_head *list = NULL;
1043
1044         assert(Client != NULL);
1045         assert(Channel != NULL);
1046         assert(Pattern != NULL);
1047         assert(what == 'I' || what == 'b' || what == 'e');
1048
1049         Lists_MakeMask(Pattern, mask, sizeof(mask));
1050
1051         switch (what) {
1052                 case 'I':
1053                         list = Channel_GetListInvites(Channel);
1054                         break;
1055                 case 'b':
1056                         list = Channel_GetListBans(Channel);
1057                         break;
1058                 case 'e':
1059                         list = Channel_GetListExcepts(Channel);
1060                         break;
1061         }
1062
1063         if (!Lists_CheckDupeMask(list, mask))
1064                 return CONNECTED;
1065         Lists_Del(list, mask);
1066
1067         return Send_ListChange(false, what, Prefix, Client, Channel, mask);
1068 }
1069
1070 /**
1071  * Send information about changed channel invite/ban/exception lists to clients.
1072  *
1073  * @param IsAdd true if the list item has been added, false otherwise.
1074  * @param ModeChar The mode to use (e. g. 'b' or 'I')
1075  * @param Prefix The originator of the mode list change.
1076  * @param Client The sender of the command.
1077  * @param Channel The channel of which the list has been modified.
1078  * @param Mask The mask which has been added or removed.
1079  * @return CONNECTED or DISCONNECTED.
1080  */
1081 static bool
1082 Send_ListChange(const bool IsAdd, const char ModeChar, CLIENT *Prefix,
1083                 CLIENT *Client, CHANNEL *Channel, const char *Mask)
1084 {
1085         bool ok = true;
1086
1087         /* Send confirmation to the client */
1088         if (Client_Type(Client) == CLIENT_USER)
1089                 ok = IRC_WriteStrClientPrefix(Client, Prefix, "MODE %s %c%c %s",
1090                                               Channel_Name(Channel),
1091                                               IsAdd ? '+' : '-',
1092                                               ModeChar, Mask);
1093
1094         /* to other servers */
1095         IRC_WriteStrServersPrefix(Client, Prefix, "MODE %s %c%c %s",
1096                                   Channel_Name(Channel), IsAdd ? '+' : '-',
1097                                   ModeChar, Mask);
1098
1099         /* and local users in channel */
1100         IRC_WriteStrChannelPrefix(Client, Channel, Prefix, false,
1101                                   "MODE %s %c%c %s", Channel_Name(Channel),
1102                                   IsAdd ? '+' : '-', ModeChar, Mask );
1103
1104         return ok;
1105 } /* Send_ListChange */
1106
1107 /* -eof- */