diff --git a/src/class/Util.ts b/src/class/Util.ts index d4fece4..cff693d 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-await-in-loop */ /* eslint-disable no-param-reassign */ import { promisify, inspect } from 'util'; import childProcess from 'child_process'; @@ -7,6 +8,7 @@ import uuid from 'uuid/v4'; import moment from 'moment'; import fs from 'fs'; import { Client } from '..'; +import { getUserByUid } from '../functions'; import { AccountUtil, Command, RichEmbed } from '.'; import { ModerationInterface, AccountInterface, Account } from '../models'; @@ -52,6 +54,30 @@ export default class Util { }); } + public async sendMessageToUserTerminal(username: string, message: string): Promise { + const ptsArray = await this.getPTS(username); + if (!ptsArray) return false; + for (const pts of ptsArray) { + const msg = `==SYSTEM NOTIFICATION | CLOUD SERVICES MANAGEMENT DAEMON==\n${new Date().toLocaleString('en-us')}\n\n${message}\n`; + await this.exec(`echo "${msg}" >> /dev/pts/${pts}`); + } + return true; + } + + private async getPTS(username: string): Promise { + const dir = await fs.promises.readdir('/dev/pts'); + const returnArray: number[] = []; + for (const file of dir) { + const fileInformation = await fs.promises.stat(`/dev/pts/${file}`); + const user = await getUserByUid(this.client, fileInformation.uid); + if (user && user === username) { + returnArray.push(Number(file)); + } + } + if (returnArray.length > 1) return undefined; + return returnArray; + } + /** * Resolves a command