5a2e929ec9a6773146b6689c04ecf793136d3b00
[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 _0xefd2=['name','key','active','status','Imap','messageStatus','total','isNil','stringify','mailAccount:update','refresh','hasOwnProperty','box','handleImapBox','bind','handleImapEmail','end','disabled','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','from','then','Mail\x20accepted\x20from\x20routing','catch','lastSync','error','sendMail','send','Smtp','verify','verified','exports','lodash','bluebird','md5','../../../config/environment','../../../config/logger','email','defaults','redis','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','mailAccounts','imapAccounts','smtp','prototype','emit'];(function(_0x204239,_0x5ec272){var _0x577511=function(_0x3f305c){while(--_0x3f305c){_0x204239['push'](_0x204239['shift']());}};_0x577511(++_0x5ec272);}(_0xefd2,0x8c));var _0x2efd=function(_0x1589dd,_0x2b9d22){_0x1589dd=_0x1589dd-0x0;var _0x1bddd1=_0xefd2[_0x1589dd];return _0x1bddd1;};'use strict';var _=require(_0x2efd('0x0'));var moment=require('moment');var BPromise=require(_0x2efd('0x1'));var md5=require(_0x2efd('0x2'));var Redis=require('ioredis');var config=require(_0x2efd('0x3'));var logger=require(_0x2efd('0x4'))(_0x2efd('0x5'));config['redis']=_[_0x2efd('0x6')](config[_0x2efd('0x7')],{'host':_0x2efd('0x8'),'port':0x18eb});var io=require(_0x2efd('0x9'))(new Redis(config[_0x2efd('0x7')]));var EmailImap=require(_0x2efd('0xa'));var EmailSmtp=require(_0x2efd('0xb'));var mailAccount=require(_0x2efd('0xc'));var md5Accounts={};function EmailChannel(_0x3477d9){logger[_0x2efd('0xd')]('start\x20email\x20channel');this[_0x2efd('0xe')]=_0x3477d9[_0x2efd('0xe')];this[_0x2efd('0xf')]={};this[_0x2efd('0x10')]=new EmailSmtp();}EmailChannel[_0x2efd('0x11')][_0x2efd('0x12')]=function(_0x199f79){var _0x41e52a=this[_0x2efd('0xe')][_0x199f79];var _0x34542d={'id':_0x41e52a['id'],'name':_0x41e52a[_0x2efd('0x13')],'key':_0x41e52a[_0x2efd('0x14')],'active':_0x41e52a[_0x2efd('0x15')],'Imap':{'status':_0x41e52a['Imap'][_0x2efd('0x16')],'messageStatus':_0x41e52a[_0x2efd('0x17')][_0x2efd('0x18')],'total':_0x41e52a[_0x2efd('0x17')][_0x2efd('0x19')]}};if(_[_0x2efd('0x1a')](md5Accounts[_0x199f79])){md5Accounts[_0x199f79]={};}var _0x7bb8b5=md5(JSON[_0x2efd('0x1b')](_0x34542d));if(md5Accounts[_0x199f79][_0x2efd('0x2')]!==_0x7bb8b5){md5Accounts[_0x199f79][_0x2efd('0x2')]=_0x7bb8b5;io[_0x2efd('0x12')](_0x2efd('0x1c'),_0x34542d);}};EmailChannel[_0x2efd('0x11')][_0x2efd('0x1d')]=function(_0x12b446){for(var _0x5c0f27 in _0x12b446){if(_0x12b446['hasOwnProperty'](_0x5c0f27)){if(_0x12b446[_0x5c0f27][_0x2efd('0x17')]&&_0x12b446[_0x5c0f27][_0x2efd('0x15')]){var _0x3849b0=_0x12b446[_0x5c0f27][_0x2efd('0x17')];if(!this[_0x2efd('0xf')][_0x2efd('0x1e')](_0x3849b0['id'])){this[_0x2efd('0xf')][_0x3849b0['id']]=new EmailImap(_0x3849b0,_0x12b446[_0x5c0f27][_0x2efd('0x5')]);this[_0x2efd('0xf')][_0x3849b0['id']]['on'](_0x2efd('0x1f'),this[_0x2efd('0x20')][_0x2efd('0x21')](this,_0x5c0f27));this[_0x2efd('0xf')][_0x3849b0['id']]['on']('email',this[_0x2efd('0x22')][_0x2efd('0x21')](this,_0x5c0f27));this['imapAccounts'][_0x3849b0['id']]['on'](_0x2efd('0x23'),this['handleImapEnd']['bind'](this,_0x5c0f27));this[_0x2efd('0xf')][_0x3849b0['id']]['on']('error',this['handleImapError'][_0x2efd('0x21')](this,_0x5c0f27));}}else{if(_[_0x2efd('0x1a')](this['mailAccounts'][_0x5c0f27][_0x2efd('0x17')])){this['mailAccounts'][_0x5c0f27]['Imap']={};}this[_0x2efd('0xe')][_0x5c0f27][_0x2efd('0x17')][_0x2efd('0x16')]=_0x2efd('0x24');this[_0x2efd('0xe')][_0x5c0f27][_0x2efd('0x17')][_0x2efd('0x18')]='';this[_0x2efd('0x12')](_0x5c0f27);}}}};EmailChannel[_0x2efd('0x11')][_0x2efd('0x20')]=function(_0x384d74,_0x12e3ef){if(this[_0x2efd('0xe')][_0x384d74]){this[_0x2efd('0xe')][_0x384d74]['Imap'][_0x2efd('0x16')]=_0x2efd('0x25');this[_0x2efd('0xe')][_0x384d74][_0x2efd('0x17')]['messageStatus']=_0x12e3ef['name'];if(_0x12e3ef['messages']){this['mailAccounts'][_0x384d74]['Imap']['total']=_0x12e3ef[_0x2efd('0x26')][_0x2efd('0x19')];}return this[_0x2efd('0x12')](_0x384d74);}};EmailChannel[_0x2efd('0x11')]['handleImapEmail']=function(_0x2c542a,_0xb4482b){logger[_0x2efd('0xd')](_0x2efd('0x27'),_0xb4482b[_0x2efd('0x28')],_0xb4482b['lastName'],_0xb4482b[_0x2efd('0x29')],_0x2c542a);if(this[_0x2efd('0xe')][_0x2c542a]){return mailAccount['notify'](_0x2c542a,_0xb4482b)[_0x2efd('0x2a')](function(_0x21b659){if(_0x21b659){logger[_0x2efd('0xd')](_0x2efd('0x2b'));}})[_0x2efd('0x2c')](function(_0x1dad03){logger['error']('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0x2efd('0x1b')](_0x1dad03));});}};EmailChannel[_0x2efd('0x11')]['handleImapEnd']=function(_0xf28728){if(this['mailAccounts'][_0xf28728]&&this[_0x2efd('0xe')][_0xf28728][_0x2efd('0x17')]){if(this['imapAccounts'][this['mailAccounts'][_0xf28728][_0x2efd('0x17')]['id']]){delete this[_0x2efd('0xf')][this[_0x2efd('0xe')][_0xf28728][_0x2efd('0x17')]['id']];}this['mailAccounts'][_0xf28728]['Imap'][_0x2efd('0x2d')]=moment();this[_0x2efd('0x12')](_0xf28728);}};EmailChannel[_0x2efd('0x11')]['handleImapError']=function(_0x1b52bd,_0x1679d4){if(this['mailAccounts'][_0x1b52bd]){this[_0x2efd('0xe')][_0x1b52bd][_0x2efd('0x17')][_0x2efd('0x16')]=_0x2efd('0x2e');this[_0x2efd('0xe')][_0x1b52bd][_0x2efd('0x17')][_0x2efd('0x18')]=_0x1679d4;return this[_0x2efd('0x12')](_0x1b52bd);}};EmailChannel[_0x2efd('0x11')][_0x2efd('0x2f')]=function(_0x12fd29,_0x59982c){return this[_0x2efd('0x10')][_0x2efd('0x30')](_0x12fd29,_0x59982c);};EmailChannel[_0x2efd('0x11')]['verifySmtp']=function(_0x4e8f4e){var _0x4a6eb7=this;if(this[_0x2efd('0xe')][_0x4e8f4e['id']]){if(_[_0x2efd('0x1a')](this[_0x2efd('0xe')][_0x4e8f4e['id']]['Smtp'])){this['mailAccounts'][_0x4e8f4e['id']][_0x2efd('0x31')]={};}this[_0x2efd('0xe')][_0x4e8f4e['id']]['Smtp']['lastSync']=moment();return new BPromise(function(_0x2f3210,_0x4e6f43){return _0x4a6eb7['smtp'][_0x2efd('0x32')](_0x4e8f4e)[_0x2efd('0x2a')](function(_0x1a8121){_0x4a6eb7[_0x2efd('0xe')][_0x4e8f4e['id']][_0x2efd('0x31')]['status']=_0x2efd('0x33');_0x4a6eb7['mailAccounts'][_0x4e8f4e['id']]['Smtp'][_0x2efd('0x18')]='';_0x4a6eb7[_0x2efd('0x12')](_0x4e8f4e['id']);_0x2f3210(_0x1a8121);})[_0x2efd('0x2c')](function(_0xa5562c){_0x4a6eb7[_0x2efd('0xe')][_0x4e8f4e['id']]['Smtp'][_0x2efd('0x16')]='error';_0x4a6eb7['mailAccounts'][_0x4e8f4e['id']]['Smtp']['messageStatus']=_0xa5562c;_0x4a6eb7[_0x2efd('0x12')](_0x4e8f4e['id']);_0x4e6f43(_0xa5562c);});});}return this['smtp'][_0x2efd('0x32')](_0x4e8f4e);};module[_0x2efd('0x34')]=EmailChannel;