9ecafd90065e36d77acf38958734b47732c56dec
[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 _0xa67e=['./imap','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','smtp','prototype','key','active','Imap','status','messageStatus','total','emit','refresh','hasOwnProperty','service','provider','imapAccounts','handleImapBox','handleImapEmail','bind','end','handleImapError','isNil','disabled','connected','messages','firstName','lastName','from','notify','then','catch','error','stringify','handleImapEnd','send','verifySmtp','Smtp','verify','exports','lodash','moment','bluebird','md5','ioredis','../../../config/logger','email','redis','defaults','localhost','socket.io-emitter'];(function(_0x7b360c,_0x5cec49){var _0x2dad8e=function(_0x560e7f){while(--_0x560e7f){_0x7b360c['push'](_0x7b360c['shift']());}};_0x2dad8e(++_0x5cec49);}(_0xa67e,0x19d));var _0xea67=function(_0x2f6731,_0x9e0f51){_0x2f6731=_0x2f6731-0x0;var _0x2c8a3a=_0xa67e[_0x2f6731];return _0x2c8a3a;};'use strict';var _=require(_0xea67('0x0'));var moment=require(_0xea67('0x1'));var BPromise=require(_0xea67('0x2'));var md5=require(_0xea67('0x3'));var Redis=require(_0xea67('0x4'));var config=require('../../../config/environment');var logger=require(_0xea67('0x5'))(_0xea67('0x6'));config[_0xea67('0x7')]=_[_0xea67('0x8')](config[_0xea67('0x7')],{'host':_0xea67('0x9'),'port':0x18eb});var io=require(_0xea67('0xa'))(new Redis(config[_0xea67('0x7')]));var EmailImap=require(_0xea67('0xb'));var EmailSmtp=require('./smtp');var mailAccount=require(_0xea67('0xc'));var md5Accounts={};function EmailChannel(_0x11a410){logger[_0xea67('0xd')](_0xea67('0xe'));this[_0xea67('0xf')]=_0x11a410[_0xea67('0xf')];this['imapAccounts']={};this[_0xea67('0x10')]=new EmailSmtp();}EmailChannel[_0xea67('0x11')]['emit']=function(_0x39fd80){var _0x392858=this[_0xea67('0xf')][_0x39fd80];var _0xf1032c={'id':_0x392858['id'],'name':_0x392858['name'],'key':_0x392858[_0xea67('0x12')],'active':_0x392858[_0xea67('0x13')],'Imap':{'status':_0x392858[_0xea67('0x14')][_0xea67('0x15')],'messageStatus':_0x392858[_0xea67('0x14')][_0xea67('0x16')],'total':_0x392858[_0xea67('0x14')][_0xea67('0x17')]}};if(_['isNil'](md5Accounts[_0x39fd80])){md5Accounts[_0x39fd80]={};}var _0x5222c3=md5(JSON['stringify'](_0xf1032c));if(md5Accounts[_0x39fd80][_0xea67('0x3')]!==_0x5222c3){md5Accounts[_0x39fd80][_0xea67('0x3')]=_0x5222c3;io[_0xea67('0x18')]('mailAccount:update',_0xf1032c);}};EmailChannel['prototype'][_0xea67('0x19')]=function(_0x1c1a15){for(var _0x478678 in _0x1c1a15){if(_0x1c1a15[_0xea67('0x1a')](_0x478678)){if(_0x1c1a15[_0x478678]['Imap']&&_0x1c1a15[_0x478678][_0xea67('0x13')]){var _0x5d5637=_0x1c1a15[_0x478678][_0xea67('0x14')];if(!_0x5d5637[_0xea67('0x1b')]&&_0x1c1a15[_0x478678]['provider']!=='custom')_0x5d5637[_0xea67('0x1b')]=_0x1c1a15[_0x478678][_0xea67('0x1c')];if(!this[_0xea67('0x1d')][_0xea67('0x1a')](_0x5d5637['id'])){this[_0xea67('0x1d')][_0x5d5637['id']]=new EmailImap(_0x5d5637,_0x1c1a15[_0x478678]['email']);this[_0xea67('0x1d')][_0x5d5637['id']]['on']('box',this[_0xea67('0x1e')]['bind'](this,_0x478678));this[_0xea67('0x1d')][_0x5d5637['id']]['on'](_0xea67('0x6'),this[_0xea67('0x1f')][_0xea67('0x20')](this,_0x478678));this[_0xea67('0x1d')][_0x5d5637['id']]['on'](_0xea67('0x21'),this['handleImapEnd'][_0xea67('0x20')](this,_0x478678));this[_0xea67('0x1d')][_0x5d5637['id']]['on']('error',this[_0xea67('0x22')][_0xea67('0x20')](this,_0x478678));}}else{if(_[_0xea67('0x23')](this['mailAccounts'][_0x478678][_0xea67('0x14')])){this[_0xea67('0xf')][_0x478678][_0xea67('0x14')]={};}this['mailAccounts'][_0x478678][_0xea67('0x14')][_0xea67('0x15')]=_0xea67('0x24');this['mailAccounts'][_0x478678][_0xea67('0x14')][_0xea67('0x16')]='';this[_0xea67('0x18')](_0x478678);}}}};EmailChannel[_0xea67('0x11')]['handleImapBox']=function(_0x49f94d,_0x5aa085){if(this['mailAccounts'][_0x49f94d]){this[_0xea67('0xf')][_0x49f94d][_0xea67('0x14')][_0xea67('0x15')]=_0xea67('0x25');this[_0xea67('0xf')][_0x49f94d][_0xea67('0x14')][_0xea67('0x16')]=_0x5aa085['name'];if(_0x5aa085[_0xea67('0x26')]){this[_0xea67('0xf')][_0x49f94d]['Imap'][_0xea67('0x17')]=_0x5aa085['messages'][_0xea67('0x17')];}return this['emit'](_0x49f94d);}};EmailChannel[_0xea67('0x11')][_0xea67('0x1f')]=function(_0x2807c5,_0x19fb59){logger[_0xea67('0xd')]('New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s',_0x19fb59[_0xea67('0x27')],_0x19fb59[_0xea67('0x28')],_0x19fb59[_0xea67('0x29')],_0x2807c5);if(this[_0xea67('0xf')][_0x2807c5]){return mailAccount[_0xea67('0x2a')](_0x2807c5,_0x19fb59)[_0xea67('0x2b')](function(_0xf7941c){if(_0xf7941c){logger[_0xea67('0xd')]('Mail\x20accepted\x20from\x20routing');}})[_0xea67('0x2c')](function(_0xc91161){logger[_0xea67('0x2d')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0xea67('0x2e')](_0xc91161));});}};EmailChannel['prototype'][_0xea67('0x2f')]=function(_0x307b56){if(this[_0xea67('0xf')][_0x307b56]&&this[_0xea67('0xf')][_0x307b56][_0xea67('0x14')]){if(this[_0xea67('0x1d')][this[_0xea67('0xf')][_0x307b56]['Imap']['id']]){delete this[_0xea67('0x1d')][this[_0xea67('0xf')][_0x307b56][_0xea67('0x14')]['id']];}this[_0xea67('0xf')][_0x307b56]['Imap']['lastSync']=moment();this[_0xea67('0x18')](_0x307b56);}};EmailChannel['prototype']['handleImapError']=function(_0x23766f,_0x87ce63){if(this[_0xea67('0xf')][_0x23766f]){this[_0xea67('0xf')][_0x23766f][_0xea67('0x14')][_0xea67('0x15')]='error';this[_0xea67('0xf')][_0x23766f][_0xea67('0x14')][_0xea67('0x16')]=_0x87ce63;return this[_0xea67('0x18')](_0x23766f);}};EmailChannel[_0xea67('0x11')]['sendMail']=function(_0x114244,_0x3a7ce0){return this[_0xea67('0x10')][_0xea67('0x30')](_0x114244,_0x3a7ce0);};EmailChannel[_0xea67('0x11')][_0xea67('0x31')]=function(_0x566939){var _0xe44768=this;if(this[_0xea67('0xf')][_0x566939['id']]){if(_[_0xea67('0x23')](this[_0xea67('0xf')][_0x566939['id']][_0xea67('0x32')])){this[_0xea67('0xf')][_0x566939['id']]['Smtp']={};}this[_0xea67('0xf')][_0x566939['id']][_0xea67('0x32')]['lastSync']=moment();return new BPromise(function(_0x17bde7,_0x2770c3){return _0xe44768[_0xea67('0x10')][_0xea67('0x33')](_0x566939)[_0xea67('0x2b')](function(_0x40075b){_0xe44768[_0xea67('0xf')][_0x566939['id']][_0xea67('0x32')][_0xea67('0x15')]='verified';_0xe44768['mailAccounts'][_0x566939['id']][_0xea67('0x32')][_0xea67('0x16')]='';_0xe44768['emit'](_0x566939['id']);_0x17bde7(_0x40075b);})[_0xea67('0x2c')](function(_0x5d6144){_0xe44768[_0xea67('0xf')][_0x566939['id']][_0xea67('0x32')][_0xea67('0x15')]=_0xea67('0x2d');_0xe44768[_0xea67('0xf')][_0x566939['id']][_0xea67('0x32')]['messageStatus']=_0x5d6144;_0xe44768[_0xea67('0x18')](_0x566939['id']);_0x2770c3(_0x5d6144);});});}return this[_0xea67('0x10')][_0xea67('0x33')](_0x566939);};module[_0xea67('0x34')]=EmailChannel;