tglib
This commit is contained in:
109
src/inc/clients/irc.js
Normal file
109
src/inc/clients/irc.js
Normal file
@ -0,0 +1,109 @@
|
||||
var net = require("net");
|
||||
var tls = require("tls");
|
||||
var EventEmitter = require("events").EventEmitter;
|
||||
var util = require("util");
|
||||
|
||||
class irc {
|
||||
constructor(options) {
|
||||
EventEmitter.call(this);
|
||||
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.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.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);
|
||||
});
|
||||
this.socket.setEncoding('utf-8');
|
||||
this.socket.on('data', msg => {
|
||||
msg = this.parse(msg);
|
||||
switch (msg.command) {
|
||||
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 "PRIVMSG":
|
||||
this.emit('message', this.reply(msg));
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
send() {
|
||||
for (let i = 0; i < arguments.length; i++)
|
||||
this.socket.write(arguments[i]);
|
||||
this.socket.write('\n');
|
||||
}
|
||||
parse(data) {
|
||||
var raw = data;
|
||||
if (data.charAt(0) === ':') {
|
||||
var i = data.indexOf(' ');
|
||||
var prefix = data.slice(1, i);
|
||||
data = data.slice(i + 1);
|
||||
}
|
||||
var 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(' ');
|
||||
if (i === -1)
|
||||
i = data.length;
|
||||
params.push(data.slice(0, i));
|
||||
data = data.slice(i + 1);
|
||||
}
|
||||
if (data)
|
||||
params.push(data.slice(1));
|
||||
return {
|
||||
raw: raw,
|
||||
prefix: prefix,
|
||||
command: command,
|
||||
params: params
|
||||
};
|
||||
}
|
||||
reply(tmp) {
|
||||
let usertmp = tmp.prefix.split("!");
|
||||
let hosttmp = usertmp[1].split("@");
|
||||
let user = {
|
||||
nick: usertmp[0],
|
||||
username: hosttmp[0],
|
||||
hostname: hosttmp[1]
|
||||
};
|
||||
return {
|
||||
type: "irc",
|
||||
network: this.network,
|
||||
channel: tmp.params[0],
|
||||
user: user,
|
||||
message: tmp.params[1].replace(/\r?\n|\r/g, ''),
|
||||
time: ~~(Date.now() / 1000),
|
||||
reply: msg => {
|
||||
this.send(`PRIVMSG ${tmp.params[0]} ${msg}`);
|
||||
},
|
||||
replyAction: msg => {
|
||||
this.send(`PRIVMSG ${tmp.params[0]} \u0001ACTION ${msg}\u0001`);
|
||||
},
|
||||
replyNotice: msg => {
|
||||
this.send(`NOTICE ${tmp.params[0]} ${msg}`);
|
||||
//rpc.emit('call', this.network, 'notice', [this.channel, msg]);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = irc;
|
||||
util.inherits(irc, EventEmitter);
|
47
src/inc/clients/tg.js
Normal file
47
src/inc/clients/tg.js
Normal file
@ -0,0 +1,47 @@
|
||||
let tgapi = require('node-telegram-bot-api');
|
||||
let EventEmitter = require("events").EventEmitter;
|
||||
let util = require("util");
|
||||
|
||||
class tg {
|
||||
constructor(options) {
|
||||
EventEmitter.call(this);
|
||||
this.options = options || {};
|
||||
this.token = options.token || null;
|
||||
this.options.polling = options.polling || true;
|
||||
this.client = new tgapi(this.options.token, this.options);
|
||||
|
||||
this.client.on('message', msg => {
|
||||
let time = ~~(Date.now() / 1000);
|
||||
if (msg.date >= (time - 10))
|
||||
this.emit('message', this.reply(msg));
|
||||
});
|
||||
}
|
||||
send(id, msg) {
|
||||
this.client.sendMessage(id, msg);
|
||||
}
|
||||
reply(tmp) {
|
||||
return {
|
||||
type: "tg",
|
||||
channel: tmp.chat.title,
|
||||
channelid: tmp.chat.id,
|
||||
user: {
|
||||
nick: tmp.from.first_name,
|
||||
username: tmp.from.username
|
||||
},
|
||||
message: tmp.text,
|
||||
time: tmp.date,
|
||||
reply: function (msg) {
|
||||
this.send(tmp.chat.id, msg);
|
||||
},
|
||||
replyAction: function (msg) {
|
||||
this.send(tmp.chat.id, msg);
|
||||
},
|
||||
replyNotice: function (msg) {
|
||||
this.send(tmp.chat.id, msg);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = tg;
|
||||
util.inherits(tg, EventEmitter);
|
Reference in New Issue
Block a user