]>
Commit | Line | Data |
---|---|---|
183b8e2f P |
1 | /* Automatically generated by refactor.pl. |
2 | * | |
3 | * | |
4 | * CMDNAME: rollbackaccount | |
5 | * CMDLEVEL: QCMD_OPER | |
6 | * CMDARGS: 2 | |
7 | * CMDDESC: Roll back password/email changes on an account. | |
8 | * CMDFUNC: csa_dorollbackaccount | |
9 | * CMDPROTO: int csa_dorollbackaccount(void *source, int cargc, char **cargv); | |
10 | */ | |
11 | ||
12 | #include "../chanserv.h" | |
13 | #include "../../lib/irc_string.h" | |
6b2ca929 | 14 | #include "../../pqsql/pqsql.h" |
15 | ||
16 | #include <libpq-fe.h> | |
183b8e2f P |
17 | #include <stdio.h> |
18 | #include <string.h> | |
19 | ||
6b2ca929 | 20 | void csdb_dorollbackaccount_real(PGconn *dbconn, void *arg) { |
c54295ef | 21 | nick *np=getnickbynumeric((unsigned long)arg); |
6b2ca929 | 22 | reguser *rup; |
23 | unsigned int userID; | |
24 | char *oldpass, *newpass, *oldemail, *newemail; | |
25 | time_t changetime, authtime; | |
26 | PGresult *pgres; | |
27 | int i, num; | |
28 | ||
b3565978 CP |
29 | if(!dbconn) |
30 | return; | |
31 | ||
6b2ca929 | 32 | pgres=PQgetResult(dbconn); |
33 | ||
34 | if (PQresultStatus(pgres) != PGRES_TUPLES_OK) { | |
35 | Error("chanserv", ERR_ERROR, "Error loading account rollback data."); | |
36 | return; | |
37 | } | |
38 | ||
39 | if (PQnfields(pgres) != 7) { | |
40 | Error("chanserv", ERR_ERROR, "Account rollback data format error."); | |
c34af51f | 41 | PQclear(pgres); |
6b2ca929 | 42 | return; |
43 | } | |
44 | ||
45 | num=PQntuples(pgres); | |
46 | ||
47 | if (!np) { | |
48 | PQclear(pgres); | |
49 | return; | |
50 | } | |
51 | ||
52 | if (!(rup=getreguserfromnick(np)) || !UHasOperPriv(rup)) { | |
53 | Error("chanserv", ERR_ERROR, "No reguser pointer or oper privs in rollback account."); | |
54 | PQclear(pgres); | |
55 | return; | |
56 | } | |
57 | ||
58 | chanservsendmessage(np, "Attempting to rollback account %s:", rup->username); | |
59 | for (i=0; i<num; i++) { | |
60 | userID=strtoul(PQgetvalue(pgres, i, 0), NULL, 10); | |
61 | changetime=strtoul(PQgetvalue(pgres, i, 1), NULL, 10); | |
62 | authtime=strtoul(PQgetvalue(pgres, i, 2), NULL, 10); | |
63 | oldpass=PQgetvalue(pgres, i, 3); | |
64 | newpass=PQgetvalue(pgres, i, 4); | |
65 | oldemail=PQgetvalue(pgres, i, 5); | |
66 | newemail=PQgetvalue(pgres, i, 6); | |
67 | if (strlen(newpass) > 0) { | |
68 | setpassword(rup, oldpass); | |
69 | chanservsendmessage(np, "Restoring old password (%s -> %s)", newpass, oldpass); | |
70 | } | |
71 | else if (strlen(newemail) > 0) { | |
9708f78f | 72 | /* WARNING: lastemail untouched */ |
6b2ca929 | 73 | freesstring(rup->email); |
74 | rup->email=getsstring(oldemail, EMAILLEN); | |
75 | rup->lastemailchange=changetime; | |
76 | chanservsendmessage(np, "Restoring old email (%s -> %s)", newemail, oldemail); | |
77 | } | |
78 | } | |
79 | csdb_updateuser(rup); | |
80 | chanservstdmessage(np, QM_DONE); | |
81 | ||
82 | PQclear(pgres); | |
83 | } | |
84 | ||
85 | void csdb_rollbackaccounthistory(nick *np, reguser* rup, time_t starttime) { | |
b3565978 | 86 | q9u_asyncquery(csdb_dorollbackaccount_real, (void *)np->numeric, |
6b2ca929 | 87 | "SELECT userID, changetime, authtime, oldpassword, newpassword, oldemail, newemail from accounthistory where " |
88 | "userID=%u and changetime>%lu order by changetime desc limit 10", rup->ID, starttime); | |
89 | } | |
90 | ||
183b8e2f P |
91 | int csa_dorollbackaccount(void *source, int cargc, char **cargv) { |
92 | reguser *rup, *trup; | |
93 | nick *sender=source; | |
94 | time_t starttime=getnettime(); | |
95 | long duration; | |
96 | ||
97 | if (!(rup=getreguserfromnick(sender))) | |
98 | return CMD_ERROR; | |
99 | ||
100 | if (cargc < 2) { | |
101 | chanservstdmessage(sender, QM_NOTENOUGHPARAMS, "rollbackaccount"); | |
102 | return CMD_ERROR; | |
103 | } | |
104 | ||
105 | if (!(trup=findreguser(sender, cargv[0]))) | |
106 | return CMD_ERROR; | |
107 | ||
108 | if (UHasOperPriv(trup)) { | |
109 | chanservstdmessage(sender, QM_NOACCESS, "rollbackaccount", cargv[0]); | |
110 | return CMD_ERROR; | |
111 | } | |
112 | ||
113 | if (!(duration=durationtolong(cargv[1]))) { | |
114 | chanservsendmessage(sender, "Invalid duration."); | |
115 | return CMD_ERROR; | |
116 | } | |
117 | starttime-=duration; | |
118 | ||
119 | cs_log(sender,"ROLLBACKACCOUNT %s #%s %s", trup->username, rup->username, cargv[1]); | |
120 | ||
121 | csdb_rollbackaccounthistory(sender, trup, starttime); | |
122 | ||
123 | return CMD_OK; | |
124 | } |