<commit_before>'use strict';
/* globals module, require */

var winston = module.parent.require('winston'),
    async = module.parent.require('async'),
    nconf = module.parent.require('nconf'),

    Meta = module.parent.require('./meta'),
    User = module.parent.require('./user'),
    Posts = module.parent.require('./posts'),
    Topics = module.parent.require('./topics'),
    hostEmailer = module.parent.require('./emailer'),
    Privileges = module.parent.require('./privileges'),
    SocketHelpers = module.parent.require('./socket.io/helpers'),

    mandrill,
    Emailer = {
        settings: {}
    };

Emailer.init = function(data, callback) {

    var render = function(req, res) {
        res.render('admin/plugins/emailer-mandrill', {
            url: nconf.get('url'),
            base_url: nconf.get('base_url')
        });
    };

    Meta.settings.get('mandrill', function(err, settings) {
        Emailer.settings = Object.freeze(settings);
        Emailer.receiptRegex = new RegExp('^reply-([\\d]+)@' + Emailer.settings.receive_domain + '$');

        if (!err && settings && settings.apiKey) {
            mandrill = require('node-mandrill')(settings.apiKey || 'Replace Me');
        } else {
            winston.error('[plugins/emailer-mandrill] API key not set!');
        }

        data.router.get('/admin/plugins/emailer-mandrill', data.middleware.admin.buildHeader, render);
        data.router.get('/api/admin/plugins/emailer-mandrill', render);
        data.router.head('/emailer-mandrill/reply', function(req, res) {
            res.sendStatus(200);
        });
        data.router.post('/emailer-mandrill/reply', Emailer.receive);

        if (typeof callback === 'function') {
            callback();
        }
    });
};

Emailer.send = function(data, callback) {
    if (mandrill) {
        var headers = {};

        if (data._raw.notification.pid && Emailer.settings.hasOwnProperty('receive_domain')) {
            headers['Reply-To'] = 'reply-' + data._raw.notification.pid + '@' + Emailer.settings.receive_domain;
        }
        async.waterfall([
            function(next) {
                if (data.fromUid) {
                    next(null, data.fromUid);
                } else if (data._raw.notification.pid) {
                    Posts.getPostField(data._raw.notification.pid, 'uid', next);
                } else {
                    next(null, false);
                }
            },
            function(uid, next) {
                if (uid === false) { return next(null, {}); }
                User.getSettings(uid, function(err, settings) {
                    if (err) {
                        return next(err);
                    }

                    if (settings.showemail) {<commit_msg>Fix Cannot read property 'pid' of undefined<commit_after>'use strict';
/* globals module, require */

var winston = module.parent.require('winston'),
    async = module.parent.require('async'),
    nconf = module.parent.require('nconf'),
    _ = module.parent.require('lodash'),

    Meta = module.parent.require('./meta'),
    User = module.parent.require('./user'),
    Posts = module.parent.require('./posts'),
    Topics = module.parent.require('./topics'),
    hostEmailer = module.parent.require('./emailer'),
    Privileges = module.parent.require('./privileges'),
    SocketHelpers = module.parent.require('./socket.io/helpers'),

    mandrill,
    Emailer = {
        settings: {}
    };

Emailer.init = function(data, callback) {

    var render = function(req, res) {
        res.render('admin/plugins/emailer-mandrill', {
            url: nconf.get('url'),
            base_url: nconf.get('base_url')
        });
    };

    Meta.settings.get('mandrill', function(err, settings) {
        Emailer.settings = Object.freeze(settings);
        Emailer.receiptRegex = new RegExp('^reply-([\\d]+)@' + Emailer.settings.receive_domain + '$');

        if (!err && settings && settings.apiKey) {
            mandrill = require('node-mandrill')(settings.apiKey || 'Replace Me');
        } else {
            winston.error('[plugins/emailer-mandrill] API key not set!');
        }

        data.router.get('/admin/plugins/emailer-mandrill', data.middleware.admin.buildHeader, render);
        data.router.get('/api/admin/plugins/emailer-mandrill', render);
        data.router.head('/emailer-mandrill/reply', function(req, res) {
            res.sendStatus(200);
        });
        data.router.post('/emailer-mandrill/reply', Emailer.receive);

        if (typeof callback === 'function') {
            callback();
        }
    });
};

Emailer.send = function(data, callback) {
    if (mandrill) {
        var headers = {};

        if (_.get(data, '_raw.notification.pid') && Emailer.settings.hasOwnProperty('receive_domain')) {
            headers['Reply-To'] = 'reply-' + data._raw.notification.pid + '@' + Emailer.settings.receive_domain;
        }
        async.waterfall([
            function(next) {
                if (data.fromUid) {
                    next(null, data.fromUid);
                } else if (_.get(data, '_raw.notification.pid')) {
                    Posts.getPostField(data._raw.notification.pid, 'uid', next);
                } else {
                    next(null, false);
                }
            },
            function(uid, next) {
                if (uid === false) { return next(null, {}); }
                User.getSettings(uid, function(err, settings) {
                    if (err) {
                        return next(err);
                    }

                    if (settings.showemail) {
{'fraction_non_alphanumeric': 0.1588835212023618, 'fraction_numerical': 0.0010735373054213634, 'mean_word_length': 2.0613362541073386, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7257798', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1568, 'n_words': 260}