]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
80a6003fc576ec6ceac1beb2eb1fe69dfc961cc8
[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, modeok = true, skiponce, use_servermode = false, retval;
301         int mode_arg, arg_arg;
302         CLIENT *client;
303         long l;
304         size_t len;
305
306         if (Channel_IsModeless(Channel))
307                 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
308                                 Client_ID(Client), Channel_Name(Channel));
309
310         /* Mode request: let's answer it :-) */
311         if (Req->argc <= 1)
312                 return Channel_Mode_Answer_Request(Origin, Channel);
313
314         /* Is the user allowed to change modes? */
315         if (Client_Type(Client) == CLIENT_USER) {
316                 /* Is the originating user on that channel? */
317                 if (!Channel_IsMemberOf(Channel, Origin))
318                         return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
319                                 Client_ID(Origin), Channel_Name(Channel));
320                 modeok = false;
321                 /* channel operator? */
322                 if (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
334         mode_arg = 1;
335         mode_ptr = Req->argv[mode_arg];
336         if (Req->argc > mode_arg + 1)
337                 arg_arg = mode_arg + 1;
338         else
339                 arg_arg = -1;
340
341         /* Initial state: set or unset modes? */
342         skiponce = false;
343         switch (*mode_ptr) {
344         case '-':
345                 set = false;
346                 break;
347         case '+':
348                 set = true;
349                 break;
350         default:
351                 set = true;
352                 skiponce = true;
353         }
354
355         /* Prepare reply string */
356         strcpy(the_modes, set ? "+" : "-");
357         the_args[0] = '\0';
358
359         x[1] = '\0';
360         connected = CONNECTED;
361         while (mode_ptr) {
362                 if (!skiponce)
363                         mode_ptr++;
364                 if (!*mode_ptr) {
365                         /* Try next argument if there's any */
366                         if (arg_arg > mode_arg)
367                                 mode_arg = arg_arg;
368                         else
369                                 mode_arg++;
370
371                         if (mode_arg >= Req->argc)
372                                 break;
373                         mode_ptr = Req->argv[mode_arg];
374
375                         if (Req->argc > mode_arg + 1)
376                                 arg_arg = mode_arg + 1;
377                         else
378                                 arg_arg = -1;
379                 }
380                 skiponce = false;
381
382                 switch (*mode_ptr) {
383                 case '+':
384                 case '-':
385                         if (((*mode_ptr == '+') && !set)
386                             || ((*mode_ptr == '-') && set)) {
387                                 /* Action modifier ("+"/"-") must be changed ... */
388                                 len = strlen(the_modes) - 1;
389                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
390                                         /* Adjust last action modifier in result */
391                                         the_modes[len] = *mode_ptr;
392                                 } else {
393                                         /* Append modifier character to result string */
394                                         x[0] = *mode_ptr;
395                                         strlcat(the_modes, x, sizeof(the_modes));
396                                 }
397                                 set = *mode_ptr == '+';
398                         }
399                         continue;
400                 }
401
402                 /* Are there arguments left? */
403                 if (arg_arg >= Req->argc)
404                         arg_arg = -1;
405
406                 /* Validate modes */
407                 x[0] = '\0';
408                 argadd[0] = '\0';
409                 client = NULL;
410                 switch (*mode_ptr) {
411                 /* --- Channel modes --- */
412                 case 'i': /* Invite only */
413                 case 'm': /* Moderated */
414                 case 'n': /* Only members can write */
415                 case 's': /* Secret channel */
416                 case 't': /* Topic locked */
417                 case 'z': /* Secure connections only */
418                         if (modeok)
419                                 x[0] = *mode_ptr;
420                         else
421                                 connected = IRC_WriteStrClient(Origin,
422                                         ERR_CHANOPRIVSNEEDED_MSG,
423                                         Client_ID(Origin), Channel_Name(Channel));
424                         break;
425                 case 'k': /* Channel key */
426                         if (!set) {
427                                 if (modeok)
428                                         x[0] = *mode_ptr;
429                                 else
430                                         connected = IRC_WriteStrClient(Origin,
431                                                 ERR_CHANOPRIVSNEEDED_MSG,
432                                                 Client_ID(Origin),
433                                                 Channel_Name(Channel));
434                                 break;
435                         }
436                         if (arg_arg > mode_arg) {
437                                 if (modeok) {
438                                         Channel_ModeDel(Channel, 'k');
439                                         Channel_SetKey(Channel,
440                                                        Req->argv[arg_arg]);
441                                         strlcpy(argadd, Channel_Key(Channel),
442                                                 sizeof(argadd));
443                                         x[0] = *mode_ptr;
444                                 } else {
445                                         connected = IRC_WriteStrClient(Origin,
446                                                 ERR_CHANOPRIVSNEEDED_MSG,
447                                                 Client_ID(Origin),
448                                                 Channel_Name(Channel));
449                                 }
450                                 Req->argv[arg_arg][0] = '\0';
451                                 arg_arg++;
452                         } else {
453                                 connected = IRC_WriteStrClient(Origin,
454                                         ERR_NEEDMOREPARAMS_MSG,
455                                         Client_ID(Origin), Req->command);
456                                 goto chan_exit;
457                         }
458                         break;
459                 case 'l': /* Member limit */
460                         if (!set) {
461                                 if (modeok)
462                                         x[0] = *mode_ptr;
463                                 else
464                                         connected = IRC_WriteStrClient(Origin,
465                                                 ERR_CHANOPRIVSNEEDED_MSG,
466                                                 Client_ID(Origin),
467                                                 Channel_Name(Channel));
468                                 break;
469                         }
470                         if (arg_arg > mode_arg) {
471                                 if (modeok) {
472                                         l = atol(Req->argv[arg_arg]);
473                                         if (l > 0 && l < 0xFFFF) {
474                                                 Channel_ModeDel(Channel, 'l');
475                                                 Channel_SetMaxUsers(Channel, l);
476                                                 snprintf(argadd, sizeof(argadd),
477                                                          "%ld", l);
478                                                 x[0] = *mode_ptr;
479                                         }
480                                 } else {
481                                         connected = IRC_WriteStrClient(Origin,
482                                                 ERR_CHANOPRIVSNEEDED_MSG,
483                                                 Client_ID(Origin),
484                                                 Channel_Name(Channel));
485                                 }
486                                 Req->argv[arg_arg][0] = '\0';
487                                 arg_arg++;
488                         } else {
489                                 connected = IRC_WriteStrClient(Origin,
490                                         ERR_NEEDMOREPARAMS_MSG,
491                                         Client_ID(Origin), Req->command);
492                                 goto chan_exit;
493                         }
494                         break;
495                 case 'P': /* Persistent channel */
496                         if (modeok) {
497                                 /* Only IRC operators are allowed to
498                                  * set the 'P' channel mode! */
499                                 if (set && !(Client_OperByMe(Client)
500                                     || Client_Type(Client) == CLIENT_SERVER))
501                                         connected = IRC_WriteStrClient(Origin,
502                                                 ERR_NOPRIVILEGES_MSG,
503                                                 Client_ID(Origin));
504                                 else
505                                         x[0] = 'P';
506                         } else
507                                 connected = IRC_WriteStrClient(Origin,
508                                         ERR_CHANOPRIVSNEEDED_MSG,
509                                         Client_ID(Origin),
510                                         Channel_Name(Channel));
511                         break;
512                 /* --- Channel user modes --- */
513                 case 'o': /* Channel operator */
514                 case 'v': /* Voice */
515                         if (arg_arg > mode_arg) {
516                                 if (modeok) {
517                                         client = Client_Search(Req->argv[arg_arg]);
518                                         if (client)
519                                                 x[0] = *mode_ptr;
520                                         else
521                                                 connected = IRC_WriteStrClient(Client,
522                                                         ERR_NOSUCHNICK_MSG,
523                                                         Client_ID(Client),
524                                                         Req->argv[arg_arg]);
525                                 } else {
526                                         connected = IRC_WriteStrClient(Origin,
527                                                 ERR_CHANOPRIVSNEEDED_MSG,
528                                                 Client_ID(Origin),
529                                                 Channel_Name(Channel));
530                                 }
531                                 Req->argv[arg_arg][0] = '\0';
532                                 arg_arg++;
533                         } else {
534                                 connected = IRC_WriteStrClient(Origin,
535                                         ERR_NEEDMOREPARAMS_MSG,
536                                         Client_ID(Origin), Req->command);
537                                 goto chan_exit;
538                         }
539                         break;
540                 /* --- Channel lists --- */
541                 case 'I': /* Invite lists */
542                 case 'b': /* Ban lists */
543                         if (arg_arg > mode_arg) {
544                                 /* modify list */
545                                 if (modeok) {
546                                         connected = set
547                                            ? Add_Ban_Invite(*mode_ptr, Origin,
548                                                 Client, Channel,
549                                                 Req->argv[arg_arg])
550                                            : Del_Ban_Invite(*mode_ptr, Origin,
551                                                 Client, Channel,
552                                                 Req->argv[arg_arg]);
553                                 } else {
554                                         connected = IRC_WriteStrClient(Origin,
555                                                 ERR_CHANOPRIVSNEEDED_MSG,
556                                                 Client_ID(Origin),
557                                                 Channel_Name(Channel));
558                                 }
559                                 Req->argv[arg_arg][0] = '\0';
560                                 arg_arg++;
561                         } else {
562                                 if (*mode_ptr == 'I')
563                                         Channel_ShowInvites(Origin, Channel);
564                                 else
565                                         Channel_ShowBans(Origin, Channel);
566                         }
567                         break;
568                 default:
569                         Log(LOG_DEBUG,
570                             "Unknown mode \"%c%c\" from \"%s\" on %s!?",
571                             set ? '+' : '-', *mode_ptr, Client_ID(Origin),
572                             Channel_Name(Channel));
573                         if (Client_Type(Client) != CLIENT_SERVER)
574                                 connected = IRC_WriteStrClient(Origin,
575                                         ERR_UMODEUNKNOWNFLAG2_MSG,
576                                         Client_ID(Origin),
577                                         set ? '+' : '-', *mode_ptr);
578                         x[0] = '\0';
579                         goto chan_exit;
580                 }       /* switch() */
581
582                 if (!connected)
583                         break;
584
585                 /* Is there a valid mode change? */
586                 if (!x[0])
587                         continue;
588
589                 /* Validate target client */
590                 if (client && (!Channel_IsMemberOf(Channel, client))) {
591                         if (!IRC_WriteStrClient
592                             (Origin, ERR_USERNOTINCHANNEL_MSG,
593                              Client_ID(Origin), Client_ID(client),
594                              Channel_Name(Channel)))
595                                 break;
596
597                         continue;
598                 }
599
600                 if (client) {
601                         /* Channel-User-Mode */
602                         retval = set
603                                ? Channel_UserModeAdd(Channel, client, x[0])
604                                : Channel_UserModeDel(Channel, client, x[0]);
605                         if (retval) {
606                                 strlcat(the_args, " ", sizeof(the_args));
607                                 strlcat(the_args, Client_ID(client),
608                                         sizeof(the_args));
609                                 strlcat(the_modes, x, sizeof(the_modes));
610                                 LogDebug
611                                     ("User \"%s\": Mode change on %s, now \"%s\"",
612                                      Client_Mask(client), Channel_Name(Channel),
613                                      Channel_UserModes(Channel, client));
614                         }
615                 } else {
616                         /* Channel-Mode */
617                         retval = set
618                                ? Channel_ModeAdd(Channel, x[0])
619                                : Channel_ModeDel(Channel, x[0]);
620                         if (retval) {
621                                 strlcat(the_modes, x, sizeof(the_modes));
622                                 LogDebug("Channel %s: Mode change, now \"%s\".",
623                                          Channel_Name(Channel),
624                                          Channel_Modes(Channel));
625                         }
626                 }
627
628                 /* Are there additional arguments to add? */
629                 if (argadd[0]) {
630                         strlcat(the_args, " ", sizeof(the_args));
631                         strlcat(the_args, argadd, sizeof(the_args));
632                 }
633         }
634
635       chan_exit:
636         /* Are there changed modes? */
637         if (the_modes[1]) {
638                 /* Clean up mode string */
639                 len = strlen(the_modes) - 1;
640                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
641                         the_modes[len] = '\0';
642
643                 if (Client_Type(Client) == CLIENT_SERVER) {
644                         /* MODE requests for local channels from other servers
645                          * are definitely invalid! */
646                         if (Channel_IsLocal(Channel)) {
647                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
648                                 return CONNECTED;
649                         }
650
651                         /* Forward mode changes to channel users and all the
652                          * other remote servers: */
653                         IRC_WriteStrServersPrefix(Client, Origin,
654                                 "MODE %s %s%s", Channel_Name(Channel),
655                                 the_modes, the_args);
656                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
657                                 false, "MODE %s %s%s", Channel_Name(Channel),
658                                 the_modes, the_args);
659                 } else {
660                         if (use_servermode)
661                                 Origin = Client_ThisServer();
662                         /* Send reply to client and inform other servers and channel users */
663                         connected = IRC_WriteStrClientPrefix(Client, Origin,
664                                         "MODE %s %s%s", Channel_Name(Channel),
665                                         the_modes, the_args);
666                         /* Only forward requests for non-local channels */
667                         if (!Channel_IsLocal(Channel))
668                                 IRC_WriteStrServersPrefix(Client, Origin,
669                                         "MODE %s %s%s", Channel_Name(Channel),
670                                         the_modes, the_args);
671                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
672                                 false, "MODE %s %s%s", Channel_Name(Channel),
673                                 the_modes, the_args);
674                 }
675         }
676
677         IRC_SetPenalty(Client, 1);
678         return connected;
679 } /* Channel_Mode */
680
681
682 GLOBAL bool
683 IRC_AWAY( CLIENT *Client, REQUEST *Req )
684 {
685         assert( Client != NULL );
686         assert( Req != NULL );
687
688         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
689
690         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
691         {
692                 Client_SetAway( Client, Req->argv[0] );
693                 Client_ModeAdd( Client, 'a' );
694                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
695                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
696         }
697         else
698         {
699                 Client_ModeDel( Client, 'a' );
700                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
701                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
702         }
703 } /* IRC_AWAY */
704
705
706 static bool
707 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
708 {
709         const char *mask;
710         bool already;
711         bool ret;
712
713         assert( Client != NULL );
714         assert( Channel != NULL );
715         assert( Pattern != NULL );
716         assert(what == 'I' || what == 'b');
717
718         mask = Lists_MakeMask(Pattern);
719
720         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
721         if (!already) {
722                 if (what == 'I')
723                         ret = Channel_AddInvite(Channel, mask, false);
724                 else
725                         ret = Channel_AddBan(Channel, mask);
726                 if (!ret)
727                         return CONNECTED;
728         }
729         if (already && (Client_Type(Prefix) == CLIENT_SERVER))
730                 return CONNECTED;
731
732         if (what == 'I')
733                 return Send_ListChange("+I", Prefix, Client, Channel, mask);
734         return Send_ListChange("+b", Prefix, Client, Channel, mask);
735 }
736
737
738 static bool
739 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
740 {
741         const char *mask;
742         struct list_head *list;
743
744         assert( Client != NULL );
745         assert( Channel != NULL );
746         assert( Pattern != NULL );
747         assert(what == 'I' || what == 'b');
748
749         mask = Lists_MakeMask( Pattern );
750
751         if (what == 'I')
752                 list = Channel_GetListInvites(Channel);
753         else
754                 list = Channel_GetListBans(Channel);
755
756         Lists_Del(list, mask);
757         if (what == 'I')
758                 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
759         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
760 }
761
762
763 static bool
764 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
765                 CHANNEL *Channel, const char *Mask)
766 {
767         bool ok;
768
769         if( Client_Type( Client ) == CLIENT_USER )
770         {
771                 /* send confirmation to client */
772                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
773         }
774         else ok = true;
775
776         /* to other servers */
777         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
778
779         /* and local users in channel */
780         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
781         
782         return ok;
783 } /* Send_ListChange */
784
785
786 /* -eof- */