]>
Commit | Line | Data |
---|---|---|
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 | |
23 | struct authhistoryinfo { | |
24 | unsigned int numeric; | |
25 | unsigned int userID; | |
26 | }; | |
27 | ||
ee8cd7d0 | 28 | void 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; | |
6b2ca929 | 36 | struct tm *tmp; |
37 | char tbuf1[15], tbuf2[15], uhbuf[51]; | |
38 | ||
b3565978 CP |
39 | if(!dbconn) { |
40 | free(ahi); | |
41 | return; | |
42 | } | |
43 | ||
ee8cd7d0 | 44 | pgres=dbgetresult(dbconn); |
6b2ca929 | 45 | |
ee8cd7d0 | 46 | if (!dbquerysuccessful(pgres)) { |
6b2ca929 | 47 | Error("chanserv", ERR_ERROR, "Error loading auth history data."); |
c34af51f | 48 | free(ahi); |
6b2ca929 | 49 | return; |
50 | } | |
51 | ||
ee8cd7d0 | 52 | if (dbnumfields(pgres) != 7) { |
6b2ca929 | 53 | Error("chanserv", ERR_ERROR, "Auth history data format error."); |
ee8cd7d0 | 54 | dbclear(pgres); |
c34af51f | 55 | free(ahi); |
6b2ca929 | 56 | return; |
57 | } | |
58 | ||
6b2ca929 | 59 | if (!np) { |
ee8cd7d0 | 60 | dbclear(pgres); |
6b2ca929 | 61 | free(ahi); |
62 | return; | |
63 | } | |
64 | ||
65 | if (!(rup=getreguserfromnick(np))) { | |
ee8cd7d0 | 66 | dbclear(pgres); |
6b2ca929 | 67 | free(ahi); |
68 | return; | |
69 | } | |
70 | chanservstdmessage(np, QM_AUTHHISTORYHEADER); | |
ee8cd7d0 CP |
71 | while(dbfetchrow(pgres)) { |
72 | if (!UHasHelperPriv(rup) && (strtoul(dbgetvalue(pgres, 0), NULL, 10) != rup->ID)) { | |
73 | dbclear(pgres); | |
6b2ca929 | 74 | free(ahi); |
75 | return; | |
76 | } | |
ee8cd7d0 CP |
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); | |
79313d98 CP |
82 | tmp=gmtime(&ahauthtime); |
83 | strftime(tbuf1, sizeof(tbuf1), Q9_FORMAT_TIME, tmp); | |
6b2ca929 | 84 | if (ahdisconnecttime) { |
79313d98 CP |
85 | tmp=gmtime(&ahdisconnecttime); |
86 | strftime(tbuf2, sizeof(tbuf2), Q9_FORMAT_TIME, tmp); | |
6b2ca929 | 87 | } |
88 | snprintf(uhbuf,50,"%s!%s@%s", ahnick, ahuser, ahhost); | |
ee8cd7d0 | 89 | chanservsendmessage(np, "#%-2d %-50s %-15s %-15s %s", ++count, uhbuf, tbuf1, ahdisconnecttime?tbuf2:"never", dbgetvalue(pgres,6)); |
6b2ca929 | 90 | } |
91 | chanservstdmessage(np, QM_ENDOFLIST); | |
92 | ||
ee8cd7d0 | 93 | dbclear(pgres); |
6b2ca929 | 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; | |
b3565978 | 103 | q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi, |
522e2b8c | 104 | "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where " |
6b2ca929 | 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))) { | |
1482fb78 | 121 | chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[0]); |
6b2ca929 | 122 | return CMD_ERROR; |
123 | } | |
1482fb78 | 124 | } else { |
6b2ca929 | 125 | trup=rup; |
1482fb78 | 126 | } |
127 | ||
6b2ca929 | 128 | csdb_retreiveauthhistory(sender, trup, 10); |
129 | ||
130 | return CMD_OK; | |
131 | } |