hardlounge/server/plugins/inputs/msg.ts

128 lines
2.7 KiB
TypeScript
Raw Normal View History

import {PluginInputHandler} from "./index";
import Msg, {MessageType} from "../../models/msg";
import Chan, {ChanType} from "../../models/chan";
const commands = ["query", "msg", "say"];
2019-05-13 07:07:45 +00:00
function getTarget(cmd: string, args: string[], chan: Chan) {
2019-05-13 07:07:45 +00:00
switch (cmd) {
2019-07-17 09:33:59 +00:00
case "msg":
case "query":
return args.shift();
default:
return chan.name;
2019-05-13 07:07:45 +00:00
}
}
const input: PluginInputHandler = function (network, chan, cmd, args) {
let targetName = getTarget(cmd, args, chan);
2019-05-13 07:07:45 +00:00
if (cmd === "query") {
if (!targetName) {
2019-07-17 09:33:59 +00:00
chan.pushMessage(
this,
new Msg({
type: MessageType.ERROR,
2019-07-17 09:33:59 +00:00
text: "You cannot open a query window without an argument.",
})
);
2019-05-13 07:07:45 +00:00
return;
}
const target = network.getChannel(targetName);
if (typeof target === "undefined") {
const char = targetName[0];
2019-07-17 09:33:59 +00:00
if (
network.irc.network.options.CHANTYPES &&
network.irc.network.options.CHANTYPES.includes(char)
) {
chan.pushMessage(
this,
new Msg({
type: MessageType.ERROR,
2019-07-17 09:33:59 +00:00
text: "You can not open query windows for channels, use /join instead.",
})
);
2019-05-13 07:07:45 +00:00
return;
}
for (let i = 0; i < network.irc.network.options.PREFIX.length; i++) {
if (network.irc.network.options.PREFIX[i].symbol === char) {
2019-07-17 09:33:59 +00:00
chan.pushMessage(
this,
new Msg({
type: MessageType.ERROR,
2022-02-09 23:27:34 +00:00
text: "You can not open query windows for names starting with a user prefix.",
2019-07-17 09:33:59 +00:00
})
);
2019-05-13 07:07:45 +00:00
return;
}
}
const newChan = this.createChannel({
type: ChanType.QUERY,
2019-05-13 07:07:45 +00:00
name: targetName,
});
this.emit("join", {
network: network.uuid,
chan: newChan.getFilteredClone(true),
shouldOpen: true,
index: network.addChannel(newChan),
});
this.save();
newChan.loadMessages(this, network);
}
}
if (args.length === 0) {
return true;
}
if (!targetName) {
return true;
2014-09-13 21:29:45 +00:00
}
2016-03-08 13:36:25 +00:00
2018-01-11 11:33:36 +00:00
const msg = args.join(" ");
2016-05-25 07:23:03 +00:00
if (msg.length === 0) {
return true;
}
2019-05-13 07:07:45 +00:00
network.irc.say(targetName, msg);
2016-03-08 13:36:25 +00:00
// If the IRCd does not support echo-message, simulate the message
// being sent back to us.
2016-04-22 16:38:13 +00:00
if (!network.irc.network.cap.isEnabled("echo-message")) {
const parsedTarget = network.irc.network.extractTargetGroup(targetName);
let targetGroup;
if (parsedTarget) {
targetName = parsedTarget.target as string;
targetGroup = parsedTarget.target_group;
}
2019-05-13 07:07:45 +00:00
const channel = network.getChannel(targetName);
2016-04-22 16:38:13 +00:00
if (typeof channel !== "undefined") {
2018-01-11 11:33:36 +00:00
network.irc.emit("privmsg", {
nick: network.irc.user.nick,
ident: network.irc.user.username,
hostname: network.irc.user.host,
target: targetName,
group: targetGroup,
message: msg,
2016-04-22 16:38:13 +00:00
});
}
2014-09-13 21:29:45 +00:00
}
2016-03-06 09:24:56 +00:00
return true;
2014-09-13 21:29:45 +00:00
};
export default {
commands,
input,
};