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.

90 lines
2.7 KiB
JavaScript

const fs = require('fs');
const remoteExec = require('./remoteExec.js');
const sdk = require('matrix-js-sdk');
const yaml = require('js-yaml');
const commandUtil = require('./commands.js');
global.config = yaml.safeLoad(fs.readFileSync('config.yaml'));
const matrixClient = sdk.createClient({
baseUrl: config.matrix.baseurl,
accessToken: config.matrix.accesstoken,
userId: config.matrix.userid
});
// Autojoin for the bot (keep commented out when not needed to prevent abuse)
/*matrixClient.on('RoomMember.membership', function(event, member) {
if(member.membership === 'invite' && member.userId === userid) {
matrixClient.joinRoom(member.roomId).done(function() {
console.log('Auto-joined %s', member.roomId);
});
}
});*/
//captures unhandled rejections for easier debugging
process.on('unhandledRejection', (reason) => {
console.log('Reason: ' + reason);
});
const commands = {};
commands['rebuild'] = {};
commands['rebuild'].regex = /^!rebuild ([a-zA-Z0-9\-]*)$/;
commands['rebuild'].usage = '!rebuild <container>';
commands['rebuild'].exec = function(args, room, event) {
const container = args[1];
matrixClient.sendNotice(room.roomId, 'rebuilding container ' + container);
remoteExec('./rebuild.sh ' + container)
.then(function(result) {
matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container);
}, function(result) {
matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuild of container ' + container + ' failed.\ncode: ' + result.code + '\nstdout: \n```\n' + result.stdout + '```\nstderr: \n```\n' + result.stderr + '\n```');
});
};
matrixClient.on('Room.timeline', function(event, room, resetTimeline) {
if(resetTimeline) {
return;
}
if(event.getType() !== 'm.room.message') {
return;
}
let body = event.getContent().body;
let project = body.match(/^\!([a-zA-Z]*)/);
if(project === null) {
return;
}
project = project[1];
let command = commands[project];
if(command !== undefined) {
const command = commands[project];
const args = body.match(command.regex);
if(args === null) {
matrixClient.sendNotice(room.roomId, 'usage: ' + command.usage);
return;
}
command.exec(args, room, event);
}else{ //command not found
let tmp = commandUtil.fetchCommand(project, commands);
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();