Anope IRC Services  Version 2.0
ns_alist.cpp
Go to the documentation of this file.
1 /* NickServ 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 CommandNSAList : public Command
15 {
16  static bool ChannelSort(ChannelInfo *ci1, ChannelInfo *ci2)
17  {
18  return ci::less()(ci1->name, ci2->name);
19  }
20 
21  public:
22  CommandNSAList(Module *creator) : Command(creator, "nickserv/alist", 0, 2)
23  {
24  this->SetDesc(_("List channels you have access on"));
25  this->SetSyntax(_("[\037nickname\037]"));
26  }
27 
28  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
29  {
30  Anope::string nick = source.GetNick();
31  NickCore *nc = source.nc;
32 
33  if (params.size() && source.HasPriv("nickserv/alist"))
34  {
35  nick = params[0];
36  const NickAlias *na = NickAlias::Find(nick);
37  if (!na)
38  {
39  source.Reply(NICK_X_NOT_REGISTERED, nick.c_str());
40  return;
41  }
42  nc = na->nc;
43  }
44 
45  ListFormatter list(source.GetAccount());
46  int chan_count = 0;
47 
48  list.AddColumn(_("Number")).AddColumn(_("Channel")).AddColumn(_("Access")).AddColumn(_("Description"));
49 
50  std::deque<ChannelInfo *> queue;
51  nc->GetChannelReferences(queue);
52  std::sort(queue.begin(), queue.end(), ChannelSort);
53 
54  for (unsigned i = 0; i < queue.size(); ++i)
55  {
56  ChannelInfo *ci = queue[i];
58 
59  if (ci->GetFounder() == nc)
60  {
61  ++chan_count;
62  entry["Number"] = stringify(chan_count);
63  entry["Channel"] = (ci->HasExt("CS_NO_EXPIRE") ? "!" : "") + ci->name;
64  entry["Access"] = Language::Translate(source.GetAccount(), _("Founder"));
65  entry["Description"] = ci->desc;
66  list.AddEntry(entry);
67  continue;
68  }
69 
70  if (ci->GetSuccessor() == nc)
71  {
72  ++chan_count;
73  entry["Number"] = stringify(chan_count);
74  entry["Channel"] = (ci->HasExt("CS_NO_EXPIRE") ? "!" : "") + ci->name;
75  entry["Access"] = Language::Translate(source.GetAccount(), _("Successor"));
76  entry["Description"] = ci->desc;
77  list.AddEntry(entry);
78  continue;
79  }
80 
81  AccessGroup access = ci->AccessFor(nc);
82  if (access.empty())
83  continue;
84 
85  ++chan_count;
86 
87  entry["Number"] = stringify(chan_count);
88  entry["Channel"] = (ci->HasExt("CS_NO_EXPIRE") ? "!" : "") + ci->name;
89  for (unsigned j = 0; j < access.size(); ++j)
90  entry["Access"] = entry["Access"] + ", " + access[j]->AccessSerialize();
91  entry["Access"] = entry["Access"].substr(2);
92  entry["Description"] = ci->desc;
93  list.AddEntry(entry);
94  }
95 
96  std::vector<Anope::string> replies;
97  list.Process(replies);
98 
99  if (!chan_count)
100  {
101  source.Reply(_("\002%s\002 has no access in any channels."), nc->display.c_str());
102  }
103  else
104  {
105  source.Reply(_("Channels that \002%s\002 has access on:"), nc->display.c_str());
106 
107  for (unsigned i = 0; i < replies.size(); ++i)
108  source.Reply(replies[i]);
109 
110  source.Reply(_("End of list - %d channels shown."), chan_count);
111  }
112  }
113 
114  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
115  {
116  this->SendSyntax(source);
117  source.Reply(" ");
118  source.Reply(_("Lists all channels you have access on.\n"
119  " \n"
120  "Channels that have the \037NOEXPIRE\037 option set will be\n"
121  "prefixed by an exclamation mark. The nickname parameter is\n"
122  "limited to Services Operators"));
123 
124  return true;
125  }
126 };
127 
128 class NSAList : public Module
129 {
131 
132  public:
133  NSAList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
134  commandnsalist(this)
135  {
136 
137  }
138 };
139 
Serialize::Reference< NickCore > nc
Definition: account.h:47
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
CommandNSAList(Module *creator)
Definition: ns_alist.cpp:22
Definition: hashcomp.h:84
void GetChannelReferences(std::deque< ChannelInfo * > &queue)
Definition: nickcore.cpp:251
Anope::string name
Definition: regchannel.h:63
Anope::string desc
Definition: regchannel.h:64
#define NICK_X_NOT_REGISTERED
Definition: language.h:79
static bool ChannelSort(ChannelInfo *ci1, ChannelInfo *ci2)
Definition: ns_alist.cpp:16
NickCore * GetSuccessor() const
Definition: regchannel.cpp:378
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
std::map< Anope::string, Anope::string > ListEntry
Definition: lists.h:68
NickCore * GetFounder() const
Definition: regchannel.cpp:364
Anope::string display
Definition: account.h:113
#define anope_override
Definition: services.h:56
#define MODULE_INIT(x)
Definition: modules.h:45
CoreExport const char * Translate(const char *string)
Definition: language.cpp:59
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
AccessGroup AccessFor(const User *u)
Definition: regchannel.cpp:413
Anope::string stringify(const T &x)
Definition: anope.h:710
CommandNSAList commandnsalist
Definition: ns_alist.cpp:130
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: ns_alist.cpp:28
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: ns_alist.cpp:114
NSAList(const Anope::string &modname, const Anope::string &creator)
Definition: ns_alist.cpp:133
void SendSyntax(CommandSource &)
Definition: command.cpp:145
const char * c_str() const
Definition: anope.h:117
#define _(x)
Definition: services.h:50
bool HasExt(const Anope::string &name) const
Definition: extensible.cpp:31
ListFormatter & AddColumn(const Anope::string &name)
Definition: misc.cpp:128