]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/authtracker/authtracker_query.c
e42344177ac7040d702bd2ffcc28d0f2812fb8bb
[irc/quakenet/newserv.git] / chanserv / authtracker / authtracker_query.c
1 /*
2 * The interface to the actual database
3 */
4
5 #include "authtracker.h"
6 #include "../../pqsql/pqsql.h"
7 #include "../../nick/nick.h"
8 #include "../../core/error.h"
9
10 #include <libpq-fe.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 void at_logquit(unsigned long userid, time_t accountts, time_t when, char *reason) {
15 char lreason[100], escreason[205];
16 strncpy(lreason,reason,99);
17 lreason[99]='\0';
18
19 PQescapeString(escreason, lreason, strlen(lreason));
20
21 pqquery("UPDATE authhistory SET disconnecttime=%lu, quitreason='%s' WHERE userID=%lu AND authtime=%lu",
22 when, escreason, userid, accountts);
23 }
24
25 void at_lognewsession(unsigned int userid, nick *np) {
26 char escnick[NICKLEN*2+1];
27 char escuser[USERLEN*2+1];
28 char eschost[HOSTLEN*2+1];
29
30 PQescapeString(escnick, np->nick, strlen(np->nick));
31 PQescapeString(escuser, np->ident, strlen(np->ident));
32 PQescapeString(eschost, np->host->name->content, np->host->name->length);
33
34 pqquery("INSERT INTO authhistory (userID, nick, username, host, authtime, disconnecttime, numeric) "
35 "VALUES (%lu, '%s', '%s', '%s', %lu, %lu, %u)",
36 userid, escnick, escuser, eschost, np->accountts, 0UL, np->numeric);
37 }
38
39 static void real_at_finddanglingsessions(PGconn *dbconn, void *arg) {
40 PGresult *pgres;
41 unsigned int i,num;
42
43 pgres=PQgetResult(dbconn);
44
45 if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
46 Error("chanserv",ERR_ERROR,"Error loading dangling sessions.");
47 return;
48 }
49
50 if (PQnfields(pgres)!=3) {
51 Error("authtracker",ERR_ERROR,"Dangling sessions format error");
52 return;
53 }
54
55 num=PQntuples(pgres);
56
57 for (i=0;i<num;i++) {
58 at_lostnick(
59 strtoul(PQgetvalue(pgres,i,0),NULL,10), /* numeric */
60 strtoul(PQgetvalue(pgres,i,1),NULL,10), /* userID */
61 strtoul(PQgetvalue(pgres,i,2),NULL,10), /* authtime */
62 time(NULL),
63 AT_RESTART
64 );
65 }
66
67 PQclear(pgres);
68
69 at_dbloaded(0, NULL);
70 }
71
72 void at_finddanglingsessions() {
73 pqasyncquery(real_at_finddanglingsessions, NULL,
74 "SELECT numeric,userID,authtime FROM authhistory WHERE disconnecttime=0");
75
76 }