<commit_before>'kiwi public';

import _ from 'lodash';
import Logger from '@/libs/Logger';
import bouncerMiddleware from '@/libs/BouncerMiddleware';

let log = Logger.namespace('BouncerProvider.js');

export default class BouncerProvider {
    constructor(state) {
        this.state = state;

        // This is the network that will be used to control the bouncer
        this.controllerNetwork = null;

        // The detected credentials for the BNC
        this.bnc = {
            enabled: false,
            username: '',
            password: '',
            server: '',
            port: 6667,
            tls: false,
            direct: false,
            path: '',
            registered: false,
        };

        // A snapshot of the current networks. Compared against to detect changed networks
        this.networksSnapshot = Object.create(null);

        state.$on('irc.server options', this.onNetworkOptions.bind(this));
    }

    enable(server, port, tls, direct, path) {
        this.bnc.server = server;
        this.bnc.port = port || 6667;
        this.bnc.tls = !!tls;
        this.bnc.direct = !!direct;
        this.bnc.path = path || '';
        this.bnc.enabled = true;

        // this.monitorNetworkChanges();
        this.listenToState();
    }

    // Try to get connected network that can be used to control the bouncer
    getController() {
        if (this.controllerNetwork && this.controllerNetwork.state === 'connected') {
            return this.controllerNetwork;
        }

        this.controllerNetwork = null;
        for (let i = 0; i < this.state.networks.length; i++) {
            let network = this.state.networks[i];
            let client = network.ircClient;
            if (network.state === 'connected' && client.network.cap.isEnabled('bouncer')) {
                this.controllerNetwork = network;
                break;
            }
        }

        return this.controllerNetwork;
    }

    async onNetworkOptions(event, network) {
        let client = network.ircClient;

        if (!this.bnc.enabled) {
            return;
        }

        if (!client.network.cap.isEnabled('bouncer')) {
            return;
        }

        // First-run setup. Only run on the first connection of the first network connected
        if (this.getController() === network && !this.bnc.registered) {
            await this.initAndAddNetworks(network);
        }

        // Set the bncname if the network upstream exists and we havn't already set it
        if (network.ircClient.bnc.hasNetwork() && !network.connection.bncname) {
            network.connection.bncname = client.bnc.tags().network;
        }
    }

    async initAndAddNetworks(network) {
        let client = network.ircClient;

        this.bnc.registered = true;

        // hide the empty controller network
        network.hidden = true;

        // populate network list from the controller connection
        let bncNetworks = await client.bnc.getNetworks();
        let bncBuffers = [];

        try {<commit_msg>Connecting directly to a bouncer network or local account<commit_after>'kiwi public';

import _ from 'lodash';
import Logger from '@/libs/Logger';
import bouncerMiddleware from '@/libs/BouncerMiddleware';

let log = Logger.namespace('BouncerProvider.js');

export default class BouncerProvider {
    constructor(state) {
        this.state = state;

        // This is the network that will be used to control the bouncer
        this.controllerNetwork = null;

        // The detected credentials for the BNC
        this.bnc = {
            enabled: false,
            username: '',
            password: '',
            server: '',
            port: 6667,
            tls: false,
            direct: false,
            path: '',
            registered: false,
        };

        // A snapshot of the current networks. Compared against to detect changed networks
        this.networksSnapshot = Object.create(null);

        state.$on('irc.motd', this.onNetworkMotd.bind(this));
    }

    enable(server, port, tls, direct, path) {
        this.bnc.server = server;
        this.bnc.port = port || 6667;
        this.bnc.tls = !!tls;
        this.bnc.direct = !!direct;
        this.bnc.path = path || '';
        this.bnc.enabled = true;

        // this.monitorNetworkChanges();
        this.listenToState();
    }

    // Try to get connected network that can be used to control the bouncer
    getController() {
        if (this.controllerNetwork && this.controllerNetwork.state === 'connected') {
            return this.controllerNetwork;
        }

        this.controllerNetwork = null;
        for (let i = 0; i < this.state.networks.length; i++) {
            let network = this.state.networks[i];
            let client = network.ircClient;
            if (network.state === 'connected' && client.network.cap.isEnabled('bouncer')) {
                this.controllerNetwork = network;
                break;
            }
        }

        return this.controllerNetwork;
    }

    async onNetworkMotd(event, network) {
        let client = network.ircClient;

        if (!this.bnc.enabled) {
            return;
        }

        if (!client.network.cap.isEnabled('bouncer')) {
            return;
        }

        // Set the bncname if the network upstream exists and we havn't already set it
        if (client.bnc.hasNetwork() && !network.connection.bncname) {
            network.connection.bncname = client.bnc.tags().network;
        }

        // First-run setup. Only run on the first connection of the first network connected
        if (this.getController() === network && !this.bnc.registered) {
            await this.initAndAddNetworks(network);
        }
    }

    async initAndAddNetworks(network) {
        let client = network.ircClient;

        this.bnc.registered = true;

        // hide the empty (non-network) controller network
        if (!network.ircClient.bnc.hasNetwork()) {
            network.hidden = true;
        } else {
            network.hidden = false;
        }

        // populate network list from the controller connection
        let bncNetworks = await client.bnc.getNetworks();
        let bncBuffers = [];

        try {
{'fraction_non_alphanumeric': 0.10782580748834968, 'fraction_numerical': 0.0028924955809095294, 'mean_word_length': 1.9289411764705882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4304022', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1651, 'n_words': 447}