Built motion from commit (unavailable).|2.2.0
[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 _0xa6b0=['catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','handleImapEnd','lastSync','sendMail','send','Smtp','verified','verify','exports','lodash','moment','ioredis','../../../config/environment','../../../config/logger','redis','defaults','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','start\x20email\x20channel','imapAccounts','smtp','prototype','emit','mailAccounts','name','key','active','Imap','status','total','isNil','stringify','md5','mailAccount:update','refresh','hasOwnProperty','box','bind','email','end','error','handleImapError','disabled','messageStatus','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','notify','then','Mail\x20accepted\x20from\x20routing'];(function(_0x678834,_0x2716b4){var _0x284370=function(_0x161631){while(--_0x161631){_0x678834['push'](_0x678834['shift']());}};_0x284370(++_0x2716b4);}(_0xa6b0,0xaf));var _0x0a6b=function(_0x12eb31,_0x4ed94d){_0x12eb31=_0x12eb31-0x0;var _0x1ba464=_0xa6b0[_0x12eb31];return _0x1ba464;};'use strict';var _=require(_0x0a6b('0x0'));var moment=require(_0x0a6b('0x1'));var BPromise=require('bluebird');var md5=require('md5');var Redis=require(_0x0a6b('0x2'));var config=require(_0x0a6b('0x3'));var logger=require(_0x0a6b('0x4'))('email');config[_0x0a6b('0x5')]=_[_0x0a6b('0x6')](config[_0x0a6b('0x5')],{'host':_0x0a6b('0x7'),'port':0x18eb});var io=require(_0x0a6b('0x8'))(new Redis(config[_0x0a6b('0x5')]));var EmailImap=require(_0x0a6b('0x9'));var EmailSmtp=require(_0x0a6b('0xa'));var mailAccount=require(_0x0a6b('0xb'));var md5Accounts={};function EmailChannel(_0x340a33){logger[_0x0a6b('0xc')](_0x0a6b('0xd'));this['mailAccounts']=_0x340a33['mailAccounts'];this[_0x0a6b('0xe')]={};this[_0x0a6b('0xf')]=new EmailSmtp();}EmailChannel[_0x0a6b('0x10')][_0x0a6b('0x11')]=function(_0x81a412){var _0xe681c9=this[_0x0a6b('0x12')][_0x81a412];var _0x405046={'id':_0xe681c9['id'],'name':_0xe681c9[_0x0a6b('0x13')],'key':_0xe681c9[_0x0a6b('0x14')],'active':_0xe681c9[_0x0a6b('0x15')],'Imap':{'status':_0xe681c9[_0x0a6b('0x16')][_0x0a6b('0x17')],'messageStatus':_0xe681c9[_0x0a6b('0x16')]['messageStatus'],'total':_0xe681c9['Imap'][_0x0a6b('0x18')]}};if(_[_0x0a6b('0x19')](md5Accounts[_0x81a412])){md5Accounts[_0x81a412]={};}var _0x2abc4e=md5(JSON[_0x0a6b('0x1a')](_0x405046));if(md5Accounts[_0x81a412][_0x0a6b('0x1b')]!==_0x2abc4e){md5Accounts[_0x81a412][_0x0a6b('0x1b')]=_0x2abc4e;io[_0x0a6b('0x11')](_0x0a6b('0x1c'),_0x405046);}};EmailChannel[_0x0a6b('0x10')][_0x0a6b('0x1d')]=function(_0x4797d1){for(var _0xa36d75 in _0x4797d1){if(_0x4797d1[_0x0a6b('0x1e')](_0xa36d75)){if(_0x4797d1[_0xa36d75]['Imap']&&_0x4797d1[_0xa36d75][_0x0a6b('0x15')]){var _0x4d0e3b=_0x4797d1[_0xa36d75][_0x0a6b('0x16')];if(!this[_0x0a6b('0xe')][_0x0a6b('0x1e')](_0x4d0e3b['id'])){this[_0x0a6b('0xe')][_0x4d0e3b['id']]=new EmailImap(_0x4d0e3b,_0x4797d1[_0xa36d75]['email']);this[_0x0a6b('0xe')][_0x4d0e3b['id']]['on'](_0x0a6b('0x1f'),this['handleImapBox'][_0x0a6b('0x20')](this,_0xa36d75));this[_0x0a6b('0xe')][_0x4d0e3b['id']]['on'](_0x0a6b('0x21'),this['handleImapEmail'][_0x0a6b('0x20')](this,_0xa36d75));this[_0x0a6b('0xe')][_0x4d0e3b['id']]['on'](_0x0a6b('0x22'),this['handleImapEnd'][_0x0a6b('0x20')](this,_0xa36d75));this[_0x0a6b('0xe')][_0x4d0e3b['id']]['on'](_0x0a6b('0x23'),this[_0x0a6b('0x24')][_0x0a6b('0x20')](this,_0xa36d75));}}else{if(_['isNil'](this[_0x0a6b('0x12')][_0xa36d75][_0x0a6b('0x16')])){this[_0x0a6b('0x12')][_0xa36d75]['Imap']={};}this[_0x0a6b('0x12')][_0xa36d75][_0x0a6b('0x16')][_0x0a6b('0x17')]=_0x0a6b('0x25');this[_0x0a6b('0x12')][_0xa36d75]['Imap'][_0x0a6b('0x26')]='';this[_0x0a6b('0x11')](_0xa36d75);}}}};EmailChannel[_0x0a6b('0x10')]['handleImapBox']=function(_0x3a9092,_0x51e760){if(this[_0x0a6b('0x12')][_0x3a9092]){this[_0x0a6b('0x12')][_0x3a9092][_0x0a6b('0x16')]['status']='connected';this[_0x0a6b('0x12')][_0x3a9092][_0x0a6b('0x16')][_0x0a6b('0x26')]=_0x51e760[_0x0a6b('0x13')];if(_0x51e760[_0x0a6b('0x27')]){this[_0x0a6b('0x12')][_0x3a9092]['Imap'][_0x0a6b('0x18')]=_0x51e760[_0x0a6b('0x27')]['total'];}return this[_0x0a6b('0x11')](_0x3a9092);}};EmailChannel['prototype']['handleImapEmail']=function(_0xbf2f1a,_0x24e96a){logger[_0x0a6b('0xc')](_0x0a6b('0x28'),_0x24e96a[_0x0a6b('0x29')],_0x24e96a['lastName'],_0x24e96a['from'],_0xbf2f1a);if(this[_0x0a6b('0x12')][_0xbf2f1a]){return mailAccount[_0x0a6b('0x2a')](_0xbf2f1a,_0x24e96a)[_0x0a6b('0x2b')](function(_0xca2fe8){if(_0xca2fe8){logger['info'](_0x0a6b('0x2c'));}})[_0x0a6b('0x2d')](function(_0x52e352){logger['error'](_0x0a6b('0x2e'),JSON[_0x0a6b('0x1a')](_0x52e352));});}};EmailChannel['prototype'][_0x0a6b('0x2f')]=function(_0x395bc3){if(this[_0x0a6b('0x12')][_0x395bc3]&&this[_0x0a6b('0x12')][_0x395bc3][_0x0a6b('0x16')]){if(this[_0x0a6b('0xe')][this[_0x0a6b('0x12')][_0x395bc3][_0x0a6b('0x16')]['id']]){delete this[_0x0a6b('0xe')][this[_0x0a6b('0x12')][_0x395bc3][_0x0a6b('0x16')]['id']];}this[_0x0a6b('0x12')][_0x395bc3]['Imap'][_0x0a6b('0x30')]=moment();this[_0x0a6b('0x11')](_0x395bc3);}};EmailChannel['prototype'][_0x0a6b('0x24')]=function(_0x9f0853,_0x206612){if(this[_0x0a6b('0x12')][_0x9f0853]){this[_0x0a6b('0x12')][_0x9f0853][_0x0a6b('0x16')][_0x0a6b('0x17')]=_0x0a6b('0x23');this[_0x0a6b('0x12')][_0x9f0853]['Imap'][_0x0a6b('0x26')]=_0x206612;return this[_0x0a6b('0x11')](_0x9f0853);}};EmailChannel[_0x0a6b('0x10')][_0x0a6b('0x31')]=function(_0x3dd4df,_0x3f2449){return this[_0x0a6b('0xf')][_0x0a6b('0x32')](_0x3dd4df,_0x3f2449);};EmailChannel[_0x0a6b('0x10')]['verifySmtp']=function(_0x1ee446){var _0x404ba8=this;if(this[_0x0a6b('0x12')][_0x1ee446['id']]){if(_[_0x0a6b('0x19')](this[_0x0a6b('0x12')][_0x1ee446['id']][_0x0a6b('0x33')])){this[_0x0a6b('0x12')][_0x1ee446['id']][_0x0a6b('0x33')]={};}this[_0x0a6b('0x12')][_0x1ee446['id']]['Smtp'][_0x0a6b('0x30')]=moment();return new BPromise(function(_0x5d035a,_0x5a32d6){return _0x404ba8[_0x0a6b('0xf')]['verify'](_0x1ee446)['then'](function(_0x2f1221){_0x404ba8[_0x0a6b('0x12')][_0x1ee446['id']][_0x0a6b('0x33')]['status']=_0x0a6b('0x34');_0x404ba8['mailAccounts'][_0x1ee446['id']]['Smtp'][_0x0a6b('0x26')]='';_0x404ba8['emit'](_0x1ee446['id']);_0x5d035a(_0x2f1221);})[_0x0a6b('0x2d')](function(_0x5094ad){_0x404ba8[_0x0a6b('0x12')][_0x1ee446['id']]['Smtp']['status']=_0x0a6b('0x23');_0x404ba8[_0x0a6b('0x12')][_0x1ee446['id']][_0x0a6b('0x33')][_0x0a6b('0x26')]=_0x5094ad;_0x404ba8['emit'](_0x1ee446['id']);_0x5a32d6(_0x5094ad);});});}return this[_0x0a6b('0xf')][_0x0a6b('0x35')](_0x1ee446);};module[_0x0a6b('0x36')]=EmailChannel;