-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (239 loc) · 8.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
require("dotenv").config();
const puppeteer = require("puppeteer");
const SELECTORS = require("./selectors");
const parse = require("date-fns/parse");
const isValid = require("date-fns/is_valid");
const isBefore = require("date-fns/is_before");
const format = require("date-fns/format");
const addDays = require("date-fns/add_days");
const isDate = require("date-fns/is_date");
const dateFormat = "YYYY-MM-DD";
const maxRetries = 3;
const navigationTimeout = parseInt(process.env.NAVIGATION_TIMEOUT_MS, 10);
const maxSimultaneousDeletions = parseInt(
process.env.MAX_SIMULTANEOUS_DELETIONS,
10
);
const headlessMode = process.env.HEADLESS_MODE === "true";
const letters = "abcdefghijklmnopqrstuvwxyz"; // Spambots don't seem to bother with capital letters. Meh :)
(async () => {
const browser = await puppeteer.launch({ headless: headlessMode });
const page = await browser.newPage();
await login(page);
const userSid = await getQueryParam(page, "sid");
console.log(`Logged into forum. sid: ${userSid}`);
await loginToAdmin(page, userSid);
const adminSid = await getQueryParam(page, "sid");
console.log(`Logged into ACP. sid: ${adminSid}`);
const joinedFrom = parse(process.env.JOINED_BEFORE_START);
const joinedEnd = parse(process.env.JOINED_BEFORE_END);
const totalDeleted = await loopPruneUsers(
page,
adminSid,
joinedFrom,
joinedEnd
);
console.log(`Done! Deleted ${totalDeleted} users.`);
await browser.close();
process.on("SIGINT", async () => {
console.log("Closing Chrome manually.");
try {
await browser.close();
process.exit();
} catch (e) {
console.log(e);
process.exit();
}
});
})();
/**
* Login to the forum (as a regular user)
*/
async function login(page) {
await page.goto(`${process.env.FORUM_URL}/ucp.php?mode=login`);
await page.click(SELECTORS.LOGIN_NAME);
await page.keyboard.type(process.env.USERNAME);
await page.click(SELECTORS.LOGIN_PASSWORD);
await page.keyboard.type(process.env.PASSWORD);
return await clickToSubmit(page, SELECTORS.LOGIN_SUBMIT);
}
/**
* Access the ACP.
*
* @param {*} page Puppeteer object.
* @param {string} sid Access token generated by phpbb after login.
*/
async function loginToAdmin(page, sid) {
await page.goto(`${process.env.FORUM_URL}/adm/index.php?sid=${sid}`, {
timeout: 60000
});
await page.click(SELECTORS.ACP_PASSWORD);
await page.keyboard.type(process.env.PASSWORD);
await clickToSubmit(page, SELECTORS.ACP_SUBMIT);
}
/**
* Repeatedly submit the "Prune" form for each day between the given range.
*
* @param {*} page Puppeteer object.
* @param {string} sid Admin access token generated by phpbb after ACP login.
* @param {Date} startDate First "Joined Before" date to start with.
* @param {Date} endDate Final "Joined Before" date to end with (i.e. no users who joined after this will be deleted)
*/
async function loopPruneUsers(page, sid, startDate, endDate) {
if (!isValid(startDate) || !isValid(endDate)) {
return console.error(
"Your joined from start or end dates were invalid. Quitting."
);
}
if (!isBefore(startDate, endDate)) {
return console.error(
"Your joined from start date must be before the end date. Quitting."
);
}
console.log(`Ready to prune users.`);
console.log(`Joined from: ${startDate}`);
console.log(`Joined to: ${endDate}`);
console.log("----------");
let joinDate = startDate;
let totalDeleted = 0;
let retryCounter = 0; // Prevent infinite retry loops.
let nameFilter = -1; // Index in "letters" string.
while (isBefore(joinDate, endDate) && retryCounter < maxRetries) {
try {
let username;
// Limit today's result set by "username" filter?
if (nameFilter >= 0) {
// Cycled through all used letters? Attempt to clear anything remaining.
if (nameFilter >= letters.length) {
username = undefined;
} else {
username = letters.charAt(nameFilter) + "********";
}
}
let deleted = await pruneUsers(page, sid, joinDate, username);
totalDeleted += deleted;
retryCounter = 0; // Reset counter on success.
// Did we delete all users for today? No if either:
// - We were limited by the batch size
// - We were using "username" to limit today's results
if (deleted !== maxSimultaneousDeletions) {
if (username) {
nameFilter++;
} else {
nameFilter = -1; // Reset counter for new day.
joinDate = addDays(joinDate, 1);
}
}
} catch (e) {
console.log(e);
console.log(`Prune failed. Re-trying for ${joinDate}`);
retryCounter++;
nameFilter++; // Limit the number of results in the next batch.
}
}
return totalDeleted;
}
/**
* Start the user prune process.
*
* @param {*} page Puppeteer object.
* @param {string} sid Admin access token generated by phpbb after ACP login.
* @param {date} joinedBefore Date value to use in "Joined Before" field
* @param {string} username Optional. Search value to use in "Username" field
* @returns {number} Of users deleted.
*/
async function pruneUsers(page, sid, joinedBefore, username) {
await page.goto(
`${process.env.FORUM_URL}/adm/index.php?sid=${sid}&i=acp_prune&mode=users`
);
const joinedBeforeValue = format(joinedBefore, dateFormat);
console.log(
`Preparing to prune users who joined before: ${joinedBeforeValue}`
);
if (username) {
console.log(`Username matching: ${username}`);
await page.click(SELECTORS.USERNAME);
await page.keyboard.type(username);
}
await page.click(SELECTORS.JOINED_BEFORE);
await page.keyboard.type(joinedBeforeValue);
await page.click(SELECTORS.POST_COUNT);
await page.keyboard.type(process.env.POST_COUNT);
await page.click(SELECTORS.DELETE_POSTS);
await page.click(SELECTORS.DELETE_USERS);
await clickToSubmit(page, SELECTORS.PRUNE_SUBMIT);
// If no users matched the query, there is nothing to do.
const error = await page.$(SELECTORS.ERROR_BOX);
if (error) {
console.log("No users to delete.");
return 0;
}
// How many users will be deleted?
await page.waitForSelector(SELECTORS.PRUNE_USER_RESULTS);
const usersToDelete = await page.$$(SELECTORS.PRUNE_USER_RESULTS);
let numberToDelete = usersToDelete.length;
// Are there so many that we'd rather split by letter?
if (!username && numberToDelete > 1500) {
throw `Deliberately choose to split by letter instead. Total: ${numberToDelete}`;
}
// Are there more results than we can delete in 1 batch?
// If so, de-select any past the batch limit.
if (numberToDelete > maxSimultaneousDeletions) {
console.log(
`Too many results, de-selecting extras: ${numberToDelete -
maxSimultaneousDeletions}`
);
for (let i = maxSimultaneousDeletions; i < usersToDelete.length; i++) {
await usersToDelete[i].click();
}
numberToDelete = maxSimultaneousDeletions;
}
console.log(`Deleting ${numberToDelete} users`);
// Confirm.
try {
await clickToSubmit(page, SELECTORS.PRUNE_CONFIRM_SUBMIT);
const success = await page.$(SELECTORS.SUCCESS_BOX);
if (!success) {
throw "Deletion failed.";
}
} catch (e) {
throw "Failed to submit deletion form.";
}
return numberToDelete;
}
/**
* Get a query param from the current URL.
*
* @param {*} page Puppeteer object.
* @param {string} name Name of the param to retrieve.
*/
async function getQueryParam(page, name) {
return page.$eval(
"body",
(body, name) => {
const url = location.search;
name = name.replace(/[\[\]]/g, "\\$&");
const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
},
name
);
}
/**
* Click to submit a form and wait for navigation to succeed.
*
* @param {*} page Puppeteer object.
* @param {string} selector Selector for the button to clicl.
*/
async function clickToSubmit(page, selector) {
await page.waitForSelector(selector);
return await Promise.all([
page.waitForNavigation({
timeout: navigationTimeout
}),
page.click(selector)
]);
}