1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 static Module* SQLModule;
19 static std::string dbid;
21 enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
23 enum QueryState { FIND_SOURCE, FIND_NICK, FIND_HOST, DONE};
27 std::map<unsigned long,QueryInfo*> active_queries;
32 InspIRCd* ServerInstance;
46 QueryInfo(InspIRCd* Instance, const std::string &n, const std::string &s, const std::string &h, unsigned long i, int cat)
48 ServerInstance = Instance;
55 sourceid = nickid = hostid = -1;
56 date = ServerInstance->Time();
60 void Go(SQLresult* res)
62 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery(""));
66 if (res->Rows() && sourceid == -1 && !insert)
68 sourceid = atoi(res->GetValue(0,0).d.c_str());
69 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
74 active_queries[req.id] = this;
77 else if (res->Rows() && sourceid == -1 && insert)
79 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
84 active_queries[req.id] = this;
89 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % source);
94 active_queries[req.id] = this;
100 if (res->Rows() && nickid == -1 && !insert)
102 nickid = atoi(res->GetValue(0,0).d.c_str());
103 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
108 active_queries[req.id] = this;
111 else if (res->Rows() && nickid == -1 && insert)
113 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
118 active_queries[req.id] = this;
123 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % nick);
128 active_queries[req.id] = this;
134 if (res->Rows() && hostid == -1 && !insert)
136 hostid = atoi(res->GetValue(0,0).d.c_str());
137 req = SQLrequest(MyMod, SQLModule, dbid,
138 SQLquery("INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES('?','?','?','?','?')") % category % nickid % hostid % sourceid % date);
143 active_queries[req.id] = this;
146 else if (res->Rows() && hostid == -1 && insert)
148 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
153 active_queries[req.id] = this;
158 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_hosts (hostname) VALUES('?')") % hostname);
163 active_queries[req.id] = this;
169 std::map<unsigned long,QueryInfo*>::iterator x = active_queries.find(req.id);
170 if (x != active_queries.end())
173 active_queries.erase(x);
180 /* $ModDesc: Logs network-wide data to an SQL database */
182 class ModuleSQLLog : public Module
186 ModuleSQLLog(InspIRCd* Me)
189 ServerInstance->Modules->UseInterface("SQLutils");
190 ServerInstance->Modules->UseInterface("SQL");
192 Module* SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
194 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
196 SQLModule = ServerInstance->Modules->FindFeature("SQL");
200 active_queries.clear();
202 Implementation eventlist[] = { I_OnRehash, I_OnOper, I_OnGlobalOper, I_OnKill,
203 I_OnPreCommand, I_OnUserConnect, I_OnUserQuit, I_OnLoadModule, I_OnRequest };
204 ServerInstance->Modules->Attach(eventlist, this, 9);
207 virtual ~ModuleSQLLog()
209 ServerInstance->Modules->DoneWithInterface("SQL");
210 ServerInstance->Modules->DoneWithInterface("SQLutils");
216 ConfigReader Conf(ServerInstance);
217 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
220 virtual void OnRehash(User* user, const std::string ¶meter)
225 virtual const char* OnRequest(Request* request)
227 if(strcmp(SQLRESID, request->GetId()) == 0)
230 std::map<unsigned long, QueryInfo*>::iterator n;
232 res = static_cast<SQLresult*>(request);
233 n = active_queries.find(res->id);
235 if (n != active_queries.end())
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 = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
256 QueryInfo* i = new QueryInfo(ServerInstance, nick, source, host, req.id, category);
258 active_queries[req.id] = i;
262 virtual void OnOper(User* user, const std::string &opertype)
264 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
267 virtual void OnGlobalOper(User* user)
269 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
272 virtual int OnKill(User* source, User* dest, const std::string &reason)
274 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
278 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *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(":")+parameters[0],user->server);
287 virtual void OnUserConnect(User* user)
289 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
292 virtual void OnUserQuit(User* 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("$Id$", VF_VENDOR, API_VERSION);
309 MODULE_INIT(ModuleSQLLog)