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