Anope IRC Services  Version 2.0
opertype.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2008-2011 Robin Burchell <w00t@inspircd.org>
4  * Copyright (C) 2008-2014 Anope Team <team@anope.org>
5  *
6  * Please read COPYING and README for further details.
7  *
8  */
9 
10 #include "services.h"
11 #include "anope.h"
12 #include "opertype.h"
13 #include "config.h"
14 
15 std::vector<Oper *> Oper::opers;
16 
17 Oper::Oper(const Anope::string &n, OperType *o) : name(n), ot(o), require_oper(false)
18 {
19  opers.push_back(this);
20 }
21 
23 {
24  std::vector<Oper *>::iterator it = std::find(opers.begin(), opers.end(), this);
25  if (it != opers.end())
26  opers.erase(it);
27 }
28 
30 {
31  for (unsigned i = 0; i < opers.size(); ++i)
32  {
33  Oper *o = opers[i];
34 
35  if (o->name.equals_ci(name))
36  return o;
37  }
38 
39  return NULL;
40 }
41 
43 {
44  for (unsigned i = 0; i < Config->MyOperTypes.size(); ++i)
45  {
46  OperType *ot = Config->MyOperTypes[i];
47 
48  if (ot->GetName() == name)
49  return ot;
50  }
51 
52  return NULL;
53 }
54 
55 OperType::OperType(const Anope::string &nname) : name(nname)
56 {
57 }
58 
59 bool OperType::HasCommand(const Anope::string &cmdstr) const
60 {
61  for (std::list<Anope::string>::const_iterator it = this->commands.begin(), it_end = this->commands.end(); it != it_end; ++it)
62  {
63  const Anope::string &s = *it;
64 
65  if (!s.find('~') && Anope::Match(cmdstr, s.substr(1)))
66  return false;
67  else if (Anope::Match(cmdstr, s))
68  return true;
69  }
70  for (std::set<OperType *>::const_iterator iit = this->inheritances.begin(), iit_end = this->inheritances.end(); iit != iit_end; ++iit)
71  {
72  OperType *ot = *iit;
73 
74  if (ot->HasCommand(cmdstr))
75  return true;
76  }
77 
78  return false;
79 }
80 
81 bool OperType::HasPriv(const Anope::string &privstr) const
82 {
83  for (std::list<Anope::string>::const_iterator it = this->privs.begin(), it_end = this->privs.end(); it != it_end; ++it)
84  {
85  const Anope::string &s = *it;
86 
87  if (!s.find('~') && Anope::Match(privstr, s.substr(1)))
88  return false;
89  else if (Anope::Match(privstr, s))
90  return true;
91  }
92  for (std::set<OperType *>::const_iterator iit = this->inheritances.begin(), iit_end = this->inheritances.end(); iit != iit_end; ++iit)
93  {
94  OperType *ot = *iit;
95 
96  if (ot->HasPriv(privstr))
97  return true;
98  }
99 
100  return false;
101 }
102 
104 {
105  this->commands.push_back(cmdstr);
106 }
107 
108 void OperType::AddPriv(const Anope::string &privstr)
109 {
110  this->privs.push_back(privstr);
111 }
112 
114 {
115  return this->name;
116 }
117 
119 {
120  if (ot != this)
121  this->inheritances.insert(ot);
122 }
123 
124 const std::list<Anope::string> OperType::GetCommands() const
125 {
126  std::list<Anope::string> cmd_list = this->commands;
127  for (std::set<OperType *>::const_iterator it = this->inheritances.begin(), it_end = this->inheritances.end(); it != it_end; ++it)
128  {
129  OperType *ot = *it;
130  std::list<Anope::string> cmds = ot->GetCommands();
131  for (std::list<Anope::string>::const_iterator it2 = cmds.begin(), it2_end = cmds.end(); it2 != it2_end; ++it2)
132  cmd_list.push_back(*it2);
133  }
134  return cmd_list;
135 }
136 
137 const std::list<Anope::string> OperType::GetPrivs() const
138 {
139  std::list<Anope::string> priv_list = this->privs;
140  for (std::set<OperType *>::const_iterator it = this->inheritances.begin(), it_end = this->inheritances.end(); it != it_end; ++it)
141  {
142  OperType *ot = *it;
143  std::list<Anope::string> priv = ot->GetPrivs();
144  for (std::list<Anope::string>::const_iterator it2 = priv.begin(), it2_end = priv.end(); it2 != it2_end; ++it2)
145  priv_list.push_back(*it2);
146  }
147  return priv_list;
148 }
149 
std::list< Anope::string > privs
Definition: opertype.h:54
std::set< OperType * > inheritances
Definition: opertype.h:68
Definition: opertype.h:18
bool HasPriv(const Anope::string &privstr) const
Definition: opertype.cpp:81
const Anope::string & GetName() const
Definition: opertype.cpp:113
void AddPriv(const Anope::string &privstr)
Definition: opertype.cpp:108
Anope::string name
Definition: opertype.h:21
bool equals_ci(const char *_str) const
Definition: anope.h:78
virtual ~Oper()
Definition: opertype.cpp:22
Anope::string name
Definition: opertype.h:49
static OperType * Find(const Anope::string &name)
Definition: opertype.cpp:42
OperType(const Anope::string &nname)
Definition: opertype.cpp:55
string substr(size_type pos=0, size_type n=npos) const
Definition: anope.h:277
static std::vector< Oper * > opers
Definition: opertype.h:35
Definition: Config.cs:26
CoreExport bool Match(const string &str, const string &mask, bool case_sensitive=false, bool use_regex=false)
Definition: misc.cpp:407
const std::list< Anope::string > GetPrivs() const
Definition: opertype.cpp:137
const std::list< Anope::string > GetCommands() const
Definition: opertype.cpp:124
Oper(const Anope::string &n, OperType *o)
Definition: opertype.cpp:17
Anope::string name
Definition: access.cpp:22
void Inherits(OperType *ot)
Definition: opertype.cpp:118
static Oper * Find(const Anope::string &name)
Definition: opertype.cpp:29
bool HasCommand(const Anope::string &cmdstr) const
Definition: opertype.cpp:59
void AddCommand(const Anope::string &cmdstr)
Definition: opertype.cpp:103
size_type find(const string &_str, size_type pos=0) const
Definition: anope.h:192
std::list< Anope::string > commands
Definition: opertype.h:64