danke eslint

This commit is contained in:
Flummi 2017-11-18 11:53:58 +01:00
parent eca4671b0b
commit 10130e3b5c
7 changed files with 206 additions and 182 deletions

28
.eslintrc.json Normal file
View File

@ -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"
]
}
}

View File

@ -1,16 +1,15 @@
import { cfg, read } from './inc/cfg.js'; import { read } from "./inc/cfg.js";
import { wrapper, clients } from './inc/wrapper.js'; import { wrapper, clients } from "./inc/wrapper.js";
const safeEval = require('safe-eval'); const safeEval = require("safe-eval");
read().then(() => { read().then(() => {
let bot = new wrapper(); let bot = new wrapper();
bot.on('message', e => { // Todo: eventhandler bot.on("message", e => { // Todo: eventhandler
let orig = e.message; let orig = e.message;
let tmp = orig.split(" "); let tmp = orig.split(" ");
let cmd = tmp[0].toLowerCase();
tmp.shift(); tmp.shift();
let args = tmp; 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 if (e.message.match(/^\.js /)) { // JS-Sandbox
args = e.message.substring(3); args = e.message.substring(3);
@ -21,10 +20,10 @@ read().then(() => {
console: { console: {
log: console.log log: console.log
} }
} };
try { try {
var output = safeEval(args, context); var output = safeEval(args, context);
if (typeof output !== undefined && output !== 'undefined' && output) { if (typeof output !== undefined && output !== "undefined" && output) {
let blah = JSON.stringify(output); let blah = JSON.stringify(output);
if (blah != "Converting circular structure to JSON") { 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); 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.`); e.replyAction(`drischt mit einem großen Stück Butterkäse auf ${args[0]} ein.`);
} }
}); });
bot.on('ctcp:version', e => { bot.on("ctcp:version", e => {
e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`) e.write(`notice ${e.user.nick} :\u0001VERSION Pimmel 2.1\u0001`);
}); });
}); });

View File

@ -1,4 +1,4 @@
import sql from './sql.js'; import sql from "./sql.js";
let cfg = { let cfg = {
client: {}, client: {},
@ -15,18 +15,14 @@ const read = () => new Promise((resolve, reject) => {
for(let row in rows) { for(let row in rows) {
cfg[rows[row].class][rows[row].key] = ((type, value) => { cfg[rows[row].class][rows[row].key] = ((type, value) => {
switch(type) { switch(type) {
case 'string': case "string":
return value; return value;
break; case "int":
case 'int': return parseInt(value);
return parseInt(value); case "bool":
break; return value === "true";
case 'bool': case "json":
return value === 'true'; return JSON.parse(value);
break;
case 'json':
return JSON.parse(value);
break;
} }
})(rows[row].type, rows[row].value); })(rows[row].type, rows[row].value);
} }

View File

@ -33,167 +33,169 @@ class irc {
this.send(`NICK ${this.nickname}`); this.send(`NICK ${this.nickname}`);
this.send(`USER ${this.username} 0 * : ${this.realname}`); this.send(`USER ${this.username} 0 * : ${this.realname}`);
}); });
this.socket.setEncoding('utf-8'); this.socket.setEncoding("utf-8");
this.socket.on('data', msg => { this.socket.on("data", msg => {
msg = this.parse(msg); msg = this.parse(msg);
let tmpuser = {}; let tmpuser = {};
switch (msg.command) { // auslagern! switch (msg.command) { // auslagern!
// WHOIS BEGIN // WHOIS BEGIN
case "307": // Rizon Account case "307": // Rizon Account
tmpuser = {}; tmpuser = {};
if(this.server.user.has( msg.params[1].toLowerCase() )) if(this.server.user.has( msg.params[1].toLowerCase() ))
tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); tmpuser = this.server.user.get( msg.params[1].toLowerCase() );
tmpuser.account = msg.params[1].toLowerCase(); tmpuser.account = msg.params[1].toLowerCase();
tmpuser.registered = true; tmpuser.registered = true;
this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); this.server.user.set( msg.params[1].toLowerCase(), tmpuser );
break; break;
case "311": // first case "311": // first
tmpuser = {}; tmpuser = {};
if (this.server.user.has( msg.params[1].toLowerCase() )) if (this.server.user.has( msg.params[1].toLowerCase() ))
tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); tmpuser = this.server.user.get( msg.params[1].toLowerCase() );
tmpuser.nickname = msg.params[1].toLowerCase(); tmpuser.nickname = msg.params[1].toLowerCase();
tmpuser.username = msg.params[2]; tmpuser.username = msg.params[2];
tmpuser.hostname = msg.params[3]; tmpuser.hostname = msg.params[3];
tmpuser.realname = msg.params[5]; tmpuser.realname = msg.params[5];
this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); this.server.user.set( msg.params[1].toLowerCase(), tmpuser );
break; break;
case "313": // Oper case "313": // Oper
tmpuser = {}; tmpuser = {};
if (this.server.user.has( msg.params[1].toLowerCase() )) if (this.server.user.has( msg.params[1].toLowerCase() ))
tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); tmpuser = this.server.user.get( msg.params[1].toLowerCase() );
tmpuser.oper = true; tmpuser.oper = true;
this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); this.server.user.set( msg.params[1].toLowerCase(), tmpuser );
break; break;
case "318": // last (check Data) case "318": // last (check Data)
tmpuser = {}; tmpuser = {};
if (this.server.user.has( msg.params[1].toLowerCase() )) if (this.server.user.has( msg.params[1].toLowerCase() ))
tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); tmpuser = this.server.user.get( msg.params[1].toLowerCase() );
tmpuser = { tmpuser = {
nickname: tmpuser.nickname || false, nickname: tmpuser.nickname || false,
username: tmpuser.username || false, username: tmpuser.username || false,
hostname: tmpuser.hostname || false, hostname: tmpuser.hostname || false,
realname: tmpuser.realname || false, realname: tmpuser.realname || false,
account: tmpuser.account || false, account: tmpuser.account || false,
registered: tmpuser.registered || false, registered: tmpuser.registered || false,
oper: tmpuser.oper || false, oper: tmpuser.oper || false,
channels: tmpuser.channels || [], channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000) cached: ~~(Date.now() / 1000)
}; };
this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); this.server.user.set( msg.params[1].toLowerCase(), tmpuser );
break; break;
case "319": // Chanlist Todo case "319": // Chanlist Todo
let tmpchan = new Map(); let tmpchan = new Map();
tmpuser = {}; tmpuser = {};
if (this.server.user.has( msg.params[1].toLowerCase() )) { if (this.server.user.has( msg.params[1].toLowerCase() )) {
tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); tmpuser = this.server.user.get( msg.params[1].toLowerCase() );
if(tmpuser.channels) if(tmpuser.channels)
tmpchan = tmpuser.channels; tmpchan = tmpuser.channels;
} }
let chans = msg.params[2].trim().split(" "); let chans = msg.params[2].trim().split(" ");
for(let chan in chans) { for(let chan in chans) {
chan = chans[chan].split("#"); chan = chans[chan].split("#");
tmpchan.set(`#${chan[1]}`, chan[0]); tmpchan.set(`#${chan[1]}`, chan[0]);
} }
tmpuser.channels = tmpchan; tmpuser.channels = tmpchan;
this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); this.server.user.set( msg.params[1].toLowerCase(), tmpuser );
break; break;
case "330": // Quarknet case "330": // Quarknet
tmpuser = {}; tmpuser = {};
if (this.server.user.has( msg.params[1].toLowerCase() )) if (this.server.user.has( msg.params[1].toLowerCase() ))
tmpuser = this.server.user.get( msg.params[1].toLowerCase() ); tmpuser = this.server.user.get( msg.params[1].toLowerCase() );
tmpuser.account = msg.params[2]; tmpuser.account = msg.params[2];
tmpuser.registered = true; tmpuser.registered = true;
this.server.user.set( msg.params[1].toLowerCase(), tmpuser ); this.server.user.set( msg.params[1].toLowerCase(), tmpuser );
break; break;
// WHOIS END // WHOIS END
case "001": // welcome case "001": // welcome
this.server.me = msg.params[0]; this.server.me = msg.params[0];
this.join(this.options.channels); this.join(this.options.channels);
this.emit('data', ['connected', msg.params[1]]); 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]);
break; break;
case "315": // who_end case "352": // who_entry
console.log(this.server.channel); 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; 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() { send() {
for (let i = 0; i < arguments.length; i++) for (let i = 0; i < arguments.length; i++)
this.socket.write(arguments[i]); this.socket.write(arguments[i]);
this.socket.write('\n'); this.socket.write("\n");
} }
parse(data) { parse(data) {
var raw = data; let raw = data;
if (data.charAt(0) === ':') { let i = 0;
var i = data.indexOf(' '); let prefix = "";
var prefix = data.slice(1, i); if (data.charAt(0) === ":") {
i = data.indexOf(" ");
prefix = data.slice(1, i);
data = data.slice(i + 1); data = data.slice(i + 1);
} }
var i = data.indexOf(' '); i = data.indexOf(" ");
if (i === -1) if (i === -1)
i = data.length; i = data.length;
var command = data.slice(0, i); var command = data.slice(0, i);
data = data.slice(i + 1); data = data.slice(i + 1);
var params = []; var params = [];
while (data && data.charAt(0) !== ':') { while (data && data.charAt(0) !== ":") {
var i = data.indexOf(' '); i = data.indexOf(" ");
if (i === -1) if (i === -1)
i = data.length; i = data.length;
params.push(data.slice(0, i)); params.push(data.slice(0, i));
data = data.slice(i + 1); data = data.slice(i + 1);
} }
if (data) { if (data) {
data = data.replace(/\r?\n|\r/g, ''); data = data.replace(/\r?\n|\r/g, "");
params.push(data.slice(1)); params.push(data.slice(1));
} }
return { return {
@ -232,10 +234,10 @@ class irc {
}; };
} }
join(channel) { join(channel) {
this.send(`JOIN ${(typeof channel === "object") ? channel.join(',') : channel}`); this.send(`JOIN ${(typeof channel === "object") ? channel.join(",") : channel}`);
} }
part(channel, msg=false) { 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) { whois(user, force = false) {
user = user.toLowerCase(); user = user.toLowerCase();

View File

@ -1,4 +1,4 @@
let tgapi = require('node-telegram-bot-api'); let tgapi = require("node-telegram-bot-api");
let EventEmitter = require("events").EventEmitter; let EventEmitter = require("events").EventEmitter;
let util = require("util"); let util = require("util");
@ -10,11 +10,11 @@ class tg {
this.options.polling = options.polling || true; this.options.polling = options.polling || true;
this.client = new tgapi(this.options.token, this.options); 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)) { if (msg.date >= (~~(Date.now() / 1000) - 10)) {
try { try {
msg.text = msg.text && msg.text.match(/^\//g) ? msg.text.replace(/^\//g, "\.") : msg.text; msg.text = msg.text && msg.text.match(/^\//g) ? msg.text.replace(/^\//g, ".") : msg.text;
this.emit('data', ['message', this.reply(msg)]); this.emit("data", ["message", this.reply(msg)]);
} }
catch(err) { catch(err) {
console.log(err); console.log(err);

View File

@ -1,4 +1,4 @@
import mysql from 'nodejs-mysql'; import mysql from "nodejs-mysql";
const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) ); const sql = mysql.getInstance( require(`${__dirname}/../../cfg/mysql.json`) );
export default sql; export default sql;

View File

@ -1,30 +1,29 @@
import { cfg, read } from './cfg.js'; import { cfg } from "./cfg.js";
//import { loadEvents } from './lib.js';
const irclib = require('./clients/irc.js'); const irclib = require("./clients/irc.js");
const tglib = require('./clients/tg.js') const tglib = require("./clients/tg.js");
const util = require('util'); const util = require("util");
const EventEmitter = require('events').EventEmitter; const EventEmitter = require("events").EventEmitter;
const clients = []; const clients = [];
const wrapper = function () { const wrapper = function () {
for (let srv in cfg.client) { for (let srv in cfg.client) {
switch (cfg.client[srv].type) { switch (cfg.client[srv].type) {
case "irc": case "irc":
clients.push({ clients.push({
name: cfg.client[srv].network, name: cfg.client[srv].network,
type: "irc", type: "irc",
client: new irclib(cfg.client[srv]) client: new irclib(cfg.client[srv])
}); });
break; break;
case "tg": case "tg":
clients.push({ clients.push({
name: "tg", name: "tg",
type: "tg", type: "tg",
client: new tglib(cfg.client[srv]) client: new tglib(cfg.client[srv])
}); });
break; break;
} }
} }