]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/authcmds/authhistory.c
fix indentation
[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 struct tm *tmp;
37 char tbuf1[15], tbuf2[15], uhbuf[51];
38
39 if(!dbconn) {
40 free(ahi);
41 return;
42 }
43
44 pgres=dbgetresult(dbconn);
45
46 if (!dbquerysuccessful(pgres)) {
47 Error("chanserv", ERR_ERROR, "Error loading auth history data.");
48 free(ahi);
49 return;
50 }
51
52 if (dbnumfields(pgres) != 7) {
53 Error("chanserv", ERR_ERROR, "Auth history data format error.");
54 dbclear(pgres);
55 free(ahi);
56 return;
57 }
58
59 if (!np) {
60 dbclear(pgres);
61 free(ahi);
62 return;
63 }
64
65 if (!(rup=getreguserfromnick(np))) {
66 dbclear(pgres);
67 free(ahi);
68 return;
69 }
70 chanservstdmessage(np, QM_AUTHHISTORYHEADER);
71 while(dbfetchrow(pgres)) {
72 if (!UHasHelperPriv(rup) && (strtoul(dbgetvalue(pgres, 0), NULL, 10) != rup->ID)) {
73 dbclear(pgres);
74 free(ahi);
75 return;
76 }
77 ahnick=dbgetvalue(pgres, 1);
78 ahuser=dbgetvalue(pgres, 2);
79 ahhost=dbgetvalue(pgres, 3);
80 ahauthtime=strtoul(dbgetvalue(pgres, 4), NULL, 10);
81 ahdisconnecttime=strtoul(dbgetvalue(pgres, 5), NULL, 10);
82 tmp=gmtime(&ahauthtime);
83 strftime(tbuf1, sizeof(tbuf1), Q9_FORMAT_TIME, tmp);
84 if (ahdisconnecttime) {
85 tmp=gmtime(&ahdisconnecttime);
86 strftime(tbuf2, sizeof(tbuf2), Q9_FORMAT_TIME, tmp);
87 }
88 snprintf(uhbuf,50,"%s!%s@%s", ahnick, ahuser, ahhost);
89 chanservsendmessage(np, "#%-2d %-50s %-15s %-15s %s", ++count, uhbuf, tbuf1, ahdisconnecttime?tbuf2:"never", dbgetvalue(pgres,6));
90 }
91 chanservstdmessage(np, QM_ENDOFLIST);
92
93 dbclear(pgres);
94 free(ahi);
95 }
96
97 void csdb_retreiveauthhistory(nick *np, reguser *rup, int limit) {
98 struct authhistoryinfo *ahi;
99
100 ahi=(struct authhistoryinfo *)malloc(sizeof(struct authhistoryinfo));
101 ahi->numeric=np->numeric;
102 ahi->userID=rup->ID;
103 q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi,
104 "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where "
105 "userID=%u order by authtime desc limit %d", rup->ID, limit);
106 }
107
108 int csa_doauthhistory(void *source, int cargc, char **cargv) {
109 reguser *rup, *trup;
110 nick *sender=source;
111
112 if (!(rup=getreguserfromnick(sender)))
113 return CMD_ERROR;
114
115 if (cargc >= 1) {
116 if (!(trup=findreguser(sender, cargv[0])))
117 return CMD_ERROR;
118
119 /* don't allow non-opers to view oper auth history, but allow helpers to view non-oper history */
120 if ((trup != rup) && ((UHasOperPriv(trup) && !UHasOperPriv(rup)) || !UHasHelperPriv(rup))) {
121 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[0]);
122 return CMD_ERROR;
123 }
124 } else {
125 trup=rup;
126 }
127
128 csdb_retreiveauthhistory(sender, trup, 10);
129
130 return CMD_OK;
131 }