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