commit a9f9217dd794cd5d59cb6db87ea6b95b00d6d7c0 Author: Christian Richter Date: Fri Jul 15 14:18:59 2022 +0200 initial commit Signed-off-by: Christian Richter diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b83fd20 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/*.json +node_modules \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/config/config.json.example b/config/config.json.example new file mode 100644 index 0000000..c64aca2 --- /dev/null +++ b/config/config.json.example @@ -0,0 +1,18 @@ +{ + "homeServerUrl": "https://matrix.your-homeserver.org", + "accessToken": "youraccesstokencanbeobtainedthroughriot", + "storage": "config/bot.json", + "webServerPort": "8080", + "webSocketServerPort":"8090", + "monitorChannels": [ + "!yoursourceroomeid0:matrix.your-homeserver.org", + "!yoursourceroomeid1:matrix.your-homeserver.org", + "!yoursourceroomeid2:matrix.your-homeserver.org" + ], + "relayTypes" : [ + "m.image", + "m.video" + ], + "extractYoutubeLinks": true, + "showYoutubeTitle": true +} \ No newline at end of file diff --git a/weirdradio.js b/weirdradio.js new file mode 100644 index 0000000..cab1d50 --- /dev/null +++ b/weirdradio.js @@ -0,0 +1,91 @@ +const cheerio = require("cheerio"); +const request = require("request"); +const jsonfile = require("jsonfile"); +const sdk = require("matrix-bot-sdk"); +const http = require("http"); +const WebSocket = require("ws"); +const fs = require("fs"); +const path = require("path"); + +const configfile = "config/config.json"; +var config = jsonfile.readFileSync(configfile); + +const MatrixClient = sdk.MatrixClient; +const SimpleFsStorageProvider = sdk.SimpleFsStorageProvider; +const AutojoinRoomsMixin = sdk.AutojoinRoomsMixin; + +const client = new MatrixClient( + config.homeServerUrl, + config.accessToken, + new SimpleFsStorageProvider(config.storage) +); + +// load assets +let assets = []; +var files = fs.readdirSync("assets/"); +files.forEach((name) => { + assets[name] = fs.readFileSync("assets/" + name); +}); +// create server, this is for delivering the iframe page +const webServer = http + .createServer((req, res) => { + reqFileName = path.parse(req.url).base; + if (reqFileName == "") { + reqFileName = "index.html"; + } + if (assets[reqFileName] != undefined && assets[reqFileName] != null) { + res.writeHead(200, { "Content-Type": "text/html" }); + res.end(assets[reqFileName]); + } else { + res.writeHead(404, { "Content-Type": "text/plain" }); + res.end("404 - file not found"); + } + }) + .listen(config.webServerPort); +console.log("Webserver started"); + +const webSocketServer = new WebSocket.Server({ + port: config.webSocketServerPort, +}); +console.log("Websocketserver started"); + +let sockets = []; +webSocketServer.on("connection", function (socket) { + sockets.push(socket); + + // When you receive a message, send that message to every socket. + //socket.on('message', function(msg) { + //sockets.forEach(s => s.send(msg)); + //}); + + // When a socket closes, or disconnects, remove it from the array. + socket.on("close", function () { + sockets = sockets.filter((s) => s !== socket); + }); +}); + +// matrix client +AutojoinRoomsMixin.setupOnClient(client); +client.start().then(() => console.log("Matrix-Client started!")); + +client.on("room.message", (roomId, event) => { + if (!config.monitorChannels.includes(roomId)) return; + if (!event["content"]) return; + const sender = event["sender"]; + const body = event["content"]["body"]; + const type = event["content"]["msgtype"]; + const info = event["content"]["info"]; + const url = event["content"]["url"]; + if (type == "m.text") { + // TODO: beautify this regexp, too greedy, match only on explizit watch links, maybe transcribe into embedd links here + link_matches = body.match(/https?:\/\/[^\ ]*youtu[^\ ]*/g); + if (link_matches && link_matches.length > 0) { + console.log("Relaying: " + link_matches[0]); + // pass to server + var obj = { + link: link_matches[0], + }; + sockets.forEach((s) => s.send(JSON.stringify(obj))); + } + } +});