1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
20 #include "configreader.h"
25 static Module* SQLModule;
27 static std::string dbid;
30 enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
32 enum QueryState { FIND_SOURCE, INSERT_SOURCE, FIND_NICK, INSERT_NICK, FIND_HOST, INSERT_HOST, INSERT_LOGENTRY, DONE };
36 std::map<unsigned long,QueryInfo*> active_queries;
50 std::string lastquery;
52 QueryInfo(const std::string &n, const std::string &h, unsigned long i, int cat)
59 sourceid = nickid = hostid = -1;
64 void Go(SQLresult* res)
66 // Nothing here and not sent yet
67 SQLrequest req = SQLreq(MyMod, SQLModule, dbid, "", "");
69 log(DEBUG,"State: %d",qs);
74 // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
75 // If we find it, advance to FIND_NICK state, otherwise go to INSERT_SOURCE
80 sourceid = atoi(res->GetValue(0,0).d.c_str());
83 else qs = INSERT_SOURCE;
87 if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
93 lastquery = "SELECT id,actor FROM ircd_log_actors WHERE actor='?'";
94 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
98 active_queries[req.id] = this;
102 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
108 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
109 // after we've done this, go back to FIND_SOURCE
110 lastquery = "INSERT INTO ircd_log_actors VALUES('','?')";
111 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')", nick);
115 active_queries[req.id] = this;
119 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
124 // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
125 // If we find it, advance to FIND_HOST state, otherwise go to INSERT_NICK
130 nickid = atoi(res->GetValue(0,0).d.c_str());
133 else qs = INSERT_NICK;
137 if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
143 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
147 active_queries[req.id] = this;
151 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
157 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
158 // after we've done this, go back to FIND_NICK
159 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')",nick);
163 active_queries[req.id] = this;
167 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
171 // "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'"
172 // If we find it, advance to INSERT_LOGENTRY state, otherwise go to INSERT_HOST
177 hostid = atoi(res->GetValue(0,0).d.c_str());
178 qs = INSERT_LOGENTRY;
180 else qs = INSERT_HOST;
184 if (lastquery == "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'")
190 lastquery = "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'";
191 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'",hostname);
195 active_queries[req.id] = this;
199 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
205 // "INSERT INTO ircd_log_hosts VALUES('','"+host+"')"
206 // after we've done this, go back to FIND_HOST
207 lastquery = "INSERT INTO ircd_log_hosts VALUES('','?')";
208 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts VALUES('','?')", hostname);
211 qs = INSERT_LOGENTRY;
212 active_queries[req.id] = this;
216 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
219 case INSERT_LOGENTRY:
220 // INSERT INTO ircd_log VALUES('',%lu,%lu,%lu,%lu,%lu)",(unsigned long)category,(unsigned long)nickid,(unsigned long)hostid,(unsigned long)sourceid,(unsigned long)date
221 // aaand done! (discard result)
222 if ((nickid == -1) || (hostid == -1) || (sourceid == -1))
229 lastquery = "INSERT INTO ircd_log VALUES()";
230 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log VALUES('',"+ConvToStr(category)+","+ConvToStr(nickid)+","+ConvToStr(hostid)+","+ConvToStr(sourceid)+","+ConvToStr(date)+")");
242 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
247 active_queries[req.id] = NULL;
253 /* $ModDesc: Logs network-wide data to an SQL database */
255 class ModuleSQLLog : public Module
263 ConfigReader Conf(Srv);
265 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
267 SQLModule = Srv->FindFeature("SQL");
269 log(DEFAULT,"WARNING: m_sqllog.so could not initialize because an SQL module is not loaded. Load the module and rehash your server.");
273 ModuleSQLLog(InspIRCd* Me)
274 : Module::Module(Me), Srv(Me)
278 active_queries.clear();
281 void Implements(char* List)
283 List[I_OnRehash] = List[I_OnOper] = List[I_OnGlobalOper] = List[I_OnKill] = 1;
284 List[I_OnPreCommand] = List[I_OnUserConnect] = List[I_OnGlobalConnect] = 1;
285 List[I_OnUserQuit] = List[I_OnLoadModule] = List[I_OnRequest] = 1;
288 virtual void OnRehash(const std::string ¶meter)
293 virtual char* OnRequest(Request* request)
295 log(DEBUG,"OnRequest in m_sqllog.so");
296 if(strcmp(SQLRESID, request->GetId()) == 0)
299 std::map<unsigned long, QueryInfo*>::iterator n;
301 res = static_cast<SQLresult*>(request);
302 log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetId(), res->id);
304 n = active_queries.find(res->id);
306 if (n != active_queries.end())
308 log(DEBUG,"This is an active query");
311 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
312 active_queries.erase(n);
318 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
320 // is the sql module loaded? If not, we don't attempt to do anything.
324 SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
327 QueryInfo* i = new QueryInfo(nick, host, req.id, category);
329 active_queries[req.id] = i;
330 log(DEBUG,"Active query id %d",req.id);
334 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
337 /*long nickid = InsertNick(nick);
338 long sourceid = InsertNick(source);
339 long hostid = InsertHost(host);
340 InsertEntry((unsigned)category,(unsigned)nickid,(unsigned)hostid,(unsigned)sourceid,(unsigned long)time(NULL));*/
343 virtual void OnOper(userrec* user, const std::string &opertype)
345 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
348 virtual void OnGlobalOper(userrec* user)
350 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
353 virtual int OnKill(userrec* source, userrec* dest, const std::string &reason)
355 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
359 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
361 if ((command == "GLINE") || (command == "KLINE") || (command == "ELINE") || (command == "ZLINE"))
363 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
368 virtual void OnUserConnect(userrec* user)
370 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
373 virtual void OnGlobalConnect(userrec* user)
375 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
378 virtual void OnUserQuit(userrec* user, const std::string &reason)
380 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
383 virtual void OnLoadModule(Module* mod, const std::string &name)
385 AddLogEntry(LT_LOADMODULE,name,Srv->Config->ServerName, Srv->Config->ServerName);
388 virtual ~ModuleSQLLog()
392 virtual Version GetVersion()
394 return Version(1,0,0,1,VF_VENDOR);
399 class ModuleSQLLogFactory : public ModuleFactory
402 ModuleSQLLogFactory()
406 ~ModuleSQLLogFactory()
410 virtual Module * CreateModule(InspIRCd* Me)
412 return new ModuleSQLLog(Me);
418 extern "C" void * init_module( void )
420 return new ModuleSQLLogFactory;