From b744f4fbc0c54279d8e053f69f7eceb3023301e3 Mon Sep 17 00:00:00 2001 From: Hendrik Eeckhaut Date: Thu, 22 Aug 2024 11:18:04 +0200 Subject: [PATCH] feat: WIP discord DM plugin --- examples/discord_dm/index.d.ts | 13 ++++ examples/discord_dm/index.js | 124 +++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 examples/discord_dm/index.d.ts create mode 100644 examples/discord_dm/index.js diff --git a/examples/discord_dm/index.d.ts b/examples/discord_dm/index.d.ts new file mode 100644 index 0000000..f9f785a --- /dev/null +++ b/examples/discord_dm/index.d.ts @@ -0,0 +1,13 @@ +declare module 'main' { + export function start(): I32; + export function two(): I32; + export function three(): I32; + export function config(): I32; +} + +declare module 'extism:host' { + interface user { + redirect(ptr: I64): void; + notarize(ptr: I64): I64; + } +} diff --git a/examples/discord_dm/index.js b/examples/discord_dm/index.js new file mode 100644 index 0000000..b4e5fa0 --- /dev/null +++ b/examples/discord_dm/index.js @@ -0,0 +1,124 @@ +function isValidHost(urlString) { + const url = new URL(urlString); + return url.hostname === 'discord.com' || url.hostname === 'discord.gg' +} + +// https://discord.com/channels/@me/1252416264475770891 +function gotoDiscord() { + const { redirect } = Host.getFunctions(); + const mem = Memory.fromString('https://discord.com/channels/@me'); + redirect(mem.offset); +} + +function extractConversationId(urlString) { + const url = new URL(urlString); + if (url.hostname === 'discord.com' && /\/channels\/@me\/[0-9]+$/.test(url.pathname)) { + return url.pathname.split('/@me/')[1] + } else { + return url.pathname.split('/channels/')[1] + } +} + +function start() { + if (!isValidHost(Config.get('tabUrl'))) { + gotoDiscord(); + Host.outputString(JSON.stringify(false)); + return; + } + Host.outputString(JSON.stringify(true)); +} + +function two() { + const conversationId = extractConversationId(Config.get('tabUrl')); + // const cookies = JSON.parse(Config.get('cookies'))['discord.com']; + const headers = JSON.parse(Config.get('headers'))['discord.com']; + + // console.log("conversationId"); + // console.log(JSON.stringify(conversationId)); + // console.log(JSON.stringify(headers['Authorization'])); + + if ( + !conversationId || + !headers['Authorization'] + ) { + Host.outputString(JSON.stringify(false)); + return; + } + + Host.outputString( + JSON.stringify({ + url: `https://discord.com/api/v9/channels/${conversationId}/messages?limit=2`, + method: 'GET', + headers: { + Host: 'discord.com', + Accept: '*/*', + 'Accept-Encoding': 'identity', + 'User-Agent': headers['User-Agent'], + Authorization: headers['Authorization'], + Connection: 'close' + }, + secretHeaders: [ + `authorization: ${headers['Authorization']}` + ] + }) + ) +} + +function three() { + const params = JSON.parse(Host.inputString()); + const { notarize } = Host.getFunctions(); + + // console.log("params"); + // console.log(JSON.stringify(params)); + + if (!params) { + Host.outputString(JSON.stringify(false)); + } else { + const mem = Memory.fromString(JSON.stringify(params)); + const idOffset = notarize(mem.offset); + const id = Memory.find(idOffset).readString(); + Host.outputString(JSON.stringify(id)); + } +} + +function config() { + Host.outputString( + JSON.stringify({ + title: 'Discord DMs', + description: 'Notarize your Discord DMs', + + steps: [ + { + title: "Goto Discord DM's", + description: "Log in to your discord if you haven't already", + cta: "Go to discord.com", + action: 'start' + }, + { + title: 'Open the DM you want to notarize', + description: "Pick a short conversation (to meet the current size limits)", + cta: 'Check', + action: 'two' + }, + { + title: 'Notarize DM', + cta: 'Notarize', + action: 'three', + prover: true + } + ], + hostFunctions: ['redirect', 'notarize'], + cookies: [], + headers: ['discord.com'], + requests: [ + { + url: `https://discord.com/api/v9/channels/*/messages?limit=2`, + method: 'GET', + }, + ], + }), + ); +} + + +module.exports = { config, start, two, three };