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