summaryrefslogtreecommitdiff
path: root/modules/extra/sql_log.cpp
blob: 11c8ebd1a8fe1dee5d0c5094b3b0456241f3950e (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
/*
 * Anope IRC Services
 *
 * Copyright (C) 2014-2017 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 SQLLog : public Module
	, public EventHook<Event::LogMessage>
{
	std::set<Anope::string> inited;
	Anope::string table;

 public:
	SQLLog(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR | EXTRA)
		, EventHook<Event::LogMessage>(this)
	{
	}

	void OnReload(Configuration::Conf *conf) override
	{
		Configuration::Block *config = conf->GetModule(this);
		this->table = config->Get<Anope::string>("table", "logs");
	}

	void OnLogMessage(LogInfo *li, const Logger *l, const Anope::string &msg) override
	{
		Anope::string ref_name;
		ServiceReference<SQL::Provider> SQL;

		for (unsigned i = 0; i < li->targets.size(); ++i)
		{
			const Anope::string &target = li->targets[i];
			size_t sz = target.find("sql_log:");
			if (!sz)
			{
				ref_name = target.substr(8);
				SQL = ServiceReference<SQL::Provider>(ref_name);
				break;
			}
		}

		if (!SQL)
			return;

		if (!inited.count(ref_name))
		{
			inited.insert(ref_name);

			SQL::Query create("CREATE TABLE IF NOT EXISTS `" + table + "` ("
						"`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,"
						"`type` varchar(64) NOT NULL,"
						"`user` varchar(64) NOT NULL,"
						"`acc` varchar(64) NOT NULL,"
						"`command` varchar(64) NOT NULL,"
						"`channel` varchar(64) NOT NULL,"
						"`msg` text NOT NULL"
					")");

			SQL->Run(NULL, create);
		}

		SQL::Query insert("INSERT INTO `" + table + "` (`type`,`user`,`acc`,`command`,`channel`,`msg`)"
			"VALUES (@type@, @user@, @acc@, @command@, @channel@, @msg@)");

		switch (l->GetType())
		{
			case LogType::ADMIN:
				insert.SetValue("type", "ADMIN");
				break;
			case LogType::OVERRIDE:
				insert.SetValue("type", "OVERRIDE");
				break;
			case LogType::COMMAND:
				insert.SetValue("type", "COMMAND");
				break;
			case LogType::SERVER:
				insert.SetValue("type", "SERVER");
				break;
			case LogType::CHANNEL:
				insert.SetValue("type", "CHANNEL");
				break;
			case LogType::USER:
				insert.SetValue("type", "USER");
				break;
			case LogType::MODULE:
				insert.SetValue("type", "MODULE");
				break;
			case LogType::NORMAL:
				insert.SetValue("type", "NORMAL");
				break;
			default:
				return;
		}

		User *u = l->GetUser();
		insert.SetValue("user", u ? u->nick : "");

		NickServ::Account *acc = l->GetAccount();
		insert.SetValue("acc", acc ? acc->GetDisplay() : "");

		Command *cmd = l->GetCommand();
		insert.SetValue("command", cmd ? cmd->GetName() : "");

		ChanServ::Channel *ci = l->GetCi();
		insert.SetValue("channel", ci ? ci->GetName() : "");

		insert.SetValue("msg", msg);

		SQL->Run(NULL, insert);
	}
};

MODULE_INIT(SQLLog)