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/>.
28 ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type, Class mclass)
29 : ServiceProvider(Creator, Name, SERVICE_MODE)
30 , modeid(ModeParser::MODEID_MAX)
31 , parameters_taken(Params)
37 , ranktoset(HALFOP_VALUE)
38 , ranktounset(HALFOP_VALUE)
42 CullResult ModeHandler::cull()
45 ServerInstance->Modes->DelMode(this);
46 return classbase::cull();
49 ModeHandler::~ModeHandler()
53 bool ModeHandler::NeedsParam(bool adding) const
55 switch (parameters_taken)
67 std::string ModeHandler::GetUserParameter(const User* user) const
72 ModResult ModeHandler::AccessCheck(User*, Channel*, std::string &, bool)
74 return MOD_RES_PASSTHRU;
77 ModeAction ModeHandler::OnModeChange(User*, User*, Channel*, std::string&, bool)
79 return MODEACTION_DENY;
82 void ModeHandler::DisplayList(User*, Channel*)
86 void ModeHandler::DisplayEmptyList(User*, Channel*)
90 void ModeHandler::OnParameterMissing(User* user, User* dest, Channel* channel)
92 const std::string message = InspIRCd::Format("You must specify a parameter for the %s mode", name.c_str());
94 user->WriteNumeric(Numerics::InvalidModeParameter(channel, this, "*", message));
96 user->WriteNumeric(Numerics::InvalidModeParameter(dest, this, "*", message));
99 bool ModeHandler::ResolveModeConflict(std::string& theirs, const std::string& ours, Channel*)
101 return (theirs < ours);
104 void ModeHandler::RegisterService()
106 ServerInstance->Modes.AddMode(this);
107 ServerInstance->Modules.AddReferent((GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + name, this);
110 ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
112 /* We're either trying to add a mode we already have or
113 remove a mode we don't have, deny. */
114 if (dest->IsModeSet(this) == adding)
115 return MODEACTION_DENY;
117 /* adding will be either true or false, depending on if we
118 are adding or removing the mode, since we already checked
119 to make sure we aren't adding a mode we have or that we
120 aren't removing a mode we don't have, we don't have to do any
121 other checks here to see if it's true or false, just add or
123 dest->SetMode(this, adding);
125 return MODEACTION_ALLOW;
129 ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
131 /* We're either trying to add a mode we already have or
132 remove a mode we don't have, deny. */
133 if (channel->IsModeSet(this) == adding)
134 return MODEACTION_DENY;
136 /* adding will be either true or false, depending on if we
137 are adding or removing the mode, since we already checked
138 to make sure we aren't adding a mode we have or that we
139 aren't removing a mode we don't have, we don't have to do any
140 other checks here to see if it's true or false, just add or
142 channel->SetMode(this, adding);
144 return MODEACTION_ALLOW;
147 ModeWatcher::ModeWatcher(Module* Creator, const std::string& modename, ModeType type)
148 : mode(modename), m_type(type), creator(Creator)
150 ServerInstance->Modes->AddModeWatcher(this);
153 ModeWatcher::~ModeWatcher()
155 ServerInstance->Modes->DelModeWatcher(this);
158 bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool)
163 void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool)
167 PrefixMode::PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank, char PrefixChar)
168 : ModeHandler(Creator, Name, ModeLetter, PARAM_ALWAYS, MODETYPE_CHANNEL, MC_PREFIX)
176 ModResult PrefixMode::AccessCheck(User* src, Channel*, std::string& value, bool adding)
178 if (!adding && src->nick == value && selfremove)
179 return MOD_RES_ALLOW;
180 return MOD_RES_PASSTHRU;
183 ModeAction PrefixMode::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
186 if (IS_LOCAL(source))
187 target = ServerInstance->FindNickOnly(parameter);
189 target = ServerInstance->FindNick(parameter);
193 source->WriteNumeric(Numerics::NoSuchNick(parameter));
194 return MODEACTION_DENY;
197 Membership* memb = chan->GetUser(target);
199 return MODEACTION_DENY;
201 parameter = target->nick;
202 return (memb->SetPrefix(this, adding) ? MODEACTION_ALLOW : MODEACTION_DENY);
205 void PrefixMode::Update(unsigned int rank, unsigned int setrank, unsigned int unsetrank, bool selfrm)
209 ranktounset = unsetrank;
213 ModeAction ParamModeBase::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
217 if (chan->GetModeParameter(this) == parameter)
218 return MODEACTION_DENY;
220 if (OnSet(source, chan, parameter) != MODEACTION_ALLOW)
221 return MODEACTION_DENY;
223 chan->SetMode(this, true);
225 // Handler might have changed the parameter internally
227 this->GetParameter(chan, parameter);
231 if (!chan->IsModeSet(this))
232 return MODEACTION_DENY;
233 this->OnUnsetInternal(source, chan);
234 chan->SetMode(this, false);
236 return MODEACTION_ALLOW;
239 ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Modes::Change& mcitem, bool SkipACL)
241 ModeType type = chan ? MODETYPE_CHANNEL : MODETYPE_USER;
243 ModeHandler* mh = mcitem.mh;
244 bool adding = mcitem.adding;
245 const bool needs_param = mh->NeedsParam(adding);
247 std::string& parameter = mcitem.param;
248 // crop mode parameter size to 250 characters
249 if (parameter.length() > 250 && adding)
250 parameter.erase(250);
252 ModResult MOD_RESULT;
253 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, parameter, adding));
255 if (IS_LOCAL(user) && (MOD_RESULT == MOD_RES_DENY))
256 return MODEACTION_DENY;
258 const char modechar = mh->GetModeChar();
260 if (chan && !SkipACL && (MOD_RESULT != MOD_RES_ALLOW))
262 MOD_RESULT = mh->AccessCheck(user, chan, parameter, adding);
264 if (MOD_RESULT == MOD_RES_DENY)
265 return MODEACTION_DENY;
266 if (MOD_RESULT == MOD_RES_PASSTHRU)
268 unsigned int neededrank = mh->GetLevelRequired(adding);
269 /* Compare our rank on the channel against the rank of the required prefix,
270 * allow if >= ours. Because mIRC and xchat throw a tizz if the modes shown
271 * in NAMES(X) are not in rank order, we know the most powerful mode is listed
272 * first, so we don't need to iterate, we just look up the first instead.
274 unsigned int ourrank = chan->GetPrefixValue(user);
275 if (ourrank < neededrank)
277 const PrefixMode* neededmh = NULL;
278 const PrefixModeList& prefixmodes = GetPrefixModes();
279 for (PrefixModeList::const_iterator i = prefixmodes.begin(); i != prefixmodes.end(); ++i)
281 const PrefixMode* const privmh = *i;
282 if (privmh->GetPrefixRank() >= neededrank)
284 // this mode is sufficient to allow this action
285 if (!neededmh || privmh->GetPrefixRank() < neededmh->GetPrefixRank())
290 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, chan->name, InspIRCd::Format("You must have channel %s access or above to %sset channel mode %c",
291 neededmh->name.c_str(), adding ? "" : "un", modechar));
293 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, chan->name, InspIRCd::Format("You cannot %sset channel mode %c", (adding ? "" : "un"), modechar));
294 return MODEACTION_DENY;
299 // Ask mode watchers whether this mode change is OK
300 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
301 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
303 ModeWatcher* mw = i->second;
304 if (mw->GetModeType() == type)
306 if (!mw->BeforeMode(user, targetuser, chan, parameter, adding))
307 return MODEACTION_DENY;
309 // A module whacked the parameter completely, and there was one. Abort.
310 if ((needs_param) && (parameter.empty()))
311 return MODEACTION_DENY;
315 if (IS_LOCAL(user) && !user->IsOper())
317 const std::bitset<64>& disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes;
318 if (disabled.test(modechar - 'A'))
320 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - %s mode %c has been locked by the administrator",
321 type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
322 return MODEACTION_DENY;
326 if ((adding) && (IS_LOCAL(user)) && (mh->NeedsOper()) && (!user->HasModePermission(mh)))
328 /* It's an oper only mode, and they don't have access to it. */
331 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Oper type %s does not have access to set %s mode %c",
332 user->oper->name.c_str(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
336 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Only operators may set %s mode %c",
337 type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
339 return MODEACTION_DENY;
342 /* Call the handler for the mode */
343 ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding);
345 if ((needs_param) && (parameter.empty()))
346 return MODEACTION_DENY;
348 if (ma != MODEACTION_ALLOW)
351 itpair = modewatchermap.equal_range(mh->name);
352 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
354 ModeWatcher* mw = i->second;
355 if (mw->GetModeType() == type)
356 mw->AfterMode(user, targetuser, chan, parameter, adding);
359 return MODEACTION_ALLOW;
362 void ModeParser::ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex, unsigned int endindex)
364 if (endindex > parameters.size())
365 endindex = parameters.size();
367 const std::string& mode_sequence = parameters[beginindex];
370 unsigned int param_at = beginindex+1;
372 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
374 unsigned char modechar = *letter;
375 if (modechar == '+' || modechar == '-')
377 adding = (modechar == '+');
381 ModeHandler *mh = this->FindMode(modechar, type);
384 /* No mode handler? Unknown mode character then. */
385 user->WriteNumeric(type == MODETYPE_CHANNEL ? ERR_UNKNOWNMODE : ERR_UNKNOWNSNOMASK, modechar, "is unknown mode char to me");
389 std::string parameter;
390 if ((mh->NeedsParam(adding)) && (param_at < endindex))
391 parameter = parameters[param_at++];
393 changelist.push(mh, adding, parameter);
397 static bool IsModeParamValid(User* user, Channel* targetchannel, User* targetuser, const Modes::Change& item)
399 // An empty parameter is never acceptable
400 if (item.param.empty())
402 item.mh->OnParameterMissing(user, targetuser, targetchannel);
406 // The parameter cannot begin with a ':' character or contain a space
407 if ((item.param[0] == ':') || (item.param.find(' ') != std::string::npos))
413 // Returns true if we should apply a merged mode, false if we should skip it
414 static bool ShouldApplyMergedMode(Channel* chan, Modes::Change& item)
416 ModeHandler* mh = item.mh;
417 if ((!chan) || (!chan->IsModeSet(mh)) || (mh->IsListMode()))
418 // Mode not set here or merge is not applicable, apply the incoming mode
421 // Mode handler decides
422 std::string ours = chan->GetModeParameter(mh);
423 return mh->ResolveModeConflict(item.param, ours, chan);
426 void ModeParser::Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags)
428 // Call ProcessSingle until the entire list is processed, but at least once to ensure
429 // LastParse and LastChangeList are cleared
430 unsigned int processed = 0;
433 unsigned int n = ProcessSingle(user, targetchannel, targetuser, changelist, flags, processed);
436 while (processed < changelist.size());
439 unsigned int ModeParser::ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags, unsigned int beginindex)
441 LastChangeList.clear();
443 unsigned int modes_processed = 0;
444 Modes::ChangeList::List& list = changelist.getlist();
445 for (Modes::ChangeList::List::iterator i = list.begin()+beginindex; i != list.end(); ++i)
449 Modes::Change& item = *i;
450 ModeHandler* mh = item.mh;
452 // If a mode change has been given for a mode that does not exist then reject
453 // it. This can happen when core_reloadmodule attempts to restore a mode that
458 // If the mode is supposed to have a parameter then we first take a look at item.param
459 // and, if we were asked to, also handle mode merges now
460 if (mh->NeedsParam(item.adding))
462 // Skip the mode if the parameter does not pass basic validation
463 if (!IsModeParamValid(user, targetchannel, targetuser, item))
466 // If this is a merge and we won we don't apply this mode
467 if ((flags & MODE_MERGE) && (!ShouldApplyMergedMode(targetchannel, item)))
471 ModeAction ma = TryMode(user, targetuser, targetchannel, item, (!(flags & MODE_CHECKACCESS)));
473 if (ma != MODEACTION_ALLOW)
476 LastChangeList.push(mh, item.adding, item.param);
478 if (LastChangeList.size() >= ServerInstance->Config->Limits.MaxModes)
480 /* mode sequence is getting too long */
485 if (!LastChangeList.empty())
487 ClientProtocol::Events::Mode modeevent(user, targetchannel, targetuser, LastChangeList);
490 targetchannel->Write(modeevent);
494 LocalUser* localtarget = IS_LOCAL(targetuser);
496 localtarget->Send(modeevent);
499 FOREACH_MOD(OnMode, (user, targetuser, targetchannel, LastChangeList, flags));
502 return modes_processed;
505 void ModeParser::ShowListModeList(User* user, Channel* chan, ModeHandler* mh)
508 ModResult MOD_RESULT;
509 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, "", true));
510 if (MOD_RESULT == MOD_RES_DENY)
515 // Ask mode watchers whether it's OK to show the list
516 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
517 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
519 ModeWatcher* mw = i->second;
520 if (mw->GetModeType() == MODETYPE_CHANNEL)
522 std::string dummyparam;
524 if (!mw->BeforeMode(user, NULL, chan, dummyparam, true))
526 // A mode watcher doesn't want us to show the list
534 mh->DisplayList(user, chan);
536 mh->DisplayEmptyList(user, chan);
540 void ModeParser::CleanMask(std::string &mask)
542 std::string::size_type pos_of_pling = mask.find_first_of('!');
543 std::string::size_type pos_of_at = mask.find_first_of('@');
544 std::string::size_type pos_of_dot = mask.find_first_of('.');
545 std::string::size_type pos_of_colons = mask.find("::"); /* Because ipv6 addresses are colon delimited -- double so it treats extban as nick */
547 if (mask.length() >= 2 && mask[1] == ':')
548 return; // if it's an extban, don't even try guess how it needs to be formed.
550 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
552 /* Just a nick, or just a host - or clearly ipv6 (starting with :) */
553 if ((pos_of_dot == std::string::npos) && (pos_of_colons == std::string::npos) && mask[0] != ':')
555 /* It has no '.' in it, it must be a nick. */
560 /* Got a dot in it? Has to be a host */
561 mask = "*!*@" + mask;
564 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
566 /* Has an @ but no !, its a user@host */
569 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
571 /* Has a ! but no @, it must be a nick!ident */
576 ModeHandler::Id ModeParser::AllocateModeId(ModeType mt)
578 for (ModeHandler::Id i = 0; i != MODEID_MAX; ++i)
580 if (!modehandlersbyid[mt][i])
584 throw ModuleException("Out of ModeIds");
587 void ModeParser::AddMode(ModeHandler* mh)
589 if (!ModeParser::IsModeChar(mh->GetModeChar()))
590 throw ModuleException("Invalid letter for mode " + mh->name);
592 /* A mode prefix of ',' is not acceptable, it would fuck up server to server.
593 * A mode prefix of ':' will fuck up both server to server, and client to server.
594 * A mode prefix of '#' will mess up /whois and /privmsg
596 PrefixMode* pm = mh->IsPrefixMode();
599 if ((pm->GetPrefix() > 126) || (pm->GetPrefix() == ',') || (pm->GetPrefix() == ':') || (pm->GetPrefix() == '#'))
600 throw ModuleException("Invalid prefix for mode " + mh->name);
602 if (FindPrefix(pm->GetPrefix()))
603 throw ModuleException("Prefix already exists for mode " + mh->name);
606 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
608 throw ModuleException("Letter is already in use for mode " + mh->name);
610 // The mode needs an id if it is either a user mode, a simple mode (flag) or a parameter mode.
611 // Otherwise (for listmodes and prefix modes) the id remains MODEID_MAX, which is invalid.
612 ModeHandler::Id modeid = MODEID_MAX;
613 if ((mh->GetModeType() == MODETYPE_USER) || (mh->IsParameterMode()) || (!mh->IsListMode()))
614 modeid = AllocateModeId(mh->GetModeType());
616 if (!modehandlersbyname[mh->GetModeType()].insert(std::make_pair(mh->name, mh)).second)
617 throw ModuleException("Mode name already in use: " + mh->name);
619 // Everything is fine, add the mode
621 // If we allocated an id for this mode then save it and put the mode handler into the slot
622 if (modeid != MODEID_MAX)
625 modehandlersbyid[mh->GetModeType()][modeid] = mh;
630 mhlist.prefix.push_back(pm);
631 else if (mh->IsListModeBase())
632 mhlist.list.push_back(mh->IsListModeBase());
635 bool ModeParser::DelMode(ModeHandler* mh)
637 if (!ModeParser::IsModeChar(mh->GetModeChar()))
640 ModeHandlerMap& mhmap = modehandlersbyname[mh->GetModeType()];
641 ModeHandlerMap::iterator mhmapit = mhmap.find(mh->name);
642 if ((mhmapit == mhmap.end()) || (mhmapit->second != mh))
645 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
649 /* Note: We can't stack here, as we have modes potentially being removed across many different channels.
650 * To stack here we have to make the algorithm slower. Discuss.
652 switch (mh->GetModeType())
656 const user_hash& users = ServerInstance->Users->GetUsers();
657 for (user_hash::const_iterator i = users.begin(); i != users.end(); )
659 User* user = i->second;
661 mh->RemoveMode(user);
665 case MODETYPE_CHANNEL:
667 const chan_hash& chans = ServerInstance->GetChans();
668 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); )
670 // The channel may not be in the hash after RemoveMode(), see m_permchannels
671 Channel* chan = i->second;
674 Modes::ChangeList changelist;
675 mh->RemoveMode(chan, changelist);
676 this->Process(ServerInstance->FakeClient, chan, NULL, changelist, MODE_LOCALONLY);
682 mhmap.erase(mhmapit);
683 if (mh->GetId() != MODEID_MAX)
684 modehandlersbyid[mh->GetModeType()][mh->GetId()] = NULL;
686 if (mh->IsPrefixMode())
687 mhlist.prefix.erase(std::find(mhlist.prefix.begin(), mhlist.prefix.end(), mh->IsPrefixMode()));
688 else if (mh->IsListModeBase())
689 mhlist.list.erase(std::find(mhlist.list.begin(), mhlist.list.end(), mh->IsListModeBase()));
693 ModeHandler* ModeParser::FindMode(const std::string& modename, ModeType mt)
695 ModeHandlerMap& mhmap = modehandlersbyname[mt];
696 ModeHandlerMap::const_iterator it = mhmap.find(modename);
697 if (it != mhmap.end())
703 ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt)
705 if (!ModeParser::IsModeChar(modeletter))
708 return modehandlers[mt][modeletter-65];
711 PrefixMode* ModeParser::FindPrefixMode(unsigned char modeletter)
713 ModeHandler* mh = FindMode(modeletter, MODETYPE_CHANNEL);
716 return mh->IsPrefixMode();
719 PrefixMode* ModeParser::FindPrefix(unsigned const char pfxletter)
721 const PrefixModeList& list = GetPrefixModes();
722 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
725 if (pm->GetPrefix() == pfxletter)
731 std::string ModeParser::GiveModeList(ModeType mt)
733 std::string type1; /* Listmodes EXCEPT those with a prefix */
734 std::string type2; /* Modes that take a param when adding or removing */
735 std::string type3; /* Modes that only take a param when adding */
736 std::string type4; /* Modes that dont take a param */
738 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
740 ModeHandler* mh = modehandlers[mt][mode-65];
741 /* One parameter when adding */
744 if (mh->NeedsParam(true))
746 PrefixMode* pm = mh->IsPrefixMode();
747 if ((mh->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0)))
749 type1 += mh->GetModeChar();
753 /* ... and one parameter when removing */
754 if (mh->NeedsParam(false))
756 /* But not a list mode */
759 type2 += mh->GetModeChar();
764 /* No parameters when removing */
765 type3 += mh->GetModeChar();
771 type4 += mh->GetModeChar();
776 return type1 + "," + type2 + "," + type3 + "," + type4;
779 struct PrefixModeSorter
781 bool operator()(PrefixMode* lhs, PrefixMode* rhs)
783 return lhs->GetPrefixRank() < rhs->GetPrefixRank();
787 std::string ModeParser::BuildPrefixes(bool lettersAndModes)
789 std::string mletters;
790 std::string mprefixes;
791 std::vector<PrefixMode*> prefixes;
793 const PrefixModeList& list = GetPrefixModes();
794 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
798 prefixes.push_back(pm);
801 std::sort(prefixes.begin(), prefixes.end(), PrefixModeSorter());
802 for (std::vector<PrefixMode*>::const_reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); ++n)
804 mletters += (*n)->GetPrefix();
805 mprefixes += (*n)->GetModeChar();
808 return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters;
811 void ModeParser::AddModeWatcher(ModeWatcher* mw)
813 modewatchermap.insert(std::make_pair(mw->GetModeName(), mw));
816 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
818 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mw->GetModeName());
819 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
823 modewatchermap.erase(i);
831 void ModeHandler::RemoveMode(User* user)
833 // Remove the mode if it's set on the user
834 if (user->IsModeSet(this->GetModeChar()))
836 Modes::ChangeList changelist;
837 changelist.push_remove(this);
838 ServerInstance->Modes->Process(ServerInstance->FakeClient, NULL, user, changelist, ModeParser::MODE_LOCALONLY);
842 void ModeHandler::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
844 if (channel->IsModeSet(this))
846 if (this->NeedsParam(false))
847 // Removing this mode requires a parameter
848 changelist.push_remove(this, channel->GetModeParameter(this));
850 changelist.push_remove(this);
854 void PrefixMode::RemoveMode(Channel* chan, Modes::ChangeList& changelist)
856 const Channel::MemberMap& userlist = chan->GetUsers();
857 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
859 if (i->second->HasMode(this))
860 changelist.push_remove(this, i->first->nick);
864 bool ModeParser::IsModeChar(char chr)
866 return ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z'));
869 ModeParser::ModeParser()
871 /* Clear mode handler list */
872 memset(modehandlers, 0, sizeof(modehandlers));
873 memset(modehandlersbyid, 0, sizeof(modehandlersbyid));
876 ModeParser::~ModeParser()