]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqllog.cpp
bf8e1a21b39641f0c1609c2c3007481d2f7caa43
[user/henk/code/inspircd.git] / src / modules / m_sqllog.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "m_sqlv2.h"
16
17 static Module* SQLModule;
18 static Module* MyMod;
19 static std::string dbid;
20
21 enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
22
23 enum QueryState { FIND_SOURCE, FIND_NICK, FIND_HOST, DONE};
24
25 class QueryInfo;
26
27 std::map<unsigned long,QueryInfo*> active_queries;
28
29 class QueryInfo
30 {
31 public:
32         QueryState qs;
33         unsigned long id;
34         std::string nick;
35         std::string source;
36         std::string hostname;
37         int sourceid;
38         int nickid;
39         int hostid;
40         int category;
41         time_t date;
42         bool insert;
43
44         QueryInfo(const std::string &n, const std::string &s, const std::string &h, unsigned long i, int cat)
45         {
46                 qs = FIND_SOURCE;
47                 nick = n;
48                 source = s;
49                 hostname = h;
50                 id = i;
51                 category = cat;
52                 sourceid = nickid = hostid = -1;
53                 date = ServerInstance->Time();
54                 insert = false;
55         }
56
57         void Go(SQLresult* res)
58         {
59                 switch (qs)
60                 {
61                         case FIND_SOURCE:
62                                 if (res->Rows() && sourceid == -1 && !insert)
63                                 {
64                                         sourceid = atoi(res->GetValue(0,0).d.c_str());
65                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
66                                         req.Send();
67                                         insert = false;
68                                         qs = FIND_NICK;
69                                         active_queries[req.id] = this;
70                                 }
71                                 else if (res->Rows() && sourceid == -1 && insert)
72                                 {
73                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
74                                         req.Send();
75                                         insert = false;
76                                         qs = FIND_SOURCE;
77                                         active_queries[req.id] = this;
78                                 }
79                                 else
80                                 {
81                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % source);
82                                         req.Send();
83                                         insert = true;
84                                         qs = FIND_SOURCE;
85                                         active_queries[req.id] = this;
86                                 }
87                         break;
88
89                         case FIND_NICK:
90                                 if (res->Rows() && nickid == -1 && !insert)
91                                 {
92                                         nickid = atoi(res->GetValue(0,0).d.c_str());
93                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
94                                         req.Send();
95                                         insert = false;
96                                         qs = FIND_HOST;
97                                         active_queries[req.id] = this;
98                                 }
99                                 else if (res->Rows() && nickid == -1 && insert)
100                                 {
101                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
102                                         req.Send();
103                                         insert = false;
104                                         qs = FIND_NICK;
105                                         active_queries[req.id] = this;
106                                 }
107                                 else
108                                 {
109                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % nick);
110                                         req.Send();
111                                         insert = true;
112                                         qs = FIND_NICK;
113                                         active_queries[req.id] = this;
114                                 }
115                         break;
116
117                         case FIND_HOST:
118                                 if (res->Rows() && hostid == -1 && !insert)
119                                 {
120                                         hostid = atoi(res->GetValue(0,0).d.c_str());
121                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid,
122                                                         SQLquery("INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES('?','?','?','?','?')") % category % nickid % hostid % sourceid % date);
123                                         req.Send();
124                                         insert = true;
125                                         qs = DONE;
126                                         active_queries[req.id] = this;
127                                 }
128                                 else if (res->Rows() && hostid == -1 && insert)
129                                 {
130                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
131                                         req.Send();
132                                         insert = false;
133                                         qs = FIND_HOST;
134                                         active_queries[req.id] = this;
135                                 }
136                                 else
137                                 {
138                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_hosts (hostname) VALUES('?')") % hostname);
139                                         req.Send();
140                                         insert = true;
141                                         qs = FIND_HOST;
142                                         active_queries[req.id] = this;
143                                 }
144                         break;
145
146                         case DONE:
147                         break;
148                 }
149         }
150 };
151
152 /* $ModDesc: Logs network-wide data to an SQL database */
153
154 class ModuleSQLLog : public Module
155 {
156
157  public:
158         ModuleSQLLog()
159         {
160                 Module* SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
161                 if (!SQLutils)
162                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
163
164                 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_DATA, "SQL");
165                 if (!prov)
166                         throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
167                 SQLModule = prov->creator;
168
169                 OnRehash(NULL);
170                 MyMod = this;
171                 active_queries.clear();
172
173                 Implementation eventlist[] = { I_OnRehash, I_OnOper, I_OnGlobalOper, I_OnKill,
174                         I_OnPreCommand, I_OnUserRegister, I_OnUserQuit, I_OnLoadModule };
175                 ServerInstance->Modules->Attach(eventlist, this, 8);
176         }
177
178         void ReadConfig()
179         {
180                 ConfigReader Conf;
181                 dbid = Conf.ReadValue("sqllog","dbid",0);       // database id of a database configured in sql module
182         }
183
184         virtual void OnRehash(User* user)
185         {
186                 ReadConfig();
187         }
188
189         void OnRequest(Request& request)
190         {
191                 if(strcmp(SQLRESID, request.id) == 0)
192                 {
193                         SQLresult* res = static_cast<SQLresult*>(&request);
194                         std::map<unsigned long, QueryInfo*>::iterator n;
195
196                         n = active_queries.find(res->id);
197
198                         if (n != active_queries.end())
199                         {
200                                 n->second->Go(res);
201                                 active_queries.erase(n);
202                         }
203                 }
204         }
205
206         void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
207         {
208                 // is the sql module loaded? If not, we don't attempt to do anything.
209                 if (!SQLModule)
210                         return;
211
212                 SQLrequest req = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
213                 req.Send();
214                 QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
215                 i->qs = FIND_SOURCE;
216                 active_queries[req.id] = i;
217         }
218
219         virtual void OnOper(User* user, const std::string &opertype)
220         {
221                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
222         }
223
224         virtual void OnGlobalOper(User* user)
225         {
226                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
227         }
228
229         virtual ModResult OnKill(User* source, User* dest, const std::string &reason)
230         {
231                 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
232                 return MOD_RES_PASSTHRU;
233         }
234
235         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
236         {
237                 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
238                 {
239                         AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+parameters[0],user->server);
240                 }
241                 return MOD_RES_PASSTHRU;
242         }
243
244         virtual ModResult OnUserRegister(LocalUser* user)
245         {
246                 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
247                 return MOD_RES_PASSTHRU;
248         }
249
250         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
251         {
252                 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
253         }
254
255         virtual void OnLoadModule(Module* mod)
256         {
257                 AddLogEntry(LT_LOADMODULE,mod->ModuleSourceFile,ServerInstance->Config->ServerName.c_str(), ServerInstance->Config->ServerName.c_str());
258         }
259
260         virtual Version GetVersion()
261         {
262                 return Version("Logs network-wide data to an SQL database", VF_VENDOR);
263         }
264
265 };
266
267 MODULE_INIT(ModuleSQLLog)