Anope IRC Services  Version 2.0
os_oper.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 
15 {
16  MyOper(const Anope::string &n, OperType *o) : Oper(n, o), Serializable("Oper") { }
17 
19  {
20  data["name"] << this->name;
21  data["type"] << this->ot->GetName();
22  }
23 
25  {
26  Anope::string stype, sname;
27 
28  data["type"] >> stype;
29  data["name"] >> sname;
30 
31  OperType *ot = OperType::Find(stype);
32  if (ot == NULL)
33  return NULL;
34  NickCore *nc = NickCore::Find(sname);
35  if (nc == NULL)
36  return NULL;
37 
38  MyOper *myo;
39  if (obj)
40  myo = anope_dynamic_static_cast<MyOper *>(obj);
41  else
42  myo = new MyOper(nc->display, ot);
43  nc->o = myo;
44  Log(LOG_NORMAL, "operserv/oper") << "Tied oper " << nc->display << " to type " << ot->GetName();
45  return myo;
46  }
47 };
48 
49 class CommandOSOper : public Command
50 {
51  bool HasPrivs(CommandSource &source, OperType *ot) const
52  {
53  std::list<Anope::string> commands = ot->GetCommands(), privs = ot->GetPrivs();
54 
55  for (std::list<Anope::string>::iterator it = commands.begin(); it != commands.end(); ++it)
56  if (!source.HasCommand(*it))
57  return false;
58 
59  for (std::list<Anope::string>::iterator it = privs.begin(); it != privs.end(); ++it)
60  if (!source.HasPriv(*it))
61  return false;
62 
63  return true;
64  }
65 
66  public:
67  CommandOSOper(Module *creator) : Command(creator, "operserv/oper", 1, 3)
68  {
69  this->SetDesc(_("View and change Services Operators"));
70  this->SetSyntax(_("ADD \037oper\037 \037type\037"));
71  this->SetSyntax(_("DEL \037oper\037"));
72  this->SetSyntax(_("INFO \037type\037"));
73  this->SetSyntax("LIST");
74  }
75 
76  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
77  {
78  const Anope::string &subcommand = params[0];
79 
80  if (subcommand.equals_ci("ADD") && params.size() > 2)
81  {
82  const Anope::string &oper = params[1];
83  const Anope::string &otype = params[2];
84 
85  const NickAlias *na = NickAlias::Find(oper);
86  if (na == NULL)
87  source.Reply(NICK_X_NOT_REGISTERED, oper.c_str());
88  else if (na->nc->o)
89  source.Reply(_("Nick \002%s\002 is already an operator."), na->nick.c_str());
90  else
91  {
92  OperType *ot = OperType::Find(otype);
93  if (ot == NULL)
94  {
95  source.Reply(_("Oper type \002%s\002 has not been configured."), otype.c_str());
96  return;
97  }
98 
99  if (!HasPrivs(source, ot))
100  {
101  source.Reply(ACCESS_DENIED);
102  return;
103  }
104 
105  na->nc->o = new MyOper(na->nc->display, ot);
106 
107  if (Anope::ReadOnly)
108  source.Reply(READ_ONLY_MODE);
109 
110  Log(LOG_ADMIN, source, this) << "ADD " << na->nick << " as type " << ot->GetName();
111  source.Reply("%s (%s) added to the \002%s\002 list.", na->nick.c_str(), na->nc->display.c_str(), ot->GetName().c_str());
112  }
113  }
114  else if (subcommand.equals_ci("DEL") && params.size() > 1)
115  {
116  const Anope::string &oper = params[1];
117 
118  const NickAlias *na = NickAlias::Find(oper);
119  if (na == NULL)
120  source.Reply(NICK_X_NOT_REGISTERED, oper.c_str());
121  else if (!na->nc || !na->nc->o)
122  source.Reply(_("Nick \002%s\002 is not a Services Operator."), oper.c_str());
123  else if (!HasPrivs(source, na->nc->o->ot))
124  source.Reply(ACCESS_DENIED);
125  else
126  {
127  delete na->nc->o;
128  na->nc->o = NULL;
129 
130  if (Anope::ReadOnly)
131  source.Reply(READ_ONLY_MODE);
132 
133  Log(LOG_ADMIN, source, this) << "DEL " << na->nick;
134  source.Reply(_("Oper privileges removed from %s (%s)."), na->nick.c_str(), na->nc->display.c_str());
135  }
136  }
137  else if (subcommand.equals_ci("LIST"))
138  {
139  source.Reply(_("Name Type"));
140  for (nickcore_map::const_iterator it = NickCoreList->begin(), it_end = NickCoreList->end(); it != it_end; ++it)
141  {
142  const NickCore *nc = it->second;
143 
144  if (!nc->o)
145  continue;
146 
147  source.Reply(_("%-8s %s"), nc->o->name.c_str(), nc->o->ot->GetName().c_str());
148  if (std::find(Config->Opers.begin(), Config->Opers.end(), nc->o) != Config->Opers.end())
149  source.Reply(_(" This oper is configured in the configuration file."));
150  for (std::list<User *>::const_iterator uit = nc->users.begin(); uit != nc->users.end(); ++uit)
151  {
152  User *u = *uit;
153  source.Reply(_(" %s is online using this oper block."), u->nick.c_str());
154  }
155  }
156  }
157  else if (subcommand.equals_ci("INFO") && params.size() > 1)
158  {
159  Anope::string fulltype = params[1];
160  if (params.size() > 2)
161  fulltype += " " + params[2];
162  OperType *ot = OperType::Find(fulltype);
163  if (ot == NULL)
164  source.Reply(_("Oper type \002%s\002 has not been configured."), fulltype.c_str());
165  else
166  {
167  if (ot->GetCommands().empty())
168  source.Reply(_("Opertype \002%s\002 has no allowed commands."), ot->GetName().c_str());
169  else
170  {
171  source.Reply(_("Available commands for \002%s\002:"), ot->GetName().c_str());
172  Anope::string buf;
173  std::list<Anope::string> cmds = ot->GetCommands();
174  for (std::list<Anope::string>::const_iterator it = cmds.begin(), it_end = cmds.end(); it != it_end; ++it)
175  {
176  buf += *it + " ";
177  if (buf.length() > 400)
178  {
179  source.Reply("%s", buf.c_str());
180  buf.clear();
181  }
182  }
183  if (!buf.empty())
184  {
185  source.Reply("%s", buf.c_str());
186  buf.clear();
187  }
188  }
189  if (ot->GetPrivs().empty())
190  source.Reply(_("Opertype \002%s\002 has no allowed privileges."), ot->GetName().c_str());
191  else
192  {
193  source.Reply(_("Available privileges for \002%s\002:"), ot->GetName().c_str());
194  Anope::string buf;
195  std::list<Anope::string> privs = ot->GetPrivs();
196  for (std::list<Anope::string>::const_iterator it = privs.begin(), it_end = privs.end(); it != it_end; ++it)
197  {
198  buf += *it + " ";
199  if (buf.length() > 400)
200  {
201  source.Reply("%s", buf.c_str());
202  buf.clear();
203  }
204  }
205  if (!buf.empty())
206  {
207  source.Reply("%s", buf.c_str());
208  buf.clear();
209  }
210  }
211  if (!ot->modes.empty())
212  source.Reply(_("Opertype \002%s\002 receives modes \002%s\002 once identified."), ot->GetName().c_str(), ot->modes.c_str());
213  }
214  }
215  else
216  this->OnSyntaxError(source, subcommand);
217 
218  return;
219  }
220 
221  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
222  {
223  this->SendSyntax(source);
224  source.Reply(" ");
225  source.Reply(_("Allows you to change and view Services Operators.\n"
226  "Note that operators removed by this command but are still set in\n"
227  "the configuration file are not permanently affected by this."));
228  return true;
229  }
230 };
231 
232 class OSOper : public Module
233 {
236 
237  public:
238  OSOper(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
239  myoper_type("Oper", MyOper::Unserialize), commandosoper(this)
240  {
241  }
242 
244  {
245  for (nickcore_map::const_iterator it = NickCoreList->begin(), it_end = NickCoreList->end(); it != it_end; ++it)
246  {
247  NickCore *nc = it->second;
248 
249  if (nc->o && dynamic_cast<MyOper *>(nc->o))
250  {
251  delete nc->o;
252  nc->o = NULL;
253  }
254  }
255  }
256 };
257 
Serialize::Reference< NickCore > nc
Definition: account.h:47
CoreExport bool ReadOnly
Definition: main.cpp:28
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
OSOper(const Anope::string &modname, const Anope::string &creator)
Definition: os_oper.cpp:238
CoreExport Serialize::Checker< nickcore_map > NickCoreList
void clear()
Definition: anope.h:187
Definition: opertype.h:18
static Serializable * Unserialize(Serializable *obj, Serialize::Data &data)
Definition: os_oper.cpp:24
Anope::string nick
Definition: account.h:37
#define ACCESS_DENIED
Definition: language.h:73
bool HasPrivs(CommandSource &source, OperType *ot) const
Definition: os_oper.cpp:51
const Anope::string & GetName() const
Definition: opertype.cpp:113
~OSOper()
Definition: os_oper.cpp:243
Definition: users.h:34
bool HasCommand(const Anope::string &cmd)
Definition: command.cpp:60
#define NICK_X_NOT_REGISTERED
Definition: language.h:79
Anope::string name
Definition: opertype.h:21
#define READ_ONLY_MODE
Definition: language.h:71
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
bool equals_ci(const char *_str) const
Definition: anope.h:78
static OperType * Find(const Anope::string &name)
Definition: opertype.cpp:42
Anope::string modes
Definition: opertype.h:72
Serialize::Type myoper_type
Definition: os_oper.cpp:234
size_type length() const
Definition: anope.h:131
Definition: Config.cs:26
OperType * ot
Definition: opertype.h:23
virtual void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
Definition: command.cpp:191
const std::list< Anope::string > GetPrivs() const
Definition: opertype.cpp:137
bool HasPriv(const Anope::string &cmd)
Definition: command.cpp:69
MyOper(const Anope::string &n, OperType *o)
Definition: os_oper.cpp:16
Anope::string display
Definition: account.h:113
Oper * o
Definition: account.h:131
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
const std::list< Anope::string > GetCommands() const
Definition: opertype.cpp:124
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: os_oper.cpp:221
#define MODULE_INIT(x)
Definition: modules.h:45
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
Anope::string nick
Definition: users.h:62
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: os_oper.cpp:76
T anope_dynamic_static_cast(O ptr)
Definition: anope.h:774
void SendSyntax(CommandSource &)
Definition: command.cpp:145
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
CommandOSOper commandosoper
Definition: os_oper.cpp:235
void Serialize(Serialize::Data &data) const anope_override
Definition: os_oper.cpp:18
std::list< User * > users
Definition: account.h:140
CommandOSOper(Module *creator)
Definition: os_oper.cpp:67
static NickCore * Find(const Anope::string &nick)
Definition: nickcore.cpp:258
#define _(x)
Definition: services.h:50
Type(const Anope::string &n, unserialize_func f, Module *owner=NULL)