]> jfr.im git - irc/quakenet/newserv.git/blame - chanserv/authcmds/authhistory.c
CHANSERV: add walls to AUTHHISTORY.
[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"
ee8cd7d0 18#include "../../dbapi/dbapi.h"
6b2ca929 19
20#include <stdio.h>
21#include <string.h>
6b2ca929 22
23struct authhistoryinfo {
24 unsigned int numeric;
25 unsigned int userID;
26};
27
ee8cd7d0 28void csdb_doauthhistory_real(DBConn *dbconn, void *arg) {
6b2ca929 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;
ee8cd7d0
CP
34 DBResult *pgres;
35 int count=0;
b7eebdcc 36 char tbuf1[TIMELEN], tbuf2[TIMELEN], uhbuf[NICKLEN+HOSTLEN+USERLEN+5];
6b2ca929 37
b3565978
CP
38 if(!dbconn) {
39 free(ahi);
40 return;
41 }
42
ee8cd7d0 43 pgres=dbgetresult(dbconn);
6b2ca929 44
ee8cd7d0 45 if (!dbquerysuccessful(pgres)) {
6b2ca929 46 Error("chanserv", ERR_ERROR, "Error loading auth history data.");
c34af51f 47 free(ahi);
6b2ca929 48 return;
49 }
50
ee8cd7d0 51 if (dbnumfields(pgres) != 7) {
6b2ca929 52 Error("chanserv", ERR_ERROR, "Auth history data format error.");
ee8cd7d0 53 dbclear(pgres);
c34af51f 54 free(ahi);
6b2ca929 55 return;
56 }
57
6b2ca929 58 if (!np) {
ee8cd7d0 59 dbclear(pgres);
6b2ca929 60 free(ahi);
61 return;
62 }
63
64 if (!(rup=getreguserfromnick(np))) {
ee8cd7d0 65 dbclear(pgres);
6b2ca929 66 free(ahi);
67 return;
68 }
b7eebdcc 69 chanservstdmessage(np, QM_AUTHHISTORYHEADER); /* @TIMELEN */
ee8cd7d0
CP
70 while(dbfetchrow(pgres)) {
71 if (!UHasHelperPriv(rup) && (strtoul(dbgetvalue(pgres, 0), NULL, 10) != rup->ID)) {
72 dbclear(pgres);
6b2ca929 73 free(ahi);
74 return;
75 }
ee8cd7d0
CP
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);
b7eebdcc
CP
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 */
6b2ca929 88 }
89 chanservstdmessage(np, QM_ENDOFLIST);
90
ee8cd7d0 91 dbclear(pgres);
6b2ca929 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;
b3565978 101 q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi,
522e2b8c 102 "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where "
6b2ca929 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
6a127402
CP
117 /* if target != command issuer */
118 if (trup != rup) {
119 /* only opers and helpers can view authhistory of other users */
120 if (!UHasHelperPriv(rup)) {
121 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[0]);
122 return CMD_ERROR;
123 }
124
125 /* and only opers can view opers history */
126 if (UHasOperPriv(trup) && !UHasOperPriv(rup)) {
127 chanservwallmessage("%s (%s) just FAILED using AUTHHISTORY on %s", sender->nick, rup->username, trup->username);
128 chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[0]);
129 return CMD_ERROR;
130 }
131
132 /* checks passed */
133
134 chanservwallmessage("%s (%s) used AUTHHISTORY on %s", sender->nick, rup->username, trup->username);
6b2ca929 135 }
1482fb78 136 } else {
6b2ca929 137 trup=rup;
1482fb78 138 }
6b2ca929 139 csdb_retreiveauthhistory(sender, trup, 10);
140
141 return CMD_OK;
142}