1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "configreader.h"
22 static Module* SQLModule;
24 static std::string dbid;
26 enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
28 enum QueryState { FIND_SOURCE, INSERT_SOURCE, FIND_NICK, INSERT_NICK, FIND_HOST, INSERT_HOST, INSERT_LOGENTRY, DONE };
32 std::map<unsigned long,QueryInfo*> active_queries;
46 std::string lastquery;
48 QueryInfo(const std::string &n, const std::string &h, unsigned long i, int cat)
55 sourceid = nickid = hostid = -1;
60 void Go(SQLresult* res)
62 // Nothing here and not sent yet
63 SQLrequest req = SQLreq(MyMod, SQLModule, dbid, "", "");
68 // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
69 // If we find it, advance to FIND_NICK state, otherwise go to INSERT_SOURCE
74 sourceid = atoi(res->GetValue(0,0).d.c_str());
77 else qs = INSERT_SOURCE;
81 if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
87 lastquery = "SELECT id,actor FROM ircd_log_actors WHERE actor='?'";
88 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
92 active_queries[req.id] = this;
96 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
102 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
103 // after we've done this, go back to FIND_SOURCE
104 lastquery = "INSERT INTO ircd_log_actors VALUES('','?')";
105 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')", nick);
109 active_queries[req.id] = this;
113 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
118 // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
119 // If we find it, advance to FIND_HOST state, otherwise go to INSERT_NICK
124 nickid = atoi(res->GetValue(0,0).d.c_str());
127 else qs = INSERT_NICK;
131 if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
137 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
141 active_queries[req.id] = this;
145 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
151 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
152 // after we've done this, go back to FIND_NICK
153 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')",nick);
157 active_queries[req.id] = this;
161 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
165 // "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'"
166 // If we find it, advance to INSERT_LOGENTRY state, otherwise go to INSERT_HOST
171 hostid = atoi(res->GetValue(0,0).d.c_str());
172 qs = INSERT_LOGENTRY;
174 else qs = INSERT_HOST;
178 if (lastquery == "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'")
184 lastquery = "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'";
185 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'",hostname);
189 active_queries[req.id] = this;
193 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
199 // "INSERT INTO ircd_log_hosts VALUES('','"+host+"')"
200 // after we've done this, go back to FIND_HOST
201 lastquery = "INSERT INTO ircd_log_hosts VALUES('','?')";
202 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts VALUES('','?')", hostname);
205 qs = INSERT_LOGENTRY;
206 active_queries[req.id] = this;
210 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
213 case INSERT_LOGENTRY:
214 // 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
215 // aaand done! (discard result)
216 if ((nickid == -1) || (hostid == -1) || (sourceid == -1))
223 lastquery = "INSERT INTO ircd_log VALUES()";
224 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log VALUES('',"+ConvToStr(category)+","+ConvToStr(nickid)+","+ConvToStr(hostid)+","+ConvToStr(sourceid)+","+ConvToStr(date)+")");
236 //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
241 active_queries[req.id] = NULL;
247 /* $ModDesc: Logs network-wide data to an SQL database */
249 class ModuleSQLLog : public Module
257 ConfigReader Conf(Srv);
259 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
261 SQLModule = Srv->FindFeature("SQL");
263 ServerInstance->Log(DEFAULT,"WARNING: m_sqllog.so could not initialize because an SQL module is not loaded. Load the module and rehash your server.");
267 ModuleSQLLog(InspIRCd* Me)
268 : Module::Module(Me), Srv(Me)
272 active_queries.clear();
275 void Implements(char* List)
277 List[I_OnRehash] = List[I_OnOper] = List[I_OnGlobalOper] = List[I_OnKill] = 1;
278 List[I_OnPreCommand] = List[I_OnUserConnect] = 1;
279 List[I_OnUserQuit] = List[I_OnLoadModule] = List[I_OnRequest] = 1;
282 virtual void OnRehash(const std::string ¶meter)
287 virtual char* OnRequest(Request* request)
289 ServerInstance->Log(DEBUG,"OnRequest in m_sqllog.so");
290 if(strcmp(SQLRESID, request->GetId()) == 0)
293 std::map<unsigned long, QueryInfo*>::iterator n;
295 res = static_cast<SQLresult*>(request);
296 ServerInstance->Log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetId(), res->id);
298 n = active_queries.find(res->id);
300 if (n != active_queries.end())
302 ServerInstance->Log(DEBUG,"This is an active query");
305 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
306 active_queries.erase(n);
312 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
314 // is the sql module loaded? If not, we don't attempt to do anything.
318 SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
321 QueryInfo* i = new QueryInfo(nick, host, req.id, category);
323 active_queries[req.id] = i;
324 ServerInstance->Log(DEBUG,"Active query id %d",req.id);
328 ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
331 /*long nickid = InsertNick(nick);
332 long sourceid = InsertNick(source);
333 long hostid = InsertHost(host);
334 InsertEntry((unsigned)category,(unsigned)nickid,(unsigned)hostid,(unsigned)sourceid,(unsigned long)time(NULL));*/
337 virtual void OnOper(userrec* user, const std::string &opertype)
339 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
342 virtual void OnGlobalOper(userrec* user)
344 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
347 virtual int OnKill(userrec* source, userrec* dest, const std::string &reason)
349 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
353 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
355 if ((command == "GLINE") || (command == "KLINE") || (command == "ELINE") || (command == "ZLINE"))
357 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
362 virtual void OnUserConnect(userrec* user)
364 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
367 virtual void OnUserQuit(userrec* user, const std::string &reason)
369 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
372 virtual void OnLoadModule(Module* mod, const std::string &name)
374 AddLogEntry(LT_LOADMODULE,name,Srv->Config->ServerName, Srv->Config->ServerName);
377 virtual ~ModuleSQLLog()
381 virtual Version GetVersion()
383 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
388 class ModuleSQLLogFactory : public ModuleFactory
391 ModuleSQLLogFactory()
395 ~ModuleSQLLogFactory()
399 virtual Module * CreateModule(InspIRCd* Me)
401 return new ModuleSQLLog(Me);
407 extern "C" void * init_module( void )
409 return new ModuleSQLLogFactory;