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