Anope IRC Services  Version 2.0
os_list.cpp
Go to the documentation of this file.
1 /* OperServ 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 CommandOSChanList : public Command
15 {
16  public:
17  CommandOSChanList(Module *creator) : Command(creator, "operserv/chanlist", 0, 2)
18  {
19  this->SetDesc(_("Lists all channel records"));
20  this->SetSyntax(_("[{\037pattern\037 | \037nick\037} [\037SECRET\037]]"));
21  }
22 
23  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
24  {
25  const Anope::string &pattern = !params.empty() ? params[0] : "";
26  const Anope::string &opt = params.size() > 1 ? params[1] : "";
27  std::set<Anope::string> modes;
28  User *u2;
29 
30  if (!pattern.empty())
31  Log(LOG_ADMIN, source, this) << "for " << pattern;
32  else
33  Log(LOG_ADMIN, source, this);
34 
35  if (!opt.empty() && opt.equals_ci("SECRET"))
36  {
37  modes.insert("SECRET");
38  modes.insert("PRIVATE");
39  }
40 
41  ListFormatter list(source.GetAccount());
42  list.AddColumn(_("Name")).AddColumn(_("Users")).AddColumn(_("Modes")).AddColumn(_("Topic"));
43 
44  if (!pattern.empty() && (u2 = User::Find(pattern, true)))
45  {
46  source.Reply(_("\002%s\002 channel list:"), u2->nick.c_str());
47 
48  for (User::ChanUserList::iterator uit = u2->chans.begin(), uit_end = u2->chans.end(); uit != uit_end; ++uit)
49  {
50  ChanUserContainer *cc = uit->second;
51 
52  if (!modes.empty())
53  for (std::set<Anope::string>::iterator it = modes.begin(), it_end = modes.end(); it != it_end; ++it)
54  if (!cc->chan->HasMode(*it))
55  continue;
56 
58  entry["Name"] = cc->chan->name;
59  entry["Users"] = stringify(cc->chan->users.size());
60  entry["Modes"] = cc->chan->GetModes(true, true);
61  entry["Topic"] = cc->chan->topic;
62  list.AddEntry(entry);
63  }
64  }
65  else
66  {
67  source.Reply(_("Channel list:"));
68 
69  for (channel_map::const_iterator cit = ChannelList.begin(), cit_end = ChannelList.end(); cit != cit_end; ++cit)
70  {
71  Channel *c = cit->second;
72 
73  if (!pattern.empty() && !Anope::Match(c->name, pattern, false, true))
74  continue;
75  if (!modes.empty())
76  for (std::set<Anope::string>::iterator it = modes.begin(), it_end = modes.end(); it != it_end; ++it)
77  if (!c->HasMode(*it))
78  continue;
79 
81  entry["Name"] = c->name;
82  entry["Users"] = stringify(c->users.size());
83  entry["Modes"] = c->GetModes(true, true);
84  entry["Topic"] = c->topic;
85  list.AddEntry(entry);
86  }
87  }
88 
89  std::vector<Anope::string> replies;
90  list.Process(replies);
91 
92  for (unsigned i = 0; i < replies.size(); ++i)
93  source.Reply(replies[i]);
94 
95  source.Reply(_("End of channel list."));
96  }
97 
98  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
99  {
100  this->SendSyntax(source);
101  source.Reply(" ");
102  source.Reply(_("Lists all channels currently in use on the IRC network, whether they\n"
103  "are registered or not.\n"
104  " \n"
105  "If \002pattern\002 is given, lists only channels that match it. If a nickname\n"
106  "is given, lists only the channels the user using it is on. If SECRET is\n"
107  "specified, lists only channels matching \002pattern\002 that have the +s or\n"
108  "+p mode."));
109 
110  const Anope::string &regexengine = Config->GetBlock("options")->Get<const Anope::string>("regexengine");
111  if (!regexengine.empty())
112  {
113  source.Reply(" ");
114  source.Reply(_("Regex matches are also supported using the %s engine.\n"
115  "Enclose your pattern in // if this is desired."), regexengine.c_str());
116  }
117 
118  return true;
119  }
120 };
121 
123 {
124  public:
125  CommandOSUserList(Module *creator) : Command(creator, "operserv/userlist", 0, 2)
126  {
127  this->SetDesc(_("Lists all user records"));
128  this->SetSyntax(_("[{\037pattern\037 | \037channel\037} [\037INVISIBLE\037]]"));
129  }
130 
131  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
132  {
133  const Anope::string &pattern = !params.empty() ? params[0] : "";
134  const Anope::string &opt = params.size() > 1 ? params[1] : "";
135  Channel *c;
136  std::set<Anope::string> modes;
137 
138  if (!pattern.empty())
139  Log(LOG_ADMIN, source, this) << "for " << pattern;
140  else
141  Log(LOG_ADMIN, source, this);
142 
143  if (!opt.empty() && opt.equals_ci("INVISIBLE"))
144  modes.insert("INVIS");
145 
146  ListFormatter list(source.GetAccount());
147  list.AddColumn(_("Name")).AddColumn(_("Mask"));
148 
149  if (!pattern.empty() && (c = Channel::Find(pattern)))
150  {
151  source.Reply(_("\002%s\002 users list:"), pattern.c_str());
152 
153  for (Channel::ChanUserList::iterator cuit = c->users.begin(), cuit_end = c->users.end(); cuit != cuit_end; ++cuit)
154  {
155  ChanUserContainer *uc = cuit->second;
156 
157  if (!modes.empty())
158  for (std::set<Anope::string>::iterator it = modes.begin(), it_end = modes.end(); it != it_end; ++it)
159  if (!uc->user->HasMode(*it))
160  continue;
161 
163  entry["Name"] = uc->user->nick;
164  entry["Mask"] = uc->user->GetIdent() + "@" + uc->user->GetDisplayedHost();
165  list.AddEntry(entry);
166  }
167  }
168  else
169  {
170  /* Historically this has been ordered, so... */
171  Anope::map<User *> ordered_map;
172  for (user_map::const_iterator it = UserListByNick.begin(); it != UserListByNick.end(); ++it)
173  ordered_map[it->first] = it->second;
174 
175  source.Reply(_("Users list:"));
176 
177  for (Anope::map<User *>::const_iterator it = ordered_map.begin(); it != ordered_map.end(); ++it)
178  {
179  User *u2 = it->second;
180 
181  if (!pattern.empty())
182  {
183  Anope::string mask = u2->nick + "!" + u2->GetIdent() + "@" + u2->GetDisplayedHost(), mask2 = u2->nick + "!" + u2->GetIdent() + "@" + u2->host, mask3 = u2->nick + "!" + u2->GetIdent() + "@" + u2->ip.addr();
184  if (!Anope::Match(mask, pattern) && !Anope::Match(mask2, pattern) && !Anope::Match(mask3, pattern))
185  continue;
186  if (!modes.empty())
187  for (std::set<Anope::string>::iterator mit = modes.begin(), mit_end = modes.end(); mit != mit_end; ++mit)
188  if (!u2->HasMode(*mit))
189  continue;
190  }
191 
193  entry["Name"] = u2->nick;
194  entry["Mask"] = u2->GetIdent() + "@" + u2->GetDisplayedHost();
195  list.AddEntry(entry);
196  }
197  }
198 
199  std::vector<Anope::string> replies;
200  list.Process(replies);
201 
202  for (unsigned i = 0; i < replies.size(); ++i)
203  source.Reply(replies[i]);
204 
205  source.Reply(_("End of users list."));
206  return;
207  }
208 
209  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
210  {
211  this->SendSyntax(source);
212  source.Reply(" ");
213  source.Reply(_("Lists all users currently online on the IRC network, whether their\n"
214  "nick is registered or not.\n"
215  " \n"
216  "If \002pattern\002 is given, lists only users that match it (it must be in\n"
217  "the format nick!user@host). If \002channel\002 is given, lists only users\n"
218  "that are on the given channel. If INVISIBLE is specified, only users\n"
219  "with the +i flag will be listed."));
220 
221  const Anope::string &regexengine = Config->GetBlock("options")->Get<const Anope::string>("regexengine");
222  if (!regexengine.empty())
223  {
224  source.Reply(" ");
225  source.Reply(_("Regex matches are also supported using the %s engine.\n"
226  "Enclose your pattern in // if this is desired."), regexengine.c_str());
227  }
228 
229  return true;
230  }
231 };
232 
233 class OSList : public Module
234 {
237 
238  public:
239  OSList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
240  commandoschanlist(this), commandosuserlist(this)
241  {
242 
243  }
244 };
245 
bool HasMode(const Anope::string &name) const
Definition: users.cpp:513
CommandOSUserList commandosuserlist
Definition: os_list.cpp:236
OSList(const Anope::string &modname, const Anope::string &creator)
Definition: os_list.cpp:239
const ModeList & GetModes() const
Definition: channels.cpp:238
const Anope::string & GetIdent() const
Definition: users.cpp:264
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: os_list.cpp:98
CommandOSChanList(Module *creator)
Definition: os_list.cpp:17
Anope::string addr() const
Definition: sockets.cpp:73
ChanUserList chans
Definition: users.h:87
Definition: users.h:34
CommandOSChanList commandoschanlist
Definition: os_list.cpp:235
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
bool equals_ci(const char *_str) const
Definition: anope.h:78
Anope::string name
Definition: channels.h:44
Anope::string topic
Definition: channels.h:59
Channel * chan
Definition: channels.h:26
std::map< Anope::string, Anope::string > ListEntry
Definition: lists.h:68
Definition: Config.cs:26
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: os_list.cpp:23
CoreExport bool Match(const string &str, const string &mask, bool case_sensitive=false, bool use_regex=false)
Definition: misc.cpp:407
CommandOSUserList(Module *creator)
Definition: os_list.cpp:125
const Anope::string & GetDisplayedHost() const
Definition: users.cpp:203
static Anope::map< std::pair< bool, Anope::string > > modes
Definition: cs_mode.cpp:745
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
Anope::string host
Definition: users.h:65
sockaddrs ip
Definition: users.h:75
#define MODULE_INIT(x)
Definition: modules.h:45
CoreExport channel_map ChannelList
Definition: channels.cpp:29
ChanUserList users
Definition: channels.h:56
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
size_t HasMode(const Anope::string &name, const Anope::string &param="")
Definition: channels.cpp:201
Anope::string stringify(const T &x)
Definition: anope.h:710
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: os_list.cpp:131
Anope::string nick
Definition: users.h:62
static User * Find(const Anope::string &name, bool nick_only=false)
Definition: users.cpp:815
static Channel * Find(const Anope::string &name)
Definition: channels.cpp:920
CoreExport user_map UserListByNick
Definition: users.cpp:28
void SendSyntax(CommandSource &)
Definition: command.cpp:145
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: os_list.cpp:209
#define _(x)
Definition: services.h:50
ListFormatter & AddColumn(const Anope::string &name)
Definition: misc.cpp:128