Built motion from commit 44b1f254.|2.6.26
[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 _0xda0d=['bluebird','md5','ioredis','../../../config/environment','../../../config/logger','email','redis','defaults','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','key','active','status','messageStatus','Imap','isNil','stringify','mailAccount:update','refresh','hasOwnProperty','custom','service','handleImapBox','bind','handleImapEmail','end','handleImapEnd','handleImapError','disabled','connected','name','messages','total','lastName','from','notify','then','Mail\x20accepted\x20from\x20routing','catch','error','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','sendMail','send','verifySmtp','Smtp','verify','verified','lodash','moment'];(function(_0x122ef9,_0x451d2a){var _0x42fba0=function(_0x17c9d3){while(--_0x17c9d3){_0x122ef9['push'](_0x122ef9['shift']());}};_0x42fba0(++_0x451d2a);}(_0xda0d,0x125));var _0xdda0=function(_0x32da48,_0x485299){_0x32da48=_0x32da48-0x0;var _0x181fc0=_0xda0d[_0x32da48];return _0x181fc0;};'use strict';var _=require(_0xdda0('0x0'));var moment=require(_0xdda0('0x1'));var BPromise=require(_0xdda0('0x2'));var md5=require(_0xdda0('0x3'));var Redis=require(_0xdda0('0x4'));var config=require(_0xdda0('0x5'));var logger=require(_0xdda0('0x6'))(_0xdda0('0x7'));config[_0xdda0('0x8')]=_[_0xdda0('0x9')](config[_0xdda0('0x8')],{'host':'localhost','port':0x18eb});var io=require(_0xdda0('0xa'))(new Redis(config['redis']));var EmailImap=require(_0xdda0('0xb'));var EmailSmtp=require(_0xdda0('0xc'));var mailAccount=require(_0xdda0('0xd'));var md5Accounts={};function EmailChannel(_0x32fb1c){logger[_0xdda0('0xe')](_0xdda0('0xf'));this[_0xdda0('0x10')]=_0x32fb1c[_0xdda0('0x10')];this[_0xdda0('0x11')]={};this[_0xdda0('0x12')]=new EmailSmtp();}EmailChannel[_0xdda0('0x13')][_0xdda0('0x14')]=function(_0x346d3f){var _0x129e56=this[_0xdda0('0x10')][_0x346d3f];var _0x59445a={'id':_0x129e56['id'],'name':_0x129e56['name'],'key':_0x129e56[_0xdda0('0x15')],'active':_0x129e56[_0xdda0('0x16')],'Imap':{'status':_0x129e56['Imap'][_0xdda0('0x17')],'messageStatus':_0x129e56['Imap'][_0xdda0('0x18')],'total':_0x129e56[_0xdda0('0x19')]['total']}};if(_[_0xdda0('0x1a')](md5Accounts[_0x346d3f])){md5Accounts[_0x346d3f]={};}var _0x164121=md5(JSON[_0xdda0('0x1b')](_0x59445a));if(md5Accounts[_0x346d3f][_0xdda0('0x3')]!==_0x164121){md5Accounts[_0x346d3f]['md5']=_0x164121;io[_0xdda0('0x14')](_0xdda0('0x1c'),_0x59445a);}};EmailChannel[_0xdda0('0x13')][_0xdda0('0x1d')]=function(_0x48dbec){for(var _0x38cad0 in _0x48dbec){if(_0x48dbec[_0xdda0('0x1e')](_0x38cad0)){if(_0x48dbec[_0x38cad0][_0xdda0('0x19')]&&_0x48dbec[_0x38cad0]['active']){var _0x263deb=_0x48dbec[_0x38cad0][_0xdda0('0x19')];if(!_0x263deb['service']&&_0x48dbec[_0x38cad0]['provider']!==_0xdda0('0x1f'))_0x263deb[_0xdda0('0x20')]=_0x48dbec[_0x38cad0]['provider'];if(!this[_0xdda0('0x11')][_0xdda0('0x1e')](_0x263deb['id'])){this[_0xdda0('0x11')][_0x263deb['id']]=new EmailImap(_0x263deb,_0x48dbec[_0x38cad0]['email']);this[_0xdda0('0x11')][_0x263deb['id']]['on']('box',this[_0xdda0('0x21')][_0xdda0('0x22')](this,_0x38cad0));this[_0xdda0('0x11')][_0x263deb['id']]['on'](_0xdda0('0x7'),this[_0xdda0('0x23')][_0xdda0('0x22')](this,_0x38cad0));this['imapAccounts'][_0x263deb['id']]['on'](_0xdda0('0x24'),this[_0xdda0('0x25')][_0xdda0('0x22')](this,_0x38cad0));this[_0xdda0('0x11')][_0x263deb['id']]['on']('error',this[_0xdda0('0x26')][_0xdda0('0x22')](this,_0x38cad0));}}else{if(_[_0xdda0('0x1a')](this[_0xdda0('0x10')][_0x38cad0][_0xdda0('0x19')])){this[_0xdda0('0x10')][_0x38cad0][_0xdda0('0x19')]={};}this[_0xdda0('0x10')][_0x38cad0][_0xdda0('0x19')][_0xdda0('0x17')]=_0xdda0('0x27');this[_0xdda0('0x10')][_0x38cad0][_0xdda0('0x19')][_0xdda0('0x18')]='';this['emit'](_0x38cad0);}}}};EmailChannel[_0xdda0('0x13')]['handleImapBox']=function(_0x2c6aa6,_0x31d267){if(this['mailAccounts'][_0x2c6aa6]){this['mailAccounts'][_0x2c6aa6][_0xdda0('0x19')][_0xdda0('0x17')]=_0xdda0('0x28');this[_0xdda0('0x10')][_0x2c6aa6]['Imap']['messageStatus']=_0x31d267[_0xdda0('0x29')];if(_0x31d267[_0xdda0('0x2a')]){this[_0xdda0('0x10')][_0x2c6aa6][_0xdda0('0x19')][_0xdda0('0x2b')]=_0x31d267[_0xdda0('0x2a')][_0xdda0('0x2b')];}return this['emit'](_0x2c6aa6);}};EmailChannel['prototype']['handleImapEmail']=function(_0x53b93e,_0x280973){logger['info']('New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s',_0x280973['firstName'],_0x280973[_0xdda0('0x2c')],_0x280973[_0xdda0('0x2d')],_0x53b93e);if(this[_0xdda0('0x10')][_0x53b93e]){return mailAccount[_0xdda0('0x2e')](_0x53b93e,_0x280973)[_0xdda0('0x2f')](function(_0x37e2c1){if(_0x37e2c1){logger[_0xdda0('0xe')](_0xdda0('0x30'));}})[_0xdda0('0x31')](function(_0x34dda6){logger[_0xdda0('0x32')](_0xdda0('0x33'),JSON['stringify'](_0x34dda6));});}};EmailChannel[_0xdda0('0x13')]['handleImapEnd']=function(_0x129a22){if(this[_0xdda0('0x10')][_0x129a22]&&this[_0xdda0('0x10')][_0x129a22][_0xdda0('0x19')]){if(this['imapAccounts'][this[_0xdda0('0x10')][_0x129a22][_0xdda0('0x19')]['id']]){delete this['imapAccounts'][this[_0xdda0('0x10')][_0x129a22][_0xdda0('0x19')]['id']];}this[_0xdda0('0x10')][_0x129a22][_0xdda0('0x19')][_0xdda0('0x34')]=moment();this[_0xdda0('0x14')](_0x129a22);}};EmailChannel['prototype'][_0xdda0('0x26')]=function(_0x7aba5f,_0x3c611e){if(this[_0xdda0('0x10')][_0x7aba5f]){this[_0xdda0('0x10')][_0x7aba5f][_0xdda0('0x19')][_0xdda0('0x17')]=_0xdda0('0x32');this[_0xdda0('0x10')][_0x7aba5f][_0xdda0('0x19')][_0xdda0('0x18')]=_0x3c611e;return this['emit'](_0x7aba5f);}};EmailChannel[_0xdda0('0x13')][_0xdda0('0x35')]=function(_0xb488ef,_0x323209){return this[_0xdda0('0x12')][_0xdda0('0x36')](_0xb488ef,_0x323209);};EmailChannel[_0xdda0('0x13')][_0xdda0('0x37')]=function(_0x3bc1fd){var _0x5eb1df=this;if(this[_0xdda0('0x10')][_0x3bc1fd['id']]){if(_[_0xdda0('0x1a')](this[_0xdda0('0x10')][_0x3bc1fd['id']][_0xdda0('0x38')])){this[_0xdda0('0x10')][_0x3bc1fd['id']][_0xdda0('0x38')]={};}this[_0xdda0('0x10')][_0x3bc1fd['id']][_0xdda0('0x38')][_0xdda0('0x34')]=moment();return new BPromise(function(_0x231b7f,_0x4f1a49){return _0x5eb1df[_0xdda0('0x12')][_0xdda0('0x39')](_0x3bc1fd)[_0xdda0('0x2f')](function(_0x401c6d){_0x5eb1df[_0xdda0('0x10')][_0x3bc1fd['id']]['Smtp'][_0xdda0('0x17')]=_0xdda0('0x3a');_0x5eb1df[_0xdda0('0x10')][_0x3bc1fd['id']][_0xdda0('0x38')][_0xdda0('0x18')]='';_0x5eb1df['emit'](_0x3bc1fd['id']);_0x231b7f(_0x401c6d);})[_0xdda0('0x31')](function(_0x3bee40){_0x5eb1df['mailAccounts'][_0x3bc1fd['id']][_0xdda0('0x38')]['status']='error';_0x5eb1df['mailAccounts'][_0x3bc1fd['id']]['Smtp'][_0xdda0('0x18')]=_0x3bee40;_0x5eb1df[_0xdda0('0x14')](_0x3bc1fd['id']);_0x4f1a49(_0x3bee40);});});}return this[_0xdda0('0x12')][_0xdda0('0x39')](_0x3bc1fd);};module['exports']=EmailChannel;