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