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