2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
6 * Copyright (C) 2016-2019 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2012-2016, 2018 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2012 Shawn Smith <ShawnSmith0828@gmail.com>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
12 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
13 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
14 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
15 * Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
17 * This file is part of InspIRCd. InspIRCd is free software: you can
18 * redistribute it and/or modify it under the terms of the GNU General Public
19 * License as published by the Free Software Foundation, version 2.
21 * This program is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type, Class mclass)
34 : ServiceProvider(Creator, Name, SERVICE_MODE)
35 , modeid(ModeParser::MODEID_MAX)
36 , parameters_taken(Params)
42 , ranktoset(HALFOP_VALUE)
43 , ranktounset(HALFOP_VALUE)
47 CullResult ModeHandler::cull()
50 ServerInstance->Modes->DelMode(this);
51 return classbase::cull();
54 ModeHandler::~ModeHandler()
58 bool ModeHandler::NeedsParam(bool adding) const
60 switch (parameters_taken)
72 std::string ModeHandler::GetUserParameter(const User* user) const
77 ModResult ModeHandler::AccessCheck(User*, Channel*, std::string &, bool)
79 return MOD_RES_PASSTHRU;
82 ModeAction ModeHandler::OnModeChange(User*, User*, Channel*, std::string&, bool)
84 return MODEACTION_DENY;
87 void ModeHandler::DisplayList(User*, Channel*)
91 void ModeHandler::DisplayEmptyList(User*, Channel*)
95 void ModeHandler::OnParameterMissing(User* user, User* dest, Channel* channel)
97 std::string message = InspIRCd::Format("You must specify a parameter for the %s mode.", name.c_str());
99 message.append(InspIRCd::Format(" Syntax: %s.", syntax.c_str()));
102 user->WriteNumeric(Numerics::InvalidModeParameter(channel, this, "*", message));
104 user->WriteNumeric(Numerics::InvalidModeParameter(dest, this, "*", message));
107 void ModeHandler::OnParameterInvalid(User* user, Channel* targetchannel, User* targetuser, const std::string& parameter)
110 user->WriteNumeric(Numerics::InvalidModeParameter(targetchannel, this, "*"));
112 user->WriteNumeric(Numerics::InvalidModeParameter(targetuser, this, "*"));
115 bool ModeHandler::ResolveModeConflict(std::string& theirs, const std::string& ours, Channel*)
117 return (theirs < ours);
120 void ModeHandler::RegisterService()
122 ServerInstance->Modes.AddMode(this);
123 ServerInstance->Modules.AddReferent((GetModeType() == MODETYPE_CHANNEL ? "mode/" : "umode/") + name, this);
126 ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
128 /* We're either trying to add a mode we already have or
129 remove a mode we don't have, deny. */
130 if (dest->IsModeSet(this) == adding)
131 return MODEACTION_DENY;
133 /* adding will be either true or false, depending on if we
134 are adding or removing the mode, since we already checked
135 to make sure we aren't adding a mode we have or that we
136 aren't removing a mode we don't have, we don't have to do any
137 other checks here to see if it's true or false, just add or
139 dest->SetMode(this, adding);
141 return MODEACTION_ALLOW;
145 ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
147 /* We're either trying to add a mode we already have or
148 remove a mode we don't have, deny. */
149 if (channel->IsModeSet(this) == adding)
150 return MODEACTION_DENY;
152 /* adding will be either true or false, depending on if we
153 are adding or removing the mode, since we already checked
154 to make sure we aren't adding a mode we have or that we
155 aren't removing a mode we don't have, we don't have to do any
156 other checks here to see if it's true or false, just add or
158 channel->SetMode(this, adding);
160 return MODEACTION_ALLOW;
163 ModeWatcher::ModeWatcher(Module* Creator, const std::string& modename, ModeType type)
164 : mode(modename), m_type(type), creator(Creator)
166 ServerInstance->Modes->AddModeWatcher(this);
169 ModeWatcher::~ModeWatcher()
171 ServerInstance->Modes->DelModeWatcher(this);
174 bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool)
179 void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool)
183 PrefixMode::PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, unsigned int Rank, char PrefixChar)
184 : ModeHandler(Creator, Name, ModeLetter, PARAM_ALWAYS, MODETYPE_CHANNEL, MC_PREFIX)
193 ModResult PrefixMode::AccessCheck(User* src, Channel*, std::string& value, bool adding)
195 if (!adding && src->nick == value && selfremove)
196 return MOD_RES_ALLOW;
197 return MOD_RES_PASSTHRU;
200 ModeAction PrefixMode::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
203 if (IS_LOCAL(source))
204 target = ServerInstance->FindNickOnly(parameter);
206 target = ServerInstance->FindNick(parameter);
210 source->WriteNumeric(Numerics::NoSuchNick(parameter));
211 return MODEACTION_DENY;
214 Membership* memb = chan->GetUser(target);
216 return MODEACTION_DENY;
218 parameter = target->nick;
219 return (memb->SetPrefix(this, adding) ? MODEACTION_ALLOW : MODEACTION_DENY);
222 void PrefixMode::Update(unsigned int rank, unsigned int setrank, unsigned int unsetrank, bool selfrm)
226 ranktounset = unsetrank;
230 ModeAction ParamModeBase::OnModeChange(User* source, User*, Channel* chan, std::string& parameter, bool adding)
234 if (chan->GetModeParameter(this) == parameter)
235 return MODEACTION_DENY;
237 if (OnSet(source, chan, parameter) != MODEACTION_ALLOW)
238 return MODEACTION_DENY;
240 chan->SetMode(this, true);
242 // Handler might have changed the parameter internally
244 this->GetParameter(chan, parameter);
248 if (!chan->IsModeSet(this))
249 return MODEACTION_DENY;
250 this->OnUnsetInternal(source, chan);
251 chan->SetMode(this, false);
253 return MODEACTION_ALLOW;
256 ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, Modes::Change& mcitem, bool SkipACL)
258 ModeType type = chan ? MODETYPE_CHANNEL : MODETYPE_USER;
260 ModeHandler* mh = mcitem.mh;
261 bool adding = mcitem.adding;
262 const bool needs_param = mh->NeedsParam(adding);
264 std::string& parameter = mcitem.param;
265 // crop mode parameter size to MODE_PARAM_MAX characters
266 if (parameter.length() > MODE_PARAM_MAX && adding)
267 parameter.erase(MODE_PARAM_MAX);
269 ModResult MOD_RESULT;
270 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, parameter, adding));
272 if (IS_LOCAL(user) && (MOD_RESULT == MOD_RES_DENY))
273 return MODEACTION_DENY;
275 const char modechar = mh->GetModeChar();
277 if (chan && !SkipACL && (MOD_RESULT != MOD_RES_ALLOW))
279 MOD_RESULT = mh->AccessCheck(user, chan, parameter, adding);
281 if (MOD_RESULT == MOD_RES_DENY)
282 return MODEACTION_DENY;
283 if (MOD_RESULT == MOD_RES_PASSTHRU)
285 unsigned int neededrank = mh->GetLevelRequired(adding);
286 /* Compare our rank on the channel against the rank of the required prefix,
287 * allow if >= ours. Because mIRC and xchat throw a tizz if the modes shown
288 * in NAMES(X) are not in rank order, we know the most powerful mode is listed
289 * first, so we don't need to iterate, we just look up the first instead.
291 unsigned int ourrank = chan->GetPrefixValue(user);
292 if (ourrank < neededrank)
294 const PrefixMode* neededmh = NULL;
295 const PrefixModeList& prefixmodes = GetPrefixModes();
296 for (PrefixModeList::const_iterator i = prefixmodes.begin(); i != prefixmodes.end(); ++i)
298 const PrefixMode* const privmh = *i;
299 if (privmh->GetPrefixRank() >= neededrank)
301 // this mode is sufficient to allow this action
302 if (!neededmh || privmh->GetPrefixRank() < neededmh->GetPrefixRank())
307 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, chan->name, InspIRCd::Format("You must have channel %s access or above to %sset channel mode %c",
308 neededmh->name.c_str(), adding ? "" : "un", modechar));
310 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, chan->name, InspIRCd::Format("You cannot %sset channel mode %c", (adding ? "" : "un"), modechar));
311 return MODEACTION_DENY;
316 // Ask mode watchers whether this mode change is OK
317 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
318 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
320 ModeWatcher* mw = i->second;
321 if (mw->GetModeType() == type)
323 if (!mw->BeforeMode(user, targetuser, chan, parameter, adding))
324 return MODEACTION_DENY;
326 // A module whacked the parameter completely, and there was one. Abort.
327 if ((needs_param) && (parameter.empty()))
328 return MODEACTION_DENY;
332 if ((chan || (!chan && adding)) && IS_LOCAL(user) && mh->NeedsOper() && !user->HasModePermission(mh))
334 /* It's an oper only mode, and they don't have access to it. */
337 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Oper type %s does not have access to %sset %s mode %c",
338 user->oper->name.c_str(), adding ? "" : "un", type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
342 user->WriteNumeric(ERR_NOPRIVILEGES, InspIRCd::Format("Permission Denied - Only operators may %sset %s mode %c",
343 adding ? "" : "un", type == MODETYPE_CHANNEL ? "channel" : "user", modechar));
345 return MODEACTION_DENY;
348 /* Call the handler for the mode */
349 ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding);
351 if ((needs_param) && (parameter.empty()))
352 return MODEACTION_DENY;
354 if (ma != MODEACTION_ALLOW)
357 itpair = modewatchermap.equal_range(mh->name);
358 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
360 ModeWatcher* mw = i->second;
361 if (mw->GetModeType() == type)
362 mw->AfterMode(user, targetuser, chan, parameter, adding);
365 return MODEACTION_ALLOW;
368 void ModeParser::ModeParamsToChangeList(User* user, ModeType type, const std::vector<std::string>& parameters, Modes::ChangeList& changelist, unsigned int beginindex, unsigned int endindex)
370 if (endindex > parameters.size())
371 endindex = parameters.size();
373 const std::string& mode_sequence = parameters[beginindex];
376 unsigned int param_at = beginindex+1;
378 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
380 unsigned char modechar = *letter;
381 if (modechar == '+' || modechar == '-')
383 adding = (modechar == '+');
387 ModeHandler *mh = this->FindMode(modechar, type);
390 /* No mode handler? Unknown mode character then. */
391 user->WriteNumeric(type == MODETYPE_CHANNEL ? ERR_UNKNOWNMODE : ERR_UNKNOWNSNOMASK, modechar, "is an unknown mode character");
395 std::string parameter;
396 if ((mh->NeedsParam(adding)) && (param_at < endindex))
397 parameter = parameters[param_at++];
399 changelist.push(mh, adding, parameter);
403 static bool IsModeParamValid(User* user, Channel* targetchannel, User* targetuser, const Modes::Change& item)
405 // An empty parameter is never acceptable
406 if (item.param.empty())
408 item.mh->OnParameterMissing(user, targetuser, targetchannel);
412 // The parameter cannot begin with a ':' character or contain a space
413 if ((item.param[0] == ':') || (item.param.find(' ') != std::string::npos))
415 item.mh->OnParameterInvalid(user, targetchannel, targetuser, item.param);
422 // Returns true if we should apply a merged mode, false if we should skip it
423 static bool ShouldApplyMergedMode(Channel* chan, Modes::Change& item)
425 ModeHandler* mh = item.mh;
426 if ((!chan) || (!chan->IsModeSet(mh)) || (mh->IsListMode()))
427 // Mode not set here or merge is not applicable, apply the incoming mode
430 // Mode handler decides
431 std::string ours = chan->GetModeParameter(mh);
432 return mh->ResolveModeConflict(item.param, ours, chan);
435 void ModeParser::Process(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags)
437 // Call ProcessSingle until the entire list is processed, but at least once to ensure
438 // LastParse and LastChangeList are cleared
439 unsigned int processed = 0;
442 unsigned int n = ProcessSingle(user, targetchannel, targetuser, changelist, flags, processed);
445 while (processed < changelist.size());
448 unsigned int ModeParser::ProcessSingle(User* user, Channel* targetchannel, User* targetuser, Modes::ChangeList& changelist, ModeProcessFlag flags, unsigned int beginindex)
450 LastChangeList.clear();
452 unsigned int modes_processed = 0;
453 Modes::ChangeList::List& list = changelist.getlist();
454 for (Modes::ChangeList::List::iterator i = list.begin()+beginindex; i != list.end(); ++i)
458 Modes::Change& item = *i;
459 ModeHandler* mh = item.mh;
461 // If a mode change has been given for a mode that does not exist then reject
462 // it. This can happen when core_reloadmodule attempts to restore a mode that
467 // If the mode is supposed to have a parameter then we first take a look at item.param
468 // and, if we were asked to, also handle mode merges now
469 if (mh->NeedsParam(item.adding))
471 // Skip the mode if the parameter does not pass basic validation
472 if (!IsModeParamValid(user, targetchannel, targetuser, item))
475 // If this is a merge and we won we don't apply this mode
476 if ((flags & MODE_MERGE) && (!ShouldApplyMergedMode(targetchannel, item)))
480 ModeAction ma = TryMode(user, targetuser, targetchannel, item, (!(flags & MODE_CHECKACCESS)));
482 if (ma != MODEACTION_ALLOW)
485 LastChangeList.push(mh, item.adding, item.param);
487 if (LastChangeList.size() >= ServerInstance->Config->Limits.MaxModes)
489 /* mode sequence is getting too long */
494 if (!LastChangeList.empty())
496 ClientProtocol::Events::Mode modeevent(user, targetchannel, targetuser, LastChangeList);
499 targetchannel->Write(modeevent);
503 LocalUser* localtarget = IS_LOCAL(targetuser);
505 localtarget->Send(modeevent);
508 FOREACH_MOD(OnMode, (user, targetuser, targetchannel, LastChangeList, flags));
511 return modes_processed;
514 void ModeParser::ShowListModeList(User* user, Channel* chan, ModeHandler* mh)
517 ModResult MOD_RESULT;
518 FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, mh, "", true));
519 if (MOD_RESULT == MOD_RES_DENY)
524 // Ask mode watchers whether it's OK to show the list
525 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mh->name);
526 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
528 ModeWatcher* mw = i->second;
529 if (mw->GetModeType() == MODETYPE_CHANNEL)
531 std::string dummyparam;
533 if (!mw->BeforeMode(user, NULL, chan, dummyparam, true))
535 // A mode watcher doesn't want us to show the list
543 mh->DisplayList(user, chan);
545 mh->DisplayEmptyList(user, chan);
549 void ModeParser::CleanMask(std::string &mask)
551 std::string::size_type pos_of_pling = mask.find_first_of('!');
552 std::string::size_type pos_of_at = mask.find_first_of('@');
553 std::string::size_type pos_of_dot = mask.find_first_of('.');
554 std::string::size_type pos_of_colons = mask.find("::"); /* Because ipv6 addresses are colon delimited -- double so it treats extban as nick */
556 if (mask.length() >= 2 && mask[1] == ':')
557 return; // if it's an extban, don't even try guess how it needs to be formed.
559 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
561 /* Just a nick, or just a host - or clearly ipv6 (starting with :) */
562 if ((pos_of_dot == std::string::npos) && (pos_of_colons == std::string::npos) && mask[0] != ':')
564 /* It has no '.' in it, it must be a nick. */
569 /* Got a dot in it? Has to be a host */
570 mask = "*!*@" + mask;
573 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
575 /* Has an @ but no !, its a user@host */
578 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
580 /* Has a ! but no @, it must be a nick!ident */
585 ModeHandler::Id ModeParser::AllocateModeId(ModeType mt)
587 for (ModeHandler::Id i = 0; i != MODEID_MAX; ++i)
589 if (!modehandlersbyid[mt][i])
593 throw ModuleException("Out of ModeIds");
596 void ModeParser::AddMode(ModeHandler* mh)
598 if (!ModeParser::IsModeChar(mh->GetModeChar()))
599 throw ModuleException(InspIRCd::Format("Mode letter for %s is invalid: %c",
600 mh->name.c_str(), mh->GetModeChar()));
602 /* A mode prefix of ',' is not acceptable, it would fuck up server to server.
603 * A mode prefix of ':' will fuck up both server to server, and client to server.
604 * A mode prefix of '#' will mess up /whois and /privmsg
606 PrefixMode* pm = mh->IsPrefixMode();
609 if ((pm->GetPrefix() > 126) || (pm->GetPrefix() == ',') || (pm->GetPrefix() == ':') || (pm->GetPrefix() == '#'))
610 throw ModuleException(InspIRCd::Format("Mode prefix for %s is invalid: %c",
611 mh->name.c_str(), pm->GetPrefix()));
613 PrefixMode* otherpm = FindPrefix(pm->GetPrefix());
615 throw ModuleException(InspIRCd::Format("Mode prefix for %s already used by %s from %s: %c",
616 mh->name.c_str(), otherpm->name.c_str(), otherpm->creator->ModuleSourceFile.c_str(), pm->GetPrefix()));
619 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
621 throw ModuleException(InspIRCd::Format("Mode letter for %s already used by %s from %s: %c",
622 mh->name.c_str(), slot->name.c_str(), slot->creator->ModuleSourceFile.c_str(), mh->GetModeChar()));
624 // The mode needs an id if it is either a user mode, a simple mode (flag) or a parameter mode.
625 // Otherwise (for listmodes and prefix modes) the id remains MODEID_MAX, which is invalid.
626 ModeHandler::Id modeid = MODEID_MAX;
627 if ((mh->GetModeType() == MODETYPE_USER) || (mh->IsParameterMode()) || (!mh->IsListMode()))
628 modeid = AllocateModeId(mh->GetModeType());
630 std::pair<ModeHandlerMap::iterator, bool> res = modehandlersbyname[mh->GetModeType()].insert(std::make_pair(mh->name, mh));
633 ModeHandler* othermh = res.first->second;
634 throw ModuleException(InspIRCd::Format("Mode name %s already used by %c from %s",
635 mh->name.c_str(), othermh->GetModeChar(), othermh->creator->ModuleSourceFile.c_str()));
638 // Everything is fine, add the mode
640 // If we allocated an id for this mode then save it and put the mode handler into the slot
641 if (modeid != MODEID_MAX)
644 modehandlersbyid[mh->GetModeType()][modeid] = mh;
649 mhlist.prefix.push_back(pm);
650 else if (mh->IsListModeBase())
651 mhlist.list.push_back(mh->IsListModeBase());
654 bool ModeParser::DelMode(ModeHandler* mh)
656 if (!ModeParser::IsModeChar(mh->GetModeChar()))
659 ModeHandlerMap& mhmap = modehandlersbyname[mh->GetModeType()];
660 ModeHandlerMap::iterator mhmapit = mhmap.find(mh->name);
661 if ((mhmapit == mhmap.end()) || (mhmapit->second != mh))
664 ModeHandler*& slot = modehandlers[mh->GetModeType()][mh->GetModeChar()-65];
668 /* Note: We can't stack here, as we have modes potentially being removed across many different channels.
669 * To stack here we have to make the algorithm slower. Discuss.
671 switch (mh->GetModeType())
675 const user_hash& users = ServerInstance->Users->GetUsers();
676 for (user_hash::const_iterator i = users.begin(); i != users.end(); )
678 User* user = i->second;
680 mh->RemoveMode(user);
684 case MODETYPE_CHANNEL:
686 const chan_hash& chans = ServerInstance->GetChans();
687 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); )
689 // The channel may not be in the hash after RemoveMode(), see m_permchannels
690 Channel* chan = i->second;
693 Modes::ChangeList changelist;
694 mh->RemoveMode(chan, changelist);
695 this->Process(ServerInstance->FakeClient, chan, NULL, changelist, MODE_LOCALONLY);
701 mhmap.erase(mhmapit);
702 if (mh->GetId() != MODEID_MAX)
703 modehandlersbyid[mh->GetModeType()][mh->GetId()] = NULL;
705 if (mh->IsPrefixMode())
706 mhlist.prefix.erase(std::find(mhlist.prefix.begin(), mhlist.prefix.end(), mh->IsPrefixMode()));
707 else if (mh->IsListModeBase())
708 mhlist.list.erase(std::find(mhlist.list.begin(), mhlist.list.end(), mh->IsListModeBase()));
712 ModeHandler* ModeParser::FindMode(const std::string& modename, ModeType mt)
714 ModeHandlerMap& mhmap = modehandlersbyname[mt];
715 ModeHandlerMap::const_iterator it = mhmap.find(modename);
716 if (it != mhmap.end())
722 ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt)
724 if (!ModeParser::IsModeChar(modeletter))
727 return modehandlers[mt][modeletter-65];
730 PrefixMode* ModeParser::FindPrefixMode(unsigned char modeletter)
732 ModeHandler* mh = FindMode(modeletter, MODETYPE_CHANNEL);
735 return mh->IsPrefixMode();
738 PrefixMode* ModeParser::FindPrefix(unsigned const char pfxletter)
740 const PrefixModeList& list = GetPrefixModes();
741 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
744 if (pm->GetPrefix() == pfxletter)
750 std::string ModeParser::GiveModeList(ModeType mt)
752 std::string type1; /* Listmodes EXCEPT those with a prefix */
753 std::string type2; /* Modes that take a param when adding or removing */
754 std::string type3; /* Modes that only take a param when adding */
755 std::string type4; /* Modes that dont take a param */
757 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
759 ModeHandler* mh = modehandlers[mt][mode-65];
760 /* One parameter when adding */
763 if (mh->NeedsParam(true))
765 PrefixMode* pm = mh->IsPrefixMode();
766 if ((mh->IsListMode()) && ((!pm) || (pm->GetPrefix() == 0)))
768 type1 += mh->GetModeChar();
772 /* ... and one parameter when removing */
773 if (mh->NeedsParam(false))
775 /* But not a list mode */
778 type2 += mh->GetModeChar();
783 /* No parameters when removing */
784 type3 += mh->GetModeChar();
790 type4 += mh->GetModeChar();
795 return type1 + "," + type2 + "," + type3 + "," + type4;
798 struct PrefixModeSorter
800 bool operator()(PrefixMode* lhs, PrefixMode* rhs)
802 return lhs->GetPrefixRank() < rhs->GetPrefixRank();
806 std::string ModeParser::BuildPrefixes(bool lettersAndModes)
808 std::string mletters;
809 std::string mprefixes;
810 std::vector<PrefixMode*> prefixes;
812 const PrefixModeList& list = GetPrefixModes();
813 for (PrefixModeList::const_iterator i = list.begin(); i != list.end(); ++i)
817 prefixes.push_back(pm);
820 std::sort(prefixes.begin(), prefixes.end(), PrefixModeSorter());
821 for (std::vector<PrefixMode*>::const_reverse_iterator n = prefixes.rbegin(); n != prefixes.rend(); ++n)
823 mletters += (*n)->GetPrefix();
824 mprefixes += (*n)->GetModeChar();
827 return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters;
830 void ModeParser::AddModeWatcher(ModeWatcher* mw)
832 modewatchermap.insert(std::make_pair(mw->GetModeName(), mw));
835 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
837 std::pair<ModeWatcherMap::iterator, ModeWatcherMap::iterator> itpair = modewatchermap.equal_range(mw->GetModeName());
838 for (ModeWatcherMap::iterator i = itpair.first; i != itpair.second; ++i)
842 modewatchermap.erase(i);
850 void ModeHandler::RemoveMode(User* user)
852 // Remove the mode if it's set on the user
853 if (user->IsModeSet(this->GetModeChar()))
855 Modes::ChangeList changelist;
856 changelist.push_remove(this);
857 ServerInstance->Modes->Process(ServerInstance->FakeClient, NULL, user, changelist, ModeParser::MODE_LOCALONLY);
861 void ModeHandler::RemoveMode(Channel* channel, Modes::ChangeList& changelist)
863 if (channel->IsModeSet(this))
865 if (this->NeedsParam(false))
866 // Removing this mode requires a parameter
867 changelist.push_remove(this, channel->GetModeParameter(this));
869 changelist.push_remove(this);
873 void PrefixMode::RemoveMode(Channel* chan, Modes::ChangeList& changelist)
875 const Channel::MemberMap& userlist = chan->GetUsers();
876 for (Channel::MemberMap::const_iterator i = userlist.begin(); i != userlist.end(); ++i)
878 if (i->second->HasMode(this))
879 changelist.push_remove(this, i->first->nick);
883 bool ModeParser::IsModeChar(char chr)
885 return ((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z'));
888 ModeParser::ModeParser()
890 /* Clear mode handler list */
891 memset(modehandlers, 0, sizeof(modehandlers));
892 memset(modehandlersbyid, 0, sizeof(modehandlersbyid));
895 ModeParser::~ModeParser()