|
|
let fs = require('fs'); let sdk = require('matrix-js-sdk'); let message = require('./message.js'); let utility = require('./utility.js'); let logging = require('./logging');
class Bot { constructor(config, buildInfo) { this.config = config; this.buildInfo = buildInfo; this.connected = false; }
sendStatusStartup() { let promises = [Promise.resolve(true)] if (this.connected) { this.config.statusRooms.forEach(roomId => { logging.logger.debug("Notifying %s", roomId, "of startup"); promises.push(this.client.sendMessage( roomId, message.createBasic("Started with version: " + this.buildInfo) ).then(function () { logging.logger.debug("Notified %s", roomId, "of startup"); })); }); } else { logging.logger.warn("Attempting to send startup message while disconnected"); } return Promise.all(promises); }
sendStatusShutdown() { let promises = [Promise.resolve(true)] if (this.connected) { this.config.statusRooms.forEach(roomId => { logging.logger.debug("Notifying %s", roomId, "of Shutdown"); promises.push(this.client.sendMessage( roomId, message.createBasic("Shutting down") ).then(function () { logging.logger.debug("Notified %s", roomId, "of shutdown"); })); }); } else { logging.logger.warn("Attempting to send shutdown message while disconnected"); } return Promise.all(promises); } }
function getBuildInfo(buildInfoPath) { try { return fs.readFileSync(buildInfoPath, "utf8"); } catch (err) { if (err.code === 'ENOENT') { return "UNKNOWN_" + utility.toISODateString(new Date()); } else { logging.logger.error("Unexpected Error! " + err); } } }
function sanitizeConfig(config) { let clonedConfig = { ...config }; clonedConfig.accessToken = "******" return clonedConfig; }
function create(configFile) { let config = require(configFile); logging.logger.info("Running with config:"); logging.logger.debug(sanitizeConfig(config)); let buildInfo = getBuildInfo("../build.info") logging.logger.info("Running version:", buildInfo); return new Bot(config, buildInfo); }
function init(bot) { logging.logger.info("Creating Matrix Client") bot.client = sdk.createClient({ baseUrl: bot.config.baseUrl, accessToken: bot.config.accessToken, userId: bot.config.userId });
bot.client.on("sync", async function (state, previousState, data) { switch (state) { case "PREPARED": bot.connected = true; await bot.sendStatusStartup(); break; } });
bot.client.on("RoomMember.membership", function (event, member) { if (member.membership === "invite" && bot.config.admin.indexOf(ember.userId) >= 0) { bot.client.joinRoom(member.roomId).done(function () { logging.logger.info("Auto-joined %s", member.roomId); }); } });
/* Capture Exit Conditions */
["SIGINT", "SIGTERM"].forEach((signature) => { process.on(signature, async () => { await bot.sendStatusShutdown() .then(function () { logging.logger.info("Gracefully stopping Matrix SDK Client") bot.client.stopClient(); }); process.exit(0); }); });
process.on('exit', async function () { logging.logger.info("Shutting Down"); });
return bot; }
function run(bot) { // logging.logger.info("Initializing Crypto");
// await bot.client.initCrypto();
logging.logger.info("Starting Matrix SDK Client"); bot.client.startClient(); }
exports.create = create; exports.init = init; exports.run = run;
|