<commit_before>// Generated by CoffeeScript 1.9.1
var User, checkBody, db, logger, nodemailer, sendEmail, user;

nodemailer = require("nodemailer");

logger = require('printit')({
  date: false,
  prefix: 'controllers:mails'
});

User = require('../lib/user');

user = new User();

db = require('../helpers/db_connect_helper').db_connect();

sendEmail = function(mailOptions, callback) {
  var transport;
  transport = nodemailer.createTransport("SMTP", {});
  return transport.sendMail(mailOptions, function(error, response) {
    transport.close();
    return callback(error, response);
  });
};

checkBody = function(body, attributes) {
  var attr, i, len, missingAttributes;
  missingAttributes = [];
  for (i = 0, len = attributes.length; i < len; i++) {
    attr = attributes[i];
    if (body[attr] == null) {
      missingAttributes.push(attr);
    }
  }
  return missingAttributes;
};

module.exports.send = function(req, res, next) {
  var attrs, body, err, mailOptions, missingAttributes;
  body = req.body;
  missingAttributes = checkBody(body, ['to', 'from', 'subject', 'content']);
  if (missingAttributes.length > 0) {
    attrs = missingAttributes.join(" ");
    err = new Error("Body has at least one missing attribute (" + attrs + ").");
    err.status = 400;
    return next(err);
  } else {
    mailOptions = {
      to: body.to,
      from: body.from,
      subject: body.subject,
      cc: body.cc,
      bcc: body.bcc,
      replyTo: body.replyTo,
      inReplyTo: body.inReplyTo,
      references: body.references,
      headers: body.headers,
      alternatives: body.alternatives,
      envelope: body.envelope,
      messageId: body.messageId,
      date: body.date,
      encoding: body.encoding,
      text: body.content,
      html: body.html || void 0
    };
    if (body.attachments != null) {
      mailOptions.attachments = body.attachments.map(function(attachment) {
        var newAttach;
        return newAttach = {
          filename: attachment.filename,
          contents: new Buffer(attachment.content.split(",")[1], 'base64')
        };
      });
    }
    return sendEmail(mailOptions, function(error, response) {
      if (error) {
        logger.info("[sendMail] Error : " + error);
        error.code = 'postfix_unavailable';
        error.status = 501;
        return next(error);
      } else {
        return res.send(200, response);
      }
    });
  }
};

module.exports.sendToUser = function(req, res, next) {
  var attrs, body, err, missingAttributes;
  body = req.body;
  missingAttributes = checkBody(body, ['from', 'subject', 'content']);
  if (missingAttributes.length > 0) {
    attrs = missingAttributes.join(" ");
    err = new Error("Body has at least one missing attribute (" + attrs + ").");
    err.status = 400;
    return next(err);
  } else {
    return user.getUser(function(err, user) {
      var mailOptions;
      if (err) {
        logger.info("[sendMailToUser] err: " + err);
        return next(err);
      } else {
        mailOptions = {
          to: user.email,
          from: body.from,
          subject: body.subject,
          text: body.content,
          html: body.html || void 0
        };
        if (body.attachments != null) {
          mailOptions.attachments = body.attachments;
        }
        return sendEmail(mailOptions, function(error, response) {
          if (error) {
            logger.info("[sendMail] Error : " + error);
            error.code = 'postfix_unavailable';
            error.status = 501;
            return next(error);
          } else {
            return res.send(200, response);
          }
        });
      }
    });
  }
};

module.exports.sendFromUser = function(req, res, next) {
  var attrs, body, err, missingAttributes;
  body = req.body;
  missingAttributes = checkBody(body, ['to', 'subject', 'content']);
  if (missingAttributes.length > 0) {
    attrs = missingAttributes.join(" ");
    err = new Error("Body has at least one missing attribute (" + attrs + ").");
    err.status = 400;
    return next(err);
  } else {
    return db.view('cozyinstance/all', function(err, instance) {
      return db.view('user/all', function(err, users) {
        var displayName, domain, mailOptions, ref, ref1, ref2;
        if ((instance != null ? (ref = instance[0]) != null ? ref.value.domain : void 0 : void 0) != null) {
          domain = instance[0].value.domain;
        } else {
          domain = 'your.cozy.io';
        }
        if (((users != null ? (ref1 = users[0]) != null ? ref1.value.public_name : void 0 : void 0) != null) && ((users != null ? (ref2 = users[0]) != null ? ref2.value.public_name : void 0 : void 0) != null) !== '') {
          displayName = users[0].value.public_name;
          displayName = displayName.toLowerCase().replace(' ', '-');
          displayName += "-";
        } else {
          displayName = '';
        }
        mailOptions = {
          to: body.to,
          from: displayName + "noreply@" + domain,
          subject: body.subject,
          text: body.content,
          html: body.html || void 0
        };
        if (body.attachments != null) {
          mailOptions.attachments = body.attachments;
        }
        return sendEmail(mailOptions, function(error, response) {
          if (error) {
            logger.info("[sendMail] Error : " + error);
            error.code = 'postfix_unavailable';
            error.status = 501;
            return next(error);
          } else {
            return res.send(200, response);
          }
        });
      });<commit_msg>built<commit_after>// Generated by CoffeeScript 1.9.1
var User, checkBody, db, logger, nodemailer, sendEmail, user;

nodemailer = require("nodemailer");

logger = require('printit')({
  date: false,
  prefix: 'controllers:mails'
});

User = require('../lib/user');

user = new User();

db = require('../helpers/db_connect_helper').db_connect();

sendEmail = function(mailOptions, callback) {
  var transport;
  transport = nodemailer.createTransport("SMTP", {});
  return transport.sendMail(mailOptions, function(error, response) {
    transport.close();
    return callback(error, response);
  });
};

checkBody = function(body, attributes) {
  var attr, i, len, missingAttributes;
  missingAttributes = [];
  for (i = 0, len = attributes.length; i < len; i++) {
    attr = attributes[i];
    if (body[attr] == null) {
      missingAttributes.push(attr);
    }
  }
  return missingAttributes;
};

module.exports.send = function(req, res, next) {
  var attrs, body, err, mailOptions, missingAttributes;
  body = req.body;
  missingAttributes = checkBody(body, ['to', 'from', 'subject', 'content']);
  if (missingAttributes.length > 0) {
    attrs = missingAttributes.join(" ");
    err = new Error("Body has at least one missing attribute (" + attrs + ").");
    err.status = 400;
    return next(err);
  } else {
    mailOptions = {
      to: body.to,
      from: body.from,
      subject: body.subject,
      cc: body.cc,
      bcc: body.bcc,
      replyTo: body.replyTo,
      inReplyTo: body.inReplyTo,
      references: body.references,
      headers: body.headers,
      alternatives: body.alternatives,
      envelope: body.envelope,
      messageId: body.messageId,
      date: body.date,
      encoding: body.encoding,
      text: body.content,
      html: body.html || void 0
    };
    if (body.attachments != null) {
      mailOptions.attachments = body.attachments.map(function(attachment) {
        var newAttach;
        return newAttach = {
          filename: attachment.filename,
          contents: new Buffer(attachment.content.split(",")[1], 'base64')
        };
      });
    }
    return sendEmail(mailOptions, function(error, response) {
      if (error) {
        logger.info("[sendMail] Error : " + error);
        error.code = 'postfix_unavailable';
        error.status = 501;
        return next(error);
      } else {
        return res.send(200, response);
      }
    });
  }
};

module.exports.sendToUser = function(req, res, next) {
  var attrs, body, err, missingAttributes;
  body = req.body;
  missingAttributes = checkBody(body, ['from', 'subject', 'content']);
  if (missingAttributes.length > 0) {
    attrs = missingAttributes.join(" ");
    err = new Error("Body has at least one missing attribute (" + attrs + ").");
    err.status = 400;
    return next(err);
  } else {
    return user.getUser(function(err, user) {
      var mailOptions;
      if (err) {
        logger.info("[sendMailToUser] err: " + err);
        return next(err);
      } else {
        mailOptions = {
          to: user.email,
          from: body.from,
          subject: body.subject,
          text: body.content,
          html: body.html || void 0
        };
        if (body.attachments != null) {
          mailOptions.attachments = body.attachments;
        }
        return sendEmail(mailOptions, function(error, response) {
          if (error) {
            logger.info("[sendMail] Error : " + error);
            error.code = 'postfix_unavailable';
            error.status = 501;
            return next(error);
          } else {
            return res.send(200, response);
          }
        });
      }
    });
  }
};

module.exports.sendFromUser = function(req, res, next) {
  var attrs, body, err, missingAttributes;
  body = req.body;
  missingAttributes = checkBody(body, ['to', 'subject', 'content']);
  if (missingAttributes.length > 0) {
    attrs = missingAttributes.join(" ");
    err = new Error("Body has at least one missing attribute (" + attrs + ").");
    err.status = 400;
    return next(err);
  } else {
    return db.view('cozyinstance/all', function(err, instance) {
      return db.view('user/all', function(err, users) {
        var displayName, domain, mailOptions, ref, ref1, ref2;
        if ((instance != null ? (ref = instance[0]) != null ? ref.value.domain : void 0 : void 0) != null) {
          domain = instance[0].value.domain;
          if (domain.indexOf('https://') !== -1) {
            domain = domain.substring(8, domain.length);
          }
        } else {
          domain = 'your.cozy.io';
        }
        if (((users != null ? (ref1 = users[0]) != null ? ref1.value.public_name : void 0 : void 0) != null) && ((users != null ? (ref2 = users[0]) != null ? ref2.value.public_name : void 0 : void 0) != null) !== '') {
          displayName = users[0].value.public_name;
          displayName = displayName.toLowerCase().replace(' ', '-');
          displayName += "-";
        } else {
          displayName = '';
        }
        mailOptions = {
          to: body.to,
          from: displayName + "noreply@" + domain,
          subject: body.subject,
          text: body.content,
          html: body.html || void 0
        };
        if (body.attachments != null) {
          mailOptions.attachments = body.attachments;
        }
        return sendEmail(mailOptions, function(error, response) {
          if (error) {
            logger.info("[sendMail] Error : " + error);
            error.code = 'postfix_unavailable';
            error.status = 501;
            return next(error);
          } else {
            return res.send(200, response);
          }
        });
      });
{'fraction_non_alphanumeric': 0.14569595437533417, 'fraction_numerical': 0.010336838353234717, 'mean_word_length': 2.337198929527208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '4311612', 'n_tokens_mistral': 3396, 'n_tokens_neox': 3335, 'n_words': 802}