Move connection events to a separate file, sync PREFIX with frontend

This commit is contained in:
Pavel Djundik 2016-03-08 20:50:48 +02:00 committed by Maxime Poulin
parent 470b0a2c4f
commit 28ae544b2a
6 changed files with 97 additions and 53 deletions

View file

@ -146,6 +146,7 @@ $(function() {
}) })
); );
var channels = $.map(data.networks, function(n) { var channels = $.map(data.networks, function(n) {
sidebar.find("#network-" + n.id).data("options", n.serverOptions);
return n.channels; return n.channels;
}); });
chat.html( chat.html(
@ -294,6 +295,7 @@ $(function() {
networks: [data.network] networks: [data.network]
}) })
); );
sidebar.find("#network-" + data.network.id).data("options", data.network.serverOptions);
chat.append( chat.append(
render("chat", { render("chat", {
channels: data.network.channels channels: data.network.channels
@ -310,6 +312,10 @@ $(function() {
sortable(); sortable();
}); });
socket.on("network_changed", function(data) {
sidebar.find("#network-" + data.network).data("options", data.serverOptions);
});
socket.on("nick", function(data) { socket.on("nick", function(data) {
var id = data.network; var id = data.network;
var nick = data.nick; var nick = data.nick;

View file

@ -1,7 +1,6 @@
var _ = require("lodash"); var _ = require("lodash");
var Chan = require("./models/chan"); var Chan = require("./models/chan");
var crypto = require("crypto"); var crypto = require("crypto");
var identd = require("./identd");
var log = require("./log"); var log = require("./log");
var Msg = require("./models/msg"); var Msg = require("./models/msg");
var Network = require("./models/network"); var Network = require("./models/network");
@ -12,6 +11,7 @@ module.exports = Client;
var id = 0; var id = 0;
var events = [ var events = [
"connection",
"ctcp", "ctcp",
"error", "error",
"invite", "invite",
@ -172,8 +172,8 @@ Client.prototype.connect = function(args) {
return; return;
} }
var irc = new ircFramework.Client(); network.irc = new ircFramework.Client();
irc.connect({ network.irc.connect({
host: network.host, host: network.host,
port: network.port, port: network.port,
nick: nick, nick: nick,
@ -186,49 +186,7 @@ Client.prototype.connect = function(args) {
auto_reconnect: false, // TODO: Enable auto reconnection auto_reconnect: false, // TODO: Enable auto reconnection
}); });
network.irc = irc; network.irc.on("registered", function() {
events.forEach(function(plugin) {
var path = "./plugins/irc-events/" + plugin;
require(path).apply(client, [
irc,
network
]);
});
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 delay = 1000;
var commands = args.commands; var commands = args.commands;
if (Array.isArray(commands)) { if (Array.isArray(commands)) {
@ -247,10 +205,18 @@ Client.prototype.connect = function(args) {
if (join) { if (join) {
setTimeout(function() { setTimeout(function() {
join = join.replace(/\,/g, " ").split(/\s+/g); join = join.replace(/\,/g, " ").split(/\s+/g);
irc.join(join); network.irc.join(join);
}, delay); }, delay);
} }
}); });
events.forEach(function(plugin) {
var path = "./plugins/irc-events/" + plugin;
require(path).apply(client, [
network.irc,
network
]);
});
}; };
Client.prototype.setPassword = function(hash) { Client.prototype.setPassword = function(hash) {
@ -373,10 +339,8 @@ Client.prototype.quit = function() {
} }
this.networks.forEach(function(network) { this.networks.forEach(function(network) {
var irc = network.irc; var irc = network.irc;
if (network.connected) { if (irc.connection) {
irc.quit(); irc.connection.end();
} else {
irc.stream.end();
} }
}); });
}; };

View file

@ -16,9 +16,11 @@ function Network(attr) {
username: "", username: "",
realname: "", realname: "",
channels: [], channels: [],
connected: false,
id: id++, id: id++,
irc: null, irc: null,
serverOptions: {
PREFIX: [],
},
}, attr)); }, attr));
this.name = attr.name || prettify(attr.host); this.name = attr.name || prettify(attr.host);
this.channels.unshift( this.channels.unshift(

View file

@ -0,0 +1,68 @@
var identd = require("../../identd");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
text: "Network created, connecting to " + network.host + ":" + network.port + "..."
})
});
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({
text: "Disconnected from the network."
})
});
});
irc.on("socket error", function(err) {
console.log(err);
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
type: Msg.Type.ERROR,
text: "Socket error: " + err
})
});
});
irc.on("reconnecting", function() {
client.emit("msg", {
chan: network.channels[0].id,
msg: new Msg({
text: "Reconnecting..."
})
});
});
irc.on("server options", function(data) {
if (network.serverOptions.PREFIX === data.options.PREFIX) {
return;
}
network.serverOptions.PREFIX = data.options.PREFIX;
client.emit("network_changed", {
network: network.id,
serverOptions: network.serverOptions
});
});
};

View file

@ -42,6 +42,11 @@ module.exports = function(irc, network) {
} }
} }
// Server messages go to server window
if (data.from_server) {
chan = network.channels[0];
}
var self = data.nick === irc.user.nick; var self = data.nick === irc.user.nick;
// Self messages are never highlighted // Self messages are never highlighted

View file

@ -3,7 +3,6 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("registered", function(data) { irc.on("registered", function(data) {
network.connected = true;
network.nick = data.nick; network.nick = data.nick;
var lobby = network.channels[0]; var lobby = network.channels[0];
var msg = new Msg({ var msg = new Msg({