From 8c35748aee2c665895d151bccc525974ee083be0 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sun, 17 Nov 2019 01:35:50 +0000 Subject: [PATCH] Added and changed stuff --- src/commands/securesign_account.ts | 47 ++++++++++++++++++++++++++++++ src/commands/securesign_init.ts | 1 + src/functions/checkSS.ts | 25 ++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/commands/securesign_account.ts create mode 100644 src/functions/checkSS.ts diff --git a/src/commands/securesign_account.ts b/src/commands/securesign_account.ts new file mode 100644 index 0000000..374309f --- /dev/null +++ b/src/commands/securesign_account.ts @@ -0,0 +1,47 @@ +import { Message, PrivateChannel } from 'eris'; +import { Client } from '..'; +import { Command, RichEmbed } from '../class'; +import { AccountInterface } from '../models'; + +export default class SecureSign_Account extends Command { + constructor(client: Client) { + super(client); + this.name = 'account'; + this.description = 'Provides SecureSign account details for currently logged in user'; + this.usage = `${this.client.config.prefix}securesign account`; + this.enabled = true; + this.guildOnly = false; + } + + public async run(message: Message, args: string[]) { + try { + const user = await this.client.db.Account.findOne({ userID: message.author.id }); + if (!user || (user.permissions.staff && !(message.channel instanceof PrivateChannel))) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Run this command in your DMs!***`); + + let account: AccountInterface; + if (!args[0] || !user.permissions.staff) account = user; + else account = await this.client.db.Account.findOne({ $or: [{ userID: args[0] }, { username: args[0] }, { emailAddress: args[0] }] }); + + if (!account) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not found***`); + if (!account.hash) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Account not initialized***`); + const msg = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Loading account details...***`); + + const details = await this.client.util.exec(`sudo -H -u ${account.username} bash -c 'securesign-canary account'`); + const info = details.replace(/^\s+|\s+$/g, '').replace(/\n/g, '\n**').replace(/: /g, ':** ').split('\n'); + const title = info.shift(); + const description = info.join('\n'); + const content = ''; + + const embed = new RichEmbed(); + embed.setTitle(title); + embed.setDescription(description); + embed.setAuthor(this.client.user.username, this.client.user.avatarURL); + embed.setFooter(`Requested by ${message.member.username}#${message.member.discriminator}`, message.member.avatarURL); + + // @ts-ignore + return msg.edit({ content, embed }); + } catch (error) { + return this.client.util.handleError(error, message, this); + } + } +} diff --git a/src/commands/securesign_init.ts b/src/commands/securesign_init.ts index 0ec9ab1..e9d8417 100644 --- a/src/commands/securesign_init.ts +++ b/src/commands/securesign_init.ts @@ -47,6 +47,7 @@ export default class SecureSign_Init extends Command { } const init = await this.client.util.exec(`sudo -H -u ${account.username} bash -c 'securesign-canary init -a ${args[0]}'`); if (!init.replace(/^\s+|\s+$/g, '').endsWith('Initialization sequence completed.')) throw new Error(`Account initialization did not complete successfully:\n${init}`); + await this.client.db.Account.updateOne({ userID: message.author.id }, { $set: { hash: args[0] } }); return msg.edit(`${this.client.stores.emojis.success} ***Account initialized***`); } catch (error) { return this.client.util.handleError(error, message, this); diff --git a/src/functions/checkSS.ts b/src/functions/checkSS.ts new file mode 100644 index 0000000..ee4bd6d --- /dev/null +++ b/src/functions/checkSS.ts @@ -0,0 +1,25 @@ +/* eslint-disable no-await-in-loop */ +import axios from 'axios'; +import { Client } from '..'; + +export default function checkSS(client: Client) { + setInterval(async () => { + const accounts = await client.db.Account.find(); + const hashes = accounts.filter((h) => h.hash); + for (const { hash, userID } of hashes) { + try { + await axios({ + method: 'get', + url: 'https://api.securesign.org/account/details', + headers: { Authorization: hash }, + }); + } catch (error) { + const { status } = error.response; + if (status === 400 || status === 401 || status === 403 || status === 404) { + client.db.Account.updateOne({ hash }, { $set: { hash: null } }); + client.getDMChannel(userID).then((channel) => channel.createMessage('Your SecureSign password has been reset - please reinitialize your SecureSign account')).catch(); + } + } + } + }, 60000); +}