-'use strict';
-
-var _ = require('lodash');
-var path = require('path');
-var config = require('../environment');
-var ImapListener = require("mail-listener2");
-var MailServerIn = require('../../models').MailServerIn;
-var MailMessage = require('../../models').MailMessage;
-var MailRoom = require('../../models').MailRoom;
-var Contact = require('../../models').Contact;
-var ContactEmail = require('../../models').ContactEmail;
-var sequelize = require('../../models').sequelize;
-
-// Public
-module.exports = {
- create: function (doc) {
-
- var imap;
-
- function onUpdate(doc) {
- if (!doc.changed('state') && !doc.changed('source') && doc.username === imap.imap._config.user) {
- console.log("MAIL - Account " + doc.username + " IMAP UPDATED");
- imap.stop();
- onSave(doc);
- }
- }
-
- function onRemove(doc) {
- if (doc.username === imap.imap._config.user) {
- console.log("MAIL - Account " + doc.username + " IMAP DESTROY");
- imap.stop();
- imap = null;
- }
- }
-
- function onSave(doc) {
-
- if (imap) {
- imap.stop(doc);
- imap = null;
- }
-
- imap = new ImapListener({
- username: doc.username,
- password: doc.password,
- host: doc.host,
- port: doc.port,
- tls: doc.ssl,
- tlsOptions: {
- rejectUnauthorized: false
- },
- mailbox: doc.mailbox, // mailbox to monitor
- searchFilter: [doc.filter], // the search filter being used after an IDLE notification has been retrieved
- markSeen: true, // all fetched email willbe marked as seen and not fetched next time
- fetchUnreadOnStart: true, // use it only if you want to get all unread email on lib start. Default is `false`,,
- mailParserOptions: {
- streamAttachments: false
- }, // options to be passed to mailParser lib.
- attachments: true, // download attachments as they are encountered to the project directory
- attachmentOptions: {
- directory: path.join(config.root, 'server', 'files', 'attachments', '/')
- } // specify a download directory for attachments
- });
-
- imap.start();
-
- imap.on("server:connected", function () {
- console.log("MAIL - Account " + doc.username + " IMAP CONNECTED");
- // SETUP CONNECTED STATUS
- return MailServerIn
- .findById(doc.id)
- .then(function (msi) {
- return msi
- .update({
- state: 'CONNECTED'
- });
- })
- .catch(function (err) {
- console.error(err);
- });
- });
-
- imap.on("server:disconnected", function () {
- console.log("MAIL - Account " + doc.MailAccountId + " IMAP DISCONNECTED");
- MailServerIn
- .findById(doc.id)
- .then(function (msi) {
- msi
- .updateAttributes({
- state: 'DISCONNECTED'
- });
- })
- .catch(function (err) {
- console.error(err);
- });
- });
-
- imap.on("error", function (err) {
- console.log("MAIL - Account " + doc.MailAccountId + " IMAP ERROR", err);
- MailServerIn
- .findById(doc.id)
- .then(function (msi) {
- msi
- .updateAttributes({
- state: 'ERROR',
- source: err.source
- });
- })
- .catch(function (err) {
- console.error(err);
- });
- });
-
- imap.on("mail", function (msg, seqno, attributes) {
- var _mRoom;
- var _mMessage;
- var _mFrom, _mTo, _mCc, _mBcc;
-
- function createMailMessage(mailRoom) {
- _mRoom = mailRoom;
-
- return MailMessage
- .create({
- messageId: msg.messageId,
- subject: msg.subject,
- from: _.pluck(msg.from, 'address').join(';'),
- to: msg.to ? _.pluck(msg.to, 'address').join(';') : null,
- cc: msg.cc ? _.pluck(msg.cc, 'address').join(';') : null,
- bcc: msg.cc ? _.pluck(msg.bcc, 'address').join(';') : null,
- status: 'RECEIVED',
- html: msg.html || null,
- text: msg.text || null,
- MailAttachments: msg.attachments || [],
- MailRoomId: mailRoom.id,
- });
- }
-
- function createFrom(mailMessage) {
- _mMessage = mailMessage;
-
- return ContactEmail
- .findOrCreate({
- where: {
- email: _mMessage.from
- },
- defaults: {
- email: _mMessage.from
- }
- });
- }
-
- function createTo(mFrom, created) {
- var promises = [];
- var tos = _mMessage.to ? _.pluck(msg.to, 'address') : [];
- _mFrom = mFrom;
-
- tos.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- }
- }));
- });
-
- return promises;
- }
-
- function createCc(mTo) {
- var promises = [];
- var ccs = _mMessage.cc ? _.pluck(msg.cc, 'address') : [];
- _mTo = _.map(mTo, function (elm) {
- return elm[0];
- });
-
- ccs.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- }
- }));
- });
-
- return promises;
- }
-
- function createBcc(mCc) {
- var promises = [];
- var bccs = _mMessage.bcc ? _.pluck(msg.bcc, 'address') : [];
- _mCc = _.map(mCc, function (elm) {
- return elm[0];
- });
-
- bccs.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- }
- }));
- });
-
- return promises;
- }
-
- function setBcc(mBcc) {
- _mBcc = _.map(mBcc, function (elm) {
- return elm[0];
- });
-
- return;
- }
-
- return sequelize.transaction(function (t) {
- if (msg.inReplyTo) {
- console.log('msg', msg);
- return MailMessage
- .findOne({
- where: {
- messageId: msg.inReplyTo[0]
- },
- include: [{
- all: true
- }]
- })
- .then(function (mailMessageParent) {
- return mailMessageParent
- .getMailRoom();
- })
- .then(function (mailRoom) {
- _mRoom = mailRoom;
-
- return MailMessage
- .create({
- messageId: msg.messageId,
- subject: msg.subject,
- from: _.pluck(msg.from, 'address').join(';'),
- to: msg.to ? _.pluck(msg.to, 'address').join(';') : null,
- cc: msg.cc ? _.pluck(msg.cc, 'address').join(';') : null,
- bcc: msg.cc ? _.pluck(msg.bcc, 'address').join(';') : null,
- status: 'RECEIVED',
- html: msg.html || null,
- text: msg.text || null,
- MailAttachments: msg.attachments || [],
- MailRoomId: mailRoom.id,
- }, {
- include: [{
- all: true
- }],
- transaction: t
- });
- })
- .then(function (mailMessage) {
- _mMessage = mailMessage;
-
- return ContactEmail
- .findOrCreate({
- where: {
- email: _mMessage.from
- },
- defaults: {
- email: _mMessage.from
- },
- transaction: t
- });
- })
- .spread(function (mFrom, created) {
- var promises = [];
- var tos = _mMessage.to ? _.pluck(msg.to, 'address') : [];
- _mFrom = mFrom;
-
- tos.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- },
- transaction: t
- }));
- });
-
- return promises;
- })
- .all()
- .then(function (mTo) {
- var promises = [];
- var ccs = _mMessage.cc ? _.pluck(msg.cc, 'address') : [];
- _mTo = _.map(mTo, function (elm) {
- return elm[0];
- });
-
- ccs.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- },
- transaction: t
- }));
- });
-
- return promises;
- })
- .all()
- .then(function (mCc) {
- var promises = [];
- var bccs = _mMessage.bcc ? _.pluck(msg.bcc, 'address') : [];
- _mCc = _.map(mCc, function (elm) {
- return elm[0];
- });
-
- bccs.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- },
- transaction: t
- }));
- });
-
- return promises;
- })
- .all()
- .then(function (mBcc) {
- _mBcc = _.map(mBcc, function (elm) {
- return elm[0];
- });
-
- return;
- })
- .then(function () {
- return _mMessage
- .setFrom(_mFrom, {
- transaction: t
- });
- })
- .then(function () {
- return _mMessage
- .setTo(_mTo, {
- transaction: t
- });
- })
- .then(function () {
- return _mMessage
- .setCc(_mCc, {
- transaction: t
- });
- })
- .then(function () {
- return _mMessage
- .setBcc(_mBcc, {
- transaction: t
- });
- });
-
- } else {
- return MailRoom
- .create({
- roomId: msg.messageId,
- subject: msg.subject,
- from: _.pluck(msg.from, 'address').join(';'),
- MailAccountId: doc.MailAccountId
- }, {
- transaction: t
- })
- .then(function (mailRoom) {
- _mRoom = mailRoom;
-
- return MailMessage
- .create({
- messageId: msg.messageId,
- subject: msg.subject,
- from: _.pluck(msg.from, 'address').join(';'),
- to: msg.to ? _.pluck(msg.to, 'address').join(';') : null,
- cc: msg.cc ? _.pluck(msg.cc, 'address').join(';') : null,
- bcc: msg.cc ? _.pluck(msg.bcc, 'address').join(';') : null,
- status: 'RECEIVED',
- html: msg.html || null,
- text: msg.text || null,
- MailAttachments: msg.attachments || [],
- MailRoomId: mailRoom.id,
- }, {
- include: [{
- all: true
- }],
- transaction: t
- });
- })
- .then(function (mailMessage) {
- _mMessage = mailMessage;
-
- return ContactEmail
- .findOrCreate({
- where: {
- email: _mMessage.from
- },
- defaults: {
- email: _mMessage.from
- },
- transaction: t
- });
- })
- .spread(function (mFrom, created) {
- var promises = [];
- var tos = _mMessage.to ? _.pluck(msg.to, 'address') : [];
- _mFrom = mFrom;
-
- tos.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- },
- transaction: t
- }));
- });
-
- return promises;
- })
- .all()
- .then(function (mTo) {
- var promises = [];
- var ccs = _mMessage.cc ? _.pluck(msg.cc, 'address') : [];
- _mTo = _.map(mTo, function (elm) {
- return elm[0];
- });
-
- ccs.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCrredieate({
- where: {
- email: email
- },
- defaults: {
- email: email
- },
- transaction: t
- }));
- });
-
- return promises;
- })
- .all()
- .then(function (mCc) {
- var promises = [];
- var bccs = _mMessage.bcc ? _.pluck(msg.bcc, 'address') : [];
- _mCc = _.map(mCc, function (elm) {
- return elm[0];
- });
-
- bccs.forEach(function (email) {
- promises.push(ContactEmail
- .findOrCreate({
- where: {
- email: email
- },
- defaults: {
- email: email
- },
- transaction: t
- }));
- });
-
- return promises;
- })
- .all()
- .then(function (mBcc) {
- _mBcc = _.map(mBcc, function (elm) {
- return elm[0];
- });
-
- return;
- })
- .then(function () {
- return _mMessage
- .setFrom(_mFrom, {
- transaction: t
- });
- })
- .then(function () {
- return _mMessage
- .setTo(_mTo, {
- transaction: t
- });
- })
- .then(function () {
- return _mMessage
- .setCc(_mCc, {
- transaction: t
- });
- })
- .then(function () {
- return _mMessage
- .setBcc(_mBcc, {
- transaction: t
- });
- });
- }
- }).then(function (result) {
- // Transaction has been committed
- // result is whatever the result of the promise chain returned to the transaction callback
- console.log(result);
- }).catch(function (err) {
- // Transaction has been rolled back
- // err is whatever rejected the promise chain returned to the transaction callback
- console.error(err);
- });
- });
-
- imap.on("attachment", function (attachment) {
- console.log(attachment.path);
- });
- }
-
- onSave(doc);
-
- MailServerIn.afterCreate(function (doc) {
- onSave(doc);
- });
- // HANDLE ACCOUNT UPDATE/DELETE
- MailServerIn.afterUpdate(function (doc) {
- onUpdate(doc);
- });
- MailServerIn.afterDestroy(function (doc) {
- onRemove(doc);
- });
- }
-};
+var _0x80c6=["\x75\x73\x65\x20\x73\x74\x72\x69\x63\x74","\x75\x74\x69\x6C","\x70\x61\x74\x68","\x6C\x6F\x64\x61\x73\x68","\x62\x6C\x75\x65\x62\x69\x72\x64","\x6D\x61\x69\x6C\x2D\x6C\x69\x73\x74\x65\x6E\x65\x72\x32","\x77\x72\x69\x74\x65\x46\x69\x6C\x65","\x66\x73","\x70\x72\x6F\x6D\x69\x73\x69\x66\x79","\x2E\x2E\x2F\x65\x6E\x76\x69\x72\x6F\x6E\x6D\x65\x6E\x74","\x4D\x61\x69\x6C\x53\x65\x72\x76\x65\x72\x49\x6E","\x2E\x2E\x2F\x2E\x2E\x2F\x6D\x6F\x64\x65\x6C\x73","\x4D\x61\x69\x6C\x52\x6F\x6F\x6D","\x4D\x61\x69\x6C\x4D\x65\x73\x73\x61\x67\x65","\x4D\x61\x69\x6C\x41\x63\x63\x6F\x75\x6E\x74","\x4D\x61\x69\x6C\x41\x74\x74\x61\x63\x68\x6D\x65\x6E\x74","\x65\x72\x72\x6F\x72","\x6F\x6E","\x63\x6C\x6F\x73\x65","\x49\x4D\x41\x50\x20\x53\x45\x52\x56\x45\x52\x20\x25\x73\x20\x45\x58\x49\x54\x45\x44\x20\x57\x49\x54\x48\x20\x43\x4F\x44\x45\x20\x25\x73\x2C\x20\x50\x52\x4F\x43\x45\x53\x53\x20\x49\x44\x3A\x20\x25\x73\x20","\x61\x72\x67\x76","\x70\x69\x64","\x66\x6F\x72\x6D\x61\x74","\x6C\x6F\x67","\x49\x4D\x41\x50\x20\x53\x45\x52\x56\x45\x52\x20\x25\x73\x20\x53\x54\x41\x52\x54\x49\x4E\x47\x2C\x20\x50\x52\x4F\x43\x45\x53\x53\x20\x49\x44\x3A\x20\x25\x73\x20","\x73\x74\x61\x74\x65","\x43\x4F\x4E\x4E\x45\x43\x54\x49\x4E\x47","\x73\x65\x6E\x64","\x6D\x61\x69\x6C","\x49\x4D\x41\x50\x20\x53\x45\x52\x56\x45\x52\x20\x25\x73\x20\x4E\x45\x57\x20\x4D\x53\x47\x20\x46\x52\x4F\x4D\x20\x25\x73\x20","\x3B","\x6A\x6F\x69\x6E","\x66\x72\x6F\x6D","\x61\x64\x64\x72\x65\x73\x73","\x6D\x61\x70","\x74\x79\x70\x65","\x6D\x73\x67","\x6D\x61\x69\x6C\x53\x65\x72\x76\x65\x72\x49\x6E\x49\x64","\x6D\x61\x69\x6C\x41\x63\x63\x6F\x75\x6E\x74\x49\x64","\x49\x4D\x41\x50\x20\x53\x45\x52\x56\x45\x52\x20\x25\x73\x20\x45\x52\x52\x52\x4F\x52\x2C\x20\x50\x52\x4F\x43\x45\x53\x53\x20\x49\x44\x3A\x20\x25\x73\x20","\x75\x73\x65\x72\x6E\x61\x6D\x65","\x45\x52\x52\x4F\x52","\x73\x74\x72\x69\x6E\x67\x69\x66\x79","\x73\x65\x72\x76\x65\x72\x3A\x64\x69\x73\x63\x6F\x6E\x6E\x65\x63\x74\x65\x64","\x49\x4D\x41\x50\x20\x53\x45\x52\x56\x45\x52\x20\x25\x73\x20\x44\x49\x53\x43\x4F\x4E\x4E\x45\x43\x54\x45\x44\x2C\x20\x50\x52\x4F\x43\x45\x53\x53\x20\x49\x44\x3A\x20\x25\x73\x20","\x73\x74\x6F\x70","\x44\x49\x53\x43\x4F\x4E\x4E\x45\x43\x54\x45\x44","\x73\x65\x72\x76\x65\x72\x3A\x63\x6F\x6E\x6E\x65\x63\x74\x65\x64","\x49\x4D\x41\x50\x20\x53\x45\x52\x56\x45\x52\x20\x25\x73\x20\x43\x4F\x4E\x4E\x45\x43\x54\x45\x44\x2C\x20\x50\x52\x4F\x43\x45\x53\x53\x20\x49\x44\x3A\x20\x25\x73\x20","\x43\x4F\x4E\x4E\x45\x43\x54\x45\x44","\x70\x61\x73\x73\x77\x6F\x72\x64","\x68\x6F\x73\x74","\x70\x6F\x72\x74","\x73\x73\x6C","\x6D\x61\x69\x6C\x62\x6F\x78","\x66\x69\x6C\x74\x65\x72","\x63\x6F\x6E\x6E\x54\x69\x6D\x65\x6F\x75\x74","\x61\x75\x74\x68\x54\x69\x6D\x65\x6F\x75\x74","\x6B\x65\x65\x70\x61\x6C\x69\x76\x65","\x69\x64","\x4D\x61\x69\x6C\x41\x63\x63\x6F\x75\x6E\x74\x49\x64","\x73\x74\x61\x72\x74","\x63\x61\x74\x63\x68","\x74\x68\x65\x6E","\x66\x69\x6E\x64\x42\x79\x49\x64"];_0x80c6[0];var util=require(_0x80c6[1]);var path=require(_0x80c6[2]);var _=require(_0x80c6[3]);var Promise=require(_0x80c6[4]);var ImapListener=require(_0x80c6[5]);var writeFile=Promise[_0x80c6[8]](require(_0x80c6[7])[_0x80c6[6]]);var config=require(_0x80c6[9]);var Server=require(_0x80c6[11])[_0x80c6[10]];var Room=require(_0x80c6[11])[_0x80c6[12]];var Message=require(_0x80c6[11])[_0x80c6[13]];var Account=require(_0x80c6[11])[_0x80c6[14]];var Attachment=require(_0x80c6[11])[_0x80c6[15]];var imap;var timeout;process[_0x80c6[17]](_0x80c6[18],function(_0xf180x10){console[_0x80c6[23]](util[_0x80c6[22]](_0x80c6[19],process[_0x80c6[20]][3],_0xf180x10,process[_0x80c6[21]]))})[_0x80c6[17]](_0x80c6[16],function(_0xf180xf){console[_0x80c6[16]](_0xf180xf)});console[_0x80c6[23]](util[_0x80c6[22]](_0x80c6[24],process[_0x80c6[20]][3],process[_0x80c6[21]]));process[_0x80c6[27]]({type:_0x80c6[25],state:_0x80c6[26]});function onSave(_0xf180x12){imap= new ImapListener({username:_0xf180x12[_0x80c6[40]],password:_0xf180x12[_0x80c6[50]],host:_0xf180x12[_0x80c6[51]],port:_0xf180x12[_0x80c6[52]],tls:_0xf180x12[_0x80c6[53]],tlsOptions:{rejectUnauthorized:false},mailbox:_0xf180x12[_0x80c6[54]],searchFilter:[_0xf180x12[_0x80c6[55]]],markSeen:true,fetchUnreadOnStart:true,connTimeout:_0xf180x12[_0x80c6[56]],authTimeout:_0xf180x12[_0x80c6[57]],keepalive:_0xf180x12[_0x80c6[58]]})[_0x80c6[17]](_0x80c6[47],function(){console[_0x80c6[23]](util[_0x80c6[22]](_0x80c6[48],_0xf180x12[_0x80c6[40]],process[_0x80c6[21]]));process[_0x80c6[27]]({type:_0x80c6[25],state:_0x80c6[49]})})[_0x80c6[17]](_0x80c6[43],function(){console[_0x80c6[23]](util[_0x80c6[22]](_0x80c6[44],_0xf180x12[_0x80c6[40]],process[_0x80c6[21]]));imap[_0x80c6[45]]();imap=null;process[_0x80c6[27]]({type:_0x80c6[25],state:_0x80c6[46]})})[_0x80c6[17]](_0x80c6[16],function(_0xf180xf){console[_0x80c6[23]](util[_0x80c6[22]](_0x80c6[39],_0xf180x12[_0x80c6[40]],process[_0x80c6[21]]));process[_0x80c6[27]]({type:_0x80c6[25],state:_0x80c6[41],source:JSON[_0x80c6[42]](_0xf180xf)})})[_0x80c6[17]](_0x80c6[28],function(_0xf180x13,_0xf180x14,_0xf180x15){console[_0x80c6[23]](util[_0x80c6[22]](_0x80c6[29],process[_0x80c6[20]][3],_[_0x80c6[34]](_0xf180x13[_0x80c6[32]],_0x80c6[33])[_0x80c6[31]](_0x80c6[30])));_0xf180x13[_0x80c6[35]]=_0x80c6[36];_0xf180x13[_0x80c6[37]]=imap[_0x80c6[37]];_0xf180x13[_0x80c6[38]]=imap[_0x80c6[38]];process[_0x80c6[27]](_0xf180x13)});imap[_0x80c6[37]]=_0xf180x12[_0x80c6[59]];imap[_0x80c6[38]]=_0xf180x12[_0x80c6[60]];imap[_0x80c6[61]]()}Server[_0x80c6[64]](process[_0x80c6[20]][2])[_0x80c6[63]](function(_0xf180x12){onSave(_0xf180x12)})[_0x80c6[62]](function(_0xf180xf){console[_0x80c6[16]](_0xf180xf)})
\ No newline at end of file