2016-10-09 21:14:02 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-18 10:24:50 +01:00
|
|
|
var _ = require("lodash");
|
2016-03-08 19:50:48 +01:00
|
|
|
var Msg = require("../../models/msg");
|
2016-07-03 10:39:29 +02:00
|
|
|
var Chan = require("../../models/chan");
|
2016-08-06 20:39:39 +02:00
|
|
|
var Helper = require("../../helper");
|
2016-03-08 19:50:48 +01:00
|
|
|
|
|
|
|
module.exports = function(irc, network) {
|
|
|
|
var client = this;
|
|
|
|
|
2016-04-19 12:20:18 +02:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
text: "Network created, connecting to " + network.host + ":" + network.port + "..."
|
2016-09-25 08:41:10 +02:00
|
|
|
}), true);
|
2016-03-08 19:50:48 +01:00
|
|
|
|
2016-06-17 12:46:15 +02: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(", ")
|
2016-09-25 08:41:10 +02:00
|
|
|
}), true);
|
2016-06-17 12:46:15 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 10:24:50 +01:00
|
|
|
// Always restore away message for this network
|
|
|
|
if (network.awayMessage) {
|
|
|
|
irc.raw("AWAY", network.awayMessage);
|
|
|
|
// Only set generic away message if there are no clients attached
|
|
|
|
} else if (client.awayMessage && _.size(client.attachedClients) === 0) {
|
|
|
|
irc.raw("AWAY", client.awayMessage);
|
|
|
|
}
|
|
|
|
|
2016-06-17 12:46:15 +02:00
|
|
|
var delay = 1000;
|
|
|
|
var commands = network.commands;
|
|
|
|
if (Array.isArray(commands)) {
|
2016-10-12 09:55:40 +02:00
|
|
|
commands.forEach(cmd => {
|
2016-06-17 12:46:15 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
client.input({
|
|
|
|
target: network.channels[0].id,
|
|
|
|
text: cmd
|
|
|
|
});
|
|
|
|
}, delay);
|
|
|
|
delay += 1000;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-12 09:55:40 +02:00
|
|
|
network.channels.forEach(chan => {
|
2016-07-03 10:39:29 +02:00
|
|
|
if (chan.type !== Chan.Type.CHANNEL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-17 12:46:15 +02:00
|
|
|
setTimeout(function() {
|
|
|
|
network.irc.join(chan.name);
|
|
|
|
}, delay);
|
2016-06-28 00:00:51 +02:00
|
|
|
delay += 1000;
|
2016-06-17 12:46:15 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-08 19:50:48 +01:00
|
|
|
irc.on("socket connected", function() {
|
2016-09-27 19:33:28 +02:00
|
|
|
network.prefixLookup = {};
|
|
|
|
irc.network.options.PREFIX.forEach(function(mode) {
|
|
|
|
network.prefixLookup[mode.mode] = mode.symbol;
|
|
|
|
});
|
|
|
|
|
2016-04-19 12:20:18 +02:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
text: "Connected to the network."
|
2016-09-25 08:41:10 +02:00
|
|
|
}), true);
|
2016-03-08 19:50:48 +01:00
|
|
|
});
|
|
|
|
|
2016-04-13 09:10:44 +02:00
|
|
|
irc.on("close", function() {
|
2016-04-19 12:20:18 +02:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
2016-07-02 20:45:41 +02:00
|
|
|
text: "Disconnected from the network, and will not reconnect. Use /connect to reconnect again."
|
2016-09-25 08:41:10 +02:00
|
|
|
}), true);
|
2016-03-08 19:50:48 +01:00
|
|
|
});
|
|
|
|
|
2016-12-17 10:51:33 +01:00
|
|
|
let identSocketId;
|
2016-11-20 14:23:35 +01:00
|
|
|
|
2016-12-17 10:51:33 +01:00
|
|
|
irc.on("raw socket connected", function(socket) {
|
2017-03-17 21:19:08 +01:00
|
|
|
identSocketId = client.manager.identHandler.addSocket(socket, client.name || network.username);
|
2016-12-17 10:51:33 +01:00
|
|
|
});
|
2016-04-26 22:41:08 +02:00
|
|
|
|
2016-12-17 10:51:33 +01:00
|
|
|
irc.on("socket close", function() {
|
|
|
|
if (identSocketId > 0) {
|
2017-03-17 21:19:08 +01:00
|
|
|
client.manager.identHandler.removeSocket(identSocketId);
|
2016-12-17 10:51:33 +01:00
|
|
|
identSocketId = 0;
|
|
|
|
}
|
|
|
|
});
|
2016-04-26 22:41:08 +02:00
|
|
|
|
2016-12-10 09:53:06 +01:00
|
|
|
if (Helper.config.debug.ircFramework) {
|
2016-08-06 20:39:39 +02:00
|
|
|
irc.on("debug", function(message) {
|
|
|
|
log.debug("[" + client.name + " (#" + client.id + ") on " + network.name + " (#" + network.id + ")]", message);
|
2016-12-10 09:53:06 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Helper.config.debug.raw) {
|
|
|
|
irc.on("raw", function(message) {
|
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
from: message.from_server ? "«" : "»",
|
|
|
|
self: !message.from_server,
|
|
|
|
type: "raw",
|
|
|
|
text: message.line
|
|
|
|
}), true);
|
2016-08-06 20:39:39 +02:00
|
|
|
});
|
|
|
|
}
|
2016-07-02 20:45:41 +02:00
|
|
|
|
2016-03-08 19:50:48 +01:00
|
|
|
irc.on("socket error", function(err) {
|
2016-04-19 12:20:18 +02:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
|
|
|
type: Msg.Type.ERROR,
|
|
|
|
text: "Socket error: " + err
|
2016-09-25 08:41:10 +02:00
|
|
|
}), true);
|
2016-03-08 19:50:48 +01:00
|
|
|
});
|
|
|
|
|
2016-07-02 20:45:41 +02:00
|
|
|
irc.on("reconnecting", function(data) {
|
2016-04-19 12:20:18 +02:00
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
2016-07-02 20:45:41 +02:00
|
|
|
text: "Disconnected from the network. Reconnecting in " + Math.round(data.wait / 1000) + " seconds… (Attempt " + data.attempt + " of " + data.max_retries + ")"
|
2016-09-25 08:41:10 +02:00
|
|
|
}), true);
|
2016-04-19 12:20:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("ping timeout", function() {
|
|
|
|
network.channels[0].pushMessage(client, new Msg({
|
2016-07-02 20:45:41 +02:00
|
|
|
text: "Ping timeout, disconnecting…"
|
2016-09-25 08:41:10 +02:00
|
|
|
}), true);
|
2016-03-08 19:50:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("server options", function(data) {
|
|
|
|
if (network.serverOptions.PREFIX === data.options.PREFIX) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-11 17:34:50 +01:00
|
|
|
network.prefixLookup = {};
|
|
|
|
|
2016-10-12 09:55:40 +02:00
|
|
|
data.options.PREFIX.forEach(mode => {
|
2016-03-11 17:34:50 +01:00
|
|
|
network.prefixLookup[mode.mode] = mode.symbol;
|
|
|
|
});
|
|
|
|
|
2016-03-08 19:50:48 +01:00
|
|
|
network.serverOptions.PREFIX = data.options.PREFIX;
|
|
|
|
|
|
|
|
client.emit("network_changed", {
|
|
|
|
network: network.id,
|
|
|
|
serverOptions: network.serverOptions
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|