<commit_before>var logger = require("winston");
var async = require("async");

module.exports = {
    version: "1.1.0",
    name: "User identifier",
    author: "Windsdon",
    init: WhoisMod
}

function WhoisMod(e, callback) {
    this._e = e;
    e._disco.addCommandHandler(async.apply(whoisHandler, e), "start");

    e.register.addCommand(["whois"], ["whois.id"], [
        {
            id: "user",
            type: "mention",
            required: true
        }
    ], whoisID, "View user aliases");

    e.register.addCommand(["whois", "set"], ["whois.set"], [
        {
            id: "user",
            type: "mention",
            required: true
        },
        {
            id: "nick",
            type: "string",
            required: false
        }
    ], whoisSet, "Set user alias");

    e.register.addCommand(["whois", "enable"], ["whois.config.enable"], [], whoisEnable, "Enable alias parsing");
    e.register.addCommand(["whois", "disable"], ["whois.config.disable"], [], whoisDisable, "Disable alias parsing");
    e.register.addCommand(["whois", "fix"], ["whois.fix"], [], whoisFix, "Update all users");

    callback();
}

// users db:
// uid, name (current name), old: [(previous names)]
// alias db:
// uid, alias OR config, value
function whoisHandler(e, o, callback) {
    var dbUsers = e.db.getDatabase("names");
    var dbAlias = e.db.getDatabase("alias", o.serverID);
    callback = callback || () => {};

    dbUsers.find({
        uid: o.userID
    }, function(err, data) {
        if(err) {
            logger.error(err);
            return;
        }

        if(data.length != 0) {
            if(data[0].name != o.user) {
                dbUsers.update({ _id: data[0]._id }, {
                    $set: {
                        name: o.user
                    }
                }, {}, function(err, num) {
                    if(err) {
                        logger.error(err);
                    }
                });
            }
            if(data[0].old.indexOf(o.user) == -1) {
                // add new alias
                dbUsers.update({ _id: data[0]._id }, {
                    $push: {
                        old: o.user
                    }
                }, {}, function(err, num) {
                    if(err) {
                        logger.error(err);
                    }
                });
            }
        } else {
            logger.debug("Insert: " + JSON.stringify(o));
            dbUsers.insert({
                uid: o.userID,
                name: o.user,
                old: [o.user]
            }, function(err, data) {
                if(err) {
                    logger.error(err);
                    return;
                }
            });
        }
    });


    // parse aliases<commit_msg>Fixed debug messages crashing whois handler<commit_after>var logger = require("winston");
var async = require("async");

module.exports = {
    version: "1.1.0",
    name: "User identifier",
    author: "Windsdon",
    init: WhoisMod
}

function WhoisMod(e, callback) {
    this._e = e;
    e._disco.addCommandHandler(async.apply(whoisHandler, e), "start");

    e.register.addCommand(["whois"], ["whois.id"], [
        {
            id: "user",
            type: "mention",
            required: true
        }
    ], whoisID, "View user aliases");

    e.register.addCommand(["whois", "set"], ["whois.set"], [
        {
            id: "user",
            type: "mention",
            required: true
        },
        {
            id: "nick",
            type: "string",
            required: false
        }
    ], whoisSet, "Set user alias");

    e.register.addCommand(["whois", "enable"], ["whois.config.enable"], [], whoisEnable, "Enable alias parsing");
    e.register.addCommand(["whois", "disable"], ["whois.config.disable"], [], whoisDisable, "Disable alias parsing");
    e.register.addCommand(["whois", "fix"], ["whois.fix"], [], whoisFix, "Update all users");

    callback();
}

// users db:
// uid, name (current name), old: [(previous names)]
// alias db:
// uid, alias OR config, value
function whoisHandler(e, o, callback) {
    var dbUsers = e.db.getDatabase("names");
    var dbAlias = e.db.getDatabase("alias", o.serverID);
    callback = callback || () => {};

    dbUsers.find({
        uid: o.userID
    }, function(err, data) {
        if(err) {
            logger.error(err);
            return;
        }

        if(data.length != 0) {
            if(data[0].name != o.user) {
                dbUsers.update({ _id: data[0]._id }, {
                    $set: {
                        name: o.user
                    }
                }, {}, function(err, num) {
                    if(err) {
                        logger.error(err);
                    }
                });
            }
            if(data[0].old.indexOf(o.user) == -1) {
                // add new alias
                dbUsers.update({ _id: data[0]._id }, {
                    $push: {
                        old: o.user
                    }
                }, {}, function(err, num) {
                    if(err) {
                        logger.error(err);
                    }
                });
            }
        } else {
            dbUsers.insert({
                uid: o.userID,
                name: o.user,
                old: [o.user]
            }, function(err, data) {
                if(err) {
                    logger.error(err);
                    return;
                }
            });
        }
    });


    // parse aliases
{'fraction_non_alphanumeric': 0.15748740100791936, 'fraction_numerical': 0.0032397408207343412, 'mean_word_length': 1.3077242524916943, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3479507', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1578, 'n_words': 322}