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