]>
Commit | Line | Data |
---|---|---|
6b2ca929 | 1 | /* Automatically generated by refactor.pl. |
2 | * | |
3 | * | |
4 | * CMDNAME: authhistory | |
5 | * CMDLEVEL: QCMD_AUTHED | |
a49f1229 | 6 | * CMDARGS: 2 |
6b2ca929 | 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; | |
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 | ||
95 | void csdb_retreiveauthhistory(nick *np, reguser *rup, int limit) { | |
96 | struct authhistoryinfo *ahi; | |
a49f1229 | 97 | char limitstr[30]; |
98 | ||
99 | if (limit) { | |
100 | sprintf(limitstr, " limit %d",limit); | |
101 | } else { | |
102 | limitstr[0]='\0'; | |
103 | } | |
6b2ca929 | 104 | |
105 | ahi=(struct authhistoryinfo *)malloc(sizeof(struct authhistoryinfo)); | |
106 | ahi->numeric=np->numeric; | |
107 | ahi->userID=rup->ID; | |
b3565978 | 108 | q9a_asyncquery(csdb_doauthhistory_real, (void *)ahi, |
522e2b8c | 109 | "SELECT userID, nick, username, host, authtime, disconnecttime, quitreason from chanserv.authhistory where " |
a49f1229 | 110 | "userID=%u order by authtime desc%s", rup->ID, limitstr); |
6b2ca929 | 111 | } |
112 | ||
113 | int csa_doauthhistory(void *source, int cargc, char **cargv) { | |
114 | reguser *rup, *trup; | |
115 | nick *sender=source; | |
a49f1229 | 116 | unsigned int arg=0; |
117 | unsigned int limit=10; | |
118 | ||
6b2ca929 | 119 | if (!(rup=getreguserfromnick(sender))) |
120 | return CMD_ERROR; | |
121 | ||
a49f1229 | 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]))) | |
6b2ca929 | 133 | return CMD_ERROR; |
134 | ||
6a127402 CP |
135 | /* if target != command issuer */ |
136 | if (trup != rup) { | |
137 | /* only opers and helpers can view authhistory of other users */ | |
138 | if (!UHasHelperPriv(rup)) { | |
063c7d26 | 139 | chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]); |
6a127402 CP |
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); | |
063c7d26 | 146 | chanservstdmessage(sender, QM_NOACCESSONUSER, "authhistory", cargv[arg]); |
6a127402 CP |
147 | return CMD_ERROR; |
148 | } | |
149 | ||
150 | /* checks passed */ | |
6a127402 | 151 | chanservwallmessage("%s (%s) used AUTHHISTORY on %s", sender->nick, rup->username, trup->username); |
6b2ca929 | 152 | } |
1482fb78 | 153 | } else { |
6b2ca929 | 154 | trup=rup; |
1482fb78 | 155 | } |
063c7d26 | 156 | |
a49f1229 | 157 | csdb_retreiveauthhistory(sender, trup, limit); |
6b2ca929 | 158 | |
159 | return CMD_OK; | |
160 | } |