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.

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