]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/authcmds/authhistory.c
3621657d3a5935f7d4f8639f3022c912024ddf42
[irc/quakenet/newserv.git] / chanserv / authcmds / authhistory.c
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);
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.
13 */
14
15 #include "../chanserv.h"
16 #include "../authlib.h"
17 #include "../../lib/irc_string.h"
18 #include "../../dbapi/dbapi.h"
19
20 #include <stdio.h>
21 #include <string.h>
22
23 struct authhistoryinfo {
24 unsigned int numeric;
25 unsigned int userID;
26 };
27
28 void csdb_doauthhistory_real(DBConn *dbconn, void *arg) {
29 struct authhistoryinfo *ahi=(struct authhistoryinfo*)arg;
30 nick *np=getnickbynumeric(ahi->numeric);
31 reguser *rup;
32 char *ahnick, *ahuser, *ahhost;
33 time_t ahauthtime, ahdisconnecttime;
34 DBResult *pgres;
35 int count=0;
36 char tbuf1[TIMELEN], tbuf2[TIMELEN], uhbuf[NICKLEN+HOSTLEN+USERLEN+5];
37
38 if(!dbconn) {
39 free(ahi);
40 return;
41 }
42
43 pgres=dbgetresult(dbconn);
44
45 if (!dbquerysuccessful(pgres)) {
46 Error("chanserv", ERR_ERROR, "Error loading auth history data.");
47 free(ahi);
48 return;
49 }
50
51 if (dbnumfields(pgres) != 7) {
52 Error("chanserv", ERR_ERROR, "Auth history data format error.");
53 dbclear(pgres);
54 free(ahi);
55 return;
56 }
57
58 if (!np) {
59 dbclear(pgres);
60 free(ahi);
61 return;
62 }
63
64 if (!(rup=getreguserfromnick(np))) {
65 dbclear(pgres);
66 free(ahi);
67 return;
68 }
69 chanservstdmessage(np, QM_AUTHHISTORYHEADER); /* @TIMELEN */
70 while(dbfetchrow(pgres)) {
71 if (!UHasHelperPriv(rup) && (strtoul(dbgetvalue(pgres, 0), NULL, 10) != rup->ID)) {
72 dbclear(pgres);
73 free(ahi);
74 return;
75 }
76 ahnick=dbgetvalue(pgres, 1);
77 ahuser=dbgetvalue(pgres, 2);
78 ahhost=dbgetvalue(pgres, 3);
79 ahauthtime=strtoul(dbgetvalue(pgres, 4), NULL, 10);
80 ahdisconnecttime=strtoul(dbgetvalue(pgres, 5), NULL, 10);
81
82 q9strftime(tbuf1, sizeof(tbuf1), ahauthtime);
83 if (ahdisconnecttime)
84 q9strftime(tbuf2, sizeof(tbuf2), ahdisconnecttime);
85
86 snprintf(uhbuf,sizeof(uhbuf),"%s!%s@%s", ahnick, ahuser, ahhost);
87 chanservsendmessage(np, "#%-2d %-50s %-19s %-19s %s", ++count, uhbuf, tbuf1, ahdisconnecttime?tbuf2:"never", dbgetvalue(pgres,6)); /* @TIMELEN */
88 }
89 chanservstdmessage(np, QM_ENDOFLIST);
90
91 dbclear(pgres);
92 free(ahi);
93 }
94
95 void 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 q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi,
102 "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where "
103 "userID=%u order by authtime desc limit %d", rup->ID, limit);
104 }
105
106 int 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))) {
119 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[0]);
120 return CMD_ERROR;
121 }
122 } else {
123 trup=rup;
124 }
125
126 csdb_retreiveauthhistory(sender, trup, 10);
127
128 return CMD_OK;
129 }