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