/* * Vencord, a modification for Discord's desktop app * Copyright (c) 2022 Vendicated and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import { ApplicationCommandInputType, ApplicationCommandOptionType, findOption, sendBotMessage } from "@api/Commands"; import { Upload } from "@api/MessageEvents"; import { definePluginSettings, Settings } from "@api/Settings"; import ErrorBoundary from "@components/ErrorBoundary"; import { reverseExtensionMap } from "@equicordplugins/fixFileExtensions"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; import { findByCodeLazy } from "@webpack"; import { useState } from "@webpack/common"; const ActionBarIcon = findByCodeLazy(".actionBarIcon)"); const enum Methods { Random, Consistent, Timestamp, } const ANONYMISE_UPLOAD_SYMBOL = Symbol("vcAnonymise"); export const tarExtMatcher = /\.tar\.\w+$/; const settings = definePluginSettings({ anonymiseByDefault: { description: "Whether to anonymise file names by default", type: OptionType.BOOLEAN, default: true, }, spoilerMessages: { description: "Spoiler messages", type: OptionType.BOOLEAN, default: false, }, method: { description: "Anonymising method", type: OptionType.SELECT, options: [ { label: "Random Characters", value: Methods.Random, default: true }, { label: "Consistent", value: Methods.Consistent }, { label: "Timestamp", value: Methods.Timestamp }, ], }, randomisedLength: { description: "Random characters length", type: OptionType.NUMBER, default: 7, disabled: () => settings.store.method !== Methods.Random, }, consistent: { description: "Consistent filename", type: OptionType.STRING, default: "image", disabled: () => settings.store.method !== Methods.Consistent, }, }); export default definePlugin({ name: "AnonymiseFileNames", authors: [Devs.fawn], description: "Anonymise uploaded file names", settings, patches: [ { find: 'type:"UPLOAD_START"', replacement: { match: /await \i\.uploadFiles\((\i),/, replace: "$1.forEach($self.anonymise),$&" }, }, { find: "#{intl::ATTACHMENT_UTILITIES_SPOILER}", replacement: { match: /(?<=children:\[)(?=.{10,80}tooltip:.{0,100}#{intl::ATTACHMENT_UTILITIES_SPOILER})/, replace: "arguments[0].canEdit!==false?$self.AnonymiseUploadButton(arguments[0]):null," }, }, ], AnonymiseUploadButton: ErrorBoundary.wrap(({ upload }: { upload: Upload; }) => { const [anonymise, setAnonymise] = useState(upload[ANONYMISE_UPLOAD_SYMBOL] ?? settings.store.anonymiseByDefault); function onToggleAnonymise() { upload[ANONYMISE_UPLOAD_SYMBOL] = !anonymise; setAnonymise(!anonymise); } return ( {anonymise ? : } ); }, { noop: true }), anonymise(upload: Upload) { const originalFileName = upload.filename; const tarMatch = tarExtMatcher.exec(originalFileName); const extIdx = tarMatch?.index ?? originalFileName.lastIndexOf("."); let ext = extIdx !== -1 ? originalFileName.slice(extIdx) : ""; const addSpoilerPrefix = (str: string) => settings.store.spoilerMessages ? "SPOILER_" + str : str; if (Settings.plugins.FixFileExtensions.enabled) { ext = reverseExtensionMap[ext] || ext; } if ((upload[ANONYMISE_UPLOAD_SYMBOL] ?? settings.store.anonymiseByDefault) === false) return addSpoilerPrefix(originalFileName + ext); const newFilename = (() => { switch (settings.store.method) { case Methods.Random: const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const returnedName = Array.from( { length: settings.store.randomisedLength }, () => chars[Math.floor(Math.random() * chars.length)] ).join("") + ext; return addSpoilerPrefix(returnedName); case Methods.Consistent: return addSpoilerPrefix(settings.store.consistent + ext); case Methods.Timestamp: return addSpoilerPrefix(Date.now().toString() + ext); } })(); upload.filename = newFilename; }, commands: [{ name: "Spoiler", description: "Toggle your spoiler", inputType: ApplicationCommandInputType.BUILT_IN, options: [ { name: "value", description: "Toggle your Spoiler (default is toggle)", required: false, type: ApplicationCommandOptionType.BOOLEAN, }, ], execute: async (args, ctx) => { settings.store.spoilerMessages = !!findOption(args, "value", !settings.store.spoilerMessages); sendBotMessage(ctx.channel.id, { content: settings.store.spoilerMessages ? "Spoiler enabled!" : "Spoiler disabled!", }); }, }], });