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