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