2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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 * Handle channel mode and channel-user mode changes
283 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
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;
293 /* Are modes allowed on channel? */
294 if (Channel_Name(Channel)[0] == '+')
295 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
296 Client_ID(Client), Channel_Name(Channel));
298 /* Mode request: let's answer it :-) */
300 return Channel_Mode_Answer_Request(Origin, Channel);
302 /* Is the user allowed to change modes? */
303 if (Client_Type(Client) == CLIENT_USER) {
304 /* Is the originating user on that channel? */
305 if (!Channel_IsMemberOf(Channel, Origin))
306 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
307 Client_ID(Origin), Channel_Name(Channel));
309 /* channel operator? */
310 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
312 else if (Conf_OperCanMode) {
313 /* IRC-Operators can use MODE as well */
314 if (Client_OperByMe(Origin)) {
316 if (Conf_OperServerMode)
317 use_servermode = true; /* Change Origin to Server */
323 mode_ptr = Req->argv[mode_arg];
324 if (Req->argc > mode_arg + 1)
325 arg_arg = mode_arg + 1;
329 /* Initial state: set or unset modes? */
343 /* Prepare reply string */
344 strcpy(the_modes, set ? "+" : "-");
353 /* Try next argument if there's any */
354 if (arg_arg > mode_arg)
359 if (mode_arg >= Req->argc)
361 mode_ptr = Req->argv[mode_arg];
363 if (Req->argc > mode_arg + 1)
364 arg_arg = mode_arg + 1;
373 if (((*mode_ptr == '+') && !set)
374 || ((*mode_ptr == '-') && set)) {
375 /* Action modifier ("+"/"-") must be changed ... */
376 len = strlen(the_modes) - 1;
377 if (the_modes[len] == '+' || the_modes[len] == '-') {
378 /* Adjust last action modifier in result */
379 the_modes[len] = *mode_ptr;
381 /* Append modifier character to result string */
383 strlcat(the_modes, x, sizeof(the_modes));
385 set = *mode_ptr == '+';
390 /* Are there arguments left? */
391 if (arg_arg >= Req->argc)
399 /* --- Channel modes --- */
400 case 'i': /* Invite only */
401 case 'm': /* Moderated */
402 case 'n': /* Only members can write */
403 case 's': /* Secret channel */
404 case 't': /* Topic locked */
408 ok = IRC_WriteStrClient(Origin,
409 ERR_CHANOPRIVSNEEDED_MSG,
410 Client_ID(Origin), Channel_Name(Channel));
412 case 'k': /* Channel key */
417 ok = IRC_WriteStrClient(Origin,
418 ERR_CHANOPRIVSNEEDED_MSG,
420 Channel_Name(Channel));
423 if (arg_arg > mode_arg) {
425 Channel_ModeDel(Channel, 'k');
426 Channel_SetKey(Channel,
428 strlcpy(argadd, Channel_Key(Channel),
432 ok = IRC_WriteStrClient(Origin,
433 ERR_CHANOPRIVSNEEDED_MSG,
435 Channel_Name(Channel));
437 Req->argv[arg_arg][0] = '\0';
440 ok = IRC_WriteStrClient(Origin,
441 ERR_NEEDMOREPARAMS_MSG,
442 Client_ID(Origin), Req->command);
446 case 'l': /* Member limit */
451 ok = IRC_WriteStrClient(Origin,
452 ERR_CHANOPRIVSNEEDED_MSG,
454 Channel_Name(Channel));
457 if (arg_arg > mode_arg) {
459 l = atol(Req->argv[arg_arg]);
460 if (l > 0 && l < 0xFFFF) {
461 Channel_ModeDel(Channel, 'l');
462 Channel_SetMaxUsers(Channel, l);
463 snprintf(argadd, sizeof(argadd),
468 ok = IRC_WriteStrClient(Origin,
469 ERR_CHANOPRIVSNEEDED_MSG,
471 Channel_Name(Channel));
473 Req->argv[arg_arg][0] = '\0';
476 ok = IRC_WriteStrClient(Origin,
477 ERR_NEEDMOREPARAMS_MSG,
478 Client_ID(Origin), Req->command);
482 case 'P': /* Persistent channel */
484 /* Only IRC operators are allowed to
485 * set the 'P' channel mode! */
486 if (set && !(Client_OperByMe(Client)
487 || Client_Type(Client) == CLIENT_SERVER))
488 ok = IRC_WriteStrClient(Origin,
489 ERR_NOPRIVILEGES_MSG,
494 ok = IRC_WriteStrClient(Origin,
495 ERR_CHANOPRIVSNEEDED_MSG,
497 Channel_Name(Channel));
499 /* --- Channel user modes --- */
500 case 'o': /* Channel operator */
501 case 'v': /* Voice */
502 if (arg_arg > mode_arg) {
504 client = Client_Search(Req->argv[arg_arg]);
508 ok = IRC_WriteStrClient(Client,
513 ok = IRC_WriteStrClient(Origin,
514 ERR_CHANOPRIVSNEEDED_MSG,
516 Channel_Name(Channel));
518 Req->argv[arg_arg][0] = '\0';
521 ok = IRC_WriteStrClient(Origin,
522 ERR_NEEDMOREPARAMS_MSG,
523 Client_ID(Origin), Req->command);
527 /* --- Channel lists --- */
528 case 'I': /* Invite lists */
529 case 'b': /* Ban lists */
530 if (arg_arg > mode_arg) {
534 ? Add_Ban_Invite(*mode_ptr, Origin,
537 : Del_Ban_Invite(*mode_ptr, Origin,
541 ok = IRC_WriteStrClient(Origin,
542 ERR_CHANOPRIVSNEEDED_MSG,
544 Channel_Name(Channel));
546 Req->argv[arg_arg][0] = '\0';
549 if (*mode_ptr == 'I')
550 Channel_ShowInvites(Origin, Channel);
552 Channel_ShowBans(Origin, Channel);
557 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
558 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
559 Channel_Name(Channel));
560 if (Client_Type(Client) != CLIENT_SERVER)
561 ok = IRC_WriteStrClient(Origin,
562 ERR_UMODEUNKNOWNFLAG2_MSG,
564 set ? '+' : '-', *mode_ptr);
572 /* Is there a valid mode change? */
576 /* Validate target client */
577 if (client && (!Channel_IsMemberOf(Channel, client))) {
578 if (!IRC_WriteStrClient
579 (Origin, ERR_USERNOTINCHANNEL_MSG,
580 Client_ID(Origin), Client_ID(client),
581 Channel_Name(Channel)))
588 /* Channel-User-Mode */
590 ? Channel_UserModeAdd(Channel, client, x[0])
591 : Channel_UserModeDel(Channel, client, x[0]);
593 strlcat(the_args, " ", sizeof(the_args));
594 strlcat(the_args, Client_ID(client),
596 strlcat(the_modes, x, sizeof(the_modes));
598 ("User \"%s\": Mode change on %s, now \"%s\"",
599 Client_Mask(client), Channel_Name(Channel),
600 Channel_UserModes(Channel, client));
605 ? Channel_ModeAdd(Channel, x[0])
606 : Channel_ModeDel(Channel, x[0]);
608 strlcat(the_modes, x, sizeof(the_modes));
609 LogDebug("Channel %s: Mode change, now \"%s\".",
610 Channel_Name(Channel),
611 Channel_Modes(Channel));
615 /* Are there additional arguments to add? */
617 strlcat(the_args, " ", sizeof(the_args));
618 strlcat(the_args, argadd, sizeof(the_args));
623 /* Are there changed modes? */
625 /* Clean up mode string */
626 len = strlen(the_modes) - 1;
627 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
628 the_modes[len] = '\0';
630 if (Client_Type(Client) == CLIENT_SERVER) {
631 /* MODE requests for local channels from other servers
632 * are definitely invalid! */
633 if (Channel_IsLocal(Channel)) {
634 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
638 /* Forward mode changes to channel users and all the
639 * other remote servers: */
640 IRC_WriteStrServersPrefix(Client, Origin,
641 "MODE %s %s%s", Channel_Name(Channel),
642 the_modes, the_args);
643 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
644 false, "MODE %s %s%s", Channel_Name(Channel),
645 the_modes, the_args);
648 Origin = Client_ThisServer();
649 /* Send reply to client and inform other servers and channel users */
650 ok = IRC_WriteStrClientPrefix(Client, Origin,
651 "MODE %s %s%s", Channel_Name(Channel),
652 the_modes, the_args);
653 /* Only forward requests for non-local channels */
654 if (!Channel_IsLocal(Channel))
655 IRC_WriteStrServersPrefix(Client, Origin,
656 "MODE %s %s%s", Channel_Name(Channel),
657 the_modes, the_args);
658 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
659 false, "MODE %s %s%s", Channel_Name(Channel),
660 the_modes, the_args);
664 IRC_SetPenalty(Client, 1);
670 IRC_AWAY( CLIENT *Client, REQUEST *Req )
672 assert( Client != NULL );
673 assert( Req != NULL );
675 /* Falsche Anzahl Parameter? */
676 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
678 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
681 Client_SetAway( Client, Req->argv[0] );
682 Client_ModeAdd( Client, 'a' );
683 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
684 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
689 Client_ModeDel( Client, 'a' );
690 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
691 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
697 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
703 assert( Client != NULL );
704 assert( Channel != NULL );
705 assert( Pattern != NULL );
706 assert(what == 'I' || what == 'b');
708 mask = Lists_MakeMask(Pattern);
710 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
713 ret = Channel_AddInvite(Channel, mask, false);
715 ret = Channel_AddBan(Channel, mask);
719 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
723 return Send_ListChange("+I", Prefix, Client, Channel, mask);
724 return Send_ListChange("+b", Prefix, Client, Channel, mask);
729 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
732 struct list_head *list;
734 assert( Client != NULL );
735 assert( Channel != NULL );
736 assert( Pattern != NULL );
737 assert(what == 'I' || what == 'b');
739 mask = Lists_MakeMask( Pattern );
742 list = Channel_GetListInvites(Channel);
744 list = Channel_GetListBans(Channel);
746 Lists_Del(list, mask);
748 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
749 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
754 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask )
756 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
760 if( Client_Type( Client ) == CLIENT_USER )
762 /* Bestaetigung an Client */
763 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
767 /* an andere Server */
768 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
770 /* und lokale User im Channel */
771 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
774 } /* Send_ListChange */