anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

cs_akick.cpp (17540B)

      1 /* ChanServ core functions
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #include "module.h"
     13 
     14 class CommandCSAKick : public Command
     15 {
     16 	void Enforce(CommandSource &source, ChannelInfo *ci)
     17 	{
     18 		Channel *c = ci->c;
     19 		int count = 0;
     20 
     21 		if (!c)
     22 		{
     23 			return;
     24 		}
     25 
     26 		for (Channel::ChanUserList::iterator it = c->users.begin(), it_end = c->users.end(); it != it_end; )
     27 		{
     28 			ChanUserContainer *uc = it->second;
     29 			++it;
     30 
     31 			if (c->CheckKick(uc->user))
     32 				++count;
     33 		}
     34 
     35 		bool override = !source.AccessFor(ci).HasPriv("AKICK");
     36 		Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "ENFORCE, affects " << count << " users";
     37 
     38 		source.Reply(_("AKICK ENFORCE for \002%s\002 complete; \002%d\002 users were affected."), ci->name.c_str(), count);
     39 	}
     40 
     41 	void DoAdd(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
     42 	{
     43 		Anope::string mask = params[2];
     44 		Anope::string reason = params.size() > 3 ? params[3] : "";
     45 		const NickAlias *na = NickAlias::Find(mask);
     46 		NickCore *nc = NULL;
     47 		const AutoKick *akick;
     48 		unsigned reasonmax = Config->GetModule("chanserv")->Get<unsigned>("reasonmax", "200");
     49 
     50 		if (reason.length() > reasonmax)
     51 			reason = reason.substr(0, reasonmax);
     52 
     53 		if (IRCD->IsExtbanValid(mask))
     54 			; /* If this is an extban don't try to complete the mask */
     55 		else if (IRCD->IsChannelValid(mask))
     56 		{
     57 			/* Also don't try to complete the mask if this is a channel */
     58 
     59 			if (mask.equals_ci(ci->name) && ci->HasExt("PEACE"))
     60 			{
     61 				source.Reply(ACCESS_DENIED);
     62 				return;
     63 			}
     64 		}
     65 		else if (!na)
     66 		{
     67 			/* If the mask contains a realname the reason must be prepended with a : */
     68 			if (mask.find('#') != Anope::string::npos)
     69 			{
     70 				size_t r = reason.find(':');
     71 				if (r != Anope::string::npos)
     72 				{
     73 					mask += " " + reason.substr(0, r);
     74 					mask.trim();
     75 					reason = reason.substr(r + 1);
     76 					reason.trim();
     77 				}
     78 				else
     79 				{
     80 					mask = mask + " " + reason;
     81 					reason.clear();
     82 				}
     83 			}
     84 
     85 			Entry e("", mask);
     86 
     87 			mask = (e.nick.empty() ? "*" : e.nick) + "!"
     88 				+ (e.user.empty() ? "*" : e.user) + "@"
     89 				+ (e.host.empty() ? "*" : e.host);
     90 			if (!e.real.empty())
     91 				mask += "#" + e.real;
     92 		}
     93 		else
     94 			nc = na->nc;
     95 
     96 		/* Check excepts BEFORE we get this far */
     97 		if (ci->c)
     98 		{
     99 			std::vector<Anope::string> modes = ci->c->GetModeList("EXCEPT");
    100 			for (unsigned int i = 0; i < modes.size(); ++i)
    101 			{
    102 				if (Anope::Match(modes[i], mask))
    103 				{
    104 					source.Reply(CHAN_EXCEPTED, mask.c_str(), ci->name.c_str());
    105 					return;
    106 				}
    107 			}
    108 		}
    109 
    110 		bool override = !source.AccessFor(ci).HasPriv("AKICK");
    111 		/* Opers overriding get to bypass PEACE */
    112 		if (override)
    113 			;
    114 		/* These peace checks are only for masks */
    115 		else if (IRCD->IsChannelValid(mask))
    116 			;
    117 		/* Check whether target nick has equal/higher access
    118 		* or whether the mask matches a user with higher/equal access - Viper */
    119 		else if (ci->HasExt("PEACE") && nc)
    120 		{
    121 			AccessGroup nc_access = ci->AccessFor(nc), u_access = source.AccessFor(ci);
    122 			if (nc == ci->GetFounder() || nc_access >= u_access)
    123 			{
    124 				source.Reply(ACCESS_DENIED);
    125 				return;
    126 			}
    127 		}
    128 		else if (ci->HasExt("PEACE"))
    129 		{
    130 			/* Match against all currently online users with equal or
    131 			 * higher access. - Viper */
    132 			for (user_map::const_iterator it = UserListByNick.begin(); it != UserListByNick.end(); ++it)
    133 			{
    134 				User *u2 = it->second;
    135 
    136 				AccessGroup nc_access = ci->AccessFor(nc), u_access = source.AccessFor(ci);
    137 				Entry entry_mask("", mask);
    138 
    139 				if ((ci->AccessFor(u2).HasPriv("FOUNDER") || nc_access >= u_access) && entry_mask.Matches(u2))
    140 				{
    141 					source.Reply(ACCESS_DENIED);
    142 					return;
    143 				}
    144 			}
    145 
    146 			/* Match against the lastusermask of all nickalias's with equal
    147 			 * or higher access. - Viper */
    148 			for (nickalias_map::const_iterator it = NickAliasList->begin(), it_end = NickAliasList->end(); it != it_end; ++it)
    149 			{
    150 				na = it->second;
    151 
    152 				AccessGroup nc_access = ci->AccessFor(na->nc), u_access = source.AccessFor(ci);
    153 				if (na->nc && (na->nc == ci->GetFounder() || nc_access >= u_access))
    154 				{
    155 					Anope::string buf = na->nick + "!" + na->last_usermask;
    156 					if (Anope::Match(buf, mask))
    157 					{
    158 						source.Reply(ACCESS_DENIED);
    159 						return;
    160 					}
    161 				}
    162 			 }
    163 		}
    164 
    165 		for (unsigned j = 0, end = ci->GetAkickCount(); j < end; ++j)
    166 		{
    167 			akick = ci->GetAkick(j);
    168 			if (akick->nc ? akick->nc == nc : mask.equals_ci(akick->mask))
    169 			{
    170 				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());
    171 				return;
    172 			}
    173 		}
    174 
    175 		if (ci->GetAkickCount() >= Config->GetModule(this->owner)->Get<unsigned>("autokickmax"))
    176 		{
    177 			source.Reply(_("Sorry, you can only have %d autokick masks on a channel."), Config->GetModule(this->owner)->Get<unsigned>("autokickmax"));
    178 			return;
    179 		}
    180 
    181 		if (nc)
    182 			akick = ci->AddAkick(source.GetNick(), nc, reason);
    183 		else
    184 			akick = ci->AddAkick(source.GetNick(), mask, reason);
    185 
    186 		Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to add " << mask << (reason == "" ? "" : ": ") << reason;
    187 
    188 		FOREACH_MOD(OnAkickAdd, (source, ci, akick));
    189 
    190 		source.Reply(_("\002%s\002 added to %s autokick list."), mask.c_str(), ci->name.c_str());
    191 
    192 		this->Enforce(source, ci);
    193 	}
    194 
    195 	void DoDel(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
    196 	{
    197 		const Anope::string &mask = params[2];
    198 		unsigned i, end;
    199 
    200 		if (!ci->GetAkickCount())
    201 		{
    202 			source.Reply(_("%s autokick list is empty."), ci->name.c_str());
    203 			return;
    204 		}
    205 
    206 		/* Special case: is it a number/list?  Only do search if it isn't. */
    207 		if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
    208 		{
    209 			class AkickDelCallback : public NumberList
    210 			{
    211 				CommandSource &source;
    212 				ChannelInfo *ci;
    213 				Command *c;
    214 				unsigned deleted;
    215 				AccessGroup ag;
    216 			 public:
    217 				AkickDelCallback(CommandSource &_source, ChannelInfo *_ci, Command *_c, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), c(_c), deleted(0), ag(source.AccessFor(ci))
    218 				{
    219 				}
    220 
    221 				~AkickDelCallback()
    222 				{
    223 					if (!deleted)
    224 						source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str());
    225 					else if (deleted == 1)
    226 						source.Reply(_("Deleted 1 entry from %s autokick list."), ci->name.c_str());
    227 					else
    228 						source.Reply(_("Deleted %d entries from %s autokick list."), deleted, ci->name.c_str());
    229 				}
    230 
    231 				void HandleNumber(unsigned number) anope_override
    232 				{
    233 					if (!number || number > ci->GetAkickCount())
    234 						return;
    235 
    236 					const AutoKick *akick = ci->GetAkick(number - 1);
    237 
    238 					FOREACH_MOD(OnAkickDel, (source, ci, akick));
    239 
    240 					bool override = !ag.HasPriv("AKICK");
    241 					Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "to delete " << (akick->nc ? akick->nc->display : akick->mask);
    242 
    243 					++deleted;
    244 					ci->EraseAkick(number - 1);
    245 				}
    246 			}
    247 			delcallback(source, ci, this, mask);
    248 			delcallback.Process();
    249 		}
    250 		else
    251 		{
    252 			const NickAlias *na = NickAlias::Find(mask);
    253 			const NickCore *nc = na ? *na->nc : NULL;
    254 
    255 			for (i = 0, end = ci->GetAkickCount(); i < end; ++i)
    256 			{
    257 				const AutoKick *akick = ci->GetAkick(i);
    258 
    259 				if (akick->nc ? akick->nc == nc : mask.equals_ci(akick->mask))
    260 					break;
    261 			}
    262 
    263 			if (i == ci->GetAkickCount())
    264 			{
    265 				source.Reply(_("\002%s\002 not found on %s autokick list."), mask.c_str(), ci->name.c_str());
    266 				return;
    267 			}
    268 
    269 			bool override = !source.AccessFor(ci).HasPriv("AKICK");
    270 			Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << mask;
    271 
    272 			FOREACH_MOD(OnAkickDel, (source, ci, ci->GetAkick(i)));
    273 
    274 			ci->EraseAkick(i);
    275 
    276 			source.Reply(_("\002%s\002 deleted from %s autokick list."), mask.c_str(), ci->name.c_str());
    277 		}
    278 	}
    279 
    280 	void ProcessList(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params, ListFormatter &list)
    281 	{
    282 		const Anope::string &mask = params.size() > 2 ? params[2] : "";
    283 
    284 		if (!mask.empty() && isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
    285 		{
    286 			class AkickListCallback : public NumberList
    287 			{
    288 				ListFormatter &list;
    289 				ChannelInfo *ci;
    290 
    291 			 public:
    292 				AkickListCallback(ListFormatter &_list, ChannelInfo *_ci, const Anope::string &numlist) : NumberList(numlist, false), list(_list), ci(_ci)
    293 				{
    294 				}
    295 
    296 				void HandleNumber(unsigned number) anope_override
    297 				{
    298 					if (!number || number > ci->GetAkickCount())
    299 						return;
    300 
    301 					const AutoKick *akick = ci->GetAkick(number - 1);
    302 
    303 					Anope::string timebuf, lastused;
    304 					if (akick->addtime)
    305 						timebuf = Anope::strftime(akick->addtime, NULL, true);
    306 					else
    307 						timebuf = UNKNOWN;
    308 					if (akick->last_used)
    309 						lastused = Anope::strftime(akick->last_used, NULL, true);
    310 					else
    311 						lastused = UNKNOWN;
    312 
    313 					ListFormatter::ListEntry entry;
    314 					entry["Number"] = stringify(number);
    315 					if (akick->nc)
    316 						entry["Mask"] = akick->nc->display;
    317 					else
    318 						entry["Mask"] = akick->mask;
    319 					entry["Creator"] = akick->creator;
    320 					entry["Created"] = timebuf;
    321 					entry["Last used"] = lastused;
    322 					entry["Reason"] = akick->reason;
    323 					this->list.AddEntry(entry);
    324 				}
    325 			}
    326 			nl_list(list, ci, mask);
    327 			nl_list.Process();
    328 		}
    329 		else
    330 		{
    331 			for (unsigned i = 0, end = ci->GetAkickCount(); i < end; ++i)
    332 			{
    333 				const AutoKick *akick = ci->GetAkick(i);
    334 
    335 				if (!mask.empty())
    336 				{
    337 					if (!akick->nc && !Anope::Match(akick->mask, mask))
    338 						continue;
    339 					if (akick->nc && !Anope::Match(akick->nc->display, mask))
    340 						continue;
    341 				}
    342 
    343 				Anope::string timebuf, lastused;
    344 				if (akick->addtime)
    345 					timebuf = Anope::strftime(akick->addtime, NULL, true);
    346 				else
    347 					timebuf = UNKNOWN;
    348 				if (akick->last_used)
    349 					lastused = Anope::strftime(akick->last_used, NULL, true);
    350 				else
    351 					lastused = UNKNOWN;
    352 
    353 				ListFormatter::ListEntry entry;
    354 				entry["Number"] = stringify(i + 1);
    355 				if (akick->nc)
    356 					entry["Mask"] = akick->nc->display;
    357 				else
    358 					entry["Mask"] = akick->mask;
    359 				entry["Creator"] = akick->creator;
    360 				entry["Created"] = timebuf;
    361 				entry["Last used"] = lastused;
    362 				entry["Reason"] = akick->reason;
    363 				list.AddEntry(entry);
    364 			}
    365 		}
    366 
    367 		if (list.IsEmpty())
    368 			source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str());
    369 		else
    370 		{
    371 			std::vector<Anope::string> replies;
    372 			list.Process(replies);
    373 
    374 			source.Reply(_("Autokick list for %s:"), ci->name.c_str());
    375 
    376 			for (unsigned i = 0; i < replies.size(); ++i)
    377 				source.Reply(replies[i]);
    378 
    379 			source.Reply(_("End of autokick list"));
    380 		}
    381 	}
    382 
    383 	void DoList(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
    384 	{
    385 		if (!ci->GetAkickCount())
    386 		{
    387 			source.Reply(_("%s autokick list is empty."), ci->name.c_str());
    388 			return;
    389 		}
    390 
    391 		ListFormatter list(source.GetAccount());
    392 		list.AddColumn(_("Number")).AddColumn(_("Mask")).AddColumn(_("Reason"));
    393 		this->ProcessList(source, ci, params, list);
    394 	}
    395 
    396 	void DoView(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
    397 	{
    398 		if (!ci->GetAkickCount())
    399 		{
    400 			source.Reply(_("%s autokick list is empty."), ci->name.c_str());
    401 			return;
    402 		}
    403 
    404 		ListFormatter list(source.GetAccount());
    405 		list.AddColumn(_("Number")).AddColumn(_("Mask")).AddColumn(_("Creator")).AddColumn(_("Created")).AddColumn(_("Last used")).AddColumn(_("Reason"));
    406 		this->ProcessList(source, ci, params, list);
    407 	}
    408 
    409 	void DoEnforce(CommandSource &source, ChannelInfo *ci)
    410 	{
    411 		Channel *c = ci->c;
    412 
    413 		if (!c)
    414 		{
    415 			source.Reply(CHAN_X_NOT_IN_USE, ci->name.c_str());
    416 			return;
    417 		}
    418 
    419 		this->Enforce(source, ci);
    420 	}
    421 
    422 	void DoClear(CommandSource &source, ChannelInfo *ci)
    423 	{
    424 		bool override = !source.AccessFor(ci).HasPriv("AKICK");
    425 		Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to clear the akick list";
    426 
    427 		ci->ClearAkick();
    428 		source.Reply(_("Channel %s akick list has been cleared."), ci->name.c_str());
    429 	}
    430 
    431  public:
    432 	CommandCSAKick(Module *creator) : Command(creator, "chanserv/akick", 2, 4)
    433 	{
    434 		this->SetDesc(_("Maintain the AutoKick list"));
    435 		this->SetSyntax(_("\037channel\037 ADD {\037nick\037 | \037mask\037} [\037reason\037]"));
    436 		this->SetSyntax(_("\037channel\037 DEL {\037nick\037 | \037mask\037 | \037entry-num\037 | \037list\037}"));
    437 		this->SetSyntax(_("\037channel\037 LIST [\037mask\037 | \037entry-num\037 | \037list\037]"));
    438 		this->SetSyntax(_("\037channel\037 VIEW [\037mask\037 | \037entry-num\037 | \037list\037]"));
    439 		this->SetSyntax(_("\037channel\037 ENFORCE"));
    440 		this->SetSyntax(_("\037channel\037 CLEAR"));
    441 	}
    442 
    443 	void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
    444 	{
    445 		Anope::string chan = params[0];
    446 		Anope::string cmd = params[1];
    447 		Anope::string mask = params.size() > 2 ? params[2] : "";
    448 
    449 		ChannelInfo *ci = ChannelInfo::Find(params[0]);
    450 		if (ci == NULL)
    451 		{
    452 			source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
    453 			return;
    454 		}
    455 
    456 		bool is_list = cmd.equals_ci("LIST") || cmd.equals_ci("VIEW");
    457 
    458 		bool has_access = false;
    459 		if (source.AccessFor(ci).HasPriv("AKICK") || source.HasPriv("chanserv/access/modify"))
    460 			has_access = true;
    461 		else if (is_list && source.HasPriv("chanserv/access/list"))
    462 			has_access = true;
    463 
    464 		if (mask.empty() && (cmd.equals_ci("ADD") || cmd.equals_ci("DEL")))
    465 			this->OnSyntaxError(source, cmd);
    466 		else if (!has_access)
    467 			source.Reply(ACCESS_DENIED);
    468 		else if (!cmd.equals_ci("LIST") && !cmd.equals_ci("VIEW") && !cmd.equals_ci("ENFORCE") && Anope::ReadOnly)
    469 			source.Reply(_("Sorry, channel autokick list modification is temporarily disabled."));
    470 		else if (cmd.equals_ci("ADD"))
    471 			this->DoAdd(source, ci, params);
    472 		else if (cmd.equals_ci("DEL"))
    473 			this->DoDel(source, ci, params);
    474 		else if (cmd.equals_ci("LIST"))
    475 			this->DoList(source, ci, params);
    476 		else if (cmd.equals_ci("VIEW"))
    477 			this->DoView(source, ci, params);
    478 		else if (cmd.equals_ci("ENFORCE"))
    479 			this->DoEnforce(source, ci);
    480 		else if (cmd.equals_ci("CLEAR"))
    481 			this->DoClear(source, ci);
    482 		else
    483 			this->OnSyntaxError(source, "");
    484 
    485 		return;
    486 	}
    487 
    488 	bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
    489 	{
    490 		BotInfo *bi = Config->GetClient("NickServ");
    491 		this->SendSyntax(source);
    492 		source.Reply(" ");
    493 		source.Reply(_("Maintains the \002AutoKick list\002 for a channel.  If a user\n"
    494 				"on the AutoKick list attempts to join the channel,\n"
    495 				"%s will ban that user from the channel, then kick\n"
    496 				"the user.\n"
    497 				" \n"
    498 				"The \002AKICK ADD\002 command adds the given nick or usermask\n"
    499 				"to the AutoKick list.  If a \037reason\037 is given with\n"
    500 				"the command, that reason will be used when the user is\n"
    501 				"kicked; if not, the default reason is \"User has been\n"
    502 				"banned from the channel\".\n"
    503 				"When akicking a \037registered nick\037 the %s account\n"
    504 				"will be added to the akick list instead of the mask.\n"
    505 				"All users within that nickgroup will then be akicked.\n"),
    506 				source.service->nick.c_str(), bi ? bi->nick.c_str() : "NickServ");
    507 		source.Reply(_(
    508 				" \n"
    509 				"The \002AKICK DEL\002 command removes the given nick or mask\n"
    510 				"from the AutoKick list.  It does not, however, remove any\n"
    511 				"bans placed by an AutoKick; those must be removed\n"
    512 				"manually.\n"
    513 				" \n"
    514 				"The \002AKICK LIST\002 command displays the AutoKick list, or\n"
    515 				"optionally only those AutoKick entries which match the\n"
    516 				"given mask.\n"
    517 				" \n"
    518 				"The \002AKICK VIEW\002 command is a more verbose version of the\n"
    519 				"\002AKICK LIST\002 command.\n"
    520 				" \n"
    521 				"The \002AKICK ENFORCE\002 command causes %s to enforce the\n"
    522 				"current AKICK list by removing those users who match an\n"
    523 				"AKICK mask.\n"
    524 				" \n"
    525 				"The \002AKICK CLEAR\002 command clears all entries of the\n"
    526 				"akick list."), source.service->nick.c_str());
    527 		return true;
    528 	}
    529 };
    530 
    531 class CSAKick : public Module
    532 {
    533 	CommandCSAKick commandcsakick;
    534 
    535  public:
    536 	CSAKick(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
    537 		commandcsakick(this)
    538 	{
    539 	}
    540 
    541 	EventReturn OnCheckKick(User *u, Channel *c, Anope::string &mask, Anope::string &reason) anope_override
    542 	{
    543 		if (!c->ci || c->MatchesList(u, "EXCEPT"))
    544 			return EVENT_CONTINUE;
    545 
    546 		for (unsigned j = 0, end = c->ci->GetAkickCount(); j < end; ++j)
    547 		{
    548 			AutoKick *autokick = c->ci->GetAkick(j);
    549 			bool kick = false;
    550 
    551 			if (autokick->nc)
    552 				kick = autokick->nc == u->Account();
    553 			else if (IRCD->IsChannelValid(autokick->mask))
    554 			{
    555 				Channel *chan = Channel::Find(autokick->mask);
    556 				kick = chan != NULL && chan->FindUser(u);
    557 			}
    558 			else
    559 				kick = Entry("BAN", autokick->mask).Matches(u);
    560 
    561 			if (kick)
    562 			{
    563 				Log(LOG_DEBUG_2) << u->nick << " matched akick " << (autokick->nc ? autokick->nc->display : autokick->mask);
    564 				autokick->last_used = Anope::CurTime;
    565 				if (!autokick->nc && autokick->mask.find('#') == Anope::string::npos)
    566 					mask = autokick->mask;
    567 				reason = autokick->reason;
    568 				if (reason.empty())
    569 				{
    570 					reason = Language::Translate(u, Config->GetModule(this)->Get<const Anope::string>("autokickreason").c_str());
    571 					reason = reason.replace_all_cs("%n", u->nick)
    572 							.replace_all_cs("%c", c->name);
    573 				}
    574 				if (reason.empty())
    575 					reason = Language::Translate(u, _("User has been banned from the channel"));
    576 				return EVENT_STOP;
    577 			}
    578 		}
    579 
    580 		return EVENT_CONTINUE;
    581 	}
    582 };
    583 
    584 MODULE_INIT(CSAKick)