e40c31157540489a9939e6f3f1d3ef4d9bc4cccc
[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 _0xe9fd=['service','provider','imapAccounts','hasOwnProperty','email','box','handleImapBox','bind','handleImapEmail','end','handleImapEnd','handleImapError','disabled','connected','messages','total','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','from','then','info','catch','error','Mail\x20not\x20accepted\x20from\x20routing\x20%s','stringify','sendMail','send','verifySmtp','Smtp','verify','verified','exports','lodash','moment','bluebird','../../../config/environment','../../../config/logger','redis','defaults','localhost','socket.io-emitter','./imap','start\x20email\x20channel','mailAccounts','smtp','prototype','emit','name','key','Imap','status','messageStatus','isNil','md5','mailAccount:update','refresh','active'];(function(_0x5aa1c2,_0x1adfb1){var _0x44a665=function(_0x3910ef){while(--_0x3910ef){_0x5aa1c2['push'](_0x5aa1c2['shift']());}};_0x44a665(++_0x1adfb1);}(_0xe9fd,0x176));var _0xde9f=function(_0x54d87e,_0x25dde1){_0x54d87e=_0x54d87e-0x0;var _0x2538c4=_0xe9fd[_0x54d87e];return _0x2538c4;};'use strict';var _=require(_0xde9f('0x0'));var moment=require(_0xde9f('0x1'));var BPromise=require(_0xde9f('0x2'));var md5=require('md5');var Redis=require('ioredis');var config=require(_0xde9f('0x3'));var logger=require(_0xde9f('0x4'))('email');config[_0xde9f('0x5')]=_[_0xde9f('0x6')](config[_0xde9f('0x5')],{'host':_0xde9f('0x7'),'port':0x18eb});var io=require(_0xde9f('0x8'))(new Redis(config[_0xde9f('0x5')]));var EmailImap=require(_0xde9f('0x9'));var EmailSmtp=require('./smtp');var mailAccount=require('../rpc/mailAccount');var md5Accounts={};function EmailChannel(_0x258c71){logger['info'](_0xde9f('0xa'));this[_0xde9f('0xb')]=_0x258c71['mailAccounts'];this['imapAccounts']={};this[_0xde9f('0xc')]=new EmailSmtp();}EmailChannel[_0xde9f('0xd')][_0xde9f('0xe')]=function(_0x1fa3e5){var _0x441e8f=this[_0xde9f('0xb')][_0x1fa3e5];var _0x5d68db={'id':_0x441e8f['id'],'name':_0x441e8f[_0xde9f('0xf')],'key':_0x441e8f[_0xde9f('0x10')],'active':_0x441e8f['active'],'Imap':{'status':_0x441e8f[_0xde9f('0x11')][_0xde9f('0x12')],'messageStatus':_0x441e8f['Imap'][_0xde9f('0x13')],'total':_0x441e8f[_0xde9f('0x11')]['total']}};if(_[_0xde9f('0x14')](md5Accounts[_0x1fa3e5])){md5Accounts[_0x1fa3e5]={};}var _0x3ee018=md5(JSON['stringify'](_0x5d68db));if(md5Accounts[_0x1fa3e5][_0xde9f('0x15')]!==_0x3ee018){md5Accounts[_0x1fa3e5][_0xde9f('0x15')]=_0x3ee018;io['emit'](_0xde9f('0x16'),_0x5d68db);}};EmailChannel[_0xde9f('0xd')][_0xde9f('0x17')]=function(_0x378a27){for(var _0x2d8224 in _0x378a27){if(_0x378a27['hasOwnProperty'](_0x2d8224)){if(_0x378a27[_0x2d8224][_0xde9f('0x11')]&&_0x378a27[_0x2d8224][_0xde9f('0x18')]){var _0x21d9d5=_0x378a27[_0x2d8224][_0xde9f('0x11')];if(!_0x21d9d5[_0xde9f('0x19')]&&_0x378a27[_0x2d8224][_0xde9f('0x1a')]!=='custom')_0x21d9d5['service']=_0x378a27[_0x2d8224][_0xde9f('0x1a')];if(!this[_0xde9f('0x1b')][_0xde9f('0x1c')](_0x21d9d5['id'])){this[_0xde9f('0x1b')][_0x21d9d5['id']]=new EmailImap(_0x21d9d5,_0x378a27[_0x2d8224][_0xde9f('0x1d')]);this['imapAccounts'][_0x21d9d5['id']]['on'](_0xde9f('0x1e'),this[_0xde9f('0x1f')][_0xde9f('0x20')](this,_0x2d8224));this[_0xde9f('0x1b')][_0x21d9d5['id']]['on'](_0xde9f('0x1d'),this[_0xde9f('0x21')]['bind'](this,_0x2d8224));this['imapAccounts'][_0x21d9d5['id']]['on'](_0xde9f('0x22'),this[_0xde9f('0x23')][_0xde9f('0x20')](this,_0x2d8224));this[_0xde9f('0x1b')][_0x21d9d5['id']]['on']('error',this[_0xde9f('0x24')]['bind'](this,_0x2d8224));}}else{if(_[_0xde9f('0x14')](this[_0xde9f('0xb')][_0x2d8224][_0xde9f('0x11')])){this['mailAccounts'][_0x2d8224][_0xde9f('0x11')]={};}this['mailAccounts'][_0x2d8224][_0xde9f('0x11')][_0xde9f('0x12')]=_0xde9f('0x25');this[_0xde9f('0xb')][_0x2d8224][_0xde9f('0x11')][_0xde9f('0x13')]='';this[_0xde9f('0xe')](_0x2d8224);}}}};EmailChannel[_0xde9f('0xd')]['handleImapBox']=function(_0x1b5dba,_0x4d0265){if(this['mailAccounts'][_0x1b5dba]){this[_0xde9f('0xb')][_0x1b5dba]['Imap'][_0xde9f('0x12')]=_0xde9f('0x26');this[_0xde9f('0xb')][_0x1b5dba][_0xde9f('0x11')][_0xde9f('0x13')]=_0x4d0265[_0xde9f('0xf')];if(_0x4d0265['messages']){this[_0xde9f('0xb')][_0x1b5dba][_0xde9f('0x11')]['total']=_0x4d0265[_0xde9f('0x27')][_0xde9f('0x28')];}return this['emit'](_0x1b5dba);}};EmailChannel[_0xde9f('0xd')][_0xde9f('0x21')]=function(_0x11fe3c,_0x467da7){logger['info'](_0xde9f('0x29'),_0x467da7[_0xde9f('0x2a')],_0x467da7['lastName'],_0x467da7[_0xde9f('0x2b')],_0x11fe3c);if(this[_0xde9f('0xb')][_0x11fe3c]){return mailAccount['notify'](_0x11fe3c,_0x467da7)[_0xde9f('0x2c')](function(_0x1fc2ab){if(_0x1fc2ab){logger[_0xde9f('0x2d')]('Mail\x20accepted\x20from\x20routing');}})[_0xde9f('0x2e')](function(_0x52395d){logger[_0xde9f('0x2f')](_0xde9f('0x30'),JSON[_0xde9f('0x31')](_0x52395d));});}};EmailChannel[_0xde9f('0xd')][_0xde9f('0x23')]=function(_0xb086d8){if(this[_0xde9f('0xb')][_0xb086d8]&&this[_0xde9f('0xb')][_0xb086d8]['Imap']){if(this[_0xde9f('0x1b')][this[_0xde9f('0xb')][_0xb086d8][_0xde9f('0x11')]['id']]){delete this['imapAccounts'][this[_0xde9f('0xb')][_0xb086d8][_0xde9f('0x11')]['id']];}this[_0xde9f('0xb')][_0xb086d8][_0xde9f('0x11')]['lastSync']=moment();this[_0xde9f('0xe')](_0xb086d8);}};EmailChannel[_0xde9f('0xd')][_0xde9f('0x24')]=function(_0x59dd96,_0x37a7f3){if(this[_0xde9f('0xb')][_0x59dd96]){this[_0xde9f('0xb')][_0x59dd96][_0xde9f('0x11')]['status']='error';this[_0xde9f('0xb')][_0x59dd96][_0xde9f('0x11')][_0xde9f('0x13')]=_0x37a7f3;return this['emit'](_0x59dd96);}};EmailChannel[_0xde9f('0xd')][_0xde9f('0x32')]=function(_0x26c0a3,_0x4a9495){return this[_0xde9f('0xc')][_0xde9f('0x33')](_0x26c0a3,_0x4a9495);};EmailChannel[_0xde9f('0xd')][_0xde9f('0x34')]=function(_0x2bd4d5){var _0x391ee1=this;if(this[_0xde9f('0xb')][_0x2bd4d5['id']]){if(_[_0xde9f('0x14')](this[_0xde9f('0xb')][_0x2bd4d5['id']][_0xde9f('0x35')])){this['mailAccounts'][_0x2bd4d5['id']][_0xde9f('0x35')]={};}this[_0xde9f('0xb')][_0x2bd4d5['id']]['Smtp']['lastSync']=moment();return new BPromise(function(_0x2445b0,_0x319d21){return _0x391ee1[_0xde9f('0xc')][_0xde9f('0x36')](_0x2bd4d5)[_0xde9f('0x2c')](function(_0x3c082c){_0x391ee1[_0xde9f('0xb')][_0x2bd4d5['id']][_0xde9f('0x35')]['status']=_0xde9f('0x37');_0x391ee1['mailAccounts'][_0x2bd4d5['id']][_0xde9f('0x35')][_0xde9f('0x13')]='';_0x391ee1[_0xde9f('0xe')](_0x2bd4d5['id']);_0x2445b0(_0x3c082c);})[_0xde9f('0x2e')](function(_0x545faa){_0x391ee1[_0xde9f('0xb')][_0x2bd4d5['id']][_0xde9f('0x35')][_0xde9f('0x12')]=_0xde9f('0x2f');_0x391ee1['mailAccounts'][_0x2bd4d5['id']]['Smtp'][_0xde9f('0x13')]=_0x545faa;_0x391ee1['emit'](_0x2bd4d5['id']);_0x319d21(_0x545faa);});});}return this[_0xde9f('0xc')][_0xde9f('0x36')](_0x2bd4d5);};module[_0xde9f('0x38')]=EmailChannel;