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