From 484160eb4ed560eeda97c94a42dd8e31431ab251 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Wed, 24 Jan 2024 12:34:03 +0000 Subject: Shuffle modules around a bit. --- modules/chanserv/chanserv.cpp | 481 ++++++++++++ modules/chanserv/cs_access.cpp | 939 +++++++++++++++++++++++ modules/chanserv/cs_akick.cpp | 585 +++++++++++++++ modules/chanserv/cs_ban.cpp | 254 +++++++ modules/chanserv/cs_clone.cpp | 262 +++++++ modules/chanserv/cs_drop.cpp | 97 +++ modules/chanserv/cs_enforce.cpp | 279 +++++++ modules/chanserv/cs_entrymsg.cpp | 294 ++++++++ modules/chanserv/cs_fantasy_stats.cpp | 180 +++++ modules/chanserv/cs_fantasy_top.cpp | 194 +++++ modules/chanserv/cs_flags.cpp | 518 +++++++++++++ modules/chanserv/cs_getkey.cpp | 75 ++ modules/chanserv/cs_info.cpp | 100 +++ modules/chanserv/cs_invite.cpp | 113 +++ modules/chanserv/cs_kick.cpp | 149 ++++ modules/chanserv/cs_list.cpp | 267 +++++++ modules/chanserv/cs_log.cpp | 404 ++++++++++ modules/chanserv/cs_mode.cpp | 1066 ++++++++++++++++++++++++++ modules/chanserv/cs_register.cpp | 126 ++++ modules/chanserv/cs_seen.cpp | 461 ++++++++++++ modules/chanserv/cs_set.cpp | 1326 +++++++++++++++++++++++++++++++++ modules/chanserv/cs_set_misc.cpp | 222 ++++++ modules/chanserv/cs_status.cpp | 125 ++++ modules/chanserv/cs_statusupdate.cpp | 68 ++ modules/chanserv/cs_suspend.cpp | 296 ++++++++ modules/chanserv/cs_sync.cpp | 68 ++ modules/chanserv/cs_topic.cpp | 273 +++++++ modules/chanserv/cs_unban.cpp | 132 ++++ modules/chanserv/cs_updown.cpp | 245 ++++++ modules/chanserv/cs_xop.cpp | 651 ++++++++++++++++ 30 files changed, 10250 insertions(+) create mode 100644 modules/chanserv/chanserv.cpp create mode 100644 modules/chanserv/cs_access.cpp create mode 100644 modules/chanserv/cs_akick.cpp create mode 100644 modules/chanserv/cs_ban.cpp create mode 100644 modules/chanserv/cs_clone.cpp create mode 100644 modules/chanserv/cs_drop.cpp create mode 100644 modules/chanserv/cs_enforce.cpp create mode 100644 modules/chanserv/cs_entrymsg.cpp create mode 100644 modules/chanserv/cs_fantasy_stats.cpp create mode 100644 modules/chanserv/cs_fantasy_top.cpp create mode 100644 modules/chanserv/cs_flags.cpp create mode 100644 modules/chanserv/cs_getkey.cpp create mode 100644 modules/chanserv/cs_info.cpp create mode 100644 modules/chanserv/cs_invite.cpp create mode 100644 modules/chanserv/cs_kick.cpp create mode 100644 modules/chanserv/cs_list.cpp create mode 100644 modules/chanserv/cs_log.cpp create mode 100644 modules/chanserv/cs_mode.cpp create mode 100644 modules/chanserv/cs_register.cpp create mode 100644 modules/chanserv/cs_seen.cpp create mode 100644 modules/chanserv/cs_set.cpp create mode 100644 modules/chanserv/cs_set_misc.cpp create mode 100644 modules/chanserv/cs_status.cpp create mode 100644 modules/chanserv/cs_statusupdate.cpp create mode 100644 modules/chanserv/cs_suspend.cpp create mode 100644 modules/chanserv/cs_sync.cpp create mode 100644 modules/chanserv/cs_topic.cpp create mode 100644 modules/chanserv/cs_unban.cpp create mode 100644 modules/chanserv/cs_updown.cpp create mode 100644 modules/chanserv/cs_xop.cpp (limited to 'modules/chanserv') diff --git a/modules/chanserv/chanserv.cpp b/modules/chanserv/chanserv.cpp new file mode 100644 index 000000000..c47f2d77f --- /dev/null +++ b/modules/chanserv/chanserv.cpp @@ -0,0 +1,481 @@ +/* ChanServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" +#include "modules/cs_mode.h" + +inline static Anope::string BotModes() +{ + return Config->GetModule("botserv")->Get("botmodes", + Config->GetModule("chanserv")->Get("botmodes", "o") + ); +} + +class ChanServCore final + : public Module + , public ChanServService +{ + Reference ChanServ; + std::vector defaults; + ExtensibleItem inhabit; + ExtensibleRef persist; + bool always_lower = false; + +public: + ChanServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, PSEUDOCLIENT | VENDOR), + ChanServService(this), inhabit(this, "inhabit"), persist("PERSIST") + { + } + + void Hold(Channel *c) override + { + /** A timer used to keep the BotServ bot/ChanServ in the channel + * after kicking the last user in a channel + */ + class ChanServTimer final + : public Timer + { + Reference &ChanServ; + ExtensibleItem &inhabit; + Reference c; + + public: + /** Constructor + * @param chan The channel + */ + ChanServTimer(Reference &cs, ExtensibleItem &i, Module *m, Channel *chan) : Timer(m, Config->GetModule(m)->Get("inhabit", "15s")), ChanServ(cs), inhabit(i), c(chan) + { + if (!ChanServ || !c) + return; + inhabit.Set(c, true); + if (!c->ci || !c->ci->bi) + ChanServ->Join(c); + else if (!c->FindUser(c->ci->bi)) + c->ci->bi->Join(c); + + /* Set +ntsi to prevent rejoin */ + c->SetMode(NULL, "NOEXTERNAL"); + c->SetMode(NULL, "TOPIC"); + c->SetMode(NULL, "SECRET"); + c->SetMode(NULL, "INVITE"); + } + + /** Called when the delay is up + * @param The current time + */ + void Tick(time_t) override + { + if (!c) + return; + + /* In the event we don't part */ + c->RemoveMode(NULL, "SECRET"); + c->RemoveMode(NULL, "INVITE"); + + inhabit.Unset(c); /* now we're done changing modes, unset inhabit */ + + if (!c->ci || !c->ci->bi) + { + if (ChanServ) + ChanServ->Part(c); + } + /* If someone has rejoined this channel in the meantime, don't part the bot */ + else if (c->users.size() <= 1) + c->ci->bi->Part(c); + } + }; + + if (inhabit.HasExt(c)) + return; + + new ChanServTimer(ChanServ, inhabit, this->owner, c); + } + + void OnReload(Configuration::Conf *conf) override + { + const Anope::string &channick = conf->GetModule(this)->Get("client"); + + if (channick.empty()) + throw ConfigException(Module::name + ": must be defined"); + + BotInfo *bi = BotInfo::Find(channick, true); + if (!bi) + throw ConfigException(Module::name + ": no bot named " + channick); + + ChanServ = bi; + + spacesepstream(conf->GetModule(this)->Get("defaults", "keeptopic peace cs_secure securefounder signkick")).GetTokens(defaults); + if (defaults.empty()) + { + defaults.emplace_back("KEEPTOPIC"); + defaults.emplace_back("PEACE"); + defaults.emplace_back("CS_SECURE"); + defaults.emplace_back("SECUREFOUNDER"); + defaults.emplace_back("SIGNKICK"); + } + else if (defaults[0].equals_ci("none")) + defaults.clear(); + + always_lower = conf->GetModule(this)->Get("always_lower_ts"); + } + + void OnBotDelete(BotInfo *bi) override + { + if (bi == ChanServ) + ChanServ = NULL; + } + + EventReturn OnBotPrivmsg(User *u, BotInfo *bi, Anope::string &message) override + { + if (bi == ChanServ && Config->GetModule(this)->Get("opersonly") && !u->HasMode("OPER")) + { + u->SendMessage(bi, ACCESS_DENIED); + return EVENT_STOP; + } + + return EVENT_CONTINUE; + } + + void OnDelCore(NickCore *nc) override + { + std::deque chans; + nc->GetChannelReferences(chans); + int max_reg = Config->GetModule(this)->Get("maxregistered"); + + for (auto *ci : chans) + { + if (ci->GetFounder() == nc) + { + NickCore *newowner = NULL; + if (ci->GetSuccessor() && ci->GetSuccessor() != nc && (ci->GetSuccessor()->IsServicesOper() || !max_reg || ci->GetSuccessor()->channelcount < max_reg)) + newowner = ci->GetSuccessor(); + else + { + const ChanAccess *highest = NULL; + for (unsigned j = 0; j < ci->GetAccessCount(); ++j) + { + const ChanAccess *ca = ci->GetAccess(j); + NickCore *anc = ca->GetAccount(); + + if (!anc || (!anc->IsServicesOper() && max_reg && anc->channelcount >= max_reg) || (anc == nc)) + continue; + if (!highest || *ca > *highest) + highest = ca; + } + if (highest) + newowner = highest->GetAccount(); + } + + if (newowner) + { + Log(LOG_NORMAL, "chanserv/drop", ChanServ) << "Transferring foundership of " << ci->name << " from deleted nick " << nc->display << " to " << newowner->display; + ci->SetFounder(newowner); + ci->SetSuccessor(NULL); + } + else + { + Log(LOG_NORMAL, "chanserv/drop", ChanServ) << "Deleting channel " << ci->name << " owned by deleted nick " << nc->display; + + delete ci; + continue; + } + } + + if (ci->GetSuccessor() == nc) + ci->SetSuccessor(NULL); + + for (unsigned j = 0; j < ci->GetAccessCount(); ++j) + { + const ChanAccess *ca = ci->GetAccess(j); + + if (ca->GetAccount() == nc) + { + delete ci->EraseAccess(j); + break; + } + } + + for (unsigned j = 0; j < ci->GetAkickCount(); ++j) + { + const AutoKick *akick = ci->GetAkick(j); + if (akick->nc == nc) + { + ci->EraseAkick(j); + break; + } + } + } + } + + void OnDelChan(ChannelInfo *ci) override + { + /* remove access entries that are this channel */ + + std::deque chans; + ci->GetChannelReferences(chans); + + for (const auto &chan : chans) + { + ChannelInfo *c = ChannelInfo::Find(chan); + if (!c) + continue; + + for (unsigned j = 0; j < c->GetAccessCount(); ++j) + { + ChanAccess *a = c->GetAccess(j); + + if (a->Mask().equals_ci(ci->name)) + { + delete a; + break; + } + } + } + + if (ci->c) + { + ci->c->RemoveMode(ci->WhoSends(), "REGISTERED", "", false); + + const Anope::string &require = Config->GetModule(this)->Get("require"); + if (!require.empty()) + ci->c->SetModes(ci->WhoSends(), false, "-%s", require.c_str()); + } + } + + EventReturn OnPreHelp(CommandSource &source, const std::vector ¶ms) override + { + if (!params.empty() || source.c || source.service != *ChanServ) + return EVENT_CONTINUE; + source.Reply(_("\002%s\002 allows you to register and control various\n" + "aspects of channels. %s can often prevent\n" + "malicious users from \"taking over\" channels by limiting\n" + "who is allowed channel operator privileges. Available\n" + "commands are listed below; to use them, type\n" + "\002%s%s \037command\037\002. For more information on a\n" + "specific command, type \002%s%s HELP \037command\037\002.\n"), + ChanServ->nick.c_str(), ChanServ->nick.c_str(), Config->StrictPrivmsg.c_str(), ChanServ->nick.c_str(), Config->StrictPrivmsg.c_str(), ChanServ->nick.c_str()); + return EVENT_CONTINUE; + } + + void OnPostHelp(CommandSource &source, const std::vector ¶ms) override + { + if (!params.empty() || source.c || source.service != *ChanServ) + return; + time_t chanserv_expire = Config->GetModule(this)->Get("expire", "30d"); + if (chanserv_expire >= 86400) + source.Reply(_(" \n" + "Note that any channel which is not used for %lu days\n" + "(i.e. which no user on the channel's access list enters\n" + "for that period of time) will be automatically dropped."), (unsigned long)chanserv_expire / 86400); + if (source.IsServicesOper()) + source.Reply(_(" \n" + "Services Operators can also, depending on their access drop\n" + "any channel, view (and modify) the access, levels and akick\n" + "lists and settings for any channel.")); + } + + void OnCheckModes(Reference &c) override + { + if (!c) + return; + + if (c->ci) + c->SetMode(c->ci->WhoSends(), "REGISTERED", "", false); + else + c->RemoveMode(c->WhoSends(), "REGISTERED", "", false); + + const Anope::string &require = Config->GetModule(this)->Get("require"); + if (!require.empty()) + { + if (c->ci) + c->SetModes(c->ci->WhoSends(), false, "+%s", require.c_str()); + else + c->SetModes(c->WhoSends(), false, "-%s", require.c_str()); + } + } + + void OnCreateChan(ChannelInfo *ci) override + { + /* Set default chan flags */ + for (const auto &def : defaults) + ci->Extend(def.upper()); + } + + EventReturn OnCanSet(User *u, const ChannelMode *cm) override + { + if (Config->GetModule(this)->Get("nomlock").find(cm->mchar) != Anope::string::npos + || Config->GetModule(this)->Get("require").find(cm->mchar) != Anope::string::npos) + return EVENT_STOP; + return EVENT_CONTINUE; + } + + void OnChannelSync(Channel *c) override + { + bool perm = c->HasMode("PERM") || (c->ci && persist && persist->HasExt(c->ci)); + if (!perm && !c->botchannel && (c->users.empty() || (c->users.size() == 1 && c->users.begin()->second->user->server == Me))) + { + this->Hold(c); + } + } + + void OnLog(Log *l) override + { + if (l->type == LOG_CHANNEL) + l->bi = ChanServ; + } + + void OnExpireTick() override + { + time_t chanserv_expire = Config->GetModule(this)->Get("expire", "30d"); + + if (!chanserv_expire || Anope::NoExpire || Anope::ReadOnly) + return; + + for (registered_channel_map::const_iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end; ) + { + ChannelInfo *ci = it->second; + ++it; + + bool expire = false; + + if (Anope::CurTime - ci->last_used >= chanserv_expire) + { + if (ci->c) + { + time_t last_used = ci->last_used; + for (Channel::ChanUserList::const_iterator cit = ci->c->users.begin(), cit_end = ci->c->users.end(); cit != cit_end && last_used == ci->last_used; ++cit) + ci->AccessFor(cit->second->user); + expire = last_used == ci->last_used; + } + else + expire = true; + } + + FOREACH_MOD(OnPreChanExpire, (ci, expire)); + + if (expire) + { + Log(LOG_NORMAL, "chanserv/expire", ChanServ) << "Expiring channel " << ci->name << " (founder: " << (ci->GetFounder() ? ci->GetFounder()->display : "(none)") << ")"; + FOREACH_MOD(OnChanExpire, (ci)); + delete ci; + } + } + } + + EventReturn OnCheckDelete(Channel *c) override + { + /* Do not delete this channel if ChanServ/a BotServ bot is inhabiting it */ + if (inhabit.HasExt(c)) + return EVENT_STOP; + + return EVENT_CONTINUE; + } + + void OnPostInit() override + { + if (!persist) + return; + + ChannelMode *perm = ModeManager::FindChannelModeByName("PERM"); + + /* Find all persistent channels and create them, as we are about to finish burst to our uplink */ + for (const auto &[_, ci] : *RegisteredChannelList) + { + if (!persist->HasExt(ci)) + continue; + + bool c; + ci->c = Channel::FindOrCreate(ci->name, c, ci->time_registered); + ci->c->syncing |= created; + + if (perm) + { + ci->c->SetMode(NULL, perm); + } + else + { + if (!ci->bi) + ci->WhoSends()->Assign(NULL, ci); + if (ci->c->FindUser(ci->bi) == NULL) + { + ChannelStatus status(BotModes()); + ci->bi->Join(ci->c, &status); + } + } + } + + } + + void OnChanRegistered(ChannelInfo *ci) override + { + if (!persist || !ci->c) + return; + /* Mark the channel as persistent */ + if (ci->c->HasMode("PERM")) + persist->Set(ci); + /* Persist may be in def cflags, set it here */ + else if (persist->HasExt(ci)) + ci->c->SetMode(NULL, "PERM"); + } + + void OnJoinChannel(User *u, Channel *c) override + { + if (always_lower && c->ci && c->creation_time > c->ci->time_registered) + { + Log(LOG_DEBUG) << "Changing TS of " << c->name << " from " << c->creation_time << " to " << c->ci->time_registered; + c->creation_time = c->ci->time_registered; + IRCD->SendChannel(c); + c->Reset(); + } + } + + EventReturn OnChannelModeSet(Channel *c, MessageSource &setter, ChannelMode *mode, const Anope::string ¶m) override + { + if (!always_lower && Anope::CurTime == c->creation_time && c->ci && setter.GetUser() && !setter.GetUser()->server->IsULined()) + { + ChanUserContainer *cu = c->FindUser(setter.GetUser()); + ChannelMode *cm = ModeManager::FindChannelModeByName("OP"); + if (cu && cm && !cu->status.HasMode(cm->mchar)) + { + /* Our -o and their mode change crossing, bounce their mode */ + c->RemoveMode(c->ci->WhoSends(), mode, param); + /* We don't set mlocks until after the join has finished processing, it will stack with this change, + * so there isn't much for the user to remove except -nt etc which is likely locked anyway. + */ + } + } + + return EVENT_CONTINUE; + } + + void OnChanInfo(CommandSource &source, ChannelInfo *ci, InfoFormatter &info, bool show_all) override + { + if (!show_all) + return; + + time_t chanserv_expire = Config->GetModule(this)->Get("expire", "30d"); + if (!ci->HasExt("CS_NO_EXPIRE") && chanserv_expire && !Anope::NoExpire && ci->last_used != Anope::CurTime) + info[_("Expires")] = Anope::strftime(ci->last_used + chanserv_expire, source.GetAccount()); + } + + void OnSetCorrectModes(User *user, Channel *chan, AccessGroup &access, bool &give_modes, bool &take_modes) override + { + if (always_lower) + // Since we always lower the TS, the other side will remove the modes if the channel ts lowers, so we don't + // have to worry about it + take_modes = false; + else if (ModeManager::FindChannelModeByName("REGISTERED")) + // Otherwise if the registered channel mode exists, we should remove modes if the channel is not +r + take_modes = !chan->HasMode("REGISTERED"); + } +}; + +MODULE_INIT(ChanServCore) diff --git a/modules/chanserv/cs_access.cpp b/modules/chanserv/cs_access.cpp new file mode 100644 index 000000000..7625f05aa --- /dev/null +++ b/modules/chanserv/cs_access.cpp @@ -0,0 +1,939 @@ +/* ChanServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" + +static std::map defaultLevels; + +static inline void reset_levels(ChannelInfo *ci) +{ + ci->ClearLevels(); + for (auto &[priv, level] : defaultLevels) + ci->SetLevel(priv, level); +} + +class AccessChanAccess final + : public ChanAccess +{ +public: + int level = 0; + + AccessChanAccess(AccessProvider *p) : ChanAccess(p) + { + } + + bool HasPriv(const Anope::string &name) const override + { + return this->ci->GetLevel(name) != ACCESS_INVALID && this->level >= this->ci->GetLevel(name); + } + + Anope::string AccessSerialize() const override + { + return stringify(this->level); + } + + void AccessUnserialize(const Anope::string &data) override + { + try + { + this->level = convertTo(data); + } + catch (const ConvertException &) + { + } + } + + bool operator>(const ChanAccess &other) const override + { + if (this->provider != other.provider) + return ChanAccess::operator>(other); + else + return this->level > anope_dynamic_static_cast(&other)->level; + } + + bool operator<(const ChanAccess &other) const override + { + if (this->provider != other.provider) + return ChanAccess::operator<(other); + else + return this->level < anope_dynamic_static_cast(&other)->level; + } +}; + +class AccessAccessProvider final + : public AccessProvider +{ +public: + static AccessAccessProvider *me; + + AccessAccessProvider(Module *o) : AccessProvider(o, "access/access") + { + me = this; + } + + ChanAccess *Create() override + { + return new AccessChanAccess(this); + } +}; +AccessAccessProvider* AccessAccessProvider::me; + +class CommandCSAccess final + : public Command +{ + void DoAdd(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + Anope::string mask = params[2]; + Privilege *p = NULL; + int level = ACCESS_INVALID; + + try + { + level = convertTo(params[3]); + } + catch (const ConvertException &) + { + p = PrivilegeManager::FindPrivilege(params[3]); + if (p != NULL && defaultLevels[p->name]) + level = defaultLevels[p->name]; + } + + if (!level) + { + source.Reply(_("Access level must be non-zero.")); + return; + } + else if (level <= ACCESS_INVALID || level >= ACCESS_FOUNDER) + { + source.Reply(CHAN_ACCESS_LEVEL_RANGE, ACCESS_INVALID + 1, ACCESS_FOUNDER - 1); + return; + } + + AccessGroup u_access = source.AccessFor(ci); + const ChanAccess *highest = u_access.Highest(); + + AccessChanAccess tmp_access(AccessAccessProvider::me); + tmp_access.ci = ci; + tmp_access.level = level; + + bool override = false; + const NickAlias *na = NULL; + + if ((!highest || *highest <= tmp_access) && !u_access.founder) + { + if (source.HasPriv("chanserv/access/modify")) + override = true; + else + { + source.Reply(ACCESS_DENIED); + return; + } + } + + if (IRCD->IsChannelValid(mask)) + { + if (Config->GetModule("chanserv")->Get("disallow_channel_access")) + { + source.Reply(_("Channels may not be on access lists.")); + return; + } + + ChannelInfo *targ_ci = ChannelInfo::Find(mask); + if (targ_ci == NULL) + { + source.Reply(CHAN_X_NOT_REGISTERED, mask.c_str()); + return; + } + else if (ci == targ_ci) + { + source.Reply(_("You can't add a channel to its own access list.")); + return; + } + + mask = targ_ci->name; + } + else + { + na = NickAlias::Find(mask); + + if (!na && Config->GetModule("chanserv")->Get("disallow_hostmask_access")) + { + source.Reply(_("Masks and unregistered users may not be on access lists.")); + return; + } + else if (na && na->nc->HasExt("NEVEROP")) + { + source.Reply(_("\002%s\002 does not wish to be added to channel access lists."), + na->nc->display.c_str()); + return; + } + else if (mask.find_first_of("!*@") == Anope::string::npos && !na) + { + User *targ = User::Find(mask, true); + if (targ != NULL) + mask = "*!*@" + targ->GetDisplayedHost(); + else + { + source.Reply(NICK_X_NOT_REGISTERED, mask.c_str()); + return; + } + } + + if (na) + mask = na->nick; + } + + for (unsigned i = ci->GetAccessCount(); i > 0; --i) + { + const ChanAccess *access = ci->GetAccess(i - 1); + if ((na && na->nc == access->GetAccount()) || mask.equals_ci(access->Mask())) + { + /* Don't allow lowering from a level >= u_level */ + if ((!highest || *access >= *highest) && !u_access.founder && !source.HasPriv("chanserv/access/modify")) + { + source.Reply(ACCESS_DENIED); + return; + } + delete ci->EraseAccess(i - 1); + break; + } + } + + unsigned access_max = Config->GetModule("chanserv")->Get("accessmax", "1024"); + if (access_max && ci->GetDeepAccessCount() >= access_max) + { + source.Reply(_("Sorry, you can only have %d access entries on a channel, including access entries from other channels."), access_max); + return; + } + + ServiceReference provider("AccessProvider", "access/access"); + if (!provider) + return; + AccessChanAccess *access = anope_dynamic_static_cast(provider->Create()); + access->SetMask(mask, ci); + access->creator = source.GetNick(); + access->level = level; + access->last_seen = 0; + access->created = Anope::CurTime; + access->description = params.size() > 4 ? params[4] : ""; + ci->AddAccess(access); + + FOREACH_MOD(OnAccessAdd, (ci, source, access)); + + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to add " << mask << " with level " << level; + if (p != NULL) + source.Reply(_("\002%s\002 added to %s access list at privilege %s (level %d)"), access->Mask().c_str(), ci->name.c_str(), p->name.c_str(), level); + else + source.Reply(_("\002%s\002 added to %s access list at level \002%d\002."), access->Mask().c_str(), ci->name.c_str(), level); + } + + void DoDel(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + Anope::string mask = params[2]; + + const NickAlias *na = NickAlias::Find(mask); + if (na && na->nc) + { + mask = na->nc->display; + } + else if (!isdigit(mask[0]) && mask.find_first_of("#!*@") == Anope::string::npos) + { + User *targ = User::Find(mask, true); + if (targ != NULL) + mask = "*!*@" + targ->GetDisplayedHost(); + else + { + source.Reply(NICK_X_NOT_REGISTERED, mask.c_str()); + return; + } + } + + if (!ci->GetAccessCount()) + source.Reply(_("%s access list is empty."), ci->name.c_str()); + else if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos) + { + class AccessDelCallback final + : public NumberList + { + CommandSource &source; + ChannelInfo *ci; + Command *c; + unsigned deleted = 0; + Anope::string Nicks; + bool denied = false; + bool override = false; + public: + AccessDelCallback(CommandSource &_source, ChannelInfo *_ci, Command *_c, const Anope::string &numlist) : NumberList(numlist, true), source(_source), ci(_ci), c(_c) + { + if (!source.AccessFor(ci).HasPriv("ACCESS_CHANGE") && source.HasPriv("chanserv/access/modify")) + this->override = true; + } + + ~AccessDelCallback() override + { + if (denied && !deleted) + source.Reply(ACCESS_DENIED); + else if (!deleted) + source.Reply(_("No matching entries on %s access list."), ci->name.c_str()); + else + { + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "to delete " << Nicks; + + if (deleted == 1) + source.Reply(_("Deleted 1 entry from %s access list."), ci->name.c_str()); + else + source.Reply(_("Deleted %d entries from %s access list."), deleted, ci->name.c_str()); + } + } + + void HandleNumber(unsigned Number) override + { + if (!Number || Number > ci->GetAccessCount()) + return; + + ChanAccess *access = ci->GetAccess(Number - 1); + + AccessGroup ag = source.AccessFor(ci); + const ChanAccess *u_highest = ag.Highest(); + + if ((!u_highest || *u_highest <= *access) && !ag.founder && !this->override && access->GetAccount() != source.nc) + { + denied = true; + return; + } + + ++deleted; + if (!Nicks.empty()) + Nicks += ", " + access->Mask(); + else + Nicks = access->Mask(); + + ci->EraseAccess(Number - 1); + + FOREACH_MOD(OnAccessDel, (ci, source, access)); + delete access; + } + } + delcallback(source, ci, this, mask); + delcallback.Process(); + } + else + { + AccessGroup u_access = source.AccessFor(ci); + const ChanAccess *highest = u_access.Highest(); + + for (unsigned i = ci->GetAccessCount(); i > 0; --i) + { + ChanAccess *access = ci->GetAccess(i - 1); + if (mask.equals_ci(access->Mask())) + { + if (access->GetAccount() != source.nc && !u_access.founder && (!highest || *highest <= *access) && !source.HasPriv("chanserv/access/modify")) + source.Reply(ACCESS_DENIED); + else + { + source.Reply(_("\002%s\002 deleted from %s access list."), access->Mask().c_str(), ci->name.c_str()); + bool override = !u_access.founder && !u_access.HasPriv("ACCESS_CHANGE") && access->GetAccount() != source.nc; + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << access->Mask(); + + ci->EraseAccess(i - 1); + FOREACH_MOD(OnAccessDel, (ci, source, access)); + delete access; + } + return; + } + } + + source.Reply(_("\002%s\002 not found on %s access list."), mask.c_str(), ci->name.c_str()); + } + + return; + } + + void ProcessList(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms, ListFormatter &list) + { + const Anope::string &nick = params.size() > 2 ? params[2] : ""; + + if (!ci->GetAccessCount()) + source.Reply(_("%s access list is empty."), ci->name.c_str()); + else if (!nick.empty() && nick.find_first_not_of("1234567890,-") == Anope::string::npos) + { + class AccessListCallback final + : public NumberList + { + ListFormatter &list; + ChannelInfo *ci; + + public: + AccessListCallback(ListFormatter &_list, ChannelInfo *_ci, const Anope::string &numlist) : NumberList(numlist, false), list(_list), ci(_ci) + { + } + + void HandleNumber(unsigned number) override + { + if (!number || number > ci->GetAccessCount()) + return; + + const ChanAccess *access = ci->GetAccess(number - 1); + + Anope::string timebuf; + if (ci->c) + { + for (const auto &[_, cuc] : ci->c->users) + { + ChannelInfo *p; + if (access->Matches(cuc->user, cuc->user->Account(), p)) + timebuf = "Now"; + } + } + if (timebuf.empty()) + { + if (access->last_seen == 0) + timebuf = "Never"; + else + timebuf = Anope::strftime(access->last_seen, NULL, true); + } + + ListFormatter::ListEntry entry; + entry["Number"] = stringify(number); + entry["Level"] = access->AccessSerialize(); + entry["Mask"] = access->Mask(); + entry["By"] = access->creator; + entry["Last seen"] = timebuf; + entry["Description"] = access->description; + this->list.AddEntry(entry); + } + } + nl_list(list, ci, nick); + nl_list.Process(); + } + else + { + for (unsigned i = 0, end = ci->GetAccessCount(); i < end; ++i) + { + const ChanAccess *access = ci->GetAccess(i); + + if (!nick.empty() && !Anope::Match(access->Mask(), nick)) + continue; + + Anope::string timebuf; + if (ci->c) + { + for (auto &[_, cuc] : ci->c->users) + { + ChannelInfo *p; + if (access->Matches(cuc->user, cuc->user->Account(), p)) + timebuf = "Now"; + } + } + if (timebuf.empty()) + { + if (access->last_seen == 0) + timebuf = "Never"; + else + timebuf = Anope::strftime(access->last_seen, NULL, true); + } + + ListFormatter::ListEntry entry; + entry["Number"] = stringify(i + 1); + entry["Level"] = access->AccessSerialize(); + entry["Mask"] = access->Mask(); + entry["By"] = access->creator; + entry["Last seen"] = timebuf; + entry["Description"] = access->description; + list.AddEntry(entry); + } + } + + if (list.IsEmpty()) + source.Reply(_("No matching entries on %s access list."), ci->name.c_str()); + else + { + std::vector replies; + list.Process(replies); + + source.Reply(_("Access list for %s:"), ci->name.c_str()); + + for (const auto &reply : replies) + source.Reply(reply); + + source.Reply(_("End of access list")); + } + + return; + } + + void DoList(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + if (!ci->GetAccessCount()) + { + source.Reply(_("%s access list is empty."), ci->name.c_str()); + return; + } + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Number")).AddColumn(_("Level")).AddColumn(_("Mask")).AddColumn(_("Description")); + this->ProcessList(source, ci, params, list); + } + + void DoView(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + if (!ci->GetAccessCount()) + { + source.Reply(_("%s access list is empty."), ci->name.c_str()); + return; + } + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Number")).AddColumn(_("Level")).AddColumn(_("Mask")).AddColumn(_("By")).AddColumn(_("Last seen")).AddColumn(_("Description")); + this->ProcessList(source, ci, params, list); + } + + void DoClear(CommandSource &source, ChannelInfo *ci) + { + if (!source.IsFounder(ci) && !source.HasPriv("chanserv/access/modify")) + source.Reply(ACCESS_DENIED); + else + { + FOREACH_MOD(OnAccessClear, (ci, source)); + + ci->ClearAccess(); + + source.Reply(_("Channel %s access list has been cleared."), ci->name.c_str()); + + bool override = !source.IsFounder(ci); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to clear the access list"; + } + + return; + } + +public: + CommandCSAccess(Module *creator) : Command(creator, "chanserv/access", 2, 5) + { + this->SetDesc(_("Modify the list of privileged users")); + this->SetSyntax(_("\037channel\037 ADD \037mask\037 \037level\037 [\037description\037]")); + this->SetSyntax(_("\037channel\037 DEL {\037mask\037 | \037entry-num\037 | \037list\037}")); + this->SetSyntax(_("\037channel\037 LIST [\037mask\037 | \037list\037]")); + this->SetSyntax(_("\037channel\037 VIEW [\037mask\037 | \037list\037]")); + this->SetSyntax(_("\037channel\037 CLEAR")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &cmd = params[1]; + const Anope::string &nick = params.size() > 2 ? params[2] : ""; + const Anope::string &s = params.size() > 3 ? params[3] : ""; + + ChannelInfo *ci = ChannelInfo::Find(params[0]); + if (ci == NULL) + { + source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str()); + return; + } + + bool is_list = cmd.equals_ci("LIST") || cmd.equals_ci("VIEW"); + bool is_clear = cmd.equals_ci("CLEAR"); + bool is_del = cmd.equals_ci("DEL"); + + bool has_access = false; + if (source.HasPriv("chanserv/access/modify")) + has_access = true; + else if (is_list && source.HasPriv("chanserv/access/list")) + has_access = true; + else if (is_list && source.AccessFor(ci).HasPriv("ACCESS_LIST")) + has_access = true; + else if (source.AccessFor(ci).HasPriv("ACCESS_CHANGE")) + has_access = true; + else if (is_del) + { + const NickAlias *na = NickAlias::Find(nick); + if (na && na->nc == source.GetAccount()) + has_access = true; + } + + /* If LIST, we don't *require* any parameters, but we can take any. + * If DEL, we require a nick and no level. + * Else (ADD), we require a level (which implies a nick). */ + if (is_list || is_clear ? 0 : (cmd.equals_ci("DEL") ? (nick.empty() || !s.empty()) : s.empty())) + this->OnSyntaxError(source, cmd); + else if (!has_access) + source.Reply(ACCESS_DENIED); + else if (Anope::ReadOnly && !is_list) + source.Reply(_("Sorry, channel access list modification is temporarily disabled.")); + else if (cmd.equals_ci("ADD")) + this->DoAdd(source, ci, params); + else if (cmd.equals_ci("DEL")) + this->DoDel(source, ci, params); + else if (cmd.equals_ci("LIST")) + this->DoList(source, ci, params); + else if (cmd.equals_ci("VIEW")) + this->DoView(source, ci, params); + else if (cmd.equals_ci("CLEAR")) + this->DoClear(source, ci); + else + this->OnSyntaxError(source, ""); + + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Maintains the \002access list\002 for a channel. The access\n" + "list specifies which users are allowed chanop status or\n" + "access to %s commands on the channel. Different\n" + "user levels allow for access to different subsets of\n" + "privileges. Any registered user not on the access list has\n" + "a user level of 0, and any unregistered user has a user level\n" + "of -1."), source.service->nick.c_str()); + source.Reply(" "); + source.Reply(_("The \002ACCESS ADD\002 command adds the given mask to the\n" + "access list with the given user level; if the mask is\n" + "already present on the list, its access level is changed to\n" + "the level specified in the command. The \037level\037 specified\n" + "may be a numerical level or the name of a privilege (eg AUTOOP).\n" + "When a user joins the channel the access they receive is from the\n" + "highest level entry in the access list.")); + if (!Config->GetModule("chanserv")->Get("disallow_channel_access")) + source.Reply(_("The given mask may also be a channel, which will use the\n" + "access list from the other channel up to the given \037level\037.")); + source.Reply(" "); + source.Reply(_("The \002ACCESS DEL\002 command removes the given nick from the\n" + "access list. If a list of entry numbers is given, those\n" + "entries are deleted. (See the example for LIST below.)\n" + "You may remove yourself from an access list, even if you\n" + "do not have access to modify that list otherwise.")); + source.Reply(" "); + source.Reply(_("The \002ACCESS LIST\002 command displays the access list. If\n" + "a wildcard mask is given, only those entries matching the\n" + "mask are displayed. If a list of entry numbers is given,\n" + "only those entries are shown; for example:\n" + " \002ACCESS #channel LIST 2-5,7-9\002\n" + " Lists access entries numbered 2 through 5 and\n" + " 7 through 9.\n" + " \n" + "The \002ACCESS VIEW\002 command displays the access list similar\n" + "to \002ACCESS LIST\002 but shows the creator and last used time.\n" + " \n" + "The \002ACCESS CLEAR\002 command clears all entries of the\n" + "access list.")); + source.Reply(" "); + + BotInfo *bi; + Anope::string cmd; + if (Command::FindCommandFromService("chanserv/levels", bi, cmd)) + source.Reply(_("\002User access levels\002 can be seen by using the\n" + "\002%s\002 command; type \002%s%s HELP LEVELS\002 for\n" + "information."), cmd.c_str(), Config->StrictPrivmsg.c_str(), bi->nick.c_str()); + return true; + } +}; + +class CommandCSLevels final + : public Command +{ + void DoSet(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + const Anope::string &what = params[2]; + const Anope::string &lev = params[3]; + + int level; + + if (lev.equals_ci("FOUNDER")) + level = ACCESS_FOUNDER; + else + { + try + { + level = convertTo(lev); + } + catch (const ConvertException &) + { + this->OnSyntaxError(source, "SET"); + return; + } + } + + if (level <= ACCESS_INVALID || level > ACCESS_FOUNDER) + source.Reply(_("Level must be between %d and %d inclusive."), ACCESS_INVALID + 1, ACCESS_FOUNDER - 1); + else + { + Privilege *p = PrivilegeManager::FindPrivilege(what); + if (p == NULL) + source.Reply(_("Setting \002%s\002 not known. Type \002%s%s HELP LEVELS\002 for a list of valid settings."), what.c_str(), Config->StrictPrivmsg.c_str(), source.service->nick.c_str()); + else + { + bool override = !source.AccessFor(ci).HasPriv("FOUNDER"); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to set " << p->name << " to level " << level; + + ci->SetLevel(p->name, level); + FOREACH_MOD(OnLevelChange, (source, ci, p->name, level)); + + if (level == ACCESS_FOUNDER) + source.Reply(_("Level for %s on channel %s changed to founder only."), p->name.c_str(), ci->name.c_str()); + else + source.Reply(_("Level for \002%s\002 on channel %s changed to \002%d\002."), p->name.c_str(), ci->name.c_str(), level); + } + } + } + + void DoDisable(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + const Anope::string &what = params[2]; + + /* Don't allow disabling of the founder level. It would be hard to change it back if you don't have access to use this command */ + if (what.equals_ci("FOUNDER")) + { + source.Reply(_("You can not disable the founder privilege because it would be impossible to re-enable it at a later time.")); + return; + } + + Privilege *p = PrivilegeManager::FindPrivilege(what); + if (p != NULL) + { + bool override = !source.AccessFor(ci).HasPriv("FOUNDER"); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to disable " << p->name; + + ci->SetLevel(p->name, ACCESS_INVALID); + FOREACH_MOD(OnLevelChange, (source, ci, p->name, ACCESS_INVALID)); + + source.Reply(_("\002%s\002 disabled on channel %s."), p->name.c_str(), ci->name.c_str()); + return; + } + + source.Reply(_("Setting \002%s\002 not known. Type \002%s%s HELP LEVELS\002 for a list of valid settings."), what.c_str(), Config->StrictPrivmsg.c_str(), source.service->nick.c_str()); + } + + void DoList(CommandSource &source, ChannelInfo *ci) + { + source.Reply(_("Access level settings for channel %s:"), ci->name.c_str()); + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Name")).AddColumn(_("Level")); + + const std::vector &privs = PrivilegeManager::GetPrivileges(); + + for (const auto &p : privs) + { + int16_t j = ci->GetLevel(p.name); + + ListFormatter::ListEntry entry; + entry["Name"] = p.name; + + if (j == ACCESS_INVALID) + entry["Level"] = Language::Translate(source.GetAccount(), _("(disabled)")); + else if (j == ACCESS_FOUNDER) + entry["Level"] = Language::Translate(source.GetAccount(), _("(founder only)")); + else + entry["Level"] = stringify(j); + + list.AddEntry(entry); + } + + std::vector replies; + list.Process(replies); + + for (const auto &reply : replies) + source.Reply(reply); + } + + void DoReset(CommandSource &source, ChannelInfo *ci) + { + bool override = !source.AccessFor(ci).HasPriv("FOUNDER"); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to reset all levels"; + + reset_levels(ci); + FOREACH_MOD(OnLevelChange, (source, ci, "ALL", 0)); + + source.Reply(_("Access levels for \002%s\002 reset to defaults."), ci->name.c_str()); + return; + } + +public: + CommandCSLevels(Module *creator) : Command(creator, "chanserv/levels", 2, 4) + { + this->SetDesc(_("Redefine the meanings of access levels")); + this->SetSyntax(_("\037channel\037 SET \037type\037 \037level\037")); + this->SetSyntax(_("\037channel\037 {DIS | DISABLE} \037type\037")); + this->SetSyntax(_("\037channel\037 LIST")); + this->SetSyntax(_("\037channel\037 RESET")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &cmd = params[1]; + const Anope::string &what = params.size() > 2 ? params[2] : ""; + const Anope::string &s = params.size() > 3 ? params[3] : ""; + + ChannelInfo *ci = ChannelInfo::Find(params[0]); + if (ci == NULL) + { + source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str()); + return; + } + + bool has_access = false; + if (source.HasPriv("chanserv/access/modify")) + has_access = true; + else if (cmd.equals_ci("LIST") && source.HasPriv("chanserv/access/list")) + has_access = true; + else if (source.AccessFor(ci).HasPriv("FOUNDER")) + has_access = true; + + /* If SET, we want two extra parameters; if DIS[ABLE] or FOUNDER, we want only + * one; else, we want none. + */ + if (cmd.equals_ci("SET") ? s.empty() : (cmd.substr(0, 3).equals_ci("DIS") ? (what.empty() || !s.empty()) : !what.empty())) + this->OnSyntaxError(source, cmd); + else if (!has_access) + source.Reply(ACCESS_DENIED); + else if (Anope::ReadOnly && !cmd.equals_ci("LIST")) + source.Reply(READ_ONLY_MODE); + else if (cmd.equals_ci("SET")) + this->DoSet(source, ci, params); + else if (cmd.equals_ci("DIS") || cmd.equals_ci("DISABLE")) + this->DoDisable(source, ci, params); + else if (cmd.equals_ci("LIST")) + this->DoList(source, ci); + else if (cmd.equals_ci("RESET")) + this->DoReset(source, ci); + else + this->OnSyntaxError(source, ""); + + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + if (subcommand.equals_ci("DESC")) + { + source.Reply(_("The following feature/function names are available:")); + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Name")).AddColumn(_("Description")); + + for (const auto &p : PrivilegeManager::GetPrivileges()) + { + ListFormatter::ListEntry entry; + entry["Name"] = p.name; + entry["Description"] = Language::Translate(source.nc, p.desc.c_str()); + list.AddEntry(entry); + } + + std::vector replies; + list.Process(replies); + + for (const auto &reply : replies) + source.Reply(reply); + } + else + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("The \002LEVELS\002 command allows fine control over the meaning of\n" + "the numeric access levels used for channels. With this\n" + "command, you can define the access level required for most\n" + "of %s's functions. (The \002SET FOUNDER\002 and this command\n" + "are always restricted to the channel founder.)\n" + " \n" + "\002LEVELS SET\002 allows the access level for a function or group of\n" + "functions to be changed. \002LEVELS DISABLE\002 (or \002DIS\002 for short)\n" + "disables an automatic feature or disallows access to a\n" + "function by anyone, INCLUDING the founder (although, the founder\n" + "can always re-enable it). Use \002LEVELS SET founder\002 to make a level\n" + "founder only.\n" + " \n" + "\002LEVELS LIST\002 shows the current levels for each function or\n" + "group of functions. \002LEVELS RESET\002 resets the levels to the\n" + "default levels of a newly-created channel.\n" + " \n" + "For a list of the features and functions whose levels can be\n" + "set, see \002HELP LEVELS DESC\002."), source.service->nick.c_str()); + } + return true; + } +}; + +class CSAccess final + : public Module +{ + AccessAccessProvider accessprovider; + CommandCSAccess commandcsaccess; + CommandCSLevels commandcslevels; + +public: + CSAccess(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + accessprovider(this), commandcsaccess(this), commandcslevels(this) + { + this->SetPermanent(true); + + } + + void OnReload(Configuration::Conf *conf) override + { + defaultLevels.clear(); + + for (int i = 0; i < conf->CountBlock("privilege"); ++i) + { + Configuration::Block *priv = conf->GetBlock("privilege", i); + + const Anope::string &pname = priv->Get("name"); + + Privilege *p = PrivilegeManager::FindPrivilege(pname); + if (p == NULL) + continue; + + const Anope::string &value = priv->Get("level"); + if (value.empty()) + continue; + else if (value.equals_ci("founder")) + defaultLevels[p->name] = ACCESS_FOUNDER; + else if (value.equals_ci("disabled")) + defaultLevels[p->name] = ACCESS_INVALID; + else + defaultLevels[p->name] = priv->Get("level"); + } + } + + void OnCreateChan(ChannelInfo *ci) override + { + reset_levels(ci); + } + + EventReturn OnGroupCheckPriv(const AccessGroup *group, const Anope::string &priv) override + { + if (group->ci == NULL) + return EVENT_CONTINUE; + + const ChanAccess *highest = group->Highest(); + if (highest && highest->provider == &accessprovider) + { + /* Access accessprovider is the only accessprovider with the concept of negative access, + * so check they don't have negative access + */ + const AccessChanAccess *aca = anope_dynamic_static_cast(highest); + + if (aca->level < 0) + return EVENT_CONTINUE; + } + + /* Special case. Allows a level of -1 to match anyone, and a level of 0 to match anyone identified. */ + int16_t level = group->ci->GetLevel(priv); + if (level == -1) + return EVENT_ALLOW; + else if (level == 0 && group->nc && !group->nc->HasExt("UNCONFIRMED")) + return EVENT_ALLOW; + return EVENT_CONTINUE; + } +}; + +MODULE_INIT(CSAccess) diff --git a/modules/chanserv/cs_akick.cpp b/modules/chanserv/cs_akick.cpp new file mode 100644 index 000000000..6457f2856 --- /dev/null +++ b/modules/chanserv/cs_akick.cpp @@ -0,0 +1,585 @@ +/* ChanServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" + +class CommandCSAKick final + : public Command +{ + void Enforce(CommandSource &source, ChannelInfo *ci) + { + Channel *c = ci->c; + int count = 0; + + if (!c) + { + return; + } + + for (Channel::ChanUserList::iterator it = c->users.begin(), it_end = c->users.end(); it != it_end; ) + { + ChanUserContainer *uc = it->second; + ++it; + + if (c->CheckKick(uc->user)) + ++count; + } + + bool override = !source.AccessFor(ci).HasPriv("AKICK"); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "ENFORCE, affects " << count << " users"; + + source.Reply(_("AKICK ENFORCE for \002%s\002 complete; \002%d\002 users were affected."), ci->name.c_str(), count); + } + + void DoAdd(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + Anope::string mask = params[2]; + Anope::string reason = params.size() > 3 ? params[3] : ""; + const NickAlias *na = NickAlias::Find(mask); + NickCore *nc = NULL; + const AutoKick *akick; + unsigned reasonmax = Config->GetModule("chanserv")->Get("reasonmax", "200"); + + if (reason.length() > reasonmax) + reason = reason.substr(0, reasonmax); + + if (IRCD->IsExtbanValid(mask)) + ; /* If this is an extban don't try to complete the mask */ + else if (IRCD->IsChannelValid(mask)) + { + /* Also don't try to complete the mask if this is a channel */ + + if (mask.equals_ci(ci->name) && ci->HasExt("PEACE")) + { + source.Reply(ACCESS_DENIED); + return; + } + } + else if (!na) + { + /* If the mask contains a realname the reason must be prepended with a : */ + if (mask.find('#') != Anope::string::npos) + { + size_t r = reason.find(':'); + if (r != Anope::string::npos) + { + mask += " " + reason.substr(0, r); + mask.trim(); + reason = reason.substr(r + 1); + reason.trim(); + } + else + { + mask = mask + " " + reason; + reason.clear(); + } + } + + Entry e("", mask); + + mask = (e.nick.empty() ? "*" : e.nick) + "!" + + (e.user.empty() ? "*" : e.user) + "@" + + (e.host.empty() ? "*" : e.host); + if (!e.real.empty()) + mask += "#" + e.real; + } + else + nc = na->nc; + + /* Check excepts BEFORE we get this far */ + if (ci->c) + { + for (const auto &mode : ci->c->GetModeList("EXCEPT")) + { + if (Anope::Match(mode, mask)) + { + source.Reply(CHAN_EXCEPTED, mask.c_str(), ci->name.c_str()); + return; + } + } + } + + bool override = !source.AccessFor(ci).HasPriv("AKICK"); + /* Opers overriding get to bypass PEACE */ + if (override) + ; + /* These peace checks are only for masks */ + else if (IRCD->IsChannelValid(mask)) + ; + /* Check whether target nick has equal/higher access + * or whether the mask matches a user with higher/equal access - Viper */ + else if (ci->HasExt("PEACE") && nc) + { + AccessGroup nc_access = ci->AccessFor(nc), u_access = source.AccessFor(ci); + if (nc == ci->GetFounder() || nc_access >= u_access) + { + source.Reply(ACCESS_DENIED); + return; + } + } + else if (ci->HasExt("PEACE")) + { + /* Match against all currently online users with equal or + * higher access. - Viper */ + for (const auto &[_, u2] : UserListByNick) + { + AccessGroup nc_access = ci->AccessFor(nc), u_access = source.AccessFor(ci); + Entry entry_mask("", mask); + + if ((ci->AccessFor(u2).HasPriv("FOUNDER") || nc_access >= u_access) && entry_mask.Matches(u2)) + { + source.Reply(ACCESS_DENIED); + return; + } + } + + /* Match against the lastusermask of all nickalias's with equal + * or higher access. - Viper */ + for (const auto &[_, na2] : *NickAliasList) + { + na = na2; + + AccessGroup nc_access = ci->AccessFor(na->nc), u_access = source.AccessFor(ci); + if (na->nc && (na->nc == ci->GetFounder() || nc_access >= u_access)) + { + Anope::string buf = na->nick + "!" + na->last_usermask; + if (Anope::Match(buf, mask)) + { + source.Reply(ACCESS_DENIED); + return; + } + } + } + } + + for (unsigned j = 0, end = ci->GetAkickCount(); j < end; ++j) + { + akick = ci->GetAkick(j); + if (akick->nc ? akick->nc == nc : mask.equals_ci(akick->mask)) + { + source.Reply(_("\002%s\002 already exists on %s autokick list."), akick->nc ? akick->nc->display.c_str() : akick->mask.c_str(), ci->name.c_str()); + return; + } + } + + if (ci->GetAkickCount() >= Config->GetModule(this->owner)->Get("autokickmax")) + { + source.Reply(_("Sorry, you can only have %d autokick masks on a channel."), Config->GetModule(this->owner)->Get("autokickmax")); + return; + } + + if (nc) + akick = ci->AddAkick(source.GetNick(), nc, reason); + else + akick = ci->AddAkick(source.GetNick(), mask, reason); + + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to add " << mask << (reason == "" ? "" : ": ") << reason; + + FOREACH_MOD(OnAkickAdd, (source, ci, akick)); + + source.Reply(_("\002%s\002 added to %s autokick list."), mask.c_str(), ci->name.c_str()); + + this->Enforce(source, ci); + } + + void DoDel(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + const Anope::string &mask = params[2]; + unsigned i, end; + + if (!ci->GetAkickCount()) + { + source.Reply(_("%s autokick list is empty."), ci->name.c_str()); + return; + } + + /* Special case: is it a number/list? Only do search if it isn't. */ + if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos) + { + class AkickDelCallback final + : public NumberList + { + CommandSource &source; + ChannelInfo *ci; + Command *c; + unsigned deleted = 0; + AccessGroup ag; + public: + AkickDelCallback(CommandSource &_source, ChannelInfo *_ci, Command *_c, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), c(_c), ag(source.AccessFor(ci)) + { + } + + ~AkickDelCallback() override + { + if (!deleted) + source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str()); + else if (deleted == 1) + source.Reply(_("Deleted 1 entry from %s autokick list."), ci->name.c_str()); + else + source.Reply(_("Deleted %d entries from %s autokick list."), deleted, ci->name.c_str()); + } + + void HandleNumber(unsigned number) override + { + if (!number || number > ci->GetAkickCount()) + return; + + const AutoKick *akick = ci->GetAkick(number - 1); + + FOREACH_MOD(OnAkickDel, (source, ci, akick)); + + bool override = !ag.HasPriv("AKICK"); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "to delete " << (akick->nc ? akick->nc->display : akick->mask); + + ++deleted; + ci->EraseAkick(number - 1); + } + } + delcallback(source, ci, this, mask); + delcallback.Process(); + } + else + { + const NickAlias *na = NickAlias::Find(mask); + const NickCore *nc = na ? *na->nc : NULL; + + for (i = 0, end = ci->GetAkickCount(); i < end; ++i) + { + const AutoKick *akick = ci->GetAkick(i); + + if (akick->nc ? akick->nc == nc : mask.equals_ci(akick->mask)) + break; + } + + if (i == ci->GetAkickCount()) + { + source.Reply(_("\002%s\002 not found on %s autokick list."), mask.c_str(), ci->name.c_str()); + return; + } + + bool override = !source.AccessFor(ci).HasPriv("AKICK"); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << mask; + + FOREACH_MOD(OnAkickDel, (source, ci, ci->GetAkick(i))); + + ci->EraseAkick(i); + + source.Reply(_("\002%s\002 deleted from %s autokick list."), mask.c_str(), ci->name.c_str()); + } + } + + void ProcessList(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms, ListFormatter &list) + { + const Anope::string &mask = params.size() > 2 ? params[2] : ""; + + if (!mask.empty() && isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos) + { + class AkickListCallback final + : public NumberList + { + ListFormatter &list; + ChannelInfo *ci; + + public: + AkickListCallback(ListFormatter &_list, ChannelInfo *_ci, const Anope::string &numlist) : NumberList(numlist, false), list(_list), ci(_ci) + { + } + + void HandleNumber(unsigned number) override + { + if (!number || number > ci->GetAkickCount()) + return; + + const AutoKick *akick = ci->GetAkick(number - 1); + + Anope::string timebuf, lastused; + if (akick->addtime) + timebuf = Anope::strftime(akick->addtime, NULL, true); + else + timebuf = UNKNOWN; + if (akick->last_used) + lastused = Anope::strftime(akick->last_used, NULL, true); + else + lastused = UNKNOWN; + + ListFormatter::ListEntry entry; + entry["Number"] = stringify(number); + if (akick->nc) + entry["Mask"] = akick->nc->display; + else + entry["Mask"] = akick->mask; + entry["Creator"] = akick->creator; + entry["Created"] = timebuf; + entry["Last used"] = lastused; + entry["Reason"] = akick->reason; + this->list.AddEntry(entry); + } + } + nl_list(list, ci, mask); + nl_list.Process(); + } + else + { + for (unsigned i = 0, end = ci->GetAkickCount(); i < end; ++i) + { + const AutoKick *akick = ci->GetAkick(i); + + if (!mask.empty()) + { + if (!akick->nc && !Anope::Match(akick->mask, mask)) + continue; + if (akick->nc && !Anope::Match(akick->nc->display, mask)) + continue; + } + + Anope::string timebuf, lastused; + if (akick->addtime) + timebuf = Anope::strftime(akick->addtime, NULL, true); + else + timebuf = UNKNOWN; + if (akick->last_used) + lastused = Anope::strftime(akick->last_used, NULL, true); + else + lastused = UNKNOWN; + + ListFormatter::ListEntry entry; + entry["Number"] = stringify(i + 1); + if (akick->nc) + entry["Mask"] = akick->nc->display; + else + entry["Mask"] = akick->mask; + entry["Creator"] = akick->creator; + entry["Created"] = timebuf; + entry["Last used"] = lastused; + entry["Reason"] = akick->reason; + list.AddEntry(entry); + } + } + + if (list.IsEmpty()) + source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str()); + else + { + std::vector replies; + list.Process(replies); + + source.Reply(_("Autokick list for %s:"), ci->name.c_str()); + + for (const auto &reply : replies) + source.Reply(reply); + + source.Reply(_("End of autokick list")); + } + } + + void DoList(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + if (!ci->GetAkickCount()) + { + source.Reply(_("%s autokick list is empty."), ci->name.c_str()); + return; + } + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Number")).AddColumn(_("Mask")).AddColumn(_("Reason")); + this->ProcessList(source, ci, params, list); + } + + void DoView(CommandSource &source, ChannelInfo *ci, const std::vector ¶ms) + { + if (!ci->GetAkickCount()) + { + source.Reply(_("%s autokick list is empty."), ci->name.c_str()); + return; + } + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Number")).AddColumn(_("Mask")).AddColumn(_("Creator")).AddColumn(_("Created")).AddColumn(_("Last used")).AddColumn(_("Reason")); + this->ProcessList(source, ci, params, list); + } + + void DoEnforce(CommandSource &source, ChannelInfo *ci) + { + Channel *c = ci->c; + + if (!c) + { + source.Reply(CHAN_X_NOT_IN_USE, ci->name.c_str()); + return; + } + + this->Enforce(source, ci); + } + + void DoClear(CommandSource &source, ChannelInfo *ci) + { + bool override = !source.AccessFor(ci).HasPriv("AKICK"); + Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to clear the akick list"; + + ci->ClearAkick(); + source.Reply(_("Channel %s akick list has been cleared."), ci->name.c_str()); + } + +public: + CommandCSAKick(Module *creator) : Command(creator, "chanserv/akick", 2, 4) + { + this->SetDesc(_("Maintain the AutoKick list")); + this->SetSyntax(_("\037channel\037 ADD {\037nick\037 | \037mask\037} [\037reason\037]")); + this->SetSyntax(_("\037channel\037 DEL {\037nick\037 | \037mask\037 | \037entry-num\037 | \037list\037}")); + this->SetSyntax(_("\037channel\037 LIST [\037mask\037 | \037entry-num\037 | \037list\037]")); + this->SetSyntax(_("\037channel\037 VIEW [\037mask\037 | \037entry-num\037 | \037list\037]")); + this->SetSyntax(_("\037channel\037 ENFORCE")); + this->SetSyntax(_("\037channel\037 CLEAR")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + Anope::string chan = params[0]; + Anope::string cmd = params[1]; + Anope::string mask = params.size() > 2 ? params[2] : ""; + + ChannelInfo *ci = ChannelInfo::Find(params[0]); + if (ci == NULL) + { + source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str()); + return; + } + + bool is_list = cmd.equals_ci("LIST") || cmd.equals_ci("VIEW"); + + bool has_access = false; + if (source.AccessFor(ci).HasPriv("AKICK") || source.HasPriv("chanserv/access/modify")) + has_access = true; + else if (is_list && source.HasPriv("chanserv/access/list")) + has_access = true; + + if (mask.empty() && (cmd.equals_ci("ADD") || cmd.equals_ci("DEL"))) + this->OnSyntaxError(source, cmd); + else if (!has_access) + source.Reply(ACCESS_DENIED); + else if (!cmd.equals_ci("LIST") && !cmd.equals_ci("VIEW") && !cmd.equals_ci("ENFORCE") && Anope::ReadOnly) + source.Reply(_("Sorry, channel autokick list modification is temporarily disabled.")); + else if (cmd.equals_ci("ADD")) + this->DoAdd(source, ci, params); + else if (cmd.equals_ci("DEL")) + this->DoDel(source, ci, params); + else if (cmd.equals_ci("LIST")) + this->DoList(source, ci, params); + else if (cmd.equals_ci("VIEW")) + this->DoView(source, ci, params); + else if (cmd.equals_ci("ENFORCE")) + this->DoEnforce(source, ci); + else if (cmd.equals_ci("CLEAR")) + this->DoClear(source, ci); + else + this->OnSyntaxError(source, ""); + + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + BotInfo *bi = Config->GetClient("NickServ"); + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Maintains the \002AutoKick list\002 for a channel. If a user\n" + "on the AutoKick list attempts to join the channel,\n" + "%s will ban that user from the channel, then kick\n" + "the user.\n" + " \n" + "The \002AKICK ADD\002 command adds the given nick or usermask\n" + "to the AutoKick list. If a \037reason\037 is given with\n" + "the command, that reason will be used when the user is\n" + "kicked; if not, the default reason is \"User has been\n" + "banned from the channel\".\n" + "When akicking a \037registered nick\037 the %s account\n" + "will be added to the akick list instead of the mask.\n" + "All users within that nickgroup will then be akicked.\n"), + source.service->nick.c_str(), bi ? bi->nick.c_str() : "NickServ"); + source.Reply(_( + " \n" + "The \002AKICK DEL\002 command removes the given nick or mask\n" + "from the AutoKick list. It does not, however, remove any\n" + "bans placed by an AutoKick; those must be removed\n" + "manually.\n" + " \n" + "The \002AKICK LIST\002 command displays the AutoKick list, or\n" + "optionally only those AutoKick entries which match the\n" + "given mask.\n" + " \n" + "The \002AKICK VIEW\002 command is a more verbose version of the\n" + "\002AKICK LIST\002 command.\n" + " \n" + "The \002AKICK ENFORCE\002 command causes %s to enforce the\n" + "current AKICK list by removing those users who match an\n" + "AKICK mask.\n" + " \n" + "The \002AKICK CLEAR\002 command clears all entries of the\n" + "akick list."), source.service->nick.c_str()); + return true; + } +}; + +class CSAKick final + : public Module +{ + CommandCSAKick commandcsakick; + +public: + CSAKick(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandcsakick(this) + { + } + + EventReturn OnCheckKick(User *u, Channel *c, Anope::string &mask, Anope::string &reason) override + { + if (!c->ci || c->MatchesList(u, "EXCEPT")) + return EVENT_CONTINUE; + + for (unsigned j = 0, end = c->ci->GetAkickCount(); j < end; ++j) + { + AutoKick *autokick = c->ci->GetAkick(j); + bool kick = false; + + if (autokick->nc) + kick = autokick->nc == u->Account(); + else if (IRCD->IsChannelValid(autokick->mask)) + { + Channel *chan = Channel::Find(autokick->mask); + kick = chan != NULL && chan->FindUser(u); + } + else + kick = Entry("BAN", autokick->mask).Matches(u); + + if (kick) + { + Log(LOG_DEBUG_2) << u->nick << " matched akick " << (autokick->nc ? autokick->nc->display : autokick->mask); + autokick->last_used = Anope::CurTime; + if (!autokick->nc && autokick->mask.find('#') == Anope::string::npos) + mask = autokick-