1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "configreader.h"
24 #include "connection.h"
28 #include "inspstring.h"
29 #include "helperfuncs.h"
34 #include "modes/cmode_s.h"
36 #include "modes/cmode_p.h"
38 #include "modes/cmode_b.h"
40 #include "modes/cmode_m.h"
41 /* +t (only (half) ops can change topic) */
42 #include "modes/cmode_t.h"
43 /* +n (no external messages) */
44 #include "modes/cmode_n.h"
45 /* +i (invite only) */
46 #include "modes/cmode_i.h"
47 /* +k (keyed channel) */
48 #include "modes/cmode_k.h"
49 /* +l (channel user limit) */
50 #include "modes/cmode_l.h"
53 extern std::vector<Module*> modules;
54 extern std::vector<ircd_module*> factory;
55 extern InspIRCd* ServerInstance;
56 extern ServerConfig* Config;
60 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
61 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
65 ModeHandler::~ModeHandler()
69 bool ModeHandler::IsListMode()
74 ModeType ModeHandler::GetModeType()
79 bool ModeHandler::NeedsOper()
84 int ModeHandler::GetNumParams(bool adding)
86 return adding ? n_params_on : n_params_off;
89 char ModeHandler::GetModeChar()
94 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
96 return MODEACTION_DENY;
99 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
103 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
105 return (ours < theirs);
108 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
112 ModeWatcher::~ModeWatcher()
116 char ModeWatcher::GetModeChar()
121 ModeType ModeWatcher::GetModeType()
126 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
131 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
135 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
138 if ((!user) || (!dest) || (!chan) || (!*dest))
145 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
151 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
156 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
158 if (((ucrec*)(*i))->channel == chan)
160 if (((ucrec*)(*i))->uc_modes & MASK)
164 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
168 ((ucrec*)(*i))->channel->AddOppedUser(d);
171 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
174 ((ucrec*)(*i))->channel->AddVoicedUser(d);
177 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
184 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
189 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
191 if (((ucrec*)(*i))->channel == chan)
193 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
197 ((ucrec*)(*i))->uc_modes ^= MASK;
201 ((ucrec*)(*i))->channel->DelOppedUser(d);
204 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
207 ((ucrec*)(*i))->channel->DelVoicedUser(d);
210 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
217 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
219 userrec *d = this->SanityChecks(user,dest,chan,status);
226 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
228 if (MOD_RESULT == ACR_DENY)
230 if (MOD_RESULT == ACR_DEFAULT)
232 if ((status < STATUS_OP) && (!is_uline(user->server)))
234 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
240 return this->Grant(d,chan,UCMODE_OP);
245 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
247 userrec *d = this->SanityChecks(user,dest,chan,status);
254 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
256 if (MOD_RESULT == ACR_DENY)
258 if (MOD_RESULT == ACR_DEFAULT)
260 if ((status < STATUS_OP) && (!is_uline(user->server)))
262 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
268 return this->Grant(d,chan,UCMODE_HOP);
273 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
275 userrec *d = this->SanityChecks(user,dest,chan,status);
282 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
284 if (MOD_RESULT == ACR_DENY)
286 if (MOD_RESULT == ACR_DEFAULT)
288 if ((status < STATUS_HOP) && (!is_uline(user->server)))
290 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
296 return this->Grant(d,chan,UCMODE_VOICE);
301 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
303 userrec *d = this->SanityChecks(user,dest,chan,status);
310 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
312 if (MOD_RESULT == ACR_DENY)
314 if (MOD_RESULT == ACR_DEFAULT)
316 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
318 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
324 return this->Revoke(d,chan,UCMODE_OP);
329 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
331 userrec *d = this->SanityChecks(user,dest,chan,status);
338 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
340 if (MOD_RESULT == ACR_DENY)
342 if (MOD_RESULT == ACR_DEFAULT)
344 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
345 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
347 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
353 return this->Revoke(d,chan,UCMODE_HOP);
358 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
360 userrec *d = this->SanityChecks(user,dest,chan,status);
367 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
369 if (MOD_RESULT == ACR_DENY)
371 if (MOD_RESULT == ACR_DEFAULT)
373 if ((status < STATUS_HOP) && (!is_uline(user->server)))
375 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
381 return this->Revoke(d,chan,UCMODE_VOICE);
386 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
388 std::string target = parameters[0];
389 ModeType type = MODETYPE_USER;
390 unsigned char mask = 0;
391 chanrec* targetchannel = FindChan(parameters[0]);
392 userrec* targetuser = Find(parameters[0]);
394 log(DEBUG,"ModeParser::Process start");
400 log(DEBUG,"Target type is CHANNEL");
401 type = MODETYPE_CHANNEL;
406 log(DEBUG,"Target type is USER");
407 type = MODETYPE_USER;
412 /* No such nick/channel */
413 log(DEBUG,"Target type is UNKNOWN, bailing");
416 std::string mode_sequence = parameters[1];
417 std::string parameter = "";
418 std::ostringstream parameter_list;
419 std::string output_sequence = "";
420 bool adding = true, state_change = false;
422 int parameter_counter = 2; /* Index of first parameter */
424 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
426 unsigned char modechar = *letter;
431 log(DEBUG,"Iterate mode letter %c",modechar);
434 * For + and - mode characters, we don't just stick the character into the output sequence.
435 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
436 * appearing in the output sequence, we store a flag which says there was a state change,
437 * which is set on any + or -, however, the + or - that we finish on is only appended to
438 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
441 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
442 * however, will allow the + if it is the first item in the sequence, regardless.
444 if ((!adding) || (!output_sequence.length()))
450 if ((adding) || (!output_sequence.length()))
458 * Watch carefully for the sleight of hand trick.
459 * 65 is the ascii value of 'A'. We take this from
460 * the char we're looking at to get a number between
461 * 1 and 127. We then logic-or it to get the hashed
462 * position, dependent on wether its a channel or
463 * a user mode. This is a little stranger, but a lot
464 * faster, than using a map of pairs.
466 handler_id = (modechar - 65) | mask;
468 if (modehandlers[handler_id])
472 log(DEBUG,"Found a ModeHandler* for mode %c",modechar);
474 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
476 log(DEBUG,"Call a ModeWatcher*");
477 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
480 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
482 log(DEBUG,"Modetype match, calling handler");
484 if (modehandlers[handler_id]->GetNumParams(adding))
486 log(DEBUG,"ModeHandler* for this mode says it has parameters. pcnt=%d parameter_counter=%d",pcnt,parameter_counter);
488 if (parameter_counter < pcnt)
490 parameter = parameters[parameter_counter++];
494 /* No parameter, continue to the next mode */
498 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
500 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
502 /* The handler nuked the parameter and they are supposed to have one.
503 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
504 * so we bail to the next mode character.
509 if (ma == MODEACTION_ALLOW)
511 log(DEBUG,"ModeAction was allow");
513 /* We're about to output a valid mode letter - was there previously a pending state-change? */
516 log(DEBUG,"Appending state change");
517 output_sequence.append(adding ? "+" : "-");
520 /* Add the mode letter */
521 output_sequence.push_back(modechar);
522 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
524 /* Is there a valid parameter for this mode? If so add it to the parameter list */
525 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
527 log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
528 parameter_list << " " << parameter;
531 /* Call all the AfterMode events in the mode watchers for this mode */
532 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
534 log(DEBUG,"Called a ModeWatcher* after event");
535 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
538 /* Reset the state change flag */
539 state_change = false;
546 /* Was there at least one valid mode in the sequence? */
547 if (output_sequence != "")
551 if (type == MODETYPE_CHANNEL)
553 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
558 if (type == MODETYPE_CHANNEL)
560 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
561 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
562 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
570 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
575 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
580 void ModeParser::CleanMask(std::string &mask)
582 std::string::size_type pos_of_pling = mask.find_first_of('!');
583 std::string::size_type pos_of_at = mask.find_first_of('@');
584 std::string::size_type pos_of_dot = mask.find_first_of('.');
585 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
587 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
589 /* Just a nick, or just a host */
590 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
592 /* It has no '.' in it, it must be a nick. */
597 /* Got a dot in it? Has to be a host */
598 mask = "*!*@" + mask;
601 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
603 /* Has an @ but no !, its a user@host */
606 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
608 /* Has a ! but no @, it must be a nick!ident */
613 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
615 unsigned char mask = 0;
616 unsigned char pos = 0;
618 /* Yes, i know, this might let people declare modes like '_' or '^'.
619 * If they do that, thats their problem, and if i ever EVER see an
620 * official InspIRCd developer do that, i'll beat them with a paddle!
622 if ((modeletter < 'A') || (modeletter > 'z'))
625 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
626 pos = (modeletter-65) | mask;
628 if (modehandlers[pos])
631 modehandlers[pos] = mh;
632 log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
636 ModeParser::ModeParser()
638 /* Clear mode list */
639 memset(modehandlers, 0, sizeof(modehandlers));
640 memset(modewatchers, 0, sizeof(modewatchers));
642 /* Initialise the RFC mode letters */
643 this->AddMode(new ModeChannelSecret, 's');
644 this->AddMode(new ModeChannelPrivate, 'p');
645 this->AddMode(new ModeChannelBan, 'b');
646 this->AddMode(new ModeChannelModerated, 'm');
647 this->AddMode(new ModeChannelTopicOps, 't');
648 this->AddMode(new ModeChannelNoExternal, 'n');
649 this->AddMode(new ModeChannelInviteOnly, 'i');
650 this->AddMode(new ModeChannelKey, 'k');
651 this->AddMode(new ModeChannelLimit, 'l');
652 /* TODO: Modes +o, +v, +h */