Built motion from commit 3d3e98f2.|2.6.29
[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 _0xa7e8=['hasOwnProperty','provider','custom','box','handleImapBox','bind','handleImapEnd','error','handleImapError','disabled','connected','name','messages','info','firstName','lastName','from','notify','Mail\x20accepted\x20from\x20routing','sendMail','send','verifySmtp','Smtp','verify','verified','exports','lodash','md5','ioredis','../../../config/environment','../../../config/logger','email','redis','localhost','socket.io-emitter','../rpc/mailAccount','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','key','active','Imap','status','messageStatus','total','isNil','stringify','emit','refresh'];(function(_0x181f86,_0x21f4b9){var _0x80bc75=function(_0x540aa5){while(--_0x540aa5){_0x181f86['push'](_0x181f86['shift']());}};_0x80bc75(++_0x21f4b9);}(_0xa7e8,0x14c));var _0x8a7e=function(_0x3479f1,_0x2b66d8){_0x3479f1=_0x3479f1-0x0;var _0x39b73e=_0xa7e8[_0x3479f1];return _0x39b73e;};'use strict';var _=require(_0x8a7e('0x0'));var moment=require('moment');var BPromise=require('bluebird');var md5=require(_0x8a7e('0x1'));var Redis=require(_0x8a7e('0x2'));var config=require(_0x8a7e('0x3'));var logger=require(_0x8a7e('0x4'))(_0x8a7e('0x5'));config[_0x8a7e('0x6')]=_['defaults'](config['redis'],{'host':_0x8a7e('0x7'),'port':0x18eb});var io=require(_0x8a7e('0x8'))(new Redis(config[_0x8a7e('0x6')]));var EmailImap=require('./imap');var EmailSmtp=require('./smtp');var mailAccount=require(_0x8a7e('0x9'));var md5Accounts={};function EmailChannel(_0x45ef5f){logger['info'](_0x8a7e('0xa'));this['mailAccounts']=_0x45ef5f[_0x8a7e('0xb')];this[_0x8a7e('0xc')]={};this[_0x8a7e('0xd')]=new EmailSmtp();}EmailChannel[_0x8a7e('0xe')]['emit']=function(_0x362f65){var _0x3dbddc=this[_0x8a7e('0xb')][_0x362f65];var _0x3345be={'id':_0x3dbddc['id'],'name':_0x3dbddc['name'],'key':_0x3dbddc[_0x8a7e('0xf')],'active':_0x3dbddc[_0x8a7e('0x10')],'Imap':{'status':_0x3dbddc[_0x8a7e('0x11')][_0x8a7e('0x12')],'messageStatus':_0x3dbddc[_0x8a7e('0x11')][_0x8a7e('0x13')],'total':_0x3dbddc[_0x8a7e('0x11')][_0x8a7e('0x14')]}};if(_[_0x8a7e('0x15')](md5Accounts[_0x362f65])){md5Accounts[_0x362f65]={};}var _0x3a88f7=md5(JSON[_0x8a7e('0x16')](_0x3345be));if(md5Accounts[_0x362f65][_0x8a7e('0x1')]!==_0x3a88f7){md5Accounts[_0x362f65][_0x8a7e('0x1')]=_0x3a88f7;io[_0x8a7e('0x17')]('mailAccount:update',_0x3345be);}};EmailChannel[_0x8a7e('0xe')][_0x8a7e('0x18')]=function(_0x123b3f){for(var _0x30a64a in _0x123b3f){if(_0x123b3f[_0x8a7e('0x19')](_0x30a64a)){if(_0x123b3f[_0x30a64a][_0x8a7e('0x11')]&&_0x123b3f[_0x30a64a]['active']){var _0x1bae8c=_0x123b3f[_0x30a64a][_0x8a7e('0x11')];if(!_0x1bae8c['service']&&_0x123b3f[_0x30a64a][_0x8a7e('0x1a')]!==_0x8a7e('0x1b'))_0x1bae8c['service']=_0x123b3f[_0x30a64a]['provider'];if(!this[_0x8a7e('0xc')][_0x8a7e('0x19')](_0x1bae8c['id'])){this[_0x8a7e('0xc')][_0x1bae8c['id']]=new EmailImap(_0x1bae8c,_0x123b3f[_0x30a64a][_0x8a7e('0x5')]);this[_0x8a7e('0xc')][_0x1bae8c['id']]['on'](_0x8a7e('0x1c'),this[_0x8a7e('0x1d')][_0x8a7e('0x1e')](this,_0x30a64a));this[_0x8a7e('0xc')][_0x1bae8c['id']]['on']('email',this['handleImapEmail'][_0x8a7e('0x1e')](this,_0x30a64a));this[_0x8a7e('0xc')][_0x1bae8c['id']]['on']('end',this[_0x8a7e('0x1f')][_0x8a7e('0x1e')](this,_0x30a64a));this[_0x8a7e('0xc')][_0x1bae8c['id']]['on'](_0x8a7e('0x20'),this[_0x8a7e('0x21')][_0x8a7e('0x1e')](this,_0x30a64a));}}else{if(_[_0x8a7e('0x15')](this['mailAccounts'][_0x30a64a][_0x8a7e('0x11')])){this[_0x8a7e('0xb')][_0x30a64a]['Imap']={};}this['mailAccounts'][_0x30a64a][_0x8a7e('0x11')][_0x8a7e('0x12')]=_0x8a7e('0x22');this['mailAccounts'][_0x30a64a]['Imap'][_0x8a7e('0x13')]='';this[_0x8a7e('0x17')](_0x30a64a);}}}};EmailChannel[_0x8a7e('0xe')][_0x8a7e('0x1d')]=function(_0x15dcad,_0x9dee98){if(this[_0x8a7e('0xb')][_0x15dcad]){this[_0x8a7e('0xb')][_0x15dcad][_0x8a7e('0x11')][_0x8a7e('0x12')]=_0x8a7e('0x23');this[_0x8a7e('0xb')][_0x15dcad]['Imap'][_0x8a7e('0x13')]=_0x9dee98[_0x8a7e('0x24')];if(_0x9dee98[_0x8a7e('0x25')]){this[_0x8a7e('0xb')][_0x15dcad][_0x8a7e('0x11')][_0x8a7e('0x14')]=_0x9dee98['messages']['total'];}return this['emit'](_0x15dcad);}};EmailChannel[_0x8a7e('0xe')]['handleImapEmail']=function(_0x2b28fb,_0x41eb2c){logger[_0x8a7e('0x26')]('New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s',_0x41eb2c[_0x8a7e('0x27')],_0x41eb2c[_0x8a7e('0x28')],_0x41eb2c[_0x8a7e('0x29')],_0x2b28fb);if(this['mailAccounts'][_0x2b28fb]){return mailAccount[_0x8a7e('0x2a')](_0x2b28fb,_0x41eb2c)['then'](function(_0x2de75c){if(_0x2de75c){logger[_0x8a7e('0x26')](_0x8a7e('0x2b'));}})['catch'](function(_0x59a6ee){logger['error']('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0x8a7e('0x16')](_0x59a6ee));});}};EmailChannel[_0x8a7e('0xe')][_0x8a7e('0x1f')]=function(_0x5624b2){if(this[_0x8a7e('0xb')][_0x5624b2]&&this['mailAccounts'][_0x5624b2][_0x8a7e('0x11')]){if(this[_0x8a7e('0xc')][this[_0x8a7e('0xb')][_0x5624b2][_0x8a7e('0x11')]['id']]){delete this['imapAccounts'][this[_0x8a7e('0xb')][_0x5624b2][_0x8a7e('0x11')]['id']];}this[_0x8a7e('0xb')][_0x5624b2][_0x8a7e('0x11')]['lastSync']=moment();this[_0x8a7e('0x17')](_0x5624b2);}};EmailChannel['prototype'][_0x8a7e('0x21')]=function(_0x5424e2,_0x3985f2){if(this[_0x8a7e('0xb')][_0x5424e2]){this[_0x8a7e('0xb')][_0x5424e2][_0x8a7e('0x11')]['status']=_0x8a7e('0x20');this[_0x8a7e('0xb')][_0x5424e2]['Imap'][_0x8a7e('0x13')]=_0x3985f2;return this['emit'](_0x5424e2);}};EmailChannel[_0x8a7e('0xe')][_0x8a7e('0x2c')]=function(_0x561f6c,_0x4f1389){return this[_0x8a7e('0xd')][_0x8a7e('0x2d')](_0x561f6c,_0x4f1389);};EmailChannel['prototype'][_0x8a7e('0x2e')]=function(_0x5e6766){var _0x18f463=this;if(this['mailAccounts'][_0x5e6766['id']]){if(_[_0x8a7e('0x15')](this['mailAccounts'][_0x5e6766['id']][_0x8a7e('0x2f')])){this[_0x8a7e('0xb')][_0x5e6766['id']][_0x8a7e('0x2f')]={};}this[_0x8a7e('0xb')][_0x5e6766['id']]['Smtp']['lastSync']=moment();return new BPromise(function(_0x21c83e,_0x244ed5){return _0x18f463['smtp'][_0x8a7e('0x30')](_0x5e6766)['then'](function(_0x4eb421){_0x18f463[_0x8a7e('0xb')][_0x5e6766['id']]['Smtp'][_0x8a7e('0x12')]=_0x8a7e('0x31');_0x18f463[_0x8a7e('0xb')][_0x5e6766['id']][_0x8a7e('0x2f')]['messageStatus']='';_0x18f463[_0x8a7e('0x17')](_0x5e6766['id']);_0x21c83e(_0x4eb421);})['catch'](function(_0x478541){_0x18f463[_0x8a7e('0xb')][_0x5e6766['id']]['Smtp'][_0x8a7e('0x12')]=_0x8a7e('0x20');_0x18f463[_0x8a7e('0xb')][_0x5e6766['id']]['Smtp'][_0x8a7e('0x13')]=_0x478541;_0x18f463[_0x8a7e('0x17')](_0x5e6766['id']);_0x244ed5(_0x478541);});});}return this[_0x8a7e('0xd')][_0x8a7e('0x30')](_0x5e6766);};module[_0x8a7e('0x32')]=EmailChannel;