Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const models = require('../models');
- const _ = require('lodash');
- const Promise = require('bluebird');
- const Imap = require('imap');
- const MailParser = require('mailparser').MailParser;
- const async = require('async');
- var getEmailAccounts = function () {
- var criteria = {
- where: {
- removed: false,
- imapUser: {$ne: null},
- imapPassword: {$ne: null},
- imapHost: {$ne: null},
- imapPort: {$ne: null},
- imapTls: {$ne: null}
- }
- };
- return models.emailAccounts.findAll(criteria);
- };
- var getMailsFromAccount = function (emailAccount) {
- return new Promise(function (resolve, reject) {
- var results = [];
- var userData = {
- user: emailAccount.imapUser,
- password: emailAccount.imapPassword,
- host: emailAccount.imapHost,
- port: emailAccount.imapPort,
- tls: emailAccount.imapTls
- };
- var imap = new Imap(userData);
- function openInbox(cb) {
- imap.openBox('INBOX', true, cb);
- }
- imap.once('error', function (err) {
- sails.log.error('Connection error: ' + err.message + ' For: ' + JSON.stringify(userData));
- return reject(err.message);
- });
- imap.once('ready', function () {
- openInbox(function (err) {
- if (err) reject(err);
- imap.search(['ALL', ['SINCE', '20 Sep, 2016']], function(err, messages) {
- var f = imap.fetch(messages, {bodies: ''});
- f.on('message', function (msg) {
- var parser = new MailParser();
- parser.on('end', function (msg) {
- msg.agencyId = emailAccount.agencyId;
- results.push(msg);
- if (results.length == messages.length) {
- imap.end();
- }
- });
- msg.on('body', function (stream) {
- stream.on('data', function (chunk) {
- parser.write(chunk.toString('utf8'));
- });
- });
- msg.once('end', function () {
- parser.end();
- });
- });
- f.once('error', function (err) {
- console.log('Fetch error: ' + err);
- });
- });
- });
- });
- imap.once('error', function (error) {
- resolve(error);
- });
- imap.once('end', function () {
- resolve(results);
- });
- imap.connect();
- });
- };
- var createEventIfNotExists = function (email) {
- return models.events.findOne({
- where: {
- removed: false,
- messageId: email.messageId,
- agencyId: email.agencyId
- }
- })
- .then(function(event) {
- if (event) {
- return {
- email: email,
- event: event
- };
- }
- return models.events.create({
- eventTypeId: 1,
- content: email.text,
- title: email.subject,
- date: email.date,
- agencyId: email.agencyId,
- messageId: email.messageId
- })
- .then(function(event) {
- return {
- email: email,
- event: event
- };
- });
- });
- };
- var createContactIfNotExists = function(agencyId, emailAddress) {
- return models.contacts.findOne({
- where: {
- removed: false,
- agencyId: agencyId,
- email: emailAddress
- }
- })
- .then(function (contact) {
- if (contact) {
- return contact;
- }
- return models.contacts.create({
- agencyId: agencyId,
- email: emailAddress
- });
- });
- };
- var attachContactToEvent = function(typeId, contactId, eventId) {
- return models.eventForContacts.findOne({
- where: {
- contactId: contactId,
- eventId: eventId
- }
- })
- .then(function(result) {
- if (result) {
- return result;
- }
- return models.eventForContacts.create({
- eventForContactTypeId: typeId,
- contactId: contactId,
- eventId: eventId
- });
- });
- };
- var attachContactsToEvent = function(data) {
- var email = data.email;
- var event = data.event;
- var fields = ['from', 'cc', 'bcc'];
- return _.map(fields, (field) => {
- return _.map(email[field], function (item) {
- createContactIfNotExists(event.agencyId, item.address)
- .then(function (contact) {
- return attachContactToEvent(
- EventForContactType[field.toUpperCase()],
- contact.id,
- event.id
- );
- });
- });
- });
- };
- var parseEmail = function (email) {
- return createEventIfNotExists(email)
- .then(attachContactsToEvent);
- };
- var processEmailAccount = function (emailAccount) {
- return getMailsFromAccount(emailAccount)
- .then(function (emails) {
- return Promise.all(_.map(emails, parseEmail));
- })
- .catch(function (err) {
- sails.log.error(err);
- });
- };
- var getMails = function () {
- return getEmailAccounts()
- .then(function (emailAccounts) {
- return new Promise(function(resolve) {
- async.eachLimit(emailAccounts, sails.config.app.emailParseLimit, processEmailAccount, function() {
- resolve();
- });
- });
- })
- .catch(function (err) {
- sails.log.error(err);
- });
- };
- module.exports = {
- getMails: getMails
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement