This commit is contained in:
Lillith 2024-09-17 11:33:19 -07:00 committed by GitHub
commit 979685d49f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 409 additions and 0 deletions

View file

@ -0,0 +1,5 @@
# PasswordProtect
Allows you to password protect your dms and channels!
![A popup warning you that a channel is password protected](https://github.com/Vendicated/Vencord/assets/44179559/2424085e-3090-4310-9d56-62667941c57c)

View file

@ -0,0 +1,22 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { openModalLazy } from "@utils/modal";
import { checkPassword } from "../data";
import { ModalType, PasswordModal } from "./modal";
export async function openAccessModal(channelId: string, cb: (success: boolean) => void) {
await openModalLazy(async () => {
return modalProps => <PasswordModal modalProps={modalProps} channelId={channelId} type={ModalType.Access} callback={async password => {
if (password) {
cb(await checkPassword(password, channelId));
} else {
cb(false);
}
}} />;
});
}

View file

@ -0,0 +1,71 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
import { Menu } from "@webpack/common";
import { isPasswordProtected } from "../data";
import { openLockModal } from "./lockModal";
import { openUnlockModal } from "./unlockModal";
function createPasswordItem(channelId: string) {
const isProtected = isPasswordProtected(channelId);
return (
<Menu.MenuItem
id="password-protect"
label="Password Protect"
>
{!isProtected && (
<>
<Menu.MenuItem
id="vc-password-protect-lock"
label="Lock"
color="brand"
action={() => openLockModal(channelId)}
/>
</>
)}
{isProtected && (
<>
<Menu.MenuItem
id="vc-password-protect-unlock"
label="Unlock"
color="danger"
action={() => openUnlockModal(channelId)}
/>
</>
)}
</Menu.MenuItem>
);
}
const GroupDMContext: NavContextMenuPatchCallback = (children, props) => {
const container = findGroupChildrenByChildId("leave-channel", children);
container?.unshift(createPasswordItem(props.channel.id));
};
const UserContext: NavContextMenuPatchCallback = (children, props) => {
const container = findGroupChildrenByChildId("close-dm", children);
if (container) {
const idx = container.findIndex(c => c?.props?.id === "close-dm");
container.splice(idx, 0, createPasswordItem(props.channel.id));
}
};
const ChannelContect: NavContextMenuPatchCallback = (children, props) => {
const container = findGroupChildrenByChildId(["mute-channel", "unmute-channel"], children);
container?.unshift(createPasswordItem(props.channel.id));
};
export const contextMenus = {
"gdm-context": GroupDMContext,
"user-context": UserContext,
"channel-context": ChannelContect
};

View file

@ -0,0 +1,24 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { openModalLazy } from "@utils/modal";
import { setPassword } from "../data";
import { isChannelCurrent, reloadChannel } from "../utils";
import { ModalType, PasswordModal } from "./modal";
export async function openLockModal(channelId: string) {
await openModalLazy(async () => {
return modalProps => <PasswordModal modalProps={modalProps} channelId={channelId} type={ModalType.Lock} callback={password => {
if (password) {
setPassword(channelId, password);
if (isChannelCurrent(channelId)) {
reloadChannel();
}
}
}} />;
});
}

View file

@ -0,0 +1,91 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { classNameFactory } from "@api/Styles";
import { ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot } from "@utils/modal";
import { Button, Forms, Text, TextInput, useEffect, useState } from "@webpack/common";
import { checkPassword } from "../data";
export enum ModalType {
Lock = "Lock",
Unlock = "Unlock",
Access = "Access"
}
interface Props {
channelId: string;
type: ModalType;
callback: (password?: string) => void;
modalProps: ModalProps;
}
const cl = classNameFactory("vc-password-modal-");
export function PasswordModal({ channelId, type, callback, modalProps }: Props) {
const [password, setPassword] = useState("");
const [confirmPasswordValue, setConfirmPasswordValue] = useState("");
const [error, setError] = useState("");
useEffect(() => {
(async () => {
if (password !== confirmPasswordValue && [ModalType.Lock, ModalType.Unlock].includes(type)) {
setError("Passwords do not match");
} else if (password === "") {
setError("Please enter a password");
}
else if (type !== ModalType.Lock && !(await checkPassword(password, channelId))) {
setError("Incorrect Password");
} else {
setError("");
}
})();
}, [password, confirmPasswordValue]);
const onSubmit = async (e: React.FormEvent<HTMLFormElement> | React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
e.preventDefault();
if (error) return callback();
modalProps.onClose();
callback(password);
};
return (
<ModalRoot {...modalProps}>
<ModalHeader>
<Text variant="heading-lg/semibold" style={{ flexGrow: 1 }}>{type}</Text>
</ModalHeader>
{/* form is here so when you press enter while in the text input it submits */}
<form onSubmit={onSubmit}>
<ModalContent className={cl("content")}>
<Forms.FormSection>
<Forms.FormTitle>Password</Forms.FormTitle>
<TextInput
type="password"
value={password}
onChange={e => setPassword(e)}
/>
</Forms.FormSection>
{[ModalType.Lock, ModalType.Unlock].includes(type) && (
<Forms.FormSection>
<Forms.FormTitle>Confirm Password</Forms.FormTitle>
<TextInput
type="password"
value={confirmPasswordValue}
onChange={e => setConfirmPasswordValue(e)}
/>
</Forms.FormSection>
)}
<Forms.FormDivider />
{error && <Text color="text-danger">{error}</Text>}
</ModalContent>
<ModalFooter>
<Button type="submit" onClick={onSubmit} disabled={!!error}>{type}</Button>
</ModalFooter>
</form>
</ModalRoot>
);
}

View file

@ -0,0 +1,26 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { openModalLazy } from "@utils/modal";
import { checkPassword, removePassword } from "../data";
import { isChannelCurrent, reloadChannel } from "../utils";
import { ModalType, PasswordModal } from "./modal";
export async function openUnlockModal(channelId: string) {
await openModalLazy(async () => {
return modalProps => <PasswordModal modalProps={modalProps} channelId={channelId} type={ModalType.Unlock} callback={async password => {
if (password) {
if (await checkPassword(password, channelId)) {
removePassword(channelId);
if (isChannelCurrent(channelId)) {
reloadChannel();
}
}
}
}} />;
});
}

View file

@ -0,0 +1,60 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { DataStore } from "@api/index";
import { Channel } from "discord-types/general";
import { isChannelCurrent, reloadChannel, sha256 } from "./utils";
let data: Record<string, string> = {};
const accessedChannels: string[] = [];
export async function initData() {
const newData = await DataStore.get("passwordProtect");
if (newData) {
data = newData;
}
}
export async function saveData() {
await DataStore.set("passwordProtect", data);
}
export function isLocked(channelId: string) {
if (accessedChannels.includes(channelId)) return false;
return isPasswordProtected(channelId);
}
export function isPasswordProtected(channelId: string) {
return data?.[channelId] !== undefined;
}
export function getPasswordHash(channelId: string) {
return data?.[channelId];
}
export async function setPassword(channelId: string, password: string) {
data![channelId] = await sha256(password);
await saveData();
}
export async function removePassword(channelId: string) {
delete data![channelId];
await saveData();
}
export async function checkPassword(input: string, channelId: string) {
return await sha256(input) === getPasswordHash(channelId);
}
export function accessChannel(channel: Channel) {
accessedChannels.push(channel.id);
if (isChannelCurrent(channel.id)) reloadChannel();
setTimeout(() => {
accessedChannels.splice(accessedChannels.indexOf(channel.id), 1);
}, 1000);
}

View file

@ -0,0 +1,84 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { Devs } from "@utils/constants";
import { getCurrentChannel } from "@utils/discord";
import definePlugin from "@utils/types";
import { openAccessModal } from "./components/accessModal";
import { contextMenus } from "./components/contextMenus";
import { accessChannel, initData, isLocked, isPasswordProtected, saveData } from "./data";
interface NSFWBlockProps {
title: string;
description: string;
agreement: string;
disagreement: string;
onAgree: () => void;
onDisagree: () => void;
}
export default definePlugin({
name: "PasswordProtect",
description: "Passcode protect servers, channels, and dms. WARNING: Disabling the plugin will allow anyone to open the channels!",
authors: [Devs.ImLvna],
contextMenus: contextMenus,
patches: [
{
find: "GuildNSFWAgreeStore",
replacement: {
match: /didAgree\((\i)\){/,
replace: "$&if($self.isLocked($1)) return false; if($self.isPasswordProtected($1)) return true;"
}
},
{
find: "return this.nsfw",
replacement: {
match: /return this.nsfw/,
replace: "if($self.isLocked(this.id)) return true;$&"
}
},
{
find: ".gatedContent,",
replacement: {
match: /this.props/,
replace: "$self.patchProps($&)"
}
}
],
patchProps(props: NSFWBlockProps) {
const channel = getCurrentChannel();
if (!isPasswordProtected(channel.id)) return props;
props.title = "This channel is password protected";
props.description = "This channel is password protected. Please enter the password to view the content.";
props.agreement = "Enter password";
props.disagreement = "Cancel";
props.onAgree = () => {
openAccessModal(channel.id, async success => {
console.log(success);
if (success) {
accessChannel(channel);
}
}
);
};
return props;
},
isLocked,
isPasswordProtected,
start() {
initData();
},
stop() {
saveData();
},
});

View file

@ -0,0 +1,26 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { getCurrentChannel } from "@utils/discord";
import { NavigationRouter } from "@webpack/common";
export async function sha256(message) {
const buf = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(message));
const array = Array.from(new Uint8Array(buf));
const str = array.map(b => b.toString(16).padStart(2, "0")).join("");
return str;
}
export function isChannelCurrent(channelId: string) {
return getCurrentChannel()?.id === channelId;
}
export async function reloadChannel() {
const channel = getCurrentChannel();
NavigationRouter.transitionTo("/channels/@me");
await new Promise(r => setTimeout(r, 0));
NavigationRouter.transitionTo(`/channels/${channel.guild_id || "@me"}/${channel.id}`);
}