2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2012 Shawn Smith <shawn@inspircd.org>
5 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
8 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
9 * Copyright (C) 2004-2008 Craig Edwards <craigedwards@brainbox.cc>
10 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "builtinmodes.h"
29 ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type, Class mclass)
30 : ServiceProvider(Creator, Name, SERVICE_MODE), modeid(ModeParser::MODEID_MAX),
31 parameters_taken(Params), mode(modeletter), oper(false),
32 list(false), m_type(type), type_id(mclass), levelrequired(HALFOP_VALUE)
36 CullResult ModeHandler::cull()
39 ServerInstance->Modes->DelMode(this);
40 return classbase::cull();
43 ModeHandler::~ModeHandler()
47 int ModeHandler::GetNumParams(bool adding)
49 switch (parameters_taken)
54 return adding ? 1 : 0;
61 std::string ModeHandler::GetUserParameter(User* user)
66 ModResult ModeHandler::AccessCheck(User*, Channel*, std::string &, bool)
68 return MOD_RES_PASSTHRU;
71 ModeAction ModeHandler::OnModeChange(User*, User*, Channel*, std::string&, bool)
73 return MODEACTION_DENY;
76 void ModeHandler::DisplayList(User*, Channel*)
80 void ModeHandler::DisplayEmptyList(User*, Channel*)
84 void ModeHandler::OnParameterMissing(User* user, User* dest, Channel* channel)
88 bool ModeHandler::ResolveModeConflict(std::string& theirs, const std::string& ours, Channel*)
90 return (theirs < ours);
93 ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
95 /* We're either trying to add a mode we already have or
96 remove a mode we don't have, deny. */
97 if (dest->IsModeSet(this) == adding)
98 return MODEACTION_DENY;
100 /* adding will be either true or false, depending on if we
101 are adding or removing the mode, since we already checked
102 to make sure we aren't adding a mode we have or that we
103 aren't removing a mode we don't have, we don't have to do any
104 other checks here to see if it's true or false, just add or
106 dest->SetMode(this, adding);
108 return MODEACTION_ALLOW;
112 ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
114 /* We're either trying to add a mode we already have or
115 remove a mode we don't have, deny. */
116 if (channel->IsModeSet(this) == adding)
117 return MODEACTION_DENY;
119 /* adding will be either true or false, depending on if we
120 are adding or removing the mode, since we already checked
121 to make sure we aren't adding a mode we have or that we
122 aren't removing a mode we don't have, we don't have to do any
123 other checks here to see if it's true or false, just add or
125 channel->SetMode(this, adding);
127 return MODEACTION_ALLOW;
130 ModeWatcher::ModeWatcher(Module* Creator, const std::string& modename, ModeType type)
131 : mode(modename), m_type(type), creator(Creator)
133 ServerInstance->Modes->AddModeWatcher(this);
136 ModeWatcher::~ModeWatcher()
138 ServerInstance->Modes->DelModeWatcher(this);
141 ModeType ModeWatcher::GetModeType()
146 bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool)
151 void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool)
155 void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text)
159 /* Display channel's current mode string */
160 user->WriteNumeric(RPL_CHANNELMODEIS, "%s +%s", targetchannel->name.c_str(), targetchannel->ChanModes(targetchannel->HasUser(user)));
161 user->WriteNumeric(RPL_CHANNELCREATED, "%s %lu", targetchannel->name.c_str(), (unsigned long)targetchannel->age);
166 if (targetuser == user || user->HasPrivPermission("users/auspex"))
168 /* Display user's current mode string */
169 user->WriteNumeric(RPL_UMODEIS, ":+%s", targetuser->FormatModes());
170 if ((targetuser->IsOper()))
172 ModeHandler* snomask = FindMode('s', MODETYPE_USER);
173 user->WriteNumeric(RPL_SNOMASKIS, "%s :Server notice mask", snomask->GetUserParameter(user).c_str());
179 user->WriteNumeric(ERR_USERSDONTMATCH, ":Can't view modes for other users");
185 PrefixMode::PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank, char PrefixChar)
186 : ModeHandler(Creator, Name, ModeLetter, PARAM_ALWAYS, MODETYPE_CHANNEL, MC_PREFIX)
187 , prefix(PrefixChar), prefixrank(Rank)
192 ModeAction PrefixMode::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
195 if (IS_LOCAL(source))
196 target = ServerInstance->FindNickOnly(parameter);
198 target = ServerInstance->FindNick(parameter);
202 source->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameter.c_str());
203 return MODEACTION_DENY;
206 Membership* memb = chan->GetUser(target);
208 return MODEACTION_DENY;
210 parameter = target->nick;
211 return (memb->SetPrefix(this, adding) ? MODEACTION_ALLOW : MODEACTION_DENY);
214 ModeAction ParamModeBase::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
218 if (chan->GetModeParameter(this) == parameter)
219 return MODEACTION_DENY;
221 if (OnSet(source, chan, parameter) != MODEACTION_ALLOW)
222 return MODEACTION_DENY;
224 chan->SetMode(this, true);
226 // Handler might have changed the parameter internally
228 this->GetParameter(chan, parameter);
232 if (!chan->IsModeSet(this))
233 return MODEACTION_DENY;
234 this->OnUnsetInternal(source, chan);
235 chan->SetMode(this, false);
237 return MODEACTION_ALLOW;
240 ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Modes::Change& mcitem, bool SkipACL)
242 ModeType type = chan ? MODETYPE_CHANNEL : MODETYPE_USER;
244 ModeHandler* mh = mcitem.mh;
245 bool adding = mcitem.adding;
246 int pcnt = mh->GetNumParams(adding);
248 std::string& parameter = mcitem.param;
249 // crop mode parameter size to 250 characters
250 if (parameter.length() > 250 && adding)
251 parameter = parameter.substr(0, 250);
253 ModResult MOD_RESULT;
254 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, parameter, adding));
256 if (IS_LOCAL(user) && (MOD_RESULT == MOD_RES_DENY))
257 return MODEACTION_DENY;
259 const char modechar = mh->GetModeChar();
261 if (chan && !SkipACL && (MOD_RESULT != MOD_RES_ALLOW))
263 MOD_RESULT = mh->AccessCheck(user, chan, parameter, adding);
265 if (MOD_RESULT == MOD_RES_DENY)
266 return MODEACTION_DENY;
267 if (MOD_RESULT == MOD_RES_PASSTHRU)
269 unsigned int neededrank = mh->GetLevelRequired();
270 /* Compare our rank on the channel against the rank of the required prefix,
271 * allow if >= ours. Because mIRC and xchat throw a tizz if the modes shown
272 * in NAMES(X) are not in rank order, we know the most powerful mode is listed
273 * first, so we don't need to iterate, we just look up the first instead.
275 unsigned int ourrank = chan->GetPrefixValue(user);
276 if (ourrank < neededrank)
278 PrefixMode* neededmh = NULL;
279 for(char c='A'; c <= 'z'; c++)
281 PrefixMode* privmh = FindPrefixMode(c);
282 if (privmh && privmh->GetPrefixRank() >= neededrank)
284 // this mode is sufficient to allow this action
285 if (!neededmh || privmh->GetPrefixRank() < neededmh->GetPrefixRank())
290 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You must have channel %s access or above to %sset channel mode %c",
291 chan->name.c_str(), neededmh->name.c_str(), adding ? "" : "un", modechar);
293 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You cannot %sset channel mode %c",
294 chan->name.c_str(), adding ? "" : "un", modechar);
295 return MODEACTION_DENY;
300 // Ask mode watchers whether this mode change is OK
301 std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mh->name);
302 for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
304 ModeWatcher* mw = i->second;
305 if (mw->GetModeType() == type)
307 if (!mw->BeforeMode(user, targetuser, chan, parameter, adding))
308 return MODEACTION_DENY;
310 // A module whacked the parameter completely, and there was one. Abort.
311 if (pcnt && parameter.empty())
312 return MODEACTION_DENY;
316 if (IS_LOCAL(user) && !user->IsOper())
318 char* disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes;
319 if (disabled[modechar - 'A'])
321 user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - %s mode %c has been locked by the administrator",
322 type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
323 return MODEACTION_DENY;
327 if (adding && IS_LOCAL(user) && mh->NeedsOper() && !user->HasModePermission(modechar, type))
329 /* It's an oper only mode, and they don't have access to it. */
332 user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - Oper type %s does not have access to set %s mode %c",
333 user->oper->name.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
337 user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - Only operators may set %s mode %c",
338 type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
340 return MODEACTION_DENY;
343 /* Call the handler for the mode */
344 ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding);
346 if (pcnt && parameter.empty())
347 return MODEACTION_DENY;
349 if (ma != MODEACTION_ALLOW)
352 itpair = modewatchermap.equal_range(mh->name);
353 for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
355 ModeWatcher* mw = i->second;
356 if (mw->GetModeType() == type)
357 mw->AfterMode(user, targetuser, chan, parameter, adding);
360 return MODEACTION_ALLOW;
363 void ModeParser::Process(const std::vector<std::string>& parameters, User* user, ModeProcessFlag flags)
365 const std::string& target = parameters[0];
366 Channel* targetchannel = ServerInstance->FindChan(target);
367 User* targetuser = NULL;
371 targetuser = ServerInstance->FindNickOnly(target);
373 targetuser = ServerInstance->FindNick(target);
375 ModeType type = targetchannel ? MODETYPE_CHANNEL : MODETYPE_USER;
378 LastChangeList.clear();
380 if ((!targetchannel) && ((!targetuser) || (IS_SERVER(targetuser))))
382 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", target.c_str());
385 if (parameters.size() == 1)
387 this->DisplayCurrentModes(user, targetuser, targetchannel, target.c_str());
391 // Populate a temporary Modes::ChangeList with the parameters
392 Modes::ChangeList changelist;
393 ModeParamsToChangeList(user, type, parameters, changelist);
395 ModResult MOD_RESULT;
396 FIRST_MOD_RESULT(OnPreMode, MOD_RESULT, (user, targetuser, targetchannel, parameters));
400 if (MOD_RESULT == MOD_RES_PASSTHRU)
402 if ((targetuser) && (user != targetuser))
404 // Local users may only change the modes of other users if a module explicitly allows it
405 user->WriteNumeric(ERR_USERSDONTMATCH, ":Can't change mode for other users");
409 // This is a mode change by a local user and modules didn't explicitly allow/deny.
410 // Ensure access checks will happen for each mode being changed.
411 flags |= MODE_CHECKACCESS;
413 else if (MOD_RESULT == MOD_RES_DENY)
414 return; // Entire mode change denied by a module
417 ProcessSingle(user, targetchannel, targetuser, changelist, flags);
419 if ((LastParse.empty()) && (targetchannel) && (parameters.size() == 2))
421 /* Special case for displaying the list for listmodes,
422 * e.g. MODE #chan b, or MODE #chan +b without a parameter
424 this->DisplayListModes(user, targetchannel, parameters[1]);
428 void ModeParser::ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist)
430 const std::string& mode_sequence = parameters[1];
433 unsigned int param_at = 2;
435 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
437 unsigned char modechar = *letter;
438 if (modechar == '+' || modechar == '-')
440 adding = (modechar == '+');
444 ModeHandler *mh = this->FindMode(modechar, type);
447 /* No mode handler? Unknown mode character then. */
448 user->WriteNumeric(type == MODETYPE_CHANNEL ? ERR_UNKNOWNMODE : ERR_UNKNOWNSNOMASK, "%c :is unknown mode char to me", modechar);
452 std::string parameter;
453 if (mh->GetNumParams(adding) && param_at < parameters.size())
454 parameter = parameters[param_at++];
456 changelist.push(mh, adding, parameter);
460 static bool IsModeParamValid(User* user, Channel* targetchannel, User* targetuser, const Modes::Change& item)
462 // An empty parameter is never acceptable
463 if (item.param.empty())
465 item.mh->OnParameterMissing(user, targetuser, targetchannel);
469 // The parameter cannot begin with a ':' character or contain a space
470 if ((item.param[0] == ':') || (item.param.find(' ') != std::string::npos))
476 // Returns true if we should apply a merged mode, false if we should skip it
477 static bool ShouldApplyMergedMode(Channel* chan, Modes::Change& item)
479 ModeHandler* mh = item.mh;
480 if ((!chan) || (!chan->IsModeSet(mh)) || (mh->IsListMode()))
481 // Mode not set here or merge is not applicable, apply the incoming mode
484 // Mode handler decides
485 std::string ours = chan->GetModeParameter(mh);
486 return mh->ResolveModeConflict(item.param, ours, chan);
489 void ModeParser::Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags)
491 // Call ProcessSingle until the entire list is processed, but at least once to ensure
492 // LastParse and LastChangeList are cleared
493 unsigned int processed = 0;
496 unsigned int n = ProcessSingle(user, targetchannel, targetuser, changelist, flags, processed);
499 while (processed < changelist.size());
502 unsigned int ModeParser::ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags, unsigned int beginindex)
505 LastChangeList.clear();
507 unsigned int modes_processed = 0;
508 std::string output_mode;
509 std::string output_parameters;
511 char output_pm = '\0'; // current output state, '+' or '-'
512 Modes::ChangeList::List& list = changelist.getlist();
513 for (Modes::ChangeList::List::iterator i = list.begin()+beginindex; i != list.end(); ++i)
517 Modes::Change& item = *i;
518 ModeHandler* mh = item.mh;
520 // If the mode is supposed to have a parameter then we first take a look at item.param
521 // and, if we were asked to, also handle mode merges now
522 if (mh->GetNumParams(item.adding))
524 // Skip the mode if the parameter does not pass basic validation
525 if (!IsModeParamValid(user, targetchannel, targetuser, item))
528 // If this is a merge and we won we don't apply this mode
529 if ((flags & MODE_MERGE) && (!ShouldApplyMergedMode(targetchannel, item)))
533 ModeAction ma = TryMode(user, targetuser, targetchannel, item, (!(flags & MODE_CHECKACCESS)));
535 if (ma != MODEACTION_ALLOW)
538 char needed_pm = item.adding ? '+' : '-';
539 if (needed_pm != output_pm)
541 output_pm = needed_pm;
542 output_mode.append(1, output_pm);
544 output_mode.push_back(mh->GetModeChar());
546 if (!item.param.empty())
548 output_parameters.push_back(' ');
549 output_parameters.append(item.param);
551 LastChangeList.push(mh, item.adding, item.param);
553 if ((output_mode.length() + output_parameters.length() > 450)
554 || (output_mode.length() > 100)
555 || (LastChangeList.size() >= ServerInstance->Config->Limits.MaxModes))
557 /* mode sequence is getting too long */
562 if (!output_mode.empty())
564 LastParse = targetchannel ? targetchannel->name : targetuser->nick;
565 LastParse.append(" ");
566 LastParse.append(output_mode);
567 LastParse.append(output_parameters);
570 targetchannel->WriteChannel(user, "MODE " + LastParse);
572 targetuser->WriteFrom(user, "MODE " + LastParse);
574 FOREACH_MOD(OnMode, (user, targetuser, targetchannel, LastChangeList, flags, output_mode));
577 return modes_processed;
580 void ModeParser::DisplayListModes(User* user, Channel* chan, const std::string& mode_sequence)
584 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
586 unsigned char mletter = *letter;
590 /* Ensure the user doesnt request the same mode twice,
591 * so they cant flood themselves off out of idiocy.
593 if (sent[mletter] == seq)
598 ModeHandler *mh = this->FindMode(mletter, MODETYPE_CHANNEL);
600 if (!mh || !mh->IsListMode())
603 ModResult MOD_RESULT;
604 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, "", true));
605 if (MOD_RESULT == MOD_RES_DENY)
609 if (!user->HasPrivPermission("channels/auspex") && ServerInstance->Config->HideModeLists[mletter] && (chan->GetPrefixValue(user) < HALFOP_VALUE))
611 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You do not have access to view the +%c list",
612 chan->name.c_str(), mletter);
616 // Ask mode watchers whether it's OK to show the list
617 std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mh->name);
618 for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
620 ModeWatcher* mw = i->second;
621 if (mw->GetModeType() == MODETYPE_CHANNEL)
623 std::string dummyparam;
625 if (!mw->BeforeMode(user, NULL, chan, dummyparam, true))
627 // A mode watcher doesn't want us to show the list
635 mh->DisplayList(user, chan);
637 mh->DisplayEmptyList(user, chan);
641 void ModeParser::CleanMask(std::string &mask)
643 std::string::size_type pos_of_pling = mask.find_first_of('!');
644 std::string::size_type pos_of_at = mask.find_first_of('@');
645 std::string::size_type pos_of_dot = mask.find_first_of('.');
646 std::string::size_type pos_of_colons = mask.find("::"); /* Because ipv6 addresses are colon delimited -- double so it treats extban as nick */
648 if (mask.length() >= 2 && mask[1] == ':')
649 return; // if it's an extban, don't even try guess how it needs to be formed.
651 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
653 /* Just a nick, or just a host - or clearly ipv6 (starting with :) */
654 if ((pos_of_dot == std::string::npos) && (pos_of_colons == std::string::npos) && mask[0] != ':')
656 /* It has no '.' in it, it must be a nick. */
661 /* Got a dot in it? Has to be a host */
662 mask = "*!*@" + mask;
665 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
667 /* Has an @ but no !, its a user@host */
670 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
672 /* Has a ! but no @, it must be a nick!ident */
677 ModeHandler::Id ModeParser::AllocateModeId(ModeType mt)
679 for (ModeHandler::Id i = 0; i != MODEID_MAX; ++i)
681 if (!modehandlersbyid[mt][i])
685 throw ModuleException("Out of ModeIds");
688 void ModeParser::AddMode(ModeHandler* mh)
690 /* Yes, i know, this might let people declare modes like '_' or '^'.
691 * If they do that, thats their problem, and if i ever EVER see an
692 * official InspIRCd developer do that, i'll beat them with a paddle!
694 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
695 throw ModuleException("Invalid letter for mode " + mh->name);
697 /* A mode prefix of ',' is not acceptable, it would fuck up server to server.
698 * A mode prefix of ':' will fuck up both server to server, and client to server.
699 * A mode prefix of '#' will mess up /whois and /privmsg
701 PrefixMode* pm = mh->IsPrefixMode();
704 if ((pm->GetPrefix() > 126) || (pm->GetPrefix() == ',') || (pm->GetPrefix() == ':') || (pm->GetPrefix() == '#'))
705 throw ModuleException("Invalid prefix for mode " + mh->name);
707 if (FindPrefix(pm->GetPrefix()))
708 throw ModuleException("Prefix already exists for mode " + mh->name);
711 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
713 throw ModuleException("Letter is already in use for mode " + mh->name);
715 // The mode needs an id if it is either a user mode, a simple mode (flag) or a parameter mode.
716 // Otherwise (for listmodes and prefix modes) the id remains MODEID_MAX, which is invalid.
717 ModeHandler::Id modeid = MODEID_MAX;
718 if ((mh->GetModeType() == MODETYPE_USER) || (mh->IsParameterMode()) || (!mh->IsListMode()))
719 modeid = AllocateModeId(mh->GetModeType());
721 if (!modehandlersbyname[mh->GetModeType()].insert(std::make_pair(mh->name, mh)).second)
722 throw ModuleException("Mode name already in use: " + mh->name);
724 // Everything is fine, add the mode
726 // If we allocated an id for this mode then save it and put the mode handler into the slot
727 if (modeid != MODEID_MAX)
730 modehandlersbyid[mh->GetModeType()][modeid] = mh;
735 mhlist.prefix.push_back(pm);
736 else if (mh->IsListModeBase())
737 mhlist.list.push_back(mh->IsListModeBase());
739 RecreateModeListFor004Numeric();
742 bool ModeParser::DelMode(ModeHandler* mh)
744 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
747 ModeHandlerMap& mhmap = modehandlersbyname[mh->GetModeType()];
748 ModeHandlerMap::iterator mhmapit = mhmap.find(mh->name);
749 if ((mhmapit == mhmap.end()) || (mhmapit->second != mh))
752 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
756 /* Note: We can't stack here, as we have modes potentially being removed across many different channels.
757 * To stack here we have to make the algorithm slower. Discuss.
759 switch (mh->GetModeType())
763 const user_hash& users = ServerInstance->Users->GetUsers();
764 for (user_hash::const_iterator i = users.begin(); i != users.end(); )
766 User* user = i->second;
768 mh->RemoveMode(user);
772 case MODETYPE_CHANNEL:
774 const chan_hash& chans = ServerInstance->GetChans();
775 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); )
777 // The channel may not be in the hash after RemoveMode(), see m_permchannels
778 Channel* chan = i->second;
781 Modes::ChangeList changelist;
782 mh->RemoveMode(chan, changelist);
783 this->Process(ServerInstance->FakeClient, chan, NULL, changelist, MODE_LOCALONLY);
789 mhmap.erase(mhmapit);
790 if (mh->GetId() != MODEID_MAX)
791 modehandlersbyid[mh->GetModeType()][mh->GetId()] = NULL;
793 if (mh->IsPrefixMode())
794 mhlist.prefix.erase(std::find(mhlist.prefix.begin(), mhlist.prefix.end(), mh->IsPrefixMode()));
795 else if (mh->IsListModeBase())
796 mhlist.list.erase(std::find(mhlist.list.begin(), mhlist.list.end(), mh->IsListModeBase()));
798 RecreateModeListFor004Numeric();
802 ModeHandler* ModeParser::FindMode(const std::string& modename, ModeType mt)
804 ModeHandlerMap& mhmap = modehandlersbyname[mt];
805 ModeHandlerMap::const_iterator it = mhmap.find(modename);
806 if (it != mhmap.end())
812 ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt)
814 if ((modeletter < 'A') || (modeletter > 'z'))
817 return modehandlers[mt][modeletter-65];
820 PrefixMode* ModeParser::FindPrefixMode(unsigned char modeletter)
822 ModeHandler* mh = FindMode(modeletter, MODETYPE_CHANNEL);
825 return mh->IsPrefixMode();
828 std::string ModeParser::CreateModeList(ModeType mt, bool needparam)
832 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
834 ModeHandler* mh = modehandlers[mt][mode-65];
835 if ((mh) && ((!needparam) || (mh->GetNumParams(true))))
836 modestr.push_back(mode);
842 void ModeParser::RecreateModeListFor004Numeric()
844 Cached004ModeList = CreateModeList(MODETYPE_USER) + " " + CreateModeList(MODETYPE_CHANNEL) + " " + CreateModeList(MODETYPE_CHANNEL, true);
847 PrefixMode* ModeParser::FindPrefix(unsigned const char pfxletter)
849 const PrefixModeList& list = GetPrefixModes();
850 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
853 if (pm->GetPrefix() == pfxletter)
859 std::string ModeParser::GiveModeList(ModeType mt)
861 std::string type1; /* Listmodes EXCEPT those with a prefix */
862 std::string type2; /* Modes that take a param when adding or removing */
863 std::string type3; /* Modes that only take a param when adding */
864 std::string type4; /* Modes that dont take a param */
866 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
868 ModeHandler* mh = modehandlers[mt][mode-65];
869 /* One parameter when adding */
872 if (mh->GetNumParams(true))
874 PrefixMode* pm = mh->IsPrefixMode();
875 if ((mh->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0)))
877 type1 += mh->GetModeChar();
881 /* ... and one parameter when removing */
882 if (mh->GetNumParams(false))
884 /* But not a list mode */
887 type2 += mh->GetModeChar();
892 /* No parameters when removing */
893 type3 += mh->GetModeChar();
899 type4 += mh->GetModeChar();
904 return type1 + "," + type2 + "," + type3 + "," + type4;
907 std::string ModeParser::BuildPrefixes(bool lettersAndModes)
909 std::string mletters;
910 std::string mprefixes;
911 std::map<int,std::pair<char,char> > prefixes;
913 const PrefixModeList& list = GetPrefixModes();
914 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
918 prefixes[pm->GetPrefixRank()] = std::make_pair(pm->GetPrefix(), pm->GetModeChar());
921 for(std::map<int,std::pair<char,char> >::reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); n++)
923 mletters = mletters + n->second.first;
924 mprefixes = mprefixes + n->second.second;
927 return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters;
930 void ModeParser::AddModeWatcher(ModeWatcher* mw)
932 modewatchermap.insert(std::make_pair(mw->GetModeName(), mw));
935 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
937 std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mw->GetModeName());
938 for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
942 modewatchermap.erase(i);
950 void ModeHandler::RemoveMode(User* user)
952 // Remove the mode if it's set on the user
953 if (user->IsModeSet(this->GetModeChar()))
955 Modes::ChangeList changelist;
956 changelist.push_remove(this);
957 ServerInstance->Modes->Process(ServerInstance->FakeClient, NULL, user, changelist, ModeParser::MODE_LOCALONLY);
961 void ModeHandler::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
963 if (channel->IsModeSet(this))
965 if (this->GetNumParams(false))
966 // Removing this mode requires a parameter
967 changelist.push_remove(this, channel->GetModeParameter(this));
969 changelist.push_remove(this);
973 void PrefixMode::RemoveMode(Channel* chan, Modes::ChangeList& changelist)
975 const Channel::MemberMap& userlist = chan->GetUsers();
976 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
978 if (i->second->hasMode(this->GetModeChar()))
979 changelist.push_remove(this, i->first->nick);
985 SimpleChannelModeHandler s;
986 SimpleChannelModeHandler p;
987 SimpleChannelModeHandler m;
988 SimpleChannelModeHandler t;
990 SimpleChannelModeHandler n;
991 SimpleChannelModeHandler i;
999 SimpleUserModeHandler ui;
1000 ModeUserOperator uo;
1001 ModeUserServerNoticeMask us;
1004 : s(NULL, "secret", 's')
1005 , p(NULL, "private", 'p')
1006 , m(NULL, "moderated", 'm')
1007 , t(NULL, "topiclock", 't')
1008 , n(NULL, "noextmsg", 'n')
1009 , i(NULL, "inviteonly", 'i')
1010 , ui(NULL, "invisible", 'i')
1016 ServiceProvider* modes[] = { &s, &p, &m, &t, &n, &i, &k, &l, &b, &o, &v,
1018 ServerInstance->Modules->AddServices(modes, sizeof(modes)/sizeof(ServiceProvider*));
1022 static builtin_modes static_modes;
1024 void ModeParser::InitBuiltinModes()
1026 static_modes.init();
1027 static_modes.b.DoRehash();
1030 ModeParser::ModeParser()
1032 /* Clear mode handler list */
1033 memset(modehandlers, 0, sizeof(modehandlers));
1034 memset(modehandlersbyid, 0, sizeof(modehandlersbyid));
1037 memset(&sent, 0, sizeof(sent));
1040 ModeParser::~ModeParser()