]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
cf6354171df7a454dbd704163b0a146e90673729
[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.31 2003/01/21 21:04:16 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                 /* Validate target client */
513                 if( client && ( ! Channel_IsMemberOf( Channel, client )))
514                 {
515                         if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
516                         continue;
517                 }
518
519                 if( set )
520                 {
521                         /* Set mode */
522                         if( client )
523                         {
524                                 /* Channel-User-Mode */
525                                 if( Channel_UserModeAdd( Channel, client, x[0] ))
526                                 {
527                                         strlcat( the_args, Client_ID( client ), sizeof( the_args ));
528                                         strlcat( the_args, " ", sizeof( the_args ));
529                                         strlcat( the_modes, x, sizeof( the_modes ));
530                                         Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
531                                 }
532                         }
533                         else
534                         {
535                                 /* Channel-Mode */
536                                 if( Channel_ModeAdd( Channel, x[0] ))
537                                 {
538                                         strlcat( the_modes, x, sizeof( the_modes ));
539                                         Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
540                                 }
541                         }
542                 }
543                 else
544                 {
545                         /* Unset mode */
546                         if( client )
547                         {
548                                 /* Channel-User-Mode */
549                                 if( Channel_UserModeDel( Channel, client, x[0] ))
550                                 {
551                                         strlcat( the_args, Client_ID( client ), sizeof( the_args ));
552                                         strlcat( the_args, " ", sizeof( the_args ));
553                                         strlcat( the_modes, x, sizeof( the_modes ));
554                                         Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
555                                 }
556                         }
557                         else
558                         {
559                                 /* Channel-Mode */
560                                 if( Channel_ModeDel( Channel, x[0] ))
561                                 {
562                                         strlcat( the_modes, x, sizeof( the_modes ));
563                                         Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
564                                 }
565                         }
566                 }
567
568                 /* Are there additional arguments to add? */
569                 if( argadd[0] )
570                 {
571                         if( the_args[strlen( the_args ) - 1] != ' ' ) strlcat( the_args, " ", sizeof( the_args ));
572                         strlcat( the_args, argadd, sizeof( the_args ));
573                 }
574         }
575 chan_exit:
576
577         /* Are there changed modes? */
578         if( the_modes[1] )
579         {
580                 /* Clean up mode string */
581                 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
582
583                 /* Clean up argument string if there are none */
584                 if( ! the_args[1] ) the_args[0] = '\0';
585
586                 if( Client_Type( Client ) == CLIENT_SERVER )
587                 {
588                         /* Forward mode changes to channel users and other servers */
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                 else
593                 {
594                         /* Send reply to client and inform other servers and channel users */
595                         ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
596                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
597                         IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
598                 }
599         }
600
601         return CONNECTED;
602 } /* Channel_Mode */
603
604
605 GLOBAL BOOLEAN
606 IRC_AWAY( CLIENT *Client, REQUEST *Req )
607 {
608         assert( Client != NULL );
609         assert( Req != NULL );
610
611         /* Falsche Anzahl Parameter? */
612         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
613
614         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
615         {
616                 /* AWAY setzen */
617                 Client_SetAway( Client, Req->argv[0] );
618                 Client_ModeAdd( Client, 'a' );
619                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
620                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
621         }
622         else
623         {
624                 /* AWAY loeschen */
625                 Client_ModeDel( Client, 'a' );
626                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
627                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
628         }
629 } /* IRC_AWAY */
630
631
632 LOCAL BOOLEAN
633 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
634 {
635         CHAR *mask;
636
637         assert( Client != NULL );
638         assert( Channel != NULL );
639         assert( Pattern != NULL );
640
641         mask = Lists_MakeMask( Pattern );
642
643         if( ! Lists_AddInvited( Prefix, mask, Channel, FALSE )) return CONNECTED;
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
653         assert( Client != NULL );
654         assert( Channel != NULL );
655         assert( Pattern != NULL );
656
657         mask = Lists_MakeMask( Pattern );
658
659         if( ! Lists_AddBanned( Prefix, mask, Channel )) return CONNECTED;
660         return Send_ListChange( "+b", Prefix, Client, Channel, mask );
661 } /* Add_Ban */
662
663
664 LOCAL BOOLEAN
665 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
666 {
667         CHAR *mask;
668
669         assert( Client != NULL );
670         assert( Channel != NULL );
671         assert( Pattern != NULL );
672
673         mask = Lists_MakeMask( Pattern );
674         Lists_DelInvited( mask, Channel );
675         return Send_ListChange( "-I", Prefix, Client, Channel, mask );
676 } /* Del_Invite */
677
678
679 LOCAL BOOLEAN
680 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
681 {
682         CHAR *mask;
683
684         assert( Client != NULL );
685         assert( Channel != NULL );
686         assert( Pattern != NULL );
687
688         mask = Lists_MakeMask( Pattern );
689         Lists_DelBanned( mask, Channel );
690         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
691 } /* Del_Ban */
692
693
694 LOCAL BOOLEAN
695 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
696 {
697         /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
698
699         BOOLEAN ok;
700
701         if( Client_Type( Client ) == CLIENT_USER )
702         {
703                 /* Bestaetigung an Client */
704                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
705         }
706         else ok = TRUE;
707
708         /* an andere Server */
709         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
710
711         /* und lokale User im Channel */
712         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
713         
714         return ok;
715 } /* Send_ListChange */
716
717
718 /* -eof- */