Anope IRC Services  Version 2.0
m_sql_oper.cpp
Go to the documentation of this file.
1 #include "module.h"
2 #include "modules/sql.h"
3 
4 struct SQLOper : Oper
5 {
6  SQLOper(const Anope::string &n, OperType *o) : Oper(n, o) { }
7 };
8 
10 {
12 
14  {
17  ~SQLOperResultDeleter() { delete res; }
18  };
19 
20  public:
21  SQLOperResult(Module *m, User *u) : SQL::Interface(m), user(u) { }
22 
24  {
25  SQLOperResultDeleter d(this);
26 
27  if (!user || !user->Account() || r.Rows() == 0)
28  return;
29 
30  Anope::string opertype;
31  try
32  {
33  opertype = r.Get(0, "opertype");
34  }
35  catch (const SQL::Exception &)
36  {
37  return;
38  }
39 
40  Log(LOG_DEBUG) << "m_sql_oper: Got result for " << user->nick << ", opertype " << opertype;
41 
43  try
44  {
45  modes = r.Get(0, "modes");
46  }
47  catch (const SQL::Exception &) { }
48 
49  BotInfo *OperServ = Config->GetClient("OperServ");
50  if (opertype.empty())
51  {
52  if (user->Account() && user->Account()->o && dynamic_cast<SQLOper *>(user->Account()->o))
53  {
54  delete user->Account()->o;
55  user->Account()->o = NULL;
56 
57  Log(this->owner) << "m_sql_oper: Removed services operator from " << user->nick << " (" << user->Account()->display << ")";
58  user->RemoveMode(OperServ, "OPER"); // Probably not set, just incase
59  }
60  return;
61  }
62 
63  OperType *ot = OperType::Find(opertype);
64  if (ot == NULL)
65  {
66  Log(this->owner) << "m_sql_oper: Oper " << user->nick << " has type " << opertype << ", but this opertype does not exist?";
67  return;
68  }
69 
70  if (!user->Account()->o || user->Account()->o->ot != ot)
71  {
72  Log(this->owner) << "m_sql_oper: Tieing oper " << user->nick << " to type " << opertype;
73  user->Account()->o = new SQLOper(user->Account()->display, ot);
74  }
75 
76  if (!user->HasMode("OPER"))
77  {
78  IRCD->SendOper(user);
79 
80  if (!modes.empty())
81  user->SetModes(OperServ, "%s", modes.c_str());
82  }
83  }
84 
86  {
87  SQLOperResultDeleter d(this);
88  Log(this->owner) << "m_sql_oper: Error executing query " << r.GetQuery().query << ": " << r.GetError();
89  }
90 };
91 
92 class ModuleSQLOper : public Module
93 {
96 
98 
99  public:
100  ModuleSQLOper(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR)
101  {
102  }
103 
105  {
106  for (nickcore_map::const_iterator it = NickCoreList->begin(), it_end = NickCoreList->end(); it != it_end; ++it)
107  {
108  NickCore *nc = it->second;
109 
110  if (nc->o && dynamic_cast<SQLOper *>(nc->o))
111  {
112  delete nc->o;
113  nc->o = NULL;
114  }
115  }
116  }
117 
119  {
120  Configuration::Block *config = conf->GetModule(this);
121 
122  this->engine = config->Get<const Anope::string>("engine");
123  this->query = config->Get<const Anope::string>("query");
124 
125  this->SQL = ServiceReference<SQL::Provider>("SQL::Provider", this->engine);
126  }
127 
129  {
130  if (!this->SQL)
131  {
132  Log() << "Unable to find SQL engine";
133  return;
134  }
135 
136  SQL::Query q(this->query);
137  q.SetValue("a", u->Account()->display);
138  q.SetValue("i", u->ip.addr());
139 
140  this->SQL->Run(new SQLOperResult(this, u), q);
141 
142  Log(LOG_DEBUG) << "m_sql_oper: Checking authentication for " << u->Account()->display;
143  }
144 };
145 
Anope::string query
Definition: m_sql_oper.cpp:95
Definition: bots.h:24
bool HasMode(const Anope::string &name) const
Definition: users.cpp:513
Module * owner
Definition: sql.h:186
CoreExport Serialize::Checker< nickcore_map > NickCoreList
SQLOperResult(Module *m, User *u)
Definition: m_sql_oper.cpp:21
Definition: opertype.h:18
SQLOperResultDeleter(SQLOperResult *r)
Definition: m_sql_oper.cpp:16
Definition: users.h:34
void OnReload(Configuration::Conf *conf) anope_override
Definition: m_sql_oper.cpp:118
void OnResult(const SQL::Result &r) anope_override
Definition: m_sql_oper.cpp:23
void OnError(const SQL::Result &r) anope_override
Definition: m_sql_oper.cpp:85
void SetValue(const Anope::string &key, const T &value, bool escape=true)
Definition: sql.h:121
static OperType * Find(const Anope::string &name)
Definition: opertype.cpp:42
Interface(Module *m)
Definition: sql.h:188
SQLOper(const Anope::string &n, OperType *o)
Definition: m_sql_oper.cpp:6
Definition: sql.h:96
Definition: Config.cs:26
OperType * ot
Definition: opertype.h:23
Anope::string engine
Definition: m_sql_oper.cpp:94
ServiceReference< SQL::Provider > SQL
Definition: m_sql_oper.cpp:97
Anope::string display
Definition: account.h:113
Oper * o
Definition: account.h:131
Reference< User > user
Definition: m_sql_oper.cpp:11
void OnNickIdentify(User *u) anope_override
Definition: m_sql_oper.cpp:128
static Anope::map< std::pair< bool, Anope::string > > modes
Definition: cs_mode.cpp:745
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
CoreExport IRCDProto * IRCD
Definition: protocol.cpp:23
#define MODULE_INIT(x)
Definition: modules.h:45
Definition: sql.h:8
void SetModes(BotInfo *bi, const char *umodes,...)
Definition: users.cpp:601
void RemoveMode(BotInfo *bi, UserMode *um, const Anope::string &param="")
Definition: users.cpp:587
Anope::string nick
Definition: users.h:62
NickCore * Account() const
Definition: users.cpp:422
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
T Get(const Anope::string &tag)
Definition: config.h:44
virtual void SendOper(User *u)
Definition: protocol.cpp:426
Definition: modules.h:163
ModuleSQLOper(const Anope::string &modname, const Anope::string &creator)
Definition: m_sql_oper.cpp:100