summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpeavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7>2006-12-29 05:13:21 +0000
committerpeavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7>2006-12-29 05:13:21 +0000
commit80af129f5275b707c58537b8044d991a0c7e01d3 (patch)
tree504305e2cc029eb63ba0bd4c735e773c014c85dd /src
parent67f77f29ed346c54a0316282ae3ec16d01dc7821 (diff)
Majorly rewrite to totally funk up SQL logging module. Works well with both MySQL and PostGres now.
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6156 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src')
-rw-r--r--src/modules/extra/m_sqllog.cpp220
1 files changed, 72 insertions, 148 deletions
diff --git a/src/modules/extra/m_sqllog.cpp b/src/modules/extra/m_sqllog.cpp
index ca3c80a96..401db811c 100644
--- a/src/modules/extra/m_sqllog.cpp
+++ b/src/modules/extra/m_sqllog.cpp
@@ -25,7 +25,7 @@ static std::string dbid;
enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
-enum QueryState { FIND_SOURCE, INSERT_SOURCE, FIND_NICK, INSERT_NICK, FIND_HOST, INSERT_HOST, INSERT_LOGENTRY, DONE };
+enum QueryState { FIND_SOURCE, FIND_NICK, FIND_HOST, DONE};
class QueryInfo;
@@ -33,210 +33,139 @@ std::map<unsigned long,QueryInfo*> active_queries;
class QueryInfo
{
- public:
+public:
QueryState qs;
unsigned long id;
std::string nick;
+ std::string source;
std::string hostname;
int sourceid;
int nickid;
int hostid;
int category;
time_t date;
- std::string lastquery;
+ bool insert;
- QueryInfo(const std::string &n, const std::string &h, unsigned long i, int cat)
+ QueryInfo(const std::string &n, const std::string &s, const std::string &h, unsigned long i, int cat)
{
qs = FIND_SOURCE;
nick = n;
+ source = s;
hostname = h;
id = i;
category = cat;
sourceid = nickid = hostid = -1;
date = time(NULL);
- lastquery = "";
+ insert = false;
}
void Go(SQLresult* res)
{
- // Nothing here and not sent yet
SQLrequest req = SQLreq(MyMod, SQLModule, dbid, "", "");
-
switch (qs)
{
case FIND_SOURCE:
- // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
- // If we find it, advance to FIND_NICK state, otherwise go to INSERT_SOURCE
- if (res->Cols())
+ if (res->Rows() && sourceid == -1 && !insert)
{
- if (sourceid == -1)
+ sourceid = atoi(res->GetValue(0,0).d.c_str());
+ req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
+ if(req.Send())
{
- sourceid = atoi(res->GetValue(0,0).d.c_str());
+ insert = false;
qs = FIND_NICK;
+ active_queries[req.id] = this;
}
- else qs = INSERT_SOURCE;
}
- else
+ else if (res->Rows() && sourceid == -1 && insert)
{
- if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
- {
- qs = INSERT_SOURCE;
- }
- else
+ req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", source);
+ if(req.Send())
{
- lastquery = "SELECT id,actor FROM ircd_log_actors WHERE actor='?'";
- req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
- if(req.Send())
- {
- qs = FIND_SOURCE;
- active_queries[req.id] = this;
- }
- else
- {
- //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
- }
- break;
+ insert = false;
+ qs = FIND_SOURCE;
+ active_queries[req.id] = this;
}
}
- case INSERT_SOURCE:
- // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
- // after we've done this, go back to FIND_SOURCE
- lastquery = "INSERT INTO ircd_log_actors VALUES('','?')";
- req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')", nick);
- if(req.Send())
- {
- qs = FIND_NICK;
- active_queries[req.id] = this;
- }
else
{
- //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
+ req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors (actor) VALUES('?')", source);
+ if(req.Send())
+ {
+ insert = true;
+ qs = FIND_SOURCE;
+ active_queries[req.id] = this;
+ }
}
-
break;
+
case FIND_NICK:
- // "SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'"
- // If we find it, advance to FIND_HOST state, otherwise go to INSERT_NICK
- if (res->Cols())
+ if (res->Rows() && nickid == -1 && !insert)
{
- if (nickid == -1)
- {
- nickid = atoi(res->GetValue(0,0).d.c_str());
- qs = FIND_HOST;
- }
- else qs = INSERT_NICK;
- }
- else
- {
- if (lastquery == "SELECT id,actor FROM ircd_log_actors WHERE actor='?'")
- {
- qs = INSERT_NICK;
- }
- else
+ nickid = atoi(res->GetValue(0,0).d.c_str());
+ req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'", hostname);
+ if(req.Send())
{
- req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
- if(req.Send())
- {
- qs = FIND_NICK;
- active_queries[req.id] = this;
- }
- else
- {
- //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
- }
- break;
+ insert = false;
+ qs = FIND_HOST;
+ active_queries[req.id] = this;
}
}
- case INSERT_NICK:
- // "INSERT INTO ircd_log_actors VALUES('','"+nick+"')")
- // after we've done this, go back to FIND_NICK
- req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors VALUES('','?')",nick);
- if(req.Send())
+ else if (res->Rows() && nickid == -1 && insert)
{
- qs = FIND_HOST;
- active_queries[req.id] = this;
+ req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
+ if(req.Send())
+ {
+ insert = false;
+ qs = FIND_NICK;
+ active_queries[req.id] = this;
+ }
}
else
{
- //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
+ req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_actors (actor) VALUES('?')",nick);
+ if(req.Send())
+ {
+ insert = true;
+ qs = FIND_NICK;
+ active_queries[req.id] = this;
+ }
}
break;
+
case FIND_HOST:
- // "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'"
- // If we find it, advance to INSERT_LOGENTRY state, otherwise go to INSERT_HOST
- if (res->Cols())
- {
- if (hostid == -1)
- {
- hostid = atoi(res->GetValue(0,0).d.c_str());
- qs = INSERT_LOGENTRY;
- }
- else qs = INSERT_HOST;
- }
- else
+ if (res->Rows() && hostid == -1 && !insert)
{
- if (lastquery == "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'")
- {
- qs = INSERT_HOST;
- }
- else
+ hostid = atoi(res->GetValue(0,0).d.c_str());
+ req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log (category_id,nick,host,source,dtime) VALUES("+ConvToStr(category)+","+ConvToStr(nickid)+","+ConvToStr(hostid)+","+ConvToStr(sourceid)+","+ConvToStr(date)+")");
+ if(req.Send())
{
- lastquery = "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'";
- req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'",hostname);
- if(req.Send())
- {
- qs = FIND_HOST;
- active_queries[req.id] = this;
- }
- else
- {
- //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
- }
- break;
+ insert = true;
+ qs = DONE;
+ active_queries[req.id] = this;
}
}
- case INSERT_HOST:
- // "INSERT INTO ircd_log_hosts VALUES('','"+host+"')"
- // after we've done this, go back to FIND_HOST
- lastquery = "INSERT INTO ircd_log_hosts VALUES('','?')";
- req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts VALUES('','?')", hostname);
- if(req.Send())
- {
- qs = INSERT_LOGENTRY;
- active_queries[req.id] = this;
- }
- else
- {
- //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
- }
- break;
- case INSERT_LOGENTRY:
- // 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
- // aaand done! (discard result)
- if ((nickid == -1) || (hostid == -1) || (sourceid == -1))
+ else if (res->Rows() && hostid == -1 && insert)
{
- qs = FIND_SOURCE;
- this->Go(res);
+ req = SQLreq(MyMod, SQLModule, dbid, "SELECT id,hostname FROM ircd_log_hosts WHERE hostname='?'", hostname);
+ if(req.Send())
+ {
+ insert = false;
+ qs = FIND_HOST;
+ active_queries[req.id] = this;
+ }
}
else
{
- lastquery = "INSERT INTO ircd_log VALUES()";
- req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log VALUES('',"+ConvToStr(category)+","+ConvToStr(nickid)+","+ConvToStr(hostid)+","+ConvToStr(sourceid)+","+ConvToStr(date)+")");
- /*,category,
- nickid,
- hostid,
- sourceid,
- date);*/
+ req = SQLreq(MyMod, SQLModule, dbid, "INSERT INTO ircd_log_hosts (hostname) VALUES('?')", hostname);
if(req.Send())
{
- qs = DONE;
- }
- else
- {
- //ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
+ insert = true;
+ qs = FIND_HOST;
+ active_queries[req.id] = this;
}
}
break;
+
case DONE:
delete active_queries[req.id];
active_queries[req.id] = NULL;
@@ -329,10 +258,10 @@ class ModuleSQLLog : public Module
if (!SQLModule)
return;
- SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", nick);
+ SQLrequest req = SQLreq(this, SQLModule, dbid, "SELECT id,actor FROM ircd_log_actors WHERE actor='?'", source);
if(req.Send())
{
- QueryInfo* i = new QueryInfo(nick, host, req.id, category);
+ QueryInfo* i = new QueryInfo(nick, source, host, req.id, category);
i->qs = FIND_SOURCE;
active_queries[req.id] = i;
ServerInstance->Log(DEBUG,"Active query id %d",req.id);
@@ -341,11 +270,6 @@ class ModuleSQLLog : public Module
{
ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
}
-
- /*long nickid = InsertNick(nick);
- long sourceid = InsertNick(source);
- long hostid = InsertHost(host);
- InsertEntry((unsigned)category,(unsigned)nickid,(unsigned)hostid,(unsigned)sourceid,(unsigned long)time(NULL));*/
}
virtual void OnOper(userrec* user, const std::string &opertype)