summaryrefslogtreecommitdiff
path: root/modules/extra/m_sql_oper.cpp
blob: 523272a4933e7468f057598101a2f0dcc69411f1 (plain)
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
#include "module.h"
#include "sql.h"

class SQLOperResult : public SQLInterface
{
	Reference<User> user;

	struct SQLOperResultDeleter
	{
		SQLOperResult *res;
		SQLOperResultDeleter(SQLOperResult *r) : res(r) { }
		~SQLOperResultDeleter() { delete res; }
	};

 public:
	SQLOperResult(Module *m, User *u) : SQLInterface(m), user(u) { }

	void OnResult(const SQLResult &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 SQLException &)
		{
			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 SQLException &) { }

		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, UMODE_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(UMODE_OPER))
		{
			IRCD->SendOper(user);

			if (!modes.empty())
				user->SetModes(OperServ, "%s", modes.c_str());
		}
	}

	void OnError(const SQLResult &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<SQLProvider> SQL;

 public:
	ModuleSQLOper(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, SUPPORTED)
	{
		this->SetAuthor("Anope");

		Implementation i[] = { I_OnReload, I_OnNickIdentify };
		ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));

		this->OnReload();
	}

	void OnReload() anope_override
	{
		ConfigReader config;

		this->engine = config.ReadValue("m_sql_oper", "engine", "", 0);
		this->query = config.ReadValue("m_sql_oper", "query", "", 0);

		this->SQL = ServiceReference<SQLProvider>("SQLProvider", this->engine);
	}

	void OnNickIdentify(User *u) anope_override
	{
		if (!this->SQL)
		{
			Log() << "Unable to find SQL engine";
			return;
		}

		SQLQuery 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)