add basic support for matrix

This commit is contained in:
vincent 2026-04-07 21:16:06 +02:00
parent a9fe7ab8ed
commit cb07e76c75
9 changed files with 165 additions and 22 deletions

View file

@ -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),
];
}

View file

@ -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;

View file

@ -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"]
CMD ["node", "index.js"]

110
MatrixBot/class/Client.js Normal file
View file

@ -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;

1
MatrixBot/index.js Normal file
View file

@ -0,0 +1 @@
module.exports = require("./class/Client");

View file

@ -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

View file

@ -1,6 +0,0 @@
{
"verbose": false,
"discordChannels": ["1161739009614151751", "727212463988146206"],
"ircChannels": ["#urlab"],
"discordAppId": "866317638337953792"
}

View file

@ -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()
bridge.start();

View file

@ -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"
}
}