finally add support for encryption :DDD

This commit is contained in:
Ashley 2024-08-25 18:37:24 +00:00
parent 99e0b86d50
commit 9291d930b4
5 changed files with 37 additions and 21 deletions

View file

@ -5,6 +5,7 @@ import sdkExt from "./lib/ext.js";
import { resolve } from "node:path";
import fs from "node:fs";
import util from "util";
import { LocalStorage } from 'node-localstorage';
import fetch from "node-fetch";
global.fetch = fetch;
@ -15,11 +16,17 @@ global.Olm = Olm;
import env from "dotenv";
env.config();
const localStorage = new LocalStorage("./data/localstorage");
const cryptoStore = new sdk.LocalStorageCryptoStore(localStorage);
const store = new sdk.MemoryStore({ localStorage });
const client = sdk.createClient({
baseUrl: process.env.BASE_URL,
accessToken: process.env.ACCESS_TOKEN,
deviceId: process.env.DEVICE_ID,
userId: process.env.USER_ID
userId: process.env.USER_ID,
cryptoStore,
store
});
sdkExt(client);
@ -72,8 +79,7 @@ function doCommand(client, event, cmd, args) {
if((command.owner && event.sender.userId != process.env.OWNER_ID) || (command.minPwr && event.sender.powerLevel < command.minPwr && event.sender.userId != process.env.OWNER_ID)) {
var addl = command.minPwr ? `this command requires a power level of ${command.minPwr}\nyour power level is ${event.sender.powerLevel}` : "this command is owner-only.";
client.reply(event, addl, '<img src="mxc://possum.city/b4Vo1BTcq49B7TbFWCqq76HQWQEdNIqq" alt="nuh uh" /><br>' + addl);
client.reply(event, addl, '<img src="mxc://possum.city/b4Vo1BTcq49B7TbFWCqq76HQWQEdNIqq" alt="nuh uh" /><br>' + addl.replaceAll("\n", "<br>"));
return true;
}
@ -82,26 +88,29 @@ function doCommand(client, event, cmd, args) {
}
client.once("sync", async function (state, prevState, data) {
switch (state) {
case "PREPARED":
client.name = client._store.users[client.credentials.userId].displayName;
client.log("[sdk:sync]", "connected:", client.name);
prefixes.push(client.name + ": ");
prefixes.push(client.name + " ");
client.initialized = true;
break;
}
if(state != "PREPARED") return;
client.setGlobalErrorOnUnknownDevices(false);
client.name = client._store.users[client.credentials.userId].displayName;
client.log("[sdk:sync]", "connected:", client.name);
prefixes.push(client.name + ": ");
prefixes.push(client.name + " ");
client.initialized = true;
});
client.on(sdk.RoomEvent.Timeline, async function (event, room, toStartOfTimeline) {
await client.decryptEventIfNeeded(event);
if (!client.initialized || toStartOfTimeline || event.getType() !== "m.room.message" || event.sender.userId == client.credentials.userId) {
return;
}
if(event.event.content["m.relates_to"] != null)
event.event.content.body = event.event.content.body.substring(event.event.content.body.indexOf("\n\n") + 2);
var content = event.getContent();
if(content["m.relates_to"] != null)
content.body = content.body.substring(content.body.indexOf("\n\n") + 2);
event.sender = client.getRoom(event.sender.roomId).getMember(event.getSender());
var content = event.event.content.body;
var content = content.body;
var isCommand = false;
for(const prefix of prefixes) {
@ -132,5 +141,5 @@ client.on("Room.myMembership", function (room, membership, prevMembership) {
}
});
//await client.initCrypto();
await client.initCrypto();
await client.startClient();

View file

@ -23,8 +23,10 @@ export default function(client) {
}
client.reply = function(event, text, html) {
var content = event.getContent();
var mcontent = {
body: "> <" + event.sender.userId + "> " + event.event.content.body.split("\n")[0] + "\n\n" + 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": {
@ -32,13 +34,15 @@ export default function(client) {
}
}
};
if(html) {
mcontent = {
format: "org.matrix.custom.html",
formatted_body: "<mx-reply><blockquote><a href=\"https://m.posm.gay/#/" + event.event.room_id + "/" + event.event.event_id + "?via=possum.city\">In reply to</a> <a href=\"https://m.posm.gay/#/" + event.sender.userId + "\">" + event.sender.userId + "</a><br>" + encode(event.event.content.body.split("\n")[0]) + "</blockquote></mx-reply>" + html.trim(),
formatted_body: "<mx-reply><blockquote><a href=\"https://m.posm.gay/#/" + event.event.room_id + "/" + event.event.event_id + "?via=possum.city\">In reply to</a> <a href=\"https://m.posm.gay/#/" + event.sender.userId + "\">" + event.sender.userId + "</a><br>" + encode(content.body.split("\n")[0]) + "</blockquote></mx-reply>" + html.trim(),
...mcontent
}
}
client.sendEvent(event.event.room_id, "m.room.message", mcontent, "", (err, res) => {
console.log(err);
});

View file

@ -4,7 +4,7 @@ import { JSDOM } from "jsdom";
import util from "util";
async function onMessage(client, event) {
const embed = await fedimbed(event.event.content.body);
const embed = await fedimbed(event.getContent().body);
if(!embed) return;
const dom = new JSDOM("<!DOCTYPE html><body></body>");
const document = dom.window.document;

View file

@ -19,10 +19,12 @@ var emoticons = [
];
async function onMessage(client, event) {
if(event.event.content["m.new_content"] != null) return;
var content = event.getContent();
if(content["m.new_content"] != null) return;
for(const meow of meows) {
if(event.event.content.body.toLowerCase().includes(meow)) {
if(content.body.toLowerCase().includes(meow)) {
var reply = meows.random();
reply += "!".repeat(Math.random()*5)

View file

@ -16,6 +16,7 @@
"jsdom": "^24.1.1",
"matrix-js-sdk": "^32.0.0",
"node-fetch": "^3.3.2",
"node-localstorage": "^3.0.5",
"sync-rpc": "^1.3.6"
}
}