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.
 
 
 
 
 

138 lines
4.7 KiB

let sdk = require('matrix-js-sdk');
let message = require('./message');
let utility = require('./utility');
let { logger } = require('./logging');
class Bot {
constructor(config, buildInfo) {
this.config = config;
this.buildInfo = buildInfo;
this.connected = false;
}
/**
* Initialize the bot connection
*/
init(messageCallback) {
logger.info("Creating Matrix Client")
this.client = sdk.createClient({
baseUrl: this.config.baseUrl,
userId: this.config.userId
});
this.client.on("sync", async (state, previousState, data) => {
switch (state) {
case "PREPARED":
this.connected = true;
await this.sendStatusStartup();
this.client.getJoinedRooms()
.done((rooms) => {
logger.info("Connected to: %o", rooms)
});
break;
case "SYNCING":
logger.debug("Syncing")
break;
case "RECONNECTING":
logger.debug("Reconnecting");
break;
default:
logger.error("Unexpected sync state: %s", state);
process.exit(1);
}
});
this.client.on("RoomMember.membership", (event, member) => {
if (member.membership === "invite"
&& this.config.admin.indexOf(ember.userId) >= 0) {
this.client.joinRoom(member.roomId).done(() => {
logger.info("Auto-joined %s", member.roomId);
});
}
});
this.client.on("Room.timeline", messageCallback);
return this;
}
async connect() {
// logger.info("Initializing Crypto");
// await bot.client.initCrypto();
let botClient = this.client;
let botConfig = this.config;
let startServerConnection = async () => {
logger.info("Starting Matrix SDK Client");
await this.client.startClient();
}
let connectWithPassword = (err, data) => {
if (err === null) {
logger.info("Successfully authenticated with password %o", data);
startServerConnection();
} else {
logger.error("Failed to authenticate with password %o", err);
}
}
let connectWithToken = (err, data) => {
if (err === null) {
logger.info("Successfully authenticated with access token %o", data);
startServerConnection();
} else {
logger.error("Failed to authenticate with access token %o", err);
if (typeof botConfig.userPassword !== 'undefined') {
botClient.loginWithPassword(botConfig.userId, botConfig.userPassword, connectWithPassword);
} else {
logger.error("No fallback password provided!");
}
}
}
logger.info("Authenticating With Server");
botClient.loginWithToken(this.config.accessToken, connectWithToken);
}
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, message.types.NOTICE)
).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", message.types.NOTICE)
).then(() => {
logger.debug("Notified %s of shutdown", roomId);
}));
});
} else {
logger.warn("Attempting to send shutdown message while disconnected");
}
return Promise.all(promises);
}
}
function create(config) {
let buildInfo = utility.getBuildInfo();
logger.info("Running version: %s", buildInfo);
return new Bot(config, buildInfo);
}
exports.create = create;