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 void ModeHandler::RegisterService()
95 ServerInstance->Modes.AddMode(this);
96 ServerInstance->Modules.AddReferent((GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + name, this);
99 ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
101 /* We're either trying to add a mode we already have or
102 remove a mode we don't have, deny. */
103 if (dest->IsModeSet(this) == adding)
104 return MODEACTION_DENY;
106 /* adding will be either true or false, depending on if we
107 are adding or removing the mode, since we already checked
108 to make sure we aren't adding a mode we have or that we
109 aren't removing a mode we don't have, we don't have to do any
110 other checks here to see if it's true or false, just add or
112 dest->SetMode(this, adding);
114 return MODEACTION_ALLOW;
118 ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
120 /* We're either trying to add a mode we already have or
121 remove a mode we don't have, deny. */
122 if (channel->IsModeSet(this) == adding)
123 return MODEACTION_DENY;
125 /* adding will be either true or false, depending on if we
126 are adding or removing the mode, since we already checked
127 to make sure we aren't adding a mode we have or that we
128 aren't removing a mode we don't have, we don't have to do any
129 other checks here to see if it's true or false, just add or
131 channel->SetMode(this, adding);
133 return MODEACTION_ALLOW;
136 ModeWatcher::ModeWatcher(Module* Creator, const std::string& modename, ModeType type)
137 : mode(modename), m_type(type), creator(Creator)
139 ServerInstance->Modes->AddModeWatcher(this);
142 ModeWatcher::~ModeWatcher()
144 ServerInstance->Modes->DelModeWatcher(this);
147 ModeType ModeWatcher::GetModeType()
152 bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool)
157 void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool)
161 PrefixMode::PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank, char PrefixChar)
162 : ModeHandler(Creator, Name, ModeLetter, PARAM_ALWAYS, MODETYPE_CHANNEL, MC_PREFIX)
163 , prefix(PrefixChar), prefixrank(Rank)
168 ModeAction PrefixMode::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
171 if (IS_LOCAL(source))
172 target = ServerInstance->FindNickOnly(parameter);
174 target = ServerInstance->FindNick(parameter);
178 source->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameter.c_str());
179 return MODEACTION_DENY;
182 Membership* memb = chan->GetUser(target);
184 return MODEACTION_DENY;
186 parameter = target->nick;
187 return (memb->SetPrefix(this, adding) ? MODEACTION_ALLOW : MODEACTION_DENY);
190 ModeAction ParamModeBase::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
194 if (chan->GetModeParameter(this) == parameter)
195 return MODEACTION_DENY;
197 if (OnSet(source, chan, parameter) != MODEACTION_ALLOW)
198 return MODEACTION_DENY;
200 chan->SetMode(this, true);
202 // Handler might have changed the parameter internally
204 this->GetParameter(chan, parameter);
208 if (!chan->IsModeSet(this))
209 return MODEACTION_DENY;
210 this->OnUnsetInternal(source, chan);
211 chan->SetMode(this, false);
213 return MODEACTION_ALLOW;
216 ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Modes::Change& mcitem, bool SkipACL)
218 ModeType type = chan ? MODETYPE_CHANNEL : MODETYPE_USER;
220 ModeHandler* mh = mcitem.mh;
221 bool adding = mcitem.adding;
222 int pcnt = mh->GetNumParams(adding);
224 std::string& parameter = mcitem.param;
225 // crop mode parameter size to 250 characters
226 if (parameter.length() > 250 && adding)
227 parameter.erase(250);
229 ModResult MOD_RESULT;
230 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, parameter, adding));
232 if (IS_LOCAL(user) && (MOD_RESULT == MOD_RES_DENY))
233 return MODEACTION_DENY;
235 const char modechar = mh->GetModeChar();
237 if (chan && !SkipACL && (MOD_RESULT != MOD_RES_ALLOW))
239 MOD_RESULT = mh->AccessCheck(user, chan, parameter, adding);
241 if (MOD_RESULT == MOD_RES_DENY)
242 return MODEACTION_DENY;
243 if (MOD_RESULT == MOD_RES_PASSTHRU)
245 unsigned int neededrank = mh->GetLevelRequired();
246 /* Compare our rank on the channel against the rank of the required prefix,
247 * allow if >= ours. Because mIRC and xchat throw a tizz if the modes shown
248 * in NAMES(X) are not in rank order, we know the most powerful mode is listed
249 * first, so we don't need to iterate, we just look up the first instead.
251 unsigned int ourrank = chan->GetPrefixValue(user);
252 if (ourrank < neededrank)
254 PrefixMode* neededmh = NULL;
255 for(char c='A'; c <= 'z'; c++)
257 PrefixMode* privmh = FindPrefixMode(c);
258 if (privmh && privmh->GetPrefixRank() >= neededrank)
260 // this mode is sufficient to allow this action
261 if (!neededmh || privmh->GetPrefixRank() < neededmh->GetPrefixRank())
266 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You must have channel %s access or above to %sset channel mode %c",
267 chan->name.c_str(), neededmh->name.c_str(), adding ? "" : "un", modechar);
269 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You cannot %sset channel mode %c",
270 chan->name.c_str(), adding ? "" : "un", modechar);
271 return MODEACTION_DENY;
276 // Ask mode watchers whether this mode change is OK
277 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
278 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
280 ModeWatcher* mw = i->second;
281 if (mw->GetModeType() == type)
283 if (!mw->BeforeMode(user, targetuser, chan, parameter, adding))
284 return MODEACTION_DENY;
286 // A module whacked the parameter completely, and there was one. Abort.
287 if (pcnt && parameter.empty())
288 return MODEACTION_DENY;
292 if (IS_LOCAL(user) && !user->IsOper())
294 char* disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes;
295 if (disabled[modechar - 'A'])
297 user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - %s mode %c has been locked by the administrator",
298 type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
299 return MODEACTION_DENY;
303 if (adding && IS_LOCAL(user) && mh->NeedsOper() && !user->HasModePermission(modechar, type))
305 /* It's an oper only mode, and they don't have access to it. */
308 user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - Oper type %s does not have access to set %s mode %c",
309 user->oper->name.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
313 user->WriteNumeric(ERR_NOPRIVILEGES, ":Permission Denied - Only operators may set %s mode %c",
314 type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
316 return MODEACTION_DENY;
319 /* Call the handler for the mode */
320 ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding);
322 if (pcnt && parameter.empty())
323 return MODEACTION_DENY;
325 if (ma != MODEACTION_ALLOW)
328 itpair = modewatchermap.equal_range(mh->name);
329 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
331 ModeWatcher* mw = i->second;
332 if (mw->GetModeType() == type)
333 mw->AfterMode(user, targetuser, chan, parameter, adding);
336 return MODEACTION_ALLOW;
339 void ModeParser::ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex, unsigned int endindex)
341 if (endindex > parameters.size())
342 endindex = parameters.size();
344 const std::string& mode_sequence = parameters[beginindex];
347 unsigned int param_at = beginindex+1;
349 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
351 unsigned char modechar = *letter;
352 if (modechar == '+' || modechar == '-')
354 adding = (modechar == '+');
358 ModeHandler *mh = this->FindMode(modechar, type);
361 /* No mode handler? Unknown mode character then. */
362 user->WriteNumeric(type == MODETYPE_CHANNEL ? ERR_UNKNOWNMODE : ERR_UNKNOWNSNOMASK, "%c :is unknown mode char to me", modechar);
366 std::string parameter;
367 if (mh->GetNumParams(adding) && param_at < endindex)
368 parameter = parameters[param_at++];
370 changelist.push(mh, adding, parameter);
374 static bool IsModeParamValid(User* user, Channel* targetchannel, User* targetuser, const Modes::Change& item)
376 // An empty parameter is never acceptable
377 if (item.param.empty())
379 item.mh->OnParameterMissing(user, targetuser, targetchannel);
383 // The parameter cannot begin with a ':' character or contain a space
384 if ((item.param[0] == ':') || (item.param.find(' ') != std::string::npos))
390 // Returns true if we should apply a merged mode, false if we should skip it
391 static bool ShouldApplyMergedMode(Channel* chan, Modes::Change& item)
393 ModeHandler* mh = item.mh;
394 if ((!chan) || (!chan->IsModeSet(mh)) || (mh->IsListMode()))
395 // Mode not set here or merge is not applicable, apply the incoming mode
398 // Mode handler decides
399 std::string ours = chan->GetModeParameter(mh);
400 return mh->ResolveModeConflict(item.param, ours, chan);
403 void ModeParser::Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags)
405 // Call ProcessSingle until the entire list is processed, but at least once to ensure
406 // LastParse and LastChangeList are cleared
407 unsigned int processed = 0;
410 unsigned int n = ProcessSingle(user, targetchannel, targetuser, changelist, flags, processed);
413 while (processed < changelist.size());
416 unsigned int ModeParser::ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags, unsigned int beginindex)
419 LastChangeList.clear();
421 unsigned int modes_processed = 0;
422 std::string output_mode;
423 std::string output_parameters;
425 char output_pm = '\0'; // current output state, '+' or '-'
426 Modes::ChangeList::List& list = changelist.getlist();
427 for (Modes::ChangeList::List::iterator i = list.begin()+beginindex; i != list.end(); ++i)
431 Modes::Change& item = *i;
432 ModeHandler* mh = item.mh;
434 // If the mode is supposed to have a parameter then we first take a look at item.param
435 // and, if we were asked to, also handle mode merges now
436 if (mh->GetNumParams(item.adding))
438 // Skip the mode if the parameter does not pass basic validation
439 if (!IsModeParamValid(user, targetchannel, targetuser, item))
442 // If this is a merge and we won we don't apply this mode
443 if ((flags & MODE_MERGE) && (!ShouldApplyMergedMode(targetchannel, item)))
447 ModeAction ma = TryMode(user, targetuser, targetchannel, item, (!(flags & MODE_CHECKACCESS)));
449 if (ma != MODEACTION_ALLOW)
452 char needed_pm = item.adding ? '+' : '-';
453 if (needed_pm != output_pm)
455 output_pm = needed_pm;
456 output_mode.append(1, output_pm);
458 output_mode.push_back(mh->GetModeChar());
460 if (!item.param.empty())
462 output_parameters.push_back(' ');
463 output_parameters.append(item.param);
465 LastChangeList.push(mh, item.adding, item.param);
467 if ((output_mode.length() + output_parameters.length() > 450)
468 || (output_mode.length() > 100)
469 || (LastChangeList.size() >= ServerInstance->Config->Limits.MaxModes))
471 /* mode sequence is getting too long */
476 if (!output_mode.empty())
478 LastParse = targetchannel ? targetchannel->name : targetuser->nick;
479 LastParse.append(" ");
480 LastParse.append(output_mode);
481 LastParse.append(output_parameters);
484 targetchannel->WriteChannel(user, "MODE " + LastParse);
486 targetuser->WriteFrom(user, "MODE " + LastParse);
488 FOREACH_MOD(OnMode, (user, targetuser, targetchannel, LastChangeList, flags, output_mode));
491 return modes_processed;
494 void ModeParser::ShowListModeList(User* user, Channel* chan, ModeHandler* mh)
497 ModResult MOD_RESULT;
498 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, "", true));
499 if (MOD_RESULT == MOD_RES_DENY)
504 // Ask mode watchers whether it's OK to show the list
505 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
506 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
508 ModeWatcher* mw = i->second;
509 if (mw->GetModeType() == MODETYPE_CHANNEL)
511 std::string dummyparam;
513 if (!mw->BeforeMode(user, NULL, chan, dummyparam, true))
515 // A mode watcher doesn't want us to show the list
523 mh->DisplayList(user, chan);
525 mh->DisplayEmptyList(user, chan);
529 void ModeParser::CleanMask(std::string &mask)
531 std::string::size_type pos_of_pling = mask.find_first_of('!');
532 std::string::size_type pos_of_at = mask.find_first_of('@');
533 std::string::size_type pos_of_dot = mask.find_first_of('.');
534 std::string::size_type pos_of_colons = mask.find("::"); /* Because ipv6 addresses are colon delimited -- double so it treats extban as nick */
536 if (mask.length() >= 2 && mask[1] == ':')
537 return; // if it's an extban, don't even try guess how it needs to be formed.
539 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
541 /* Just a nick, or just a host - or clearly ipv6 (starting with :) */
542 if ((pos_of_dot == std::string::npos) && (pos_of_colons == std::string::npos) && mask[0] != ':')
544 /* It has no '.' in it, it must be a nick. */
549 /* Got a dot in it? Has to be a host */
550 mask = "*!*@" + mask;
553 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
555 /* Has an @ but no !, its a user@host */
558 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
560 /* Has a ! but no @, it must be a nick!ident */
565 ModeHandler::Id ModeParser::AllocateModeId(ModeType mt)
567 for (ModeHandler::Id i = 0; i != MODEID_MAX; ++i)
569 if (!modehandlersbyid[mt][i])
573 throw ModuleException("Out of ModeIds");
576 void ModeParser::AddMode(ModeHandler* mh)
578 /* Yes, i know, this might let people declare modes like '_' or '^'.
579 * If they do that, thats their problem, and if i ever EVER see an
580 * official InspIRCd developer do that, i'll beat them with a paddle!
582 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
583 throw ModuleException("Invalid letter for mode " + mh->name);
585 /* A mode prefix of ',' is not acceptable, it would fuck up server to server.
586 * A mode prefix of ':' will fuck up both server to server, and client to server.
587 * A mode prefix of '#' will mess up /whois and /privmsg
589 PrefixMode* pm = mh->IsPrefixMode();
592 if ((pm->GetPrefix() > 126) || (pm->GetPrefix() == ',') || (pm->GetPrefix() == ':') || (pm->GetPrefix() == '#'))
593 throw ModuleException("Invalid prefix for mode " + mh->name);
595 if (FindPrefix(pm->GetPrefix()))
596 throw ModuleException("Prefix already exists for mode " + mh->name);
599 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
601 throw ModuleException("Letter is already in use for mode " + mh->name);
603 // The mode needs an id if it is either a user mode, a simple mode (flag) or a parameter mode.
604 // Otherwise (for listmodes and prefix modes) the id remains MODEID_MAX, which is invalid.
605 ModeHandler::Id modeid = MODEID_MAX;
606 if ((mh->GetModeType() == MODETYPE_USER) || (mh->IsParameterMode()) || (!mh->IsListMode()))
607 modeid = AllocateModeId(mh->GetModeType());
609 if (!modehandlersbyname[mh->GetModeType()].insert(std::make_pair(mh->name, mh)).second)
610 throw ModuleException("Mode name already in use: " + mh->name);
612 // Everything is fine, add the mode
614 // If we allocated an id for this mode then save it and put the mode handler into the slot
615 if (modeid != MODEID_MAX)
618 modehandlersbyid[mh->GetModeType()][modeid] = mh;
623 mhlist.prefix.push_back(pm);
624 else if (mh->IsListModeBase())
625 mhlist.list.push_back(mh->IsListModeBase());
627 RecreateModeListFor004Numeric();
630 bool ModeParser::DelMode(ModeHandler* mh)
632 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
635 ModeHandlerMap& mhmap = modehandlersbyname[mh->GetModeType()];
636 ModeHandlerMap::iterator mhmapit = mhmap.find(mh->name);
637 if ((mhmapit == mhmap.end()) || (mhmapit->second != mh))
640 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
644 /* Note: We can't stack here, as we have modes potentially being removed across many different channels.
645 * To stack here we have to make the algorithm slower. Discuss.
647 switch (mh->GetModeType())
651 const user_hash& users = ServerInstance->Users->GetUsers();
652 for (user_hash::const_iterator i = users.begin(); i != users.end(); )
654 User* user = i->second;
656 mh->RemoveMode(user);
660 case MODETYPE_CHANNEL:
662 const chan_hash& chans = ServerInstance->GetChans();
663 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); )
665 // The channel may not be in the hash after RemoveMode(), see m_permchannels
666 Channel* chan = i->second;
669 Modes::ChangeList changelist;
670 mh->RemoveMode(chan, changelist);
671 this->Process(ServerInstance->FakeClient, chan, NULL, changelist, MODE_LOCALONLY);
677 mhmap.erase(mhmapit);
678 if (mh->GetId() != MODEID_MAX)
679 modehandlersbyid[mh->GetModeType()][mh->GetId()] = NULL;
681 if (mh->IsPrefixMode())
682 mhlist.prefix.erase(std::find(mhlist.prefix.begin(), mhlist.prefix.end(), mh->IsPrefixMode()));
683 else if (mh->IsListModeBase())
684 mhlist.list.erase(std::find(mhlist.list.begin(), mhlist.list.end(), mh->IsListModeBase()));
686 RecreateModeListFor004Numeric();
690 ModeHandler* ModeParser::FindMode(const std::string& modename, ModeType mt)
692 ModeHandlerMap& mhmap = modehandlersbyname[mt];
693 ModeHandlerMap::const_iterator it = mhmap.find(modename);
694 if (it != mhmap.end())
700 ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt)
702 if ((modeletter < 'A') || (modeletter > 'z'))
705 return modehandlers[mt][modeletter-65];
708 PrefixMode* ModeParser::FindPrefixMode(unsigned char modeletter)
710 ModeHandler* mh = FindMode(modeletter, MODETYPE_CHANNEL);
713 return mh->IsPrefixMode();
716 std::string ModeParser::CreateModeList(ModeType mt, bool needparam)
720 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
722 ModeHandler* mh = modehandlers[mt][mode-65];
723 if ((mh) && ((!needparam) || (mh->GetNumParams(true))))
724 modestr.push_back(mode);
730 void ModeParser::RecreateModeListFor004Numeric()
732 Cached004ModeList = CreateModeList(MODETYPE_USER) + " " + CreateModeList(MODETYPE_CHANNEL) + " " + CreateModeList(MODETYPE_CHANNEL, true);
735 PrefixMode* ModeParser::FindPrefix(unsigned const char pfxletter)
737 const PrefixModeList& list = GetPrefixModes();
738 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
741 if (pm->GetPrefix() == pfxletter)
747 std::string ModeParser::GiveModeList(ModeType mt)
749 std::string type1; /* Listmodes EXCEPT those with a prefix */
750 std::string type2; /* Modes that take a param when adding or removing */
751 std::string type3; /* Modes that only take a param when adding */
752 std::string type4; /* Modes that dont take a param */
754 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
756 ModeHandler* mh = modehandlers[mt][mode-65];
757 /* One parameter when adding */
760 if (mh->GetNumParams(true))
762 PrefixMode* pm = mh->IsPrefixMode();
763 if ((mh->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0)))
765 type1 += mh->GetModeChar();
769 /* ... and one parameter when removing */
770 if (mh->GetNumParams(false))
772 /* But not a list mode */
775 type2 += mh->GetModeChar();
780 /* No parameters when removing */
781 type3 += mh->GetModeChar();
787 type4 += mh->GetModeChar();
792 return type1 + "," + type2 + "," + type3 + "," + type4;
795 std::string ModeParser::BuildPrefixes(bool lettersAndModes)
797 std::string mletters;
798 std::string mprefixes;
799 insp::flat_map<int, std::pair<char, char> > prefixes;
801 const PrefixModeList& list = GetPrefixModes();
802 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
806 prefixes[pm->GetPrefixRank()] = std::make_pair(pm->GetPrefix(), pm->GetModeChar());
809 for (insp::flat_map<int, std::pair<char, char> >::reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); ++n)
811 mletters = mletters + n->second.first;
812 mprefixes = mprefixes + n->second.second;
815 return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters;
818 void ModeParser::AddModeWatcher(ModeWatcher* mw)
820 modewatchermap.insert(std::make_pair(mw->GetModeName(), mw));
823 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
825 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mw->GetModeName());
826 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
830 modewatchermap.erase(i);
838 void ModeHandler::RemoveMode(User* user)
840 // Remove the mode if it's set on the user
841 if (user->IsModeSet(this->GetModeChar()))
843 Modes::ChangeList changelist;
844 changelist.push_remove(this);
845 ServerInstance->Modes->Process(ServerInstance->FakeClient, NULL, user, changelist, ModeParser::MODE_LOCALONLY);
849 void ModeHandler::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
851 if (channel->IsModeSet(this))
853 if (this->GetNumParams(false))
854 // Removing this mode requires a parameter
855 changelist.push_remove(this, channel->GetModeParameter(this));
857 changelist.push_remove(this);
861 void PrefixMode::RemoveMode(Channel* chan, Modes::ChangeList& changelist)
863 const Channel::MemberMap& userlist = chan->GetUsers();
864 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
866 if (i->second->hasMode(this->GetModeChar()))
867 changelist.push_remove(this, i->first->nick);
873 SimpleChannelModeHandler s;
874 SimpleChannelModeHandler p;
875 SimpleChannelModeHandler m;
876 SimpleChannelModeHandler t;
878 SimpleChannelModeHandler n;
879 SimpleChannelModeHandler i;
887 SimpleUserModeHandler ui;
889 ModeUserServerNoticeMask us;
892 : s(NULL, "secret", 's')
893 , p(NULL, "private", 'p')
894 , m(NULL, "moderated", 'm')
895 , t(NULL, "topiclock", 't')
896 , n(NULL, "noextmsg", 'n')
897 , i(NULL, "inviteonly", 'i')
898 , ui(NULL, "invisible", 'i')
904 ServiceProvider* modes[] = { &s, &p, &m, &t, &n, &i, &k, &l, &b, &o, &v,
906 ServerInstance->Modules->AddServices(modes, sizeof(modes)/sizeof(ServiceProvider*));
910 static builtin_modes static_modes;
912 void ModeParser::InitBuiltinModes()
915 static_modes.b.DoRehash();
918 ModeParser::ModeParser()
920 /* Clear mode handler list */
921 memset(modehandlers, 0, sizeof(modehandlers));
922 memset(modehandlersbyid, 0, sizeof(modehandlersbyid));
925 ModeParser::~ModeParser()