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