feat(e2ee): introduce OLM encryption. refactor(storage): moved from fs to localstorage. refactor(config): added as global to registrar. chore(npm): upgraded matrix-js-sdk from 2.4.6 release to 9.5.1 release.

This commit is contained in:
vulet 2021-02-01 16:58:59 +08:00
parent 0cd373fb8a
commit 67b88f9c96
28 changed files with 264 additions and 1413 deletions

66
auth.js
View file

@ -1,41 +1,58 @@
const sdk = require('matrix-js-sdk');
const axios = require('axios');
const fs = require('fs');
const registrar = require('./registrar.js');
const { LocalStorageCryptoStore } = require('matrix-js-sdk/lib/crypto/store/localStorage-crypto-store');
module.exports.getMatrixToken = async () => {
matrixClient = sdk.createClient(registrar.config.matrix.domain);
matrixClient.loginWithPassword(registrar.config.matrix.user, registrar.config.matrix.password)
matrixClient = sdk.createClient(config.matrix.domain);
matrixClient.loginWithPassword(config.matrix.user, config.matrix.password)
.then((response) => {
registrar.matrix_auth.access_token = response.access_token;
registrar.matrix_auth.user_id = response.user_id;
fs.writeFileSync('matrix_auth.json', JSON.stringify(response, null, 2));
matrix_auth = {
user_id: response.user_id,
access_token: response.access_token,
device_id: response.device_id,
};
localStorage.setItem('matrix_auth', JSON.stringify(response, null, 2));
}).then(() => {
matrixTokenLogin();
})
.catch((e) => {
console.log(e);
});
};
matrixTokenLogin = async () => {
matrixClient = sdk.createClient({
baseUrl: config.matrix.domain,
accessToken: matrix_auth.access_token,
userId: matrix_auth.user_id,
deviceId: matrix_auth.device_id,
sessionStore: new sdk.WebStorageSessionStore(localStorage),
cryptoStore: new LocalStorageCryptoStore(localStorage),
});
matrixClient.initCrypto()
.then(() => {
if(!localStorage.getItem('crypto.device_data'))
return console.log(
'====================================================\n'+
'New OLM Encryption Keys created, please restart ligh7hau5.\n'+
'===================================================='
);
matrixClient.startClient();
});
};
module.exports.matrixTokenLogin = async () => {
matrixClient = sdk.createClient({
baseUrl: registrar.config.matrix.domain,
accessToken: registrar.matrix_auth.access_token,
userId: registrar.matrix_auth.user_id,
timelineSupport: true,
});
matrixClient.startClient();
};
module.exports.matrixTokenLogin = matrixTokenLogin;
module.exports.registerFediverseApp = async () => {
axios.post(`${registrar.config.fediverse.domain}/api/v1/apps`,
axios.post(`${config.fediverse.domain}/api/v1/apps`,
{
client_name: registrar.config.fediverse.client_name,
client_name: config.fediverse.client_name,
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
scopes: 'read write follow push',
})
.then((response) => {
axios.post(`${registrar.config.fediverse.domain}/oauth/token`,
axios.post(`${config.fediverse.domain}/oauth/token`,
{
username: registrar.config.fediverse.username,
password: registrar.config.fediverse.password,
username: config.fediverse.username,
password: config.fediverse.password,
client_id: response.data.client_id,
client_secret: response.data.client_secret,
scope: 'read write follow push',
@ -43,8 +60,7 @@ module.exports.registerFediverseApp = async () => {
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
})
.then((tokens) => {
registrar.fediverse_auth.access_token = tokens.data.access_token;
fs.writeFileSync('fediverse_auth.json', JSON.stringify(tokens.data, null, 2));
localStorage.setItem('fediverse_auth', JSON.stringify(tokens.data, null, 2));
})
.catch((e) => {
console.log(e);

View file

@ -1,7 +1,5 @@
const qs = require('qs');
const axios = require('axios');
const { JSDOM } = require('jsdom');
const registrar = require('../registrar.js');
const qs = require('qs');
const sleep = ms => new Promise(r => setTimeout(r, ms));
@ -51,11 +49,10 @@ const arc1Str = str => `<em>Archiving page <code>${str}</code></em>`;
const arc2Str = (str, title, date) => `<em>Archived page <code><a href="https://${str}">${str}</code> [${date}]</em><br /><b>${title}</b>`;
const arc3Str = str => `<em>Timed out <code>${str}</code></em>`;
const run = async (matrixClient, { roomId }, userInput, rearchive, registrar) => {
const config = registrar.config.archive;
const run = async (matrixClient, { roomId }, userInput, rearchive) => {
const instance = axios.create({
baseURL: `https://${config.domain}`,
headers: headers(config),
baseURL: `https://${config.archive.domain}`,
headers: headers(config.archive),
transformResponse: [],
timeout: 10 * 1000
});
@ -65,9 +62,9 @@ const run = async (matrixClient, { roomId }, userInput, rearchive, registrar) =>
reply = await matrixClient.sendHtmlNotice(roomId, '', reqStr(userInput));
const { refresh, id, title, date } = await archive(instance, userInput, rearchive);
if (id)
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.domain}${id}`, title, date));
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.archive.domain}${id}`, title, date));
if (refresh) {
const path = refresh.split(`https://${config.domain}`);
const path = refresh.split(`https://${config.archive.domain}`);
if (!path[1]) throw refresh;
await editNoticeHTML(matrixClient, roomId, reply, arc1Str(refresh));
let tries = 30;
@ -75,11 +72,11 @@ const run = async (matrixClient, { roomId }, userInput, rearchive, registrar) =>
await sleep(10000);
const { title, date, id } = await archive(instance, userInput);
if (rearchive == false && title !== undefined)
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.domain}${id}`, title, date));
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.archive.domain}${id}`, title, date));
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])
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.domain}${reqPath}`, title, rearchiveDate));
return await editNoticeHTML(matrixClient, roomId, reply, arc2Str(`${config.archive.domain}${reqPath}`, title, rearchiveDate));
}
return await editNoticeHTML(matrixClient, roomId, reply, arc3Str(refresh));
}
@ -94,4 +91,3 @@ const run = async (matrixClient, { roomId }, userInput, rearchive, registrar) =>
};
exports.runQuery = run;

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, registrar) {
exports.runQuery = function (matrixClient, room) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
data: { status: `@10grans@fedi.cc beg` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/unfavourite`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses/${userInput}/unfavourite`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/favourite`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses/${userInput}/favourite`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/reblog`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses/${userInput}/reblog`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',

View file

@ -1,20 +1,18 @@
const axios = require('axios');
const fs = require('fs');
exports.runQuery = function (matrixClient, room, registrar) {
exports.runQuery = function (matrixClient, room) {
setInterval(() => {
axios({
method: 'GET',
url: `${registrar.config.fediverse.domain}/api/v1/timelines/home`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/timelines/home`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((events) => {
const event = fs.readFileSync('timeline.json', 'utf8');
fs.writeFileSync('timeline.json', events.data[0].created_at, 'utf8');
if (event !== events.data[0].created_at) {
let lastEvent = JSON.parse(localStorage.getItem('timeline'));
localStorage.setItem('timeline', JSON.stringify(events.data[0].created_at, null, 2));
if (lastEvent !== events.data[0].created_at) {
if (events.data[0].reblog === null) {
matrixClient.sendHtmlNotice(room.roomId,
'',
`<b><a href="${registrar.config.fediverse.domain}/notice/${events.data[0].id}">${events.data[0].account.acct}</a>
`<b><a href="${config.fediverse.domain}/notice/${events.data[0].id}">${events.data[0].account.acct}</a>
<blockquote><i>${events.data[0].content}</i><br>
${events.data[0].media_attachments.map(media =>
`<a href="${media.remote_url}">`+`${media.description}`+'</a>'
@ -24,9 +22,9 @@ exports.runQuery = function (matrixClient, room, registrar) {
} else {
matrixClient.sendHtmlNotice(room.roomId,
'',
`<b><a href="${registrar.config.fediverse.domain}/${events.data[0].account.id}">
`<b><a href="${config.fediverse.domain}/${events.data[0].account.id}">
${events.data[0].account.acct}</a>
<font color="#7886D7">has <a href="${registrar.config.fediverse.domain}/notice/${events.data[0].id}">repeated</a>:
<font color="#7886D7">has <a href="${config.fediverse.domain}/notice/${events.data[0].id}">repeated</a>:
<blockquote><a href="${events.data[0].reblog.account.url}">${events.data[0].reblog.account.acct}</a></blockquote>
<blockquote>${events.data[0].content}<br>
${events.data[0].media_attachments.map(media =>

View file

@ -1,17 +1,18 @@
const axios = require('axios');
const fediverse_auth = JSON.parse(localStorage.getItem('fediverse_auth'));
exports.runQuery = function (matrixClient, room, userInput, registrar) {
axios.get(`${registrar.config.fediverse.domain}/api/v1/accounts/${userInput}`).then((findUID) => {
exports.runQuery = function (matrixClient, room, userInput) {
axios.get(`${config.fediverse.domain}/api/v1/accounts/${userInput}`).then((findUID) => {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/follow`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/follow`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
})
.then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',
`Subscribed:
<blockquote>${registrar.config.fediverse.domain}/${response.data.id}`);
<blockquote>${config.fediverse.domain}/${response.data.id}`);
});
}).catch((e) => {
matrixClient.sendHtmlNotice(room.roomId,

View file

@ -1,5 +1,4 @@
const qs = require('qs');
const axios = require('axios');
const FormData = require('form-data');
const emojis = { public: '🌐', unlisted: '📝', private: '🔒️', direct: '✉️' };
@ -26,7 +25,7 @@ const mediaDownload = async (url, { whitelist, blacklist }) => {
};
};
const mediaUpload = async ({ domain }, { data, filename, mimetype }, registrar) => {
const mediaUpload = async ({ domain }, { data, filename, mimetype }) => {
const form = new FormData();
form.append('file', data, {
filename: filename || 'upload',
@ -35,24 +34,24 @@ const mediaUpload = async ({ domain }, { data, filename, mimetype }, registrar)
const upload = await axios({
method: 'POST',
url: `${domain}/api/v1/media`,
headers: form.getHeaders({ Authorization: `Bearer ${registrar.fediverse_auth.access_token}` }),
headers: form.getHeaders({ Authorization: `Bearer ${fediverse_auth.access_token}` }),
data: form,
});
if(upload.statusText !== 'OK') throw upload;
return upload.data.id;
};
const run = async (matrixClient, { roomId }, content, replyId, mediaURL, subject, registrar) => {
const run = async (matrixClient, { roomId }, content, replyId, mediaURL, subject) => {
let mediaId = null;
const fediverse = registrar.config.fediverse;
const fediverse = config.fediverse;
if(mediaURL) {
const media = await mediaDownload(mediaURL, registrar.config.fediverse.mimetypes);
mediaId = await mediaUpload(fediverse, media, registrar);
const media = await mediaDownload(mediaURL, fediverse.mimetypes);
mediaId = await mediaUpload(fediverse, media);
}
const response = await axios({
method: 'POST',
url: `${fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}`, 'Content-Type': 'application/x-www-form-urlencoded' },
headers: { Authorization: `Bearer ${fediverse_auth.access_token}`, 'Content-Type': 'application/x-www-form-urlencoded' },
data : qs.stringify({
status: content,
content_type: `text/markdown`,
@ -64,13 +63,13 @@ const run = async (matrixClient, { roomId }, content, replyId, mediaURL, subject
return matrixClient.sendHtmlNotice(roomId, '', `<a href="${response.data.url}">${response.data.id}</a>`);
}
exports.runQuery = async (client, room, userInput, registrar, { isReply, hasMedia, hasSubject }) => {
exports.runQuery = async (client, room, userInput, { isReply, hasMedia, hasSubject }) => {
try {
const chunks = userInput.trim().split(' ');
if(!chunks.length || chunks.length < !!isReply + !!hasMedia) throw '';
let replyId = null;
let mediaURL = null;
const subject = hasSubject ? registrar.config.fediverse.subject : null;
const subject = hasSubject ? fediverse.subject : null;
if(isReply) {
replyId = chunks[0];
chunks.shift();
@ -79,13 +78,13 @@ exports.runQuery = async (client, room, userInput, registrar, { isReply, hasMedi
let url = new URL(chunks[0]);
chunks.shift();
if(url.protocol === 'mxc:' && url.hostname && url.pathname)
url = new URL(`${registrar.config.matrix.domain}/_matrix/media/r0/download/${url.hostname}${url.pathname}`);
url = new URL(`${config.matrix.domain}/_matrix/media/r0/download/${url.hostname}${url.pathname}`);
if(url.protocol !== 'https:') throw '';
if(!registrar.config.matrix.domains.includes(url.hostname)) throw '';
if(!config.matrix.domains.includes(url.hostname)) throw '';
if(!/^\/_matrix\/media\/r0\/download\/[^/]+\/[^/]+\/?$/.test(url.pathname)) throw '';
mediaURL = url.toString();
}
return await run(client, room, chunks.join(' '), replyId, mediaURL, subject, registrar);
return await run(client, room, chunks.join(' '), replyId, mediaURL, subject);
} catch(e) {
return client.sendHtmlNotice(room.roomId, 'Sad!', `<strong>Sad!</strong>`).catch(()=>{});
}

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
data: {
status: `@mordekai ${userInput}`,
content_type: `text/markdown`,

View file

@ -1,49 +1,45 @@
const axios = require('axios');
const fs = require('fs');
exports.runQuery = function (matrixClient, room, registrar) {
exports.runQuery = function (matrixClient, room) {
setInterval(() => {
axios({
method: 'GET',
url: `${registrar.config.fediverse.domain}/api/v1/notifications`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
}).then((notifications) => {
const event = fs.readFileSync('notification.json', 'utf8');
fs.writeFileSync('notification.json', notifications.data[0].created_at, 'utf8');
if (event !== notifications.data[0].created_at) {
if (notifications.data[0].type === 'follow') {
url: `${config.fediverse.domain}/api/v1/notifications`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((events) => {
let lastEvent = JSON.parse(localStorage.getItem('notifications'));
localStorage.setItem('notifications', JSON.stringify(events.data[0].created_at, null, 2));
if (lastEvent !== events.data[0].created_at) {
if (events.data[0].type === 'follow') {
matrixClient.sendHtmlNotice(room.roomId,
'',
`<b><a href="${registrar.config.fediverse.domain}/${notifications.data[0].account.id}">
${notifications.data[0].account.acct}</a></b>
`<b><a href="${config.fediverse.domain}/${events.data[0].account.id}">
${events.data[0].account.acct}</a></b>
<font color="#03b381"><b>has followed you.</b></font>
<br><i>${notifications.data[0].account.note}</i>`);
} else if (notifications.data[0].type === 'favourite') {
<br><i>${events.data[0].account.note}</i>`);
} else if (events.data[0].type === 'favourite') {
matrixClient.sendHtmlNotice(room.roomId,
'',
`<b><a href="${registrar.config.fediverse.domain}/${notifications.data[0].account.id}">
${notifications.data[0].account.acct}</a></b>
<font color="#03b381"><b>has <a href="${notifications.data[0].status.uri}">favorited</a>
`<b><a href="${config.fediverse.domain}/${events.data[0].account.id}">
${events.data[0].account.acct}</a></b>
<font color="#03b381"><b>has <a href="${events.data[0].status.uri}">favorited</a>
your post:</b></font>
<br><blockquote><i><b>${notifications.data[0].status.content}</i></b></blockquote>`);
} else if (notifications.data[0].type === 'mention') {
<br><blockquote><i><b>${events.data[0].status.content}</i></b></blockquote>`);
} else if (events.data[0].type === 'mention') {
matrixClient.sendHtmlNotice(room.roomId,
'',
`<b><a href="${registrar.config.fediverse.domain}/${notifications.data[0].account.id}">
${notifications.data[0].account.acct}</a></b>
<font color="#03b381"><b>has <a href="${notifications.data[0].status.uri}">mentioned</a>
you:</b></font><br><blockquote><i><b>${notifications.data[0].status.content}
<br>(id: ${notifications.data[0].status.id}) ${registrar.media.visibilityEmoji(notifications.data[0].status.visibility)}</i></b>
`<b><a href="${config.fediverse.domain}/${events.data[0].account.id}">
${events.data[0].account.acct}</a></b>
<font color="#03b381"><b>has <a href="${events.data[0].status.uri}">mentioned</a>
you:</b></font><br><blockquote><i><b>${events.data[0].status.content}
<br>(id: ${events.data[0].status.id}) ${registrar.media.visibilityEmoji(events.data[0].status.visibility)}</i></b>
</blockquote>`);
} else if (notifications.data[0].type === 'reblog') {
} else if (events.data[0].type === 'reblog') {
matrixClient.sendHtmlNotice(room.roomId,
'',
`<b><a href="${registrar.config.fediverse.domain}/${notifications.data[0].account.id}">
${notifications.data[0].account.acct}</a></b>
<font color="#03b381"><b>has <a href="${notifications.data[0].status.uri}">repeated</a>
`<b><a href="${config.fediverse.domain}/${events.data[0].account.id}">
${events.data[0].account.acct}</a></b>
<font color="#03b381"><b>has <a href="${events.data[0].status.uri}">repeated</a>
your post:</b></font><br>
<blockquote><i><b>${notifications.data[0].status.content}</i></b></blockquote>`);
<blockquote><i><b>${events.data[0].status.content}</i></b></blockquote>`);
}
}
});

View file

@ -1,15 +1,13 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/pin`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses/${userInput}/pin`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',
`Pinned:
<blockquote><i><a href="${registrar.config.fediverse.domain}/notice/${response.data.id}">
<blockquote><i><a href="${config.fediverse.domain}/notice/${response.data.id}">
${response.data.content}</a></i>
</blockquote>`);
})

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
data: { status: userInput, content_type: `text/markdown` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'DELETE',
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses/${userInput}`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, address, flaggedInput, registrar) {
exports.runQuery = function (matrixClient, room, address, flaggedInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
data: { status: flaggedInput, in_reply_to_id: address, content_type: `text/markdown` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,

View file

@ -1,14 +1,12 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput, ) {
axios({
method: 'GET',
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses/${userInput}`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',
`<b><a href="${registrar.config.fediverse.domain}/notice/${response.data.id}">${response.data.account.acct}</a>
`<b><a href="${config.fediverse.domain}/notice/${response.data.id}">${response.data.account.acct}</a>
<blockquote><i>${response.data.content}<br>
${response.data.media_attachments.map(media =>
`<a href="${media.remote_url}"><b>${media.description}</b></a>`)

View file

@ -1,10 +1,8 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, address, flaggedInput, registrar) {
exports.runQuery = function (matrixClient, room, address, flaggedInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
data: { status: `@10grans@fedi.cc tip `+ flaggedInput + ` to `+address },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,

View file

@ -1,17 +1,15 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
axios.get(`${registrar.config.fediverse.domain}/api/v1/accounts/${userInput}`).then((findUID) => {
exports.runQuery = function (matrixClient, room, userInput) {
axios.get(`${config.fediverse.domain}/api/v1/accounts/${userInput}`).then((findUID) => {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/unfollow`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/accounts/${findUID.data.id}/unfollow`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
})
.then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',
`Unsubscribed:
<blockquote>${registrar.config.fediverse.domain}/${response.data.id}`);
<blockquote>${config.fediverse.domain}/${response.data.id}`);
});
}).catch((e) => {
matrixClient.sendHtmlNotice(room.roomId,

View file

@ -1,15 +1,13 @@
const axios = require('axios');
exports.runQuery = function (matrixClient, room, userInput, registrar) {
exports.runQuery = function (matrixClient, room, userInput) {
axios({
method: 'POST',
url: `${registrar.config.fediverse.domain}/api/v1/statuses/${userInput}/unpin`,
headers: { Authorization: `Bearer ${registrar.fediverse_auth.access_token}` },
url: `${config.fediverse.domain}/api/v1/statuses/${userInput}/unpin`,
headers: { Authorization: `Bearer ${fediverse_auth.access_token}` },
}).then((response) => {
matrixClient.sendHtmlNotice(room.roomId,
'',
`Unpinned:
<blockquote><i><a href="${registrar.config.fediverse.domain}/notice/${response.data.id}">
<blockquote><i><a href="${config.fediverse.domain}/notice/${response.data.id}">
${response.data.content}</a></i>
</blockquote>`);
})

View file

@ -2,11 +2,10 @@ exports.runQuery = function (matrixClient, room) {
matrixClient.sendHtmlNotice(room.roomId,
'',
'<blockquote><b>fediverse commands<br>'
+ '+post [your message] : post<br>'
+ '+plemara [your message] : post<br>'
+ '+redact [post id] : delete post<br>'
+ '+follow [user id] : follow<br>'
+ '+unfollow [user id] : unfollow<br>'
+ '+media [homeserver image URL or MXC] [optional message] : post media<br>'
+ '+copy [post id] : repeat/repost/retweet<br>'
+ '+reply [post id] [content] : reply to post<br>'
+ '+tip [@user@fedi.url] [amount] : tip 10grans<br>'
@ -19,8 +18,7 @@ exports.runQuery = function (matrixClient, room) {
+ '+archive [URL] : archive content<br>'
+ '+rearchive [URL] : re-archive content<br>'
+ '+nitter [status URL] : redirect twitter to nitter, also embed tweet<br>'
+ '+invidious [video URL] : redirect youtube to invidious, also embed description<br>'
+ '+proxy [twitter/youtube]: both +nitter and +invidious commands combined</b><br></blockquote>'
+ '+invidious [video URL] : redirect youtube to invidious, also embed description</b><br></blockquote>'
+ '<blockquote><b>--- <i>Contributors🐱</i> ---</b><br>'
+ '<b>CRYPTOMOONERS</b><br>'
+ '<b><i>docs by LINT</i></b></blockquote>');

View file

@ -1,5 +1,3 @@
const axios = require('axios');
const headers = ({ domain, userAgent }) => ({
'Host': `${domain}`,
'User-Agent': `${userAgent}`
@ -31,28 +29,28 @@ const card = (video, base, path) =>
`<br />(${video.date})</b> <br />
</blockquote>`;
const run = async (matrixClient, { roomId }, userInput, registrar) => {
const config = registrar.config.invidious;
const run = async (matrixClient, { roomId }, userInput) => {
const instance = axios.create({
baseURL: `https://${config.domain}/api/v1/videos/`,
headers: headers(config),
baseURL: `https://${config.invidious.domain}/api/v1/videos/`,
headers: headers(config.invidious),
transformResponse: [],
timeout: 10 * 1000
});
const video = await invidious(instance, userInput);
return await matrixClient.sendHtmlNotice(roomId, '', card(video, `https://${config.domain}`, userInput));
return await matrixClient.sendHtmlNotice(roomId, '', card(video, `https://${config.invidious.domain}`, userInput));
}
exports.runQuery = async (client, room, userInput, registrar) => {
exports.runQuery = async (client, room, userInput) => {
try {
const url = new URL(userInput);
if(!registrar.config.invidious.domains.includes(url.hostname)) throw '';
if(!config.invidious.domains.includes(url.hostname)) throw '';
if(/^\/[\w-]{11}$/.test(url.pathname))
return await run(client, room, url.pathname.slice(1), registrar);
return await run(client, room, url.pathname.slice(1));
const params = new URLSearchParams(url.search).get("v");
if(!/^[\w-]{11}$/.test(params)) throw '';
return await run(client, room, params, registrar);
return await run(client, room, params);
} catch(e) {
console.log(e);
return client.sendHtmlNotice(room.roomId, 'Sad!', `<strong>Sad!</strong>`).catch(()=>{});
}
};

View file

@ -1,4 +1,3 @@
const axios = require('axios');
const { JSDOM } = require("jsdom");
const headers = ({ domain, userAgent }) => ({
@ -50,24 +49,23 @@ const card = (tweet, base, check, path) =>
(tweet.hasAttachments ? '<blockquote><b>This tweet has attached media.</b></blockquote>' : '') +
(tweet.isReply ? tweet.isReply === 'unavailable' ? '<blockquote>Replied Tweet is unavailable</blockquote>' : `<blockquote><b><a href="${base}${tweet.isReply.path}">Replied Tweet</a></b><br /><b><i>${tweet.isReply.text.replace('\n', '<br />')}</i></b></blockquote>` : '') +
(tweet.quote ? `<blockquote><b><a href="${base}${tweet.quote.path}">Quoted Tweet</a></b><br /><b><i>${tweet.quote.text.replace('\n', '<br />')}</i></b></blockquote>` : '');
const run = async (matrixClient, { roomId }, userInput, registrar) => {
const config = registrar.config.nitter;
const run = async (matrixClient, { roomId }, userInput) => {
const instance = axios.create({
baseURL: `https://${config.domain}`,
headers: headers(config),
baseURL: `https://${config.nitter.domain}`,
headers: headers(config.nitter),
transformResponse: [],
timeout: 10 * 1000
});
const tweet = await nitter(instance, userInput);
return await matrixClient.sendHtmlNotice(roomId, '', card(tweet, `https://${config.domain}`, config.check, userInput));
return await matrixClient.sendHtmlNotice(roomId, '', card(tweet, `https://${config.nitter.domain}`, config.nitter.check, userInput));
}
exports.runQuery = async (client, room, userInput, registrar) => {
exports.runQuery = async (client, room, userInput) => {
try {
const url = new URL(userInput);
if(!registrar.config.nitter.domains.includes(url.hostname)) throw '';
if(!config.nitter.domains.includes(url.hostname)) throw '';
if(!/^\/[^/]+\/status\/\d+\/?$/.test(url.pathname)) throw '';
return await run(client, room, url.pathname, registrar);
return await run(client, room, url.pathname);
} catch(e) {
return client.sendHtmlNotice(room.roomId, 'Sad!', `<strong>Sad!</strong>`).catch(()=>{});
}

View file

@ -1,9 +0,0 @@
{
"access_token": "",
"created_at": 0,
"expires_in": 0,
"me": "",
"refresh_token": "",
"scope": "",
"token_type": ""
}

38
main.js
View file

@ -1,15 +1,15 @@
const registrar = require('./registrar.js');
const auth = require('./auth.js');
global.registrar = require('./registrar.js');
registrar.matrix_auth.access_token ? auth.matrixTokenLogin() : auth.getMatrixToken();
if (!registrar.fediverse_auth.access_token && registrar.config.fediverse.username) auth.registerFediverseApp();
matrix_auth.access_token ? auth.matrixTokenLogin() : auth.getMatrixToken();
if (!fediverse_auth.access_token && config.fediverse.username) auth.registerFediverseApp();
matrixClient.on('RoomMember.membership', (event, member) => {
if (member.membership === 'invite' && member.userId === matrixClient.credentials.userId) {
matrixClient.joinRoom(member.roomId).done(() => {
matrixClient.joinRoom(member.roomId).then(() => {
console.log('Auto-joined %s', member.roomId);
});
}
if (member.membership === 'leave' && member.userId === matrixClient.credentials.userId) {
matrixClient.forget(member.roomId).then(() => {
console.log('Kicked %s', member.roomId);
@ -17,14 +17,15 @@ matrixClient.on('RoomMember.membership', (event, member) => {
}
});
matrixClient.on('Room.timeline', (event, room, toStartOfTimeline) => {
matrixClient.on('Room.timeline', async function (event, room, member, toStartOfTimeline) {
if (toStartOfTimeline) return;
if (event.isEncrypted()) await event._decryptionPromise;
if (event.getType() !== 'm.room.message') return;
if (event.getSender() === matrixClient.credentials.userId) return;
if (event.event.unsigned.age > 10000) return;
if (event.event.content.body.charAt(0) === '+') {
console.log(`Logs: ${event.event.sender} - ${event.event.content.body}`);
let args = event.event.content.body.slice(1).trim().split(/ +/g);
if (event.getContent().body.charAt(0) === '+') {
console.log(`Logs: ${event.event.sender} - ${event.getContent().body}`);
let args = event.getContent().body.slice(1).trim().split(/ +/g);
let command = args.shift().toLowerCase();
const userInput = args.join(' ');
const flaggedInput = userInput.substr(userInput.indexOf(' ') + 1);
@ -35,19 +36,19 @@ matrixClient.on('Room.timeline', (event, room, toStartOfTimeline) => {
switch(command) {
case 'config':
return;
case 'help': case 'beg': case 'flood': case 'notify':
args.push(matrixClient, room, registrar);
case 'help': case 'beg': case 'flood': case 'asdf':
args.push(matrixClient, room);
break;
case 'tip':
args.push(matrixClient, room, address, flaggedInput, registrar);
args.push(matrixClient, room, address, flaggedInput);
break;
case 'archive': case 'rearchive':
args.push(matrixClient, room, userInput, !!~command.indexOf('re'), registrar);
args.push(matrixClient, room, userInput, !!~command.indexOf('re'));
command = 'archive';
break;
case 'post': case 'reply': case 'media': case 'mediareply':
case 'random': case 'randomreply': case 'randommedia': case 'randommediareply':
args.push(matrixClient, room, userInput, registrar, {
args.push(matrixClient, room, userInput, {
isReply: !!~command.indexOf('reply'),
hasMedia: !!~command.indexOf('media'),
hasSubject: !!~command.indexOf('random'),
@ -57,17 +58,16 @@ matrixClient.on('Room.timeline', (event, room, toStartOfTimeline) => {
case 'proxy':
try {
const url = new URL(userInput);
command = registrar.config.invidious.domains.includes(url.hostname)
? 'invidious'
: registrar.config.nitter.domains.includes(url.hostname)
command = config.invidious.domains.includes(url.hostname)
? 'invidious'
: config.nitter.domains.includes(url.hostname)
? 'nitter'
: 'proxy';
} catch(e) {}
//fallthrough
default:
args.push(matrixClient, room, userInput, registrar);
args.push(matrixClient, room, userInput);
}
registrar[command] && registrar[command].runQuery.apply(null, args);
}
});

View file

@ -1,6 +0,0 @@
{
"user_id": "",
"access_token": "",
"home_server": "",
"device_id": ""
}

View file

@ -1,6 +1,6 @@
{
"name": "ligh7hau5",
"version": "1.0.0",
"version": "1.1.0",
"description": "A Matrix to Fediverse client",
"main": "main.js",
"scripts": {
@ -19,14 +19,12 @@
"homepage": "https://github.com/vulet/lighthau5#readme",
"dependencies": {
"axios": "^0.21.1",
"file-system": "^2.2.2",
"form-data": "^3.0.0",
"jsdom": "^16.2.2",
"matrix-js-sdk": "^2.4.6"
"jsdom": "^16.4.0",
"matrix-js-sdk": "^9.5.1",
"node-localstorage": "^2.1.6",
"olm": "https://packages.matrix.org/npm/olm/olm-3.2.1.tgz",
"qs": "^6.9.6"
},
"devDependencies": {
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.3"
}
"devDependencies": {}
}

View file

@ -1,16 +1,24 @@
const fs = require('fs');
global.Olm = require('olm');
global.sdk = require('matrix-js-sdk');
global.axios = require('axios');
global.config = require('./config.js');
global.auth = require('./auth.js');
//initializers
if (!fs.existsSync('fediverse_auth.json')) fs.copyFileSync('fediverse_auth.example.json', 'fediverse_auth.json');
if (!fs.existsSync('matrix_auth.json')) fs.copyFileSync('matrix_auth.example.json', 'matrix_auth.json');
if (!fs.existsSync('config.js')) fs.copyFileSync('config.example.js', 'config.js');
if (!fs.existsSync('timeline.json')) fs.writeFileSync('timeline.json', 0);
if (!fs.existsSync('notification.json')) fs.writeFileSync('notification.json', 0);
const { LocalStorage } = require('node-localstorage');
global.localStorage = new LocalStorage('./keys');
if (!localStorage.getItem('matrix_auth')){
localStorage.clear();
localStorage.setItem('matrix_auth', "{}");
}
if (!localStorage.getItem('fediverse_auth')) localStorage.setItem('fediverse_auth', "{}");
if (!localStorage.getItem('timeline')) localStorage.setItem('timeline', "{}");
if (!localStorage.getItem('notifications')) localStorage.setItem('notifications', "{}");
global.matrix_auth = JSON.parse(localStorage.getItem('matrix_auth'));
global.fediverse_auth = JSON.parse(localStorage.getItem('fediverse_auth'));
module.exports = {
config: require('./config.js'),
fediverse_auth: require('./fediverse_auth.json'),
matrix_auth: require('./matrix_auth.json'),
archive: require('./commands/archive.js'),
invidious: require('./commands/invidious.js'),
nitter: require('./commands/nitter.js'),

1224
yarn.lock

File diff suppressed because it is too large Load diff