diff --git a/Bridge/class/Bridge.js b/Bridge/class/Bridge.js index cba7d21..3b4aa0b 100644 --- a/Bridge/class/Bridge.js +++ b/Bridge/class/Bridge.js @@ -1,14 +1,14 @@ const DiscordClient = require("../../DiscordBot"); const IRCClient = require("../../IRCBot"); +const MatrixClient = require("../../MatrixBot"); class Bridge { - constructor(config) { - const verbose = config.verbose ?? false; + constructor() { + const verbose = process.env.VERBOSE == "true" ? true : false; - const discordChannels = config.discordChannels; - const ircChannels = config.ircChannels.map((chan) => - chan.toLowerCase(), - ); + const discordChannels = process.env.DISCORD_CHANNELS.split(","); + const ircChannels = process.env.IRC_CHANNELS.split(","); + const matrixChannels = process.env.MATRIX_CHANNELS.split(","); if (!discordChannels || !ircChannels) { this.log("Missing channels"); @@ -20,9 +20,15 @@ class Bridge { this, verbose, discordChannels, - config.discordAppId, + process.env.DISCORD_APP_ID, + ), + //new IRCClient(this, verbose, ircChannels), + new MatrixClient( + this, + verbose, + process.env.MATRIX_SERVER, + matrixChannels, ), - new IRCClient(this, verbose, ircChannels), ]; } diff --git a/Bridge/class/Message.js b/Bridge/class/Message.js index 34c5392..1a7bb0a 100644 --- a/Bridge/class/Message.js +++ b/Bridge/class/Message.js @@ -25,6 +25,13 @@ export class Message { (this.source_type = "irc")); } + fromMatrixFormat(matrix_message, channel) { + this.text = matrix_message.event.content.body; + this.channel = matrix_message.event.room_id; + this.author = matrix_message.getSender(); + this.source_type = "matrix"; + } + async escapeDiscordPing(text, client) { let res_text = text; diff --git a/Dockerfile b/Dockerfile index 5c540fd..3294d4b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,12 @@ FROM node WORKDIR /urbridge -COPY package.json . -RUN npm install +RUN npm install -g pnpm + +COPY package.json pnpm-lock.yaml* ./ + +RUN pnpm install + COPY . . -CMD ["node", "index.js"] \ No newline at end of file +CMD ["node", "index.js"] diff --git a/MatrixBot/class/Client.js b/MatrixBot/class/Client.js new file mode 100644 index 0000000..b8b3db0 --- /dev/null +++ b/MatrixBot/class/Client.js @@ -0,0 +1,110 @@ +const sdk = require("matrix-js-sdk"); +const { logger } = require("matrix-js-sdk/lib/logger"); +const { Message } = require("../../Bridge/class/Message"); + +class MatrixClient { + constructor(bridge, verbose = false, server, channels) { + this.bridge = bridge; + this.channels = channels; + this.verbose = verbose; + this.userId = process.env.MATRIX_USER_ID; + this.server = server; + this._room_name_to_id = {}; + + logger.disableAll(); + + this.log("serv", this.server, "chans", channels); + + this.client = sdk.createClient({ + baseUrl: "https://" + this.server, + accessToken: process.env.MATRIX_TOKEN, + userId: this.userId, + }); + } + + log(...args) { + if (this.verbose) console.log("[Matrix]", ...args); + } + + broadcast(message) { + const msg = this._fromatMessageForForward(message); + this.log("broadcasting message", msg); + for (const chan in this._room_name_to_id) { + // do not send in the channel the message is comming from + const chanId = this._room_name_to_id[chan]; + if (message.source_type != "matrix" || chanId != msg.channel) { + this._sendTextInChannel(msg.text, chanId); + } + } + } + + _fromatMessageForForward(msg) { + const res_msg = { ...msg }; + res_msg.text = "[" + msg.author + "]: " + msg.text; + return res_msg; + } + + _sendTextInChannel(text, chanId) { + this.client.sendMessage(chanId, { + msgtype: "m.text", + body: text, + }); + } + + async _joinRooms() { + for (const room of this.channels) { + await this.client.joinRoom(this._formatRoomName(room)); + } + } + + _formatRoomName(name) { + return `#${name}:${this.server}`; + } + + _onMessage(matrixEvent, room, toStartOfTimeline) { + //this.log(matrixEvent); + if (matrixEvent.getType() !== "m.room.message") return; + if (matrixEvent.getSender() == this.userId) return; // catch self messages + + const internal_msg = new Message(); + internal_msg.fromMatrixFormat(matrixEvent, room); + this.log(internal_msg); + + this.bridge.broadcast(internal_msg); + } + + async start() { + this.client.startClient({ initialSyncLimit: 10 }); + this.client.once( + sdk.ClientEvent.Sync, + async (state, prevState, res) => { + if (state === "PREPARED") { + this.log("prepared"); + await this._joinRooms(); // not needed to join every time, once is enough, but rejoin just in case + + for (const room of this.client.getRooms()) { + if (this.channels.includes(room.name)) { + this._room_name_to_id[room.name] = room.roomId; + } + } + this.log(this._room_name_to_id); + this.client.on( + "Room.timeline", + (matrixEvent, room, toStartOfTimeline) => { + this._onMessage( + matrixEvent, + room, + toStartOfTimeline, + ); + }, + ); + } else { + this.log(state); + process.exit(1); + } + }, + ); + } +} + +module.exports = MatrixClient; diff --git a/MatrixBot/index.js b/MatrixBot/index.js new file mode 100644 index 0000000..ed87020 --- /dev/null +++ b/MatrixBot/index.js @@ -0,0 +1 @@ +module.exports = require("./class/Client"); diff --git a/README.md b/README.md index 0166070..d582532 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,27 @@ systemctl start urlab_viaduk il faut potentiellement stop le service précédent et faire un `systemctl daemon-reload` une fois recréé +### Matrix + +pour regénérer un token: +```bash +curl -X POST "https://matrix.urlab.be/_matrix/client/v3/login" \ + -H "Content-Type: application/json" \ + -d '{ + "type": "m.login.password", + "identifier": { + "type": "m.id.user", + "user": "urbridge" + }, + "password": "password" + }' +``` +puis mettre le token dans .env + +si vous avez pas le password niquez vous + + + ### to do - ajouter les /command du lechebot pour avoir l'auto completion de discord diff --git a/config.json b/config.json deleted file mode 100644 index 22124a3..0000000 --- a/config.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "verbose": false, - "discordChannels": ["1161739009614151751", "727212463988146206"], - "ircChannels": ["#urlab"], - "discordAppId": "866317638337953792" -} diff --git a/index.js b/index.js index 6323870..b825ad3 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ -const Bridge = require("./Bridge") -const config = require("./config.json") -const bridge = new Bridge(config) +const Bridge = require("./Bridge"); +const bridge = new Bridge(); -bridge.start() \ No newline at end of file +bridge.start(); diff --git a/package.json b/package.json index d5fbf5f..b4f9b33 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "ISC", "dependencies": { "discord.js": "^14.14.1", - "irc-framework": "^4.13.1" + "irc-framework": "^4.13.1", + "matrix-js-sdk": "^41.2.0" } }