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