9de19e098ec8d870be056c46fdd83f4c9e4988d3
[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 _0x1ee7=['email','box','handleImapEmail','bind','end','handleImapEnd','error','status','disabled','handleImapBox','connected','messages','total','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','from','then','Mail\x20accepted\x20from\x20routing','catch','lastSync','handleImapError','sendMail','send','verifySmtp','Smtp','verified','lodash','moment','bluebird','md5','ioredis','../../../config/environment','../../../config/logger','redis','defaults','socket.io-emitter','./imap','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','name','active','messageStatus','Imap','isNil','stringify','hasOwnProperty','service','provider','custom'];(function(_0x3013c6,_0x4bb0e8){var _0x4bb428=function(_0x7ab7e1){while(--_0x7ab7e1){_0x3013c6['push'](_0x3013c6['shift']());}};_0x4bb428(++_0x4bb0e8);}(_0x1ee7,0xbf));var _0x71ee=function(_0x3afd29,_0x4e7914){_0x3afd29=_0x3afd29-0x0;var _0x3e30a2=_0x1ee7[_0x3afd29];return _0x3e30a2;};'use strict';var _=require(_0x71ee('0x0'));var moment=require(_0x71ee('0x1'));var BPromise=require(_0x71ee('0x2'));var md5=require(_0x71ee('0x3'));var Redis=require(_0x71ee('0x4'));var config=require(_0x71ee('0x5'));var logger=require(_0x71ee('0x6'))('email');config[_0x71ee('0x7')]=_[_0x71ee('0x8')](config[_0x71ee('0x7')],{'host':'localhost','port':0x18eb});var io=require(_0x71ee('0x9'))(new Redis(config[_0x71ee('0x7')]));var EmailImap=require(_0x71ee('0xa'));var EmailSmtp=require('./smtp');var mailAccount=require(_0x71ee('0xb'));var md5Accounts={};function EmailChannel(_0x55e4eb){logger[_0x71ee('0xc')](_0x71ee('0xd'));this[_0x71ee('0xe')]=_0x55e4eb[_0x71ee('0xe')];this[_0x71ee('0xf')]={};this[_0x71ee('0x10')]=new EmailSmtp();}EmailChannel[_0x71ee('0x11')][_0x71ee('0x12')]=function(_0x5e4bb4){var _0x355021=this[_0x71ee('0xe')][_0x5e4bb4];var _0x5724a0={'id':_0x355021['id'],'name':_0x355021[_0x71ee('0x13')],'key':_0x355021['key'],'active':_0x355021[_0x71ee('0x14')],'Imap':{'status':_0x355021['Imap']['status'],'messageStatus':_0x355021['Imap'][_0x71ee('0x15')],'total':_0x355021[_0x71ee('0x16')]['total']}};if(_[_0x71ee('0x17')](md5Accounts[_0x5e4bb4])){md5Accounts[_0x5e4bb4]={};}var _0xfb741b=md5(JSON[_0x71ee('0x18')](_0x5724a0));if(md5Accounts[_0x5e4bb4][_0x71ee('0x3')]!==_0xfb741b){md5Accounts[_0x5e4bb4]['md5']=_0xfb741b;io[_0x71ee('0x12')]('mailAccount:update',_0x5724a0);}};EmailChannel[_0x71ee('0x11')]['refresh']=function(_0x34ffe6){for(var _0x538091 in _0x34ffe6){if(_0x34ffe6[_0x71ee('0x19')](_0x538091)){if(_0x34ffe6[_0x538091][_0x71ee('0x16')]&&_0x34ffe6[_0x538091]['active']){var _0x20afde=_0x34ffe6[_0x538091][_0x71ee('0x16')];if(!_0x20afde[_0x71ee('0x1a')]&&_0x34ffe6[_0x538091][_0x71ee('0x1b')]!==_0x71ee('0x1c'))_0x20afde['service']=_0x34ffe6[_0x538091][_0x71ee('0x1b')];if(!this['imapAccounts'][_0x71ee('0x19')](_0x20afde['id'])){this['imapAccounts'][_0x20afde['id']]=new EmailImap(_0x20afde,_0x34ffe6[_0x538091][_0x71ee('0x1d')]);this[_0x71ee('0xf')][_0x20afde['id']]['on'](_0x71ee('0x1e'),this['handleImapBox']['bind'](this,_0x538091));this[_0x71ee('0xf')][_0x20afde['id']]['on'](_0x71ee('0x1d'),this[_0x71ee('0x1f')][_0x71ee('0x20')](this,_0x538091));this[_0x71ee('0xf')][_0x20afde['id']]['on'](_0x71ee('0x21'),this[_0x71ee('0x22')][_0x71ee('0x20')](this,_0x538091));this[_0x71ee('0xf')][_0x20afde['id']]['on'](_0x71ee('0x23'),this['handleImapError']['bind'](this,_0x538091));}}else{if(_['isNil'](this['mailAccounts'][_0x538091]['Imap'])){this['mailAccounts'][_0x538091][_0x71ee('0x16')]={};}this['mailAccounts'][_0x538091][_0x71ee('0x16')][_0x71ee('0x24')]=_0x71ee('0x25');this[_0x71ee('0xe')][_0x538091][_0x71ee('0x16')][_0x71ee('0x15')]='';this[_0x71ee('0x12')](_0x538091);}}}};EmailChannel[_0x71ee('0x11')][_0x71ee('0x26')]=function(_0x1bb96b,_0x40f500){if(this[_0x71ee('0xe')][_0x1bb96b]){this['mailAccounts'][_0x1bb96b][_0x71ee('0x16')]['status']=_0x71ee('0x27');this[_0x71ee('0xe')][_0x1bb96b][_0x71ee('0x16')]['messageStatus']=_0x40f500[_0x71ee('0x13')];if(_0x40f500[_0x71ee('0x28')]){this['mailAccounts'][_0x1bb96b][_0x71ee('0x16')][_0x71ee('0x29')]=_0x40f500[_0x71ee('0x28')][_0x71ee('0x29')];}return this[_0x71ee('0x12')](_0x1bb96b);}};EmailChannel[_0x71ee('0x11')]['handleImapEmail']=function(_0x4c4b51,_0x428d21){logger[_0x71ee('0xc')](_0x71ee('0x2a'),_0x428d21[_0x71ee('0x2b')],_0x428d21['lastName'],_0x428d21[_0x71ee('0x2c')],_0x4c4b51);if(this[_0x71ee('0xe')][_0x4c4b51]){return mailAccount['notify'](_0x4c4b51,_0x428d21)[_0x71ee('0x2d')](function(_0x27b880){if(_0x27b880){logger[_0x71ee('0xc')](_0x71ee('0x2e'));}})[_0x71ee('0x2f')](function(_0x577c4d){logger[_0x71ee('0x23')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON['stringify'](_0x577c4d));});}};EmailChannel[_0x71ee('0x11')][_0x71ee('0x22')]=function(_0x70394c){if(this['mailAccounts'][_0x70394c]&&this['mailAccounts'][_0x70394c]['Imap']){if(this[_0x71ee('0xf')][this[_0x71ee('0xe')][_0x70394c][_0x71ee('0x16')]['id']]){delete this[_0x71ee('0xf')][this[_0x71ee('0xe')][_0x70394c][_0x71ee('0x16')]['id']];}this[_0x71ee('0xe')][_0x70394c][_0x71ee('0x16')][_0x71ee('0x30')]=moment();this[_0x71ee('0x12')](_0x70394c);}};EmailChannel[_0x71ee('0x11')][_0x71ee('0x31')]=function(_0x4f76cc,_0x401d8b){if(this[_0x71ee('0xe')][_0x4f76cc]){this[_0x71ee('0xe')][_0x4f76cc][_0x71ee('0x16')]['status']=_0x71ee('0x23');this[_0x71ee('0xe')][_0x4f76cc][_0x71ee('0x16')][_0x71ee('0x15')]=_0x401d8b;return this[_0x71ee('0x12')](_0x4f76cc);}};EmailChannel['prototype'][_0x71ee('0x32')]=function(_0x397022,_0x11a11a){return this[_0x71ee('0x10')][_0x71ee('0x33')](_0x397022,_0x11a11a);};EmailChannel['prototype'][_0x71ee('0x34')]=function(_0x68fac){var _0x3b6ff2=this;if(this[_0x71ee('0xe')][_0x68fac['id']]){if(_['isNil'](this[_0x71ee('0xe')][_0x68fac['id']][_0x71ee('0x35')])){this[_0x71ee('0xe')][_0x68fac['id']][_0x71ee('0x35')]={};}this[_0x71ee('0xe')][_0x68fac['id']][_0x71ee('0x35')][_0x71ee('0x30')]=moment();return new BPromise(function(_0xd4241a,_0x338815){return _0x3b6ff2[_0x71ee('0x10')]['verify'](_0x68fac)[_0x71ee('0x2d')](function(_0x206135){_0x3b6ff2[_0x71ee('0xe')][_0x68fac['id']]['Smtp']['status']=_0x71ee('0x36');_0x3b6ff2[_0x71ee('0xe')][_0x68fac['id']][_0x71ee('0x35')]['messageStatus']='';_0x3b6ff2[_0x71ee('0x12')](_0x68fac['id']);_0xd4241a(_0x206135);})['catch'](function(_0x57e45f){_0x3b6ff2['mailAccounts'][_0x68fac['id']][_0x71ee('0x35')][_0x71ee('0x24')]='error';_0x3b6ff2[_0x71ee('0xe')][_0x68fac['id']][_0x71ee('0x35')]['messageStatus']=_0x57e45f;_0x3b6ff2[_0x71ee('0x12')](_0x68fac['id']);_0x338815(_0x57e45f);});});}return this[_0x71ee('0x10')]['verify'](_0x68fac);};module['exports']=EmailChannel;