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 * ---------------------------------------------------
23 #include <sys/types.h>
24 #include <sys/socket.h>
38 static Module* SQLModule;
40 static std::string dbid;
43 enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
45 enum QueryState { FIND_SOURCE, INSERT_SOURCE, FIND_NICK, INSERT_NICK, FIND_HOST, INSERT_HOST, INSERT_LOGENTRY, DONE };
49 std::map<unsigned long,QueryInfo*> active_queries;
64 QueryInfo(const std::string &n, const std::string &h, unsigned long i, int cat)
71 sourceid = nickid = hostid = -1;
75 void Go(SQLresult* res)
77 // Nothing here and not sent yet
78 SQLrequest req = SQLreq(MyMod, SQLModule, dbid, "", "");
80 log(DEBUG,"State: %d",qs);
85 // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
86 // If we find it, advance to FIND_NICK state, otherwise go to INSERT_SOURCE
93 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
97 active_queries[req.id] = this;
101 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
107 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
108 // after we've done this, go back to FIND_SOURCE
109 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')", nick);
113 active_queries[req.id] = this;
117 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
130 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
134 active_queries[req.id] = this;
138 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
143 // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
144 // after we've done this, go back to FIND_NICK
145 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')",nick);
149 active_queries[req.id] = this;
153 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
157 // "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'"
158 // If we find it, advance to INSERT_LOGENTRY state, otherwise go to INSERT_HOST
165 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'",hostname);
169 active_queries[req.id] = this;
173 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
178 // "INSERT INTO ircd_log_hosts VALUES('','"+host+"')"
179 // after we've done this, go back to FIND_HOST
180 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts VALUES('','?')", hostname);
183 qs = INSERT_LOGENTRY;
184 active_queries[req.id] = this;
188 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
191 case INSERT_LOGENTRY:
192 // 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
193 // aaand done! (discard result)
194 if ((nickid == -1) || (hostid == -1) || (sourceid == -1))
201 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log VALUES('',"+ConvToStr(category)+","+ConvToStr(nickid)+","+ConvToStr(hostid)+","+ConvToStr(sourceid)+","+ConvToStr(date)+")");
213 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
218 active_queries[req.id] = NULL;
224 /* $ModDesc: Logs network-wide data to an SQL database */
226 class ModuleSQLLog : public Module
234 Conf = new ConfigReader();
235 dbid = Conf->ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
237 SQLModule = Srv->FindFeature("SQL");
239 Srv->Log(DEFAULT,"WARNING: m_sqllog.so could not initialize because an SQL module is not loaded. Load the module and rehash your server.");
243 ModuleSQLLog(Server* Me) : Module::Module(Me)
248 active_queries.clear();
251 void Implements(char* List)
253 List[I_OnRehash] = List[I_OnOper] = List[I_OnGlobalOper] = List[I_OnKill] = 1;
254 List[I_OnPreCommand] = List[I_OnUserConnect] = List[I_OnGlobalConnect] = 1;
255 List[I_OnUserQuit] = List[I_OnLoadModule] = List[I_OnRequest] = 1;
258 virtual void OnRehash(const std::string ¶meter)
263 virtual char* OnRequest(Request* request)
265 if(strcmp(SQLRESID, request->GetData()) == 0)
268 std::map<unsigned long, QueryInfo*>::iterator n;
270 res = static_cast<SQLresult*>(request);
271 log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetData(), res->id);
273 n = active_queries.find(res->id);
275 if (n != active_queries.end())
277 log(DEBUG,"This is an active query");
280 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
281 active_queries.erase(n);
287 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
289 // is the sql module loaded? If not, we don't attempt to do anything.
293 SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
296 QueryInfo* i = new QueryInfo(nick, host, req.id, category);
298 active_queries[req.id] = i;
302 log(DEBUG, "SQLrequest failed: %s", req.error.Str());
305 /*long nickid = InsertNick(nick);
306 long sourceid = InsertNick(source);
307 long hostid = InsertHost(host);
308 InsertEntry((unsigned)category,(unsigned)nickid,(unsigned)hostid,(unsigned)sourceid,(unsigned long)time(NULL));*/
311 virtual void OnOper(userrec* user, const std::string &opertype)
313 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
316 virtual void OnGlobalOper(userrec* user)
318 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
321 virtual int OnKill(userrec* source, userrec* dest, const std::string &reason)
323 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
327 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
329 if ((command == "GLINE") || (command == "KLINE") || (command == "ELINE") || (command == "ZLINE"))
331 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
336 virtual void OnUserConnect(userrec* user)
338 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
341 virtual void OnGlobalConnect(userrec* user)
343 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
346 virtual void OnUserQuit(userrec* user, const std::string &reason)
348 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
351 virtual void OnLoadModule(Module* mod, const std::string &name)
353 AddLogEntry(LT_LOADMODULE,name,Srv->GetServerName(),Srv->GetServerName());
356 virtual ~ModuleSQLLog()
360 virtual Version GetVersion()
362 return Version(1,0,0,1,VF_VENDOR);
367 class ModuleSQLLogFactory : public ModuleFactory
370 ModuleSQLLogFactory()
374 ~ModuleSQLLogFactory()
378 virtual Module * CreateModule(Server* Me)
380 return new ModuleSQLLog(Me);
386 extern "C" void * init_module( void )
388 return new ModuleSQLLogFactory;