anope

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

hs_list.cpp (4728B)

      1 /* HostServ 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 CommandHSList : public Command
     15 {
     16  public:
     17 	CommandHSList(Module *creator) : Command(creator, "hostserv/list", 0, 1)
     18 	{
     19 		this->SetDesc(_("Displays one or more vhost entries"));
     20 		this->SetSyntax(_("[\037key\037|\037#X-Y\037]"));
     21 	}
     22 
     23 	void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
     24 	{
     25 		const Anope::string &key = !params.empty() ? params[0] : "";
     26 		int from = 0, to = 0, counter = 1;
     27 
     28 		/**
     29 		 * Do a check for a range here, then in the next loop
     30 		 * we'll only display what has been requested..
     31 		 **/
     32 		if (!key.empty() && key[0] == '#')
     33 		{
     34 			size_t tmp = key.find('-');
     35 			if (tmp == Anope::string::npos || tmp == key.length() || tmp == 1)
     36 			{
     37 				source.Reply(LIST_INCORRECT_RANGE);
     38 				return;
     39 			}
     40 			for (unsigned i = 1, end = key.length(); i < end; ++i)
     41 			{
     42 				if (!isdigit(key[i]) && i != tmp)
     43 				{
     44 					source.Reply(LIST_INCORRECT_RANGE);
     45 					return;
     46 				}
     47 				try
     48 				{
     49 					from = convertTo<int>(key.substr(1, tmp - 1));
     50 					to = convertTo<int>(key.substr(tmp + 1));
     51 				}
     52 				catch (const ConvertException &) { }
     53 			}
     54 		}
     55 
     56 		unsigned display_counter = 0, listmax = Config->GetModule(this->owner)->Get<unsigned>("listmax", "50");
     57 		ListFormatter list(source.GetAccount());
     58 		list.AddColumn(_("Number")).AddColumn(_("Nick")).AddColumn(_("Vhost")).AddColumn(_("Creator")).AddColumn(_("Created"));
     59 
     60 		for (nickalias_map::const_iterator it = NickAliasList->begin(), it_end = NickAliasList->end(); it != it_end; ++it)
     61 		{
     62 			const NickAlias *na = it->second;
     63 
     64 			if (!na->HasVhost())
     65 				continue;
     66 
     67 			if (!key.empty() && key[0] != '#')
     68 			{
     69 				if ((Anope::Match(na->nick, key) || Anope::Match(na->GetVhostHost(), key)) && display_counter < listmax)
     70 				{
     71 					++display_counter;
     72 
     73 					ListFormatter::ListEntry entry;
     74 					entry["Number"] = stringify(display_counter);
     75 					entry["Nick"] = na->nick;
     76 					if (!na->GetVhostIdent().empty())
     77 						entry["Vhost"] = na->GetVhostIdent() + "@" + na->GetVhostHost();
     78 					else
     79 						entry["Vhost"] = na->GetVhostHost();
     80 					entry["Creator"] = na->GetVhostCreator();
     81 					entry["Created"] = Anope::strftime(na->GetVhostCreated(), NULL, true);
     82 					list.AddEntry(entry);
     83 				}
     84 			}
     85 			else
     86 			{
     87 				/**
     88 				 * List the host if its in the display range, and not more
     89 				 * than NSListMax records have been displayed...
     90 				 **/
     91 				if (((counter >= from && counter <= to) || (!from && !to)) && display_counter < listmax)
     92 				{
     93 					++display_counter;
     94 					ListFormatter::ListEntry entry;
     95 					entry["Number"] = stringify(display_counter);
     96 					entry["Nick"] = na->nick;
     97 					if (!na->GetVhostIdent().empty())
     98 						entry["Vhost"] = na->GetVhostIdent() + "@" + na->GetVhostHost();
     99 					else
    100 						entry["Vhost"] = na->GetVhostHost();
    101 					entry["Creator"] = na->GetVhostCreator();
    102 					entry["Created"] = Anope::strftime(na->GetVhostCreated(), NULL, true);
    103 					list.AddEntry(entry);
    104 				}
    105 			}
    106 			++counter;
    107 		}
    108 
    109 		if (!display_counter)
    110 		{
    111 			source.Reply(_("No records to display."));
    112 			return;
    113 		}
    114 
    115 		if (!key.empty())
    116 			source.Reply(_("Displayed records matching key \002%s\002 (count: \002%d\002)."), key.c_str(), display_counter);
    117 		else
    118 		{
    119 			if (from)
    120 				source.Reply(_("Displayed records from \002%d\002 to \002%d\002."), from, to);
    121 			else
    122 				source.Reply(_("Displayed all records (count: \002%d\002)."), display_counter);
    123 		}
    124 
    125 		std::vector<Anope::string> replies;
    126 		list.Process(replies);
    127 
    128 		for (unsigned i = 0; i < replies.size(); ++i)
    129 			source.Reply(replies[i]);
    130 	}
    131 
    132 	bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
    133 	{
    134 		this->SendSyntax(source);
    135 		source.Reply(" ");
    136 		source.Reply(_("This command lists registered vhosts to the operator.\n"
    137 				"If a \037key\037 is specified, only entries whose nick or vhost match\n"
    138 				"the pattern given in \037key\037 are displayed e.g. Rob* for all\n"
    139 				"entries beginning with \"Rob\"\n"
    140 				"If a \037#X-Y\037 style is used, only entries between the range of \002X\002\n"
    141 				"and \002Y\002 will be displayed, e.g. \002#1-3\002 will display the first 3\n"
    142 				"nick/vhost entries."));
    143 		return true;
    144 	}
    145 };
    146 
    147 class HSList : public Module
    148 {
    149 	CommandHSList commandhslist;
    150 
    151  public:
    152 	HSList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
    153 		commandhslist(this)
    154 	{
    155 		if (!IRCD || !IRCD->CanSetVHost)
    156 			throw ModuleException("Your IRCd does not support vhosts");
    157 	}
    158 };
    159 
    160 MODULE_INIT(HSList)