]> jfr.im git - irc/quakenet/newserv.git/blame - chanserv/authcmds/authhistory.c
Add a way of doing 'clean' unloads in pqsql.
[irc/quakenet/newserv.git] / chanserv / authcmds / authhistory.c
CommitLineData
6b2ca929 1/* Automatically generated by refactor.pl.
2 *
3 *
4 * CMDNAME: authhistory
5 * CMDLEVEL: QCMD_AUTHED
6 * CMDARGS: 1
7 * CMDDESC: View auth history for an account.
8 * CMDFUNC: csa_doauthhistory
9 * CMDPROTO: int csa_doauthhistory(void *source, int cargc, char **cargv);
50cd26d6 10 * CMDHELP: Usage: AUTHHISTORY
11 * CMDHELP: Displays details of the last 10 logins with your account. Details include
12 * CMDHELP: hostmask, login time, disconnect time and reason.
6b2ca929 13 */
14
15#include "../chanserv.h"
16#include "../authlib.h"
17#include "../../lib/irc_string.h"
18#include "../../pqsql/pqsql.h"
19
20#include <stdio.h>
21#include <string.h>
22#include <libpq-fe.h>
23
24struct authhistoryinfo {
25 unsigned int numeric;
26 unsigned int userID;
27};
28
29void csdb_doauthhistory_real(PGconn *dbconn, void *arg) {
30 struct authhistoryinfo *ahi=(struct authhistoryinfo*)arg;
31 nick *np=getnickbynumeric(ahi->numeric);
32 reguser *rup;
33 char *ahnick, *ahuser, *ahhost;
34 time_t ahauthtime, ahdisconnecttime;
35 PGresult *pgres;
36 int i, num, count=0;
37 struct tm *tmp;
38 char tbuf1[15], tbuf2[15], uhbuf[51];
39
40 pgres=PQgetResult(dbconn);
41
42 if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
43 Error("chanserv", ERR_ERROR, "Error loading auth history data.");
c34af51f 44 free(ahi);
6b2ca929 45 return;
46 }
47
48 if (PQnfields(pgres) != 7) {
49 Error("chanserv", ERR_ERROR, "Auth history data format error.");
c34af51f
CP
50 PQclear(pgres);
51 free(ahi);
6b2ca929 52 return;
53 }
54
55 num=PQntuples(pgres);
56
57 if (!np) {
58 PQclear(pgres);
59 free(ahi);
60 return;
61 }
62
63 if (!(rup=getreguserfromnick(np))) {
64 PQclear(pgres);
65 free(ahi);
66 return;
67 }
68 chanservstdmessage(np, QM_AUTHHISTORYHEADER);
69 for (i=0; i<num; i++) {
70 if (!UHasHelperPriv(rup) && (strtoul(PQgetvalue(pgres, i, 0), NULL, 10) != rup->ID)) {
71 PQclear(pgres);
72 free(ahi);
73 return;
74 }
75 ahnick=PQgetvalue(pgres, i, 1);
76 ahuser=PQgetvalue(pgres, i, 2);
77 ahhost=PQgetvalue(pgres, i, 3);
78 ahauthtime=strtoul(PQgetvalue(pgres, i, 4), NULL, 10);
79 ahdisconnecttime=strtoul(PQgetvalue(pgres, i, 5), NULL, 10);
80 tmp=localtime(&ahauthtime);
81 strftime(tbuf1, 15, "%d/%m/%y %H:%M", tmp);
82 if (ahdisconnecttime) {
83 tmp=localtime(&ahdisconnecttime);
84 strftime(tbuf2, 15, "%d/%m/%y %H:%M", tmp);
85 }
86 snprintf(uhbuf,50,"%s!%s@%s", ahnick, ahuser, ahhost);
87 chanservsendmessage(np, "#%-2d %-50s %-15s %-15s %s", ++count, uhbuf, tbuf1, ahdisconnecttime?tbuf2:"never", PQgetvalue(pgres,i,6));
88 }
89 chanservstdmessage(np, QM_ENDOFLIST);
90
91 PQclear(pgres);
92 free(ahi);
93}
94
95void csdb_retreiveauthhistory(nick *np, reguser *rup, int limit) {
96 struct authhistoryinfo *ahi;
97
98 ahi=(struct authhistoryinfo *)malloc(sizeof(struct authhistoryinfo));
99 ahi->numeric=np->numeric;
100 ahi->userID=rup->ID;
101 pqasyncquery(csdb_doauthhistory_real, (void *)ahi,
102 "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from authhistory where "
103 "userID=%u order by authtime desc limit %d", rup->ID, limit);
104}
105
106int csa_doauthhistory(void *source, int cargc, char **cargv) {
107 reguser *rup, *trup;
108 nick *sender=source;
109
110 if (!(rup=getreguserfromnick(sender)))
111 return CMD_ERROR;
112
113 if (cargc >= 1) {
114 if (!(trup=findreguser(sender, cargv[0])))
115 return CMD_ERROR;
116
117 /* don't allow non-opers to view oper auth history, but allow helpers to view non-oper history */
118 if ((trup != rup) && ((UHasOperPriv(trup) && !UHasOperPriv(rup)) || !UHasHelperPriv(rup))) {
1482fb78 119 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[0]);
6b2ca929 120 return CMD_ERROR;
121 }
1482fb78 122 } else {
6b2ca929 123 trup=rup;
1482fb78 124 }
125
6b2ca929 126 csdb_retreiveauthhistory(sender, trup, 10);
127
128 return CMD_OK;
129}