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