thelounge/src/plugins/irc-events/away.ts

78 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-05-05 00:41:57 +02:00
import {IrcEventHandler} from "../../client";
import {ChanType} from "../../models/chan";
import Msg, {MessageType} from "../../models/msg";
2022-05-03 03:32:20 +02:00
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, data) {
const away = data.message;
if (data.self) {
const msg = new Msg({
self: true,
type: type,
text: away,
time: data.time,
});
network.channels[0].pushMessage(client, msg, true);
return;
}
network.channels.forEach((chan) => {
let user;
switch (chan.type) {
2022-05-02 07:56:38 +02:00
case ChanType.QUERY: {
2019-07-17 11:33:59 +02:00
if (data.nick.toLowerCase() !== chan.name.toLowerCase()) {
return;
}
2019-07-17 11:33:59 +02:00
if (chan.userAway === away) {
return;
}
2019-07-17 11:33:59 +02:00
// Store current away message on channel model,
// because query windows have no users
chan.userAway = away;
2019-07-17 11:33:59 +02: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 11:33:59 +02:00
break;
}
2022-05-02 07:56:38 +02:00
case ChanType.CHANNEL: {
2019-07-17 11:33:59 +02:00
user = chan.findUser(data.nick);
2019-07-17 11:33:59 +02:00
if (!user || user.away === away) {
return;
}
2019-07-17 11:33:59 +02:00
user.away = away;
chan.setUser(user);
client.emit("users", {
chan: chan.id,
});
2019-07-17 11:33:59 +02:00
break;
}
}
});
}
2022-05-03 03:32:20 +02:00
};