import { xxh64 } from "@node-rs/xxhash"; import { encode } from "html-entities"; import { JSDOM } from "jsdom"; import util from "util"; import fs from "fs"; Array.prototype.random = function(){return this[Math.floor(Math.random() * this.length)]} var cache = {}; var log = null; export default function(client) { client.logEvent = null; client.cache = { get: (key) => { return cache[key] ?? null; }, set: (key, value) => { cache[key] = value; fs.writeFileSync("data/cache.json", JSON.stringify(cache)); } } client.config = { get: (key) => { var configFile = "data/" + xxh64(key).toString(16) + ".json"; var configData = {}; if(fs.existsSync(configFile)) { configData = JSON.parse(fs.readFileSync(configFile)); } var configObject = { get: (key) => { return configData[key] ?? null; }, set: (key, value) => { configData[key] = value; fs.writeFileSync(configFile, JSON.stringify(configData)); } }; return configObject; } } client.reply = function(event, text, html) { var content = event.getContent(); var mcontent = { format: "org.matrix.custom.html", formatted_body: "
In reply to " + event.sender.userId + "
" + (content.formatted_body ?? content.body) + "
" + (html ?? text).trim(), body: "> <" + event.sender.userId + "> " + content.body.split("\n")[0] + "\n\n" + text.trim(), msgtype: "m.notice", "m.relates_to": { "m.in_reply_to": { "event_id": event.event.event_id } } }; client.sendEvent(event.event.room_id, "m.room.message", mcontent, "", (err, res) => { console.log(err); }); } client.logData = []; client.startup = new Date(); client.updateLog = async function() { if(log && (log.body == client.logData.join("\n") || log["m.new_content"]?.body == client.logData.join("\n"))) { setTimeout(client.updateLog, 1000); return; } var mcontent = { format: "org.matrix.custom.html", formatted_body: `
[client:logger] started: ${client.startup}\n${encode(client.logData.join("\n"))}
`, body: client.logData.join("\n"), msgtype: "m.notice" } if(!client.logEvent) { log = mcontent; } else { log["m.new_content"] = mcontent; log["m.relates_to"] = { rel_type: "m.replace", event_id: client.logEvent } } var x = await client.sendEvent(process.env.LOG_CHANNEL, "m.room.message", log, "", (err, res) => { console.log("[client:log]", res); console.error(err); }); client.logEvent = client.logEvent ?? x.event_id; setTimeout(client.updateLog, 1000); } client.log = function() { var data = util.format.apply(null, arguments); client.logData.push(data); console.log(data); } client.error = client.log; client.uploadBuffer = async function(buffer) { var hash = xxh64(buffer).toString(16); var matrixUrl = client.cache.get(hash); if(!matrixUrl) { const uploadResponse = await client.uploadContent(buffer, { rawResponse: false }); matrixUrl = uploadResponse.content_uri; client.cache.set(hash, matrixUrl); } return matrixUrl; } client.uploadMedia = async function(url) { const buffer = await fetch(url, { headers: { "User-Agent": "PossumBot/1.0 (+https://bot.possum.city/)" } }).then((res) => res.arrayBuffer()).then((buf) => Buffer.from(buf)); return client.uploadBuffer(buffer); /* var matrixUrl = client.cache.get(url); if(!matrixUrl) { const buffer = await fetch(url, { headers: { "User-Agent": "PossumBot/1.0 (+https://bot.possum.city/)", }, }).then((res) => res.arrayBuffer()).then((buf) => Buffer.from(buf)); const uploadResponse = await client.uploadContent(buffer, { rawResponse: false }); matrixUrl = uploadResponse.content_uri; client.cache.set(url, matrixUrl); } return matrixUrl;*/ } if(fs.existsSync("data/cache.json")) { var data = fs.readFileSync("data/cache.json"); try { cache = JSON.parse(data); client.log("[sdkext:cache]", 'loaded', Object.keys(cache).length, 'cache entries!'); } catch(err) { client.error("[sdkext:cache]", err); } } process.on('uncaughtException', function (err) { console.error('[core:err]', err); }); client.updateLog(); }