thelounge/src/plugins/irc-events/error.js

93 lines
2.4 KiB
JavaScript
Raw Normal View History

"use strict";
const Msg = require("../../models/msg");
const Config = require("../../config");
2014-09-13 23:29:45 +02:00
module.exports = function (irc, network) {
const client = this;
irc.on("irc error", function (data) {
const msg = new Msg({
2014-09-13 23:29:45 +02:00
type: Msg.Type.ERROR,
error: data.error,
2017-07-28 10:53:36 +02:00
showInActive: true,
nick: data.nick,
channel: data.channel,
reason: data.reason,
command: data.command,
2014-09-13 23:29:45 +02:00
});
let target = network.channels[0];
// If this error is channel specific and a channel
// with this name exists, put this error in that channel
if (data.channel) {
const channel = network.getChannel(data.channel);
if (typeof channel !== "undefined") {
target = channel;
msg.showInActive = false;
}
}
target.pushMessage(client, msg, true);
2016-03-08 10:54:17 +01:00
});
irc.on("nick in use", function (data) {
2019-09-15 21:35:18 +02:00
let message = data.nick + ": " + (data.reason || "Nickname is already in use.");
if (irc.connection.registered === false && !Config.values.public) {
2019-09-15 21:35:18 +02:00
message += " An attempt to use it will be made when this nick quits.";
// Clients usually get nick in use on connect when reconnecting to a network
// after a network failure (like ping timeout), and as a result of that,
// TL will append a random number to the nick.
// keepNick will try to set the original nick name back if it sees a QUIT for that nick.
network.keepNick = irc.user.nick;
}
const lobby = network.channels[0];
const msg = new Msg({
2016-03-08 10:54:17 +01:00
type: Msg.Type.ERROR,
2019-09-15 21:35:18 +02:00
text: message,
2017-07-28 10:53:36 +02:00
showInActive: true,
2016-03-08 10:54:17 +01:00
});
lobby.pushMessage(client, msg, true);
2016-03-08 10:54:17 +01:00
if (irc.connection.registered === false) {
const nickLen = parseInt(network.irc.network.options.NICKLEN, 10) || 16;
const random = (data.nick || irc.user.nick) + Math.floor(Math.random() * 10);
// Safeguard nick changes up to allowed length
// Some servers may send "nick in use" error even for randomly generated nicks
if (random.length <= nickLen) {
irc.changeNick(random);
}
}
client.emit("nick", {
network: network.uuid,
nick: irc.user.nick,
});
2014-09-13 23:29:45 +02:00
});
irc.on("nick invalid", function (data) {
const lobby = network.channels[0];
const msg = new Msg({
type: Msg.Type.ERROR,
2016-04-04 20:32:21 +02:00
text: data.nick + ": " + (data.reason || "Nickname is invalid."),
2017-07-28 10:53:36 +02:00
showInActive: true,
});
lobby.pushMessage(client, msg, true);
if (irc.connection.registered === false) {
irc.changeNick(Config.getDefaultNick());
}
client.emit("nick", {
network: network.uuid,
nick: irc.user.nick,
});
});
2014-09-13 23:29:45 +02:00
};