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