]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqllog.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / modules / extra / m_sqllog.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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                 ServerInstance = Instance;
47                 qs = FIND_SOURCE;
48                 nick = n;
49                 source = s;
50                 hostname = h;
51                 id = i;
52                 category = cat;
53                 sourceid = nickid = hostid = -1;
54                 date = ServerInstance->Time();
55                 insert = false;
56         }
57
58         void Go(SQLresult* res)
59         {
60                 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery(""));
61                 switch (qs)
62                 {
63                         case FIND_SOURCE:
64                                 if (res->Rows() && sourceid == -1 && !insert)
65                                 {
66                                         sourceid = atoi(res->GetValue(0,0).d.c_str());
67                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
68                                         if(req.Send())
69                                         {
70                                                 insert = false;
71                                                 qs = FIND_NICK;
72                                                 active_queries[req.id] = this;
73                                         }
74                                 }
75                                 else if (res->Rows() && sourceid == -1 && insert)
76                                 {
77                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
78                                         if(req.Send())
79                                         {
80                                                 insert = false;
81                                                 qs = FIND_SOURCE;
82                                                 active_queries[req.id] = this;
83                                         }
84                                 }
85                                 else
86                                 {
87                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % source);
88                                         if(req.Send())
89                                         {
90                                                 insert = true;
91                                                 qs = FIND_SOURCE;
92                                                 active_queries[req.id] = this;
93                                         }
94                                 }
95                         break;
96
97                         case FIND_NICK:
98                                 if (res->Rows() && nickid == -1 && !insert)
99                                 {
100                                         nickid = atoi(res->GetValue(0,0).d.c_str());
101                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
102                                         if(req.Send())
103                                         {
104                                                 insert = false;
105                                                 qs = FIND_HOST;
106                                                 active_queries[req.id] = this;
107                                         }
108                                 }
109                                 else if (res->Rows() && nickid == -1 && insert)
110                                 {
111                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
112                                         if(req.Send())
113                                         {
114                                                 insert = false;
115                                                 qs = FIND_NICK;
116                                                 active_queries[req.id] = this;
117                                         }
118                                 }
119                                 else
120                                 {
121                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % nick);
122                                         if(req.Send())
123                                         {
124                                                 insert = true;
125                                                 qs = FIND_NICK;
126                                                 active_queries[req.id] = this;
127                                         }
128                                 }
129                         break;
130
131                         case FIND_HOST:
132                                 if (res->Rows() && hostid == -1 && !insert)
133                                 {
134                                         hostid = atoi(res->GetValue(0,0).d.c_str());
135                                         req = SQLrequest(MyMod, SQLModule, dbid,
136                                                         SQLquery("INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES('?','?','?','?','?')") % category % nickid % hostid % sourceid % date);
137                                         if(req.Send())
138                                         {
139                                                 insert = true;
140                                                 qs = DONE;
141                                                 active_queries[req.id] = this;
142                                         }
143                                 }
144                                 else if (res->Rows() && hostid == -1 && insert)
145                                 {
146                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
147                                         if(req.Send())
148                                         {
149                                                 insert = false;
150                                                 qs = FIND_HOST;
151                                                 active_queries[req.id] = this;
152                                         }
153                                 }
154                                 else
155                                 {
156                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_hosts (hostname) VALUES('?')") % hostname);
157                                         if(req.Send())
158                                         {
159                                                 insert = true;
160                                                 qs = FIND_HOST;
161                                                 active_queries[req.id] = this;
162                                         }
163                                 }
164                         break;
165
166                         case DONE:
167                                 std::map<unsigned long,QueryInfo*>::iterator x = active_queries.find(req.id);
168                                 if (x != active_queries.end())
169                                 {
170                                         delete x->second;
171                                         active_queries.erase(x);
172                                 }
173                         break;
174                 }
175         }
176 };
177
178 /* $ModDesc: Logs network-wide data to an SQL database */
179
180 class ModuleSQLLog : public Module
181 {
182
183  public:
184         ModuleSQLLog()
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;
214                 dbid = Conf.ReadValue("sqllog","dbid",0);       // database id of a database configured in sql module
215         }
216
217         virtual void OnRehash(User* user)
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 ModResult OnKill(User* source, User* dest, const std::string &reason)
270         {
271                 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
272                 return MOD_RES_PASSTHRU;
273         }
274
275         virtual ModResult 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 MOD_RES_PASSTHRU;
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("Logs network-wide data to an SQL database", VF_VENDOR, API_VERSION);
302         }
303
304 };
305
306 MODULE_INIT(ModuleSQLLog)