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