mirror of
https://github.com/thelounge/thelounge.git
synced 2024-06-29 18:50:19 +02:00
It is much easier to follow the control flow if error checks are done on the indented path, immediately returning.
163 lines
3.6 KiB
TypeScript
163 lines
3.6 KiB
TypeScript
import Msg from "../../models/msg";
|
|
import Helper from "../../helper";
|
|
import {PluginInputHandler} from "./index";
|
|
import {IgnoreListItem} from "../../models/network";
|
|
import {MessageType} from "../../../shared/types/msg";
|
|
import {ChanType, SpecialChanType} from "../../../shared/types/chan";
|
|
|
|
const commands = ["ignore", "unignore", "ignorelist"];
|
|
|
|
const input: PluginInputHandler = function (network, chan, cmd, args) {
|
|
const client = this;
|
|
let target: string;
|
|
// let hostmask: cmd === "ignoreList" ? string : undefined;
|
|
let hostmask: IgnoreListItem | undefined;
|
|
|
|
if (cmd !== "ignorelist" && (args.length === 0 || args[0].trim().length === 0)) {
|
|
chan.pushMessage(
|
|
client,
|
|
new Msg({
|
|
type: MessageType.ERROR,
|
|
text: `Usage: /${cmd} <nick>[!ident][@host]`,
|
|
})
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if (cmd !== "ignorelist") {
|
|
// Trim to remove any spaces from the hostmask
|
|
target = args[0].trim();
|
|
hostmask = Helper.parseHostmask(target) as IgnoreListItem;
|
|
}
|
|
|
|
switch (cmd) {
|
|
case "ignore": {
|
|
// IRC nicks are case insensitive
|
|
if (hostmask!.nick.toLowerCase() === network.nick.toLowerCase()) {
|
|
chan.pushMessage(
|
|
client,
|
|
new Msg({
|
|
type: MessageType.ERROR,
|
|
text: "You can't ignore yourself",
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (
|
|
network.ignoreList.some(function (entry) {
|
|
return Helper.compareHostmask(entry, hostmask!);
|
|
})
|
|
) {
|
|
chan.pushMessage(
|
|
client,
|
|
new Msg({
|
|
type: MessageType.ERROR,
|
|
text: "The specified user/hostmask is already ignored",
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
hostmask!.when = Date.now();
|
|
network.ignoreList.push(hostmask!);
|
|
|
|
client.save();
|
|
chan.pushMessage(
|
|
client,
|
|
new Msg({
|
|
type: MessageType.ERROR, // TODO: Successfully added via type.Error 🤔 ?
|
|
text: `\u0002${hostmask!.nick}!${hostmask!.ident}@${
|
|
hostmask!.hostname
|
|
}\u000f added to ignorelist`,
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
case "unignore": {
|
|
const idx = network.ignoreList.findIndex(function (entry) {
|
|
return Helper.compareHostmask(entry, hostmask!);
|
|
});
|
|
|
|
if (idx === -1) {
|
|
chan.pushMessage(
|
|
client,
|
|
new Msg({
|
|
type: MessageType.ERROR,
|
|
text: "The specified user/hostmask is not ignored",
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
network.ignoreList.splice(idx, 1);
|
|
client.save();
|
|
|
|
chan.pushMessage(
|
|
client,
|
|
new Msg({
|
|
type: MessageType.ERROR, // TODO: Successfully removed via type.Error 🤔 ?
|
|
text: `Successfully removed \u0002${hostmask!.nick}!${hostmask!.ident}@${
|
|
hostmask!.hostname
|
|
}\u000f from ignorelist`,
|
|
})
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
case "ignorelist": {
|
|
if (network.ignoreList.length === 0) {
|
|
chan.pushMessage(
|
|
client,
|
|
new Msg({
|
|
type: MessageType.ERROR,
|
|
text: "Ignorelist is empty",
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
const chanName = "Ignored users";
|
|
const ignored = network.ignoreList.map((data) => ({
|
|
hostmask: `${data.nick}!${data.ident}@${data.hostname}`,
|
|
when: data.when,
|
|
}));
|
|
let newChan = network.getChannel(chanName);
|
|
|
|
if (typeof newChan === "undefined") {
|
|
newChan = client.createChannel({
|
|
type: ChanType.SPECIAL,
|
|
special: SpecialChanType.IGNORELIST,
|
|
name: chanName,
|
|
data: ignored,
|
|
});
|
|
client.emit("join", {
|
|
network: network.uuid,
|
|
chan: newChan.getFilteredClone(true),
|
|
shouldOpen: false,
|
|
index: network.addChannel(newChan),
|
|
});
|
|
return;
|
|
}
|
|
|
|
// TODO: add type for this chan/event
|
|
newChan.data = ignored;
|
|
|
|
client.emit("msg:special", {
|
|
chan: newChan.id,
|
|
data: ignored,
|
|
});
|
|
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
export default {
|
|
commands,
|
|
input,
|
|
};
|