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