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.

131 lines
3.9 KiB

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