1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#include "module.h"
#include "sql.h"
class SQLOperResult : public SQL::Interface
{
Reference<User> user;
struct SQLOperResultDeleter
{
SQLOperResult *res;
SQLOperResultDeleter(SQLOperResult *r) : res(r) { }
~SQLOperResultDeleter() { delete res; }
};
public:
SQLOperResult(Module *m, User *u) : SQL::Interface(m), user(u) { }
void OnResult(const SQL::Result &r) anope_override
{
SQLOperResultDeleter d(this);
if (!user || !user->Account() || r.Rows() == 0)
return;
Anope::string opertype;
try
{
opertype = r.Get(0, "opertype");
}
catch (const SQL::Exception &)
{
return;
}
Log(LOG_DEBUG) << "m_sql_oper: Got result for " << user->nick << ", opertype " << opertype;
Anope::string modes;
try
{
modes = r.Get(0, "modes");
}
catch (const SQL::Exception &) { }
if (opertype.empty())
{
if (user->Account() && user->Account()->o && !user->Account()->o->config)
{
std::vector<Oper *>::iterator it = std::find(Config->Opers.begin(), Config->Opers.end(), user->Account()->o);
if (it != Config->Opers.end())
Config->Opers.erase(it);
delete user->Account()->o;
user->Account()->o = NULL;
Log(this->owner) << "m_sql_oper: Removed services operator from " << user->nick << " (" << user->Account()->display << ")";
user->RemoveMode(OperServ, "OPER"); // Probably not set, just incase
}
return;
}
OperType *ot = OperType::Find(opertype);
if (ot == NULL)
{
Log(this->owner) << "m_sql_oper: Oper " << user->nick << " has type " << opertype << ", but this opertype does not exist?";
return;
}
if (!user->Account()->o || user->Account()->o->ot != ot)
{
Log(this->owner) << "m_sql_oper: Tieing oper " << user->nick << " to type " << opertype;
user->Account()->o = new Oper(user->Account()->display, ot);
Config->Opers.push_back(user->Account()->o);
}
if (!user->HasMode("OPER"))
{
IRCD->SendOper(user);
if (!modes.empty())
user->SetModes(OperServ, "%s", modes.c_str());
}
}
void OnError(const SQL::Result &r) anope_override
{
SQLOperResultDeleter d(this);
Log(this->owner) << "m_sql_oper: Error executing query " << r.GetQuery().query << ": " << r.GetError();
}
};
class ModuleSQLOper : public Module
{
Anope::string engine;
Anope::string query;
ServiceReference<SQL::Provider> SQL;
public:
ModuleSQLOper(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR)
{
Implementation i[] = { I_OnReload, I_OnNickIdentify };
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
}
void OnReload(ServerConfig *conf, ConfigReader &reader) anope_override
{
this->engine = reader.ReadValue("m_sql_oper", "engine", "", 0);
this->query = reader.ReadValue("m_sql_oper", "query", "", 0);
this->SQL = ServiceReference<SQL::Provider>("SQL::Provider", this->engine);
}
void OnNickIdentify(User *u) anope_override
{
if (!this->SQL)
{
Log() << "Unable to find SQL engine";
return;
}
SQL::Query q(this->query);
q.SetValue("a", u->Account()->display);
q.SetValue("i", u->ip);
this->SQL->Run(new SQLOperResult(this, u), q);
Log(LOG_DEBUG) << "m_sql_oper: Checking authentication for " << u->Account()->display;
}
};
MODULE_INIT(ModuleSQLOper)
|