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