Anope IRC Services  Version 2.0
cs_set_misc.cpp
Go to the documentation of this file.
1 /*
2  * (C) 2003-2014 Anope Team
3  * Contact us at team@anope.org
4  *
5  * Please read COPYING and README for further details.
6  *
7  * Based on the original code of Epona by Lara.
8  * Based on the original code of Services by Andy Church.
9  */
10 
11 #include "module.h"
12 #include "modules/set_misc.h"
13 
14 static Module *me;
15 
17 
18 struct CSMiscData;
20 
22 {
23  ExtensibleItem<CSMiscData>* &it = items[name];
24  if (!it)
25  try
26  {
28  }
29  catch (const ModuleException &) { }
30  return it;
31 }
32 
34 {
35  CSMiscData(Extensible *obj) : Serializable("CSMiscData") { }
36 
37  CSMiscData(ChannelInfo *c, const Anope::string &n, const Anope::string &d) : Serializable("CSMiscData")
38  {
39  object = c->name;
40  name = n;
41  data = d;
42  }
43 
45  {
46  sdata["ci"] << this->object;
47  sdata["name"] << this->name;
48  sdata["data"] << this->data;
49  }
50 
52  {
53  Anope::string sci, sname, sdata;
54 
55  data["ci"] >> sci;
56  data["name"] >> sname;
57  data["data"] >> sdata;
58 
60  if (ci == NULL)
61  return NULL;
62 
63  CSMiscData *d = NULL;
64  if (obj)
65  {
67  d->object = ci->name;
68  data["name"] >> d->name;
69  data["data"] >> d->data;
70  }
71  else
72  {
73  ExtensibleItem<CSMiscData> *item = GetItem(sname);
74  if (item)
75  d = item->Set(ci, CSMiscData(ci, sname, sdata));
76  }
77 
78  return d;
79  }
80 };
81 
83 {
84  size_t sp = command.rfind(' ');
85  if (sp != Anope::string::npos)
86  return command.substr(sp + 1);
87  return command;
88 }
89 
90 class CommandCSSetMisc : public Command
91 {
92  public:
93  CommandCSSetMisc(Module *creator, const Anope::string &cname = "chanserv/set/misc") : Command(creator, cname, 1, 2)
94  {
95  this->SetSyntax(_("\037channel\037 [\037parameters\037]"));
96  }
97 
98  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
99  {
100  if (Anope::ReadOnly)
101  {
102  source.Reply(READ_ONLY_MODE);
103  return;
104  }
105 
106  ChannelInfo *ci = ChannelInfo::Find(params[0]);
107  const Anope::string &param = params.size() > 1 ? params[1] : "";
108  if (ci == NULL)
109  {
110  source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
111  return;
112  }
113 
114  EventReturn MOD_RESULT;
115  FOREACH_RESULT(OnSetChannelOption, MOD_RESULT, (source, this, ci, param));
116  if (MOD_RESULT == EVENT_STOP)
117  return;
118 
119  if (MOD_RESULT != EVENT_ALLOW && !source.AccessFor(ci).HasPriv("SET") && source.permission.empty() && !source.HasPriv("chanserv/administration"))
120  {
121  source.Reply(ACCESS_DENIED);
122  return;
123  }
124 
125  Anope::string scommand = GetAttribute(source.command);
126  Anope::string key = "cs_set_misc:" + scommand;
127  ExtensibleItem<CSMiscData> *item = GetItem(key);
128  if (item == NULL)
129  return;
130 
131  if (!param.empty())
132  {
133  item->Set(ci, CSMiscData(ci, key, param));
134  Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to change it to " << param;
135  source.Reply(CHAN_SETTING_CHANGED, scommand.c_str(), ci->name.c_str(), params[1].c_str());
136  }
137  else
138  {
139  item->Unset(ci);
140  Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to unset it";
141  source.Reply(CHAN_SETTING_UNSET, scommand.c_str(), ci->name.c_str());
142  }
143  }
144 
146  {
147  if (descriptions.count(source.command))
148  {
149  this->SetDesc(descriptions[source.command]);
150  Command::OnServHelp(source);
151  }
152  }
153 
154  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
155  {
156  if (descriptions.count(source.command))
157  {
158  source.Reply("%s", Language::Translate(source.nc, descriptions[source.command].c_str()));
159  return true;
160  }
161  return false;
162  }
163 };
164 
165 class CSSetMisc : public Module
166 {
169 
170  public:
171  CSSetMisc(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
172  commandcssetmisc(this), csmiscdata_type("CSMiscData", CSMiscData::Unserialize)
173  {
174  me = this;
175  }
176 
178  {
179  for (Anope::map<ExtensibleItem<CSMiscData> *>::iterator it = items.begin(); it != items.end(); ++it)
180  delete it->second;
181  }
182 
184  {
185  descriptions.clear();
186 
187  for (int i = 0; i < conf->CountBlock("command"); ++i)
188  {
189  Configuration::Block *block = conf->GetBlock("command", i);
190 
191  if (block->Get<const Anope::string>("command") != "chanserv/set/misc")
192  continue;
193 
194  Anope::string cname = block->Get<const Anope::string>("name");
195  Anope::string desc = block->Get<const Anope::string>("misc_description");
196 
197  if (cname.empty() || desc.empty())
198  continue;
199 
200  descriptions[cname] = desc;
201  }
202  }
203 
205  {
206  for (Anope::map<ExtensibleItem<CSMiscData> *>::iterator it = items.begin(); it != items.end(); ++it)
207  {
208  ExtensibleItem<CSMiscData> *e = it->second;
209  MiscData *data = e->Get(ci);
210 
211  if (data != NULL)
212  info[e->name.substr(12).replace_all_cs("_", " ")] = data->data;
213  }
214  }
215 };
216 
CoreExport bool ReadOnly
Definition: main.cpp:28
void Serialize(Serialize::Data &sdata) const anope_override
Definition: cs_set_misc.cpp:44
Anope::string name
Definition: set_misc.h:11
static Anope::map< ExtensibleItem< CSMiscData > * > items
Definition: cs_set_misc.cpp:18
Definition: hashcomp.h:84
CSMiscData(ChannelInfo *c, const Anope::string &n, const Anope::string &d)
Definition: cs_set_misc.cpp:37
Anope::string name
Definition: regchannel.h:63
Anope::string desc
Definition: access.cpp:23
#define ACCESS_DENIED
Definition: language.h:73
CSSetMisc(const Anope::string &modname, const Anope::string &creator)
CSMiscData(Extensible *obj)
Definition: cs_set_misc.cpp:35
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
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
void Unset(Extensible *obj) anope_override
Definition: extensible.h:95
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
CommandCSSetMisc(Module *creator, const Anope::string &cname="chanserv/set/misc")
Definition: cs_set_misc.cpp:93
static Anope::map< Anope::string > descriptions
Definition: cs_set_misc.cpp:16
static ExtensibleItem< CSMiscData > * GetItem(const Anope::string &name)
Definition: cs_set_misc.cpp:21
static ChannelInfo * Find(const Anope::string &name)
Definition: regchannel.cpp:630
static Module * me
Definition: cs_set_misc.cpp:14
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
static const size_type npos
Definition: anope.h:44
void OnServHelp(CommandSource &source) anope_override
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
Serialize::Type csmiscdata_type
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: cs_set_misc.cpp:98
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
#define CHAN_SETTING_CHANGED
Definition: language.h:106
CommandCSSetMisc commandcssetmisc
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
static Anope::string GetAttribute(const Anope::string &command)
Definition: cs_set_misc.cpp:82
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
#define CHAN_X_NOT_REGISTERED
Definition: language.h:84
void OnReload(Configuration::Conf *conf) anope_override
T * Get(const Extensible *obj) const
Definition: extensible.h:103
T anope_dynamic_static_cast(O ptr)
Definition: anope.h:774
Anope::string name
Definition: access.cpp:22
#define CHAN_SETTING_UNSET
Definition: language.h:107
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
T Get(const Anope::string &tag)
Definition: config.h:44
static Serializable * Unserialize(Serializable *obj, Serialize::Data &data)
Definition: cs_set_misc.cpp:51
void OnChanInfo(CommandSource &source, ChannelInfo *ci, InfoFormatter &info, bool) anope_override
#define _(x)
Definition: services.h:50
Type(const Anope::string &n, unserialize_func f, Module *owner=NULL)