]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqllog.cpp
a54f476c479ce6b5425a8f2dcb73a40e5ab73c86
[user/henk/code/inspircd.git] / src / modules / m_sqllog.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "m_sqlv2.h"
16
17 static Module* SQLModule;
18 static Module* MyMod;
19 static std::string dbid;
20
21 enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
22
23 enum QueryState { FIND_SOURCE, FIND_NICK, FIND_HOST, DONE};
24
25 class QueryInfo;
26
27 std::map<unsigned long,QueryInfo*> active_queries;
28
29 class QueryInfo
30 {
31 public:
32         QueryState qs;
33         unsigned long id;
34         std::string nick;
35         std::string source;
36         std::string hostname;
37         int sourceid;
38         int nickid;
39         int hostid;
40         int category;
41         time_t date;
42         bool insert;
43
44         QueryInfo(const std::string &n, const std::string &s, const std::string &h, unsigned long i, int cat)
45         {
46                 qs = FIND_SOURCE;
47                 nick = n;
48                 source = s;
49                 hostname = h;
50                 id = i;
51                 category = cat;
52                 sourceid = nickid = hostid = -1;
53                 date = ServerInstance->Time();
54                 insert = false;
55         }
56
57         void Go(SQLresult* res)
58         {
59                 switch (qs)
60                 {
61                         case FIND_SOURCE:
62                                 if (res->Rows() && sourceid == -1 && !insert)
63                                 {
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);
66                                         req.Send();
67                                         insert = false;
68                                         qs = FIND_NICK;
69                                         active_queries[req.id] = this;
70                                 }
71                                 else if (res->Rows() && sourceid == -1 && insert)
72                                 {
73                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
74                                         req.Send();
75                                         insert = false;
76                                         qs = FIND_SOURCE;
77                                         active_queries[req.id] = this;
78                                 }
79                                 else
80                                 {
81                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % source);
82                                         req.Send();
83                                         insert = true;
84                                         qs = FIND_SOURCE;
85                                         active_queries[req.id] = this;
86                                 }
87                         break;
88
89                         case FIND_NICK:
90                                 if (res->Rows() && nickid == -1 && !insert)
91                                 {
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);
94                                         req.Send();
95                                         insert = false;
96                                         qs = FIND_HOST;
97                                         active_queries[req.id] = this;
98                                 }
99                                 else if (res->Rows() && nickid == -1 && insert)
100                                 {
101                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
102                                         req.Send();
103                                         insert = false;
104                                         qs = FIND_NICK;
105                                         active_queries[req.id] = this;
106                                 }
107                                 else
108                                 {
109                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % nick);
110                                         req.Send();
111                                         insert = true;
112                                         qs = FIND_NICK;
113                                         active_queries[req.id] = this;
114                                 }
115                         break;
116
117                         case FIND_HOST:
118                                 if (res->Rows() && hostid == -1 && !insert)
119                                 {
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);
123                                         req.Send();
124                                         insert = true;
125                                         qs = DONE;
126                                         active_queries[req.id] = this;
127                                 }
128                                 else if (res->Rows() && hostid == -1 && insert)
129                                 {
130                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
131                                         req.Send();
132                                         insert = false;
133                                         qs = FIND_HOST;
134                                         active_queries[req.id] = this;
135                                 }
136                                 else
137                                 {
138                                         SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_hosts (hostname) VALUES('?')") % hostname);
139                                         req.Send();
140                                         insert = true;
141                                         qs = FIND_HOST;
142                                         active_queries[req.id] = this;
143                                 }
144                         break;
145
146                         case DONE:
147                         break;
148                 }
149         }
150 };
151
152 /* $ModDesc: Logs network-wide data to an SQL database */
153
154 class ModuleSQLLog : public Module
155 {
156
157  public:
158         ModuleSQLLog()
159         {
160                 Module* SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
161                 if (!SQLutils)
162                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
163
164                 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_DATA, "SQL");
165                 if (!prov)
166                         throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
167                 SQLModule = prov->creator;
168
169                 OnRehash(NULL);
170                 MyMod = this;
171                 active_queries.clear();
172
173                 Implementation eventlist[] = { I_OnRehash, I_OnOper, I_OnGlobalOper, I_OnKill,
174                         I_OnPreCommand, I_OnUserRegister, I_OnUserQuit, I_OnLoadModule };
175                 ServerInstance->Modules->Attach(eventlist, this, 9);
176         }
177
178         virtual ~ModuleSQLLog()
179         {
180         }
181
182         void ReadConfig()
183         {
184                 ConfigReader Conf;
185                 dbid = Conf.ReadValue("sqllog","dbid",0);       // database id of a database configured in sql module
186         }
187
188         virtual void OnRehash(User* user)
189         {
190                 ReadConfig();
191         }
192
193         void OnRequest(Request& request)
194         {
195                 if(strcmp(SQLRESID, request.id) == 0)
196                 {
197                         SQLresult* res = static_cast<SQLresult*>(&request);
198                         std::map<unsigned long, QueryInfo*>::iterator n;
199
200                         n = active_queries.find(res->id);
201
202                         if (n != active_queries.end())
203                         {
204                                 n->second->Go(res);
205                                 active_queries.erase(n);
206                         }
207                 }
208         }
209
210         void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
211         {
212                 // is the sql module loaded? If not, we don't attempt to do anything.
213                 if (!SQLModule)
214                         return;
215
216                 SQLrequest req = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
217                 req.Send();
218                 QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
219                 i->qs = FIND_SOURCE;
220                 active_queries[req.id] = i;
221         }
222
223         virtual void OnOper(User* user, const std::string &opertype)
224         {
225                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
226         }
227
228         virtual void OnGlobalOper(User* user)
229         {
230                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
231         }
232
233         virtual ModResult OnKill(User* source, User* dest, const std::string &reason)
234         {
235                 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
236                 return MOD_RES_PASSTHRU;
237         }
238
239         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
240         {
241                 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
242                 {
243                         AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+parameters[0],user->server);
244                 }
245                 return MOD_RES_PASSTHRU;
246         }
247
248         virtual ModResult OnUserRegister(LocalUser* user)
249         {
250                 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
251                 return MOD_RES_PASSTHRU;
252         }
253
254         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
255         {
256                 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
257         }
258
259         virtual void OnLoadModule(Module* mod)
260         {
261                 AddLogEntry(LT_LOADMODULE,mod->ModuleSourceFile,ServerInstance->Config->ServerName.c_str(), ServerInstance->Config->ServerName.c_str());
262         }
263
264         virtual Version GetVersion()
265         {
266                 return Version("Logs network-wide data to an SQL database", VF_VENDOR);
267         }
268
269 };
270
271 MODULE_INIT(ModuleSQLLog)