Anope IRC Services  Version 2.0
hs_list.cpp
Go to the documentation of this file.
1 /* HostServ core functions
2  *
3  * (C) 2003-2014 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 
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 
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  {
91  if (((counter >= from && counter <= to) || (!from && !to)) && display_counter < listmax)
92  {
93  ++display_counter;
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 whos 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 {
150 
151  public:
152  HSList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
153  commandhslist(this)
154  {
155  }
156 };
157 
Anope::string nick
Definition: account.h:37
const Anope::string & GetVhostHost() const
Definition: nickalias.cpp:106
bool HasVhost() const
Definition: nickalias.cpp:96
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: hs_list.cpp:132
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: hs_list.cpp:23
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
CoreExport Serialize::Checker< nickalias_map > NickAliasList
CommandHSList(Module *creator)
Definition: hs_list.cpp:17
string substr(size_type pos=0, size_type n=npos) const
Definition: anope.h:277
std::map< Anope::string, Anope::string > ListEntry
Definition: lists.h:68
size_type length() const
Definition: anope.h:131
Definition: Config.cs:26
CoreExport bool Match(const string &str, const string &mask, bool case_sensitive=false, bool use_regex=false)
Definition: misc.cpp:407
static const size_type npos
Definition: anope.h:44
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
HSList(const Anope::string &modname, const Anope::string &creator)
Definition: hs_list.cpp:152
#define LIST_INCORRECT_RANGE
Definition: language.h:90
#define MODULE_INIT(x)
Definition: modules.h:45
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
Anope::string stringify(const T &x)
Definition: anope.h:710
const Anope::string & GetVhostCreator() const
Definition: nickalias.cpp:111
void SendSyntax(CommandSource &)
Definition: command.cpp:145
const char * c_str() const
Definition: anope.h:117
CommandHSList commandhslist
Definition: hs_list.cpp:149
CoreExport Anope::string strftime(time_t t, const NickCore *nc=NULL, bool short_output=false)
Definition: misc.cpp:356
time_t GetVhostCreated() const
Definition: nickalias.cpp:116
size_type find(const string &_str, size_type pos=0) const
Definition: anope.h:192
#define _(x)
Definition: services.h:50
ListFormatter & AddColumn(const Anope::string &name)
Definition: misc.cpp:128
const Anope::string & GetVhostIdent() const
Definition: nickalias.cpp:101
Module * owner
Definition: service.h:84