2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
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.
11 * IRC commands for mode changes (MODE, AWAY, ...)
27 #include "irc-write.h"
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 ));
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));
45 static bool Send_ListChange PARAMS(( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask ));
49 IRC_MODE( CLIENT *Client, REQUEST *Req )
54 assert( Client != NULL );
55 assert( Req != NULL );
58 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
60 /* Origin for answers */
61 if( Client_Type( Client ) == CLIENT_SERVER )
63 origin = Client_Search( Req->prefix );
64 if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
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]);
76 return Client_Mode(Client, Req, origin, cl);
78 return Channel_Mode(Client, Req, origin, chan);
80 /* No target found! */
81 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
82 Client_ID(Client), Req->argv[0]);
87 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
89 /* Handle client mode requests */
91 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
96 /* Is the client allowed to request or change the modes? */
97 if( Client_Type( Client ) == CLIENT_USER )
99 /* Users are only allowed to manipulate their own modes! */
100 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
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 ));
107 mode_ptr = Req->argv[mode_arg];
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 ));
114 /* Prepare reply string */
115 if( set ) strcpy( the_modes, "+" );
116 else strcpy( the_modes, "-" );
125 /* Try next argument if there's any */
127 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
135 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
137 /* Action modifier ("+"/"-") must be changed ... */
138 len = strlen( the_modes ) - 1;
139 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
141 /* Adjust last action modifier in result */
142 the_modes[len] = *mode_ptr;
146 /* Append modifier character to result string */
148 strlcat( the_modes, x, sizeof( the_modes ));
150 if( *mode_ptr == '+' ) set = true;
160 case 'i': /* Invisible */
161 case 's': /* Server messages */
162 case 'w': /* Wallops messages */
167 if( Client_Type( Client ) == CLIENT_SERVER )
170 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
172 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
175 case 'o': /* IRC operator (only unsettable!) */
176 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
178 Client_SetOperByMe( Target, false );
181 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
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 ));
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 );
197 /* Is there a valid mode change? */
198 if( ! x[0] ) continue;
203 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
209 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
214 /* Are there changed modes? */
217 /* Remoce needless action modifier characters */
218 len = strlen( the_modes ) - 1;
219 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
221 if( Client_Type( Client ) == CLIENT_SERVER )
223 /* Forward modes to other servers */
224 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
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 );
232 LogDebug("%s \"%s\": Mode change, now \"%s\".",
233 Client_TypeText(Target), Client_Mask(Target),
234 Client_Modes(Target));
237 IRC_SetPenalty( Client, 1 );
243 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
245 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
246 const char *mode_ptr;
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));
253 /* The sender is a member: generate extended reply */
254 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
255 mode_ptr = the_modes;
261 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
262 strlcat(the_args, argadd, sizeof(the_args));
265 strlcat(the_args, " ", sizeof(the_args));
266 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
272 strlcat(the_modes, the_args, sizeof(the_modes));
274 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
275 Client_ID(Origin), Channel_Name(Channel), the_modes);
280 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
282 /* Handle channel and channel-user modes */
284 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
285 bool ok, set, modeok = true, skiponce, use_servermode = false, retval;
286 int mode_arg, arg_arg;
291 /* Are modes allowed on channel? */
292 if (Channel_Name(Channel)[0] == '+')
293 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
294 Client_ID(Client), Channel_Name(Channel));
296 /* Mode request: let's answer it :-) */
298 return Channel_Mode_Answer_Request(Origin, Channel);
300 /* Is the user allowed to change modes? */
301 if (Client_Type(Client) == CLIENT_USER) {
302 /* Is the originating user on that channel? */
303 if (!Channel_IsMemberOf(Channel, Origin))
304 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
305 Client_ID(Origin), Channel_Name(Channel));
307 /* channel operator? */
308 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
310 else if(Conf_OperCanMode) {
311 /* IRC-Operators can use MODE as well */
312 if (Client_OperByMe(Origin)) {
314 if (Conf_OperServerMode)
315 use_servermode = true; /* Change Origin to Server */
321 mode_ptr = Req->argv[mode_arg];
322 if (Req->argc > mode_arg + 1)
323 arg_arg = mode_arg + 1;
327 /* Initial state: set or unset modes? */
330 case '-': set = false; break;
331 case '+': set = true; break;
337 /* Prepare reply string */
338 strcpy(the_modes, set ? "+" : "-");
347 /* Try next argument if there's any */
348 if (arg_arg > mode_arg)
353 if (mode_arg >= Req->argc)
355 mode_ptr = Req->argv[mode_arg];
357 if (Req->argc > mode_arg + 1)
358 arg_arg = mode_arg + 1;
367 if (((*mode_ptr == '+') && !set) || ((*mode_ptr == '-') && set)) {
368 /* Action modifier ("+"/"-") must be changed ... */
369 len = strlen( the_modes ) - 1;
370 if ((the_modes[len] == '+') || (the_modes[len] == '-')) {
371 /* Adjust last action modifier in result */
372 the_modes[len] = *mode_ptr;
374 /* Append modifier character to result string */
376 strlcat(the_modes, x, sizeof(the_modes));
378 set = *mode_ptr == '+';
383 /* Are there arguments left? */
384 if( arg_arg >= Req->argc ) arg_arg = -1;
391 /* --- Channel modes --- */
392 case 'i': /* Invite only */
393 case 'm': /* Moderated */
394 case 'n': /* Only members can write */
395 case 's': /* Secret channel */
396 case 't': /* Topic locked */
400 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
402 case 'k': /* Channel key */
407 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
410 if (arg_arg > mode_arg) {
412 Channel_ModeDel(Channel, 'k');
413 Channel_SetKey(Channel, Req->argv[arg_arg]);
414 strlcpy(argadd, Channel_Key(Channel), sizeof(argadd));
417 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
419 Req->argv[arg_arg][0] = '\0';
422 ok = IRC_WriteStrClient(Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID(Origin), Req->command);
425 case 'l': /* Member limit */
430 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
433 if (arg_arg > mode_arg) {
435 l = atol(Req->argv[arg_arg]);
436 if (l > 0 && l < 0xFFFF) {
437 Channel_ModeDel(Channel, 'l');
438 Channel_SetMaxUsers(Channel, l);
439 snprintf(argadd, sizeof(argadd), "%ld", l);
443 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
445 Req->argv[arg_arg][0] = '\0';
448 ok = IRC_WriteStrClient(Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID(Origin), Req->command);
451 case 'P': /* Persistent channel */
453 /* Only IRC operators are allowed to
454 * set the 'P' channel mode! */
455 if (set && !(Client_OperByMe(Client) || Client_Type(Client) == CLIENT_SERVER))
456 ok = IRC_WriteStrClient(Origin, ERR_NOPRIVILEGES_MSG, Client_ID(Origin));
460 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
462 /* --- Channel user modes --- */
463 case 'o': /* Channel operator */
464 case 'v': /* Voice */
465 if (arg_arg > mode_arg) {
467 client = Client_Search(Req->argv[arg_arg]);
471 ok = IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->argv[arg_arg]);
473 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
475 Req->argv[arg_arg][0] = '\0';
478 ok = IRC_WriteStrClient(Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID(Origin), Req->command);
481 /* --- Channel lists --- */
482 case 'I': /* Invite lists */
483 case 'b': /* Ban lists */
484 if (arg_arg > mode_arg) {
487 ok = set ? Add_Ban_Invite(*mode_ptr, Origin, Client, Channel, Req->argv[arg_arg])
488 : Del_Ban_Invite(*mode_ptr, Origin, Client, Channel, Req->argv[arg_arg]);
490 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
491 Client_ID(Origin), Channel_Name(Channel));
493 Req->argv[arg_arg][0] = '\0';
496 if (*mode_ptr == 'I')
497 Channel_ShowInvites(Origin, Channel);
499 Channel_ShowBans(Origin, Channel);
503 Log(LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?",
504 set ? '+' : '-', *mode_ptr, Client_ID(Origin), Channel_Name(Channel));
505 if (Client_Type(Client) != CLIENT_SERVER)
506 ok = IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID(Origin), set ? '+' : '-', *mode_ptr);
514 /* Is there a valid mode change? */
518 /* Validate target client */
519 if (client && (!Channel_IsMemberOf(Channel, client))) {
520 if (!IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
521 Client_ID(Origin), Client_ID(client), Channel_Name(Channel)))
528 /* Channel-User-Mode */
529 retval = set ? Channel_UserModeAdd(Channel, client, x[0]) : Channel_UserModeDel(Channel, client, x[0]);
531 strlcat(the_args, " ", sizeof(the_args));
532 strlcat(the_args, Client_ID(client), sizeof(the_args));
533 strlcat(the_modes, x, sizeof(the_modes));
534 Log(LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"",
535 Client_Mask(client), Channel_Name(Channel), Channel_UserModes(Channel, client));
539 retval = set ? Channel_ModeAdd(Channel, x[0]) : Channel_ModeDel(Channel, x[0]);
541 strlcat(the_modes, x, sizeof(the_modes));
542 Log(LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name(Channel), Channel_Modes(Channel));
546 /* Are there additional arguments to add? */
548 strlcat(the_args, " ", sizeof(the_args));
549 strlcat(the_args, argadd, sizeof(the_args));
553 /* Are there changed modes? */
555 /* Clean up mode string */
556 len = strlen(the_modes) - 1;
557 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
558 the_modes[len] = '\0';
560 if (Client_Type(Client) == CLIENT_SERVER) {
561 /* Forward mode changes to channel users and other servers */
562 IRC_WriteStrServersPrefix(Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args);
563 IRC_WriteStrChannelPrefix(Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name(Channel), the_modes, the_args);
566 Origin = Client_ThisServer();
567 /* Send reply to client and inform other servers and channel users */
568 ok = IRC_WriteStrClientPrefix(Client, Origin, "MODE %s %s%s",
569 Channel_Name(Channel), the_modes, the_args);
570 IRC_WriteStrServersPrefix(Client, Origin, "MODE %s %s%s",
571 Channel_Name(Channel), the_modes, the_args);
572 IRC_WriteStrChannelPrefix(Client, Channel, Origin, false, "MODE %s %s%s",
573 Channel_Name(Channel), the_modes, the_args);
577 IRC_SetPenalty(Client, 1);
583 IRC_AWAY( CLIENT *Client, REQUEST *Req )
585 assert( Client != NULL );
586 assert( Req != NULL );
588 /* Falsche Anzahl Parameter? */
589 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
591 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
594 Client_SetAway( Client, Req->argv[0] );
595 Client_ModeAdd( Client, 'a' );
596 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
597 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
602 Client_ModeDel( Client, 'a' );
603 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
604 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
610 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
616 assert( Client != NULL );
617 assert( Channel != NULL );
618 assert( Pattern != NULL );
619 assert(what == 'I' || what == 'b');
621 mask = Lists_MakeMask(Pattern);
623 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
626 ret = Channel_AddInvite(Channel, mask, false);
628 ret = Channel_AddBan(Channel, mask);
632 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
636 return Send_ListChange("+I", Prefix, Client, Channel, mask);
637 return Send_ListChange("+b", Prefix, Client, Channel, mask);
642 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
645 struct list_head *list;
647 assert( Client != NULL );
648 assert( Channel != NULL );
649 assert( Pattern != NULL );
650 assert(what == 'I' || what == 'b');
652 mask = Lists_MakeMask( Pattern );
655 list = Channel_GetListInvites(Channel);
657 list = Channel_GetListBans(Channel);
659 Lists_Del(list, mask);
661 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
662 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
667 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask )
669 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
673 if( Client_Type( Client ) == CLIENT_USER )
675 /* Bestaetigung an Client */
676 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
680 /* an andere Server */
681 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
683 /* und lokale User im Channel */
684 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
687 } /* Send_ListChange */