2020-07-03 17:16:41 +03:00
|
|
|
const { JSDOM } = require('jsdom');
|
2021-02-01 11:58:59 +03:00
|
|
|
const qs = require('qs');
|
2020-05-26 21:06:34 +03:00
|
|
|
|
|
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
|
|
|
2020-05-27 01:36:34 +03:00
|
|
|
const editNoticeHTML = (client, roomId, event, html, plain) => client.sendMessage(roomId, {
|
2020-05-26 21:06:34 +03:00
|
|
|
body: ` * ${plain || html.replace(/<[^<]+?>/g, '')}`,
|
|
|
|
formatted_body: ` * ${html}`,
|
|
|
|
format: 'org.matrix.custom.html',
|
|
|
|
msgtype: 'm.notice',
|
|
|
|
'm.new_content': {
|
|
|
|
body: plain || html.replace(/<[^<]+?>/g, ''),
|
|
|
|
formatted_body: html,
|
|
|
|
format: 'org.matrix.custom.html',
|
|
|
|
msgtype: 'm.notice'
|
|
|
|
},
|
|
|
|
'm.relates_to': {
|
|
|
|
rel_type: 'm.replace',
|
2020-05-27 01:36:34 +03:00
|
|
|
event_id: event.event_id
|
2020-05-26 21:06:34 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const headers = ({ domain, userAgent }) => ({
|
|
|
|
'Host': `${domain}`,
|
|
|
|
'User-Agent': `${userAgent}`
|
|
|
|
});
|
|
|
|
|
2020-06-23 21:56:08 +03:00
|
|
|
const archive = async (instance, url, rearchive) => {
|
2020-05-26 21:06:34 +03:00
|
|
|
const form = await instance({ method: 'GET', url: '/' });
|
|
|
|
if (form.statusText !== 'OK') throw form;
|
|
|
|
const submitId = form.data.match(/name="submitid" value="([^"]+)/);
|
|
|
|
const submit = await instance({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/submit/',
|
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
2020-06-23 21:56:08 +03:00
|
|
|
data: qs.stringify({ anyway: rearchive ? '1' : undefined, submitid: submitId ? submitId[1] : undefined, url })
|
2020-05-26 21:06:34 +03:00
|
|
|
});
|
2020-07-03 17:16:41 +03:00
|
|
|
submit.title = new JSDOM(submit.data).window.document.title;
|
2020-05-26 21:06:34 +03:00
|
|
|
if (submit.statusText !== 'OK') throw submit;
|
|
|
|
if (submit.request.path !== '/submit/')
|
2020-07-03 17:16:41 +03:00
|
|
|
return { id: submit.request.path, date: submit.headers['memento-datetime'], title: submit.title };
|
2020-05-26 21:06:34 +03:00
|
|
|
if (submit.headers.refresh)
|
|
|
|
return { refresh: submit.headers.refresh.split(';url=')[1] };
|
|
|
|
throw submit;
|
|
|
|
};
|
|
|
|
|
|
|
|
const reqStr = str => `<em>Sending archive request for <code>${str}</code></em>`;
|
|
|
|
const arc1Str = str => `<em>Archiving page <code>${str}</code></em>`;
|
2020-07-03 17:16:41 +03:00
|
|
|
const arc2Str = (str, title, date) => `<em>Archived page <code><a href="https://${str}">${str}</code> [${date}]</em><br /><b>${title}</b>`;
|
2020-05-26 21:06:34 +03:00
|
|
|
const arc3Str = str => `<em>Timed out <code>${str}</code></em>`;
|
|
|
|
|
2021-02-01 11:58:59 +03:00
|
|
|
const run = async (matrixClient, { roomId }, userInput, rearchive) => {
|
2020-05-26 21:06:34 +03:00
|
|
|
const instance = axios.create({
|
2021-02-01 11:58:59 +03:00
|
|
|
baseURL: `https://${config.archive.domain}`,
|
|
|
|
headers: headers(config.archive),
|
2020-05-26 21:06:34 +03:00
|
|
|
transformResponse: [],
|
|
|
|
timeout: 10 * 1000
|
|
|
|
});
|
2020-07-03 17:16:41 +03:00
|
|
|
|
2020-05-26 21:06:34 +03:00
|
|
|
let reply = null;
|
|
|
|
try {
|
|
|
|
reply = await matrixClient.sendHtmlNotice(roomId, '', reqStr(userInput));
|
2020-07-03 17:16:41 +03:00
|
|
|
const { refresh, id, title, date } = await archive(instance, userInput, rearchive);
|
2020-05-26 21:06:34 +03:00
|
|
|
if (id)
|
2021-02-01 11:58:59 +03:00
|
|
|
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.archive.domain}${id}`, title, date));
|
2020-05-26 21:06:34 +03:00
|
|
|
if (refresh) {
|
2021-02-01 11:58:59 +03:00
|
|
|
const path = refresh.split(`https://${config.archive.domain}`);
|
2020-05-26 21:06:34 +03:00
|
|
|
if (!path[1]) throw refresh;
|
|
|
|
await editNoticeHTML(matrixClient, roomId, reply, arc1Str(refresh));
|
|
|
|
let tries = 30;
|
|
|
|
while (tries--) {
|
|
|
|
await sleep(10000);
|
2020-07-07 23:54:05 +03:00
|
|
|
const { title, date, id } = await archive(instance, userInput);
|
2020-07-03 17:16:41 +03:00
|
|
|
if (rearchive == false && title !== undefined)
|
2021-02-01 11:58:59 +03:00
|
|
|
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.archive.domain}${id}`, title, date));
|
2020-07-03 17:16:41 +03:00
|
|
|
const { request: { path: reqPath }, headers: { 'memento-datetime': rearchiveDate } } = await instance({ method: 'HEAD', url: path[1] })
|
|
|
|
.catch(e => ({ request: { path: path[1] } }));
|
|
|
|
if (rearchive == true && reqPath !== path[1])
|
2021-02-01 11:58:59 +03:00
|
|
|
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.archive.domain}${reqPath}`, title, rearchiveDate));
|
2020-05-26 21:06:34 +03:00
|
|
|
}
|
|
|
|
return await editNoticeHTML(matrixClient, roomId, reply, arc3Str(refresh));
|
|
|
|
}
|
|
|
|
throw 'sad';
|
|
|
|
} catch (e) {
|
|
|
|
const sad = `<strong>Sad!</strong><br /><code>${`${e}`.replace(/<[^<]+?>/g, '').substr(0, 100)}</code>`;
|
|
|
|
if (reply)
|
|
|
|
editNoticeHTML(matrixClient, roomId, reply, sad, 'sad').catch(() => {});
|
|
|
|
else
|
|
|
|
matrixClient.sendHtmlNotice(roomId, 'sad', sad).catch(() => {});
|
|
|
|
}
|
2020-07-03 17:16:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.runQuery = run;
|