2021-02-01 11:58:59 +03:00
|
|
|
global.registrar = require('./registrar.js');
|
2019-07-04 00:27:39 +03:00
|
|
|
|
2021-02-14 10:57:35 +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-14 10:57:35 +03:00
|
|
|
matrixClient.on('event', async (event) => {
|
2021-08-06 15:13:36 +03:00
|
|
|
if (event.isEncrypted()) await matrixClient.decryptEventIfNeeded(event, { emit: false, isRetry: false });
|
2021-02-14 10:57:35 +03:00
|
|
|
if (event.getSender() === matrixClient.credentials.userId) return matrix.utils.selfReact(event);
|
|
|
|
if (!event.getContent()['m.relates_to']) return;
|
|
|
|
if (event.event.unsigned.age > 10000) return;
|
|
|
|
return event.getType() === 'm.room.message'
|
|
|
|
? matrix.utils.handleReply(event) : matrix.utils.handleReact(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
matrixClient.on('Room.timeline', async (event, member, toStartOfTimeline) => {
|
2021-01-17 12:58:02 +03:00
|
|
|
if (toStartOfTimeline) return;
|
2021-08-06 15:13:36 +03:00
|
|
|
if (event.isEncrypted()) await matrixClient.decryptEventIfNeeded(event, { emit: false, isRetry: false });
|
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-14 10:57:35 +03:00
|
|
|
roomId = event.event.room_id;
|
|
|
|
content = event.getContent().body;
|
|
|
|
if (content.charAt(0) === '+') {
|
|
|
|
const args = content.slice(1).trim().split(/ +/g);
|
|
|
|
const command = args.shift().toLowerCase();
|
|
|
|
console.log(`Logs: ${event.event.sender} - ${content}`);
|
|
|
|
matrix.utils.eventHandler(args, roomId, command, event);
|
2021-01-17 12:58:02 +03:00
|
|
|
}
|
|
|
|
});
|