Anope IRC Services  Version 2.0
m_sql_log.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  */
9 
10 #include "module.h"
11 #include "modules/sql.h"
12 
13 class SQLLog : public Module
14 {
15  std::set<Anope::string> inited;
17 
18  public:
19  SQLLog(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR | EXTRA)
20  {
21  }
22 
24  {
25  Configuration::Block *config = conf->GetModule(this);
26  this->table = config->Get<const Anope::string>("table", "logs");
27  }
28 
29  void OnLogMessage(LogInfo *li, const Log *l, const Anope::string &msg) anope_override
30  {
31  Anope::string ref_name;
33 
34  for (unsigned i = 0; i < li->targets.size(); ++i)
35  {
36  const Anope::string &target = li->targets[i];
37  size_t sz = target.find("sql_log:");
38  if (!sz)
39  {
40  ref_name = target.substr(8);
41  SQL = ServiceReference<SQL::Provider>("SQL::Provider", ref_name);
42  break;
43  }
44  }
45 
46  if (!SQL)
47  return;
48 
49  if (!inited.count(ref_name))
50  {
51  inited.insert(ref_name);
52 
53  SQL::Query create("CREATE TABLE IF NOT EXISTS `" + table + "` ("
54  "`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,"
55  "`type` varchar(64) NOT NULL,"
56  "`user` varchar(64) NOT NULL,"
57  "`acc` varchar(64) NOT NULL,"
58  "`command` varchar(64) NOT NULL,"
59  "`channel` varchar(64) NOT NULL,"
60  "`msg` text NOT NULL"
61  ")");
62 
63  SQL->Run(NULL, create);
64  }
65 
66  SQL::Query insert("INSERT INTO `" + table + "` (`type`,`user`,`acc`,`command`,`channel`,`msg`)"
67  "VALUES (@type@, @user@, @acc@, @command@, @channel@, @msg@)");
68 
69  switch (l->type)
70  {
71  case LOG_ADMIN:
72  insert.SetValue("type", "ADMIN");
73  break;
74  case LOG_OVERRIDE:
75  insert.SetValue("type", "OVERRIDE");
76  break;
77  case LOG_COMMAND:
78  insert.SetValue("type", "COMMAND");
79  break;
80  case LOG_SERVER:
81  insert.SetValue("type", "SERVER");
82  break;
83  case LOG_CHANNEL:
84  insert.SetValue("type", "CHANNEL");
85  break;
86  case LOG_USER:
87  insert.SetValue("type", "USER");
88  break;
89  case LOG_MODULE:
90  insert.SetValue("type", "MODULE");
91  break;
92  case LOG_NORMAL:
93  insert.SetValue("type", "NORMAL");
94  break;
95  default:
96  return;
97  }
98 
99  insert.SetValue("user", l->u ? l->u->nick : "");
100  insert.SetValue("acc", l->nc ? l->nc->display : "");
101  insert.SetValue("command", l->c ? l->c->name : "");
102  insert.SetValue("channel", l->ci ? l->ci->name : "");
103  insert.SetValue("msg", msg);
104 
105  SQL->Run(NULL, insert);
106  }
107 };
108 
Anope::string table
Definition: m_sql_log.cpp:16
void SetValue(const Anope::string &key, const T &value, bool escape=true)
Definition: sql.h:121
string substr(size_type pos=0, size_type n=npos) const
Definition: anope.h:277
Definition: sql.h:96
void OnLogMessage(LogInfo *li, const Log *l, const Anope::string &msg) anope_override
Definition: m_sql_log.cpp:29
SQLLog(const Anope::string &modname, const Anope::string &creator)
Definition: m_sql_log.cpp:19
#define anope_override
Definition: services.h:56
#define MODULE_INIT(x)
Definition: modules.h:45
Definition: sql.h:8
void OnReload(Configuration::Conf *conf) anope_override
Definition: m_sql_log.cpp:23
Definition: logger.h:53
T Get(const Anope::string &tag)
Definition: config.h:44
std::set< Anope::string > inited
Definition: m_sql_log.cpp:15
size_type find(const string &_str, size_type pos=0) const
Definition: anope.h:192
Definition: modules.h:163