2022-06-19 00:25:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/restrict-plus-operands */
|
|
|
|
import _ from "lodash";
|
|
|
|
import {IrcEventHandler} from "../../client";
|
2016-10-09 19:14:02 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import log from "../../log";
|
|
|
|
import Msg, {MessageType} from "../../models/msg";
|
|
|
|
import Helper from "../../helper";
|
|
|
|
import Config from "../../config";
|
|
|
|
import {ChanType, ChanState} from "../../models/chan";
|
2016-03-08 18:50:48 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default <IrcEventHandler>function (irc, network) {
|
2017-11-10 20:44:14 +00:00
|
|
|
const client = this;
|
2016-03-08 18:50:48 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
|
|
|
text: "Network created, connecting to " + network.host + ":" + network.port + "...",
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2016-03-08 18:50:48 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("registered", function () {
|
2016-06-17 10:46:15 +00:00
|
|
|
if (network.irc.network.cap.enabled.length > 0) {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
|
|
|
text: "Enabled capabilities: " + network.irc.network.cap.enabled.join(", "),
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2016-06-17 10:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 09:24:50 +00:00
|
|
|
// Always restore away message for this network
|
|
|
|
if (network.awayMessage) {
|
|
|
|
irc.raw("AWAY", network.awayMessage);
|
2019-07-17 09:33:59 +00:00
|
|
|
// Only set generic away message if there are no clients attached
|
2016-12-18 09:24:50 +00:00
|
|
|
} else if (client.awayMessage && _.size(client.attachedClients) === 0) {
|
|
|
|
irc.raw("AWAY", client.awayMessage);
|
|
|
|
}
|
|
|
|
|
2017-11-10 20:44:14 +00:00
|
|
|
let delay = 1000;
|
|
|
|
|
|
|
|
if (Array.isArray(network.commands)) {
|
|
|
|
network.commands.forEach((cmd) => {
|
2020-03-21 20:55:36 +00:00
|
|
|
setTimeout(function () {
|
2016-06-17 10:46:15 +00:00
|
|
|
client.input({
|
|
|
|
target: network.channels[0].id,
|
2017-11-15 06:35:15 +00:00
|
|
|
text: cmd,
|
2016-06-17 10:46:15 +00:00
|
|
|
});
|
|
|
|
}, delay);
|
|
|
|
delay += 1000;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
network.channels.forEach((chan) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
if (chan.type !== ChanType.CHANNEL) {
|
2016-07-03 08:39:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
setTimeout(function () {
|
2017-04-01 08:33:17 +00:00
|
|
|
network.irc.join(chan.name, chan.key);
|
2016-06-17 10:46:15 +00:00
|
|
|
}, delay);
|
2016-06-27 22:00:51 +00:00
|
|
|
delay += 1000;
|
2016-06-17 10:46:15 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("socket connected", function () {
|
2021-07-21 07:30:07 +00:00
|
|
|
if (irc.network.options.PREFIX) {
|
|
|
|
network.serverOptions.PREFIX.update(irc.network.options.PREFIX);
|
|
|
|
}
|
2016-09-27 17:33:28 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
|
|
|
text: "Connected to the network.",
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2018-02-19 11:12:01 +00:00
|
|
|
|
|
|
|
sendStatus();
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("close", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
2022-02-09 23:27:34 +00:00
|
|
|
text: "Disconnected from the network, and will not reconnect. Use /connect to reconnect again.",
|
2019-07-17 09:33:59 +00:00
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
2016-12-17 09:51:33 +00:00
|
|
|
let identSocketId;
|
2016-11-20 13:23:35 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("raw socket connected", function (socket) {
|
2018-03-15 13:30:37 +00:00
|
|
|
let ident = client.name || network.username;
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.useHexIp) {
|
2022-06-19 00:25:21 +00:00
|
|
|
ident = Helper.ip2hex(client.config.browser!.ip!);
|
2018-03-15 13:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
identSocketId = client.manager.identHandler.addSocket(socket, ident);
|
2016-12-17 09:51:33 +00:00
|
|
|
});
|
2016-04-26 20:41:08 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("socket close", function (error) {
|
2016-12-17 09:51:33 +00:00
|
|
|
if (identSocketId > 0) {
|
2017-03-17 20:19:08 +00:00
|
|
|
client.manager.identHandler.removeSocket(identSocketId);
|
2016-12-17 09:51:33 +00:00
|
|
|
identSocketId = 0;
|
|
|
|
}
|
2018-02-13 10:30:26 +00:00
|
|
|
|
|
|
|
network.channels.forEach((chan) => {
|
2018-08-24 08:29:03 +00:00
|
|
|
chan.users = new Map();
|
2022-06-19 00:25:21 +00:00
|
|
|
chan.state = ChanState.PARTED;
|
2018-02-13 10:30:26 +00:00
|
|
|
});
|
2018-02-19 11:12:01 +00:00
|
|
|
|
2018-02-23 18:05:19 +00:00
|
|
|
if (error) {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
2022-06-19 00:25:21 +00:00
|
|
|
type: MessageType.ERROR,
|
|
|
|
text: `Connection closed unexpectedly: ${String(error)}`,
|
2019-07-17 09:33:59 +00:00
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2018-02-23 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 19:35:18 +00:00
|
|
|
if (network.keepNick) {
|
|
|
|
// We disconnected without getting our original nick back yet, just set it back locally
|
|
|
|
irc.options.nick = irc.user.nick = network.keepNick;
|
|
|
|
|
|
|
|
network.setNick(network.keepNick);
|
|
|
|
network.keepNick = null;
|
|
|
|
|
2019-12-16 08:56:25 +00:00
|
|
|
client.emit("nick", {
|
2019-09-15 19:35:18 +00:00
|
|
|
network: network.uuid,
|
|
|
|
nick: network.nick,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-19 11:12:01 +00:00
|
|
|
sendStatus();
|
2016-12-17 09:51:33 +00:00
|
|
|
});
|
2016-04-26 20:41:08 +00:00
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.debug.ircFramework) {
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("debug", function (message) {
|
2019-07-17 09:33:59 +00:00
|
|
|
log.debug(
|
|
|
|
`[${client.name} (${client.id}) on ${network.name} (${network.uuid}]`,
|
|
|
|
message
|
|
|
|
);
|
2016-12-10 08:53:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-01 19:12:39 +00:00
|
|
|
if (Config.values.debug.raw) {
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("raw", function (message) {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
|
|
|
self: !message.from_server,
|
2022-06-19 00:25:21 +00:00
|
|
|
type: MessageType.RAW,
|
2019-07-17 09:33:59 +00:00
|
|
|
text: message.line,
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2016-08-06 18:39:39 +00:00
|
|
|
});
|
|
|
|
}
|
2016-07-02 18:45:41 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("socket error", function (err) {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
2022-06-19 00:25:21 +00:00
|
|
|
type: MessageType.ERROR,
|
2019-07-17 09:33:59 +00:00
|
|
|
text: "Socket error: " + err,
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("reconnecting", function (data) {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
2020-05-11 19:39:17 +00:00
|
|
|
text: `Disconnected from the network. Reconnecting in ${Math.round(
|
|
|
|
data.wait / 1000
|
2022-06-19 00:25:21 +00:00
|
|
|
)} seconds… (Attempt ${Number(data.attempt)})`,
|
2019-07-17 09:33:59 +00:00
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2016-04-19 10:20:18 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("ping timeout", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
network.channels[0].pushMessage(
|
|
|
|
client,
|
|
|
|
new Msg({
|
|
|
|
text: "Ping timeout, disconnecting…",
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
irc.on("server options", function (data) {
|
2021-07-21 07:30:07 +00:00
|
|
|
network.serverOptions.PREFIX.update(data.options.PREFIX);
|
2016-03-11 16:34:50 +00:00
|
|
|
|
2018-10-13 10:54:32 +00:00
|
|
|
if (data.options.CHANTYPES) {
|
|
|
|
network.serverOptions.CHANTYPES = data.options.CHANTYPES;
|
|
|
|
}
|
|
|
|
|
2017-04-16 09:31:32 +00:00
|
|
|
network.serverOptions.NETWORK = data.options.NETWORK;
|
2016-03-08 18:50:48 +00:00
|
|
|
|
2018-07-19 17:44:24 +00:00
|
|
|
client.emit("network:options", {
|
2018-04-26 09:06:01 +00:00
|
|
|
network: network.uuid,
|
2017-11-15 06:35:15 +00:00
|
|
|
serverOptions: network.serverOptions,
|
2016-03-08 18:50:48 +00:00
|
|
|
});
|
|
|
|
});
|
2018-02-19 11:12:01 +00:00
|
|
|
|
|
|
|
function sendStatus() {
|
|
|
|
const status = network.getNetworkStatus();
|
2022-06-19 00:25:21 +00:00
|
|
|
const toSend = {
|
|
|
|
...status,
|
|
|
|
network: network.uuid,
|
|
|
|
};
|
2018-02-19 11:12:01 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
client.emit("network:status", toSend);
|
2018-02-19 11:12:01 +00:00
|
|
|
}
|
2016-03-08 18:50:48 +00:00
|
|
|
};
|