aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/m_sql_command.cpp
blob: 58fa487e6fff6e2a4c2b5ed8afde65efcf568db5 (about) (plain) (blame)
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
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
 *
 * This file is part of InspIRCd.  InspIRCd 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 <http://www.gnu.org/licenses/>.
 */


/* $ModDesc: Add commands that execute arbitrary SQL queries */

#include "inspircd.h"
#include "command_parse.h"
#include "sql.h"

class UserQuery : public SQLQuery
{
 public:
	const std::string uid;
	reference<ConfigTag> tag;
	UserQuery(Module* me, const std::string& u, ConfigTag* q)
		: SQLQuery(me), uid(u), tag(q)
	{
	}

	void OnResult(SQLResult& res)
	{
		User* user = ServerInstance->FindUUID(uid);
		if (!user)
			return;
		int rows = res.Rows();
		std::string format = tag->getString("rowformat");
		std::map<std::string, std::string> items;
		user->PopulateInfoMap(items);
		if (!format.empty())
		{
			SQLEntries result;
			std::vector<std::string> cols;
			res.GetCols(cols);
			while (res.GetRow(result))
			{
				std::map<std::string, std::string> row(items);
				for(unsigned int i=0; i < cols.size(); i++)
				{
					if (result[i].nul)
						continue;
					row.insert(std::make_pair(cols[i], result[i].value));
				}

				user->SendText(MapFormatSubst(format, row));
			}
		}
		format = tag->getString("resultformat");
		if (!format.empty())
		{
			items.insert(std::make_pair("rows", ConvToStr(rows)));
			user->SendText(MapFormatSubst(format, items));
		}
	}

	void OnError(SQLerror& error)
	{
		User* user = ServerInstance->FindUUID(uid);
		if (!user)
			return;
		std::string format = tag->getString("errorformat", ":$server NOTICE $nick :SQL command failed: $msg");
		if (!format.empty())
		{
			std::map<std::string, std::string> items;
			user->PopulateInfoMap(items);
			items.insert(std::make_pair("msg", error.str));
			user->SendText(MapFormatSubst(format, items));
		}
	}
};

class SQLCommand : public Command
{
 public:
	reference<ConfigTag> tag;
	dynamic_reference<SQLProvider> db;
	SQLCommand(Module* Parent, ConfigTag* Tag)
		: Command(Parent, Tag->getString("name")), tag(Tag), db("SQL/" + Tag->getString("dbid"))
	{
		syntax = Tag->getString("syntax");
		if (Tag->getBool("operonly", true))
			flags_needed = 'o';
		if (!db)
			throw CoreException("Could not find database");
	}

	CmdResult Handle(const std::vector<std::string> &parameters, User *user)
    {
		if (!db)
			return CMD_FAILURE;

		ParamM userinfo;
		user->PopulateInfoMap(userinfo);
		for(unsigned int i=0; i < parameters.size(); i++)
			userinfo.insert(std::make_pair(ConvToStr(i + 1), parameters[i]));
		std::string fmt = tag->getString("queryformat");

		db->submit(new UserQuery(creator, user->uuid, tag), fmt, userinfo);
		return CMD_SUCCESS;
	}
};

class ModuleSQLCommand : public Module
{
 public:
	std::vector<SQLCommand*> cmds;
	void init()
	{
		// everything is in readconfig
	}

	void ReadConfig(ConfigReadStatus& status)
	{
		for(std::vector<SQLCommand*>::iterator i = cmds.begin(); i != cmds.end(); i++)
		{
			ServerInstance->Parser->RemoveCommand(*i);
			delete *i;
		}
		cmds.clear();
		ConfigTagList tags = ServerInstance->Config->GetTags("sqlcommand");
		for(ConfigIter i = tags.first; i != tags.second; i++)
		{
			ConfigTag* tag = i->second;
			SQLCommand* cmd = NULL;
			try
			{
				cmd = new SQLCommand(this, tag);
				ServerInstance->Modules->AddService(*cmd);
				cmds.push_back(cmd);
			}
			catch (CoreException& e)
			{
				status.ReportError(tag, e.err.c_str(), false);
				delete cmd;
			}
		}
	}

	~ModuleSQLCommand()
	{
		for(std::vector<SQLCommand*>::iterator i = cmds.begin(); i != cmds.end(); i++)
			delete *i;
	}

	Version GetVersion()
	{
		return Version("Add commands that execute arbitrary SQL queries", VF_VENDOR);
	}
};

MODULE_INIT(ModuleSQLCommand)