<commit_before>var promise = require('bluebird');
var base = require('./base');
var interfacePrefs,
    pluginPrefs,
    promise;

module.exports = function(deps){
    interfacePrefs = deps.interfacePrefs;
    pluginPrefs = deps.pluginPrefs;
    promise = deps.promise;
    listeners = [];
    messageSenders = {};
    yesNoQuestions = [];
    name = 'woodhouse';
    interfaces = {};
    plugins = {};

    this.listen = function(listener, command){
        this.listeners.push({listener: listener, command: command});
    };

    this.messageRecieved = function(from, interface, message) {
        for (var i = 0, len = this.listeners.length; i < len; i++) {
            var regex = new RegExp("^" + this.name + " "+ this.listeners[i].listener + "$", 'i'),
                match = regex.exec(message);
            if (match) {
                match.shift();
                this.listeners[i].command(from, interface, match);
            }
        }
    };

    this.addMessageSender = function(name, command){
        this.messageSenders[name] = command;
    }

    this.sendMessage = function(message, interface, to){
        if (interface) {
            this.messageSenders[interface](message, to);
        } else {
            for (var interface in this.messageSenders) {
                this.messageSenders[interface](message, to);
            }
        }
    };

    this.addYesNoQuestion = function(question, yesCallback, noCallback){
        this.yesNoQuestions.push({question: question, yesCallback: yesCallback, noCallback: noCallback});
    };

    this.removeLastYesNoQuestion = function(){
        this.yesNoQuestions.pop();
    };

    this.loadInterface = function(file) {
        var self = this,
            module = require("../interfaces/" + file);

        this.interfaces[file] = new module();
        this.addBaseClasses(this.interfaces[file], 'interface');
        this.interfaces[file].loadApi(this);
        interfacePrefs.findOneAsync({name: this.interfaces[file].name}).done(function(doc){
            if (doc && doc.enabled === true) {
                self.interfaces[file].init();
            }

            if (!doc) {
                var prefs = {
                    name: self.interfaces[file].name,
                    enabled:  false,
                    prefs: self.interfaces[file].defaultPrefs || {}
                }
                interfacePrefs.insertAsync(prefs);
            }
        });
    };

    this.loadPlugin = function(file) {
        var self = this,
            module = require("../plugins/" + file);

        this.plugins[file] = new module();
        this.addBaseClasses(this.plugins[file], 'plugin');
        this.plugins[file].loadApi(this);
        pluginPrefs.findOneAsync({name: this.plugins[file].name}).done(function(doc){
            if (doc && doc.enabled === true) {
                self.plugins[file].init();
            }

            if (!doc) {
                var prefs = self.plugins[file].defaultPrefs || {};
                prefs.name = self.plugins[file].name;
                prefs.enabled =  false;
                pluginPrefs.insertAsync(prefs);
            }
        });
    };

    this.getInterfacePrefs = function(name, key){
        var searchObj = {name: name};
        return new promise(function(resolve, reject) {
            if(key){
                searchObj['prefs.name'] = key;
            }
            interfacePrefs.findOneAsync({name: name}).done(function(doc){
                var prefs = {};
                for (var i = 0, len = doc.prefs.length; i < len; i++) {
                    prefs[doc.prefs[i].name] = doc.prefs[i].value;
                }
                resolve(prefs);
            });
        });
    }


    this.addBaseClasses = function(module, type){<commit_msg>updated api.js to have getPluginprefs and corrected plugin init to match interface<commit_after>var promise = require('bluebird');
var base = require('./base');
var interfacePrefs,
    pluginPrefs,
    promise;

module.exports = function(deps){
    interfacePrefs = deps.interfacePrefs;
    pluginPrefs = deps.pluginPrefs;
    promise = deps.promise;
    listeners = [];
    messageSenders = {};
    yesNoQuestions = [];
    name = 'woodhouse';
    interfaces = {};
    plugins = {};

    this.listen = function(listener, command){
        this.listeners.push({listener: listener, command: command});
    };

    this.messageRecieved = function(from, interface, message) {
        for (var i = 0, len = this.listeners.length; i < len; i++) {
            var regex = new RegExp("^" + this.name + " "+ this.listeners[i].listener + "$", 'i'),
                match = regex.exec(message);
            if (match) {
                match.shift();
                this.listeners[i].command(from, interface, match);
            }
        }
    };

    this.addMessageSender = function(name, command){
        this.messageSenders[name] = command;
    }

    this.sendMessage = function(message, interface, to){
        if (interface) {
            this.messageSenders[interface](message, to);
        } else {
            for (var interface in this.messageSenders) {
                this.messageSenders[interface](message, to);
            }
        }
    };

    this.addYesNoQuestion = function(question, yesCallback, noCallback){
        this.yesNoQuestions.push({question: question, yesCallback: yesCallback, noCallback: noCallback});
    };

    this.removeLastYesNoQuestion = function(){
        this.yesNoQuestions.pop();
    };

    this.loadInterface = function(file) {
        var self = this,
            module = require("../interfaces/" + file);

        this.interfaces[file] = new module();
        this.addBaseClasses(this.interfaces[file], 'interface');
        this.interfaces[file].loadApi(this);
        interfacePrefs.findOneAsync({name: this.interfaces[file].name}).done(function(doc){
            if (doc && doc.enabled === true) {
                self.interfaces[file].init();
            }

            if (!doc) {
                var prefs = {
                    name: self.interfaces[file].name,
                    enabled:  false,
                    prefs: self.interfaces[file].defaultPrefs || []
                }
                interfacePrefs.insertAsync(prefs);
            }
        });
    };

    this.loadPlugin = function(file) {
        var self = this,
            module = require("../plugins/" + file);

        this.plugins[file] = new module();
        this.addBaseClasses(this.plugins[file], 'plugin');
        this.plugins[file].loadApi(this);
        pluginPrefs.findOneAsync({name: this.plugins[file].name}).done(function(doc){
            if (doc && doc.enabled === true) {
                self.plugins[file].init();
            }

            if (!doc) {
                var prefs = {
                    name: self.plugins[file].name,
                    enabled:  false,
                    prefs: self.plugins[file].defaultPrefs || []
                }
                pluginPrefs.insertAsync(prefs);
            }
        });
    };

    this.getInterfacePrefs = function(name, key){
        var searchObj = {name: name};
        return new promise(function(resolve, reject) {
            if(key){
                searchObj['prefs.name'] = key;
            }
            interfacePrefs.findOneAsync({name: name}).done(function(doc){
                var prefs = {};
                for (var i = 0, len = doc.prefs.length; i < len; i++) {
                    prefs[doc.prefs[i].name] = doc.prefs[i].value;
                }
                resolve(prefs);
            });
        });
    };

    this.getPluginPrefs = function(name, key){
        var searchObj = {name: name};
        return new promise(function(resolve, reject) {
            if(key){
                searchObj['prefs.name'] = key;
            }
            pluginPrefs.findOneAsync({name: name}).done(function(doc){
                var prefs = {};
                for (var i = 0, len = doc.prefs.length; i < len; i++) {
                    prefs[doc.prefs[i].name] = doc.prefs[i].value;
                }
                resolve(prefs);
            });
        });
    }


    this.addBaseClasses = function(module, type){
{'fraction_non_alphanumeric': 0.13730979914790017, 'fraction_numerical': 0.0006086427267194157, 'mean_word_length': 1.7974123255022132, 'pattern_counts': {'":': 0, '<': 8, '<?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': '3563779', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2247, 'n_words': 421}