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