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