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 bool ModeHandler::NeedsParam(bool adding) const
49 switch (parameters_taken)
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(Numerics::NoSuchNick(parameter));
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 const bool needs_param = mh->NeedsParam(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, chan->name, InspIRCd::Format("You must have channel %s access or above to %sset channel mode %c",
267 neededmh->name.c_str(), adding ? "" : "un", modechar));
269 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, chan->name, InspIRCd::Format("You cannot %sset channel mode %c", (adding ? "" : "un"), modechar));
270 return MODEACTION_DENY;
275 // Ask mode watchers whether this mode change is OK
276 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
277 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
279 ModeWatcher* mw = i->second;
280 if (mw->GetModeType() == type)
282 if (!mw->BeforeMode(user, targetuser, chan, parameter, adding))
283 return MODEACTION_DENY;
285 // A module whacked the parameter completely, and there was one. Abort.
286 if ((needs_param) && (parameter.empty()))
287 return MODEACTION_DENY;
291 if (IS_LOCAL(user) && !user->IsOper())
293 char* disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes;
294 if (disabled[modechar - 'A'])
296 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - %s mode %c has been locked by the administrator",
297 type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
298 return MODEACTION_DENY;
302 if (adding && IS_LOCAL(user) && mh->NeedsOper() && !user->HasModePermission(modechar, type))
304 /* It's an oper only mode, and they don't have access to it. */
307 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Oper type %s does not have access to set %s mode %c",
308 user->oper->name.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
312 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Only operators may set %s mode %c",
313 type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
315 return MODEACTION_DENY;
318 /* Call the handler for the mode */
319 ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding);
321 if ((needs_param) && (parameter.empty()))
322 return MODEACTION_DENY;
324 if (ma != MODEACTION_ALLOW)
327 itpair = modewatchermap.equal_range(mh->name);
328 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
330 ModeWatcher* mw = i->second;
331 if (mw->GetModeType() == type)
332 mw->AfterMode(user, targetuser, chan, parameter, adding);
335 return MODEACTION_ALLOW;
338 void ModeParser::ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex, unsigned int endindex)
340 if (endindex > parameters.size())
341 endindex = parameters.size();
343 const std::string& mode_sequence = parameters[beginindex];
346 unsigned int param_at = beginindex+1;
348 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
350 unsigned char modechar = *letter;
351 if (modechar == '+' || modechar == '-')
353 adding = (modechar == '+');
357 ModeHandler *mh = this->FindMode(modechar, type);
360 /* No mode handler? Unknown mode character then. */
361 user->WriteNumeric(type == MODETYPE_CHANNEL ? ERR_UNKNOWNMODE : ERR_UNKNOWNSNOMASK, modechar, "is unknown mode char to me");
365 std::string parameter;
366 if ((mh->NeedsParam(adding)) && (param_at < endindex))
367 parameter = parameters[param_at++];
369 changelist.push(mh, adding, parameter);
373 static bool IsModeParamValid(User* user, Channel* targetchannel, User* targetuser, const Modes::Change& item)
375 // An empty parameter is never acceptable
376 if (item.param.empty())
378 item.mh->OnParameterMissing(user, targetuser, targetchannel);
382 // The parameter cannot begin with a ':' character or contain a space
383 if ((item.param[0] == ':') || (item.param.find(' ') != std::string::npos))
389 // Returns true if we should apply a merged mode, false if we should skip it
390 static bool ShouldApplyMergedMode(Channel* chan, Modes::Change& item)
392 ModeHandler* mh = item.mh;
393 if ((!chan) || (!chan->IsModeSet(mh)) || (mh->IsListMode()))
394 // Mode not set here or merge is not applicable, apply the incoming mode
397 // Mode handler decides
398 std::string ours = chan->GetModeParameter(mh);
399 return mh->ResolveModeConflict(item.param, ours, chan);
402 void ModeParser::Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags)
404 // Call ProcessSingle until the entire list is processed, but at least once to ensure
405 // LastParse and LastChangeList are cleared
406 unsigned int processed = 0;
409 unsigned int n = ProcessSingle(user, targetchannel, targetuser, changelist, flags, processed);
412 while (processed < changelist.size());
415 unsigned int ModeParser::ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags, unsigned int beginindex)
418 LastChangeList.clear();
420 unsigned int modes_processed = 0;
421 std::string output_mode;
422 std::string output_parameters;
424 char output_pm = '\0'; // current output state, '+' or '-'
425 Modes::ChangeList::List& list = changelist.getlist();
426 for (Modes::ChangeList::List::iterator i = list.begin()+beginindex; i != list.end(); ++i)
430 Modes::Change& item = *i;
431 ModeHandler* mh = item.mh;
433 // If the mode is supposed to have a parameter then we first take a look at item.param
434 // and, if we were asked to, also handle mode merges now
435 if (mh->NeedsParam(item.adding))
437 // Skip the mode if the parameter does not pass basic validation
438 if (!IsModeParamValid(user, targetchannel, targetuser, item))
441 // If this is a merge and we won we don't apply this mode
442 if ((flags & MODE_MERGE) && (!ShouldApplyMergedMode(targetchannel, item)))
446 ModeAction ma = TryMode(user, targetuser, targetchannel, item, (!(flags & MODE_CHECKACCESS)));
448 if (ma != MODEACTION_ALLOW)
451 char needed_pm = item.adding ? '+' : '-';
452 if (needed_pm != output_pm)
454 output_pm = needed_pm;
455 output_mode.append(1, output_pm);
457 output_mode.push_back(mh->GetModeChar());
459 if (!item.param.empty())
461 output_parameters.push_back(' ');
462 output_parameters.append(item.param);
464 LastChangeList.push(mh, item.adding, item.param);
466 if ((output_mode.length() + output_parameters.length() > 450)
467 || (output_mode.length() > 100)
468 || (LastChangeList.size() >= ServerInstance->Config->Limits.MaxModes))
470 /* mode sequence is getting too long */
475 if (!output_mode.empty())
477 LastParse = targetchannel ? targetchannel->name : targetuser->nick;
478 LastParse.append(" ");
479 LastParse.append(output_mode);
480 LastParse.append(output_parameters);
483 targetchannel->WriteChannel(user, "MODE " + LastParse);
485 targetuser->WriteFrom(user, "MODE " + LastParse);
487 FOREACH_MOD(OnMode, (user, targetuser, targetchannel, LastChangeList, flags, output_mode));
490 return modes_processed;
493 void ModeParser::ShowListModeList(User* user, Channel* chan, ModeHandler* mh)
496 ModResult MOD_RESULT;
497 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, "", true));
498 if (MOD_RESULT == MOD_RES_DENY)
503 // Ask mode watchers whether it's OK to show the list
504 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
505 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
507 ModeWatcher* mw = i->second;
508 if (mw->GetModeType() == MODETYPE_CHANNEL)
510 std::string dummyparam;
512 if (!mw->BeforeMode(user, NULL, chan, dummyparam, true))
514 // A mode watcher doesn't want us to show the list
522 mh->DisplayList(user, chan);
524 mh->DisplayEmptyList(user, chan);
528 void ModeParser::CleanMask(std::string &mask)
530 std::string::size_type pos_of_pling = mask.find_first_of('!');
531 std::string::size_type pos_of_at = mask.find_first_of('@');
532 std::string::size_type pos_of_dot = mask.find_first_of('.');
533 std::string::size_type pos_of_colons = mask.find("::"); /* Because ipv6 addresses are colon delimited -- double so it treats extban as nick */
535 if (mask.length() >= 2 && mask[1] == ':')
536 return; // if it's an extban, don't even try guess how it needs to be formed.
538 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
540 /* Just a nick, or just a host - or clearly ipv6 (starting with :) */
541 if ((pos_of_dot == std::string::npos) && (pos_of_colons == std::string::npos) && mask[0] != ':')
543 /* It has no '.' in it, it must be a nick. */
548 /* Got a dot in it? Has to be a host */
549 mask = "*!*@" + mask;
552 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
554 /* Has an @ but no !, its a user@host */
557 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
559 /* Has a ! but no @, it must be a nick!ident */
564 ModeHandler::Id ModeParser::AllocateModeId(ModeType mt)
566 for (ModeHandler::Id i = 0; i != MODEID_MAX; ++i)
568 if (!modehandlersbyid[mt][i])
572 throw ModuleException("Out of ModeIds");
575 void ModeParser::AddMode(ModeHandler* mh)
577 /* Yes, i know, this might let people declare modes like '_' or '^'.
578 * If they do that, thats their problem, and if i ever EVER see an
579 * official InspIRCd developer do that, i'll beat them with a paddle!
581 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
582 throw ModuleException("Invalid letter for mode " + mh->name);
584 /* A mode prefix of ',' is not acceptable, it would fuck up server to server.
585 * A mode prefix of ':' will fuck up both server to server, and client to server.
586 * A mode prefix of '#' will mess up /whois and /privmsg
588 PrefixMode* pm = mh->IsPrefixMode();
591 if ((pm->GetPrefix() > 126) || (pm->GetPrefix() == ',') || (pm->GetPrefix() == ':') || (pm->GetPrefix() == '#'))
592 throw ModuleException("Invalid prefix for mode " + mh->name);
594 if (FindPrefix(pm->GetPrefix()))
595 throw ModuleException("Prefix already exists for mode " + mh->name);
598 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
600 throw ModuleException("Letter is already in use for mode " + mh->name);
602 // The mode needs an id if it is either a user mode, a simple mode (flag) or a parameter mode.
603 // Otherwise (for listmodes and prefix modes) the id remains MODEID_MAX, which is invalid.
604 ModeHandler::Id modeid = MODEID_MAX;
605 if ((mh->GetModeType() == MODETYPE_USER) || (mh->IsParameterMode()) || (!mh->IsListMode()))
606 modeid = AllocateModeId(mh->GetModeType());
608 if (!modehandlersbyname[mh->GetModeType()].insert(std::make_pair(mh->name, mh)).second)
609 throw ModuleException("Mode name already in use: " + mh->name);
611 // Everything is fine, add the mode
613 // If we allocated an id for this mode then save it and put the mode handler into the slot
614 if (modeid != MODEID_MAX)
617 modehandlersbyid[mh->GetModeType()][modeid] = mh;
622 mhlist.prefix.push_back(pm);
623 else if (mh->IsListModeBase())
624 mhlist.list.push_back(mh->IsListModeBase());
626 RecreateModeListFor004Numeric();
629 bool ModeParser::DelMode(ModeHandler* mh)
631 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
634 ModeHandlerMap& mhmap = modehandlersbyname[mh->GetModeType()];
635 ModeHandlerMap::iterator mhmapit = mhmap.find(mh->name);
636 if ((mhmapit == mhmap.end()) || (mhmapit->second != mh))
639 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
643 /* Note: We can't stack here, as we have modes potentially being removed across many different channels.
644 * To stack here we have to make the algorithm slower. Discuss.
646 switch (mh->GetModeType())
650 const user_hash& users = ServerInstance->Users->GetUsers();
651 for (user_hash::const_iterator i = users.begin(); i != users.end(); )
653 User* user = i->second;
655 mh->RemoveMode(user);
659 case MODETYPE_CHANNEL:
661 const chan_hash& chans = ServerInstance->GetChans();
662 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); )
664 // The channel may not be in the hash after RemoveMode(), see m_permchannels
665 Channel* chan = i->second;
668 Modes::ChangeList changelist;
669 mh->RemoveMode(chan, changelist);
670 this->Process(ServerInstance->FakeClient, chan, NULL, changelist, MODE_LOCALONLY);
676 mhmap.erase(mhmapit);
677 if (mh->GetId() != MODEID_MAX)
678 modehandlersbyid[mh->GetModeType()][mh->GetId()] = NULL;
680 if (mh->IsPrefixMode())
681 mhlist.prefix.erase(std::find(mhlist.prefix.begin(), mhlist.prefix.end(), mh->IsPrefixMode()));
682 else if (mh->IsListModeBase())
683 mhlist.list.erase(std::find(mhlist.list.begin(), mhlist.list.end(), mh->IsListModeBase()));
685 RecreateModeListFor004Numeric();
689 ModeHandler* ModeParser::FindMode(const std::string& modename, ModeType mt)
691 ModeHandlerMap& mhmap = modehandlersbyname[mt];
692 ModeHandlerMap::const_iterator it = mhmap.find(modename);
693 if (it != mhmap.end())
699 ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt)
701 if ((modeletter < 'A') || (modeletter > 'z'))
704 return modehandlers[mt][modeletter-65];
707 PrefixMode* ModeParser::FindPrefixMode(unsigned char modeletter)
709 ModeHandler* mh = FindMode(modeletter, MODETYPE_CHANNEL);
712 return mh->IsPrefixMode();
715 std::string ModeParser::CreateModeList(ModeType mt, bool needparam)
719 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
721 ModeHandler* mh = modehandlers[mt][mode-65];
722 if ((mh) && ((!needparam) || (mh->NeedsParam(true))))
723 modestr.push_back(mode);
729 void ModeParser::RecreateModeListFor004Numeric()
731 Cached004ModeList = CreateModeList(MODETYPE_USER) + " " + CreateModeList(MODETYPE_CHANNEL) + " " + CreateModeList(MODETYPE_CHANNEL, true);
734 PrefixMode* ModeParser::FindPrefix(unsigned const char pfxletter)
736 const PrefixModeList& list = GetPrefixModes();
737 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
740 if (pm->GetPrefix() == pfxletter)
746 std::string ModeParser::GiveModeList(ModeType mt)
748 std::string type1; /* Listmodes EXCEPT those with a prefix */
749 std::string type2; /* Modes that take a param when adding or removing */
750 std::string type3; /* Modes that only take a param when adding */
751 std::string type4; /* Modes that dont take a param */
753 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
755 ModeHandler* mh = modehandlers[mt][mode-65];
756 /* One parameter when adding */
759 if (mh->NeedsParam(true))
761 PrefixMode* pm = mh->IsPrefixMode();
762 if ((mh->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0)))
764 type1 += mh->GetModeChar();
768 /* ... and one parameter when removing */
769 if (mh->NeedsParam(false))
771 /* But not a list mode */
774 type2 += mh->GetModeChar();
779 /* No parameters when removing */
780 type3 += mh->GetModeChar();
786 type4 += mh->GetModeChar();
791 return type1 + "," + type2 + "," + type3 + "," + type4;
794 struct PrefixModeSorter
796 bool operator()(PrefixMode* lhs, PrefixMode* rhs)
798 return lhs->GetPrefixRank() < rhs->GetPrefixRank();
802 std::string ModeParser::BuildPrefixes(bool lettersAndModes)
804 std::string mletters;
805 std::string mprefixes;
806 std::vector<PrefixMode*> prefixes;
808 const PrefixModeList& list = GetPrefixModes();
809 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
813 prefixes.push_back(pm);
816 std::sort(prefixes.begin(), prefixes.end(), PrefixModeSorter());
817 for (std::vector<PrefixMode*>::const_reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); ++n)
819 mletters += (*n)->GetPrefix();
820 mprefixes += (*n)->GetModeChar();
823 return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters;
826 void ModeParser::AddModeWatcher(ModeWatcher* mw)
828 modewatchermap.insert(std::make_pair(mw->GetModeName(), mw));
831 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
833 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mw->GetModeName());
834 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
838 modewatchermap.erase(i);
846 void ModeHandler::RemoveMode(User* user)
848 // Remove the mode if it's set on the user
849 if (user->IsModeSet(this->GetModeChar()))
851 Modes::ChangeList changelist;
852 changelist.push_remove(this);
853 ServerInstance->Modes->Process(ServerInstance->FakeClient, NULL, user, changelist, ModeParser::MODE_LOCALONLY);
857 void ModeHandler::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
859 if (channel->IsModeSet(this))
861 if (this->NeedsParam(false))
862 // Removing this mode requires a parameter
863 changelist.push_remove(this, channel->GetModeParameter(this));
865 changelist.push_remove(this);
869 void PrefixMode::RemoveMode(Channel* chan, Modes::ChangeList& changelist)
871 const Channel::MemberMap& userlist = chan->GetUsers();
872 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
874 if (i->second->hasMode(this->GetModeChar()))
875 changelist.push_remove(this, i->first->nick);
881 SimpleChannelModeHandler s;
882 SimpleChannelModeHandler p;
883 SimpleChannelModeHandler m;
884 SimpleChannelModeHandler t;
886 SimpleChannelModeHandler n;
887 SimpleChannelModeHandler i;
895 SimpleUserModeHandler ui;
897 ModeUserServerNoticeMask us;
900 : s(NULL, "secret", 's')
901 , p(NULL, "private", 'p')
902 , m(NULL, "moderated", 'm')
903 , t(NULL, "topiclock", 't')
904 , n(NULL, "noextmsg", 'n')
905 , i(NULL, "inviteonly", 'i')
906 , ui(NULL, "invisible", 'i')
912 ServiceProvider* modes[] = { &s, &p, &m, &t, &n, &i, &k, &l, &b, &o, &v,
914 ServerInstance->Modules->AddServices(modes, sizeof(modes)/sizeof(ServiceProvider*));
918 static builtin_modes static_modes;
920 void ModeParser::InitBuiltinModes()
923 static_modes.b.DoRehash();
926 ModeParser::ModeParser()
928 /* Clear mode handler list */
929 memset(modehandlers, 0, sizeof(modehandlers));
930 memset(modehandlersbyid, 0, sizeof(modehandlersbyid));
933 ModeParser::~ModeParser()