hs_list.cpp

Go to the documentation of this file.
00001 /* HostServ core functions
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  */
00011 
00012 /*************************************************************************/
00013 
00014 #include "module.h"
00015 
00016 class CommandHSList : public Command
00017 {
00018  public:
00019         CommandHSList(Module *creator) : Command(creator, "hostserv/list", 0, 1)
00020         {
00021                 this->SetDesc(_("Displays one or more vhost entries"));
00022                 this->SetSyntax(_("[\037key\037|\037#X-Y\037]"));
00023         }
00024 
00025         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00026         {
00027                 const Anope::string &key = !params.empty() ? params[0] : "";
00028                 int from = 0, to = 0, counter = 1;
00029 
00034                 if (!key.empty() && key[0] == '#')
00035                 {
00036                         size_t tmp = key.find('-');
00037                         if (tmp == Anope::string::npos || tmp == key.length() || tmp == 1)
00038                         {
00039                                 source.Reply(LIST_INCORRECT_RANGE);
00040                                 return;
00041                         }
00042                         for (unsigned i = 1, end = key.length(); i < end; ++i)
00043                         {
00044                                 if (!isdigit(key[i]) && i != tmp)
00045                                 {
00046                                         source.Reply(LIST_INCORRECT_RANGE);
00047                                         return;
00048                                 }
00049                                 try
00050                                 {
00051                                         from = convertTo<int>(key.substr(1, tmp - 1));
00052                                         to = convertTo<int>(key.substr(tmp + 1));
00053                                 }
00054                                 catch (const ConvertException &) { }
00055                         }
00056                 }
00057 
00058                 unsigned display_counter = 0;
00059                 ListFormatter list;
00060                 list.AddColumn("Number").AddColumn("Nick").AddColumn("Vhost").AddColumn("Creator").AddColumn("Created");
00061 
00062                 for (nickalias_map::const_iterator it = NickAliasList->begin(), it_end = NickAliasList->end(); it != it_end; ++it)
00063                 {
00064                         const NickAlias *na = it->second;
00065 
00066                         if (!na->HasVhost())
00067                                 continue;
00068 
00069                         if (!key.empty() && key[0] != '#')
00070                         {
00071                                 if ((Anope::Match(na->nick, key) || Anope::Match(na->GetVhostHost(), key)) && display_counter < Config->NSListMax)
00072                                 {
00073                                         ++display_counter;
00074 
00075                                         ListFormatter::ListEntry entry;
00076                                         entry["Number"] = stringify(display_counter);
00077                                         entry["Nick"] = na->nick;
00078                                         if (!na->GetVhostIdent().empty())
00079                                                 entry["Vhost"] = na->GetVhostIdent() + "@" + na->GetVhostHost();
00080                                         else
00081                                                 entry["Vhost"] = na->GetVhostHost();
00082                                         entry["Creator"] = na->GetVhostCreator();
00083                                         entry["Created"] = Anope::strftime(na->GetVhostCreated());
00084                                         list.AddEntry(entry);
00085                                 }
00086                         }
00087                         else
00088                         {
00093                                 if (((counter >= from && counter <= to) || (!from && !to)) && display_counter < Config->NSListMax)
00094                                 {
00095                                         ++display_counter;
00096                                         ListFormatter::ListEntry entry;
00097                                         entry["Number"] = stringify(display_counter);
00098                                         entry["Nick"] = na->nick;
00099                                         if (!na->GetVhostIdent().empty())
00100                                                 entry["Vhost"] = na->GetVhostIdent() + "@" + na->GetVhostHost();
00101                                         else
00102                                                 entry["Vhost"] = na->GetVhostHost();
00103                                         entry["Creator"] = na->GetVhostCreator();
00104                                         entry["Created"] = Anope::strftime(na->GetVhostCreated());
00105                                         list.AddEntry(entry);
00106                                 }
00107                         }
00108                         ++counter;
00109                 }
00110 
00111                 if (!display_counter)
00112                 {
00113                         source.Reply(_("No records to display."));
00114                         return;
00115                 }
00116 
00117                 if (!key.empty())
00118                         source.Reply(_("Displayed records matching key \002%s\002 (count: \002%d\002)."), key.c_str(), display_counter);
00119                 else
00120                 {
00121                         if (from)
00122                                 source.Reply(_("Displayed records from \002%d\002 to \002%d\002."), from, to);
00123                         else
00124                                 source.Reply(_("Displayed all records (count: \002%d\002)."), display_counter);
00125                 }
00126 
00127                 std::vector<Anope::string> replies;
00128                 list.Process(replies);
00129 
00130                 for (unsigned i = 0; i < replies.size(); ++i)
00131                         source.Reply(replies[i]);
00132         }
00133 
00134         bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
00135         {
00136                 this->SendSyntax(source);
00137                 source.Reply(" ");
00138                 source.Reply(_("This command lists registered vhosts to the operator\n"
00139                                 "if a \037key\037 is specified, only entries whos nick or vhost match\n"
00140                                 "the pattern given in \037key\037 are displayed e.g. Rob* for all\n"
00141                                 "entries beginning with \"Rob\"\n"
00142                                 "If a \037#X-Y\037 style is used, only entries between the range of \002X\002\n"
00143                                 "and \002Y\002 will be displayed, e.g. \002#1-3\002 will display the first 3\n"
00144                                 "nick/vhost entries.\n"
00145                                 "The list uses the value of NSListMax as a hard limit for the\n"
00146                                 "number of items to display to a operator at any one time."));
00147                 return true;
00148         }
00149 };
00150 
00151 class HSList : public Module
00152 {
00153         CommandHSList commandhslist;
00154 
00155  public:
00156         HSList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
00157                 commandhslist(this)
00158         {
00159                 this->SetAuthor("Anope");
00160 
00161         }
00162 };
00163 
00164 MODULE_INIT(HSList)