<commit_before>/*
 * creates a message_manage object that uses the Message Manager API:
 * include this file, then initialise the object when the page is loaded with
 * message_manager.config(settings)
 *
 * i.e., you *must* do something like:
 *
 *   $(document).ready(function() { 
 *     message_manager.config({url_root:'http://yourdomain.com/messages'})
 *   }
 *
 * You'll need to set the url_root, but you can leave everything else to default 
 * provided your HTML ids and classes are the same as ours (which they might be:
 * see the Message Manager's dummy client (at /client) to see the HTML we use).
 *
 * The (optional) single parameter for .config() is a hash of name-value pairs:
 *
 *     url_root           accepts the root URL to the message manager.
 *
 *     want_unique_locks  normally MM clients should relinquish all other locks 
 *                        when claiming a new one so want_unique_locks defaults 
 *                        to true; but you can set it explicitly here.
 *
 *     mm_name            name of Message Manager (used in error messages shown
 *                        to user, e.g., "please log in to Message Manager")
 *
 *     msg_prefix         all message <li> items have this as their ID prefix
 *
 *     want_nice_msgs     don't use language like "lock granted"
 *
 *     *_selector         these are the jQuery selects that will be used to find
 *                        the respective elements:
 *
 *                              message_list_selector: list of messages
 *                              status_selector:       status message display
 *                              login_selector:        login form 
 *
 * 
 *   Summary of all methods:
 *     message_manager.config([options])
 *     message_manager.setup_click_listener([options])
 *     message_manager.get_available_messages([options])
 *     message_manager.request_lock(msg_id, [options])  (default use: client code doesn't need to call this explicitly)
 *     message_manager.assign_fms_id(msg_id, fms_id, [options])
 *     message_manager.hide(msg_id, reason_text, [options])
 *     message_manager.reply(msg_id, reply_text, [options])
 *     message_manager.show_info(msg_id)
 *     message_manager.sign_out()
 *
 *  Note: options are {name:value, ...} hashes and often include "callback" which is a function that is executed on success
 *        but see the docs (request_lock executes callback if the call is successful even if the lock was denied, for example).
 *        Some methods take 'callback' as the only option, but you still need to pass it as a named option.
*/

var message_manager = (function() {

    // default/config values: to be overridden using "config({name:value, ...})"

    var _url_root              = 'http://www.example.com/message_manager/';
    var _want_unique_locks     = true; 
    var _msg_prefix            = "msg-";
    var _username;
    var _mm_name               = "Message Manager";
    var _use_fancybox          = true; // note: currently *must* have fancybox!
    var _want_nice_msgs        = false;
    
    // cached jQuery elements, populated by the (mandatory) call to config()
    var $message_list_element;
    var $status_element;
    var $login_element;
    var $htauth_username;
    var $htauth_password;
    var $hide_reasons;
    var $boilerplate_replies;

    var msg_no_config_err   = "Config error: no Message Manager URL has been specified";

    var msg_trying_for_lock = ["Trying for lock...", "Checking message..." ];
    var msg_checking_lock   = ["Checking lock...",   "Checking message..." ];
    var msg_claiming_lock   = ["Claiming lock...",   "Checking message..." ];
    var msg_lock_granted_ok = ["Lock granted OK",    "Checking message... OK"];
    var msg_lock_denied     = ["",                   "Someone is working with that message right now!"];

    function get_msg(msg) {
        return msg[_want_nice_msgs? 1 : 0];
    }

    var config = function(settings) {
        var selectors = {
            message_list_selector:    '#mm-message-list',
            status_selector:          '#mm-status-message-container',
            login_selector:           '#mm-login-container',
            username_selector:        '#mm-received-username',
            htauth_username_selector: '#mm-htauth-username',
            htauth_password_selector: '#mm-htauth-password',
            boilerplate_hide_reasons: '#mm-boilerplate-hide-reasons-box',
            boilerplate_replies:      '#mm-boilerplate-replies-box'
        };
        if (settings) {
            if (typeof settings.url_root === 'string') {
                _url_root = settings.url_root;
                if (_url_root.length > 0 && _url_root.charAt(_url_root.length-1) !== "/") {
                    _url_root+="/";
                }
            }
            if (typeof settings.want_unique_locks !== 'undefined') {
                _want_unique_locks = settings.want_unique_locks;
            }
            if (typeof settings.msg_prefix === 'string') {
                _msg_prefix = settings.msg_prefix;
            }
            for (var sel in selectors) {
                if (typeof settings[sel] === 'string') {
                    selectors[sel] = settings[sel];
                }
            }
            if (typeof settings.mm_name === 'string') {
                _mm_name = settings.mm_name;
            }
            if (typeof settings.want_nice_msgs !== 'undefined') {
                _want_nice_msgs = settings.want_nice_msgs;
            }
        }
        $message_list_element = $(selectors.message_list_selector);
        $status_element = $(selectors.status_selector);
        $login_element = $(selectors.login_selector);
        $htauth_username = $(selectors.htauth_username_selector);
        $htauth_password = $(selectors.htauth_password_selector);
        $hide_reasons = $(selectors.boilerplate_hide_reasons);
        $boilerplate_replies = $(selectors.boilerplate_replies);
        if (typeof settings.url_root === 'string' && _url_root.length===0) {
            say_status(msg_no_config_err);
        }
    };

    var make_base_auth = function(user, password) {
        var tok = user + ':' + password;
        var hash = encodeBase64(tok); // window.btoa(tok) doesn't work on all browers
        return "Basic " + hash;
    };
    
    function encodeBase64(input) {
        var
            chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
            INVALID_CHARACTER_ERR = (function () {
        // fabricate a suitable error object
        try { document.createElement('$'); }
        catch (error) { return error; }}());
        // encoder
        // [https://gist.github.com/999166] by [https://github.com/nignag]
        for (
            var block, charCode, idx = 0, map = chars, output = '';
            input.charAt(idx | 0) || (map = '=', idx % 1);
            output += map.charAt(63 & block >> 8 - idx % 1 * 8)
        ) {
            charCode = input.charCodeAt(idx += 3/4);
            if (charCode > 0xFF) throw INVALID_CHARACTER_ERR;
            block = block << 8 | charCode;
        }
        return output;
    };

    var get_current_auth_credentials = function() {
        var base_auth = "";
        var htauth_un = "";
        var htauth_pw = "";
        if ($htauth_username.size()) {
            htauth_un = $htauth_username.val();
            htauth_pw = $htauth_password.val();
        }
        if (htauth_un.length === 0 && Modernizr.sessionstorage && sessionStorage.getItem('mm_auth')) {
            base_auth = sessionStorage.getItem('mm_auth');
        } else {
            base_auth = make_base_auth(htauth_un, htauth_pw);
            if (Modernizr.sessionstorage) {
                sessionStorage.mm_auth = base_auth;
            }
        }
        return base_auth;
    };
    
    var sign_out = function() { // clear_current_auth_credentials
        if (Modernizr.sessionstorage) {
            sessionStorage.removeItem('mm_auth');
        }
        if ($htauth_password) {
            $htauth_password.val('');<commit_msg>tweaked by Mark to pass jslint; base64 encode manually<commit_after>/*
 * creates a message_manage object that uses the Message Manager API:
 * include this file, then initialise the object when the page is loaded with
 * message_manager.config(settings)
 *
 * i.e., you *must* do something like:
 *
 *   $(document).ready(function() { 
 *     message_manager.config({url_root:'http://yourdomain.com/messages'})
 *   }
 *
 * You'll need to set the url_root, but you can leave everything else to default 
 * provided your HTML ids and classes are the same as ours (which they might be:
 * see the Message Manager's dummy client (at /client) to see the HTML we use).
 *
 * The (optional) single parameter for .config() is a hash of name-value pairs:
 *
 *     url_root           accepts the root URL to the message manager.
 *
 *     want_unique_locks  normally MM clients should relinquish all other locks 
 *                        when claiming a new one so want_unique_locks defaults 
 *                        to true; but you can set it explicitly here.
 *
 *     mm_name            name of Message Manager (used in error messages shown
 *                        to user, e.g., "please log in to Message Manager")
 *
 *     msg_prefix         all message <li> items have this as their ID prefix
 *
 *     want_nice_msgs     don't use language like "lock granted"
 *
 *     *_selector         these are the jQuery selects that will be used to find
 *                        the respective elements:
 *
 *                              message_list_selector: list of messages
 *                              status_selector:       status message display
 *                              login_selector:        login form 
 *
 * 
 *   Summary of all methods:
 *     message_manager.config([options])
 *     message_manager.setup_click_listener([options])
 *     message_manager.get_available_messages([options])
 *     message_manager.request_lock(msg_id, [options])  (default use: client code doesn't need to call this explicitly)
 *     message_manager.assign_fms_id(msg_id, fms_id, [options])
 *     message_manager.hide(msg_id, reason_text, [options])
 *     message_manager.reply(msg_id, reply_text, [options])
 *     message_manager.show_info(msg_id)
 *     message_manager.sign_out()
 *
 *  Note: options are {name:value, ...} hashes and often include "callback" which is a function that is executed on success
 *        but see the docs (request_lock executes callback if the call is successful even if the lock was denied, for example).
 *        Some methods take 'callback' as the only option, but you still need to pass it as a named option.
*/

var message_manager = (function() {

    // default/config values: to be overridden using "config({name:value, ...})"

    var _url_root              = 'http://www.example.com/message_manager/';
    var _want_unique_locks     = true; 
    var _msg_prefix            = "msg-";
    var _username;
    var _mm_name               = "Message Manager";
    var _use_fancybox          = true; // note: currently *must* have fancybox!
    var _want_nice_msgs        = false;
    
    // cached jQuery elements, populated by the (mandatory) call to config()
    var $message_list_element;
    var $status_element;
    var $login_element;
    var $htauth_username;
    var $htauth_password;
    var $hide_reasons;
    var $boilerplate_replies;

    var msg_no_config_err   = "Config error: no Message Manager URL has been specified";

    var msg_trying_for_lock = ["Trying for lock...", "Checking message..." ];
    var msg_checking_lock   = ["Checking lock...",   "Checking message..." ];
    var msg_claiming_lock   = ["Claiming lock...",   "Checking message..." ];
    var msg_lock_granted_ok = ["Lock granted OK",    "Checking message... OK"];
    var msg_lock_denied     = ["",                   "Someone is working with that message right now!"];

    function get_msg(msg) {
        return msg[_want_nice_msgs? 1 : 0];
    }

    var config = function(settings) {
        var selectors = {
            message_list_selector:    '#mm-message-list',
            status_selector:          '#mm-status-message-container',
            login_selector:           '#mm-login-container',
            username_selector:        '#mm-received-username',
            htauth_username_selector: '#mm-htauth-username',
            htauth_password_selector: '#mm-htauth-password',
            boilerplate_hide_reasons: '#mm-boilerplate-hide-reasons-box',
            boilerplate_replies:      '#mm-boilerplate-replies-box'
        };
        if (settings) {
            if (typeof settings.url_root === 'string') {
                _url_root = settings.url_root;
                if (_url_root.length > 0 && _url_root.charAt(_url_root.length-1) !== "/") {
                    _url_root+="/";
                }
            }
            if (typeof settings.want_unique_locks !== 'undefined') {
                _want_unique_locks = settings.want_unique_locks;
            }
            if (typeof settings.msg_prefix === 'string') {
                _msg_prefix = settings.msg_prefix;
            }
            for (var sel in selectors) {
                if (typeof settings[sel] === 'string') {
                    selectors[sel] = settings[sel];
                }
            }
            if (typeof settings.mm_name === 'string') {
                _mm_name = settings.mm_name;
            }
            if (typeof settings.want_nice_msgs !== 'undefined') {
                _want_nice_msgs = settings.want_nice_msgs;
            }
        }
        $message_list_element = $(selectors.message_list_selector);
        $status_element = $(selectors.status_selector);
        $login_element = $(selectors.login_selector);
        $htauth_username = $(selectors.htauth_username_selector);
        $htauth_password = $(selectors.htauth_password_selector);
        $hide_reasons = $(selectors.boilerplate_hide_reasons);
        $boilerplate_replies = $(selectors.boilerplate_replies);
        if (typeof settings.url_root === 'string' && _url_root.length===0) {
            say_status(msg_no_config_err);
        }
    };

    var make_base_auth = function(user, password) {
        var tok = user + ':' + password;
        var hash = encodeBase64(tok); // window.btoa(tok) doesn't work on all browers
        return "Basic " + hash;
    };
    
    function encodeBase64(input) {
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
            INVALID_CHARACTER_ERR = (function () {
                // fabricate a suitable error object
                try {
                    document.createElement('$');
                } catch (error) {
                    return error;
                }
            }()),
            block, charCode, idx, map, output;
        // encoder (with wee change by mhl Mark to satisfy jslint)
        // [https://gist.github.com/999166] by [https://github.com/nignag]
        for (idx = 0, map = chars, output = '';
             input.charAt(idx | 0) || ((map = '=') && (idx % 1));
             output += map.charAt(63 & block >> 8 - idx % 1 * 8)) {
            charCode = input.charCodeAt(idx += 3/4);
            if (charCode > 0xFF) {
                throw INVALID_CHARACTER_ERR;
            }
            block = block << 8 | charCode;
        }
        return output;
    }

    var get_current_auth_credentials = function() {
        var base_auth = "";
        var htauth_un = "";
        var htauth_pw = "";
        if ($htauth_username.size()) {
            htauth_un = $htauth_username.val();
            htauth_pw = $htauth_password.val();
        }
        if (htauth_un.length === 0 && Modernizr.sessionstorage && sessionStorage.getItem('mm_auth')) {
            base_auth = sessionStorage.getItem('mm_auth');
        } else {
            base_auth = make_base_auth(htauth_un, htauth_pw);
            if (Modernizr.sessionstorage) {
                sessionStorage.mm_auth = base_auth;
            }
        }
        return base_auth;
    };
    
    var sign_out = function() { // clear_current_auth_credentials
        if (Modernizr.sessionstorage) {
            sessionStorage.removeItem('mm_auth');
        }
        if ($htauth_password) {
            $htauth_password.val('');
{'fraction_non_alphanumeric': 0.10537883875694487, 'fraction_numerical': 0.004762195494230417, 'mean_word_length': 2.103448275862069, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '6434958', 'n_tokens_mistral': 4801, 'n_tokens_neox': 4608, 'n_words': 1208}