This commit is contained in:
Flummi 2018-09-07 19:22:47 +02:00
parent db7e2e9f39
commit 7c8a83fa3c
17 changed files with 643 additions and 0 deletions

15
package.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "cuffeo",
"version": "1.0.0",
"description": "lul",
"main": "index.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@gitfap.de:keinBot/cuffeo.git"
},
"author": "Flummi",
"license": "ISC"
}

196
src/clients/irc.mjs Normal file
View File

@ -0,0 +1,196 @@
import { logger } from "../inc/log";
import { getLevel } from "../inc/admin";
import modules from "./irc/index";
import net from "net";
import tls from "tls";
import EventEmitter from "events";
const colors = {
red: "\x0304$1\x0304",
blue: "\x0312$1\x0312",
yellow: "\x0308$1\x0308"
};
const msgmodes = {
normal: "PRIVMSG {recipient} :{msg}",
action: "PRIVMSG {recipient} :\u0001ACTION {msg}\u0001",
notice: "NOTICE {recipient} :{msg}"
};
const replaceColor = (match, color, text) => {
if (colors.hasOwnProperty(color))
return colors[color].replace("\$1", text);
return text;
};
export class irc extends EventEmitter {
constructor(options) {
super();
this.options = options || {};
this.options.channels = this.options.channels || [];
this.options.host = this.options.host || "127.0.0.1";
this.options.port = this.options.port || 6667;
this.options.ssl = this.options.ssl || false;
this.options.selfSigned = this.options.selfSigned || false;
this.options.sasl = this.options.sasl || false;
this.network = this.options.network || "test";
this.nickname = this.options.nickname || "test";
this.username = this.options.username || "test";
this.realname = this.options.realname || "test";
this.channels = this.options.channels || [];
this.set = this.options.set || "all";
this._recachetime = 60 * 30; // 30 minutes
this._cmd = new Map();
modules.forEach(mod => mod(this));
this.server = {
set: this.set,
motd: "",
debug: false,
me: {},
channel: [],
user: new Map()
};
this.socket = (this.options.ssl ? tls : net).connect({
host: this.options.host,
port: this.options.port,
rejectUnauthorized: !this.options.selfSigned
}, () => {
this.send(`NICK ${this.nickname}`);
this.send(`USER ${this.username} 0 * : ${this.realname}`);
if(this.options.sasl)
this.send("CAP LS");
});
this.socket.setEncoding("utf-8");
this.socket.on("data", msg => {
msg.split(/\r?\n|\r/).filter(tmp => tmp.length > 0).forEach(tmp => {
const cmd = this.parse(tmp);
if (this._cmd.has(cmd.command))
this._cmd.get(cmd.command)(cmd);
})
});
}
send(data) {
this.socket.write(`${data}\n`);
logger.debug(`(${this.network}) out: ${data}`);
}
sendmsg(mode, recipient, msg) {
msg = msg.split(/\r?\n/);
if(msg.length > 6)
return false;
msg.forEach(e => {
this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", e) );
});
}
parse(data, [a, ...b] = data.split(/ +:/)) {
let tmp = a.split(" ").concat(b);
logger.debug(`(${this.network}) in: ${[...tmp]}`);
return data.charAt(0) === ":" ? {
prefix: tmp.shift(),
command: tmp.shift(),
params: tmp
} : {
prefix: null,
command: tmp.shift(),
params: tmp
};
}
reply(tmp) {
return {
type: "irc",
network: this.network,
channel: tmp.params[0],
channelid: tmp.params[0],
user: Object.assign(this.parsePrefix(tmp.prefix), {
account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix,
level: getLevel(this.network, Object.assign(this.parsePrefix(tmp.prefix), {
account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix
}))
}),
message: tmp.params[1],
time: ~~(Date.now() / 1000),
raw: tmp,
reply: msg => this.sendmsg("normal", tmp.params[0], this.format(""+msg)),
replyAction: msg => this.sendmsg("action", tmp.params[0], this.format(""+msg)),
replyNotice: msg => this.sendmsg("notice", tmp.params[0], this.format(""+msg)),
self: this.server,
_chan: this.server.channel[tmp.params[0]],
_user: this.server.user,
_cmd: this._cmd,
join: chan => this.join(chan),
part: (chan, msg) => this.part(chan, msg),
whois: user => this.whois(user),
write: msg => this.send(msg),
socket: this.socket
};
}
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"}`);
}
whois(user, force = false) {
user = user.toLowerCase();
let tmpuser = {};
if(this.server.user.hasi(user) && !force) {
tmpuser = this.server.user.geti(user);
if(tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime)
return;
}
tmpuser = {
nickname: tmpuser.nickname || false,
username: tmpuser.username || false,
hostname: tmpuser.hostname || false,
realname: tmpuser.realname || false,
account: tmpuser.account || false,
prefix: tmpuser.prefix || false,
registered: tmpuser.registered || false,
oper: tmpuser.oper || false,
channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000)
};
this.server.user.set(user, tmpuser);
this.send(`WHOIS ${user}`);
}
parsePrefix(prefix) {
prefix = /:?(.*)\!(.*)@(.*)/.exec(prefix);
if(!prefix)
return false; //this.parsePrefix(arguments);
return {
nick: prefix[1],
username: prefix[2],
hostname: prefix[3]
};
}
format(msg) {
return msg
.replace(/\[b\](.*?)\[\/b\]/g, "\x02$1\x02") // bold
.replace(/\[i\](.*?)\[\/i\]/g, "\x1D$1\x1D") // italic
.replace(/\[color=(.*?)](.*?)\[\/color\]/g, replaceColor) // colors
;
}
}
Map.prototype.hasi = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
return true;
return false;
};
Map.prototype.geti = function(val) {
for (let [key, value] of this)
if(key.toLowerCase() === val.toLowerCase())
return value;
return false;
};
Map.prototype.deli = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
this.delete(key);
};

21
src/clients/irc/cap.mjs Normal file
View File

@ -0,0 +1,21 @@
export default client => {
client._cmd.set("CAP", function (msg) { // capkram
switch (msg.params[1]) {
case "LS": // list
this.send(`CAP REQ :${msg.params[2]}`);
break;
case "ACK": // success
this.send("AUTHENTICATE PLAIN");
break;
}
}.bind(client));
client._cmd.set("AUTHENTICATE", function (msg) { // auth
if (msg.params[0].match(/\+/))
this.send(`AUTHENTICATE ${new Buffer(this.username + "\u0000" + this.username + "\u0000" + this.options.password).toString("base64")}`);
}.bind(client));
client._cmd.set("900", function (msg) { // cap end
this.send("CAP END");
}.bind(client));
};

19
src/clients/irc/index.mjs Normal file
View File

@ -0,0 +1,19 @@
import cap from "./cap";
import invite from "./invite";
import join from "./join";
import motd from "./motd";
import msg from "./msg";
import nick from "./nick";
import part from "./part";
import ping from "./ping";
import pwdreq from "./pwdreq";
import welcome from "./welcome";
import who from "./who";
import whois from "./whois";
export default [
cap, invite, join,
motd, msg, nick,
part, ping, pwdreq,
welcome, who, whois
];

View File

@ -0,0 +1,13 @@
export default client => {
client._cmd.set("INVITE", function (msg) { // invite
const user = this.parsePrefix(msg.prefix);
const channel = msg.params[1];
if(!this.server.channel.includes(channel)) {
this.join(channel);
setTimeout(() => {
this.send(`PRIVMSG ${channel} :Hi. Wurde von ${user.nick} eingeladen.`);
}, 1000);
}
}.bind(client));
};

5
src/clients/irc/join.mjs Normal file
View File

@ -0,0 +1,5 @@
export default client => {
client._cmd.set("JOIN", function (msg) { // join
this.send(`WHO ${msg.params[0]}`);
}.bind(client));
};

14
src/clients/irc/motd.mjs Normal file
View File

@ -0,0 +1,14 @@
export default client => {
client._cmd.set("372", function (msg) { // motd_entry
this.server.motd += `${msg.params[1]}\n`;
}.bind(client));
client._cmd.set("375", function (msg) { // motd_start
this.server.motd = `${msg.params[1]}\n`;
}.bind(client));
client._cmd.set("376", function (msg) { // motd_end
this.server.motd += `${msg.params[1]}\n`;
this.emit("data", ["motd", this.server.motd]);
}.bind(client));
};

9
src/clients/irc/msg.mjs Normal file
View File

@ -0,0 +1,9 @@
export default client => {
client._cmd.set("PRIVMSG", function (msg) { // privmsg
this.emit("data", msg.params[1] === "\u0001VERSION\u0001" ? ["ctcp:version", this.reply(msg)] : ["message", this.reply(msg)]);
}.bind(client));
client._cmd.set("NOTICE", function (msg) { // notice
this.emit("data", ["notice", msg.params[1]]);
}.bind(client));
};

8
src/clients/irc/nick.mjs Normal file
View File

@ -0,0 +1,8 @@
export default client => {
client._cmd.set("NICK", function (msg) { // nickchange
let prefix = this.parsePrefix(msg.prefix);
if (this.server.user.hasi(prefix.nick))
this.server.user.deli(prefix.nick);
this.whois(msg.params[0], true); // force
}.bind(client));
};

5
src/clients/irc/part.mjs Normal file
View File

@ -0,0 +1,5 @@
export default client => {
client._cmd.set("PART", function (msg) { // part
//delete this.server.user[msg.params[0]];
}.bind(client));
};

5
src/clients/irc/ping.mjs Normal file
View File

@ -0,0 +1,5 @@
export default client => {
client._cmd.set("PING", function (msg) { // ping
this.send(`PONG ${msg.params.join``}`);
}.bind(client));
};

View File

@ -0,0 +1,6 @@
export default client => {
client._cmd.set("464", function (msg) { // motd_entry
if (this.options.password.length > 0 && !this.options.sasl)
this.send(`PASS ${this.options.password}`);
}.bind(client));
};

View File

@ -0,0 +1,6 @@
export default client => {
client._cmd.set("001", function (msg) { // welcome
this.join(this.options.channels);
this.emit("data", ["connected", msg.params[1]]);
}.bind(client));
};

27
src/clients/irc/who.mjs Normal file
View File

@ -0,0 +1,27 @@
const max = 400;
let whois = [];
export default client => {
client._cmd.set("352", function (msg) { // 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]
});
whois.push(msg.params[5]);
}.bind(client));
client._cmd.set("315", function (msg) { // who_end
this.whois(whois.reduce((a, b) => {
a += `${b},`;
if(a.length >= max) {
this.whois(a.slice(0, -1));
a = "";
}
return a;
}, "").slice(0, -1));
whois = [];
}.bind(client));
};

95
src/clients/irc/whois.mjs Normal file
View File

@ -0,0 +1,95 @@
export default client => {
client._cmd.set("307", function (msg) { // whois_identified (ircd-hybrid)
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.account = msg.params[1];
tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("311", function (msg) { // whois_userdata
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.nickname = msg.params[1];
tmpuser.username = msg.params[2];
tmpuser.hostname = msg.params[3];
tmpuser.realname = msg.params[5];
tmpuser.prefix = `${msg.params[1]}!${msg.params[2]}@${msg.params[3]}`;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("313", function (msg) { // whois_oper
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.oper = true;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("318", function (msg) { // whois_end
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser = {
nickname: tmpuser.nickname || false,
username: tmpuser.username || false,
hostname: tmpuser.hostname || false,
realname: tmpuser.realname || false,
account: tmpuser.account || false,
prefix: tmpuser.prefix || false,
registered: tmpuser.registered || false,
oper: tmpuser.oper || false,
channels: tmpuser.channels || [],
cached: ~~(Date.now() / 1000)
};
if(msg.params[0] === msg.params[1])
this.server.me = tmpuser;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("319", function (msg) { // whois_chanlist
let tmpchan = new Map()
, tmpuser = {};
if (this.server.user.hasi(msg.params[1])) {
tmpuser = this.server.user.geti(msg.params[1]);
if (tmpuser.channels)
tmpchan = new Map(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;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
client._cmd.set("330", function (msg) { // whois_authed_as (snircd)
let tmpuser = {};
if (this.server.user.hasi(msg.params[1]))
tmpuser = this.server.user.geti(msg.params[1]);
tmpuser.account = msg.params[2];
tmpuser.registered = true;
this.server.user.set(msg.params[1], tmpuser);
}.bind(client));
};
Map.prototype.hasi = function (val) {
for (let [key] of this)
if (key.toLowerCase() === val.toLowerCase())
return true;
return false;
};
Map.prototype.geti = function (val) {
for (let [key, value] of this)
if (key.toLowerCase() === val.toLowerCase())
return value;
return false;
};
Map.prototype.deli = function (val) {
for (let [key] of this)
if (key.toLowerCase() === val.toLowerCase())
this.delete(key);
};

159
src/clients/tg.mjs Normal file
View File

@ -0,0 +1,159 @@
import { logger } from "../inc/log";
import { getLevel } from "../inc/admin";
import rp from "request-promise-native";
import EventEmitter from "events";
export class tg extends EventEmitter {
constructor(options) {
super();
this.options = options || {};
this.token = options.token || null;
this.options.pollrate = options.pollrate || 1000;
this.set = this.options.set || "all";
this.network = "Telegram";
this.api = `https://api.telegram.org/bot${this.token}`;
this.lastUpdate = 0;
this.lastMessage = 0;
this.server = {
set: this.set,
channel: new Map(),
user: new Map(),
me: {}
};
this.connect().then(() => {
this.poller = setInterval(() => { this.poll(); }, this.options.pollrate);
});
}
connect() {
return new Promise((resolve, reject) => {
rp(`${this.api}/getMe`, { json: true })
.then(res => {
if(res.ok) {
this.me = res.result;
this.server.me = {
nickname: res.result.first_name,
username: res.result.username,
account: res.result.id.toString(),
prefix: `${res.result.username}!${res.result.id.toString()}`,
id: res.result.id.toString()
};
resolve();
}
else {
logger.error(`(${this.network}) ${res}`);
reject();
}
})
.catch(err => {
logger.error(`(${this.network}) ${err.message}`);
reject();
});
});
}
poll() {
rp(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`, { json:true })
.then(res => {
if(res.ok && res.result.length > 0) {
res = res.result[res.result.length-1];
this.lastUpdate = res.update_id + 1;
if (res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) {
this.lastMessage = res.message.message_id;
if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
this.server.user.set(res.message.from.username || res.message.from.first_name, {
nick: res.message.from.first_name,
username: res.message.from.username,
account: res.message.from.id.toString(),
prefix: `${res.message.from.username}!${res.message.from.id.toString()}`,
id: res.message.from.id
});
}
this.emit("data", ["message", this.reply(res.message)]);
}
}
})
.catch(err => {
if(err.statusCode !== 409)
logger.error(`(${this.network}) ${err.message}`);
});
}
send(chatid, msg, reply = null) {
if(msg.length === 0 || msg.length > 2048)
return false;
const opts = {
method: 'POST',
uri: `${this.api}/sendMessage`,
body: {
chat_id: chatid,
text: msg,
parse_mode: "HTML"
},
json: true
};
if(reply)
opts.body.reply_to_message_id = reply;
rp(opts)
.then(res => {})
.catch(err => {
logger.error(`(${this.network}) ${err.message}`);
});
}
sendmsg(mode, recipient, msg) {
this.send(recipient, msg);
}
reply(tmp) {
return {
type: "tg",
network: "Telegram",
channel: tmp.chat.title,
channelid: tmp.chat.id,
user: {
prefix: `${tmp.from.username}!${tmp.from.id}`,
nick: tmp.from.first_name,
username: tmp.from.username,
account: tmp.from.id.toString(),
level: getLevel("Telegram", {
prefix: `${tmp.from.username}!${tmp.from.id}`,
nick: tmp.from.first_name,
username: tmp.from.username,
account: tmp.from.id.toString()
})
},
self: this.server,
message: tmp.text,
time: tmp.date,
raw: tmp,
reply: msg => this.send(tmp.chat.id, this.format(msg), tmp.message_id),
replyAction: msg => this.send(tmp.chat.id, this.format(`f0ck ${msg}`), tmp.message_id),
replyNotice: msg => this.send(tmp.chat.id, this.format(msg), tmp.message_id),
_user: this.server.user
};
}
format(msg) {
return msg.toString()
.split("<").join("&lt;")
.split(">").join("&gt;")
.replace(/\[b\](.*?)\[\/b\]/g, "<b>$1</b>") // bold
.replace(/\[i\](.*?)\[\/i\]/g, "<i>$1</i>") // italic
.replace(/\[color=(.*?)](.*?)\[\/color\]/g, "$2")
;
}
}
Map.prototype.hasi = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
return true;
return false;
};
Map.prototype.geti = function(val) {
for (let [key, value] of this)
if(key.toLowerCase() === val.toLowerCase())
return value;
return false;
};
Map.prototype.deli = function(val) {
for (let [key] of this)
if(key.toLowerCase() === val.toLowerCase())
this.delete(key);
};

40
src/wrapper.mjs Normal file
View File

@ -0,0 +1,40 @@
import { irc as irclib } from "./clients/irc";
import { tg as tglib } from "./clients/tg";
import EventEmitter from "events";
const clients = [];
const wrapper = class wrapper extends EventEmitter {
constructor(cfg) {
super();
for (let srv in cfg) {
if(cfg[srv].val.enabled) {
switch (cfg[srv].val.type) {
case "irc":
clients.push({
name: cfg[srv].val.network,
type: "irc",
client: new irclib(cfg[srv].val)
});
break;
case "tg":
clients.push({
name: "tg",
type: "tg",
client: new tglib(cfg[srv].val)
});
break;
}
}
}
clients.forEach(client => {
client.client.on("data", e => {
this.emit(e[0], e[1]);
});
});
}
};
export { wrapper, clients };