Baphomet is the dedicated bot for nulloctet matrix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

132 lines
3.9 KiB

let fs = require('fs');
let sdk = require('matrix-js-sdk');
let message = require('./message');
let utility = require('./utility');
let { logger } = require('./logging');
let { modules } = require('./module/index')
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 => {
logger.debug("Notifying %s of startup", roomId);
promises.push(this.client.sendMessage(
roomId, message.createBasic("Started with version: " + this.buildInfo)
).then(() => {
logger.debug("Notified %s of startup", roomId);
}));
});
} else {
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 => {
logger.debug("Notifying %s of shutdown", roomId);
promises.push(this.client.sendMessage(
roomId, message.createBasic("Shutting down")
).then(() => {
logger.debug("Notified %s of shutdown", roomId);
}));
});
} else {
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 {
logger.error("Unexpected Error!", err);
}
}
}
function sanitizeConfig(config) {
let clonedConfig = { ...config };
clonedConfig.accessToken = "******"
return clonedConfig;
}
function create(configFile) {
let config = require(configFile);
logger.info("Running with config:");
logger.debug("%o", sanitizeConfig(config));
let buildInfo = getBuildInfo("../build.info")
logger.info("Running version: %s", buildInfo);
return new Bot(config, buildInfo);
}
function init(bot) {
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", (state, previousState, data) => {
switch (state) {
case "PREPARED":
bot.connected = true;
bot.sendStatusStartup();
break;
}
});
bot.client.on("RoomMember.membership", (event, member) => {
if (member.membership === "invite"
&& bot.config.admin.indexOf(ember.userId) >= 0) {
bot.client.joinRoom(member.roomId).done(() => {
logger.info("Auto-joined %s", member.roomId);
});
}
});
/* Capture Exit Conditions */
["SIGINT", "SIGTERM"].forEach((signature) => {
process.on(signature, async () => {
await bot.sendStatusShutdown()
.then(() => {
logger.info("Gracefully stopping Matrix SDK Client")
bot.client.stopClient();
});
process.exit(0);
});
});
process.on('exit', () => {
logger.info("Shutting Down");
});
return bot;
}
function run(bot) {
// logger.info("Initializing Crypto");
// await bot.client.initCrypto();
logger.info("Starting Matrix SDK Client");
bot.client.startClient();
}
exports.create = create;
exports.init = init;
exports.run = run;