<commit_before>'kiwi public';

import _ from 'lodash';
import strftime from 'strftime';
import Irc from 'irc-framework';
import * as TextFormatting from '@/helpers/TextFormatting';
import * as IrcdDiffs from '@/helpers/IrcdDiffs';
import typingMiddleware from './TypingMiddleware';
import chathistoryMiddleware from './ChathistoryMiddleware';
import * as ServerConnection from './ServerConnection';

export function create(state, network) {
    let networkid = network.id;

    let clientOpts = {
        host: network.connection.server,
        port: network.connection.port,
        tls: network.connection.tls,
        path: network.connection.path,
        password: <PASSWORD>,
        nick: network.nick,
        username: network.username || network.nick,
        gecos: network.gecos || 'https://kiwiirc.com/',
        version: null,
        auto_reconnect: false,
        encoding: network.connection.encoding,
    };

    let ircClient = new Irc.Client(clientOpts);
    ircClient.requestCap('znc.in/self-message');
    // Current version of irc-framework only support draft/message-tags-0.2
    // TODO: Removee this once irc-framework has been updated
    ircClient.requestCap('message-tags');
    ircClient.use(chathistoryMiddleware());
    ircClient.use(clientMiddleware(state, network));
    ircClient.use(typingMiddleware());

    // Overload the connect() function to make sure we are connecting with the
    // most recent connection details from the state
    let originalIrcClientConnect = ircClient.connect;
    ircClient.connect = function connect(...args) {
        ircClient.options.host = network.connection.server;
        ircClient.options.port = network.connection.port;
        ircClient.options.tls = network.connection.tls;
        ircClient.options.path = network.connection.path;
        ircClient.options.password = <PASSWORD>;
        ircClient.options.nick = network.connection.nick;
        ircClient.options.username = network.username || network.connection.nick;
        ircClient.options.gecos = network.gecos || 'https://kiwiirc.com/';
        ircClient.options.encoding = network.connection.encoding;

        state.$emit('network.connecting', { network });

        // A direct connection uses a websocket to connect (note: some browsers limit
        // the number of connections to the same host!).
        // A non-direct connection will connect via the configured kiwi server using
        // with our own irc-framework compatible transport.
        if (!network.connection.direct) {
            ircClient.options.transport = ServerConnection.createChannelConstructor(
                state.settings.kiwiServer,
                (window.location.hash || '').substr(1),
                networkid
            );
        } else {
            ircClient.options.transport = undefined;
        }

        originalIrcClientConnect.apply(ircClient, args);
    };

    ircClient.on('raw', (event) => {
        if (!network.setting('show_raw') && !state.setting('showRaw')) {
            return;
        }

        let buffer = state.getOrAddBufferByName(networkid, '*raw');
        state.addMessage(buffer, {
            time: Date.now(),
            nick: '',<commit_msg>Support setting of custom irc-framework transports<commit_after>'kiwi public';

import _ from 'lodash';
import strftime from 'strftime';
import Irc from 'irc-framework';
import * as TextFormatting from '@/helpers/TextFormatting';
import * as IrcdDiffs from '@/helpers/IrcdDiffs';
import typingMiddleware from './TypingMiddleware';
import chathistoryMiddleware from './ChathistoryMiddleware';
import * as ServerConnection from './ServerConnection';

export function create(state, network) {
    let networkid = network.id;

    let clientOpts = {
        host: network.connection.server,
        port: network.connection.port,
        tls: network.connection.tls,
        path: network.connection.path,
        password: <PASSWORD>,
        nick: network.nick,
        username: network.username || network.nick,
        gecos: network.gecos || 'https://kiwiirc.com/',
        version: null,
        auto_reconnect: false,
        encoding: network.connection.encoding,
    };

    let ircClient = new Irc.Client(clientOpts);
    ircClient.requestCap('znc.in/self-message');
    // Current version of irc-framework only support draft/message-tags-0.2
    // TODO: Removee this once irc-framework has been updated
    ircClient.requestCap('message-tags');
    ircClient.use(chathistoryMiddleware());
    ircClient.use(clientMiddleware(state, network));
    ircClient.use(typingMiddleware());

    // Overload the connect() function to make sure we are connecting with the
    // most recent connection details from the state
    let originalIrcClientConnect = ircClient.connect;
    ircClient.connect = function connect(...args) {
        ircClient.options.host = network.connection.server;
        ircClient.options.port = network.connection.port;
        ircClient.options.tls = network.connection.tls;
        ircClient.options.path = network.connection.path;
        ircClient.options.password = <PASSWORD>;
        ircClient.options.nick = network.connection.nick;
        ircClient.options.username = network.username || network.connection.nick;
        ircClient.options.gecos = network.gecos || 'https://kiwiirc.com/';
        ircClient.options.encoding = network.connection.encoding;

        let eventObj = { network, transport: null };
        state.$emit('network.connecting', eventObj);

        if (eventObj.transport) {
            // A plugin might use its own transport of some kind
            ircClient.options.transport = eventObj.transport;
        } else if (!network.connection.direct) {
            // A direct connection uses a websocket to connect (note: some browsers limit
            // the number of connections to the same host!).
            // A non-direct connection will connect via the configured kiwi server using
            // with our own irc-framework compatible transport.
            ircClient.options.transport = ServerConnection.createChannelConstructor(
                state.settings.kiwiServer,
                (window.location.hash || '').substr(1),
                networkid
            );
        } else {
            // Use the irc-framework default transport
            ircClient.options.transport = undefined;
        }

        originalIrcClientConnect.apply(ircClient, args);
    };

    ircClient.on('raw', (event) => {
        if (!network.setting('show_raw') && !state.setting('showRaw')) {
            return;
        }

        let buffer = state.getOrAddBufferByName(networkid, '*raw');
        state.addMessage(buffer, {
            time: Date.now(),
            nick: '',
{'fraction_non_alphanumeric': 0.10946306733906852, 'fraction_numerical': 0.0008899436369029962, 'mean_word_length': 2.719249862107005, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 4, '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': '3704044', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1754, 'n_words': 440}