]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqllog.cpp
Get rid of a bunch of memory-wasting C-style strings
[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                 SQLrequest req = SQLrequest(MyMod, SQLModule, dbid, SQLquery(""));
60                 switch (qs)
61                 {
62                         case FIND_SOURCE:
63                                 if (res->Rows() && sourceid == -1 && !insert)
64                                 {
65                                         sourceid = atoi(res->GetValue(0,0).d.c_str());
66                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
67                                         if(req.Send())
68                                         {
69                                                 insert = false;
70                                                 qs = FIND_NICK;
71                                                 active_queries[req.id] = this;
72                                         }
73                                 }
74                                 else if (res->Rows() && sourceid == -1 && insert)
75                                 {
76                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
77                                         if(req.Send())
78                                         {
79                                                 insert = false;
80                                                 qs = FIND_SOURCE;
81                                                 active_queries[req.id] = this;
82                                         }
83                                 }
84                                 else
85                                 {
86                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % source);
87                                         if(req.Send())
88                                         {
89                                                 insert = true;
90                                                 qs = FIND_SOURCE;
91                                                 active_queries[req.id] = this;
92                                         }
93                                 }
94                         break;
95
96                         case FIND_NICK:
97                                 if (res->Rows() && nickid == -1 && !insert)
98                                 {
99                                         nickid = atoi(res->GetValue(0,0).d.c_str());
100                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
101                                         if(req.Send())
102                                         {
103                                                 insert = false;
104                                                 qs = FIND_HOST;
105                                                 active_queries[req.id] = this;
106                                         }
107                                 }
108                                 else if (res->Rows() && nickid == -1 && insert)
109                                 {
110                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % nick);
111                                         if(req.Send())
112                                         {
113                                                 insert = false;
114                                                 qs = FIND_NICK;
115                                                 active_queries[req.id] = this;
116                                         }
117                                 }
118                                 else
119                                 {
120                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_actors (actor) VALUES('?')") % nick);
121                                         if(req.Send())
122                                         {
123                                                 insert = true;
124                                                 qs = FIND_NICK;
125                                                 active_queries[req.id] = this;
126                                         }
127                                 }
128                         break;
129
130                         case FIND_HOST:
131                                 if (res->Rows() && hostid == -1 && !insert)
132                                 {
133                                         hostid = atoi(res->GetValue(0,0).d.c_str());
134                                         req = SQLrequest(MyMod, SQLModule, dbid,
135                                                         SQLquery("INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES('?','?','?','?','?')") % category % nickid % hostid % sourceid % date);
136                                         if(req.Send())
137                                         {
138                                                 insert = true;
139                                                 qs = DONE;
140                                                 active_queries[req.id] = this;
141                                         }
142                                 }
143                                 else if (res->Rows() && hostid == -1 && insert)
144                                 {
145                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'") % hostname);
146                                         if(req.Send())
147                                         {
148                                                 insert = false;
149                                                 qs = FIND_HOST;
150                                                 active_queries[req.id] = this;
151                                         }
152                                 }
153                                 else
154                                 {
155                                         req = SQLrequest(MyMod, SQLModule, dbid, SQLquery("INSERT INTO ircd_log_hosts (hostname) VALUES('?')") % hostname);
156                                         if(req.Send())
157                                         {
158                                                 insert = true;
159                                                 qs = FIND_HOST;
160                                                 active_queries[req.id] = this;
161                                         }
162                                 }
163                         break;
164
165                         case DONE:
166                                 std::map<unsigned long,QueryInfo*>::iterator x = active_queries.find(req.id);
167                                 if (x != active_queries.end())
168                                 {
169                                         delete x->second;
170                                         active_queries.erase(x);
171                                 }
172                         break;
173                 }
174         }
175 };
176
177 /* $ModDesc: Logs network-wide data to an SQL database */
178
179 class ModuleSQLLog : public Module
180 {
181
182  public:
183         ModuleSQLLog()
184                 {
185                 ServerInstance->Modules->UseInterface("SQLutils");
186                 ServerInstance->Modules->UseInterface("SQL");
187
188                 Module* SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
189                 if (!SQLutils)
190                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
191
192                 SQLModule = ServerInstance->Modules->FindFeature("SQL");
193
194                 OnRehash(NULL);
195                 MyMod = this;
196                 active_queries.clear();
197
198                 Implementation eventlist[] = { I_OnRehash, I_OnOper, I_OnGlobalOper, I_OnKill,
199                         I_OnPreCommand, I_OnUserConnect, I_OnUserQuit, I_OnLoadModule, I_OnRequest };
200                 ServerInstance->Modules->Attach(eventlist, this, 9);
201         }
202
203         virtual ~ModuleSQLLog()
204         {
205                 ServerInstance->Modules->DoneWithInterface("SQL");
206                 ServerInstance->Modules->DoneWithInterface("SQLutils");
207         }
208
209
210         void ReadConfig()
211         {
212                 ConfigReader Conf;
213                 dbid = Conf.ReadValue("sqllog","dbid",0);       // database id of a database configured in sql module
214         }
215
216         virtual void OnRehash(User* user)
217         {
218                 ReadConfig();
219         }
220
221         virtual const char* OnRequest(Request* request)
222         {
223                 if(strcmp(SQLRESID, request->GetId()) == 0)
224                 {
225                         SQLresult* res;
226                         std::map<unsigned long, QueryInfo*>::iterator n;
227
228                         res = static_cast<SQLresult*>(request);
229                         n = active_queries.find(res->id);
230
231                         if (n != active_queries.end())
232                         {
233                                 n->second->Go(res);
234                                 active_queries.erase(n);
235                         }
236
237                         return SQLSUCCESS;
238                 }
239
240                 return NULL;
241         }
242
243         void AddLogEntry(int category, const std::string &nick, const std::string &host, const std::string &source)
244         {
245                 // is the sql module loaded? If not, we don't attempt to do anything.
246                 if (!SQLModule)
247                         return;
248
249                 SQLrequest req = SQLrequest(this, SQLModule, dbid, SQLquery("SELECT id,actor FROM ircd_log_actors WHERE actor='?'") % source);
250                 if(req.Send())
251                 {
252                         QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
253                         i->qs = FIND_SOURCE;
254                         active_queries[req.id] = i;
255                 }
256         }
257
258         virtual void OnOper(User* user, const std::string &opertype)
259         {
260                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
261         }
262
263         virtual void OnGlobalOper(User* user)
264         {
265                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
266         }
267
268         virtual ModResult OnKill(User* source, User* dest, const std::string &reason)
269         {
270                 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
271                 return MOD_RES_PASSTHRU;
272         }
273
274         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
275         {
276                 if ((command == "GLINE" || command == "KLINE" || command == "ELINE" || command == "ZLINE") && validated)
277                 {
278                         AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+parameters[0],user->server);
279                 }
280                 return MOD_RES_PASSTHRU;
281         }
282
283         virtual void OnUserConnect(User* user)
284         {
285                 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
286         }
287
288         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
289         {
290                 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
291         }
292
293         virtual void OnLoadModule(Module* mod, const std::string &name)
294         {
295                 AddLogEntry(LT_LOADMODULE,name,ServerInstance->Config->ServerName.c_str(), ServerInstance->Config->ServerName.c_str());
296         }
297
298         virtual Version GetVersion()
299         {
300                 return Version("Logs network-wide data to an SQL database", VF_VENDOR, API_VERSION);
301         }
302
303 };
304
305 MODULE_INIT(ModuleSQLLog)