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