diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d3fdac4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 2 diff --git a/.eslintrc.yaml b/.eslintrc.yaml new file mode 100644 index 0000000..d0487f1 --- /dev/null +++ b/.eslintrc.yaml @@ -0,0 +1,51 @@ +root: true +extends: + - standard + - airbnb-base +env: + es6: true + node: true +rules: + no-useless-escape: off + no-param-reassign: off + prettier/prettier: off + prefer-rest-params: off + no-underscore-dangle: off + no-use-before-define: off + unicorn/filename-case: off + class-methods-use-this: off + import/no-default-export: off + curly: + - error + comma-dangle: + - error + - always-multiline + id-length: + - error + - + min: 2 + exceptions: + - i + - x + - y + - e + id-match: + - error + - "^(([A-Za-z0-9]+){2,})|([A-Z][A-Z_0-9]+)$" + - + properties: false + onlyDeclarations: true + indent: + - error + - 2 + - + SwitchCase: 1 + quotes: + - error + - single + semi: + - error + - always + space-before-function-paren: + - error + - always diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6017936 --- /dev/null +++ b/.gitignore @@ -0,0 +1,64 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +bower_components/ +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# VSCode +.vscode/ + +# dotenv environment variables file +.env + +*.traineddata +files/ + +package-lock.json diff --git a/configure.js b/configure.js index f5fcb0f..36b3570 100755 --- a/configure.js +++ b/configure.js @@ -1,15 +1,16 @@ const fs = require('fs'); const prompt = require('prompt'); +const { execSync } = require('child_process'); const processes = require('./processes.json'); + const dataStringify = JSON.stringify(processes); const dataParsed = JSON.parse(dataStringify); -const {exec, execSync, execFileSync} = require('child_process'); prompt.colors = false; -process.stdin.resume(); //so the program will not close instantly +process.stdin.resume(); // so the program will not close instantly -function exitHandler(options, err) { +function exitHandler (options, err) { try { if (err) { process.kill(process.pid); @@ -20,42 +21,43 @@ function exitHandler(options, err) { } else if (options.pid) { process.kill(process.pid); } - } catch (err) { - console.log(err); + } catch (error) { + console.log(error); } } -//catches ctrl+c and stop.sh events -process.on('SIGINT', exitHandler.bind(null, {exit: true})); +// catches ctrl+c and stop.sh events +process.on('SIGINT', exitHandler.bind(null, { exit: true })); -//catches "kill pid" (for example: nodemon restart) -process.on('SIGUSR1', exitHandler.bind(null, {pid: true})); -process.on('SIGUSR2', exitHandler.bind(null, {pid: true})); +// catches "kill pid" (for example: nodemon restart) +process.on('SIGUSR1', exitHandler.bind(null, { pid: true })); +process.on('SIGUSR2', exitHandler.bind(null, { pid: true })); -//catches uncaught exceptions +// catches uncaught exceptions process.on('uncaughtException', exitHandler.bind(null, { exit: true, - reason: 'uncaughtException' + reason: 'uncaughtException', })); -main(); - -async function main() { - try { - var cp = execSync('cp processes.json processes_backup.json'); +async function main () { + try { + execSync('cp processes.json processes_backup.json'); + let newName; - if (process.env.WICKRIO_BOT_NAME !== undefined) { - var newName = "WickrIO-File-Bot_" + process.env.WICKRIO_BOT_NAME; - } else { - var newName = "WickrIO-File-Bot"; - } + if (process.env.WICKRIO_BOT_NAME !== undefined) { + newName = `WickrIO-File-Bot_${process.env.WICKRIO_BOT_NAME}`; + } else { + newName = 'WickrIO-File-Bot'; + } - //var assign = Object.assign(dataParsed.apps[0].name, newName); - dataParsed.apps[0].name = newName; + // var assign = Object.assign(dataParsed.apps[0].name, newName); + dataParsed.apps[0].name = newName; - var ps = fs.writeFileSync('./processes.json', JSON.stringify(dataParsed, null, 2)); - } catch (err) { - console.log(err); - } - process.exit(); + fs.writeFileSync('./processes.json', JSON.stringify(dataParsed, null, 2)); + } catch (err) { + console.log(err); + } + process.exit(); } + +main(); diff --git a/file_bot.js b/file_bot.js index e251537..e8cab8d 100644 --- a/file_bot.js +++ b/file_bot.js @@ -1,17 +1,18 @@ +// eslint-disable-next-line import/no-unresolved const WickrIOAPI = require('wickrio_addon'); const WickrIOBotAPI = require('wickrio-bot-api'); -var fs = require('fs'); +const fs = require('fs'); module.exports = WickrIOAPI; -process.stdin.resume(); //so the program will not close instantly -var bot; +process.stdin.resume(); // so the program will not close instantly +let bot; -async function exitHandler(options, err) { +async function exitHandler (options, err) { try { - var closed = await bot.close(); + const closed = await bot.close(); console.log(closed); if (err) { - console.log("Exit Error:", err); + console.log('Exit Error:', err); process.exit(); } if (options.exit) { @@ -19,180 +20,177 @@ async function exitHandler(options, err) { } else if (options.pid) { process.kill(process.pid); } - } catch (err) { - console.log(err); + } catch (error) { + console.log(error); } } -//catches ctrl+c and stop.sh events +// catches ctrl+c and stop.sh events process.on('SIGINT', exitHandler.bind(null, { - exit: true + exit: true, })); // catches "kill pid" (for example: nodemon restart) process.on('SIGUSR1', exitHandler.bind(null, { - pid: true + pid: true, })); process.on('SIGUSR2', exitHandler.bind(null, { - pid: true + pid: true, })); -//catches uncaught exceptions +// catches uncaught exceptions process.on('uncaughtException', exitHandler.bind(null, { - exit: true + exit: true, })); -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} - -async function main() { +async function main () { try { - var status; + let status; if (process.argv[2] === undefined) { - var bot_username = fs.readFileSync('client_bot_username.txt', 'utf-8'); - bot_username = bot_username.trim(); + let botUsername = fs.readFileSync('client_bot_username.txt', 'utf-8'); + botUsername = botUsername.trim(); bot = new WickrIOBotAPI.WickrIOBot(); - status = await bot.start(bot_username) + status = await bot.start(botUsername); } else { bot = new WickrIOBotAPI.WickrIOBot(); - status = await bot.start(process.argv[2]) + status = await bot.start(process.argv[2]); } if (!status) { exitHandler(null, { exit: true, - reason: 'Client not able to start' + reason: 'Client not able to start', }); } - await bot.startListening(listen); //Passes a callback function that will receive incoming messages into the bot client + // Passes a callback function that will receive incoming messages into the bot client + await bot.startListening(listen); } catch (err) { console.log(err); } } - -async function listen(rMessage) { +// eslint-disable-next-line consistent-return +async function listen (rMessage) { rMessage = JSON.parse(rMessage); - var sender = rMessage.sender; - var vGroupID = rMessage.vgroupid; - var userArr = []; + const { sender, vgroupid: vGroupID } = rMessage; + const userArr = []; + let prevMessage; userArr.push(sender); if (rMessage.message) { - var request = rMessage.message; - var command = '', - argument = ''; - var parsedData = request.match(/(\/[a-zA-Z]+)(@[a-zA-Z0-9_-]+)?(\s+)?(.*)$/); + const request = rMessage.message; + let command = ''; + let argument = ''; + const parsedData = request.match(/(\/[a-zA-Z]+)(@[a-zA-Z0-9_-]+)?(\s+)?(.*)$/); if (parsedData !== null) { - command = parsedData[1]; + [, command] = parsedData; if (parsedData[4] !== '') { - argument = parsedData[4]; + [,,,, argument] = parsedData; } } if (command === '/list') { - var fileArr = []; + let fileArr = []; fileArr.push('List of files in the given directory:'); - var answer; - fs.readdirSync('files/').forEach(file => { + fs.readdirSync('files/').forEach((file) => { fileArr.push(file.toString()); }); fileArr = fileArr.join('\n'); try { - var sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, fileArr); + const sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, fileArr); console.log(sMessage); } catch (err) { console.log(err); } } else if (command === '/get') { - var attachment = argument; + const attachment = argument; if (attachment === '') { - var msg = "Command missing an argument. Proper format: /get FILE_NAME"; + const msg = 'Command missing an argument. Proper format: /get FILE_NAME'; return WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); } - if (! findFile(argument)) { - var msg = attachment + ' does not exist in the file directory'; + if (!findFile(argument)) { + const msg = `${attachment} does not exist in the file directory`; console.error(msg); return console.log(WickrIOAPI.cmdSendRoomMessage(vGroupID, msg)); } try { - var as = fs.accessSync('files/' + attachment, fs.constants.R_OK | fs.constants.W_OK); - var response = WickrIOAPI.cmdSendRoomAttachment(vGroupID, __dirname + '/files/' + attachment, attachment); - console.log(response) + // eslint-disable-next-line no-bitwise + fs.accessSync(`files/${attachment}`, fs.constants.R_OK | fs.constants.W_OK); + const response = WickrIOAPI.cmdSendRoomAttachment(vGroupID, `${__dirname}/files/${attachment}`, attachment); + console.log(response); } catch (err) { if (err instanceof TypeError || err instanceof ReferenceError) { console.log(err); } else { - var msg = attachment + ' does not exist in the file directory'; + const msg = `${attachment} does not exist in the file directory`; console.error(msg); return console.log(WickrIOAPI.cmdSendRoomMessage(vGroupID, msg)); } } } else if (command === '/delete') { + const attachment = argument; try { - var attachment = argument; if (attachment === '') { - var msg = "Command missing an argument. Proper format: /delete FILE_NAME"; + const msg = 'Command missing an argument. Proper format: /delete FILE_NAME'; return WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); } if (attachment === '*') { - var msg = "Sorry, I'm not allowed to delete all the files in the directory."; - var sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); + const msg = 'Sorry, I\'m not allowed to delete all the files in the directory.'; + const sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); console.log(sMessage); - return; + return null; } - if (! findFile(argument)) { - var msg = attachment + ' does not exist in the file directory'; + if (!findFile(argument)) { + const msg = `${attachment} does not exist in the file directory`; console.error(msg); return console.log(WickrIOAPI.cmdSendRoomMessage(vGroupID, msg)); } - var os = fs.statSync('files/' + attachment); + fs.statSync(`files/${attachment}`); } catch (err) { if (err instanceof TypeError || err instanceof ReferenceError) { console.log(err); } else { - var msg = attachment + ' does not exist in the file directory'; + const msg = `${attachment} does not exist in the file directory`; console.error(msg); - var sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); + const sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); console.log(sMessage); - return; + return null; } } try { - var rm = fs.unlinkSync('files/' + attachment); - var msg = "File named: " + attachment + " was deleted successfully!"; - var sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); + fs.unlinkSync(`files/${attachment}`); + const msg = `File named: ${attachment} was deleted successfully!`; + const sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); console.log(sMessage); } catch (err) { - var msg = "Unable to delete file named: " + attachment; - var sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); + const msg = `Unable to delete file named: ${attachment}`; + WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); return console.log(err); } - } else if (command === '/help') { - var help = "/help - List all available commands\n" + - "/list - Lists all available files\n" + - "/get FILE_NAME - Retrieve the specified file\n" + - "/delete FILE_NAME - Deletes the specified file\n"; - var sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, help); + const help = '/help - List all available commands\n' + + '/list - Lists all available files\n' + + '/get FILE_NAME - Retrieve the specified file\n' + + '/delete FILE_NAME - Deletes the specified file\n'; + const sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, help); console.log(sMessage); } } else if (rMessage.file && JSON.stringify(rMessage) !== JSON.stringify(prevMessage)) { - var cp = fs.copyFileSync(rMessage.file.localfilename.toString(), 'files/' + rMessage.file.filename.toString()); - var msg = "File named: '" + rMessage.file.filename + "' successfully saved to directory!"; - var sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); - var prevMessage = rMessage; + fs.copyFileSync(rMessage.file.localfilename.toString(), `files/${rMessage.file.filename.toString()}`); + const msg = `File named: '${rMessage.file.filename}' successfully saved to directory!`; + const sMessage = WickrIOAPI.cmdSendRoomMessage(vGroupID, msg); + prevMessage = rMessage; console.log(sMessage); } else { console.log(rMessage); } } -function findFile(argument) { - var fileArr = []; - fs.readdirSync('files/').forEach(file => { +function findFile (argument) { + const fileArr = []; + fs.readdirSync('files/').forEach((file) => { fileArr.push(file.toString()); }); - var found = false; - for(file of fileArr){ + let found = false; + // eslint-disable-next-line no-restricted-syntax + for (const file of fileArr) { if (file === argument) { found = true; } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index d02461c..52b4c38 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dependencies": { "pm2": "^3.5.1", "prompt": "^1.0.0", - "wickrio-bot-api": "5.64.x" + "wickrio-bot-api": "5.64.x", + "wickrio_addon": "^5.64.8" }, "scripts": { "start": "pm2 start processes.json --update-env", @@ -26,5 +27,14 @@ "messaging", "api" ], - "database": false + "database": false, + "devDependencies": { + "eslint": "^7.2.0", + "eslint-config-airbnb-base": "^14.2.0", + "eslint-config-standard": "^14.1.1", + "eslint-plugin-import": "^2.22.1", + "eslint-plugin-node": "^11.1.0", + "eslint-plugin-promise": "^4.2.1", + "eslint-plugin-standard": "^4.0.1" + } }