-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanon.js
executable file
·356 lines (315 loc) · 9.09 KB
/
anon.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env node
const fs = require("fs");
const Twit = require("twit");
const async = require("async");
const minimist = require("minimist");
const Mastodon = require("mastodon");
const Mustache = require("mustache");
const puppeteer = require("puppeteer");
const { WikiChanges } = require("wikichanges");
const { Address4, Address6 } = require("ip-address");
const argv = minimist(process.argv.slice(2), {
default: {
verbose: false,
config: "./config.json",
},
});
function log(msg, data) {
return console.log(`[${new Date().toLocaleString()}] ${msg}`, data ? data : "");
}
function address(ip) {
if (Array.from(ip).includes(":")) {
return new Address6(ip);
} else {
const i = new Address4(ip);
const subnetMask = 96 + i.subnetMask;
const newIp = `::ffff:${i.toGroup6()}/${subnetMask}`;
return new Address6(newIp);
}
}
function ipToInt(ip) {
return address(ip).bigInteger();
}
function compareIps(ip1, ip2) {
const r = ipToInt(ip1).compareTo(ipToInt(ip2));
if (r === 0) {
return 0;
} else if (r > 0) {
return 1;
} else {
return -1;
}
}
function isIpInRange(ip, block) {
if (Array.isArray(block)) {
return compareIps(ip, block[0]) >= 0 && compareIps(ip, block[1]) <= 0;
} else {
const a = address(ip);
const b = address(block);
return a.isInSubnet(b);
}
}
function isIpInAnyRange(ip, blocks) {
for (let block of Array.from(blocks)) {
if (isIpInRange(ip, block)) {
return true;
}
}
return false;
}
function getConfig(path) {
const config = loadJson(path);
// see if ranges are externally referenced as a separate .json files
if (config.accounts) {
for (let account of Array.from(config.accounts)) {
if (typeof account.ranges === "string") {
account.ranges = loadJson(account.ranges);
}
}
}
log("loaded config from", path);
return config;
}
function loadJson(path) {
if (path[0] !== "/" && path.slice(0, 2) !== "./") {
path = `./${path}`;
}
return require(path);
}
function getStatusLength(edit, name, template) {
// https://support.twitter.com/articles/78124-posting-links-in-a-tweet
const fakeUrl = "https://t.co/BzHLWr31Ce";
const status = Mustache.render(template, {
name,
url: fakeUrl,
page: edit.page,
});
return status.length;
}
function getStatus(edit, name, template) {
let page = edit.page;
const len = getStatusLength(edit, name, template);
if (len > 280) {
const newLength = edit.page.length - (len - 279);
page = edit.page.slice(0, +newLength + 1 || undefined);
}
return Mustache.render(template, {
name,
url: edit.url,
page,
});
}
const lastChange = {};
function isRepeat(edit) {
const k = `${edit.wikipedia}`;
const v = `${edit.page}:${edit.user}`;
const r = lastChange[k] === v;
lastChange[k] = v;
return r;
}
async function takeScreenshot(url) {
const browser = await puppeteer.launch({
headless: true,
defaultViewport: null,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
// enable this for Raspberry Pi
// executablePath: "chromium-browser",
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: "networkidle2" });
await page.setViewport({ width: 1024, height: 768 });
const filename = Date.now() + ".png";
const selector = "table.diff.diff-contentalign-left";
const rect = await page.evaluate((selector) => {
const element = document.querySelector(selector);
const { x, y, width, height } = element.getBoundingClientRect();
return { left: x, top: y, width, height, id: element.id };
}, selector);
await page.screenshot({
path: filename,
clip: {
x: rect.left,
y: rect.top,
width: rect.width,
height: rect.height,
},
});
await browser.close();
return filename;
}
async function sendStatus(account, status, edit) {
log(status);
if (!argv.noop && (!account.throttle || !isRepeat(edit))) {
const screenshot = await takeScreenshot(edit.url);
// Mastodon
if (account.mastodon) {
const mastodon = new Mastodon(account.mastodon);
const altText = "Screenshot of edit to " + edit.page;
const response = await mastodon.post("media", {
file: fs.createReadStream(screenshot),
description: altText,
});
if (!response.data.id) {
log("error uploading screenshot to mastodon");
return;
}
mastodon
.post("statuses", {
status: status,
media_ids: [response.data.id],
})
.catch((err) => log("error posting to mastodon", err));
}
// Twitter
if (account.access_token) {
const twitter = new Twit(account);
const b64content = fs.readFileSync(screenshot, { encoding: "base64" });
// upload the screenshot to twitter
twitter.post("media/upload", { media_data: b64content }, function (err, data) {
if (err) {
log(err);
return;
}
// add alt text for the media, for use by screen readers
const mediaIdStr = data.media_id_string;
const altText = "Screenshot of edit to " + edit.page;
const metaParams = {
media_id: mediaIdStr,
alt_text: { text: altText },
};
twitter.post("media/metadata/create", metaParams, function (err) {
if (err) {
log("metadata upload for twitter screenshot alt text failed with error", err);
}
const params = {
status: status,
media_ids: [mediaIdStr],
};
twitter.post("statuses/update", params, function (err) {
if (err) {
log(err);
}
});
fs.unlinkSync(screenshot);
});
});
}
}
}
function inspect(account, edit) {
if (edit.url) {
sendRestartMsg(account, edit);
if (account.whitelist && account.whitelist[edit.wikipedia] && account.whitelist[edit.wikipedia][edit.page]) {
const status = getStatus(edit, edit.user, account.template);
sendStatus(account, status, edit);
} else if (account.ranges && edit.anonymous) {
for (let name in account.ranges) {
const ranges = account.ranges[name];
if (isIpInAnyRange(edit.user, ranges)) {
const status = getStatus(edit, name, account.template);
sendStatus(account, status, edit);
}
}
}
}
}
let SHOULD_SEND_TEST = true;
async function sendRestartMsg(account, edit) {
if (argv.noop) return;
if (!SHOULD_SEND_TEST) return;
if (!edit.url.startsWith("https://nl")) return;
SHOULD_SEND_TEST = false;
if (account.mastodon) {
const screenshot = await takeScreenshot(edit.url);
const mastodon = new Mastodon(account.mastodon);
const altText = "Screenshot of edit to " + edit.url;
const response = await mastodon.post("media", {
file: fs.createReadStream(screenshot),
description: altText,
});
if (!response.data.id) {
log("error uploading screenshot to mastodon");
return;
}
mastodon
.post("statuses", {
status: "@vnglst@hachyderm.io I just restarted. Here's a test screenshot of a recent edit.",
visibility: "direct",
media_ids: [response.data.id],
})
.catch((err) => log("error posting to mastodon", err));
log("sent Mastodon test message");
fs.unlinkSync(screenshot);
}
if (account.access_token) {
const twitter = new Twit(account);
twitter.post("statuses/update", { status: "@vnglst I just restarted. Should be fine." }, function (err) {
if (err) {
log(err);
} else {
log("sent Twitter test message");
}
});
}
}
function checkConfig(config, error) {
if (config.accounts) {
return async.each(config.accounts, canTweet, error);
} else {
return error("missing accounts stanza in config");
}
}
function canTweet(account, error) {
if (!account.access_token) {
error(null);
} else {
try {
const twitter = new Twit(account);
const a = account["access_token"];
return twitter.get("search/tweets", { q: "cats" }, function (err, data, response) {
if (err) {
error(err + " for access_token " + a);
} else if (
!response.headers["x-access-level"] ||
response.headers["x-access-level"].substring(0, 10) !== "read-write"
) {
error(`no read-write permission for access token ${a}`);
} else {
error(null);
}
});
} catch (err) {
error(`unable to create twitter client for account: ${account}`);
}
}
}
function main() {
const config = getConfig(argv.config);
return checkConfig(config, function (err) {
if (!err) {
const wikipedia = new WikiChanges({ ircNickname: config.nick });
return wikipedia.listen((edit) => {
if (argv.verbose) {
log(JSON.stringify(edit, null, 4));
}
Array.from(config.accounts).map((account) => inspect(account, edit));
});
} else {
return log(err);
}
});
}
if (require.main === module) {
main();
}
module.exports = {
main,
address,
ipToInt,
compareIps,
isIpInRange,
isIpInAnyRange,
getConfig,
getStatus,
takeScreenshot,
};