86b03ed725f95854ef3bbd443103bebddecb7538
[motion2.git] / server / services / email / lib / email.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x2923=['isNil','stringify','emit','mailAccount:update','refresh','hasOwnProperty','service','provider','imapAccounts','email','handleImapBox','bind','handleImapEmail','end','handleImapEnd','error','disabled','connected','messages','total','firstName','from','then','catch','lastSync','sendMail','smtp','send','verifySmtp','Smtp','verify','verified','exports','lodash','bluebird','md5','redis','./imap','./smtp','../rpc/mailAccount','info','mailAccounts','prototype','name','key','active','status','messageStatus','Imap'];(function(_0x3e8be1,_0x5ae90c){var _0x5265cd=function(_0x6a3dc0){while(--_0x6a3dc0){_0x3e8be1['push'](_0x3e8be1['shift']());}};_0x5265cd(++_0x5ae90c);}(_0x2923,0x1a9));var _0x3292=function(_0x17d5a0,_0x4f2b28){_0x17d5a0=_0x17d5a0-0x0;var _0x2b31a3=_0x2923[_0x17d5a0];return _0x2b31a3;};'use strict';var _=require(_0x3292('0x0'));var moment=require('moment');var BPromise=require(_0x3292('0x1'));var md5=require(_0x3292('0x2'));var Redis=require('ioredis');var config=require('../../../config/environment');var logger=require('../../../config/logger')('email');config['redis']=_['defaults'](config['redis'],{'host':'localhost','port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x3292('0x3')]));var EmailImap=require(_0x3292('0x4'));var EmailSmtp=require(_0x3292('0x5'));var mailAccount=require(_0x3292('0x6'));var md5Accounts={};function EmailChannel(_0x2fee4d){logger[_0x3292('0x7')]('start\x20email\x20channel');this[_0x3292('0x8')]=_0x2fee4d['mailAccounts'];this['imapAccounts']={};this['smtp']=new EmailSmtp();}EmailChannel[_0x3292('0x9')]['emit']=function(_0x23fdc2){var _0x45eea5=this[_0x3292('0x8')][_0x23fdc2];var _0x249d52={'id':_0x45eea5['id'],'name':_0x45eea5[_0x3292('0xa')],'key':_0x45eea5[_0x3292('0xb')],'active':_0x45eea5[_0x3292('0xc')],'Imap':{'status':_0x45eea5['Imap'][_0x3292('0xd')],'messageStatus':_0x45eea5['Imap'][_0x3292('0xe')],'total':_0x45eea5[_0x3292('0xf')]['total']}};if(_[_0x3292('0x10')](md5Accounts[_0x23fdc2])){md5Accounts[_0x23fdc2]={};}var _0x13adf6=md5(JSON[_0x3292('0x11')](_0x249d52));if(md5Accounts[_0x23fdc2]['md5']!==_0x13adf6){md5Accounts[_0x23fdc2][_0x3292('0x2')]=_0x13adf6;io[_0x3292('0x12')](_0x3292('0x13'),_0x249d52);}};EmailChannel[_0x3292('0x9')][_0x3292('0x14')]=function(_0x1fff89){for(var _0x470a23 in _0x1fff89){if(_0x1fff89[_0x3292('0x15')](_0x470a23)){if(_0x1fff89[_0x470a23]['Imap']&&_0x1fff89[_0x470a23]['active']){var _0x4b66d0=_0x1fff89[_0x470a23][_0x3292('0xf')];if(!_0x4b66d0[_0x3292('0x16')]&&_0x1fff89[_0x470a23][_0x3292('0x17')]!=='custom')_0x4b66d0['service']=_0x1fff89[_0x470a23][_0x3292('0x17')];if(!this[_0x3292('0x18')]['hasOwnProperty'](_0x4b66d0['id'])){this[_0x3292('0x18')][_0x4b66d0['id']]=new EmailImap(_0x4b66d0,_0x1fff89[_0x470a23][_0x3292('0x19')]);this[_0x3292('0x18')][_0x4b66d0['id']]['on']('box',this[_0x3292('0x1a')][_0x3292('0x1b')](this,_0x470a23));this[_0x3292('0x18')][_0x4b66d0['id']]['on'](_0x3292('0x19'),this[_0x3292('0x1c')]['bind'](this,_0x470a23));this[_0x3292('0x18')][_0x4b66d0['id']]['on'](_0x3292('0x1d'),this[_0x3292('0x1e')]['bind'](this,_0x470a23));this['imapAccounts'][_0x4b66d0['id']]['on'](_0x3292('0x1f'),this['handleImapError']['bind'](this,_0x470a23));}}else{if(_[_0x3292('0x10')](this[_0x3292('0x8')][_0x470a23][_0x3292('0xf')])){this[_0x3292('0x8')][_0x470a23][_0x3292('0xf')]={};}this[_0x3292('0x8')][_0x470a23][_0x3292('0xf')]['status']=_0x3292('0x20');this[_0x3292('0x8')][_0x470a23][_0x3292('0xf')]['messageStatus']='';this[_0x3292('0x12')](_0x470a23);}}}};EmailChannel[_0x3292('0x9')]['handleImapBox']=function(_0x232c1e,_0x4e7564){if(this[_0x3292('0x8')][_0x232c1e]){this['mailAccounts'][_0x232c1e][_0x3292('0xf')][_0x3292('0xd')]=_0x3292('0x21');this[_0x3292('0x8')][_0x232c1e][_0x3292('0xf')]['messageStatus']=_0x4e7564[_0x3292('0xa')];if(_0x4e7564[_0x3292('0x22')]){this[_0x3292('0x8')][_0x232c1e]['Imap'][_0x3292('0x23')]=_0x4e7564[_0x3292('0x22')][_0x3292('0x23')];}return this[_0x3292('0x12')](_0x232c1e);}};EmailChannel[_0x3292('0x9')]['handleImapEmail']=function(_0x3d372b,_0x49dce8){logger[_0x3292('0x7')]('New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s',_0x49dce8[_0x3292('0x24')],_0x49dce8['lastName'],_0x49dce8[_0x3292('0x25')],_0x3d372b);if(this[_0x3292('0x8')][_0x3d372b]){return mailAccount['notify'](_0x3d372b,_0x49dce8)[_0x3292('0x26')](function(_0x2828c6){if(_0x2828c6){logger['info']('Mail\x20accepted\x20from\x20routing');}})[_0x3292('0x27')](function(_0x3fcd0c){logger[_0x3292('0x1f')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0x3292('0x11')](_0x3fcd0c));});}};EmailChannel[_0x3292('0x9')][_0x3292('0x1e')]=function(_0x1f378f){if(this[_0x3292('0x8')][_0x1f378f]&&this[_0x3292('0x8')][_0x1f378f][_0x3292('0xf')]){if(this[_0x3292('0x18')][this[_0x3292('0x8')][_0x1f378f]['Imap']['id']]){delete this[_0x3292('0x18')][this[_0x3292('0x8')][_0x1f378f]['Imap']['id']];}this['mailAccounts'][_0x1f378f][_0x3292('0xf')][_0x3292('0x28')]=moment();this[_0x3292('0x12')](_0x1f378f);}};EmailChannel[_0x3292('0x9')]['handleImapError']=function(_0x1c7737,_0x3fd46b){if(this[_0x3292('0x8')][_0x1c7737]){this[_0x3292('0x8')][_0x1c7737][_0x3292('0xf')][_0x3292('0xd')]='error';this['mailAccounts'][_0x1c7737][_0x3292('0xf')][_0x3292('0xe')]=_0x3fd46b;return this['emit'](_0x1c7737);}};EmailChannel[_0x3292('0x9')][_0x3292('0x29')]=function(_0x5ef6ac,_0x4a1243){return this[_0x3292('0x2a')][_0x3292('0x2b')](_0x5ef6ac,_0x4a1243);};EmailChannel[_0x3292('0x9')][_0x3292('0x2c')]=function(_0x12c469){var _0x20beaf=this;if(this['mailAccounts'][_0x12c469['id']]){if(_[_0x3292('0x10')](this['mailAccounts'][_0x12c469['id']]['Smtp'])){this['mailAccounts'][_0x12c469['id']][_0x3292('0x2d')]={};}this[_0x3292('0x8')][_0x12c469['id']]['Smtp'][_0x3292('0x28')]=moment();return new BPromise(function(_0x4a3be9,_0x393ec1){return _0x20beaf['smtp'][_0x3292('0x2e')](_0x12c469)[_0x3292('0x26')](function(_0x20c202){_0x20beaf[_0x3292('0x8')][_0x12c469['id']][_0x3292('0x2d')][_0x3292('0xd')]=_0x3292('0x2f');_0x20beaf[_0x3292('0x8')][_0x12c469['id']][_0x3292('0x2d')]['messageStatus']='';_0x20beaf['emit'](_0x12c469['id']);_0x4a3be9(_0x20c202);})['catch'](function(_0x16cdb1){_0x20beaf['mailAccounts'][_0x12c469['id']]['Smtp'][_0x3292('0xd')]=_0x3292('0x1f');_0x20beaf[_0x3292('0x8')][_0x12c469['id']][_0x3292('0x2d')][_0x3292('0xe')]=_0x16cdb1;_0x20beaf[_0x3292('0x12')](_0x12c469['id']);_0x393ec1(_0x16cdb1);});});}return this[_0x3292('0x2a')][_0x3292('0x2e')](_0x12c469);};module[_0x3292('0x30')]=EmailChannel;