"use strict";

const Chan = require("../../models/chan");
const Msg = require("../../models/msg");

module.exports = function(irc, network) {
	const client = this;

	irc.on("banlist", function(banlist) {
		const channel = banlist.channel;
		const bans = banlist.bans;

		if (!bans || bans.length === 0) {
			const msg = new Msg({
				time: Date.now(),
				type: Msg.Type.ERROR,
				text: "Banlist empty",
			});
			network.getChannel(channel).pushMessage(client, msg, true);
			return;
		}

		const chanName = `Banlist for ${channel}`;
		let chan = network.getChannel(chanName);

		if (typeof chan === "undefined") {
			chan = new Chan({
				type: Chan.Type.SPECIAL,
				name: chanName,
			});
			network.channels.push(chan);
			client.emit("join", {
				network: network.id,
				chan: chan.getFilteredClone(true),
			});
		}

		chan.pushMessage(client,
			new Msg({
				type: Msg.Type.BANLIST,
				bans: bans.map((data) => ({
					hostmask: data.banned,
					banned_by: data.banned_by,
					banned_at: data.banned_at * 1000,
				})),
			}),
			true
		);
	});
};

@@ -400,32 +400,43 @@
 pty%22,%0A%09%09%09%7D);%0A%09%09%09
+let chan = 
 network.getChann
@@ -446,16 +446,206 @@
 channel)
+;%0A%0A%09%09%09// Send error to lobby if we receive banlist for a channel we're not in%0A%09%09%09if (typeof chan === %22undefined%22) %7B%0A%09%09%09%09msg.showInActive = true;%0A%09%09%09%09chan = network.channels%5B0%5D;%0A%09%09%09%7D%0A%0A%09%09%09chan
 .pushMes
@@ -669,16 +669,17 @@
  true);%0A
+%0A
 %09%09%09retur

Send empty banlist error to lobby for channels user is not in