You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

68 lines
2.1 KiB
JavaScript

const commands = require('./commands.js');
const dbtools = require('./dbtools.js');
const fs = require('fs');
const permissions = require('./permissions.js');
const sdk = require('matrix-js-sdk');
const sqlite3 = require('sqlite3').verbose(); // .verbose until basics work
const yaml = require('js-yaml');
global.config = yaml.safeLoad(fs.readFileSync('config.yaml'));
global.matrixClient = sdk.createClient({
baseUrl: config.matrix.baseurl,
accessToken: config.matrix.accesstoken,
userId: config.matrix.userid
});
const db = new sqlite3.Database(config.database);
dbtools.createSchema(db);
//captures unhandled rejections for easier debugging
process.on('unhandledRejection', (reason) => {
console.log('Reason: ' + reason);
});
matrixClient.on('Room.timeline', function(event, room, resetTimeline) {
if(resetTimeline) {
return;
}
if(event.getType() !== 'm.room.message') {
return;
}
const body = event.getContent().body;
commands.expandCommand(body, commands.projects)
.then((expansion) => {
permissions.eval(event.sender.userId, expansion, db)
.then((accessgranted) => {
let subcommand = commands.projects[expansion.project][expansion.service][expansion.subcommand];
const args = expansion.args.match(subcommand.regex);
if(args === null && !subcommand.regex.test(expansion.args)) {
matrixClient.sendNotice(room.roomId, 'usage: ' + subcommand.usage);
return;
}
subcommand.exec(args, room, event);
}, (fail) => {
matrixClient.sendNotice(room.roomId, event.sender.userId + ' ' + fail);
});
}, (fail) => { // command not found
let tmp = commands.suggestFix(body, commands.projects);
if(tmp !== null){ //when the regex matches nothing
let suggestions = "";
for(let i = 0; i < tmp.length; i++){
suggestions += "\n"+tmp[i];
}
matrixClient.sendNotice(room.roomId, 'Unrecognized command.\n' +
'Did you mean:'+suggestions);
}
});
});
matrixClient.startClient();