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