{"size": 1593, "ext": "js", "max_stars_repo_path": "library/irc.js", "max_stars_repo_name": "stanier/avdg-irc-bot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "library/irc.js", "max_issues_repo_name": "stanier/avdg-irc-bot", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "library/irc.js", "max_forks_repo_name": "stanier/avdg-irc-bot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.4655172414, "max_line_length": 115, "alphanum_fraction": 0.6051475204}
var irc = require("irc");

var ircBot = function(settings) {
    this.settings = settings;
    this.settings.plugins = this.settings.plugins || {};
};

ircBot.prototype.connect = function() {
    var self = this;
    this.client = new irc.Client('chat.freenode.net', 'Alica', {
        channels: this.settings.channels,
    });

    this.client.addListener('message', function (from, to, text, message) {
        console.log(from + ' => ' + to + ': ' + text);
        self.emitPlugins('message', from, to, text, message);
    });

    this.client.addListener('message#', function(from, to, text, message) {
        self.emitPlugins('message#', from, to, text, message);
    });

    this.client.addListener('pm', function (from, message) {
        self.emitPlugins('pm', from, message);
    });

    this.client.addListener('error', function(message) {
        console.log('error: ', message);
    });
};

ircBot.prototype.disconnect = function(reason) {
    reason = reason || "Leaving...";
    this.client.disconnect(reason);
};

ircBot.prototype.on = function() {
    this.client.addListener.apply(this.client, arguments);
};

ircBot.prototype.emitPlugins = function() {
    var type = arguments[0];
    var args = [];

    for (var i = 1; i < arguments.length; i++) {
        args.push(arguments[i]);
    }

    for (var j in this.settings.plugins) {
        if (typeof this.settings.plugins[j] === "object" && typeof this.settings.plugins[j][type] === "function") {
            this.settings.plugins[j][type].apply(this, args);
        }
    }
};

module.exports = {
    ircBot: ircBot
};