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