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