<commit_before>'use strict';

class dispatcher {
    constructor(users, moduleData, systemPrefs) {
        this.users = users;
        this.moduleData = moduleData;
        this.systemPrefs = systemPrefs;
        this.listeners = {};
        this.messageSenders = {};
    }

    addMessageSender(moduleName, callback) {
        this.messageSenders[moduleName] = callback;
    }

    messageRecieved(interfaceName, from, message, interfaceUser) {
        let name;
        message = message.trim();

        if (typeof interfaceUser === 'undefined') {
            interfaceUser = from;
        }

        return this.systemPrefs.get('name').then((instanceName) => {
            name = instanceName;

            return this.users.userFromAccount(interfaceUser, interfaceName);
        }).then((user) => {
            return user.role;
        }).catch(() => {
            return this.moduleData.get('interface', interfaceName, 'default_permission');
        }).then((role) => {
            if (role && role.length  > 0) {
                return role;
            }

            return this.systemPrefs.get('default_permission');
        }).then((role) => {
            for (let listenerObjectKey in this.listeners) {
                let listener = this.listeners[listenerObjectKey].listener.replace(/\(:<(.+?)>/g, '(');

                let pattern = new RegExp("^" + name + " "+ listener + "$", 'i');

                let match = pattern.exec(message);

                if (match && this.checkRole(role, this.listeners[listenerObjectKey].role)) {
                    match.shift()
                    this.listeners[listenerObjectKey].command(from, interfaceName, match, interfaceUser);
                }
            }
        });
    }

    listen(module, listener, roleNeeded, command, isAlias) {
        if (typeof isAlias === 'undefined') {
            isAlias = false;
        }

        let listenerKey = module.name + listener.replace(
            /(\((:<(.+?)>)*([\(].+?[\)])*(.+?)\))/g,
            (group, groupinner, namegroup, name) => {
                if (typeof name !== 'undefined') {
                    return '<<' + name + '>>'
                }

                return group;
            }
        );

        this.listeners[listenerKey] = {
            module,
            listener,
            role: roleNeeded,
            command,
            isAlias
        }
    }

    sendMessage(to, interfaceName, message) {
        if (this.messageSenders[interfaceName]) {
            this.messageSenders[interfaceName](to, message);
        }
    }

    loadListenerAliases(module) {
        return this.moduleData.get('plugin', module.name, 'listeneraliases').then((aliases) => {
            for (let aliasObject of aliases) {
                let registeredListener = this.listeners[module.name + aliasObject.listener];

                if (registeredListener) {
                    let listenerParts = registeredListener.listener.match(/\((.+?)\)/g);
                    let newPartsOrder = [];
                    let aliasParts = aliasObject.alias.match(/<<(.+?)>>/g);

                    listenerParts.forEach(function(value, i) {
                        let partName = value.match(/:<(.+?)>/);

                        if (!partName) {
                            return;
                        }

                        partName = partName[1];

                        let aliasPartNum = aliasParts.indexOf('<<' + partName + '>>');

                        newPartsOrder[aliasPartNum] = i;<commit_msg>Added debounce to messagesender to group messages sent to an account within a 500ms period<commit_after>'use strict';

class dispatcher {
    constructor(users, moduleData, systemPrefs) {
        this.users = users;
        this.moduleData = moduleData;
        this.systemPrefs = systemPrefs;
        this.listeners = {};
        this.messageSenders = {};
        this.messageQueue = {};
    }

    addMessageSender(moduleName, callback) {
        this.messageSenders[moduleName] = callback;
    }

    messageRecieved(interfaceName, from, message, interfaceUser) {
        let name;
        message = message.trim();

        if (typeof interfaceUser === 'undefined') {
            interfaceUser = from;
        }

        return this.systemPrefs.get('name').then((instanceName) => {
            name = instanceName;

            return this.users.userFromAccount(interfaceUser, interfaceName);
        }).then((user) => {
            return user.role;
        }).catch(() => {
            return this.moduleData.get('interface', interfaceName, 'default_permission');
        }).then((role) => {
            if (role && role.length  > 0) {
                return role;
            }

            return this.systemPrefs.get('default_permission');
        }).then((role) => {
            for (let listenerObjectKey in this.listeners) {
                let listener = this.listeners[listenerObjectKey].listener.replace(/\(:<(.+?)>/g, '(');

                let pattern = new RegExp("^" + name + " "+ listener + "$", 'i');

                let match = pattern.exec(message);

                if (match && this.checkRole(role, this.listeners[listenerObjectKey].role)) {
                    match.shift()
                    this.listeners[listenerObjectKey].command(from, interfaceName, match, interfaceUser);
                }
            }
        });
    }

    listen(module, listener, roleNeeded, command, isAlias) {
        if (typeof isAlias === 'undefined') {
            isAlias = false;
        }

        let listenerKey = module.name + listener.replace(
            /(\((:<(.+?)>)*([\(].+?[\)])*(.+?)\))/g,
            (group, groupinner, namegroup, name) => {
                if (typeof name !== 'undefined') {
                    return '<<' + name + '>>'
                }

                return group;
            }
        );

        this.listeners[listenerKey] = {
            module,
            listener,
            role: roleNeeded,
            command,
            isAlias
        }
    }

    sendMessage(to, interfaceName, message, debounce = true) {
        if (this.messageSenders[interfaceName]) {
            if (debounce) {
                if (typeof this.messageQueue[`${interfaceName}${to}`] === `undefined`) {
                    this.messageQueue[`${interfaceName}${to}`] = {
                        messages:[]
                    }
                }

                this.messageQueue[`${interfaceName}${to}`].messages.push(message);

                if (typeof this.messageQueue[`${interfaceName}${to}`].timeout === `undefined`) {
                    this.messageQueue[`${interfaceName}${to}`].timeout = setTimeout(() => {
                        let groupedMessage = this.messageQueue[`${interfaceName}${to}`].messages.join(`\n`);
                        this.messageSenders[interfaceName](to, groupedMessage);
                        delete this.messageQueue[`${interfaceName}${to}`];
                    }, 500)
                }
            } else {
                this.messageSenders[interfaceName](to, message);
            }
        }
    }

    loadListenerAliases(module) {
        return this.moduleData.get('plugin', module.name, 'listeneraliases').then((aliases) => {
            for (let aliasObject of aliases) {
                let registeredListener = this.listeners[module.name + aliasObject.listener];

                if (registeredListener) {
                    let listenerParts = registeredListener.listener.match(/\((.+?)\)/g);
                    let newPartsOrder = [];
                    let aliasParts = aliasObject.alias.match(/<<(.+?)>>/g);

                    listenerParts.forEach(function(value, i) {
                        let partName = value.match(/:<(.+?)>/);

                        if (!partName) {
                            return;
                        }

                        partName = partName[1];

                        let aliasPartNum = aliasParts.indexOf('<<' + partName + '>>');

                        newPartsOrder[aliasPartNum] = i;
{'fraction_non_alphanumeric': 0.13841002108396377, 'fraction_numerical': 0.0012402331638348009, 'mean_word_length': 1.6190321532965248, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 38, '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': '575022', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2028, 'n_words': 374}