1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
21 #include <sys/types.h>
22 #include <sys/socket.h>
40 #define LT_DISCONNECT 6
42 #define LT_LOADMODULE 8
44 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
48 class ModuleSQLLog : public Module
57 Conf = new ConfigReader();
58 dbid = Conf->ReadInteger("sqllog","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
60 SQLModule = Srv->FindModule("m_sql.so");
62 Srv->Log(DEFAULT,"WARNING: m_SQLLog.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
72 virtual void OnRehash()
77 long InsertNick(std::string nick)
80 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT id,nick FROM ircd_log_actors WHERE actor='"+nick+"'");
81 Request queryrequest((char*)query, this, SQLModule);
82 SQLResult* result = (SQLResult*)queryrequest.Send();
83 if (result->GetType() == SQL_OK)
85 SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
86 Request rowquery((char*)rowrequest, this, SQLModule);
87 SQLResult* rowresult = (SQLResult*)rowquery.Send();
88 if (rowresult->GetType() == SQL_ROW)
90 nid = atoi(rowresult->GetField("id"));
96 query->SetQueryType(SQL_DONE);
97 query->SetConnID(dbid);
98 Request donerequest((char*)query, this, SQLModule);
103 SQLRequest* query = new SQLRequest(SQL_COUNT,dbid,"INSERT INTO ircd_log_actors VALUES('','"+nick+"')");
104 Request queryrequest((char*)query, this, SQLModule);
105 SQLResult* result = (SQLResult*)queryrequest.Send();
106 if (result->GetType() == SQL_ERROR)
108 Srv->Log(DEFAULT,"SQL log error: " + result->GetError());
114 nid = InsertNick(nick);
119 void InsertEntry(long category,long nickid,long hostid,long sourceid,unsigned long date);
121 char querybuffer[MAXBUF];
122 snprintf(querybuffer,MAXBUF,"INSERT INTO ircd_log VALUES('',%d,%d,%d,%d,%lu)",category,nickid,hostid,sourceid,date);
123 SQLRequest* query = new SQLRequest(SQL_COUNT,dbid,querybuffer);
124 Request queryrequest((char*)query, this, SQLModule);
125 SQLResult* result = (SQLResult*)queryrequest.Send();
126 if (result->GetType() == SQL_ERROR)
128 Srv->Log(DEFAULT,"SQL log error: " + result->GetError());
137 long InsertHost(std::string host)
140 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'");
141 Request queryrequest((char*)query, this, SQLModule);
142 SQLResult* result = (SQLResult*)queryrequest.Send();
143 if (result->GetType() == SQL_OK)
145 SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
146 Request rowquery((char*)rowrequest, this, SQLModule);
147 SQLResult* rowresult = (SQLResult*)rowquery.Send();
148 if (rowresult->GetType() == SQL_ROW)
150 hid = atoi(rowresult->GetField("id"));
156 query->SetQueryType(SQL_DONE);
157 query->SetConnID(dbid);
158 Request donerequest((char*)query, this, SQLModule);
163 SQLRequest* query = new SQLRequest(SQL_COUNT,dbid,"INSERT INTO ircd_log_hosts VALUES('','"+host+"')");
164 Request queryrequest((char*)query, this, SQLModule);
165 SQLResult* result = (SQLResult*)queryrequest.Send();
166 if (result->GetType() == SQL_ERROR)
168 Srv->Log(DEFAULT,"SQL log error: " + result->GetError());
174 hid = InsertHost(host);
179 void AddLogEntry(int category, std::string nick, std::string host, std::string source)
181 // is the sql module loaded? If not, we don't attempt to do anything.
185 long nickid = InsertNick(nick);
186 long sourceid = InsertNick(source);
187 long hostid = InsertHost(host);
188 InsertEntry(category,nickid,hostid,sourceid,time(NULL));
191 virtual void OnOper(userrec* user)
193 AddLogEntry(LT_OPER,user->nick,user->host,user->server);
196 virtual ~ModuleSQLLog()
201 virtual Version GetVersion()
203 return Version(1,0,0,1,VF_VENDOR);
208 class ModuleSQLLogFactory : public ModuleFactory
211 ModuleSQLLogFactory()
215 ~ModuleSQLLogFactory()
219 virtual Module * CreateModule()
221 return new ModuleSQLLog;
227 extern "C" void * init_module( void )
229 return new ModuleSQLLogFactory;