2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-03-11 16:34:50 +00:00
|
|
|
var _ = require("lodash");
|
2016-03-08 18:50:48 +00:00
|
|
|
var identd = require("../../identd");
|
|
|
|
var Msg = require("../../models/msg");
|
2016-07-03 08:39:29 +00:00
|
|
|
var Chan = require("../../models/chan");
|
2016-08-06 18:39:39 +00:00
|
|
|
var Helper = require("../../helper");
|
2016-03-08 18:50:48 +00:00
|
|
|
|
|
|
|
module.exports = function(irc, network) {
|
|
|
|
var client = this;
|
2016-04-26 20:41:08 +00:00
|
|
|
var identHandler = this.manager.identHandler;
|
2016-03-08 18:50:48 +00:00
|
|
|
|
2016-04-19 10:20:18 +00:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
text: "Network created, connecting to " + network.host + ":" + network.port + "..."
|
|
|
|
}));
|
2016-03-08 18:50:48 +00:00
|
|
|
|
2016-06-17 10:46:15 +00:00
|
|
|
irc.on("registered", function() {
|
|
|
|
if (network.irc.network.cap.enabled.length > 0) {
|
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
text: "Enabled capabilities: " + network.irc.network.cap.enabled.join(", ")
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
var delay = 1000;
|
|
|
|
var commands = network.commands;
|
|
|
|
if (Array.isArray(commands)) {
|
|
|
|
commands.forEach(function(cmd) {
|
|
|
|
setTimeout(function() {
|
|
|
|
client.input({
|
|
|
|
target: network.channels[0].id,
|
|
|
|
text: cmd
|
|
|
|
});
|
|
|
|
}, delay);
|
|
|
|
delay += 1000;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
network.channels.forEach(function(chan) {
|
2016-07-03 08:39:29 +00:00
|
|
|
if (chan.type !== Chan.Type.CHANNEL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-17 10:46:15 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
network.irc.join(chan.name);
|
|
|
|
}, delay);
|
2016-06-27 22:00:51 +00:00
|
|
|
delay += 1000;
|
2016-06-17 10:46:15 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-08 18:50:48 +00:00
|
|
|
irc.on("socket connected", function() {
|
2016-09-27 17:33:28 +00:00
|
|
|
network.prefixLookup = {};
|
|
|
|
irc.network.options.PREFIX.forEach(function(mode) {
|
|
|
|
network.prefixLookup[mode.mode] = mode.symbol;
|
|
|
|
});
|
|
|
|
|
2016-04-19 10:20:18 +00:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
text: "Connected to the network."
|
|
|
|
}));
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
2016-04-13 07:10:44 +00:00
|
|
|
irc.on("close", function() {
|
2016-04-19 10:20:18 +00:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
2016-07-02 18:45:41 +00:00
|
|
|
text: "Disconnected from the network, and will not reconnect. Use /connect to reconnect again."
|
2016-04-19 10:20:18 +00:00
|
|
|
}));
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
2016-04-26 20:40:27 +00:00
|
|
|
if (identd.isEnabled()) {
|
|
|
|
irc.on("socket connected", function() {
|
|
|
|
identd.hook(irc.connection.socket, client.name || network.username);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-26 20:41:08 +00:00
|
|
|
if (identHandler) {
|
|
|
|
irc.on("socket connected", function() {
|
|
|
|
identHandler.addSocket(irc.connection.socket, client.name || network.username);
|
|
|
|
identHandler.refresh();
|
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("socket close", function() {
|
|
|
|
identHandler.removeSocket(irc.connection.socket);
|
|
|
|
identHandler.refresh();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-06 18:39:39 +00:00
|
|
|
if (Helper.config.debug) {
|
|
|
|
irc.on("debug", function(message) {
|
|
|
|
log.debug("[" + client.name + " (#" + client.id + ") on " + network.name + " (#" + network.id + ")]", message);
|
|
|
|
});
|
|
|
|
}
|
2016-07-02 18:45:41 +00:00
|
|
|
|
2016-03-08 18:50:48 +00:00
|
|
|
irc.on("socket error", function(err) {
|
2016-04-19 10:20:18 +00:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
type: Msg.Type.ERROR,
|
|
|
|
text: "Socket error: " + err
|
|
|
|
}));
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
2016-07-02 18:45:41 +00:00
|
|
|
irc.on("reconnecting", function(data) {
|
2016-04-19 10:20:18 +00:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
2016-07-02 18:45:41 +00:00
|
|
|
text: "Disconnected from the network. Reconnecting in " + Math.round(data.wait / 1000) + " seconds… (Attempt " + data.attempt + " of " + data.max_retries + ")"
|
2016-04-19 10:20:18 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("ping timeout", function() {
|
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
2016-07-02 18:45:41 +00:00
|
|
|
text: "Ping timeout, disconnecting…"
|
2016-04-19 10:20:18 +00:00
|
|
|
}));
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("server options", function(data) {
|
|
|
|
if (network.serverOptions.PREFIX === data.options.PREFIX) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-11 16:34:50 +00:00
|
|
|
network.prefixLookup = {};
|
|
|
|
|
|
|
|
_.each(data.options.PREFIX, function(mode) {
|
|
|
|
network.prefixLookup[mode.mode] = mode.symbol;
|
|
|
|
});
|
|
|
|
|
2016-03-08 18:50:48 +00:00
|
|
|
network.serverOptions.PREFIX = data.options.PREFIX;
|
|
|
|
|
|
|
|
client.emit("network_changed", {
|
|
|
|
network: network.id,
|
|
|
|
serverOptions: network.serverOptions
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|