]> jfr.im git - irc/quakenet/newserv.git/blame - chanserv/authtracker/authtracker_query.c
Attempt to import clean unloading wrt. pqsql into Q9.
[irc/quakenet/newserv.git] / chanserv / authtracker / authtracker_query.c
CommitLineData
84563ebd 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
14void 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
25void 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)",
6cfc3444 36 userid, escnick, escuser, eschost, np->accountts, 0UL, np->numeric);
84563ebd 37}
38
39static void real_at_finddanglingsessions(PGconn *dbconn, void *arg) {
40 PGresult *pgres;
41 unsigned int i,num;
b3565978
CP
42
43 if(!dbconn)
44 return;
45
84563ebd 46 pgres=PQgetResult(dbconn);
47
48 if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
49 Error("chanserv",ERR_ERROR,"Error loading dangling sessions.");
50 return;
51 }
52
53 if (PQnfields(pgres)!=3) {
54 Error("authtracker",ERR_ERROR,"Dangling sessions format error");
b3565978 55 PQclear(pgres);
84563ebd 56 return;
57 }
58
59 num=PQntuples(pgres);
60
61 for (i=0;i<num;i++) {
62 at_lostnick(
63 strtoul(PQgetvalue(pgres,i,0),NULL,10), /* numeric */
64 strtoul(PQgetvalue(pgres,i,1),NULL,10), /* userID */
65 strtoul(PQgetvalue(pgres,i,2),NULL,10), /* authtime */
66 time(NULL),
67 AT_RESTART
68 );
69 }
70
71 PQclear(pgres);
72
73 at_dbloaded(0, NULL);
74}
75
76void at_finddanglingsessions() {
b3565978 77 pqasyncqueryi(authtrackerpq, real_at_finddanglingsessions, NULL,
84563ebd 78 "SELECT numeric,userID,authtime FROM authhistory WHERE disconnecttime=0");
79
80}