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;
44 QueryInfo(const std::string &n, const std::string &s, const std::string &h, unsigned long i, int cat)
52 sourceid = nickid = hostid = -1;
53 date = ServerInstance->Time();
57 void Go(SQLresult* res)
62 if (res->Rows() && sourceid == -1 && !insert)
64 sourceid = atoi(res->GetValue(0,0).d.c_str());
65 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
69 active_queries[req.id] = this;
71 else if (res->Rows() && sourceid == -1 && insert)
73 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
77 active_queries[req.id] = this;
81 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % source);
85 active_queries[req.id] = this;
90 if (res->Rows() && nickid == -1 && !insert)
92 nickid = atoi(res->GetValue(0,0).d.c_str());
93 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
97 active_queries[req.id] = this;
99 else if (res->Rows() && nickid == -1 && insert)
101 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
105 active_queries[req.id] = this;
109 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % nick);
113 active_queries[req.id] = this;
118 if (res->Rows() && hostid == -1 && !insert)
120 hostid = atoi(res->GetValue(0,0).d.c_str());
121 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid,
122 SQLquery("INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES('?','?','?','?','?')") % category % nickid % hostid % sourceid % date);
126 active_queries[req.id] = this;
128 else if (res->Rows() && hostid == -1 && insert)
130 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
134 active_queries[req.id] = this;
138 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_hosts (hostname) VALUES('?')") % hostname);
142 active_queries[req.id] = this;
152 /* $ModDesc: Logs network-wide data to an SQL database */
154 class ModuleSQLLog : public Module
160 ServerInstance->Modules->UseInterface("SQLutils");
161 ServerInstance->Modules->UseInterface("SQL");
163 Module* SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
165 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
167 SQLModule = ServerInstance->Modules->FindFeature("SQL");
171 active_queries.clear();
173 Implementation eventlist[] = { I_OnRehash, I_OnOper, I_OnGlobalOper, I_OnKill,
174 I_OnPreCommand, I_OnUserConnect, I_OnUserQuit, I_OnLoadModule };
175 ServerInstance->Modules->Attach(eventlist, this, 8);
178 virtual ~ModuleSQLLog()
180 ServerInstance->Modules->DoneWithInterface("SQL");
181 ServerInstance->Modules->DoneWithInterface("SQLutils");
188 dbid = Conf.ReadValue("sqllog","dbid",0); // database id of a database configured in sql module
191 virtual void OnRehash(User* user)
196 void OnRequest(Request& request)
198 if(strcmp(SQLRESID, request.id) == 0)
200 SQLresult* res = static_cast<SQLresult*>(&request);
201 std::map<unsigned long, QueryInfo*>::iterator n;
203 n = active_queries.find(res->id);
205 if (n != active_queries.end())
208 active_queries.erase(n);
213 void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
215 // is the sql module loaded? If not, we don't attempt to do anything.
219 SQLrequest req = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
221 QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
223 active_queries[req.id] = i;
226 virtual void OnOper(User* user, const std::string &opertype)
228 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
231 virtual void OnGlobalOper(User* user)
233 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
236 virtual ModResult OnKill(User* source, User* dest, const std::string &reason)
238 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
239 return MOD_RES_PASSTHRU;
242 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
244 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
246 AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+parameters[0],user->server);
248 return MOD_RES_PASSTHRU;
251 virtual void OnUserConnect(User* user)
253 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
256 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
258 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
261 virtual void OnLoadModule(Module* mod)
263 AddLogEntry(LT_LOADMODULE,mod->ModuleSourceFile,ServerInstance->Config->ServerName.c_str(), ServerInstance->Config->ServerName.c_str());
266 virtual Version GetVersion()
268 return Version("Logs network-wide data to an SQL database", VF_VENDOR);
273 MODULE_INIT(ModuleSQLLog)