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