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 * ---------------------------------------------------
18 #include "configreader.h"
21 static Module* SQLModule;
23 static std::string dbid;
25 enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
27 enum QueryState { FIND_SOURCE, FIND_NICK, FIND_HOST, DONE};
31 std::map<unsigned long,QueryInfo*> active_queries;
48 QueryInfo(const std::string &n, const std::string &s, const std::string &h, unsigned long i, int cat)
56 sourceid = nickid = hostid = -1;
61 void Go(SQLresult* res)
63 SQLrequest req = SQLreq(MyMod, SQLModule, dbid, "", "");
67 if (res->Rows() && sourceid == -1 && !insert)
69 sourceid = atoi(res->GetValue(0,0).d.c_str());
70 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
75 active_queries[req.id] = this;
78 else if (res->Rows() && sourceid == -1 && insert)
80 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", source);
85 active_queries[req.id] = this;
90 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors (actor) VALUES('?')", source);
95 active_queries[req.id] = this;
101 if (res->Rows() && nickid == -1 && !insert)
103 nickid = atoi(res->GetValue(0,0).d.c_str());
104 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'", hostname);
109 active_queries[req.id] = this;
112 else if (res->Rows() && nickid == -1 && insert)
114 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
119 active_queries[req.id] = this;
124 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors (actor) VALUES('?')",nick);
129 active_queries[req.id] = this;
135 if (res->Rows() && hostid == -1 && !insert)
137 hostid = atoi(res->GetValue(0,0).d.c_str());
138 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)+")");
143 active_queries[req.id] = this;
146 else if (res->Rows() && hostid == -1 && insert)
148 req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'", hostname);
153 active_queries[req.id] = this;
158 req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts (hostname) VALUES('?')", hostname);
163 active_queries[req.id] = this;
169 delete active_queries[req.id];
170 active_queries[req.id] = NULL;
176 /* $ModDesc: Logs network-wide data to an SQL database */
178 class ModuleSQLLog : public Module
183 ModuleSQLLog(InspIRCd* Me)
186 ServerInstance->UseInterface("SQLutils");
187 ServerInstance->UseInterface("SQL");
189 Module* SQLutils = ServerInstance->FindModule("m_sqlutils.so");
191 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
193 SQLModule = ServerInstance->FindFeature("SQL");
197 active_queries.clear();
200 virtual ~ModuleSQLLog()
202 ServerInstance->DoneWithInterface("SQL");
203 ServerInstance->DoneWithInterface("SQLutils");
206 void Implements(char* List)
208 List[I_OnRehash] = List[I_OnOper] = List[I_OnGlobalOper] = List[I_OnKill] = 1;
209 List[I_OnPreCommand] = List[I_OnUserConnect] = 1;
210 List[I_OnUserQuit] = List[I_OnLoadModule] = List[I_OnRequest] = 1;
215 ConfigReader Conf(ServerInstance);
216 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
219 virtual void OnRehash(userrec* user, const std::string ¶meter)
224 virtual char* OnRequest(Request* request)
226 if(strcmp(SQLRESID, request->GetId()) == 0)
229 std::map<unsigned long, QueryInfo*>::iterator n;
231 res = static_cast<SQLresult*>(request);
232 n = active_queries.find(res->id);
234 if (n != active_queries.end())
237 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
238 active_queries.erase(n);
247 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
249 // is the sql module loaded? If not, we don't attempt to do anything.
253 SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", source);
256 QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
258 active_queries[req.id] = i;
262 virtual void OnOper(userrec* user, const std::string &opertype)
264 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
267 virtual void OnGlobalOper(userrec* user)
269 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
272 virtual int OnKill(userrec* source, userrec* dest, const std::string &reason)
274 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
278 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
280 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
282 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
287 virtual void OnUserConnect(userrec* user)
289 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
292 virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
294 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
297 virtual void OnLoadModule(Module* mod, const std::string &name)
299 AddLogEntry(LT_LOADMODULE,name,ServerInstance->Config->ServerName, ServerInstance->Config->ServerName);
302 virtual Version GetVersion()
304 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
309 class ModuleSQLLogFactory : public ModuleFactory
312 ModuleSQLLogFactory()
316 ~ModuleSQLLogFactory()
320 virtual Module * CreateModule(InspIRCd* Me)
322 return new ModuleSQLLog(Me);
328 extern "C" void * init_module( void )
330 return new ModuleSQLLogFactory;