summaryrefslogtreecommitdiff
path: root/src/modules/mysql/db_mysql_execute.cpp
blob: 3933d54d1c4f116b9c89f541b58986cfd11b94a6 (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
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
171
172
173
174
175
176
/* RequiredLibraries: mysqlpp */

#include "db_mysql.h"
#define HASH(nick)      (((nick)[0]&31)<<5 | ((nick)[1]&31))

class FakeNickCore : public NickCore
{
 public:
 	FakeNickCore() : NickCore("-SQLUser")
	{
		if (this->next)
			this->next->prev = this->prev;
		if (this->prev)
			this->prev->next = this->next;
		else
			nclists[HASH(this->display)] = this->next;
	}

	~FakeNickCore()
	{
		insert_core(this);
		Users.clear();
	}

 	bool IsServicesOper() const { return true; }
	bool HasCommand(const std::string &) const { return true; }
	bool HasPriv(const std::string &) const { return true; }
} SQLCore;

class FakeUser : public User
{
 public:
 	FakeUser() : User("-SQLUser", "")
	{
		this->SetIdent("SQL");
		this->host = sstrdup(Config.ServerName);
		this->realname = sstrdup("Fake SQL User");
		this->hostip = sstrdup("255.255.255.255");
		this->vhost = NULL;
		this->server = Me;

		if (this->prev)
			this->prev->next = this->next;
		else
			userlist[HASH(this->nick.c_str())] = this->next;
		if (this->next)
			this->next->prev = this->prev;
		--usercnt;
	}

	~FakeUser()
	{
		User **list = &userlist[HASH(this->nick.c_str())];
		this->next = *list;
		if (*list)
			(*list)->prev = this;
		*list = this;
		++usercnt;
		nc = NULL;
	}

	void SetNewNick(const std::string &newnick) { this->nick = newnick; }

	void SendMessage(const std::string &, const char *, ...) { }
	void SendMessage(const std::string &, const std::string &) { }

	NickCore *Account() const { return nc; }
	const bool IsIdentified(bool) const { return nc ? true : false; }
} SQLUser;

class SQLTimer : public Timer
{
 public:
	SQLTimer() : Timer(me->Delay, time(NULL), true)
	{
		mysqlpp::Query query(me->Con);
		query << "TRUNCATE TABLE `anope_commands`";
		ExecuteQuery(query);
	}

	void Tick(time_t)
	{
		mysqlpp::Query query(me->Con);
		mysqlpp::StoreQueryResult qres;

		query << "SELECT * FROM `anope_commands`";
		qres = StoreQuery(query);

		if (qres && qres.num_rows())
		{
			for (size_t i = 0; i < qres.num_rows(); ++i)
			{
				User *u;
				NickAlias *na = NULL;
				bool logout = false;

				/* If they want -SQLUser to execute the command, use it */
				if (qres[i]["nick"] == "-SQLUser")
				{
					u = &SQLUser;
					u->SetNewNick("-SQLUser");
					u->Login(&SQLCore);
					logout = true;
				}
				else
				{
					/* See if the nick they want to execute the command is registered */
					na = findnick(SQLAssign(qres[i]["nick"]));
					if (na)
					{
						/* If it is and someone is online using that nick, use them */
						if (!na->nc->Users.empty())
							u = na->nc->Users.front();
						/* Make a fake nick and use that logged in as the nick we want to use */
						else
						{
							u = &SQLUser;
							u->SetNewNick(SQLAssign(qres[i]["nick"]));
							u->Login(na->nc);
							logout = true;
						}
					}
					else
					{
						/* Check if someone is online using the nick now */
						u = finduser(SQLAssign(qres[i]["nick"]));
						/* If they arent make a fake user, and use them */
						if (!u)
						{
							u = &SQLUser;
							u->SetNewNick(SQLAssign(qres[i]["nick"]));
							u->Logout();
							logout = true;
						}
					}
				}

				BotInfo *bi = findbot(SQLAssign(qres[i]["service"]));
				if (!bi)
				{
					Alog() << "Warning: SQL command for unknown service " << qres[i]["service"];
					continue;
				}

				// XXX this whole strtok thing needs to die
				char *cmdbuf = sstrdup(qres[i]["command"].c_str());
				char *cmd = strtok(cmdbuf, " ");
				mod_run_cmd(bi->nick, u, bi->cmdTable, cmd);
				delete [] cmdbuf;

				if (logout)
					u->Logout();
			}

			query << "TRUNCATE TABLE `anope_commands`";
			ExecuteQuery(query);
		}
	}
};

class DBMySQLExecute : public DBMySQL
{
	SQLTimer *_SQLTimer;
 public:
	DBMySQLExecute(const std::string &modname, const std::string &creator) : DBMySQL(modname, creator)
	{
		_SQLTimer = new SQLTimer();
	}
	
	~DBMySQLExecute()
	{
		TimerManager::DelTimer(_SQLTimer);
	}
};

MODULE_INIT(DBMySQLExecute)