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, "", "");
72 // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
73 // If we find it, advance to FIND_NICK state, otherwise go to INSERT_SOURCE
78 sourceid = atoi(res->GetValue(0,0).d.c_str());
81 else qs = INSERT_SOURCE;
85 if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
91 lastquery = "SELECT id,actor FROM ircd_log_actors WHERE actor='?'";
92 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
96 active_queries[req.id] = this;
100 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
106 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
107 // after we've done this, go back to FIND_SOURCE
108 lastquery = "INSERT INTO ircd_log_actors VALUES('','?')";
109 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')", nick);
113 active_queries[req.id] = this;
117 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
122 // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
123 // If we find it, advance to FIND_HOST state, otherwise go to INSERT_NICK
128 nickid = atoi(res->GetValue(0,0).d.c_str());
131 else qs = INSERT_NICK;
135 if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
141 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
145 active_queries[req.id] = this;
149 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
155 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
156 // after we've done this, go back to FIND_NICK
157 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')",nick);
161 active_queries[req.id] = this;
165 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
169 // "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'"
170 // If we find it, advance to INSERT_LOGENTRY state, otherwise go to INSERT_HOST
175 hostid = atoi(res->GetValue(0,0).d.c_str());
176 qs = INSERT_LOGENTRY;
178 else qs = INSERT_HOST;
182 if (lastquery == "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'")
188 lastquery = "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'";
189 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'",hostname);
193 active_queries[req.id] = this;
197 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
203 // "INSERT INTO ircd_log_hosts VALUES('','"+host+"')"
204 // after we've done this, go back to FIND_HOST
205 lastquery = "INSERT INTO ircd_log_hosts VALUES('','?')";
206 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts VALUES('','?')", hostname);
209 qs = INSERT_LOGENTRY;
210 active_queries[req.id] = this;
214 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
217 case INSERT_LOGENTRY:
218 // 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
219 // aaand done! (discard result)
220 if ((nickid == -1) || (hostid == -1) || (sourceid == -1))
227 lastquery = "INSERT INTO ircd_log VALUES()";
228 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log VALUES('',"+ConvToStr(category)+","+ConvToStr(nickid)+","+ConvToStr(hostid)+","+ConvToStr(sourceid)+","+ConvToStr(date)+")");
240 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
245 active_queries[req.id] = NULL;
251 /* $ModDesc: Logs network-wide data to an SQL database */
253 class ModuleSQLLog : public Module
261 ConfigReader Conf(Srv);
263 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
265 SQLModule = Srv->FindFeature("SQL");
267 ServerInstance->Log(DEFAULT,"WARNING: m_sqllog.so could not initialize because an SQL module is not loaded. Load the module and rehash your server.");
271 ModuleSQLLog(InspIRCd* Me)
272 : Module::Module(Me), Srv(Me)
276 active_queries.clear();
279 void Implements(char* List)
281 List[I_OnRehash] = List[I_OnOper] = List[I_OnGlobalOper] = List[I_OnKill] = 1;
282 List[I_OnPreCommand] = List[I_OnUserConnect] = List[I_OnGlobalConnect] = 1;
283 List[I_OnUserQuit] = List[I_OnLoadModule] = List[I_OnRequest] = 1;
286 virtual void OnRehash(const std::string ¶meter)
291 virtual char* OnRequest(Request* request)
293 ServerInstance->Log(DEBUG,"OnRequest in m_sqllog.so");
294 if(strcmp(SQLRESID, request->GetId()) == 0)
297 std::map<unsigned long, QueryInfo*>::iterator n;
299 res = static_cast<SQLresult*>(request);
300 ServerInstance->Log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetId(), res->id);
302 n = active_queries.find(res->id);
304 if (n != active_queries.end())
306 ServerInstance->Log(DEBUG,"This is an active query");
309 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
310 active_queries.erase(n);
316 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
318 // is the sql module loaded? If not, we don't attempt to do anything.
322 SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
325 QueryInfo* i = new QueryInfo(nick, host, req.id, category);
327 active_queries[req.id] = i;
328 ServerInstance->Log(DEBUG,"Active query id %d",req.id);
332 ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
335 /*long nickid = InsertNick(nick);
336 long sourceid = InsertNick(source);
337 long hostid = InsertHost(host);
338 InsertEntry((unsigned)category,(unsigned)nickid,(unsigned)hostid,(unsigned)sourceid,(unsigned long)time(NULL));*/
341 virtual void OnOper(userrec* user, const std::string &opertype)
343 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
346 virtual void OnGlobalOper(userrec* user)
348 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
351 virtual int OnKill(userrec* source, userrec* dest, const std::string &reason)
353 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
357 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
359 if ((command == "GLINE") || (command == "KLINE") || (command == "ELINE") || (command == "ZLINE"))
361 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
366 virtual void OnUserConnect(userrec* user)
368 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
371 virtual void OnGlobalConnect(userrec* user)
373 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
376 virtual void OnUserQuit(userrec* user, const std::string &reason)
378 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
381 virtual void OnLoadModule(Module* mod, const std::string &name)
383 AddLogEntry(LT_LOADMODULE,name,Srv->Config->ServerName, Srv->Config->ServerName);
386 virtual ~ModuleSQLLog()
390 virtual Version GetVersion()
392 return Version(1,0,0,1,VF_VENDOR);
397 class ModuleSQLLogFactory : public ModuleFactory
400 ModuleSQLLogFactory()
404 ~ModuleSQLLogFactory()
408 virtual Module * CreateModule(InspIRCd* Me)
410 return new ModuleSQLLog(Me);
416 extern "C" void * init_module( void )
418 return new ModuleSQLLogFactory;