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