{"size": 1455, "ext": "js", "max_stars_repo_path": "modules/regex/regex.js", "max_stars_repo_name": "Kagammor/depressionbot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "modules/regex/regex.js", "max_issues_repo_name": "Kagammor/depressionbot", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "modules/regex/regex.js", "max_forks_repo_name": "Kagammor/depressionbot", "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": 35.487804878, "max_line_length": 111, "alphanum_fraction": 0.4762886598}
var _ = require('underscore')._;

var regex = function(dbot) {
    this.last = {};
    this.listener = function(event) {
        var q = event.message.valMatch(/^([\d\w\s]*)?:? ?s\/(.+)\/(.+)?\/([ig]*)?$/, 5);
        if(q) {
            var flags = q[4],
                toMatch = new RegExp(q[2], flags),
                replaceWith = q[3],
                last,
                replacement;
                
            if(!replaceWith) replaceWith = "";

            if(q[1] != null) {
                var user = q[1];
                last = this.last[event.channel.name][user];
                replacement = last.replace(toMatch, replaceWith);
                if(replacement != last) event.reply(event.user + " thinks " + user + " meant: " + replacement);
            } else {
                last = this.last[event.channel.name][event.user];
                replacement = last.replace(toMatch, replaceWith);
                if(replacement != last) event.reply(event.user + " meant: " + replacement);
            }
        } else {
            if(_.has(this.last, event.channel.name)) {
               this.last[event.channel.name][event.user] = event.message; 
            } else {
                this.last[event.channel.name] = { };
                this.last[event.channel.name][event.user] = event.message;
            }
        }
    }.bind(this);
    this.on = [ 'PRIVMSG' ];
};

exports.fetch = function(dbot) {
    return new regex(dbot);
};
