]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/authcmds/authhistory.c
44f32a278a105cfb68edd88462ba789744828697
[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: 2
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 char limitstr[30];
98
99 if (limit) {
100 sprintf(limitstr, " limit %d",limit);
101 } else {
102 limitstr[0]='\0';
103 }
104
105 ahi=(struct authhistoryinfo *)malloc(sizeof(struct authhistoryinfo));
106 ahi->numeric=np->numeric;
107 ahi->userID=rup->ID;
108 q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi,
109 "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where "
110 "userID=%u order by authtime desc%s", rup->ID, limitstr);
111 }
112
113 int csa_doauthhistory(void *source, int cargc, char **cargv) {
114 reguser *rup, *trup;
115 nick *sender=source;
116 unsigned int arg=0;
117 unsigned int limit=10;
118
119 if (!(rup=getreguserfromnick(sender)))
120 return CMD_ERROR;
121
122 if (cargc) {
123 if (!ircd_strcmp(cargv[0], "-a")) {
124 if (UHasOperPriv(rup))
125 limit=0;
126
127 arg++;
128 }
129 }
130
131 if (cargc > arg) {
132 if (!(trup=findreguser(sender, cargv[arg])))
133 return CMD_ERROR;
134
135 /* if target != command issuer */
136 if (trup != rup) {
137 /* only opers and helpers can view authhistory of other users */
138 if (!UHasHelperPriv(rup)) {
139 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]);
140 return CMD_ERROR;
141 }
142
143 /* and only opers can view opers history */
144 if (UHasOperPriv(trup) && !UHasOperPriv(rup)) {
145 chanservwallmessage("%s (%s) just FAILED using AUTHHISTORY on %s", sender->nick, rup->username, trup->username);
146 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]);
147 return CMD_ERROR;
148 }
149
150 /* checks passed */
151 chanservwallmessage("%s (%s) used AUTHHISTORY on %s", sender->nick, rup->username, trup->username);
152 }
153 } else {
154 trup=rup;
155 }
156
157 csdb_retreiveauthhistory(sender, trup, limit);
158
159 return CMD_OK;
160 }