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, FIND_NICK, FIND_HOST, DONE};
32 std::map<unsigned long,QueryInfo*> active_queries;
49 QueryInfo(const std::string &n, const std::string &s, const std::string &h, unsigned long i, int cat)
57 sourceid = nickid = hostid = -1;
62 void Go(SQLresult* res)
64 SQLrequest req = SQLreq(MyMod, SQLModule, dbid, "", "");
68 if (res->Rows() && sourceid == -1 && !insert)
70 sourceid = atoi(res->GetValue(0,0).d.c_str());
71 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
76 active_queries[req.id] = this;
79 else if (res->Rows() && sourceid == -1 && insert)
81 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", source);
86 active_queries[req.id] = this;
91 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors (actor) VALUES('?')", source);
96 active_queries[req.id] = this;
102 if (res->Rows() && nickid == -1 && !insert)
104 nickid = atoi(res->GetValue(0,0).d.c_str());
105 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'", hostname);
110 active_queries[req.id] = this;
113 else if (res->Rows() && nickid == -1 && insert)
115 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
120 active_queries[req.id] = this;
125 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors (actor) VALUES('?')",nick);
130 active_queries[req.id] = this;
136 if (res->Rows() && hostid == -1 && !insert)
138 hostid = atoi(res->GetValue(0,0).d.c_str());
139 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES("+ConvToStr(category)+","+ConvToStr(nickid)+","+ConvToStr(hostid)+","+ConvToStr(sourceid)+","+ConvToStr(date)+")");
144 active_queries[req.id] = this;
147 else if (res->Rows() && hostid == -1 && insert)
149 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'", hostname);
154 active_queries[req.id] = this;
159 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts (hostname) VALUES('?')", hostname);
164 active_queries[req.id] = this;
170 delete active_queries[req.id];
171 active_queries[req.id] = NULL;
177 /* $ModDesc: Logs network-wide data to an SQL database */
179 class ModuleSQLLog : public Module
185 ModuleSQLLog(InspIRCd* Me)
186 : Module::Module(Me), Srv(Me)
188 ServerInstance->UseInterface("SQLutils");
189 ServerInstance->UseInterface("SQL");
191 Module* SQLutils = ServerInstance->FindModule("m_sqlutils.so");
193 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
195 SQLModule = Srv->FindFeature("SQL");
199 active_queries.clear();
202 virtual ~ModuleSQLLog()
204 ServerInstance->DoneWithInterface("SQL");
205 ServerInstance->DoneWithInterface("SQLutils");
208 void Implements(char* List)
210 List[I_OnRehash] = List[I_OnOper] = List[I_OnGlobalOper] = List[I_OnKill] = 1;
211 List[I_OnPreCommand] = List[I_OnUserConnect] = 1;
212 List[I_OnUserQuit] = List[I_OnLoadModule] = List[I_OnRequest] = 1;
217 ConfigReader Conf(Srv);
218 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
221 virtual void OnRehash(userrec* user, const std::string ¶meter)
226 virtual char* OnRequest(Request* request)
228 if(strcmp(SQLRESID, request->GetId()) == 0)
231 std::map<unsigned long, QueryInfo*>::iterator n;
233 res = static_cast<SQLresult*>(request);
234 ServerInstance->Log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetId(), res->id);
236 n = active_queries.find(res->id);
238 if (n != active_queries.end())
240 ServerInstance->Log(DEBUG,"This is an active query");
243 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
244 active_queries.erase(n);
250 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
255 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
257 // is the sql module loaded? If not, we don't attempt to do anything.
261 SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", source);
264 QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
266 active_queries[req.id] = i;
267 ServerInstance->Log(DEBUG,"Active query id %d",req.id);
271 ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
275 virtual void OnOper(userrec* user, const std::string &opertype)
277 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
280 virtual void OnGlobalOper(userrec* user)
282 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
285 virtual int OnKill(userrec* source, userrec* dest, const std::string &reason)
287 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
291 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
293 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
295 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
300 virtual void OnUserConnect(userrec* user)
302 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
305 virtual void OnUserQuit(userrec* user, const std::string &reason)
307 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
310 virtual void OnLoadModule(Module* mod, const std::string &name)
312 AddLogEntry(LT_LOADMODULE,name,Srv->Config->ServerName, Srv->Config->ServerName);
315 virtual Version GetVersion()
317 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
322 class ModuleSQLLogFactory : public ModuleFactory
325 ModuleSQLLogFactory()
329 ~ModuleSQLLogFactory()
333 virtual Module * CreateModule(InspIRCd* Me)
335 return new ModuleSQLLog(Me);
341 extern "C" void * init_module( void )
343 return new ModuleSQLLogFactory;