Built motion from commit f623a78c.|2.6.31
[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 _0xebd8=['service','provider','custom','box','handleImapBox','bind','end','handleImapEnd','error','handleImapError','messageStatus','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','notify','then','Mail\x20accepted\x20from\x20routing','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','sendMail','verifySmtp','Smtp','verify','verified','exports','lodash','moment','bluebird','md5','../../../config/environment','../../../config/logger','email','redis','defaults','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','name','key','active','Imap','status','total','isNil','stringify','mailAccount:update','refresh','hasOwnProperty'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0xebd8,0x111));var _0x8ebd=function(_0x3bf5f6,_0x2ab15f){_0x3bf5f6=_0x3bf5f6-0x0;var _0x4b5770=_0xebd8[_0x3bf5f6];return _0x4b5770;};'use strict';var _=require(_0x8ebd('0x0'));var moment=require(_0x8ebd('0x1'));var BPromise=require(_0x8ebd('0x2'));var md5=require(_0x8ebd('0x3'));var Redis=require('ioredis');var config=require(_0x8ebd('0x4'));var logger=require(_0x8ebd('0x5'))(_0x8ebd('0x6'));config[_0x8ebd('0x7')]=_[_0x8ebd('0x8')](config[_0x8ebd('0x7')],{'host':_0x8ebd('0x9'),'port':0x18eb});var io=require(_0x8ebd('0xa'))(new Redis(config[_0x8ebd('0x7')]));var EmailImap=require(_0x8ebd('0xb'));var EmailSmtp=require(_0x8ebd('0xc'));var mailAccount=require(_0x8ebd('0xd'));var md5Accounts={};function EmailChannel(_0x49d057){logger[_0x8ebd('0xe')](_0x8ebd('0xf'));this[_0x8ebd('0x10')]=_0x49d057['mailAccounts'];this[_0x8ebd('0x11')]={};this[_0x8ebd('0x12')]=new EmailSmtp();}EmailChannel[_0x8ebd('0x13')][_0x8ebd('0x14')]=function(_0x173cd7){var _0x528a0b=this[_0x8ebd('0x10')][_0x173cd7];var _0x29e331={'id':_0x528a0b['id'],'name':_0x528a0b[_0x8ebd('0x15')],'key':_0x528a0b[_0x8ebd('0x16')],'active':_0x528a0b[_0x8ebd('0x17')],'Imap':{'status':_0x528a0b[_0x8ebd('0x18')][_0x8ebd('0x19')],'messageStatus':_0x528a0b[_0x8ebd('0x18')]['messageStatus'],'total':_0x528a0b[_0x8ebd('0x18')][_0x8ebd('0x1a')]}};if(_[_0x8ebd('0x1b')](md5Accounts[_0x173cd7])){md5Accounts[_0x173cd7]={};}var _0x2d96e3=md5(JSON[_0x8ebd('0x1c')](_0x29e331));if(md5Accounts[_0x173cd7][_0x8ebd('0x3')]!==_0x2d96e3){md5Accounts[_0x173cd7]['md5']=_0x2d96e3;io[_0x8ebd('0x14')](_0x8ebd('0x1d'),_0x29e331);}};EmailChannel[_0x8ebd('0x13')][_0x8ebd('0x1e')]=function(_0x45e1cf){for(var _0x4b11a0 in _0x45e1cf){if(_0x45e1cf[_0x8ebd('0x1f')](_0x4b11a0)){if(_0x45e1cf[_0x4b11a0][_0x8ebd('0x18')]&&_0x45e1cf[_0x4b11a0][_0x8ebd('0x17')]){var _0x4fdb09=_0x45e1cf[_0x4b11a0][_0x8ebd('0x18')];if(!_0x4fdb09[_0x8ebd('0x20')]&&_0x45e1cf[_0x4b11a0][_0x8ebd('0x21')]!==_0x8ebd('0x22'))_0x4fdb09[_0x8ebd('0x20')]=_0x45e1cf[_0x4b11a0][_0x8ebd('0x21')];if(!this['imapAccounts']['hasOwnProperty'](_0x4fdb09['id'])){this[_0x8ebd('0x11')][_0x4fdb09['id']]=new EmailImap(_0x4fdb09,_0x45e1cf[_0x4b11a0][_0x8ebd('0x6')]);this[_0x8ebd('0x11')][_0x4fdb09['id']]['on'](_0x8ebd('0x23'),this[_0x8ebd('0x24')][_0x8ebd('0x25')](this,_0x4b11a0));this[_0x8ebd('0x11')][_0x4fdb09['id']]['on'](_0x8ebd('0x6'),this['handleImapEmail'][_0x8ebd('0x25')](this,_0x4b11a0));this[_0x8ebd('0x11')][_0x4fdb09['id']]['on'](_0x8ebd('0x26'),this[_0x8ebd('0x27')]['bind'](this,_0x4b11a0));this[_0x8ebd('0x11')][_0x4fdb09['id']]['on'](_0x8ebd('0x28'),this[_0x8ebd('0x29')][_0x8ebd('0x25')](this,_0x4b11a0));}}else{if(_[_0x8ebd('0x1b')](this['mailAccounts'][_0x4b11a0][_0x8ebd('0x18')])){this[_0x8ebd('0x10')][_0x4b11a0][_0x8ebd('0x18')]={};}this['mailAccounts'][_0x4b11a0][_0x8ebd('0x18')][_0x8ebd('0x19')]='disabled';this['mailAccounts'][_0x4b11a0]['Imap'][_0x8ebd('0x2a')]='';this['emit'](_0x4b11a0);}}}};EmailChannel[_0x8ebd('0x13')][_0x8ebd('0x24')]=function(_0x324373,_0x2548bb){if(this[_0x8ebd('0x10')][_0x324373]){this[_0x8ebd('0x10')][_0x324373][_0x8ebd('0x18')][_0x8ebd('0x19')]=_0x8ebd('0x2b');this[_0x8ebd('0x10')][_0x324373][_0x8ebd('0x18')][_0x8ebd('0x2a')]=_0x2548bb[_0x8ebd('0x15')];if(_0x2548bb[_0x8ebd('0x2c')]){this[_0x8ebd('0x10')][_0x324373][_0x8ebd('0x18')][_0x8ebd('0x1a')]=_0x2548bb[_0x8ebd('0x2c')]['total'];}return this[_0x8ebd('0x14')](_0x324373);}};EmailChannel[_0x8ebd('0x13')]['handleImapEmail']=function(_0x4a67d1,_0x503fe5){logger[_0x8ebd('0xe')](_0x8ebd('0x2d'),_0x503fe5[_0x8ebd('0x2e')],_0x503fe5[_0x8ebd('0x2f')],_0x503fe5[_0x8ebd('0x30')],_0x4a67d1);if(this[_0x8ebd('0x10')][_0x4a67d1]){return mailAccount[_0x8ebd('0x31')](_0x4a67d1,_0x503fe5)[_0x8ebd('0x32')](function(_0x4e6fad){if(_0x4e6fad){logger[_0x8ebd('0xe')](_0x8ebd('0x33'));}})[_0x8ebd('0x34')](function(_0x5aee85){logger[_0x8ebd('0x28')](_0x8ebd('0x35'),JSON[_0x8ebd('0x1c')](_0x5aee85));});}};EmailChannel['prototype'][_0x8ebd('0x27')]=function(_0x498521){if(this[_0x8ebd('0x10')][_0x498521]&&this[_0x8ebd('0x10')][_0x498521]['Imap']){if(this[_0x8ebd('0x11')][this['mailAccounts'][_0x498521][_0x8ebd('0x18')]['id']]){delete this[_0x8ebd('0x11')][this['mailAccounts'][_0x498521]['Imap']['id']];}this['mailAccounts'][_0x498521]['Imap'][_0x8ebd('0x36')]=moment();this[_0x8ebd('0x14')](_0x498521);}};EmailChannel['prototype'][_0x8ebd('0x29')]=function(_0x15229a,_0x3af2d0){if(this[_0x8ebd('0x10')][_0x15229a]){this[_0x8ebd('0x10')][_0x15229a]['Imap'][_0x8ebd('0x19')]=_0x8ebd('0x28');this[_0x8ebd('0x10')][_0x15229a][_0x8ebd('0x18')][_0x8ebd('0x2a')]=_0x3af2d0;return this[_0x8ebd('0x14')](_0x15229a);}};EmailChannel['prototype'][_0x8ebd('0x37')]=function(_0x45a000,_0x2b1e5c){return this['smtp']['send'](_0x45a000,_0x2b1e5c);};EmailChannel[_0x8ebd('0x13')][_0x8ebd('0x38')]=function(_0x331e22){var _0x3c49ca=this;if(this[_0x8ebd('0x10')][_0x331e22['id']]){if(_[_0x8ebd('0x1b')](this[_0x8ebd('0x10')][_0x331e22['id']]['Smtp'])){this[_0x8ebd('0x10')][_0x331e22['id']][_0x8ebd('0x39')]={};}this[_0x8ebd('0x10')][_0x331e22['id']][_0x8ebd('0x39')][_0x8ebd('0x36')]=moment();return new BPromise(function(_0x2fda59,_0x5314e9){return _0x3c49ca['smtp'][_0x8ebd('0x3a')](_0x331e22)['then'](function(_0x3e1c9f){_0x3c49ca[_0x8ebd('0x10')][_0x331e22['id']][_0x8ebd('0x39')]['status']=_0x8ebd('0x3b');_0x3c49ca['mailAccounts'][_0x331e22['id']][_0x8ebd('0x39')]['messageStatus']='';_0x3c49ca[_0x8ebd('0x14')](_0x331e22['id']);_0x2fda59(_0x3e1c9f);})['catch'](function(_0x50fb8d){_0x3c49ca[_0x8ebd('0x10')][_0x331e22['id']][_0x8ebd('0x39')][_0x8ebd('0x19')]=_0x8ebd('0x28');_0x3c49ca[_0x8ebd('0x10')][_0x331e22['id']][_0x8ebd('0x39')]['messageStatus']=_0x50fb8d;_0x3c49ca[_0x8ebd('0x14')](_0x331e22['id']);_0x5314e9(_0x50fb8d);});});}return this[_0x8ebd('0x12')][_0x8ebd('0x3a')](_0x331e22);};module[_0x8ebd('0x3c')]=EmailChannel;