migrate irc + tg client changes from uwe

This commit is contained in:
jkhsjdhjs 2019-08-18 00:30:38 +02:00
parent 5a33c21887
commit 3a895eda83
Signed by: jkhsjdhjs
GPG Key ID: BAC6ADBAB7D576CC
4 changed files with 37 additions and 77 deletions

View File

@ -5,9 +5,11 @@ import tls from "tls";
import EventEmitter from "events"; import EventEmitter from "events";
const colors = { const colors = {
red: "\x0304$1\x0304", red: "04",
blue: "\x0312$1\x0312", blue: "12",
yellow: "\x0308$1\x0308" yellow: "08",
green: "03",
brown: "05"
}; };
const msgmodes = { const msgmodes = {
normal: "PRIVMSG {recipient} :{msg}", normal: "PRIVMSG {recipient} :{msg}",
@ -17,7 +19,7 @@ const msgmodes = {
const replaceColor = (match, color, text) => { const replaceColor = (match, color, text) => {
if (colors.hasOwnProperty(color)) if (colors.hasOwnProperty(color))
return colors[color].replace("\$1", text); return `\x03${colors[color]}${text}\x0F`;
return text; return text;
}; };
@ -45,7 +47,6 @@ export class irc extends EventEmitter {
this.server = { this.server = {
set: this.set, set: this.set,
motd: "", motd: "",
debug: false,
me: {}, me: {},
channel: [], channel: [],
user: new Map() user: new Map()
@ -80,16 +81,14 @@ export class irc extends EventEmitter {
this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", e) ); this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", e) );
}); });
} }
parse(data, [a, ...b] = data.split(/ +:/)) { parse(data, [a, ...b] = data.split(/ +:/), tmp = a.split(" ").concat(b)) {
let tmp = a.split(" ").concat(b); let prefix = data.charAt(0) === ":" ? tmp.shift() : null
return data.charAt(0) === ":" ? { , command = tmp.shift()
prefix: tmp.shift(), , params = command.toLowerCase() === "privmsg" ? [ tmp.shift(), tmp.join(" :") ] : tmp;
command: tmp.shift(), return {
params: tmp prefix: prefix,
} : { command: command,
prefix: null, params: params
command: tmp.shift(),
params: tmp
}; };
} }
reply(tmp) { reply(tmp) {
@ -99,10 +98,14 @@ export class irc extends EventEmitter {
channel: tmp.params[0], channel: tmp.params[0],
channelid: tmp.params[0], channelid: tmp.params[0],
user: Object.assign(this.parsePrefix(tmp.prefix), { 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, account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix
}))
}), }),
message: tmp.params[1], message: tmp.params[1].replace(/\u0002/, ""),
time: ~~(Date.now() / 1000), time: ~~(Date.now() / 1000),
raw: tmp, raw: tmp,
reply: msg => this.sendmsg("normal", tmp.params[0], this.format(""+msg)), reply: msg => this.sendmsg("normal", tmp.params[0], this.format(""+msg)),
@ -128,8 +131,8 @@ export class irc extends EventEmitter {
whois(user, force = false) { whois(user, force = false) {
user = user.toLowerCase(); user = user.toLowerCase();
let tmpuser = {}; let tmpuser = {};
if(this.server.user.hasi(user) && !force) { if(this.server.user.has(user) && !force) {
tmpuser = this.server.user.geti(user); tmpuser = this.server.user.get(user);
if(tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime) if(tmpuser.cached >= ~~(Date.now() / 1000) - this._recachetime)
return; return;
} }
@ -167,21 +170,3 @@ export class irc extends EventEmitter {
; ;
} }
} }
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);
};

View File

@ -1,6 +1,11 @@
export default client => { export default client => {
client._cmd.set("PRIVMSG", function (msg) { // privmsg client._cmd.set("PRIVMSG", function (msg) { // privmsg
this.emit("data", msg.params[1] === "\u0001VERSION\u0001" ? ["ctcp:version", this.reply(msg)] : ["message", this.reply(msg)]); if(msg.params[1] === "\u0001VERSION\u0001")
return this.emit("data", ["ctcp:version", this.reply(msg)]);
else if(msg.params[1].match(/^\u0001PING .*\u0001/i))
return this.emit("data", ["ctcp:ping", this.reply(msg)]);
else
this.emit("data", ["message", this.reply(msg)]);
}.bind(client)); }.bind(client));
client._cmd.set("NOTICE", function (msg) { // notice client._cmd.set("NOTICE", function (msg) { // notice

View File

@ -75,21 +75,3 @@ export default client => {
this.server.user.set(msg.params[1], tmpuser); this.server.user.set(msg.params[1], tmpuser);
}.bind(client)); }.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);
};

View File

@ -79,7 +79,7 @@ export class tg extends EventEmitter {
method: "POST", method: "POST",
body: { body: {
chat_id: chatid, chat_id: chatid,
text: msg, text: msg.split("\n").length > 1 ? `<code>${msg}</code>` : msg,
parse_mode: "HTML" parse_mode: "HTML"
} }
}; };
@ -100,17 +100,23 @@ export class tg extends EventEmitter {
channel: tmp.chat.title, channel: tmp.chat.title,
channelid: tmp.chat.id, channelid: tmp.chat.id,
user: { 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}`, prefix: `${tmp.from.username}!${tmp.from.id}`,
nick: tmp.from.first_name, nick: tmp.from.first_name,
username: tmp.from.username, username: tmp.from.username,
account: tmp.from.id.toString() account: tmp.from.id.toString()
})
}, },
self: this.server, self: this.server,
message: tmp.text, message: tmp.text,
time: tmp.date, time: tmp.date,
raw: tmp, raw: tmp,
reply: msg => this.send(tmp.chat.id, this.format(msg), tmp.message_id), 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), replyAction: msg => this.send(tmp.chat.id, this.format(`Uwe ${msg}`), tmp.message_id),
replyNotice: msg => this.send(tmp.chat.id, this.format(msg), tmp.message_id), replyNotice: msg => this.send(tmp.chat.id, this.format(msg), tmp.message_id),
_user: this.server.user _user: this.server.user
}; };
@ -125,21 +131,3 @@ export class tg extends EventEmitter {
; ;
} }
} }
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);
};