thelounge/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 09:07:45 +02:00
function getTarget(cmd: string, args: string[], chan: Chan) {
2019-05-13 09:07:45 +02:00
switch (cmd) {
2019-07-17 11:33:59 +02:00
case "msg":
case "query":
return args.shift();
default:
return chan.name;
2019-05-13 09:07:45 +02:00
}
}
const input: PluginInputHandler = function (network, chan, cmd, args) {
let targetName = getTarget(cmd, args, chan);
2019-05-13 09:07:45 +02:00
if (cmd === "query") {
if (!targetName) {
2019-07-17 11:33:59 +02:00
chan.pushMessage(
this,
new Msg({
type: MessageType.ERROR,
2019-07-17 11:33:59 +02:00
text: "You cannot open a query window without an argument.",
})
);
2019-05-13 09:07:45 +02:00
return;
}
const target = network.getChannel(targetName);
if (typeof target === "undefined") {
const char = targetName[0];
2019-07-17 11:33:59 +02: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 11:33:59 +02:00
text: "You can not open query windows for channels, use /join instead.",
})
);
2019-05-13 09:07:45 +02: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 11:33:59 +02:00
chan.pushMessage(
this,
new Msg({
type: MessageType.ERROR,
2022-02-10 00:27:34 +01:00
text: "You can not open query windows for names starting with a user prefix.",
2019-07-17 11:33:59 +02:00
})
);
2019-05-13 09:07:45 +02:00
return;
}
}
const newChan = this.createChannel({
type: ChanType.QUERY,
2019-05-13 09:07:45 +02: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 23:29:45 +02:00
}
2016-03-08 14:36:25 +01:00
2018-01-11 12:33:36 +01:00
const msg = args.join(" ");
2016-05-25 09:23:03 +02:00
if (msg.length === 0) {
return true;
}
2019-05-13 09:07:45 +02:00
network.irc.say(targetName, msg);
2016-03-08 14:36:25 +01:00
// If the IRCd does not support echo-message, simulate the message
// being sent back to us.
2016-04-22 18:38:13 +02: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 09:07:45 +02:00
const channel = network.getChannel(targetName);
2016-04-22 18:38:13 +02:00
if (typeof channel !== "undefined") {
2018-01-11 12:33:36 +01: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 18:38:13 +02:00
});
}
2014-09-13 23:29:45 +02:00
}
2016-03-06 10:24:56 +01:00
return true;
2014-09-13 23:29:45 +02:00
};
export default {
commands,
input,
};