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