Anope IRC Services  Version 2.0
ns_set_misc.cpp
Go to the documentation of this file.
1 /*
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 #include "modules/set_misc.h"
14 
15 static Module *me;
16 
18 
19 struct NSMiscData;
21 
23 {
24  ExtensibleItem<NSMiscData>* &it = items[name];
25  if (!it)
26  try
27  {
29  }
30  catch (const ModuleException &) { }
31  return it;
32 }
33 
35 {
36  NSMiscData(Extensible *) : Serializable("NSMiscData") { }
37 
38  NSMiscData(NickCore *ncore, const Anope::string &n, const Anope::string &d) : Serializable("NSMiscData")
39  {
40  object = ncore->display;
41  name = n;
42  data = d;
43  }
44 
46  {
47  sdata["nc"] << this->object;
48  sdata["name"] << this->name;
49  sdata["data"] << this->data;
50  }
51 
53  {
54  Anope::string snc, sname, sdata;
55 
56  data["nc"] >> snc;
57  data["name"] >> sname;
58  data["data"] >> sdata;
59 
60  NickCore *nc = NickCore::Find(snc);
61  if (nc == NULL)
62  return NULL;
63 
64  NSMiscData *d = NULL;
65  if (obj)
66  {
68  d->object = nc->display;
69  data["name"] >> d->name;
70  data["data"] >> d->data;
71  }
72  else
73  {
74  ExtensibleItem<NSMiscData> *item = GetItem(sname);
75  if (item)
76  d = item->Set(nc, NSMiscData(nc, sname, sdata));
77  }
78 
79  return d;
80  }
81 };
82 
84 {
85  size_t sp = command.rfind(' ');
86  if (sp != Anope::string::npos)
87  return command.substr(sp + 1);
88  return command;
89 }
90 
91 class CommandNSSetMisc : public Command
92 {
93  public:
94  CommandNSSetMisc(Module *creator, const Anope::string &cname = "nickserv/set/misc", size_t min = 0) : Command(creator, cname, min, min + 1)
95  {
96  this->SetSyntax(_("[\037parameter\037]"));
97  }
98 
99  void Run(CommandSource &source, const Anope::string &user, const Anope::string &param)
100  {
101  if (Anope::ReadOnly)
102  {
103  source.Reply(READ_ONLY_MODE);
104  return;
105  }
106 
107  const NickAlias *na = NickAlias::Find(user);
108  if (!na)
109  {
110  source.Reply(NICK_X_NOT_REGISTERED, user.c_str());
111  return;
112  }
113  NickCore *nc = na->nc;
114 
115  EventReturn MOD_RESULT;
116  FOREACH_RESULT(OnSetNickOption, MOD_RESULT, (source, this, nc, param));
117  if (MOD_RESULT == EVENT_STOP)
118  return;
119 
120  Anope::string scommand = GetAttribute(source.command);
121  Anope::string key = "ns_set_misc:" + scommand;
122  ExtensibleItem<NSMiscData> *item = GetItem(key);
123  if (item == NULL)
124  return;
125 
126  if (!param.empty())
127  {
128  item->Set(nc, NSMiscData(nc, key, param));
129  source.Reply(CHAN_SETTING_CHANGED, scommand.c_str(), nc->display.c_str(), param.c_str());
130  }
131  else
132  {
133  item->Unset(nc);
134  source.Reply(CHAN_SETTING_UNSET, scommand.c_str(), nc->display.c_str());
135  }
136  }
137 
138  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
139  {
140  this->Run(source, source.nc->display, !params.empty() ? params[0] : "");
141  }
142 
144  {
145  if (descriptions.count(source.command))
146  {
147  this->SetDesc(descriptions[source.command]);
148  Command::OnServHelp(source);
149  }
150  }
151 
152  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
153  {
154  if (descriptions.count(source.command))
155  {
156  source.Reply("%s", Language::Translate(source.nc, descriptions[source.command].c_str()));
157  return true;
158  }
159  return false;
160  }
161 };
162 
164 {
165  public:
166  CommandNSSASetMisc(Module *creator) : CommandNSSetMisc(creator, "nickserv/saset/misc", 1)
167  {
168  this->ClearSyntax();
169  this->SetSyntax(_("\037nickname\037 [\037parameter\037]"));
170  }
171 
172  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
173  {
174  this->Run(source, params[0], params.size() > 1 ? params[1] : "");
175  }
176 };
177 
178 class NSSetMisc : public Module
179 {
183 
184  public:
185  NSSetMisc(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
186  commandnssetmisc(this), commandnssasetmisc(this), nsmiscdata_type("NSMiscData", NSMiscData::Unserialize)
187  {
188  me = this;
189  }
190 
192  {
193  for (Anope::map<ExtensibleItem<NSMiscData> *>::iterator it = items.begin(); it != items.end(); ++it)
194  delete it->second;
195  }
196 
198  {
199  descriptions.clear();
200 
201  for (int i = 0; i < conf->CountBlock("command"); ++i)
202  {
203  Configuration::Block *block = conf->GetBlock("command", i);
204 
205  const Anope::string &cmd = block->Get<const Anope::string>("command");
206 
207  if (cmd != "nickserv/set/misc" && cmd != "nickserv/saset/misc")
208  continue;
209 
210  Anope::string cname = block->Get<const Anope::string>("name");
211  Anope::string desc = block->Get<const Anope::string>("misc_description");
212 
213  if (cname.empty() || desc.empty())
214  continue;
215 
216  descriptions[cname] = desc;
217  }
218  }
219 
221  {
222  for (Anope::map<ExtensibleItem<NSMiscData> *>::iterator it = items.begin(); it != items.end(); ++it)
223  {
224  ExtensibleItem<NSMiscData> *e = it->second;
225  NSMiscData *data = e->Get(na->nc);
226 
227  if (data != NULL)
228  info[e->name.substr(12).replace_all_cs("_", " ")] = data->data;
229  }
230  }
231 };
232 
Serialize::Reference< NickCore > nc
Definition: account.h:47
CoreExport bool ReadOnly
Definition: main.cpp:28
Anope::string name
Definition: set_misc.h:11
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
NSMiscData(Extensible *)
Definition: ns_set_misc.cpp:36
static ExtensibleItem< NSMiscData > * GetItem(const Anope::string &name)
Definition: ns_set_misc.cpp:22
void Run(CommandSource &source, const Anope::string &user, const Anope::string &param)
Definition: ns_set_misc.cpp:99
Anope::string desc
Definition: access.cpp:23
CommandNSSASetMisc(Module *creator)
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
CommandNSSASetMisc commandnssasetmisc
static Anope::string GetAttribute(const Anope::string &command)
Definition: ns_set_misc.cpp:83
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
static Module * me
Definition: ns_set_misc.cpp:15
static Serializable * Unserialize(Serializable *obj, Serialize::Data &data)
Definition: ns_set_misc.cpp:52
#define NICK_X_NOT_REGISTERED
Definition: language.h:79
void Serialize(Serialize::Data &sdata) const anope_override
Definition: ns_set_misc.cpp:45
void ClearSyntax()
Definition: command.cpp:135
size_type rfind(const string &_str, size_type pos=npos) const
Definition: anope.h:197
#define READ_ONLY_MODE
Definition: language.h:71
Anope::string data
Definition: set_misc.h:12
#define FOREACH_RESULT(ename, ret, args)
Definition: modules.h:95
static Anope::map< ExtensibleItem< NSMiscData > * > items
Definition: ns_set_misc.cpp:19
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
void Unset(Extensible *obj) anope_override
Definition: extensible.h:95
CommandNSSetMisc(Module *creator, const Anope::string &cname="nickserv/set/misc", size_t min=0)
Definition: ns_set_misc.cpp:94
string substr(size_type pos=0, size_type n=npos) const
Definition: anope.h:277
Block * GetBlock(const Anope::string &name, int num=0)
Definition: config.cpp:43
CommandNSSetMisc commandnssetmisc
void OnServHelp(CommandSource &source) anope_override
static const size_type npos
Definition: anope.h:44
Anope::string command
Definition: commands.h:69
void Reply(const char *message,...)
Definition: command.cpp:96
T * Set(Extensible *obj, const T &value)
Definition: extensible.h:78
string replace_all_cs(const string &_orig, const string &_repl) const
Definition: anope.h:229
Anope::string display
Definition: account.h:113
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
#define CHAN_SETTING_CHANGED
Definition: language.h:106
static Anope::map< Anope::string > descriptions
Definition: ns_set_misc.cpp:17
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
Anope::string object
Definition: set_misc.h:10
CoreExport const char * Translate(const char *string)
Definition: language.cpp:59
Serialize::Type nsmiscdata_type
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
virtual void OnServHelp(CommandSource &source)
Definition: command.cpp:184
Anope::string name
Definition: service.h:88
void OnNickInfo(CommandSource &source, NickAlias *na, InfoFormatter &info, bool) anope_override
NSMiscData(NickCore *ncore, const Anope::string &n, const Anope::string &d)
Definition: ns_set_misc.cpp:38
T * Get(const Extensible *obj) const
Definition: extensible.h:103
T anope_dynamic_static_cast(O ptr)
Definition: anope.h:774
NSSetMisc(const Anope::string &modname, const Anope::string &creator)
Anope::string name
Definition: access.cpp:22
#define CHAN_SETTING_UNSET
Definition: language.h:107
const char * c_str() const
Definition: anope.h:117
T Get(const Anope::string &tag)
Definition: config.h:44
static NickCore * Find(const Anope::string &nick)
Definition: nickcore.cpp:258
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
#define _(x)
Definition: services.h:50
Type(const Anope::string &n, unserialize_func f, Module *owner=NULL)
void OnReload(Configuration::Conf *conf) anope_override