]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
Fixed propagation of channel mode 'P' on server links.
[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.50 2007/10/14 12: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 "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( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
247 {
248         /* Handle channel and channel-user modes */
249
250         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
251         bool ok, set, modeok = false, skiponce, use_servermode = false;
252         int mode_arg, arg_arg;
253         CLIENT *client;
254         long l;
255         size_t len;
256
257         /* Mode request: let's answer it :-) */
258         if( Req->argc == 1 )
259         {
260                 /* Member or not? -- That's the question! */
261                 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), Channel_Modes( Channel ));
262
263                 /* The sender is a member: generate extended reply */
264                 strlcpy( the_modes, Channel_Modes( Channel ), sizeof( the_modes ));
265                 mode_ptr = the_modes;
266                 the_args[0] = '\0';
267                 while( *mode_ptr )
268                 {
269                         switch( *mode_ptr )
270                         {
271                                 case 'l':
272                                         snprintf( argadd, sizeof( argadd ), " %lu", Channel_MaxUsers( Channel ));
273                                         strlcat( the_args, argadd, sizeof( the_args ));
274                                         break;
275                                 case 'k':
276                                         strlcat( the_args, " ", sizeof( the_args ));
277                                         strlcat( the_args, Channel_Key( Channel ), sizeof( the_args ));
278                                         break;
279                         }
280                         mode_ptr++;
281                 }
282                 if( the_args[0] ) strlcat( the_modes, the_args, sizeof( the_modes ));
283
284                 return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), the_modes );
285         }
286
287         /* Is the user allowed to change modes? */
288         if( Client_Type( Client ) == CLIENT_USER )
289         {
290                 /* Is the originating user on that channel? */
291                 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Channel_Name( Channel ));
292
293                 /* Is he channel operator? */
294                 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = true;
295                 else if( Conf_OperCanMode )
296                 {
297                         /* IRC-Operators can use MODE as well */
298                         if( Client_OperByMe( Origin )) {
299                                 modeok = true;
300                                 if ( Conf_OperServerMode ) use_servermode = true; /* Change Origin to Server */
301                         }
302                 }
303         }
304         else modeok = true;
305
306         mode_arg = 1;
307         mode_ptr = Req->argv[mode_arg];
308         if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
309         else arg_arg = -1;
310
311         /* Initial state: set or unset modes? */
312         skiponce = false;
313         if( *mode_ptr == '-' ) set = false;
314         else if( *mode_ptr == '+' ) set = true;
315         else set = skiponce = true;
316
317         /* Prepare reply string */
318         if( set ) strcpy( the_modes, "+" );
319         else strcpy( the_modes, "-" );
320         strcpy( the_args, " " );
321
322         x[1] = '\0';
323         ok = CONNECTED;
324         while( mode_ptr )
325         {
326                 if( ! skiponce ) mode_ptr++;
327                 if( ! *mode_ptr )
328                 {
329                         /* Try next argument if there's any */
330                         if( arg_arg > mode_arg ) mode_arg = arg_arg;
331                         else mode_arg++;
332                         if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
333                         else break;
334                         if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
335                         else arg_arg = -1;
336                 }
337                 skiponce = false;
338
339                 switch( *mode_ptr )
340                 {
341                         case '+':
342                         case '-':
343                                 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
344                                 {
345                                         /* Action modifier ("+"/"-") must be changed ... */
346                                         len = strlen( the_modes ) - 1;
347                                         if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
348                                         {
349                                                 /* Adjust last action modifier in result */
350                                                 the_modes[len] = *mode_ptr;
351                                         }
352                                         else
353                                         {
354                                                 /* Append modifier character to result string */
355                                                 x[0] = *mode_ptr;
356                                                 strlcat( the_modes, x, sizeof( the_modes ));
357                                         }
358                                         if( *mode_ptr == '+' ) set = true;
359                                         else set = false;
360                                 }
361                                 continue;
362                 }
363
364                 /* Are there arguments left? */
365                 if( arg_arg >= Req->argc ) arg_arg = -1;
366
367                 /* Validate modes */
368                 x[0] = '\0';
369                 argadd[0] = '\0';
370                 client = NULL;
371                 switch( *mode_ptr )
372                 {
373                         /* --- Channel modes --- */
374
375                         case 'i': /* Invite only */
376                         case 'm': /* Moderated */
377                         case 'n': /* Only members can write */
378                         case 's': /* Secret channel */
379                         case 't': /* Topic locked */
380                                 if( modeok ) x[0] = *mode_ptr;
381                                 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
382                                 break;
383
384                         case 'k': /* Channel key */
385                                 if( ! set )
386                                 {
387                                         if( modeok ) x[0] = *mode_ptr;
388                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
389                                         break;
390                                 }
391                                 if( arg_arg > mode_arg )
392                                 {
393                                         if( modeok )
394                                         {
395                                                 Channel_ModeDel( Channel, 'k' );
396                                                 Channel_SetKey( Channel, Req->argv[arg_arg] );
397                                                 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
398                                                 x[0] = *mode_ptr;
399                                         }
400                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
401                                         Req->argv[arg_arg][0] = '\0';
402                                         arg_arg++;
403                                 }
404                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
405                                 break;
406
407                         case 'l': /* Member limit */
408                                 if( ! set )
409                                 {
410                                         if( modeok ) x[0] = *mode_ptr;
411                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
412                                         break;
413                                 }
414                                 if( arg_arg > mode_arg )
415                                 {
416                                         if( modeok )
417                                         {
418                                                 l = atol( Req->argv[arg_arg] );
419                                                 if( l > 0 && l < 0xFFFF )
420                                                 {
421                                                         Channel_ModeDel( Channel, 'l' );
422                                                         Channel_SetMaxUsers( Channel, l );
423                                                         snprintf( argadd, sizeof( argadd ), "%ld", l );
424                                                         x[0] = *mode_ptr;
425                                                 }
426                                         }
427                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
428                                         Req->argv[arg_arg][0] = '\0';
429                                         arg_arg++;
430                                 }
431                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
432                                 break;
433
434                         case 'P': /* Persistent channel */
435                                 if (modeok) {
436                                         /* Only IRC operators are allowed to
437                                          * set the 'P' channel mode! */
438                                         if (set && ! (Client_OperByMe(Client)
439                                             || Client_Type(Client) == CLIENT_SERVER)) {
440                                                 ok = IRC_WriteStrClient(Origin,
441                                                         ERR_NOPRIVILEGES_MSG,
442                                                         Client_ID(Origin));
443                                         } else
444                                                 x[0] = 'P';
445                                 } else
446                                         ok = IRC_WriteStrClient(Origin,
447                                                 ERR_CHANOPRIVSNEEDED_MSG,
448                                                 Client_ID(Origin),
449                                                 Channel_Name(Channel));
450                                 break;
451
452                         /* --- Channel user modes --- */
453
454                         case 'o': /* Channel operator */
455                         case 'v': /* Voice */
456                                 if( arg_arg > mode_arg )
457                                 {
458                                         if( modeok )
459                                         {
460                                                 client = Client_Search( Req->argv[arg_arg] );
461                                                 if( client ) x[0] = *mode_ptr;
462                                                 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
463                                         }
464                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
465                                         Req->argv[arg_arg][0] = '\0';
466                                         arg_arg++;
467                                 }
468                                 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
469                                 break;
470
471                         /* --- Channel lists --- */
472
473                         case 'I': /* Invite lists */
474                                 if( arg_arg > mode_arg )
475                                 {
476                                         /* modify list */
477                                         if( modeok )
478                                         {
479                                                 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
480                                                 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
481                                         }
482                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
483                                         Req->argv[arg_arg][0] = '\0';
484                                         arg_arg++;
485                                 }
486                                 else Channel_ShowInvites( Origin, Channel );
487                                 break;
488
489                         case 'b': /* Ban lists */
490                                 if( arg_arg > mode_arg )
491                                 {
492                                         /* modify list */
493                                         if( modeok )
494                                         {
495                                                 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
496                                                 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
497                                         }
498                                         else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
499                                         Req->argv[arg_arg][0] = '\0';
500                                         arg_arg++;
501                                 }
502                                 else Channel_ShowBans( Origin, Channel );
503                                 break;
504
505                         default:
506                                 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
507                                 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
508                                 x[0] = '\0';
509                                 goto chan_exit;
510                 }
511                 if( ! ok ) break;
512
513                 /* Is there a valid mode change? */
514                 if( ! x[0] ) continue;
515
516                 /* Validate target client */
517                 if( client && ( ! Channel_IsMemberOf( Channel, client )))
518                 {
519                         if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
520                         continue;
521                 }
522
523                 if( set )
524                 {
525                         /* Set mode */
526                         if( client )
527                         {
528                                 /* Channel-User-Mode */
529                                 if( Channel_UserModeAdd( Channel, client, x[0] ))
530                                 {
531                                         strlcat( the_args, Client_ID( client ), sizeof( the_args ));
532                                         strlcat( the_args, " ", sizeof( the_args ));
533                                         strlcat( the_modes, x, sizeof( the_modes ));
534                                         Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
535                                 }
536                         }
537                         else
538                         {
539                                 /* Channel-Mode */
540                                 if( Channel_ModeAdd( Channel, x[0] ))
541                                 {
542                                         strlcat( the_modes, x, sizeof( the_modes ));
543                                         Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
544                                 }
545                         }
546                 }
547                 else
548                 {
549                         /* Unset mode */
550                         if( client )
551                         {
552                                 /* Channel-User-Mode */
553                                 if( Channel_UserModeDel( Channel, client, x[0] ))
554                                 {
555                                         strlcat( the_args, Client_ID( client ), sizeof( the_args ));
556                                         strlcat( the_args, " ", sizeof( the_args ));
557                                         strlcat( the_modes, x, sizeof( the_modes ));
558                                         Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
559                                 }
560                         }
561                         else
562                         {
563                                 /* Channel-Mode */
564                                 if( Channel_ModeDel( Channel, x[0] ))
565                                 {
566                                         strlcat( the_modes, x, sizeof( the_modes ));
567                                         Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
568                                 }
569                         }
570                 }
571
572                 /* Are there additional arguments to add? */
573                 if( argadd[0] )
574                 {
575                         len = strlen( the_args ) - 1;
576                         if( the_args[len] != ' ' ) strlcat( the_args, " ", sizeof( the_args ));
577                         strlcat( the_args, argadd, sizeof( the_args ));
578                 }
579         }
580 chan_exit:
581
582         /* Are there changed modes? */
583         if( the_modes[1] )
584         {
585                 /* Clean up mode string */
586                 len = strlen( the_modes ) - 1;
587                 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
588
589                 /* Clean up argument string if there are none */
590                 if( ! the_args[1] ) the_args[0] = '\0';
591
592                 if( Client_Type( Client ) == CLIENT_SERVER )
593                 {
594                         /* Forward mode changes to channel users and other servers */
595                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
596                         IRC_WriteStrChannelPrefix( Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
597                 }
598                 else
599                 {
600                         if ( use_servermode ) Origin = Client_ThisServer();
601
602                         /* Send reply to client and inform other servers and channel users */
603                         ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
604                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
605                         IRC_WriteStrChannelPrefix( Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
606                 }
607         }
608
609         IRC_SetPenalty( Client, 1 );
610         return CONNECTED;
611 } /* Channel_Mode */
612
613
614 GLOBAL bool
615 IRC_AWAY( CLIENT *Client, REQUEST *Req )
616 {
617         assert( Client != NULL );
618         assert( Req != NULL );
619
620         /* Falsche Anzahl Parameter? */
621         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
622
623         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
624         {
625                 /* AWAY setzen */
626                 Client_SetAway( Client, Req->argv[0] );
627                 Client_ModeAdd( Client, 'a' );
628                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
629                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
630         }
631         else
632         {
633                 /* AWAY loeschen */
634                 Client_ModeDel( Client, 'a' );
635                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
636                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
637         }
638 } /* IRC_AWAY */
639
640
641 static bool
642 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
643 {
644         char *mask;
645         bool already;
646
647         assert( Client != NULL );
648         assert( Channel != NULL );
649         assert( Pattern != NULL );
650
651         mask = Lists_MakeMask( Pattern );
652
653         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask );
654         if (!already) {
655                 if( ! Channel_AddInvite(Channel, mask, false ))
656                         return CONNECTED;
657         }
658         if ( already && ( Client_Type( Prefix ) == CLIENT_SERVER ))
659                 return CONNECTED;
660
661         return Send_ListChange( "+I", Prefix, Client, Channel, mask );
662 } /* Add_Invite */
663
664
665 static bool
666 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
667 {
668         char *mask;
669         bool already;
670
671         assert( Client != NULL );
672         assert( Channel != NULL );
673         assert( Pattern != NULL );
674
675         mask = Lists_MakeMask( Pattern );
676
677         already = Lists_CheckDupeMask(Channel_GetListBans(Channel), mask );
678         if (!already) {
679                 if( ! Channel_AddBan(Channel, mask))
680                         return CONNECTED;
681         }
682         if ( already && ( Client_Type( Prefix ) == CLIENT_SERVER ))
683                 return CONNECTED;
684
685         return Send_ListChange( "+b", Prefix, Client, Channel, mask );
686 } /* Add_Ban */
687
688
689 static bool
690 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
691 {
692         char *mask;
693
694         assert( Client != NULL );
695         assert( Channel != NULL );
696         assert( Pattern != NULL );
697
698         mask = Lists_MakeMask( Pattern );
699         Lists_Del(Channel_GetListInvites(Channel), mask);
700         return Send_ListChange( "-I", Prefix, Client, Channel, mask );
701 } /* Del_Invite */
702
703
704 static bool
705 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
706 {
707         char *mask;
708
709         assert( Client != NULL );
710         assert( Channel != NULL );
711         assert( Pattern != NULL );
712
713         mask = Lists_MakeMask( Pattern );
714         Lists_Del(Channel_GetListBans(Channel), mask);
715         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
716 } /* Del_Ban */
717
718
719 static bool
720 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Mask )
721 {
722         /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
723
724         bool ok;
725
726         if( Client_Type( Client ) == CLIENT_USER )
727         {
728                 /* Bestaetigung an Client */
729                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
730         }
731         else ok = true;
732
733         /* an andere Server */
734         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
735
736         /* und lokale User im Channel */
737         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
738         
739         return ok;
740 } /* Send_ListChange */
741
742
743 /* -eof- */