]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-mode.c
Correctly detect errors when handling "MODE x" commands
[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
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC commands for mode changes (like MODE, AWAY, etc.)
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "defines.h"
26 #include "conn.h"
27 #include "channel.h"
28 #include "irc-write.h"
29 #include "lists.h"
30 #include "log.h"
31 #include "parse.h"
32 #include "messages.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
191                         case 'o': /* IRC operator (only unsettable!) */
192                                 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
193                                 {
194                                         Client_SetOperByMe( Target, false );
195                                         x[0] = 'o';
196                                 }
197                                 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
198                                 break;
199
200                         case 'r': /* Restricted (only settable) */
201                                 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
202                                 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
203                                 break;
204
205                         case 'x': /* Cloak hostname */
206                                 if (Client_HasMode(Client, 'r'))
207                                         ok = IRC_WriteStrClient(Origin,
208                                                            ERR_RESTRICTED_MSG,
209                                                            Client_ID(Origin));
210                                 else
211                                         x[0] = 'x';
212                                 break;
213
214                         default:
215                                 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
216                                 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
217                                 x[0] = '\0';
218                                 goto client_exit;
219                 }
220                 if( ! ok ) break;
221
222                 /* Is there a valid mode change? */
223                 if( ! x[0] ) continue;
224
225                 if( set )
226                 {
227                         /* Set mode */
228                         if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
229
230                 }
231                 else
232                 {
233                         /* Unset mode */
234                         if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
235                 }               
236         }
237 client_exit:
238         
239         /* Are there changed modes? */
240         if( the_modes[1] )
241         {
242                 /* Remoce needless action modifier characters */
243                 len = strlen( the_modes ) - 1;
244                 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
245
246                 if( Client_Type( Client ) == CLIENT_SERVER )
247                 {
248                         /* Forward modes to other servers */
249                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
250                 }
251                 else
252                 {
253                         /* Send reply to client and inform other servers */
254                         ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
255                         IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
256                 }
257                 LogDebug("%s \"%s\": Mode change, now \"%s\".",
258                          Client_TypeText(Target), Client_Mask(Target),
259                          Client_Modes(Target));
260         }
261         
262         IRC_SetPenalty( Client, 1 );    
263         return ok;
264 } /* Client_Mode */
265
266
267 static bool
268 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
269 {
270         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
271         const char *mode_ptr;
272
273         /* Member or not? -- That's the question! */
274         if (!Channel_IsMemberOf(Channel, Origin))
275                 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
276                         Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
277
278         /* The sender is a member: generate extended reply */
279         strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
280         mode_ptr = the_modes;
281         the_args[0] = '\0';
282
283         while(*mode_ptr) {
284                 switch(*mode_ptr) {
285                 case 'l':
286                         snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
287                         strlcat(the_args, argadd, sizeof(the_args));
288                         break;
289                 case 'k':
290                         strlcat(the_args, " ", sizeof(the_args));
291                         strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
292                         break;
293                 }
294                 mode_ptr++;
295         }
296         if (the_args[0])
297                 strlcat(the_modes, the_args, sizeof(the_modes));
298
299         if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
300                                 Client_ID(Origin), Channel_Name(Channel),
301                                 the_modes))
302                 return DISCONNECTED;
303 #ifndef STRICT_RFC
304         if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
305                                   Client_ID(Origin), Channel_Name(Channel),
306                                   Channel_CreationTime(Channel)))
307                 return DISCONNECTED;
308 #endif
309         return CONNECTED;
310 }
311
312
313 /**
314  * Handle channel mode and channel-user mode changes
315  */
316 static bool
317 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
318 {
319         char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
320             argadd[CLIENT_PASS_LEN], *mode_ptr;
321         bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
322         int mode_arg, arg_arg;
323         CLIENT *client;
324         long l;
325         size_t len;
326
327         if (Channel_IsModeless(Channel))
328                 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
329                                 Client_ID(Client), Channel_Name(Channel));
330
331         /* Mode request: let's answer it :-) */
332         if (Req->argc <= 1)
333                 return Channel_Mode_Answer_Request(Origin, Channel);
334
335         Channel_CheckAdminRights(Channel, Client, Origin,
336                                  &onchannel, &modeok, &use_servermode);
337
338         if (!onchannel && !modeok)
339                 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
340                                           Client_ID(Origin),
341                                           Channel_Name(Channel));
342
343         mode_arg = 1;
344         mode_ptr = Req->argv[mode_arg];
345         if (Req->argc > mode_arg + 1)
346                 arg_arg = mode_arg + 1;
347         else
348                 arg_arg = -1;
349
350         /* Initial state: set or unset modes? */
351         skiponce = false;
352         switch (*mode_ptr) {
353         case '-':
354                 set = false;
355                 break;
356         case '+':
357                 set = true;
358                 break;
359         default:
360                 set = true;
361                 skiponce = true;
362         }
363
364         /* Prepare reply string */
365         strcpy(the_modes, set ? "+" : "-");
366         the_args[0] = '\0';
367
368         x[1] = '\0';
369         connected = CONNECTED;
370         while (mode_ptr) {
371                 if (!skiponce)
372                         mode_ptr++;
373                 if (!*mode_ptr) {
374                         /* Try next argument if there's any */
375                         if (arg_arg > mode_arg)
376                                 mode_arg = arg_arg;
377                         else
378                                 mode_arg++;
379
380                         if (mode_arg >= Req->argc)
381                                 break;
382                         mode_ptr = Req->argv[mode_arg];
383
384                         if (Req->argc > mode_arg + 1)
385                                 arg_arg = mode_arg + 1;
386                         else
387                                 arg_arg = -1;
388                 }
389                 skiponce = false;
390
391                 switch (*mode_ptr) {
392                 case '+':
393                 case '-':
394                         if (((*mode_ptr == '+') && !set)
395                             || ((*mode_ptr == '-') && set)) {
396                                 /* Action modifier ("+"/"-") must be changed ... */
397                                 len = strlen(the_modes) - 1;
398                                 if (the_modes[len] == '+' || the_modes[len] == '-') {
399                                         /* Adjust last action modifier in result */
400                                         the_modes[len] = *mode_ptr;
401                                 } else {
402                                         /* Append modifier character to result string */
403                                         x[0] = *mode_ptr;
404                                         strlcat(the_modes, x, sizeof(the_modes));
405                                 }
406                                 set = *mode_ptr == '+';
407                         }
408                         continue;
409                 }
410
411                 /* Are there arguments left? */
412                 if (arg_arg >= Req->argc)
413                         arg_arg = -1;
414
415                 /* Validate modes */
416                 x[0] = '\0';
417                 argadd[0] = '\0';
418                 client = NULL;
419                 switch (*mode_ptr) {
420                 /* --- Channel modes --- */
421                 case 'i': /* Invite only */
422                 case 'm': /* Moderated */
423                 case 'n': /* Only members can write */
424                 case 's': /* Secret channel */
425                 case 't': /* Topic locked */
426                 case 'z': /* Secure connections only */
427                         if (modeok)
428                                 x[0] = *mode_ptr;
429                         else
430                                 connected = IRC_WriteStrClient(Origin,
431                                         ERR_CHANOPRIVSNEEDED_MSG,
432                                         Client_ID(Origin), Channel_Name(Channel));
433                         break;
434                 case 'k': /* Channel key */
435                         if (!set) {
436                                 if (modeok)
437                                         x[0] = *mode_ptr;
438                                 else
439                                         connected = IRC_WriteStrClient(Origin,
440                                                 ERR_CHANOPRIVSNEEDED_MSG,
441                                                 Client_ID(Origin),
442                                                 Channel_Name(Channel));
443                                 break;
444                         }
445                         if (arg_arg > mode_arg) {
446                                 if (modeok) {
447                                         Channel_ModeDel(Channel, 'k');
448                                         Channel_SetKey(Channel,
449                                                        Req->argv[arg_arg]);
450                                         strlcpy(argadd, Channel_Key(Channel),
451                                                 sizeof(argadd));
452                                         x[0] = *mode_ptr;
453                                 } else {
454                                         connected = IRC_WriteStrClient(Origin,
455                                                 ERR_CHANOPRIVSNEEDED_MSG,
456                                                 Client_ID(Origin),
457                                                 Channel_Name(Channel));
458                                 }
459                                 Req->argv[arg_arg][0] = '\0';
460                                 arg_arg++;
461                         } else {
462                                 connected = IRC_WriteStrClient(Origin,
463                                         ERR_NEEDMOREPARAMS_MSG,
464                                         Client_ID(Origin), Req->command);
465                                 goto chan_exit;
466                         }
467                         break;
468                 case 'l': /* Member limit */
469                         if (!set) {
470                                 if (modeok)
471                                         x[0] = *mode_ptr;
472                                 else
473                                         connected = IRC_WriteStrClient(Origin,
474                                                 ERR_CHANOPRIVSNEEDED_MSG,
475                                                 Client_ID(Origin),
476                                                 Channel_Name(Channel));
477                                 break;
478                         }
479                         if (arg_arg > mode_arg) {
480                                 if (modeok) {
481                                         l = atol(Req->argv[arg_arg]);
482                                         if (l > 0 && l < 0xFFFF) {
483                                                 Channel_ModeDel(Channel, 'l');
484                                                 Channel_SetMaxUsers(Channel, l);
485                                                 snprintf(argadd, sizeof(argadd),
486                                                          "%ld", l);
487                                                 x[0] = *mode_ptr;
488                                         }
489                                 } else {
490                                         connected = IRC_WriteStrClient(Origin,
491                                                 ERR_CHANOPRIVSNEEDED_MSG,
492                                                 Client_ID(Origin),
493                                                 Channel_Name(Channel));
494                                 }
495                                 Req->argv[arg_arg][0] = '\0';
496                                 arg_arg++;
497                         } else {
498                                 connected = IRC_WriteStrClient(Origin,
499                                         ERR_NEEDMOREPARAMS_MSG,
500                                         Client_ID(Origin), Req->command);
501                                 goto chan_exit;
502                         }
503                         break;
504                 case 'O': /* IRC operators only */
505                         if (modeok) {
506                                 /* Only IRC operators are allowed to
507                                  * set the 'O' channel mode! */
508                                 if (set && !(Client_OperByMe(Client)
509                                     || Client_Type(Client) == CLIENT_SERVER))
510                                         connected = IRC_WriteStrClient(Origin,
511                                                 ERR_NOPRIVILEGES_MSG,
512                                                 Client_ID(Origin));
513                                 else
514                                         x[0] = 'O';
515                         } else
516                                 connected = IRC_WriteStrClient(Origin,
517                                                 ERR_CHANOPRIVSNEEDED_MSG,
518                                                 Client_ID(Origin),
519                                                 Channel_Name(Channel));
520                                 break;
521                 case 'P': /* Persistent channel */
522                         if (modeok) {
523                                 /* Only IRC operators are allowed to
524                                  * set the 'P' channel mode! */
525                                 if (set && !(Client_OperByMe(Client)
526                                     || Client_Type(Client) == CLIENT_SERVER))
527                                         connected = IRC_WriteStrClient(Origin,
528                                                 ERR_NOPRIVILEGES_MSG,
529                                                 Client_ID(Origin));
530                                 else
531                                         x[0] = 'P';
532                         } else
533                                 connected = IRC_WriteStrClient(Origin,
534                                         ERR_CHANOPRIVSNEEDED_MSG,
535                                         Client_ID(Origin),
536                                         Channel_Name(Channel));
537                         break;
538                 /* --- Channel user modes --- */
539                 case 'o': /* Channel operator */
540                 case 'v': /* Voice */
541                         if (arg_arg > mode_arg) {
542                                 if (modeok) {
543                                         client = Client_Search(Req->argv[arg_arg]);
544                                         if (client)
545                                                 x[0] = *mode_ptr;
546                                         else
547                                                 connected = IRC_WriteStrClient(Client,
548                                                         ERR_NOSUCHNICK_MSG,
549                                                         Client_ID(Client),
550                                                         Req->argv[arg_arg]);
551                                 } else {
552                                         connected = IRC_WriteStrClient(Origin,
553                                                 ERR_CHANOPRIVSNEEDED_MSG,
554                                                 Client_ID(Origin),
555                                                 Channel_Name(Channel));
556                                 }
557                                 Req->argv[arg_arg][0] = '\0';
558                                 arg_arg++;
559                         } else {
560                                 connected = IRC_WriteStrClient(Origin,
561                                         ERR_NEEDMOREPARAMS_MSG,
562                                         Client_ID(Origin), Req->command);
563                                 goto chan_exit;
564                         }
565                         break;
566                 /* --- Channel lists --- */
567                 case 'I': /* Invite lists */
568                 case 'b': /* Ban lists */
569                         if (arg_arg > mode_arg) {
570                                 /* modify list */
571                                 if (modeok) {
572                                         connected = set
573                                            ? Add_Ban_Invite(*mode_ptr, Origin,
574                                                 Client, Channel,
575                                                 Req->argv[arg_arg])
576                                            : Del_Ban_Invite(*mode_ptr, Origin,
577                                                 Client, Channel,
578                                                 Req->argv[arg_arg]);
579                                 } else {
580                                         connected = IRC_WriteStrClient(Origin,
581                                                 ERR_CHANOPRIVSNEEDED_MSG,
582                                                 Client_ID(Origin),
583                                                 Channel_Name(Channel));
584                                 }
585                                 Req->argv[arg_arg][0] = '\0';
586                                 arg_arg++;
587                         } else {
588                                 if (*mode_ptr == 'I')
589                                         Channel_ShowInvites(Origin, Channel);
590                                 else
591                                         Channel_ShowBans(Origin, Channel);
592                         }
593                         break;
594                 default:
595                         Log(LOG_DEBUG,
596                             "Unknown mode \"%c%c\" from \"%s\" on %s!?",
597                             set ? '+' : '-', *mode_ptr, Client_ID(Origin),
598                             Channel_Name(Channel));
599                         if (Client_Type(Client) != CLIENT_SERVER)
600                                 connected = IRC_WriteStrClient(Origin,
601                                         ERR_UMODEUNKNOWNFLAG2_MSG,
602                                         Client_ID(Origin),
603                                         set ? '+' : '-', *mode_ptr);
604                         x[0] = '\0';
605                         goto chan_exit;
606                 }       /* switch() */
607
608                 if (!connected)
609                         break;
610
611                 /* Is there a valid mode change? */
612                 if (!x[0])
613                         continue;
614
615                 /* Validate target client */
616                 if (client && (!Channel_IsMemberOf(Channel, client))) {
617                         if (!IRC_WriteStrClient
618                             (Origin, ERR_USERNOTINCHANNEL_MSG,
619                              Client_ID(Origin), Client_ID(client),
620                              Channel_Name(Channel)))
621                                 break;
622
623                         continue;
624                 }
625
626                 if (client) {
627                         /* Channel-User-Mode */
628                         retval = set
629                                ? Channel_UserModeAdd(Channel, client, x[0])
630                                : Channel_UserModeDel(Channel, client, x[0]);
631                         if (retval) {
632                                 strlcat(the_args, " ", sizeof(the_args));
633                                 strlcat(the_args, Client_ID(client),
634                                         sizeof(the_args));
635                                 strlcat(the_modes, x, sizeof(the_modes));
636                                 LogDebug
637                                     ("User \"%s\": Mode change on %s, now \"%s\"",
638                                      Client_Mask(client), Channel_Name(Channel),
639                                      Channel_UserModes(Channel, client));
640                         }
641                 } else {
642                         /* Channel-Mode */
643                         retval = set
644                                ? Channel_ModeAdd(Channel, x[0])
645                                : Channel_ModeDel(Channel, x[0]);
646                         if (retval) {
647                                 strlcat(the_modes, x, sizeof(the_modes));
648                                 LogDebug("Channel %s: Mode change, now \"%s\".",
649                                          Channel_Name(Channel),
650                                          Channel_Modes(Channel));
651                         }
652                 }
653
654                 /* Are there additional arguments to add? */
655                 if (argadd[0]) {
656                         strlcat(the_args, " ", sizeof(the_args));
657                         strlcat(the_args, argadd, sizeof(the_args));
658                 }
659         }
660
661       chan_exit:
662         /* Are there changed modes? */
663         if (the_modes[1]) {
664                 /* Clean up mode string */
665                 len = strlen(the_modes) - 1;
666                 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
667                         the_modes[len] = '\0';
668
669                 if (Client_Type(Client) == CLIENT_SERVER) {
670                         /* MODE requests for local channels from other servers
671                          * are definitely invalid! */
672                         if (Channel_IsLocal(Channel)) {
673                                 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
674                                 return CONNECTED;
675                         }
676
677                         /* Forward mode changes to channel users and all the
678                          * other remote servers: */
679                         IRC_WriteStrServersPrefix(Client, Origin,
680                                 "MODE %s %s%s", Channel_Name(Channel),
681                                 the_modes, the_args);
682                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
683                                 false, "MODE %s %s%s", Channel_Name(Channel),
684                                 the_modes, the_args);
685                 } else {
686                         if (use_servermode)
687                                 Origin = Client_ThisServer();
688                         /* Send reply to client and inform other servers and channel users */
689                         connected = IRC_WriteStrClientPrefix(Client, Origin,
690                                         "MODE %s %s%s", Channel_Name(Channel),
691                                         the_modes, the_args);
692                         /* Only forward requests for non-local channels */
693                         if (!Channel_IsLocal(Channel))
694                                 IRC_WriteStrServersPrefix(Client, Origin,
695                                         "MODE %s %s%s", Channel_Name(Channel),
696                                         the_modes, the_args);
697                         IRC_WriteStrChannelPrefix(Client, Channel, Origin,
698                                 false, "MODE %s %s%s", Channel_Name(Channel),
699                                 the_modes, the_args);
700                 }
701         }
702
703         IRC_SetPenalty(Client, 1);
704         return connected;
705 } /* Channel_Mode */
706
707
708 GLOBAL bool
709 IRC_AWAY( CLIENT *Client, REQUEST *Req )
710 {
711         assert( Client != NULL );
712         assert( Req != NULL );
713
714         if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
715
716         if(( Req->argc == 1 ) && (Req->argv[0][0] ))
717         {
718                 Client_SetAway( Client, Req->argv[0] );
719                 Client_ModeAdd( Client, 'a' );
720                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
721                 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
722         }
723         else
724         {
725                 Client_ModeDel( Client, 'a' );
726                 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
727                 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
728         }
729 } /* IRC_AWAY */
730
731
732 static bool
733 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
734 {
735         const char *mask;
736         bool already;
737         bool ret;
738
739         assert( Client != NULL );
740         assert( Channel != NULL );
741         assert( Pattern != NULL );
742         assert(what == 'I' || what == 'b');
743
744         mask = Lists_MakeMask(Pattern);
745
746         already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
747         if (!already) {
748                 if (what == 'I')
749                         ret = Channel_AddInvite(Channel, mask, false);
750                 else
751                         ret = Channel_AddBan(Channel, mask);
752                 if (!ret)
753                         return CONNECTED;
754         }
755         if (already && (Client_Type(Prefix) == CLIENT_SERVER))
756                 return CONNECTED;
757
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 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
766 {
767         const char *mask;
768         struct list_head *list;
769
770         assert( Client != NULL );
771         assert( Channel != NULL );
772         assert( Pattern != NULL );
773         assert(what == 'I' || what == 'b');
774
775         mask = Lists_MakeMask( Pattern );
776
777         if (what == 'I')
778                 list = Channel_GetListInvites(Channel);
779         else
780                 list = Channel_GetListBans(Channel);
781
782         Lists_Del(list, mask);
783         if (what == 'I')
784                 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
785         return Send_ListChange( "-b", Prefix, Client, Channel, mask );
786 }
787
788
789 static bool
790 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
791                 CHANNEL *Channel, const char *Mask)
792 {
793         bool ok;
794
795         if( Client_Type( Client ) == CLIENT_USER )
796         {
797                 /* send confirmation to client */
798                 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
799         }
800         else ok = true;
801
802         /* to other servers */
803         IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
804
805         /* and local users in channel */
806         IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
807         
808         return ok;
809 } /* Send_ListChange */
810
811
812 /* -eof- */