Start working on moving to irc-framework

This commit is contained in:
Pavel Djundik 2016-03-07 23:09:42 +02:00 committed by Maxime Poulin
parent ab95419c89
commit da425fefaf
21 changed files with 165 additions and 167 deletions

View file

@ -33,6 +33,7 @@
"event-stream": "3.3.2",
"express": "4.13.4",
"lodash": "4.6.1",
"irc-framework": "1.0.3",
"mkdirp": "0.5.1",
"moment": "2.12.0",
"read": "1.0.7",

View file

@ -3,11 +3,9 @@ var Chan = require("./models/chan");
var crypto = require("crypto");
var identd = require("./identd");
var log = require("./log");
var net = require("net");
var Msg = require("./models/msg");
var Network = require("./models/network");
var slate = require("slate-irc");
var tls = require("tls");
var ircFramework = require("irc-framework");
var Helper = require("./helper");
module.exports = Client;
@ -25,7 +23,6 @@ var events = [
"link",
"names",
"nick",
"notice",
"part",
"quit",
"topic",
@ -33,7 +30,6 @@ var events = [
"whois"
];
var inputs = [
// These inputs are sorted in order that is most likely to be used
"msg",
"part",
"action",
@ -129,97 +125,69 @@ Client.prototype.connect = function(args) {
var config = Helper.getConfig();
var client = this;
if (config.lockNetwork) {
// This check is needed to prevent invalid user configurations
if (args.host && args.host.length > 0 && args.host !== config.defaults.host) {
var invalidHostnameMsg = new Msg({
type: Msg.Type.ERROR,
text: "Hostname you specified is not allowed."
});
client.emit("msg", {
msg: invalidHostnameMsg
});
return;
}
var nick = args.nick || "lounge-user";
args.host = config.defaults.host;
args.port = config.defaults.port;
args.tls = config.defaults.tls;
}
var server = {
var network = new Network({
name: args.name || "",
host: args.host || "",
port: parseInt(args.port, 10) || (args.tls ? 6697 : 6667),
rejectUnauthorized: false
};
if (server.host.length === 0) {
var emptyHostnameMsg = new Msg({
type: Msg.Type.ERROR,
text: "You must specify a hostname to connect."
});
client.emit("msg", {
msg: emptyHostnameMsg
});
return;
}
if (config.bind) {
server.localAddress = config.bind;
if (args.tls) {
var socket = net.connect(server);
server.socket = socket;
}
}
var stream = args.tls ? tls.connect(server) : net.connect(server);
stream.on("error", function(e) {
console.log("Client#connect():\n" + e);
stream.end();
var msg = new Msg({
type: Msg.Type.ERROR,
text: "Connection error."
});
client.emit("msg", {
msg: msg
});
});
var nick = args.nick || "lounge-user";
var username = args.username || nick.replace(/[^a-zA-Z0-9]/g, "");
var realname = args.realname || "The Lounge User";
var irc = slate(stream);
identd.hook(stream, username);
if (args.password) {
irc.pass(args.password);
}
irc.me = nick;
irc.nick(nick);
irc.user(username, realname);
var network = new Network({
name: server.name,
host: server.host,
port: server.port,
tls: !!args.tls,
password: args.password,
username: username,
realname: realname,
username: args.username || nick.replace(/[^a-zA-Z0-9]/g, ""),
realname: args.realname || "The Lounge User",
commands: args.commands
});
network.irc = irc;
client.networks.push(network);
client.emit("network", {
network: network
});
if (config.lockNetwork) {
// This check is needed to prevent invalid user configurations
if (args.host && args.host.length > 0 && args.host !== config.defaults.host) {
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
type: Msg.Type.ERROR,
text: "Hostname you specified is not allowed."
})
});
return;
}
network.host = config.defaults.host;
network.port = config.defaults.port;
network.tls = config.defaults.tls;
}
if (network.host.length === 0) {
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
type: Msg.Type.ERROR,
text: "You must specify a hostname to connect."
})
});
return;
}
var irc = new ircFramework.Client();
irc.connect({
host: network.host,
port: network.port,
nick: nick,
username: network.username,
gecos: network.realname,
password: network.password,
tls: network.tls,
localAddress: config.bind,
rejectUnauthorized: false,
auto_reconnect: false, // TODO: Enable auto reconnection
});
network.irc = irc;
events.forEach(function(plugin) {
var path = "./plugins/irc-events/" + plugin;
require(path).apply(client, [
@ -228,7 +196,39 @@ Client.prototype.connect = function(args) {
]);
});
irc.once("welcome", function() {
irc.on("raw socket connected", function() {
identd.hook(irc.socket, network.username);
});
irc.on("socket connected", function() {
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
text: "Connected to the network."
})
});
});
irc.on("socket close", function() {
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
type: Msg.Type.ERROR,
text: "Disconnected from the network."
})
});
});
irc.on("reconnecting", function() {
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
text: "Reconnecting..."
})
});
});
irc.on("registered", function() {
var delay = 1000;
var commands = args.commands;
if (Array.isArray(commands)) {
@ -242,16 +242,13 @@ Client.prototype.connect = function(args) {
delay += 1000;
});
}
setTimeout(function() {
irc.write("PING " + network.host);
}, delay);
});
irc.once("pong", function() {
var join = (args.join || "");
if (join) {
join = join.replace(/\,/g, " ").split(/\s+/g);
irc.join(join);
setTimeout(function() {
join = join.replace(/\,/g, " ").split(/\s+/g);
irc.join(join);
}, delay);
}
});
};

View file

@ -30,7 +30,7 @@ function Network(attr) {
}
Network.prototype.toJSON = function() {
var json = _.extend(this, {nick: (this.irc || {}).me || ""});
var json = _.extend(this, {nick: (this.irc && this.irc.user.nick) || ""});
return _.omit(json, "irc", "password");
};
@ -45,7 +45,7 @@ Network.prototype.export = function() {
"realname",
"commands"
]);
network.nick = (this.irc || {}).me;
network.nick = (this.irc && this.irc.user.nick) || "";
network.join = _.map(
_.filter(this.channels, {type: "channel"}),
"name"

View file

@ -3,6 +3,10 @@ var _ = require("lodash");
module.exports = User;
function User(attr) {
// TODO: Remove this
attr.name = attr.nick;
attr.mode = attr.modes[0] || "";
_.merge(this, _.extend({
mode: "",
name: ""

View file

@ -18,7 +18,7 @@ exports.input = function(network, chan, cmd, args) {
text
);
irc.emit("message", {
from: irc.me,
from: irc.user.nick,
to: chan.name,
message: "\u0001ACTION " + text
});

View file

@ -21,7 +21,7 @@ exports.input = function(network, chan, cmd, args) {
var channel = _.find(network.channels, {name: target});
if (typeof channel !== "undefined") {
irc.emit("message", {
from: irc.me,
from: irc.user.nick,
to: channel.name,
message: msg
});

View file

@ -20,8 +20,8 @@ exports.input = function(network, chan, cmd, args) {
var msg = new Msg({
type: Msg.Type.NOTICE,
mode: targetChan.getMode(irc.me),
from: irc.me,
mode: targetChan.getMode(irc.user.nick),
from: irc.user.nick,
text: message
});
targetChan.messages.push(msg);

View file

@ -1,22 +1,15 @@
var pkg = require(process.cwd() + "/package.json");
module.exports = function(irc/* , network */) {
irc.on("message", function(data) {
if (data.message.indexOf("\001") !== 0) {
return;
}
var msg = data.message.replace(/\001/g, "");
var split = msg.split(" ");
switch (split[0]) {
irc.on("ctcp request", function(data) {
switch (data.type) {
case "VERSION":
irc.ctcp(
data.from,
"VERSION " + pkg.name + " " + pkg.version
);
irc.ctcpResponse(data.nick, "VERSION " + pkg.name + " " + pkg.version);
break;
case "PING":
var split = data.msg.split(" ");
if (split.length === 2) {
irc.ctcp(data.from, "PING " + split[1]);
irc.ctcpResponse(data.nick, "PING " + split[1]);
}
break;
}

View file

@ -14,7 +14,7 @@ module.exports = function(irc, network) {
});
if (!network.connected) {
if (data.cmd === "ERR_NICKNAMEINUSE") {
var random = irc.me + Math.floor(10 + (Math.random() * 89));
var random = irc.user.nick + Math.floor(10 + (Math.random() * 89));
irc.nick(random);
}
}

View file

@ -16,7 +16,7 @@ module.exports = function(irc, network) {
from: data.from,
target: target,
text: data.channel,
invitedYou: target.toLowerCase() === irc.me.toLowerCase()
invitedYou: target === irc.user.nick
});
chan.messages.push(msg);
client.emit("msg", {

View file

@ -18,20 +18,16 @@ module.exports = function(irc, network) {
chan: chan
});
}
chan.users.push(new User({name: data.nick}));
chan.users.push(new User({nick: data.nick, modes: ""}));
chan.sortUsers();
client.emit("users", {
chan: chan.id
});
var self = false;
if (data.nick.toLowerCase() === irc.me.toLowerCase()) {
self = true;
}
var msg = new Msg({
from: data.nick,
hostmask: data.hostmask.username + "@" + data.hostmask.hostname,
hostmask: data.ident + "@" + data.hostname,
type: Msg.Type.JOIN,
self: self
self: data.nick === irc.user.nick
});
chan.messages.push(msg);
client.emit("msg", {

View file

@ -12,7 +12,7 @@ module.exports = function(irc, network) {
return;
}
if (data.client === irc.me) {
if (data.client === irc.user.nick) {
chan.users = [];
} else {
chan.users = _.without(chan.users, _.find(chan.users, {name: data.client}));
@ -22,17 +22,13 @@ module.exports = function(irc, network) {
chan: chan.id
});
var self = false;
if (data.nick.toLowerCase() === irc.me.toLowerCase()) {
self = true;
}
var msg = new Msg({
type: Msg.Type.KICK,
mode: mode,
from: from,
target: data.client,
text: data.message || "",
self: self
self: data.nick === irc.user.nick
});
chan.messages.push(msg);
client.emit("msg", {

View file

@ -27,7 +27,7 @@ module.exports = function(irc, network) {
return;
}
var self = data.to.toLowerCase() === irc.me.toLowerCase();
var self = data.to.toLowerCase() === irc.user.nick.toLowerCase();
var chan = _.find(network.channels, {name: self ? data.from : data.to});
if (typeof chan === "undefined") {
return;

View file

@ -7,22 +7,32 @@ module.exports = function(irc, network) {
var client = this;
var config = Helper.getConfig();
irc.on("message", function(data) {
if (data.message.indexOf("\u0001") === 0 && data.message.substring(0, 7) !== "\u0001ACTION") {
// Hide ctcp messages.
return;
}
irc.on("notice", function(data) {
data.type = Msg.Type.NOTICE;
handleMessage(data);
});
var target = data.to;
if (target.toLowerCase() === irc.me.toLowerCase()) {
target = data.from;
irc.on("action", function(data) {
data.type = Msg.Type.ACTION;
handleMessage(data);
});
irc.on("privmsg", function(data) {
data.type = Msg.Type.MESSAGE;
handleMessage(data);
});
function handleMessage(data) {
var target = data.target;
if (target.toLowerCase() === irc.user.nick.toLowerCase()) {
target = data.nick;
}
var chan = _.find(network.channels, {name: target});
if (typeof chan === "undefined") {
chan = new Chan({
type: Chan.Type.QUERY,
name: data.from
name: data.nick
});
network.channels.push(chan);
client.emit("join", {
@ -31,19 +41,11 @@ module.exports = function(irc, network) {
});
}
var type = Msg.Type.MESSAGE;
var text = data.message;
var textSplit = text.split(" ");
if (textSplit[0] === "\u0001ACTION") {
type = Msg.Type.ACTION;
text = text.replace(/^\u0001ACTION|\u0001$/g, "");
}
var self = (data.from.toLowerCase() === irc.me.toLowerCase());
var self = data.nick === irc.user.nick;
// Self messages are never highlighted
// Non-self messages are highlighted as soon as the nick is detected
var highlight = !self && textSplit.some(function(w) {
var highlight = !self && data.msg.split(" ").some(function(w) {
return (w.replace(/^@/, "").toLowerCase().indexOf(irc.me.toLowerCase()) === 0);
});
@ -55,12 +57,11 @@ module.exports = function(irc, network) {
}
}
var name = data.from;
var msg = new Msg({
type: type,
mode: chan.getMode(name),
from: name,
text: text,
type: data.type,
mode: chan.getMode(data.nick),
from: data.nick,
text: data.msg,
self: self,
highlight: highlight
});
@ -74,5 +75,5 @@ module.exports = function(irc, network) {
chan: chan.id,
msg: msg
});
});
}
};

View file

@ -13,16 +13,12 @@ module.exports = function(irc, network) {
if (from.indexOf(".") !== -1) {
from = data.target;
}
var self = false;
if (from.toLowerCase() === irc.me.toLowerCase()) {
self = true;
}
var msg = new Msg({
type: Msg.Type.MODE,
mode: chan.getMode(from),
from: from,
text: data.mode + " " + (data.client || ""),
self: self
self: from === irc.user.nick
});
chan.messages.push(msg);
client.emit("msg", {

View file

@ -4,16 +4,31 @@ module.exports = function(irc, network) {
var client = this;
irc.on("motd", function(data) {
var lobby = network.channels[0];
data.motd.forEach(function(text) {
if (data.motd) {
data.motd.split("\n").forEach(function(text) {
var msg = new Msg({
type: Msg.Type.MOTD,
text: text
});
lobby.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
});
}
if (data.error) {
var msg = new Msg({
type: Msg.Type.MOTD,
text: text
text: data.error
});
lobby.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
});
}
});
};

View file

@ -3,13 +3,13 @@ var User = require("../../models/user");
module.exports = function(irc, network) {
var client = this;
irc.on("names", function(data) {
irc.on("userlist", function(data) {
var chan = _.find(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
return;
}
chan.users = [];
_.each(data.names, function(u) {
_.each(data.users, function(u) {
chan.users.push(new User(u));
});
chan.sortUsers();

View file

@ -6,7 +6,7 @@ module.exports = function(irc, network) {
irc.on("nick", function(data) {
var self = false;
var nick = data["new"];
if (nick === irc.me) {
if (nick === irc.user.nick) {
var lobby = network.channels[0];
var msg = new Msg({
text: "You're now known as " + nick,

View file

@ -9,7 +9,7 @@ module.exports = function(irc, network) {
return;
}
var from = data.nick;
if (from === irc.me) {
if (from === irc.user.nick) {
network.channels = _.without(network.channels, chan);
client.save();
client.emit("part", {

View file

@ -17,7 +17,7 @@ module.exports = function(irc, network) {
from: from,
text: topic,
isSetByChan: from === chan.name,
self: (from.toLowerCase() === irc.me.toLowerCase())
self: from === irc.user.nick
});
chan.messages.push(msg);
client.emit("msg", {

View file

@ -2,13 +2,12 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("welcome", function(data) {
irc.on("registered", function(data) {
network.connected = true;
irc.write("PING " + network.host);
network.nick = data.nick;
var lobby = network.channels[0];
var nick = data;
var msg = new Msg({
text: "You're now known as " + nick
text: "You're now known as " + data.nick
});
lobby.messages.push(msg);
client.emit("msg", {
@ -18,7 +17,7 @@ module.exports = function(irc, network) {
client.save();
client.emit("nick", {
network: network.id,
nick: nick
nick: data.nick
});
});
};