]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqllog.cpp
The mysql worker thread only sleeps 50 nanosecs between cycles, this is not enough...
[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
216         void ReadConfig()
217         {
218                 ConfigReader Conf(ServerInstance);
219                 dbid = Conf.ReadValue("sqllog","dbid",0);       // database id of a database configured in sql module
220         }
221
222         virtual void OnRehash(User* user, const std::string &parameter)
223         {
224                 ReadConfig();
225         }
226
227         virtual char* OnRequest(Request* request)
228         {
229                 if(strcmp(SQLRESID, request->GetId()) == 0)
230                 {
231                         SQLresult* res;
232                         std::map<unsigned long, QueryInfo*>::iterator n;
233
234                         res = static_cast<SQLresult*>(request);
235                         n = active_queries.find(res->id);
236
237                         if (n != active_queries.end())
238                         {
239                                 n->second->Go(res);
240                                 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
241                                 active_queries.erase(n);
242                         }
243
244                         return SQLSUCCESS;
245                 }
246
247                 return NULL;
248         }
249
250         void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
251         {
252                 // is the sql module loaded? If not, we don't attempt to do anything.
253                 if (!SQLModule)
254                         return;
255
256                 SQLrequest req = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
257                 if(req.Send())
258                 {
259                         QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
260                         i->qs = FIND_SOURCE;
261                         active_queries[req.id] = i;
262                 }
263         }
264
265         virtual void OnOper(User* user, const std::string &opertype)
266         {
267                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
268         }
269
270         virtual void OnGlobalOper(User* user)
271         {
272                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
273         }
274
275         virtual int OnKill(User* source, User* dest, const std::string &reason)
276         {
277                 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
278                 return 0;
279         }
280
281         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
282         {
283                 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
284                 {
285                         AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
286                 }
287                 return 0;
288         }
289
290         virtual void OnUserConnect(User* user)
291         {
292                 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
293         }
294
295         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
296         {
297                 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
298         }
299
300         virtual void OnLoadModule(Module* mod, const std::string &name)
301         {
302                 AddLogEntry(LT_LOADMODULE,name,ServerInstance->Config->ServerName, ServerInstance->Config->ServerName);
303         }
304
305         virtual Version GetVersion()
306         {
307                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
308         }
309         
310 };
311
312 MODULE_INIT(ModuleSQLLog)