]> arthur.barton.de Git - ngircd.git/blob - src/ngircd/irc-mode.c
- Non-memers of a channel could crash the server when trying to change its modes...
[ngircd.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.24.2.4 2003/01/17 19:08:57 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; strcat( the_modes, x );
146                                         }
147                                         if( *mode_ptr == '+' ) set = TRUE;
148                                         else set = FALSE;
149                                 }
150                                 continue;
151                 }
152                 
153                 /* Validate modes */
154                 x[0] = '\0';
155                 switch( *mode_ptr )
156                 {
157                         case 'a':
158                                 /* Away */
159                                 if( Client_Type( Client ) == CLIENT_SERVER )
160                                 {
161                                         x[0] = 'a';
162                                         Client_SetAway( Client, DEFAULT_AWAY_MSG );
163                                 }
164                                 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
165                                 break;
166                         case 'i':
167                                 /* Invisible */
168                                 x[0] = 'i';
169                                 break;
170                         case 'o':
171                                 /* IRC operator (only unsetable!) */
172                                 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
173                                 {
174                                         Client_SetOperByMe( Target, FALSE );
175                                         x[0] = 'o';
176                                 }
177                                 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
178                                 break;
179                         case 'r':
180                                 /* Restricted (only setable) */
181                                 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
182                                 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
183                                 break;
184                         case 's':
185                                 /* Server messages */
186                                 x[0] = 's';
187                                 break;
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] )) strcat( the_modes, x );
203
204                 }
205                 else
206                 {
207                         /* Unset mode */
208                         if( Client_ModeDel( Target, x[0] )) strcat( the_modes, x );
209                 }               
210         }
211 client_exit:
212         
213         /* Are there changed modes? */
214         if( the_modes[1] )
215         {
216                 /* Remoce needless action modifier characters */
217                 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
218
219                 if( Client_Type( Client ) == CLIENT_SERVER )
220                 {
221                         /* Forward modes to other servers */
222                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
223                 }
224                 else
225                 {
226                         /* Send reply to client and inform other servers */
227                         ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s", Client_ID( Target ), the_modes );
228                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
229                 }
230                 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
231         }
232                 
233         return ok;
234 } /* Client_Mode */
235
236
237 LOCAL BOOLEAN
238 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
239 {
240         /* Handle channel and channel-user modes */
241
242         CHAR the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
243         BOOLEAN ok, set, modeok, skiponce;
244         INT mode_arg, arg_arg;
245         CLIENT *client;
246         LONG l;
247
248         /* Mode request: let's answer it :-) */
249         if( Req->argc == 1 )
250         {
251                 /* Member or not? -- That's the question! */
252                 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), Channel_Modes( Channel ));
253
254                 /* The sender is a member: generate extended reply */
255                 strcpy( the_modes, Channel_Modes( Channel ));
256                 mode_ptr = the_modes;
257                 strcpy( the_args, "" );
258                 while( *mode_ptr )
259                 {
260                         switch( *mode_ptr )
261                         {
262                                 case 'l':
263                                         snprintf( argadd, sizeof( argadd ), " %ld", Channel_MaxUsers( Channel ));
264                                         strcat( the_args, argadd );
265                                         break;
266                                 case 'k':
267                                         strcat( the_args, " " );
268                                         strcat( the_args, Channel_Key( Channel ));
269                                         break;
270                         }
271                         mode_ptr++;
272                 }
273                 if( the_args[0] ) strcat( the_modes, the_args );
274
275                 return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), the_modes );
276         }
277
278         /* Is the user allowed to change modes? */
279         if( Client_Type( Client ) == CLIENT_USER )
280         {
281                 /* Is the originating user on that channel? */
282                 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Channel_Name( Channel ));
283
284                 /* Is he channel operator? */
285                 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = TRUE;
286                 else modeok = FALSE;
287                 if( Conf_OperCanMode )
288                 {
289                         /* auch IRC-Operatoren duerfen MODE verwenden */
290                         if( Client_OperByMe( Origin )) modeok = TRUE;
291                 }
292         }
293         else modeok = TRUE;
294
295         mode_arg = 1;
296         mode_ptr = Req->argv[mode_arg];
297         if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
298         else arg_arg = -1;
299
300         /* Initial state: set or unset modes? */
301         skiponce = FALSE;
302         if( *mode_ptr == '-' ) set = FALSE;
303         else if( *mode_ptr == '+' ) set = TRUE;
304         else set = skiponce = TRUE;
305
306         /* Prepare reply string */
307         if( set ) strcpy( the_modes, "+" );
308         else strcpy( the_modes, "-" );
309         strcpy( the_args, " " );
310
311         x[1] = '\0';
312         ok = CONNECTED;
313         while( mode_ptr )
314         {
315                 if( ! skiponce ) mode_ptr++;
316                 if( ! *mode_ptr )
317                 {
318                         /* Try next argument if there's any */
319                         if( arg_arg > mode_arg ) mode_arg = arg_arg;
320                         else mode_arg++;
321                         if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
322                         else break;
323                         if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
324                         else arg_arg = -1;
325                 }
326                 skiponce = FALSE;
327
328                 switch( *mode_ptr )
329                 {
330                         case '+':
331                         case '-':
332                                 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
333                                 {
334                                         /* Action modifier ("+"/"-") must be changed ... */
335                                         if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' ))
336                                         {
337                                                 /* Adjust last action modifier in result */
338                                                 the_modes[strlen( the_modes ) - 1] = *mode_ptr;
339                                         }
340                                         else
341                                         {
342                                                 /* Append modifier character to result string */
343                                                 x[0] = *mode_ptr; strcat( the_modes, x );
344                                         }
345                                         if( *mode_ptr == '+' ) set = TRUE;
346                                         else set = FALSE;
347                                 }
348                                 continue;
349                 }
350
351                 /* Are there arguments left? */
352                 if( arg_arg >= Req->argc ) arg_arg = -1;
353
354                 /* Validate modes */
355                 x[0] = '\0';
356                 argadd[0] = '\0';
357                 client = NULL;
358                 switch( *mode_ptr )
359                 {
360                         /* Channel modes */
361                         case 'i':
362                                 /* Invite-Only */
363                                 if( modeok ) x[0] = 'i';
364                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
365                                 break;
366                         case 'm':
367                                 /* Moderated */
368                                 if( modeok ) x[0] = 'm';
369                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
370                                 break;
371                         case 'n':
372                                 /* kein Schreiben in den Channel von aussen */
373                                 if( modeok ) x[0] = 'n';
374                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
375                                 break;
376                         case 't':
377                                 /* Topic Lock */
378                                 if( modeok ) x[0] = 't';
379                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
380                                 break;
381                         case 'P':
382                                 /* Persistent channel */
383                                 if( modeok )
384                                 {
385                                         if( set && ( ! Client_OperByMe( Client )))
386                                         {
387                                                 /* Only IRC operators are allowed to set P mode */
388                                                 ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
389                                         }
390                                         else x[0] = 'P';
391                                 }
392                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
393                                 break;
394
395                         /* Channel user modes */
396                         case 'o':
397                                 /* Channel operator */
398                         case 'v':
399                                 /* Voice */
400                                 if( arg_arg > mode_arg )
401                                 {
402                                         if( modeok )
403                                         {
404                                                 client = Client_Search( Req->argv[arg_arg] );
405                                                 if( client ) x[0] = *mode_ptr;
406                                                 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
407                                         }
408                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
409                                         Req->argv[arg_arg][0] = '\0';
410                                         arg_arg++;
411                                 }
412                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
413                                 break;
414                         case 'k':
415                                 /* Channel key */
416                                 if( ! set )
417                                 {
418                                         if( modeok ) x[0] = *mode_ptr;
419                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
420                                         break;
421                                 }
422                                 if( arg_arg > mode_arg )
423                                 {
424                                         if( modeok )
425                                         {
426                                                 Channel_ModeDel( Channel, 'k' );
427                                                 Channel_SetKey( Channel, Req->argv[arg_arg] );
428                                                 strcpy( argadd, Channel_Key( Channel ));
429                                                 x[0] = *mode_ptr;
430                                         }
431                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
432                                         Req->argv[arg_arg][0] = '\0';
433                                         arg_arg++;
434                                 }
435                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
436                                 break;
437                         case 'l':
438                                 /* Member limit */
439                                 if( ! set )
440                                 {
441                                         if( modeok ) x[0] = *mode_ptr;
442                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
443                                         break;
444                                 }
445                                 if( arg_arg > mode_arg )
446                                 {
447                                         if( modeok )
448                                         {
449                                                 l = atol( Req->argv[arg_arg] );
450                                                 if( l > 0 && l < 0xFFFF )
451                                                 {
452                                                         Channel_ModeDel( Channel, 'l' );
453                                                         Channel_SetMaxUsers( Channel, l );
454                                                         sprintf( argadd, "%ld", l );
455                                                         x[0] = *mode_ptr;
456                                                 }
457                                         }
458                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
459                                         Req->argv[arg_arg][0] = '\0';
460                                         arg_arg++;
461                                 }
462                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
463                                 break;
464
465                         /* Channel lists */
466                         case 'I':
467                                 /* Invite lists */
468                                 if( arg_arg > mode_arg )
469                                 {
470                                         /* modify list */
471                                         if( modeok )
472                                         {
473                                                 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
474                                                 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
475                                         }
476                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
477                                         Req->argv[arg_arg][0] = '\0';
478                                         arg_arg++;
479                                 }
480                                 else Lists_ShowInvites( Origin, Channel );
481                                 break;
482                         case 'b':
483                                 /* Ban lists */
484                                 if( arg_arg > mode_arg )
485                                 {
486                                         /* modify list */
487                                         if( modeok )
488                                         {
489                                                 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
490                                                 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
491                                         }
492                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
493                                         Req->argv[arg_arg][0] = '\0';
494                                         arg_arg++;
495                                 }
496                                 else Lists_ShowBans( Origin, Channel );
497                                 break;
498
499                         default:
500                                 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
501                                 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
502                                 x[0] = '\0';
503                                 goto chan_exit;
504                 }
505                 if( ! ok ) break;
506
507                 /* Is there a valid mode change? */
508                 if( ! x[0] ) continue;
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                                         strcat( the_args, Client_ID( client ));
519                                         strcat( the_args, " " ); strcat( the_modes, x );
520                                         Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
521                                 }
522                         }
523                         else
524                         {
525                                 /* Channel-Mode */
526                                 if( Channel_ModeAdd( Channel, x[0] ))
527                                 {
528                                         strcat( the_modes, x );
529                                         Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
530                                 }
531                         }
532                 }
533                 else
534                 {
535                         /* Unset mode */
536                         if( client )
537                         {
538                                 /* Channel-User-Mode */
539                                 if( Channel_UserModeDel( Channel, client, x[0] ))
540                                 {
541                                         strcat( the_args, Client_ID( client ));
542                                         strcat( the_args, " " ); strcat( the_modes, x );
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                                         strcat( the_modes, x );
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] != ' ' ) strcat( the_args, " " );
561                         strcat( the_args, argadd );
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- */