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