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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*
* Anope IRC Services
*
* Copyright (C) 2012-2016 Anope Team <team@anope.org>
*
* This file is part of Anope. Anope is free software; you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see see <http://www.gnu.org/licenses/>.
*/
#include "module.h"
#include "modules/sql.h"
class SQLOperResult : public SQL::Interface
{
Reference<User> user;
struct SQLOperResultDeleter
{
SQLOperResult *res;
SQLOperResultDeleter(SQLOperResult *r) : res(r) { }
~SQLOperResultDeleter() { delete res; }
};
void Deoper()
{
Oper *oper = user->Account()->GetOper();
if (oper != nullptr)
{
oper->Delete();
Log(this->owner) << "Removed services operator from " << user->nick << " (" << user->Account()->GetDisplay() << ")";
user->RemoveMode(Config->GetClient("OperServ"), "OPER"); // Probably not set, just incase
}
}
public:
SQLOperResult(Module *m, User *u) : SQL::Interface(m), user(u) { }
void OnResult(const SQL::Result &r) override
{
SQLOperResultDeleter d(this);
if (!user || !user->Account())
return;
if (r.Rows() == 0)
{
Log(LogType::DEBUG) << "m_sql_oper: Got 0 rows for " << user->nick;
Deoper();
return;
}
Anope::string opertype;
try
{
opertype = r.Get(0, "opertype");
}
catch (const SQL::Exception &)
{
Log(this->owner) << "Expected column named \"opertype\" but one was not found";
return;
}
Log(LogType::DEBUG) << "m_sql_oper: Got result for " << user->nick << ", opertype " << opertype;
Anope::string modes;
try
{
modes = r.Get(0, "modes");
}
catch (const SQL::Exception &) { }
ServiceBot *OperServ = Config->GetClient("OperServ");
if (opertype.empty())
{
Deoper();
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;
}
Oper *oper = user->Account()->GetOper();
if (oper == nullptr || oper->GetType() != ot)
{
Log(this->owner) << "m_sql_oper: Tieing oper " << user->nick << " to type " << opertype;
if (oper)
oper->Delete();
oper = Serialize::New<Oper *>();
oper->SetName(user->Account()->GetDisplay());
oper->SetType(ot);
user->Account()->SetOper(oper);
}
if (!user->HasMode("OPER"))
{
IRCD->SendOper(user);
if (!modes.empty())
user->SetModes(OperServ, "%s", modes.c_str());
}
}
void OnError(const SQL::Result &r) override
{
SQLOperResultDeleter d(this);
Log(this->owner) << "m_sql_oper: Error executing query " << r.GetQuery().query << ": " << r.GetError();
}
};
class ModuleSQLOper : public Module
, public EventHook<Event::NickIdentify>
{
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),
EventHook<Event::NickIdentify>(this)
{
}
void OnReload(Configuration::Conf *conf) override
{
Configuration::Block *config = conf->GetModule(this);
this->engine = config->Get<Anope::string>("engine");
this->query = config->Get<Anope::string>("query");
this->SQL = ServiceReference<SQL::Provider>(engine);
}
void OnNickIdentify(User *u) override
{
if (!this->SQL)
{
Log() << "Unable to find SQL engine: " << engine;
return;
}
SQL::Query q(this->query);
q.SetValue("a", u->Account()->GetDisplay());
q.SetValue("i", u->ip.addr());
this->SQL->Run(new SQLOperResult(this, u), q);
Log(LogType::DEBUG) << "m_sql_oper: Checking authentication for " << u->Account()->GetDisplay();
}
};
MODULE_INIT(ModuleSQLOper)
|