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.

130 lines
4.0 KiB

  1. let fs = require('fs');
  2. let sdk = require('matrix-js-sdk');
  3. let message = require('./message.js');
  4. let utility = require('./utility.js');
  5. let logging = require('./logging');
  6. class Bot {
  7. constructor(config, buildInfo) {
  8. this.config = config;
  9. this.buildInfo = buildInfo;
  10. this.connected = false;
  11. }
  12. sendStatusStartup() {
  13. let promises = [Promise.resolve(true)]
  14. if (this.connected) {
  15. this.config.statusRooms.forEach(roomId => {
  16. logging.logger.debug("Notifying %s", roomId, "of startup");
  17. promises.push(this.client.sendMessage(
  18. roomId, message.createBasic("Started with version: " + this.buildInfo)
  19. ).then(function () {
  20. logging.logger.debug("Notified %s", roomId, "of startup");
  21. }));
  22. });
  23. } else {
  24. logging.logger.warn("Attempting to send startup message while disconnected");
  25. }
  26. return Promise.all(promises);
  27. }
  28. sendStatusShutdown() {
  29. let promises = [Promise.resolve(true)]
  30. if (this.connected) {
  31. this.config.statusRooms.forEach(roomId => {
  32. logging.logger.debug("Notifying %s", roomId, "of Shutdown");
  33. promises.push(this.client.sendMessage(
  34. roomId, message.createBasic("Shutting down")
  35. ).then(function () {
  36. logging.logger.debug("Notified %s", roomId, "of shutdown");
  37. }));
  38. });
  39. } else {
  40. logging.logger.warn("Attempting to send shutdown message while disconnected");
  41. }
  42. return Promise.all(promises);
  43. }
  44. }
  45. function getBuildInfo(buildInfoPath) {
  46. try {
  47. return fs.readFileSync(buildInfoPath, "utf8");
  48. } catch (err) {
  49. if (err.code === 'ENOENT') {
  50. return "UNKNOWN_" + utility.toISODateString(new Date());
  51. } else {
  52. logging.logger.error("Unexpected Error! " + err);
  53. }
  54. }
  55. }
  56. function sanitizeConfig(config) {
  57. let clonedConfig = { ...config };
  58. clonedConfig.accessToken = "******"
  59. return clonedConfig;
  60. }
  61. function create(configFile) {
  62. let config = require(configFile);
  63. logging.logger.info("Running with config:");
  64. logging.logger.debug(sanitizeConfig(config));
  65. let buildInfo = getBuildInfo("../build.info")
  66. logging.logger.info("Running version:", buildInfo);
  67. return new Bot(config, buildInfo);
  68. }
  69. function init(bot) {
  70. logging.logger.info("Creating Matrix Client")
  71. bot.client = sdk.createClient({
  72. baseUrl: bot.config.baseUrl,
  73. accessToken: bot.config.accessToken,
  74. userId: bot.config.userId
  75. });
  76. bot.client.on("sync", async function (state, previousState, data) {
  77. switch (state) {
  78. case "PREPARED":
  79. bot.connected = true;
  80. await bot.sendStatusStartup();
  81. break;
  82. }
  83. });
  84. bot.client.on("RoomMember.membership", function (event, member) {
  85. if (member.membership === "invite"
  86. && bot.config.admin.indexOf(ember.userId) >= 0) {
  87. bot.client.joinRoom(member.roomId).done(function () {
  88. logging.logger.info("Auto-joined %s", member.roomId);
  89. });
  90. }
  91. });
  92. /* Capture Exit Conditions */
  93. ["SIGINT", "SIGTERM"].forEach((signature) => {
  94. process.on(signature, async () => {
  95. await bot.sendStatusShutdown()
  96. .then(function () {
  97. logging.logger.info("Gracefully stopping Matrix SDK Client")
  98. bot.client.stopClient();
  99. });
  100. process.exit(0);
  101. });
  102. });
  103. process.on('exit', async function () {
  104. logging.logger.info("Shutting Down");
  105. });
  106. return bot;
  107. }
  108. function run(bot) {
  109. // logging.logger.info("Initializing Crypto");
  110. // await bot.client.initCrypto();
  111. logging.logger.info("Starting Matrix SDK Client");
  112. bot.client.startClient();
  113. }
  114. exports.create = create;
  115. exports.init = init;
  116. exports.run = run;