diff options
Diffstat (limited to 'modules/extra/sql_log.cpp')
-rw-r--r-- | modules/extra/sql_log.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/modules/extra/sql_log.cpp b/modules/extra/sql_log.cpp new file mode 100644 index 000000000..a02d63b8c --- /dev/null +++ b/modules/extra/sql_log.cpp @@ -0,0 +1,120 @@ +/* + * 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) + { + } + + 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 Log *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>("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->type) + { + 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; + } + + insert.SetValue("user", l->u ? l->u->nick : ""); + insert.SetValue("acc", l->nc ? l->nc->GetDisplay() : ""); + insert.SetValue("command", l->c ? l->c->name : ""); + insert.SetValue("channel", l->ci ? l->ci->GetName() : ""); + insert.SetValue("msg", msg); + + SQL->Run(NULL, insert); + } +}; + +MODULE_INIT(SQLLog) |