From 10130e3b5ca9388fa392d68df02f79e778d051dd Mon Sep 17 00:00:00 2001 From: Flummi Date: Sat, 18 Nov 2017 11:53:58 +0100 Subject: [PATCH] danke eslint --- .eslintrc.json | 28 +++++ src/bot.js | 19 ++- src/inc/cfg.js | 22 ++-- src/inc/clients/irc.js | 270 +++++++++++++++++++++-------------------- src/inc/clients/tg.js | 8 +- src/inc/sql.js | 2 +- src/inc/wrapper.js | 39 +++--- 7 files changed, 206 insertions(+), 182 deletions(-) create mode 100644 .eslintrc.json diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..ed1280b --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,28 @@ +{ + "env": { + "es6": true, + "node": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + 2 + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "double" + ], + "semi": [ + "error", + "always" + ] + } +} \ No newline at end of file diff --git a/src/bot.js b/src/bot.js index 9409919..a93a727 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,16 +1,15 @@ -import { cfg, read } from './inc/cfg.js'; -import { wrapper, clients } from './inc/wrapper.js'; -const safeEval = require('safe-eval'); +import { read } from "./inc/cfg.js"; +import { wrapper, clients } from "./inc/wrapper.js"; +const safeEval = require("safe-eval"); read().then(() => { let bot = new wrapper(); - bot.on('message', e => { // Todo: eventhandler + bot.on("message", e => { // Todo: eventhandler let orig = e.message; let tmp = orig.split(" "); - let cmd = tmp[0].toLowerCase(); tmp.shift(); let args = tmp; - args[0] = (args[0] == String.empty || typeof args[0] === 'undefined' || args[0] == "") ? e.user.nick : args[0]; + args[0] = (args[0] == String.empty || typeof args[0] === "undefined" || args[0] == "") ? e.user.nick : args[0]; if (e.message.match(/^\.js /)) { // JS-Sandbox args = e.message.substring(3); @@ -21,10 +20,10 @@ read().then(() => { console: { log: console.log } - } + }; try { var output = safeEval(args, context); - if (typeof output !== undefined && output !== 'undefined' && output) { + if (typeof output !== undefined && output !== "undefined" && output) { let blah = JSON.stringify(output); if (blah != "Converting circular structure to JSON") { e.reply(blah.length > 220 ? `holy fuck, Ausgabe wäre viel zu lang! (${blah.length} Zeichen :DDDDDD)` : blah); @@ -56,7 +55,7 @@ read().then(() => { e.replyAction(`drischt mit einem großen Stück Butterkäse auf ${args[0]} ein.`); } }); - bot.on('ctcp:version', e => { - e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`) + bot.on("ctcp:version", e => { + e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`); }); }); \ No newline at end of file diff --git a/src/inc/cfg.js b/src/inc/cfg.js index 3e2c7dd..5f9a198 100644 --- a/src/inc/cfg.js +++ b/src/inc/cfg.js @@ -1,4 +1,4 @@ -import sql from './sql.js'; +import sql from "./sql.js"; let cfg = { client: {}, @@ -15,18 +15,14 @@ const read = () => new Promise((resolve, reject) => { for(let row in rows) { cfg[rows[row].class][rows[row].key] = ((type, value) => { switch(type) { - case 'string': - return value; - break; - case 'int': - return parseInt(value); - break; - case 'bool': - return value === 'true'; - break; - case 'json': - return JSON.parse(value); - break; + case "string": + return value; + case "int": + return parseInt(value); + case "bool": + return value === "true"; + case "json": + return JSON.parse(value); } })(rows[row].type, rows[row].value); } diff --git a/src/inc/clients/irc.js b/src/inc/clients/irc.js index 0d591db..b429302 100644 --- a/src/inc/clients/irc.js +++ b/src/inc/clients/irc.js @@ -33,167 +33,169 @@ class irc { this.send(`NICK ${this.nickname}`); this.send(`USER ${this.username} 0 * : ${this.realname}`); }); - this.socket.setEncoding('utf-8'); - this.socket.on('data', msg => { + this.socket.setEncoding("utf-8"); + this.socket.on("data", msg => { msg = this.parse(msg); let tmpuser = {}; switch (msg.command) { // auslagern! - // WHOIS BEGIN - case "307": // Rizon Account - tmpuser = {}; - if(this.server.user.has( msg.params[1].toLowerCase() )) - tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); - tmpuser.account = msg.params[1].toLowerCase(); - tmpuser.registered = true; - this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); - break; - case "311": // first - tmpuser = {}; - if (this.server.user.has( msg.params[1].toLowerCase() )) - tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); - tmpuser.nickname = msg.params[1].toLowerCase(); - tmpuser.username = msg.params[2]; - tmpuser.hostname = msg.params[3]; - tmpuser.realname = msg.params[5]; - this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); - break; - case "313": // Oper - tmpuser = {}; - if (this.server.user.has( msg.params[1].toLowerCase() )) - tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); - tmpuser.oper = true; - this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); - break; - case "318": // last (check Data) - tmpuser = {}; - if (this.server.user.has( msg.params[1].toLowerCase() )) - tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); - tmpuser = { - nickname: tmpuser.nickname || false, - username: tmpuser.username || false, - hostname: tmpuser.hostname || false, - realname: tmpuser.realname || false, - account: tmpuser.account || false, - registered: tmpuser.registered || false, - oper: tmpuser.oper || false, - channels: tmpuser.channels || [], - cached: ~~(Date.now() / 1000) - }; - this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); - break; - case "319": // Chanlist Todo - let tmpchan = new Map(); - tmpuser = {}; - if (this.server.user.has( msg.params[1].toLowerCase() )) { - tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); - if(tmpuser.channels) - tmpchan = tmpuser.channels; - } + // WHOIS BEGIN + case "307": // Rizon Account + tmpuser = {}; + if(this.server.user.has( msg.params[1].toLowerCase() )) + tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); + tmpuser.account = msg.params[1].toLowerCase(); + tmpuser.registered = true; + this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); + break; + case "311": // first + tmpuser = {}; + if (this.server.user.has( msg.params[1].toLowerCase() )) + tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); + tmpuser.nickname = msg.params[1].toLowerCase(); + tmpuser.username = msg.params[2]; + tmpuser.hostname = msg.params[3]; + tmpuser.realname = msg.params[5]; + this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); + break; + case "313": // Oper + tmpuser = {}; + if (this.server.user.has( msg.params[1].toLowerCase() )) + tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); + tmpuser.oper = true; + this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); + break; + case "318": // last (check Data) + tmpuser = {}; + if (this.server.user.has( msg.params[1].toLowerCase() )) + tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); + tmpuser = { + nickname: tmpuser.nickname || false, + username: tmpuser.username || false, + hostname: tmpuser.hostname || false, + realname: tmpuser.realname || false, + account: tmpuser.account || false, + registered: tmpuser.registered || false, + oper: tmpuser.oper || false, + channels: tmpuser.channels || [], + cached: ~~(Date.now() / 1000) + }; + this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); + break; + case "319": // Chanlist Todo + let tmpchan = new Map(); + tmpuser = {}; + if (this.server.user.has( msg.params[1].toLowerCase() )) { + tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); + if(tmpuser.channels) + tmpchan = tmpuser.channels; + } - let chans = msg.params[2].trim().split(" "); - for(let chan in chans) { - chan = chans[chan].split("#"); - tmpchan.set(`#${chan[1]}`, chan[0]); - } - tmpuser.channels = tmpchan; + let chans = msg.params[2].trim().split(" "); + for(let chan in chans) { + chan = chans[chan].split("#"); + tmpchan.set(`#${chan[1]}`, chan[0]); + } + tmpuser.channels = tmpchan; - this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); - break; - case "330": // Quarknet - tmpuser = {}; - if (this.server.user.has( msg.params[1].toLowerCase() )) - tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); - tmpuser.account = msg.params[2]; - tmpuser.registered = true; - this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); - break; + this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); + break; + case "330": // Quarknet + tmpuser = {}; + if (this.server.user.has( msg.params[1].toLowerCase() )) + tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); + tmpuser.account = msg.params[2]; + tmpuser.registered = true; + this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); + break; // WHOIS END - case "001": // welcome - this.server.me = msg.params[0]; - this.join(this.options.channels); - this.emit('data', ['connected', msg.params[1]]); - break; - case "352": // who_entry - if(!this.server.channel[msg.params[1]]) - this.server.channel[msg.params[1]] = new Map(); - this.server.channel[msg.params[1]].set(msg.params[5], { // chan - nick: msg.params[5], - username: msg.params[2], - hostname: msg.params[3] - }); - this.whois(msg.params[5]); + case "001": // welcome + this.server.me = msg.params[0]; + this.join(this.options.channels); + this.emit("data", ["connected", msg.params[1]]); break; - case "315": // who_end - console.log(this.server.channel); + case "352": // who_entry + if(!this.server.channel[msg.params[1]]) + this.server.channel[msg.params[1]] = new Map(); + this.server.channel[msg.params[1]].set(msg.params[5], { // chan + nick: msg.params[5], + username: msg.params[2], + hostname: msg.params[3] + }); + this.whois(msg.params[5]); + break; + case "315": // who_end + console.log(this.server.channel); + break; + case "372": // motd + this.server.motd += `${msg.params[1]}\n`; + break; + case "375": // motd_start + this.server.motd = `${msg.params[1]}\n`; + break; + case "376": // motd_end + this.server.motd += `${msg.params[1]}\n`; + this.emit("data", ["motd", this.server.motd]); + break; + case "464": // Password required + if (this.options.password.length > 0) + this.send(`PASS ${this.options.password}`); // pwd + break; + case "PING": + this.send(`PONG ${msg.params.join``}`); + break; + case "JOIN": + console.log("join", msg); + this.send(`WHO ${msg.params[0]}`); + break; + case "PART": + console.log("part", msg); + delete this.server.user[msg.params[0]]; + //this.whois(msg.params[0], true); // force whois + break; + case "PRIVMSG": + if (msg.params[1] === "\u0001VERSION\u0001") + this.emit("data", ["ctcp:version", this.reply(msg)]); + else + this.emit("data", ["message", this.reply(msg)]); + break; + case "NOTICE": + this.emit("data", ["notice", msg.params[1]]); + break; + default: + console.log(msg); break; - case "372": // motd - this.server.motd += `${msg.params[1]}\n`; - break; - case "375": // motd_start - this.server.motd = `${msg.params[1]}\n`; - break; - case "376": // motd_end - this.server.motd += `${msg.params[1]}\n`; - this.emit('data', ['motd', this.server.motd]); - break; - case "464": // Password required - if (this.options.password.length > 0) - this.send(`PASS ${this.options.password}`); // pwd - break; - case "PING": - this.send(`PONG ${msg.params.join``}`); - break; - case "JOIN": - console.log("join", msg); - this.send(`WHO ${msg.params[0]}`); - break; - case "PART": - console.log("part", msg); - delete this.server.user[msg.params[0]]; - //this.whois(msg.params[0], true); // force whois - break; - case "PRIVMSG": - if (msg.params[1] === "\u0001VERSION\u0001") - this.emit('data', ['ctcp:version', this.reply(msg)]); - else - this.emit('data', ['message', this.reply(msg)]); - break; - case "NOTICE": - this.emit('data', ['notice', msg.params[1]]); - break; - default: - console.log(msg); - break; } }); } send() { for (let i = 0; i < arguments.length; i++) this.socket.write(arguments[i]); - this.socket.write('\n'); + this.socket.write("\n"); } parse(data) { - var raw = data; - if (data.charAt(0) === ':') { - var i = data.indexOf(' '); - var prefix = data.slice(1, i); + let raw = data; + let i = 0; + let prefix = ""; + if (data.charAt(0) === ":") { + i = data.indexOf(" "); + prefix = data.slice(1, i); data = data.slice(i + 1); } - var i = data.indexOf(' '); + i = data.indexOf(" "); if (i === -1) i = data.length; var command = data.slice(0, i); data = data.slice(i + 1); var params = []; - while (data && data.charAt(0) !== ':') { - var i = data.indexOf(' '); + while (data && data.charAt(0) !== ":") { + i = data.indexOf(" "); if (i === -1) i = data.length; params.push(data.slice(0, i)); data = data.slice(i + 1); } if (data) { - data = data.replace(/\r?\n|\r/g, ''); + data = data.replace(/\r?\n|\r/g, ""); params.push(data.slice(1)); } return { @@ -232,10 +234,10 @@ class irc { }; } join(channel) { - this.send(`JOIN ${(typeof channel === "object") ? channel.join(',') : channel}`); + this.send(`JOIN ${(typeof channel === "object") ? channel.join(",") : channel}`); } part(channel, msg=false) { - this.send(`PART ${(typeof channel === "object") ? channel.join(',') : channel}${msg ? " " + msg : " part"}`); + this.send(`PART ${(typeof channel === "object") ? channel.join(",") : channel}${msg ? " " + msg : " part"}`); } whois(user, force = false) { user = user.toLowerCase(); diff --git a/src/inc/clients/tg.js b/src/inc/clients/tg.js index 873cd77..591cead 100644 --- a/src/inc/clients/tg.js +++ b/src/inc/clients/tg.js @@ -1,4 +1,4 @@ -let tgapi = require('node-telegram-bot-api'); +let tgapi = require("node-telegram-bot-api"); let EventEmitter = require("events").EventEmitter; let util = require("util"); @@ -10,11 +10,11 @@ class tg { this.options.polling = options.polling || true; this.client = new tgapi(this.options.token, this.options); - this.client.on('message', msg => { + this.client.on("message", msg => { if (msg.date >= (~~(Date.now() / 1000) - 10)) { try { - msg.text = msg.text && msg.text.match(/^\//g) ? msg.text.replace(/^\//g, "\.") : msg.text; - this.emit('data', ['message', this.reply(msg)]); + msg.text = msg.text && msg.text.match(/^\//g) ? msg.text.replace(/^\//g, ".") : msg.text; + this.emit("data", ["message", this.reply(msg)]); } catch(err) { console.log(err); diff --git a/src/inc/sql.js b/src/inc/sql.js index 66601f8..9c5cc58 100644 --- a/src/inc/sql.js +++ b/src/inc/sql.js @@ -1,4 +1,4 @@ -import mysql from 'nodejs-mysql'; +import mysql from "nodejs-mysql"; const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) ); export default sql; \ No newline at end of file diff --git a/src/inc/wrapper.js b/src/inc/wrapper.js index 0fd1f1c..ff0976d 100644 --- a/src/inc/wrapper.js +++ b/src/inc/wrapper.js @@ -1,30 +1,29 @@ -import { cfg, read } from './cfg.js'; -//import { loadEvents } from './lib.js'; +import { cfg } from "./cfg.js"; -const irclib = require('./clients/irc.js'); -const tglib = require('./clients/tg.js') -const util = require('util'); +const irclib = require("./clients/irc.js"); +const tglib = require("./clients/tg.js"); +const util = require("util"); -const EventEmitter = require('events').EventEmitter; +const EventEmitter = require("events").EventEmitter; const clients = []; const wrapper = function () { for (let srv in cfg.client) { switch (cfg.client[srv].type) { - case "irc": - clients.push({ - name: cfg.client[srv].network, - type: "irc", - client: new irclib(cfg.client[srv]) - }); - break; - case "tg": - clients.push({ - name: "tg", - type: "tg", - client: new tglib(cfg.client[srv]) - }); - break; + case "irc": + clients.push({ + name: cfg.client[srv].network, + type: "irc", + client: new irclib(cfg.client[srv]) + }); + break; + case "tg": + clients.push({ + name: "tg", + type: "tg", + client: new tglib(cfg.client[srv]) + }); + break; } }