]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqllog.cpp
bffed5dd9b69d3dab2f356467ddd446c56ebc1a7
[user/henk/code/inspircd.git] / src / modules / extra / m_sqllog.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <poll.h>
31 #include "users.h"
32 #include "channels.h"
33 #include "modules.h"
34 #include "inspircd.h"
35 #include "m_sql.h"
36
37 #define LT_OPER         1
38 #define LT_KILL         2
39 #define LT_SERVLINK     3
40 #define LT_XLINE        4
41 #define LT_CONNECT      5
42 #define LT_DISCONNECT   6
43 #define LT_FLOOD        7
44 #define LT_LOADMODULE   8
45
46 /* $ModDesc: Logs network-wide data to an SQL database */
47
48 Server *Srv;
49
50 class ModuleSQLLog : public Module
51 {
52         ConfigReader* Conf;
53         unsigned long dbid;
54         Module* SQLModule;
55
56  public:
57         bool ReadConfig()
58         {
59                 Conf = new ConfigReader();
60                 dbid = Conf->ReadInteger("sqllog","dbid",0,true);       // database id of a database configured in m_sql (see m_sql config)
61                 delete Conf;
62                 SQLModule = Srv->FindModule("m_sql.so");
63                 if (!SQLModule)
64                         Srv->Log(DEFAULT,"WARNING: m_SQLLog.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
65                 return (SQLModule);
66         }
67
68         ModuleSQLLog()
69         {
70                 Srv = new Server;
71                 ReadConfig();
72         }
73
74         virtual void OnRehash()
75         {
76                 ReadConfig();
77         }
78
79         long InsertNick(std::string nick)
80         {
81                 long nid = -1;
82                 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'");
83                 Request queryrequest((char*)query, this, SQLModule);
84                 SQLResult* result = (SQLResult*)queryrequest.Send();
85                 if (result->GetType() == SQL_OK)
86                 {
87                         SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
88                         Request rowquery((char*)rowrequest, this, SQLModule);
89                         SQLResult* rowresult = (SQLResult*)rowquery.Send();
90                         if (rowresult->GetType() == SQL_ROW)
91                         {
92                                 nid = atoi(rowresult->GetField("id").c_str());
93                                 delete rowresult;
94                         }
95                         delete rowrequest;
96                         delete result;
97                 }
98                 query->SetQueryType(SQL_DONE);
99                 query->SetConnID(dbid);
100                 Request donerequest((char*)query, this, SQLModule);
101                 donerequest.Send();
102                 delete query;
103                 if (nid < 1)
104                 {
105                         SQLRequest* query2 = new SQLRequest(SQL_COUNT,dbid,"INSERT INTO ircd_log_actors VALUES('','"+nick+"')");
106                         Request queryrequest2((char*)query2, this, SQLModule);
107                         SQLResult* result2 = (SQLResult*)queryrequest2.Send();
108                         if (result2->GetType() == SQL_ERROR)
109                         {
110                                 Srv->Log(DEFAULT,"SQL log error: " + result2->GetError());
111                         }
112                         if (result2)
113                                 delete result;
114                         if (query2)
115                                 delete query2;
116                         nid = InsertNick(nick);
117                 }
118                 return nid;
119         }
120
121         void InsertEntry(unsigned long category,unsigned long nickid,unsigned long hostid,unsigned long sourceid,unsigned long date)
122         {
123                 char querybuffer[MAXBUF];
124                 snprintf(querybuffer,MAXBUF,"INSERT INTO ircd_log VALUES('',%lu,%lu,%lu,%lu,%lu)",(unsigned long)category,(unsigned long)nickid,(unsigned long)hostid,(unsigned long)sourceid,(unsigned long)date);
125                 SQLRequest* query = new SQLRequest(SQL_COUNT,dbid,querybuffer);
126                 Request queryrequest((char*)query, this, SQLModule);
127                 SQLResult* result = (SQLResult*)queryrequest.Send();
128                 if (result->GetType() == SQL_ERROR)
129                 {
130                         Srv->Log(DEFAULT,"SQL log error: " + result->GetError());
131                 }
132                 if (result)
133                         delete result;
134                 if (query)
135                         delete query;
136                 return;
137         }
138
139         long InsertHost(std::string host)
140         {
141                 long hid = -1;
142                 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'");
143                 Request queryrequest((char*)query, this, SQLModule);
144                 SQLResult* result = (SQLResult*)queryrequest.Send();
145                 if (result->GetType() == SQL_OK)
146                 {
147                         SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
148                         Request rowquery((char*)rowrequest, this, SQLModule);
149                         SQLResult* rowresult = (SQLResult*)rowquery.Send();
150                         if (rowresult->GetType() == SQL_ROW)
151                         {
152                                 hid = atoi(rowresult->GetField("id").c_str());
153                                 delete rowresult;
154                         }
155                         delete rowrequest;
156                         delete result;
157                 }
158                 query->SetQueryType(SQL_DONE);
159                 query->SetConnID(dbid);
160                 Request donerequest((char*)query, this, SQLModule);
161                 donerequest.Send();
162                 delete query;
163                 if (hid < 1)
164                 {
165                         SQLRequest* query2 = new SQLRequest(SQL_COUNT,dbid,"INSERT INTO ircd_log_hosts VALUES('','"+host+"')");
166                         Request queryrequest2((char*)query2, this, SQLModule);
167                         SQLResult* result2 = (SQLResult*)queryrequest2.Send();
168                         if (result2->GetType() == SQL_ERROR)
169                         {
170                                 Srv->Log(DEFAULT,"SQL log error: " + result2->GetError());
171                         }
172                         if (result)
173                                 delete result2;
174                         if (query)
175                                 delete query2;
176                         hid = InsertHost(host);
177                 }
178                 return hid;
179         }
180
181         void AddLogEntry(int category, std::string nick, std::string host, std::string source)
182         {
183                 // is the sql module loaded? If not, we don't attempt to do anything.
184                 if (!SQLModule)
185                         return;
186
187                 long nickid = InsertNick(nick);
188                 long sourceid = InsertNick(source);
189                 long hostid = InsertHost(host);
190                 InsertEntry((unsigned)category,(unsigned)nickid,(unsigned)hostid,(unsigned)sourceid,(unsigned long)time(NULL));
191         }
192
193         virtual void OnOper(userrec* user)
194         {
195                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
196         }
197
198         virtual void OnGlobalOper(userrec* user)
199         {
200                 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
201         }
202
203         virtual int OnKill(userrec* source, userrec* dest, std::string reason)
204         {
205                 AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
206                 return 0;
207         }
208
209         virtual int OnMeshToken(char token,string_list params,serverrec* source,serverrec* reply, std::string tcp_host,std::string ipaddr,int port)
210         {
211                 if ((token == 'U') || (token == 's') || (token == 'S'))
212                         AddLogEntry(LT_SERVLINK,tcp_host,ipaddr,Srv->GetServerName());
213                 return 0;
214         }
215
216         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
217         {
218                 if ((command == "GLINE") || (command == "KLINE") || (command == "ELINE") || (command == "ZLINE"))
219                 {
220                         AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
221                 }
222                 return 0;
223         }
224
225         virtual void OnUserConnect(userrec* user)
226         {
227                 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
228         }
229
230         virtual void OnGlobalConnect(userrec* user)
231         {
232                 AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
233         }
234
235         virtual void OnUserQuit(userrec* user)
236         {
237                 AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
238         }
239
240         virtual void OnLoadModule(Module* mod,std::string name)
241         {
242                 AddLogEntry(LT_LOADMODULE,name,Srv->GetServerName(),Srv->GetServerName());
243         }
244
245         virtual ~ModuleSQLLog()
246         {
247                 delete Srv;
248         }
249         
250         virtual Version GetVersion()
251         {
252                 return Version(1,0,0,1,VF_VENDOR);
253         }
254         
255 };
256
257 class ModuleSQLLogFactory : public ModuleFactory
258 {
259  public:
260         ModuleSQLLogFactory()
261         {
262         }
263         
264         ~ModuleSQLLogFactory()
265         {
266         }
267         
268         virtual Module * CreateModule()
269         {
270                 return new ModuleSQLLog;
271         }
272         
273 };
274
275
276 extern "C" void * init_module( void )
277 {
278         return new ModuleSQLLogFactory;
279 }
280