1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 = SQLrequest(MyMod, SQLModule, dbid, SQLquery(""));
67 if (res->Rows() && sourceid == -1 && !insert)
69 sourceid = atoi(res->GetValue(0,0).d.c_str());
70 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("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 = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
85 active_queries[req.id] = this;
90 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("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 = SQLrequest(MyMod, SQLModule, dbid, SQLquery("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 = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
119 active_queries[req.id] = this;
124 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("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 = SQLrequest(MyMod, SQLModule, dbid,
139 SQLquery("INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES('?','?','?','?','?')") % category % nickid % hostid % sourceid % date);
144 active_queries[req.id] = this;
147 else if (res->Rows() && hostid == -1 && insert)
149 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
154 active_queries[req.id] = this;
159 req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_hosts (hostname) VALUES('?')") % hostname);
164 active_queries[req.id] = this;
170 std::map<unsigned long,QueryInfo*>::iterator x = active_queries.find(req.id);
171 if (x != active_queries.end())
174 active_queries.erase(x);
181 /* $ModDesc: Logs network-wide data to an SQL database */
183 class ModuleSQLLog : public Module
188 ModuleSQLLog(InspIRCd* Me)
191 ServerInstance->Modules->UseInterface("SQLutils");
192 ServerInstance->Modules->UseInterface("SQL");
194 Module* SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
196 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
198 SQLModule = ServerInstance->Modules->FindFeature("SQL");
202 active_queries.clear();
204 Implementation eventlist[] = { I_OnRehash, I_OnOper, I_OnGlobalOper, I_OnKill,
205 I_OnPreCommand, I_OnUserConnect, I_OnUserQuit, I_OnLoadModule, I_OnRequest };
206 ServerInstance->Modules->Attach(eventlist, this, 9);
209 virtual ~ModuleSQLLog()
211 ServerInstance->Modules->DoneWithInterface("SQL");
212 ServerInstance->Modules->DoneWithInterface("SQLutils");
218 ConfigReader Conf(ServerInstance);
219 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
222 virtual void OnRehash(User* user, const std::string ¶meter)
227 virtual char* OnRequest(Request* request)
229 if(strcmp(SQLRESID, request->GetId()) == 0)
232 std::map<unsigned long, QueryInfo*>::iterator n;
234 res = static_cast<SQLresult*>(request);
235 n = active_queries.find(res->id);
237 if (n != active_queries.end())
240 std::map<unsigned long, QueryInfo*>::iterator n = active_queries.find(res->id);
241 active_queries.erase(n);
250 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
252 // is the sql module loaded? If not, we don't attempt to do anything.
256 SQLrequest req = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
259 QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
261 active_queries[req.id] = i;
265 virtual void OnOper(User* user, const std::string &opertype)
267 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
270 virtual void OnGlobalOper(User* user)
272 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
275 virtual int OnKill(User* source, User* dest, const std::string &reason)
277 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
281 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
283 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
285 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
290 virtual void OnUserConnect(User* user)
292 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
295 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
297 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
300 virtual void OnLoadModule(Module* mod, const std::string &name)
302 AddLogEntry(LT_LOADMODULE,name,ServerInstance->Config->ServerName, ServerInstance->Config->ServerName);
305 virtual Version GetVersion()
307 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
312 MODULE_INIT(ModuleSQLLog)