]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqllog.cpp
Convert even more
[user/henk/code/inspircd.git] / src / modules / extra / m_sqllog.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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
186  public:
187         ModuleSQLLog(InspIRCd* Me)
188         : Module::Module(Me)
189         {
190                 ServerInstance->Modules->UseInterface("SQLutils");
191                 ServerInstance->Modules->UseInterface("SQL");
192
193                 Module* SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
194                 if (!SQLutils)
195                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
196
197                 SQLModule = ServerInstance->Modules->FindFeature("SQL");
198
199                 OnRehash(NULL,"");
200                 MyMod = this;
201                 active_queries.clear();
202
203                 Implementation eventlist[] = { I_OnRehash, I_OnOper, I_OnGlobalOper, I_OnKill,
204                         I_OnPreCommand, I_OnUserConnect, I_OnUserQuit, I_OnLoadModule, I_OnRequest };
205                 ServerInstance->Modules->Attach(eventlist, this, 9);
206         }
207
208         virtual ~ModuleSQLLog()
209         {
210                 ServerInstance->Modules->DoneWithInterface("SQL");
211                 ServerInstance->Modules->DoneWithInterface("SQLutils");
212         }
213
214
215         void ReadConfig()
216         {
217                 ConfigReader Conf(ServerInstance);
218                 dbid = Conf.ReadValue("sqllog","dbid",0);       // database id of a database configured in sql module
219         }
220
221         virtual void OnRehash(User* user, const std::string &parameter)
222         {
223                 ReadConfig();
224         }
225
226         virtual const char* OnRequest(Request* request)
227         {
228                 if(strcmp(SQLRESID, request->GetId()) == 0)
229                 {
230                         SQLresult* res;
231                         std::map<unsigned long, QueryInfo*>::iterator n;
232
233                         res = static_cast<SQLresult*>(request);
234                         n = active_queries.find(res->id);
235
236                         if (n != active_queries.end())
237                         {
238                                 n->second->Go(res);
239                                 active_queries.erase(n);
240                         }
241
242                         return SQLSUCCESS;
243                 }
244
245                 return NULL;
246         }
247
248         void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
249         {
250                 // is the sql module loaded? If not, we don't attempt to do anything.
251                 if (!SQLModule)
252                         return;
253
254                 SQLrequest req = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
255                 if(req.Send())
256                 {
257                         QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
258                         i->qs = FIND_SOURCE;
259                         active_queries[req.id] = i;
260                 }
261         }
262
263         virtual void OnOper(User* user, const std::string &opertype)
264         {
265                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
266         }
267
268         virtual void OnGlobalOper(User* user)
269         {
270                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
271         }
272
273         virtual int OnKill(User* source, User* dest, const std::string &reason)
274         {
275                 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
276                 return 0;
277         }
278
279         virtual int OnPreCommand(const std::string &command, const std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
280         {
281                 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
282                 {
283                         AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+parameters[0],user->server);
284                 }
285                 return 0;
286         }
287
288         virtual void OnUserConnect(User* user)
289         {
290                 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
291         }
292
293         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
294         {
295                 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
296         }
297
298         virtual void OnLoadModule(Module* mod, const std::string &name)
299         {
300                 AddLogEntry(LT_LOADMODULE,name,ServerInstance->Config->ServerName, ServerInstance->Config->ServerName);
301         }
302
303         virtual Version GetVersion()
304         {
305                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
306         }
307         
308 };
309
310 MODULE_INIT(ModuleSQLLog)