]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
IRC_MODE(), Client_Mode(): code cleanup [1/2]
[ngircd-alex.git] / src / ngircd / irc-mode.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 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_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
45         CHANNEL *Channel, const char *Pattern));
46 static bool Del_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
47         CHANNEL *Channel, const char *Pattern));
48
49 static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
50         CLIENT *Client, CHANNEL *Channel, const char *Mask));
51
52
53 /**
54  * Handler for the IRC "MODE" command.
55  *
56  * See RFC 2812 section 3.1.5 ("user mode message") and section 3.2.3
57  * ("channel mode message"), and RFC 2811 section 4 ("channel modes").
58  *
59  * @param Client        The client from which this command has been received.
60  * @param Req           Request structure with prefix and all parameters.
61  * @returns             CONNECTED or DISCONNECTED.
62  */
63 GLOBAL bool
64 IRC_MODE( CLIENT *Client, REQUEST *Req )
65 {
66         CLIENT *cl, *origin;
67         CHANNEL *chan;
68
69         assert(Client != NULL);
70         assert(Req != NULL);
71
72         /* No parameters? */
73         if (Req->argc < 1)
74                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
75                                           Client_ID(Client), Req->command);
76
77         /* Origin for answers */
78         if (Client_Type(Client) == CLIENT_SERVER) {
79                 origin = Client_Search(Req->prefix);
80                 if (!origin)
81                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
82                                                   Client_ID(Client),
83                                                   Req->prefix);
84         } else
85                 origin = Client;
86
87         /* Channel or user mode? */
88         cl = NULL; chan = NULL;
89         if (Client_IsValidNick(Req->argv[0]))
90                 cl = Client_Search(Req->argv[0]);
91         if (Channel_IsValidName(Req->argv[0]))
92                 chan = Channel_Search(Req->argv[0]);
93
94         if (cl)
95                 return Client_Mode(Client, Req, origin, cl);
96         if (chan)
97                 return Channel_Mode(Client, Req, origin, chan);
98
99         /* No target found! */
100         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
101                         Client_ID(Client), Req->argv[0]);
102 } /* IRC_MODE */
103
104
105 /**
106  * Handle client mode requests
107  *
108  * @param Client        The client from which this command has been received.
109  * @param Req           Request structure with prefix and all parameters.
110  * @param Origin        The originator of the MODE command (prefix).
111  * @param Target        The target (client) of this MODE command.
112  * @returns             CONNECTED or DISCONNECTED.
113  */
114 static bool
115 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
116 {
117         char the_modes[COMMAND_LEN], x[2], *mode_ptr;
118         bool ok, set;
119         int mode_arg;
120         size_t len;
121
122         /* Is the client allowed to request or change the modes? */
123         if (Client_Type(Client) == CLIENT_USER) {
124                 /* Users are only allowed to manipulate their own modes! */
125                 if (Target != Client)
126                         return IRC_WriteStrClient(Client,
127                                                   ERR_USERSDONTMATCH_MSG,
128                                                   Client_ID(Client));
129         }
130
131         /* Mode request: let's answer it :-) */
132         if (Req->argc == 1)
133                 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
134                                           Client_ID(Origin),
135                                           Client_Modes(Target));
136
137         mode_arg = 1;
138         mode_ptr = Req->argv[mode_arg];
139
140         /* Initial state: set or unset modes? */
141         if (*mode_ptr == '+') {
142                 set = true;
143                 strcpy(the_modes, "+");
144         } else if (*mode_ptr == '-') {
145                 set = false;
146                 strcpy(the_modes, "-");
147         } else
148                 return IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
149                                           Client_ID(Origin));
150
151         x[1] = '\0';
152         ok = CONNECTED;
153         while (mode_ptr) {
154                 mode_ptr++;
155                 if (!*mode_ptr) {
156                         /* Try next argument if there's any */
157                         mode_arg++;
158                         if (mode_arg < Req->argc)
159                                 mode_ptr = Req->argv[mode_arg];
160                         else
161                                 break;
162                 }
163
164                 switch( *mode_ptr )
165                 {
166                         case '+':
167                         case '-':
168                                 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
169                                 {
170                                         /* Action modifier ("+"/"-") must be changed ... */
171                                         len = strlen( the_modes ) - 1;
172                                         if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
173                                         {
174                                                 /* Adjust last action modifier in result */
175                                                 the_modes[len] = *mode_ptr;
176                                         }
177                                         else
178                                         {
179                                                 /* Append modifier character to result string */
180                                                 x[0] = *mode_ptr;
181                                                 strlcat( the_modes, x, sizeof( the_modes ));
182                                         }
183                                         if( *mode_ptr == '+' ) set = true;
184                                         else set = false;
185                                 }
186                                 continue;
187                 }
188                 
189                 /* Validate modes */
190                 x[0] = '\0';
191                 switch( *mode_ptr )
192                 {
193                         case 'i': /* Invisible */
194                         case 's': /* Server messages */
195                         case 'w': /* Wallops messages */
196                                 x[0] = *mode_ptr;
197                                 break;
198
199                         case 'a': /* Away */
200                                 if( Client_Type( Client ) == CLIENT_SERVER )
201                                 {
202                                         x[0] = 'a';
203                                         Client_SetAway( Origin, DEFAULT_AWAY_MSG );
204                                 }
205                                 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
206                                 break;
207
208                         case 'c': /* Receive connect notices
209                                    * (only settable by IRC operators!) */
210                                 if(!set || Client_OperByMe(Origin)
211                                    || Client_Type(Client) == CLIENT_SERVER)
212                                         x[0] = 'c';
213                                 else
214                                         ok = IRC_WriteStrClient(Origin,
215                                                         ERR_NOPRIVILEGES_MSG,
216                                                         Client_ID(Origin));
217                                 break;
218
219                         case 'o': /* IRC operator (only unsettable!) */
220                                 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
221                                 {
222                                         Client_SetOperByMe( Target, false );
223                                         x[0] = 'o';
224                                 }
225                                 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
226                                 break;
227
228                         case 'r': /* Restricted (only settable) */
229                                 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
230                                 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
231                                 break;
232
233                         case 'x': /* Cloak hostname */
234                                 if (Client_HasMode(Client, 'r'))
235                                         ok = IRC_WriteStrClient(Origin,
236                                                            ERR_RESTRICTED_MSG,
237                                                            Client_ID(Origin));
238                                 else
239                                         x[0] = 'x';
240                                 break;
241
242                         default:
243                                 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
244                                 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
245                                 x[0] = '\0';
246                                 goto client_exit;
247                 }
248                 if (!ok)
249                         break;
250
251                 /* Is there a valid mode change? */
252                 if (!x[0])
253                         continue;
254
255                 if (set) {
256                         if (Client_ModeAdd(Target, x[0]))
257                                 strlcat(the_modes, x, sizeof(the_modes));
258                 } else {
259                         if (Client_ModeDel(Target, x[0]))
260                                 strlcat(the_modes, x, sizeof(the_modes));
261                 }
262         }
263 client_exit:
264
265         /* Are there changed modes? */
266         if (the_modes[1]) {
267                 /* Remoce needless action modifier characters */
268                 len = strlen(the_modes) - 1;
269                 if (the_modes[len] == '+' || the_modes[len] == '-')
270                         the_modes[len] = '\0';
271
272                 if (Client_Type(Client) == CLIENT_SERVER) {
273                         /* Forward modes to other servers */
274                         IRC_WriteStrServersPrefix(Client, Origin,
275                                                   "MODE %s :%s",
276                                                   Client_ID(Target),
277                                                   the_modes);
278                 } else {
279                         /* Send reply to client and inform other servers */
280                         ok = IRC_WriteStrClientPrefix(Client, Origin,
281                                                       "MODE %s :%s",
282                                                       Client_ID(Target),
283                                                       the_modes);
284                         IRC_WriteStrServersPrefix(Client, Origin,
285                                                   "MODE %s :%s",
286                                                   Client_ID(Target),
287                                                   the_modes);
288                 }
289                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
290                          Client_TypeText(Target), Client_Mask(Target),
291                          Client_Modes(Target));
292         }
293
294         IRC_SetPenalty(Client, 1);
295         return ok;
296 } /* Client_Mode */
297
298
299 static bool
300 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
301 {
302         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
303         const char *mode_ptr;
304
305         /* Member or not? -- That's the question! */
306         if (!Channel_IsMemberOf(Channel, Origin))
307                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
308                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
309
310         /* The sender is a member: generate extended reply */
311         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
312         mode_ptr = the_modes;
313         the_args[0] = '\0';
314
315         while(*mode_ptr) {
316                 switch(*mode_ptr) {
317                 case 'l':
318                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
319                         strlcat(the_args, argadd, sizeof(the_args));
320                         break;
321                 case 'k':
322                         strlcat(the_args, " ", sizeof(the_args));
323                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
324                         break;
325                 }
326                 mode_ptr++;
327         }
328         if (the_args[0])
329                 strlcat(the_modes, the_args, sizeof(the_modes));
330
331         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
332                                 Client_ID(Origin), Channel_Name(Channel),
333                                 the_modes))
334                 return DISCONNECTED;
335 #ifndef STRICT_RFC
336         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
337                                   Client_ID(Origin), Channel_Name(Channel),
338                                   Channel_CreationTime(Channel)))
339                 return DISCONNECTED;
340 #endif
341         return CONNECTED;
342 }
343
344
345 /**
346  * Handle channel mode and channel-user mode changes
347  */
348 static bool
349 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
350 {
351         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
352             argadd[CLIENT_PASS_LEN], *mode_ptr;
353         bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
354         int mode_arg, arg_arg;
355         CLIENT *client;
356         long l;
357         size_t len;
358
359         if (Channel_IsModeless(Channel))
360                 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
361                                 Client_ID(Client), Channel_Name(Channel));
362
363         /* Mode request: let's answer it :-) */
364         if (Req->argc <= 1)
365                 return Channel_Mode_Answer_Request(Origin, Channel);
366
367         Channel_CheckAdminRights(Channel, Client, Origin,
368                                  &onchannel, &modeok, &use_servermode);
369
370         if (!onchannel && !modeok)
371                 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
372                                           Client_ID(Origin),
373                                           Channel_Name(Channel));
374
375         mode_arg = 1;
376         mode_ptr = Req->argv[mode_arg];
377         if (Req->argc > mode_arg + 1)
378                 arg_arg = mode_arg + 1;
379         else
380                 arg_arg = -1;
381
382         /* Initial state: set or unset modes? */
383         skiponce = false;
384         switch (*mode_ptr) {
385         case '-':
386                 set = false;
387                 break;
388         case '+':
389                 set = true;
390                 break;
391         default:
392                 set = true;
393                 skiponce = true;
394         }
395
396         /* Prepare reply string */
397         strcpy(the_modes, set ? "+" : "-");
398         the_args[0] = '\0';
399
400         x[1] = '\0';
401         connected = CONNECTED;
402         while (mode_ptr) {
403                 if (!skiponce)
404                         mode_ptr++;
405                 if (!*mode_ptr) {
406                         /* Try next argument if there's any */
407                         if (arg_arg > mode_arg)
408                                 mode_arg = arg_arg;
409                         else
410                                 mode_arg++;
411
412                         if (mode_arg >= Req->argc)
413                                 break;
414                         mode_ptr = Req->argv[mode_arg];
415
416                         if (Req->argc > mode_arg + 1)
417                                 arg_arg = mode_arg + 1;
418                         else
419                                 arg_arg = -1;
420                 }
421                 skiponce = false;
422
423                 switch (*mode_ptr) {
424                 case '+':
425                 case '-':
426                         if (((*mode_ptr == '+') && !set)
427                             || ((*mode_ptr == '-') && set)) {
428                                 /* Action modifier ("+"/"-") must be changed ... */
429                                 len = strlen(the_modes) - 1;
430                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
431                                         /* Adjust last action modifier in result */
432                                         the_modes[len] = *mode_ptr;
433                                 } else {
434                                         /* Append modifier character to result string */
435                                         x[0] = *mode_ptr;
436                                         strlcat(the_modes, x, sizeof(the_modes));
437                                 }
438                                 set = *mode_ptr == '+';
439                         }
440                         continue;
441                 }
442
443                 /* Are there arguments left? */
444                 if (arg_arg >= Req->argc)
445                         arg_arg = -1;
446
447                 /* Validate modes */
448                 x[0] = '\0';
449                 argadd[0] = '\0';
450                 client = NULL;
451                 switch (*mode_ptr) {
452                 /* --- Channel modes --- */
453                 case 'i': /* Invite only */
454                 case 'm': /* Moderated */
455                 case 'n': /* Only members can write */
456                 case 's': /* Secret channel */
457                 case 't': /* Topic locked */
458                 case 'z': /* Secure connections only */
459                         if (modeok)
460                                 x[0] = *mode_ptr;
461                         else
462                                 connected = IRC_WriteStrClient(Origin,
463                                         ERR_CHANOPRIVSNEEDED_MSG,
464                                         Client_ID(Origin), Channel_Name(Channel));
465                         break;
466                 case 'k': /* Channel key */
467                         if (!set) {
468                                 if (modeok)
469                                         x[0] = *mode_ptr;
470                                 else
471                                         connected = IRC_WriteStrClient(Origin,
472                                                 ERR_CHANOPRIVSNEEDED_MSG,
473                                                 Client_ID(Origin),
474                                                 Channel_Name(Channel));
475                                 break;
476                         }
477                         if (arg_arg > mode_arg) {
478                                 if (modeok) {
479                                         Channel_ModeDel(Channel, 'k');
480                                         Channel_SetKey(Channel,
481                                                        Req->argv[arg_arg]);
482                                         strlcpy(argadd, Channel_Key(Channel),
483                                                 sizeof(argadd));
484                                         x[0] = *mode_ptr;
485                                 } else {
486                                         connected = IRC_WriteStrClient(Origin,
487                                                 ERR_CHANOPRIVSNEEDED_MSG,
488                                                 Client_ID(Origin),
489                                                 Channel_Name(Channel));
490                                 }
491                                 Req->argv[arg_arg][0] = '\0';
492                                 arg_arg++;
493                         } else {
494                                 connected = IRC_WriteStrClient(Origin,
495                                         ERR_NEEDMOREPARAMS_MSG,
496                                         Client_ID(Origin), Req->command);
497                                 goto chan_exit;
498                         }
499                         break;
500                 case 'l': /* Member limit */
501                         if (!set) {
502                                 if (modeok)
503                                         x[0] = *mode_ptr;
504                                 else
505                                         connected = IRC_WriteStrClient(Origin,
506                                                 ERR_CHANOPRIVSNEEDED_MSG,
507                                                 Client_ID(Origin),
508                                                 Channel_Name(Channel));
509                                 break;
510                         }
511                         if (arg_arg > mode_arg) {
512                                 if (modeok) {
513                                         l = atol(Req->argv[arg_arg]);
514                                         if (l > 0 && l < 0xFFFF) {
515                                                 Channel_ModeDel(Channel, 'l');
516                                                 Channel_SetMaxUsers(Channel, l);
517                                                 snprintf(argadd, sizeof(argadd),
518                                                          "%ld", l);
519                                                 x[0] = *mode_ptr;
520                                         }
521                                 } else {
522                                         connected = IRC_WriteStrClient(Origin,
523                                                 ERR_CHANOPRIVSNEEDED_MSG,
524                                                 Client_ID(Origin),
525                                                 Channel_Name(Channel));
526                                 }
527                                 Req->argv[arg_arg][0] = '\0';
528                                 arg_arg++;
529                         } else {
530                                 connected = IRC_WriteStrClient(Origin,
531                                         ERR_NEEDMOREPARAMS_MSG,
532                                         Client_ID(Origin), Req->command);
533                                 goto chan_exit;
534                         }
535                         break;
536                 case 'O': /* IRC operators only */
537                         if (modeok) {
538                                 /* Only IRC operators are allowed to
539                                  * set the 'O' channel mode! */
540                                 if (set && !(Client_OperByMe(Client)
541                                     || Client_Type(Client) == CLIENT_SERVER))
542                                         connected = IRC_WriteStrClient(Origin,
543                                                 ERR_NOPRIVILEGES_MSG,
544                                                 Client_ID(Origin));
545                                 else
546                                         x[0] = 'O';
547                         } else
548                                 connected = IRC_WriteStrClient(Origin,
549                                                 ERR_CHANOPRIVSNEEDED_MSG,
550                                                 Client_ID(Origin),
551                                                 Channel_Name(Channel));
552                                 break;
553                 case 'P': /* Persistent channel */
554                         if (modeok) {
555                                 /* Only IRC operators are allowed to
556                                  * set the 'P' channel mode! */
557                                 if (set && !(Client_OperByMe(Client)
558                                     || Client_Type(Client) == CLIENT_SERVER))
559                                         connected = IRC_WriteStrClient(Origin,
560                                                 ERR_NOPRIVILEGES_MSG,
561                                                 Client_ID(Origin));
562                                 else
563                                         x[0] = 'P';
564                         } else
565                                 connected = IRC_WriteStrClient(Origin,
566                                         ERR_CHANOPRIVSNEEDED_MSG,
567                                         Client_ID(Origin),
568                                         Channel_Name(Channel));
569                         break;
570                 /* --- Channel user modes --- */
571                 case 'o': /* Channel operator */
572                 case 'v': /* Voice */
573                         if (arg_arg > mode_arg) {
574                                 if (modeok) {
575                                         client = Client_Search(Req->argv[arg_arg]);
576                                         if (client)
577                                                 x[0] = *mode_ptr;
578                                         else
579                                                 connected = IRC_WriteStrClient(Client,
580                                                         ERR_NOSUCHNICK_MSG,
581                                                         Client_ID(Client),
582                                                         Req->argv[arg_arg]);
583                                 } else {
584                                         connected = IRC_WriteStrClient(Origin,
585                                                 ERR_CHANOPRIVSNEEDED_MSG,
586                                                 Client_ID(Origin),
587                                                 Channel_Name(Channel));
588                                 }
589                                 Req->argv[arg_arg][0] = '\0';
590                                 arg_arg++;
591                         } else {
592                                 connected = IRC_WriteStrClient(Origin,
593                                         ERR_NEEDMOREPARAMS_MSG,
594                                         Client_ID(Origin), Req->command);
595                                 goto chan_exit;
596                         }
597                         break;
598                 /* --- Channel lists --- */
599                 case 'I': /* Invite lists */
600                 case 'b': /* Ban lists */
601                         if (arg_arg > mode_arg) {
602                                 /* modify list */
603                                 if (modeok) {
604                                         connected = set
605                                            ? Add_Ban_Invite(*mode_ptr, Origin,
606                                                 Client, Channel,
607                                                 Req->argv[arg_arg])
608                                            : Del_Ban_Invite(*mode_ptr, Origin,
609                                                 Client, Channel,
610                                                 Req->argv[arg_arg]);
611                                 } else {
612                                         connected = IRC_WriteStrClient(Origin,
613                                                 ERR_CHANOPRIVSNEEDED_MSG,
614                                                 Client_ID(Origin),
615                                                 Channel_Name(Channel));
616                                 }
617                                 Req->argv[arg_arg][0] = '\0';
618                                 arg_arg++;
619                         } else {
620                                 if (*mode_ptr == 'I')
621                                         Channel_ShowInvites(Origin, Channel);
622                                 else
623                                         Channel_ShowBans(Origin, Channel);
624                         }
625                         break;
626                 default:
627                         Log(LOG_DEBUG,
628                             "Unknown mode \"%c%c\" from \"%s\" on %s!?",
629                             set ? '+' : '-', *mode_ptr, Client_ID(Origin),
630                             Channel_Name(Channel));
631                         if (Client_Type(Client) != CLIENT_SERVER)
632                                 connected = IRC_WriteStrClient(Origin,
633                                         ERR_UMODEUNKNOWNFLAG2_MSG,
634                                         Client_ID(Origin),
635                                         set ? '+' : '-', *mode_ptr);
636                         x[0] = '\0';
637                         goto chan_exit;
638                 }       /* switch() */
639
640                 if (!connected)
641                         break;
642
643                 /* Is there a valid mode change? */
644                 if (!x[0])
645                         continue;
646
647                 /* Validate target client */
648                 if (client && (!Channel_IsMemberOf(Channel, client))) {
649                         if (!IRC_WriteStrClient
650                             (Origin, ERR_USERNOTINCHANNEL_MSG,
651                              Client_ID(Origin), Client_ID(client),
652                              Channel_Name(Channel)))
653                                 break;
654
655                         continue;
656                 }
657
658                 if (client) {
659                         /* Channel-User-Mode */
660                         retval = set
661                                ? Channel_UserModeAdd(Channel, client, x[0])
662                                : Channel_UserModeDel(Channel, client, x[0]);
663                         if (retval) {
664                                 strlcat(the_args, " ", sizeof(the_args));
665                                 strlcat(the_args, Client_ID(client),
666                                         sizeof(the_args));
667                                 strlcat(the_modes, x, sizeof(the_modes));
668                                 LogDebug
669                                     ("User \"%s\": Mode change on %s, now \"%s\"",
670                                      Client_Mask(client), Channel_Name(Channel),
671                                      Channel_UserModes(Channel, client));
672                         }
673                 } else {
674                         /* Channel-Mode */
675                         retval = set
676                                ? Channel_ModeAdd(Channel, x[0])
677                                : Channel_ModeDel(Channel, x[0]);
678                         if (retval) {
679                                 strlcat(the_modes, x, sizeof(the_modes));
680                                 LogDebug("Channel %s: Mode change, now \"%s\".",
681                                          Channel_Name(Channel),
682                                          Channel_Modes(Channel));
683                         }
684                 }
685
686                 /* Are there additional arguments to add? */
687                 if (argadd[0]) {
688                         strlcat(the_args, " ", sizeof(the_args));
689                         strlcat(the_args, argadd, sizeof(the_args));
690                 }
691         }
692
693       chan_exit:
694         /* Are there changed modes? */
695         if (the_modes[1]) {
696                 /* Clean up mode string */
697                 len = strlen(the_modes) - 1;
698                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
699                         the_modes[len] = '\0';
700
701                 if (Client_Type(Client) == CLIENT_SERVER) {
702                         /* MODE requests for local channels from other servers
703                          * are definitely invalid! */
704                         if (Channel_IsLocal(Channel)) {
705                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
706                                 return CONNECTED;
707                         }
708
709                         /* Forward mode changes to channel users and all the
710                          * other remote servers: */
711                         IRC_WriteStrServersPrefix(Client, Origin,
712                                 "MODE %s %s%s", Channel_Name(Channel),
713                                 the_modes, the_args);
714                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
715                                 false, "MODE %s %s%s", Channel_Name(Channel),
716                                 the_modes, the_args);
717                 } else {
718                         if (use_servermode)
719                                 Origin = Client_ThisServer();
720                         /* Send reply to client and inform other servers and channel users */
721                         connected = IRC_WriteStrClientPrefix(Client, Origin,
722                                         "MODE %s %s%s", Channel_Name(Channel),
723                                         the_modes, the_args);
724                         /* Only forward requests for non-local channels */
725                         if (!Channel_IsLocal(Channel))
726                                 IRC_WriteStrServersPrefix(Client, Origin,
727                                         "MODE %s %s%s", Channel_Name(Channel),
728                                         the_modes, the_args);
729                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
730                                 false, "MODE %s %s%s", Channel_Name(Channel),
731                                 the_modes, the_args);
732                 }
733         }
734
735         IRC_SetPenalty(Client, 1);
736         return connected;
737 } /* Channel_Mode */
738
739
740 GLOBAL bool
741 IRC_AWAY( CLIENT *Client, REQUEST *Req )
742 {
743         assert( Client != NULL );
744         assert( Req != NULL );
745
746         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
747
748         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
749         {
750                 Client_SetAway( Client, Req->argv[0] );
751                 Client_ModeAdd( Client, 'a' );
752                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
753                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
754         }
755         else
756         {
757                 Client_ModeDel( Client, 'a' );
758                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
759                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
760         }
761 } /* IRC_AWAY */
762
763
764 static bool
765 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
766 {
767         const char *mask;
768         bool already;
769         bool ret;
770
771         assert( Client != NULL );
772         assert( Channel != NULL );
773         assert( Pattern != NULL );
774         assert(what == 'I' || what == 'b');
775
776         mask = Lists_MakeMask(Pattern);
777
778         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
779         if (!already) {
780                 if (what == 'I')
781                         ret = Channel_AddInvite(Channel, mask, false);
782                 else
783                         ret = Channel_AddBan(Channel, mask);
784                 if (!ret)
785                         return CONNECTED;
786         }
787         if (already && (Client_Type(Prefix) == CLIENT_SERVER))
788                 return CONNECTED;
789
790         if (what == 'I')
791                 return Send_ListChange("+I", Prefix, Client, Channel, mask);
792         return Send_ListChange("+b", Prefix, Client, Channel, mask);
793 }
794
795
796 static bool
797 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
798 {
799         const char *mask;
800         struct list_head *list;
801
802         assert( Client != NULL );
803         assert( Channel != NULL );
804         assert( Pattern != NULL );
805         assert(what == 'I' || what == 'b');
806
807         mask = Lists_MakeMask( Pattern );
808
809         if (what == 'I')
810                 list = Channel_GetListInvites(Channel);
811         else
812                 list = Channel_GetListBans(Channel);
813
814         Lists_Del(list, mask);
815         if (what == 'I')
816                 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
817         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
818 }
819
820
821 static bool
822 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
823                 CHANNEL *Channel, const char *Mask)
824 {
825         bool ok;
826
827         if( Client_Type( Client ) == CLIENT_USER )
828         {
829                 /* send confirmation to client */
830                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
831         }
832         else ok = true;
833
834         /* to other servers */
835         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
836
837         /* and local users in channel */
838         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
839         
840         return ok;
841 } /* Send_ListChange */
842
843
844 /* -eof- */