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