ce5f54eb7ad81a40593660e048844010d36c7641
[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 _0x4bf1=['prototype','emit','name','key','active','Imap','status','total','stringify','md5','mailAccount:update','hasOwnProperty','box','end','handleImapEnd','bind','error','handleImapError','isNil','disabled','messageStatus','handleImapBox','connected','messages','handleImapEmail','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','lastName','from','then','Mail\x20accepted\x20from\x20routing','catch','send','Smtp','lastSync','smtp','verify','verified','moment','ioredis','../../../config/environment','email','redis','defaults','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','mailAccounts','imapAccounts'];(function(_0x3070dc,_0x40cf11){var _0x431489=function(_0x49bc2a){while(--_0x49bc2a){_0x3070dc['push'](_0x3070dc['shift']());}};_0x431489(++_0x40cf11);}(_0x4bf1,0x1e7));var _0x14bf=function(_0x37d80d,_0x3209c3){_0x37d80d=_0x37d80d-0x0;var _0x2d508c=_0x4bf1[_0x37d80d];return _0x2d508c;};'use strict';var _=require('lodash');var moment=require(_0x14bf('0x0'));var BPromise=require('bluebird');var md5=require('md5');var Redis=require(_0x14bf('0x1'));var config=require(_0x14bf('0x2'));var logger=require('../../../config/logger')(_0x14bf('0x3'));config[_0x14bf('0x4')]=_[_0x14bf('0x5')](config[_0x14bf('0x4')],{'host':'localhost','port':0x18eb});var io=require(_0x14bf('0x6'))(new Redis(config[_0x14bf('0x4')]));var EmailImap=require(_0x14bf('0x7'));var EmailSmtp=require(_0x14bf('0x8'));var mailAccount=require(_0x14bf('0x9'));var md5Accounts={};function EmailChannel(_0x44f01c){logger[_0x14bf('0xa')]('start\x20email\x20channel');this[_0x14bf('0xb')]=_0x44f01c['mailAccounts'];this[_0x14bf('0xc')]={};this['smtp']=new EmailSmtp();}EmailChannel[_0x14bf('0xd')][_0x14bf('0xe')]=function(_0x454396){var _0x26028a=this[_0x14bf('0xb')][_0x454396];var _0x4f0d99={'id':_0x26028a['id'],'name':_0x26028a[_0x14bf('0xf')],'key':_0x26028a[_0x14bf('0x10')],'active':_0x26028a[_0x14bf('0x11')],'Imap':{'status':_0x26028a[_0x14bf('0x12')][_0x14bf('0x13')],'messageStatus':_0x26028a[_0x14bf('0x12')]['messageStatus'],'total':_0x26028a['Imap'][_0x14bf('0x14')]}};if(_['isNil'](md5Accounts[_0x454396])){md5Accounts[_0x454396]={};}var _0x493eb1=md5(JSON[_0x14bf('0x15')](_0x4f0d99));if(md5Accounts[_0x454396][_0x14bf('0x16')]!==_0x493eb1){md5Accounts[_0x454396][_0x14bf('0x16')]=_0x493eb1;io['emit'](_0x14bf('0x17'),_0x4f0d99);}};EmailChannel[_0x14bf('0xd')]['refresh']=function(_0x2482eb){for(var _0x35f356 in _0x2482eb){if(_0x2482eb['hasOwnProperty'](_0x35f356)){if(_0x2482eb[_0x35f356][_0x14bf('0x12')]&&_0x2482eb[_0x35f356][_0x14bf('0x11')]){var _0x4fdf6c=_0x2482eb[_0x35f356][_0x14bf('0x12')];if(!this['imapAccounts'][_0x14bf('0x18')](_0x4fdf6c['id'])){this['imapAccounts'][_0x4fdf6c['id']]=new EmailImap(_0x4fdf6c,_0x2482eb[_0x35f356][_0x14bf('0x3')]);this['imapAccounts'][_0x4fdf6c['id']]['on'](_0x14bf('0x19'),this['handleImapBox']['bind'](this,_0x35f356));this['imapAccounts'][_0x4fdf6c['id']]['on'](_0x14bf('0x3'),this['handleImapEmail']['bind'](this,_0x35f356));this['imapAccounts'][_0x4fdf6c['id']]['on'](_0x14bf('0x1a'),this[_0x14bf('0x1b')][_0x14bf('0x1c')](this,_0x35f356));this[_0x14bf('0xc')][_0x4fdf6c['id']]['on'](_0x14bf('0x1d'),this[_0x14bf('0x1e')][_0x14bf('0x1c')](this,_0x35f356));}}else{if(_[_0x14bf('0x1f')](this[_0x14bf('0xb')][_0x35f356][_0x14bf('0x12')])){this['mailAccounts'][_0x35f356][_0x14bf('0x12')]={};}this[_0x14bf('0xb')][_0x35f356][_0x14bf('0x12')][_0x14bf('0x13')]=_0x14bf('0x20');this[_0x14bf('0xb')][_0x35f356][_0x14bf('0x12')][_0x14bf('0x21')]='';this['emit'](_0x35f356);}}}};EmailChannel[_0x14bf('0xd')][_0x14bf('0x22')]=function(_0x5b8540,_0x26a0ed){if(this[_0x14bf('0xb')][_0x5b8540]){this[_0x14bf('0xb')][_0x5b8540][_0x14bf('0x12')][_0x14bf('0x13')]=_0x14bf('0x23');this[_0x14bf('0xb')][_0x5b8540][_0x14bf('0x12')][_0x14bf('0x21')]=_0x26a0ed['name'];if(_0x26a0ed[_0x14bf('0x24')]){this['mailAccounts'][_0x5b8540][_0x14bf('0x12')]['total']=_0x26a0ed[_0x14bf('0x24')][_0x14bf('0x14')];}return this[_0x14bf('0xe')](_0x5b8540);}};EmailChannel[_0x14bf('0xd')][_0x14bf('0x25')]=function(_0x4d0808,_0x9570a6){logger[_0x14bf('0xa')](_0x14bf('0x26'),_0x9570a6['firstName'],_0x9570a6[_0x14bf('0x27')],_0x9570a6[_0x14bf('0x28')],_0x4d0808);if(this['mailAccounts'][_0x4d0808]){return mailAccount['notify'](_0x4d0808,_0x9570a6)[_0x14bf('0x29')](function(_0x4e9b8c){if(_0x4e9b8c){logger[_0x14bf('0xa')](_0x14bf('0x2a'));}})[_0x14bf('0x2b')](function(_0xca2dcd){logger[_0x14bf('0x1d')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0x14bf('0x15')](_0xca2dcd));});}};EmailChannel['prototype']['handleImapEnd']=function(_0x78eaf2){if(this[_0x14bf('0xb')][_0x78eaf2]&&this[_0x14bf('0xb')][_0x78eaf2][_0x14bf('0x12')]){if(this['imapAccounts'][this[_0x14bf('0xb')][_0x78eaf2][_0x14bf('0x12')]['id']]){delete this[_0x14bf('0xc')][this[_0x14bf('0xb')][_0x78eaf2][_0x14bf('0x12')]['id']];}this[_0x14bf('0xb')][_0x78eaf2][_0x14bf('0x12')]['lastSync']=moment();this[_0x14bf('0xe')](_0x78eaf2);}};EmailChannel[_0x14bf('0xd')][_0x14bf('0x1e')]=function(_0x44a75f,_0xc15c88){if(this[_0x14bf('0xb')][_0x44a75f]){this['mailAccounts'][_0x44a75f]['Imap']['status']=_0x14bf('0x1d');this[_0x14bf('0xb')][_0x44a75f][_0x14bf('0x12')]['messageStatus']=_0xc15c88;return this[_0x14bf('0xe')](_0x44a75f);}};EmailChannel['prototype']['sendMail']=function(_0x490f69,_0x27e900){return this['smtp'][_0x14bf('0x2c')](_0x490f69,_0x27e900);};EmailChannel[_0x14bf('0xd')]['verifySmtp']=function(_0x43c3bc){var _0x3ff640=this;if(this['mailAccounts'][_0x43c3bc['id']]){if(_['isNil'](this[_0x14bf('0xb')][_0x43c3bc['id']][_0x14bf('0x2d')])){this['mailAccounts'][_0x43c3bc['id']][_0x14bf('0x2d')]={};}this['mailAccounts'][_0x43c3bc['id']][_0x14bf('0x2d')][_0x14bf('0x2e')]=moment();return new BPromise(function(_0x200c2d,_0x2209d9){return _0x3ff640[_0x14bf('0x2f')][_0x14bf('0x30')](_0x43c3bc)[_0x14bf('0x29')](function(_0x3c9b9c){_0x3ff640['mailAccounts'][_0x43c3bc['id']][_0x14bf('0x2d')][_0x14bf('0x13')]=_0x14bf('0x31');_0x3ff640[_0x14bf('0xb')][_0x43c3bc['id']][_0x14bf('0x2d')][_0x14bf('0x21')]='';_0x3ff640[_0x14bf('0xe')](_0x43c3bc['id']);_0x200c2d(_0x3c9b9c);})['catch'](function(_0x1b3918){_0x3ff640['mailAccounts'][_0x43c3bc['id']][_0x14bf('0x2d')][_0x14bf('0x13')]='error';_0x3ff640[_0x14bf('0xb')][_0x43c3bc['id']]['Smtp'][_0x14bf('0x21')]=_0x1b3918;_0x3ff640[_0x14bf('0xe')](_0x43c3bc['id']);_0x2209d9(_0x1b3918);});});}return this[_0x14bf('0x2f')]['verify'](_0x43c3bc);};module['exports']=EmailChannel;