2021-02-01 11:58:59 +03:00
|
|
|
global.registrar = require('./registrar.js');
|
2019-07-04 00:27:39 +03:00
|
|
|
|
2021-02-01 11:58:59 +03:00
|
|
|
matrix_auth.access_token ? auth.matrixTokenLogin() : auth.getMatrixToken();
|
|
|
|
if (!fediverse_auth.access_token && config.fediverse.username) auth.registerFediverseApp();
|
2019-07-04 00:27:39 +03:00
|
|
|
|
2021-01-17 12:58:02 +03:00
|
|
|
matrixClient.on('RoomMember.membership', (event, member) => {
|
|
|
|
if (member.membership === 'invite' && member.userId === matrixClient.credentials.userId) {
|
2021-02-01 11:58:59 +03:00
|
|
|
matrixClient.joinRoom(member.roomId).then(() => {
|
2021-01-17 12:58:02 +03:00
|
|
|
console.log('Auto-joined %s', member.roomId);
|
|
|
|
});
|
|
|
|
}
|
2021-02-01 11:58:59 +03:00
|
|
|
|
2021-01-17 12:58:02 +03:00
|
|
|
if (member.membership === 'leave' && member.userId === matrixClient.credentials.userId) {
|
|
|
|
matrixClient.forget(member.roomId).then(() => {
|
|
|
|
console.log('Kicked %s', member.roomId);
|
|
|
|
});
|
|
|
|
}
|
2019-07-04 00:27:39 +03:00
|
|
|
});
|
|
|
|
|
2021-02-01 11:58:59 +03:00
|
|
|
matrixClient.on('Room.timeline', async function (event, room, member, toStartOfTimeline) {
|
2021-01-17 12:58:02 +03:00
|
|
|
if (toStartOfTimeline) return;
|
2021-02-01 11:58:59 +03:00
|
|
|
if (event.isEncrypted()) await event._decryptionPromise;
|
2021-01-17 12:58:02 +03:00
|
|
|
if (event.getType() !== 'm.room.message') return;
|
|
|
|
if (event.getSender() === matrixClient.credentials.userId) return;
|
|
|
|
if (event.event.unsigned.age > 10000) return;
|
2021-02-01 11:58:59 +03:00
|
|
|
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);
|
2021-01-17 12:58:02 +03:00
|
|
|
let command = args.shift().toLowerCase();
|
|
|
|
const userInput = args.join(' ');
|
|
|
|
const flaggedInput = userInput.substr(userInput.indexOf(' ') + 1);
|
|
|
|
const address = args.slice(0, 1).join(' ').replace(/"/g, '');
|
2019-07-04 00:27:39 +03:00
|
|
|
|
2021-01-17 12:58:02 +03:00
|
|
|
args = [];
|
2019-07-04 00:27:39 +03:00
|
|
|
|
2021-01-17 12:58:02 +03:00
|
|
|
switch(command) {
|
|
|
|
case 'config':
|
|
|
|
return;
|
2021-02-01 11:58:59 +03:00
|
|
|
case 'help': case 'beg': case 'flood': case 'asdf':
|
|
|
|
args.push(matrixClient, room);
|
2021-01-17 12:58:02 +03:00
|
|
|
break;
|
|
|
|
case 'tip':
|
2021-02-01 11:58:59 +03:00
|
|
|
args.push(matrixClient, room, address, flaggedInput);
|
2021-01-17 12:58:02 +03:00
|
|
|
break;
|
|
|
|
case 'archive': case 'rearchive':
|
2021-02-01 11:58:59 +03:00
|
|
|
args.push(matrixClient, room, userInput, !!~command.indexOf('re'));
|
2021-01-17 12:58:02 +03:00
|
|
|
command = 'archive';
|
|
|
|
break;
|
|
|
|
case 'post': case 'reply': case 'media': case 'mediareply':
|
|
|
|
case 'random': case 'randomreply': case 'randommedia': case 'randommediareply':
|
2021-02-01 11:58:59 +03:00
|
|
|
args.push(matrixClient, room, userInput, {
|
2021-01-17 12:58:02 +03:00
|
|
|
isReply: !!~command.indexOf('reply'),
|
|
|
|
hasMedia: !!~command.indexOf('media'),
|
|
|
|
hasSubject: !!~command.indexOf('random'),
|
|
|
|
});
|
|
|
|
command = 'media';
|
|
|
|
break;
|
|
|
|
case 'proxy':
|
|
|
|
try {
|
|
|
|
const url = new URL(userInput);
|
2021-02-01 11:58:59 +03:00
|
|
|
command = config.invidious.domains.includes(url.hostname)
|
|
|
|
? 'invidious'
|
|
|
|
: config.nitter.domains.includes(url.hostname)
|
2021-01-17 12:58:02 +03:00
|
|
|
? 'nitter'
|
|
|
|
: 'proxy';
|
|
|
|
} catch(e) {}
|
|
|
|
//fallthrough
|
|
|
|
default:
|
2021-02-01 11:58:59 +03:00
|
|
|
args.push(matrixClient, room, userInput);
|
2019-07-04 00:27:39 +03:00
|
|
|
}
|
2021-01-17 12:58:02 +03:00
|
|
|
registrar[command] && registrar[command].runQuery.apply(null, args);
|
|
|
|
}
|
|
|
|
});
|