hardlounge/server/plugins/irc-events/away.ts

73 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import {IrcEventHandler} from "../../client";
import {ChanType} from "../../models/chan";
import Msg, {MessageType} from "../../models/msg";
export default <IrcEventHandler>function (irc, network) {
const client = this;
irc.on("away", (data) => handleAway(MessageType.AWAY, data));
irc.on("back", (data) => handleAway(MessageType.BACK, data));
function handleAway(type: MessageType, data) {
const away = data.message;
if (data.self) {
const msg = new Msg({
self: true,
type: type,
text: away,
time: data.time,
});
network.getLobby().pushMessage(client, msg, true);
return;
}
network.channels.forEach((chan) => {
let user;
switch (chan.type) {
case ChanType.QUERY: {
2019-07-17 09:33:59 +00:00
if (data.nick.toLowerCase() !== chan.name.toLowerCase()) {
return;
}
2019-07-17 09:33:59 +00:00
if (chan.userAway === away) {
return;
}
2019-07-17 09:33:59 +00:00
// Store current away message on channel model,
// because query windows have no users
chan.userAway = away;
2019-07-17 09:33:59 +00:00
user = chan.getUser(data.nick);
const msg = new Msg({
type: type,
text: away || "",
time: data.time,
from: user,
});
chan.pushMessage(client, msg);
2019-07-17 09:33:59 +00:00
break;
}
case ChanType.CHANNEL: {
2019-07-17 09:33:59 +00:00
user = chan.findUser(data.nick);
2019-07-17 09:33:59 +00:00
if (!user || user.away === away) {
return;
}
2019-07-17 09:33:59 +00:00
user.away = away;
2019-07-17 09:33:59 +00:00
break;
}
}
});
}
};