<commit_before>_kiwi.model.Application = function () {
    // Set to a reference to this object within initialize()
    var that = null;

    // The auto connect details entered into the server select box
    var auto_connect_details = {};


    var model = function () {
        /** _kiwi.view.Application */
        this.view = null;

        /** _kiwi.view.StatusMessage */
        this.message = null;

        /* Address for the kiwi server */
        this.kiwi_server = null;

        this.initialize = function (options) {
            that = this;

            if (options[0].container) {
                this.set('container', options[0].container);
            }

            // The base url to the kiwi server
            this.set('base_path', options[0].base_path ? options[0].base_path : '/kiwi');

            // Any options sent down from the server
            this.server_settings = options[0].server_settings || {};

            // Best guess at where the kiwi server is
            this.detectKiwiServer();

            // Takes instances of model_network
            this.connections = new _kiwi.model.NetworkPanelList();
        };


        this.start = function () {
            // Only debug if set in the querystring
            if (!getQueryVariable('debug')) {
                manageDebug(false);
            } else {
                //manageDebug(true);
            }

            // Set the gateway up
            _kiwi.gateway = new _kiwi.model.Gateway();
            this.bindGatewayCommands(_kiwi.gateway);

            this.initializeClient();
            this.initializeGlobals();

            this.view.barsHide(true);

            this.showIntialConenctionDialog();
        };


        this.detectKiwiServer = function () {
            // If running from file, default to localhost:7777 by default
            if (window.location.protocol === 'file:') {
                this.kiwi_server = 'http://localhost:7778';
            } else {
                // Assume the kiwi server is on the same server
                this.kiwi_server = window.location.protocol + '//' + window.location.host;
            }
        };


        this.showIntialConenctionDialog = function() {
            var connection_dialog = new _kiwi.model.NewConnection();
            this.populateDefaultServerSettings(connection_dialog);

            connection_dialog.view.$el.addClass('initial');
            this.view.$el.find('.panel_container:first').append(connection_dialog.view.$el);

            var $info = $($('#tmpl_new_connection_info').html());

            if ($info.html()) {
                connection_dialog.view.infoBoxSet($info);
                connection_dialog.view.infoBoxShow();
            }

            // TODO: Shouldn't really be here but it's not working in the view.. :/
            // Hack for firefox browers: Focus is not given on this event loop iteration
            setTimeout(function(){
                connection_dialog.view.$el.find('.nick').select();
            }, 0);

            // Once connected, close this dialog and remove its own event
            var fn = function() {
                connection_dialog.view.$el.slideUp(function() {
                    connection_dialog.view.dispose();
                    connection_dialog = null;

                    _kiwi.gateway.off('onconnect', fn);
                });

            };
            _kiwi.gateway.on('onconnect', fn);
        };


        this.initializeClient = function () {
            this.view = new _kiwi.view.Application({model: this, el: this.get('container')});

            // Applets panel list
            this.applet_panels = new _kiwi.model.PanelList();
            this.applet_panels.view.$el.addClass('panellist applets');
            this.view.$el.find('#tabs').append(this.applet_panels.view.$el);

            /**
             * Set the UI components up
             */
            this.controlbox = new _kiwi.view.ControlBox({el: $('#controlbox')[0]});
            this.bindControllboxCommands(this.controlbox);

            this.topicbar = new _kiwi.view.TopicBar({el: $('#topic')[0]});

            new _kiwi.view.AppToolbar({el: $('#toolbar .app_tools')[0]});

            this.message = new _kiwi.view.StatusMessage({el: $('#status_message')[0]});

            this.resize_handle = new _kiwi.view.ResizeHandler({el: $('#memberlists_resize_handle')[0]});

            // Rejigg the UI sizes
            this.view.doLayout();
        };


        this.initializeGlobals = function () {
            _kiwi.global.connections = this.connections;
            
            _kiwi.global.components.Applet = _kiwi.model.Applet;
            _kiwi.global.components.Panel =_kiwi.model.Panel;
        };


        this.populateDefaultServerSettings = function (new_connection_dialog) {
            var parts;
            var defaults = {
                nick: getQueryVariable('nick') || '',
                server: '',
                port: 6667,
                ssl: false,
                channel: window.location.hash || '#chat',
                channel_key: ''
            };
            var uricheck;


            /**
             * Get any settings set by the server
             * These settings may be changed in the server selection dialog
             */
            if (this.server_settings.client) {
                if (this.server_settings.client.nick)
                    defaults.nick = this.server_settings.client.nick;

                if (this.server_settings.client.server)
                    defaults.server = this.server_settings.client.server;

                if (this.server_settings.client.port)
                    defaults.port = this.server_settings.client.port;

                if (this.server_settings.client.ssl)
                    defaults.ssl = this.server_settings.client.ssl;

                if (this.server_settings.client.channel)
                    defaults.channel = this.server_settings.client.channel;
            }



            /**
             * Get any settings passed in the URL
             * These settings may be changed in the server selection dialog
             */

            // Process the URL part by part, extracting as we go
            parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/');

            if (parts.length > 0) {
                parts.shift();

                if (parts.length > 0 && parts[0]) {
                    // Check to see if we're dealing with an irc: uri, or whether we need to extract the server/channel info from the HTTP URL path.
                    uricheck = parts[0].substr(0, 7).toLowerCase();
                    if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {
                        parts[0] = decodeURIComponent(parts[0]);
                        // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
                        uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);
                        /*
                            uricheck[1] = ssl (optional)
                            uricheck[2] = host
                            uricheck[3] = port (optional)
                            uricheck[4] = channel (optional)
                            uricheck[5] = channel key (optional, channel must also be set)<commit_msg>Client: Applying the nick + channel given in the URL properly #242<commit_after>_kiwi.model.Application = function () {
    // Set to a reference to this object within initialize()
    var that = null;

    // The auto connect details entered into the server select box
    var auto_connect_details = {};


    var model = function () {
        /** _kiwi.view.Application */
        this.view = null;

        /** _kiwi.view.StatusMessage */
        this.message = null;

        /* Address for the kiwi server */
        this.kiwi_server = null;

        this.initialize = function (options) {
            that = this;

            if (options[0].container) {
                this.set('container', options[0].container);
            }

            // The base url to the kiwi server
            this.set('base_path', options[0].base_path ? options[0].base_path : '/kiwi');

            // Any options sent down from the server
            this.server_settings = options[0].server_settings || {};

            // Best guess at where the kiwi server is
            this.detectKiwiServer();

            // Takes instances of model_network
            this.connections = new _kiwi.model.NetworkPanelList();
        };


        this.start = function () {
            // Only debug if set in the querystring
            if (!getQueryVariable('debug')) {
                manageDebug(false);
            } else {
                //manageDebug(true);
            }

            // Set the gateway up
            _kiwi.gateway = new _kiwi.model.Gateway();
            this.bindGatewayCommands(_kiwi.gateway);

            this.initializeClient();
            this.initializeGlobals();

            this.view.barsHide(true);

            this.showIntialConenctionDialog();
        };


        this.detectKiwiServer = function () {
            // If running from file, default to localhost:7777 by default
            if (window.location.protocol === 'file:') {
                this.kiwi_server = 'http://localhost:7778';
            } else {
                // Assume the kiwi server is on the same server
                this.kiwi_server = window.location.protocol + '//' + window.location.host;
            }
        };


        this.showIntialConenctionDialog = function() {
            var connection_dialog = new _kiwi.model.NewConnection();
            this.populateDefaultServerSettings(connection_dialog);

            connection_dialog.view.$el.addClass('initial');
            this.view.$el.find('.panel_container:first').append(connection_dialog.view.$el);

            var $info = $($('#tmpl_new_connection_info').html());

            if ($info.html()) {
                connection_dialog.view.infoBoxSet($info);
                connection_dialog.view.infoBoxShow();
            }

            // TODO: Shouldn't really be here but it's not working in the view.. :/
            // Hack for firefox browers: Focus is not given on this event loop iteration
            setTimeout(function(){
                connection_dialog.view.$el.find('.nick').select();
            }, 0);

            // Once connected, close this dialog and remove its own event
            var fn = function() {
                connection_dialog.view.$el.slideUp(function() {
                    connection_dialog.view.dispose();
                    connection_dialog = null;

                    _kiwi.gateway.off('onconnect', fn);
                });

            };
            _kiwi.gateway.on('onconnect', fn);
        };


        this.initializeClient = function () {
            this.view = new _kiwi.view.Application({model: this, el: this.get('container')});

            // Applets panel list
            this.applet_panels = new _kiwi.model.PanelList();
            this.applet_panels.view.$el.addClass('panellist applets');
            this.view.$el.find('#tabs').append(this.applet_panels.view.$el);

            /**
             * Set the UI components up
             */
            this.controlbox = new _kiwi.view.ControlBox({el: $('#controlbox')[0]});
            this.bindControllboxCommands(this.controlbox);

            this.topicbar = new _kiwi.view.TopicBar({el: $('#topic')[0]});

            new _kiwi.view.AppToolbar({el: $('#toolbar .app_tools')[0]});

            this.message = new _kiwi.view.StatusMessage({el: $('#status_message')[0]});

            this.resize_handle = new _kiwi.view.ResizeHandler({el: $('#memberlists_resize_handle')[0]});

            // Rejigg the UI sizes
            this.view.doLayout();
        };


        this.initializeGlobals = function () {
            _kiwi.global.connections = this.connections;
            
            _kiwi.global.components.Applet = _kiwi.model.Applet;
            _kiwi.global.components.Panel =_kiwi.model.Panel;
        };


        this.populateDefaultServerSettings = function (new_connection_dialog) {
            var parts;
            var defaults = {
                nick: '',
                server: '',
                port: 6667,
                ssl: false,
                channel: '#chat',
                channel_key: ''
            };
            var uricheck;


            /**
             * Get any settings set by the server
             * These settings may be changed in the server selection dialog or via URL parameters
             */
            if (this.server_settings.client) {
                if (this.server_settings.client.nick)
                    defaults.nick = this.server_settings.client.nick;

                if (this.server_settings.client.server)
                    defaults.server = this.server_settings.client.server;

                if (this.server_settings.client.port)
                    defaults.port = this.server_settings.client.port;

                if (this.server_settings.client.ssl)
                    defaults.ssl = this.server_settings.client.ssl;

                if (this.server_settings.client.channel)
                    defaults.channel = this.server_settings.client.channel;
            }



            /**
             * Get any settings passed in the URL
             * These settings may be changed in the server selection dialog
             */

            // Any query parameters first
            if (getQueryVariable('nick'))
                defaults.nick = getQueryVariable('nick');

            if (window.location.hash)
                defaults.channel = window.location.hash;


            // Process the URL part by part, extracting as we go
            parts = window.location.pathname.toString().replace(this.get('base_path'), '').split('/');

            if (parts.length > 0) {
                parts.shift();

                if (parts.length > 0 && parts[0]) {
                    // Check to see if we're dealing with an irc: uri, or whether we need to extract the server/channel info from the HTTP URL path.
                    uricheck = parts[0].substr(0, 7).toLowerCase();
                    if ((uricheck === 'ircs%3a') || (uricheck.substr(0,6) === 'irc%3a')) {
                        parts[0] = decodeURIComponent(parts[0]);
                        // irc[s]://<host>[:<port>]/[<channel>[?<password>]]
                        uricheck = /^irc(s)?:(?:\/\/?)?([^:\/]+)(?::([0-9]+))?(?:(?:\/)([^\?]*)(?:(?:\?)(.*))?)?$/.exec(parts[0]);
                        /*
                            uricheck[1] = ssl (optional)
                            uricheck[2] = host
                            uricheck[3] = port (optional)
                            uricheck[4] = channel (optional)
                            uricheck[5] = channel key (optional, channel must also be set)
{'fraction_non_alphanumeric': 0.12385901792257979, 'fraction_numerical': 0.005929775468052502, 'mean_word_length': 1.599584343609283, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '1870195', 'n_tokens_mistral': 4054, 'n_tokens_neox': 3826, 'n_words': 857}