40dfb7329994d9b6428648f6bf65e82b22f2cff3
[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.29 2003/01/08 23:00:12 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                 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = TRUE;
283                 else modeok = FALSE;
284                 if( Conf_OperCanMode )
285                 {
286                         /* auch IRC-Operatoren duerfen MODE verwenden */
287                         if( Client_OperByMe( Origin )) modeok = TRUE;
288                 }
289         }
290         else modeok = TRUE;
291
292         mode_arg = 1;
293         mode_ptr = Req->argv[mode_arg];
294         if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
295         else arg_arg = -1;
296
297         /* Initial state: set or unset modes? */
298         skiponce = FALSE;
299         if( *mode_ptr == '-' ) set = FALSE;
300         else if( *mode_ptr == '+' ) set = TRUE;
301         else set = skiponce = TRUE;
302
303         /* Prepare reply string */
304         if( set ) strcpy( the_modes, "+" );
305         else strcpy( the_modes, "-" );
306         strcpy( the_args, " " );
307
308         x[1] = '\0';
309         ok = CONNECTED;
310         while( mode_ptr )
311         {
312                 if( ! skiponce ) mode_ptr++;
313                 if( ! *mode_ptr )
314                 {
315                         /* Try next argument if there's any */
316                         if( arg_arg > mode_arg ) mode_arg = arg_arg;
317                         else mode_arg++;
318                         if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
319                         else break;
320                         if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
321                         else arg_arg = -1;
322                 }
323                 skiponce = FALSE;
324
325                 switch( *mode_ptr )
326                 {
327                         case '+':
328                         case '-':
329                                 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
330                                 {
331                                         /* Action modifier ("+"/"-") must be changed ... */
332                                         if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' ))
333                                         {
334                                                 /* Adjust last action modifier in result */
335                                                 the_modes[strlen( the_modes ) - 1] = *mode_ptr;
336                                         }
337                                         else
338                                         {
339                                                 /* Append modifier character to result string */
340                                                 x[0] = *mode_ptr;
341                                                 strlcat( the_modes, x, sizeof( the_modes ));
342                                         }
343                                         if( *mode_ptr == '+' ) set = TRUE;
344                                         else set = FALSE;
345                                 }
346                                 continue;
347                 }
348
349                 /* Are there arguments left? */
350                 if( arg_arg >= Req->argc ) arg_arg = -1;
351
352                 /* Validate modes */
353                 x[0] = '\0';
354                 argadd[0] = '\0';
355                 client = NULL;
356                 switch( *mode_ptr )
357                 {
358                         /* Channel modes */
359                         case 'i':
360                                 /* Invite-Only */
361                                 if( modeok ) x[0] = 'i';
362                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
363                                 break;
364                         case 'm':
365                                 /* Moderated */
366                                 if( modeok ) x[0] = 'm';
367                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
368                                 break;
369                         case 'n':
370                                 /* kein Schreiben in den Channel von aussen */
371                                 if( modeok ) x[0] = 'n';
372                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
373                                 break;
374                         case 't':
375                                 /* Topic Lock */
376                                 if( modeok ) x[0] = 't';
377                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
378                                 break;
379                         case 'P':
380                                 /* Persistent channel */
381                                 if( modeok )
382                                 {
383                                         if( set && ( ! Client_OperByMe( Client )))
384                                         {
385                                                 /* Only IRC operators are allowed to set P mode */
386                                                 ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
387                                         }
388                                         else x[0] = 'P';
389                                 }
390                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
391                                 break;
392
393                         /* Channel user modes */
394                         case 'o':
395                                 /* Channel operator */
396                         case 'v':
397                                 /* Voice */
398                                 if( arg_arg > mode_arg )
399                                 {
400                                         if( modeok )
401                                         {
402                                                 client = Client_Search( Req->argv[arg_arg] );
403                                                 if( client ) x[0] = *mode_ptr;
404                                                 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
405                                         }
406                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
407                                         Req->argv[arg_arg][0] = '\0';
408                                         arg_arg++;
409                                 }
410                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
411                                 break;
412                         case 'k':
413                                 /* Channel key */
414                                 if( ! set )
415                                 {
416                                         if( modeok ) x[0] = *mode_ptr;
417                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
418                                         break;
419                                 }
420                                 if( arg_arg > mode_arg )
421                                 {
422                                         if( modeok )
423                                         {
424                                                 Channel_ModeDel( Channel, 'k' );
425                                                 Channel_SetKey( Channel, Req->argv[arg_arg] );
426                                                 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
427                                                 x[0] = *mode_ptr;
428                                         }
429                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
430                                         Req->argv[arg_arg][0] = '\0';
431                                         arg_arg++;
432                                 }
433                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
434                                 break;
435                         case 'l':
436                                 /* Member limit */
437                                 if( ! set )
438                                 {
439                                         if( modeok ) x[0] = *mode_ptr;
440                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
441                                         break;
442                                 }
443                                 if( arg_arg > mode_arg )
444                                 {
445                                         if( modeok )
446                                         {
447                                                 l = atol( Req->argv[arg_arg] );
448                                                 if( l > 0 && l < 0xFFFF )
449                                                 {
450                                                         Channel_ModeDel( Channel, 'l' );
451                                                         Channel_SetMaxUsers( Channel, l );
452                                                         snprintf( argadd, sizeof( argadd ), "%ld", l );
453                                                         x[0] = *mode_ptr;
454                                                 }
455                                         }
456                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
457                                         Req->argv[arg_arg][0] = '\0';
458                                         arg_arg++;
459                                 }
460                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
461                                 break;
462
463                         /* Channel lists */
464                         case 'I':
465                                 /* Invite lists */
466                                 if( arg_arg > mode_arg )
467                                 {
468                                         /* modify list */
469                                         if( modeok )
470                                         {
471                                                 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
472                                                 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
473                                         }
474                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
475                                         Req->argv[arg_arg][0] = '\0';
476                                         arg_arg++;
477                                 }
478                                 else Lists_ShowInvites( Origin, Channel );
479                                 break;
480                         case 'b':
481                                 /* Ban lists */
482                                 if( arg_arg > mode_arg )
483                                 {
484                                         /* modify list */
485                                         if( modeok )
486                                         {
487                                                 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
488                                                 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
489                                         }
490                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
491                                         Req->argv[arg_arg][0] = '\0';
492                                         arg_arg++;
493                                 }
494                                 else Lists_ShowBans( Origin, Channel );
495                                 break;
496
497                         default:
498                                 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
499                                 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
500                                 x[0] = '\0';
501                                 goto chan_exit;
502                 }
503                 if( ! ok ) break;
504
505                 /* Is there a valid mode change? */
506                 if( ! x[0] ) continue;
507
508                 if( set )
509                 {
510                         /* Set mode */
511                         if( client )
512                         {
513                                 /* Channel-User-Mode */
514                                 if( Channel_UserModeAdd( Channel, client, x[0] ))
515                                 {
516                                         strlcat( the_args, Client_ID( client ), sizeof( the_args ));
517                                         strlcat( the_args, " ", sizeof( the_args ));
518                                         strlcat( the_modes, x, sizeof( the_modes ));
519                                         Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
520                                 }
521                         }
522                         else
523                         {
524                                 /* Channel-Mode */
525                                 if( Channel_ModeAdd( Channel, x[0] ))
526                                 {
527                                         strlcat( the_modes, x, sizeof( the_modes ));
528                                         Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
529                                 }
530                         }
531                 }
532                 else
533                 {
534                         /* Unset mode */
535                         if( client )
536                         {
537                                 /* Channel-User-Mode */
538                                 if( Channel_UserModeDel( Channel, client, x[0] ))
539                                 {
540                                         strlcat( the_args, Client_ID( client ), sizeof( the_args ));
541                                         strlcat( the_args, " ", sizeof( the_args ));
542                                         strlcat( the_modes, x, sizeof( the_modes ));
543                                         Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
544                                 }
545                         }
546                         else
547                         {
548                                 /* Channel-Mode */
549                                 if( Channel_ModeDel( Channel, x[0] ))
550                                 {
551                                         strlcat( the_modes, x, sizeof( the_modes ));
552                                         Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
553                                 }
554                         }
555                 }
556
557                 /* Are there additional arguments to add? */
558                 if( argadd[0] )
559                 {
560                         if( the_args[strlen( the_args ) - 1] != ' ' ) strlcat( the_args, " ", sizeof( the_args ));
561                         strlcat( the_args, argadd, sizeof( the_args ));
562                 }
563         }
564 chan_exit:
565
566         /* Are there changed modes? */
567         if( the_modes[1] )
568         {
569                 /* Clean up mode string */
570                 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
571
572                 /* Clean up argument string if there are none */
573                 if( ! the_args[1] ) the_args[0] = '\0';
574
575                 if( Client_Type( Client ) == CLIENT_SERVER )
576                 {
577                         /* Forward mode changes to channel users and other servers */
578                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
579                         IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
580                 }
581                 else
582                 {
583                         /* Send reply to client and inform other servers and channel users */
584                         ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
585                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
586                         IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
587                 }
588         }
589
590         return CONNECTED;
591 } /* Channel_Mode */
592
593
594 GLOBAL BOOLEAN
595 IRC_AWAY( CLIENT *Client, REQUEST *Req )
596 {
597         assert( Client != NULL );
598         assert( Req != NULL );
599
600         /* Falsche Anzahl Parameter? */
601         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
602
603         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
604         {
605                 /* AWAY setzen */
606                 Client_SetAway( Client, Req->argv[0] );
607                 Client_ModeAdd( Client, 'a' );
608                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
609                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
610         }
611         else
612         {
613                 /* AWAY loeschen */
614                 Client_ModeDel( Client, 'a' );
615                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
616                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
617         }
618 } /* IRC_AWAY */
619
620
621 LOCAL BOOLEAN
622 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
623 {
624         CHAR *mask;
625
626         assert( Client != NULL );
627         assert( Channel != NULL );
628         assert( Pattern != NULL );
629
630         mask = Lists_MakeMask( Pattern );
631
632         if( ! Lists_AddInvited( Prefix, mask, Channel, FALSE )) return CONNECTED;
633         return Send_ListChange( "+I", Prefix, Client, Channel, mask );
634 } /* Add_Invite */
635
636
637 LOCAL BOOLEAN
638 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
639 {
640         CHAR *mask;
641
642         assert( Client != NULL );
643         assert( Channel != NULL );
644         assert( Pattern != NULL );
645
646         mask = Lists_MakeMask( Pattern );
647
648         if( ! Lists_AddBanned( Prefix, mask, Channel )) return CONNECTED;
649         return Send_ListChange( "+b", Prefix, Client, Channel, mask );
650 } /* Add_Ban */
651
652
653 LOCAL BOOLEAN
654 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
655 {
656         CHAR *mask;
657
658         assert( Client != NULL );
659         assert( Channel != NULL );
660         assert( Pattern != NULL );
661
662         mask = Lists_MakeMask( Pattern );
663         Lists_DelInvited( mask, Channel );
664         return Send_ListChange( "-I", Prefix, Client, Channel, mask );
665 } /* Del_Invite */
666
667
668 LOCAL BOOLEAN
669 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
670 {
671         CHAR *mask;
672
673         assert( Client != NULL );
674         assert( Channel != NULL );
675         assert( Pattern != NULL );
676
677         mask = Lists_MakeMask( Pattern );
678         Lists_DelBanned( mask, Channel );
679         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
680 } /* Del_Ban */
681
682
683 LOCAL BOOLEAN
684 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
685 {
686         /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
687
688         BOOLEAN ok;
689
690         if( Client_Type( Client ) == CLIENT_USER )
691         {
692                 /* Bestaetigung an Client */
693                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
694         }
695         else ok = TRUE;
696
697         /* an andere Server */
698         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
699
700         /* und lokale User im Channel */
701         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
702         
703         return ok;
704 } /* Send_ListChange */
705
706
707 /* -eof- */