88957f4903f4beb621ba819ab4860c74c3449ade
[motion2.git] / server / services / email / lib / imap.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 _0x1e8d=['account','getAccount','once','ready','handleReady','end','handleEnd','bind','error','handleError','connect','inherits','prototype','connTimeout','authTimeout','service','user','modernAuthentication','xoauth2','generateXOAuth2Token','data5','password','email','anonymous@xcally.com','host','port','tls','autotls','required','handleOpenBox','emit','box','readUnseen','handleCloseBox','search','UNSEEN','handleReadUnseen','slice','message','handleFetchMessage','handleFetchError','closeBox','handleMessageBody','handleMessageEnd','handleSimpleParser','handleMessageAttributes','handleFetchEnd','stringify','textCode','exports','lodash','util','imap','mailparser','simpleParser','iconv','Iconv','events','EventEmitter','./utils','mailbox'];(function(_0x2be8c2,_0x14a4b5){var _0x97f531=function(_0xcee58d){while(--_0xcee58d){_0x2be8c2['push'](_0x2be8c2['shift']());}};_0x97f531(++_0x14a4b5);}(_0x1e8d,0x6f));var _0xd1e8=function(_0x260b23,_0x2d6117){_0x260b23=_0x260b23-0x0;var _0x9526b8=_0x1e8d[_0x260b23];return _0x9526b8;};'use strict';var _=require(_0xd1e8('0x0'));var util=require(_0xd1e8('0x1'));var Imap=require(_0xd1e8('0x2'));var simpleParser=require(_0xd1e8('0x3'))[_0xd1e8('0x4')];var Iconv=require(_0xd1e8('0x5'))[_0xd1e8('0x6')];var EventEmitter=require(_0xd1e8('0x7'))[_0xd1e8('0x8')];var utils=require(_0xd1e8('0x9'));var logger=require('../../../config/logger')('email');function EmailImap(_0x1daa61,_0x1fd059){this[_0xd1e8('0xa')]=_0x1daa61['mailbox'];this[_0xd1e8('0xb')]=this[_0xd1e8('0xc')](_0x1daa61,_0x1fd059);this['imap']=new Imap(this[_0xd1e8('0xb')]);this[_0xd1e8('0x2')][_0xd1e8('0xd')](_0xd1e8('0xe'),this[_0xd1e8('0xf')]['bind'](this));this[_0xd1e8('0x2')][_0xd1e8('0xd')](_0xd1e8('0x10'),this[_0xd1e8('0x11')][_0xd1e8('0x12')](this));this['imap']['on'](_0xd1e8('0x13'),this[_0xd1e8('0x14')][_0xd1e8('0x12')](this));this[_0xd1e8('0x2')][_0xd1e8('0x15')]();}util[_0xd1e8('0x16')](EmailImap,EventEmitter);EmailImap[_0xd1e8('0x17')]['getAccount']=function(_0x411248,_0x105ef4){var _0xd1c35c={'connTimeout':_0x411248['connTimeout']?_0x411248[_0xd1e8('0x18')]*0x3e8:0x2710,'authTimeout':_0x411248[_0xd1e8('0x19')]?_0x411248[_0xd1e8('0x19')]*0x3e8:0x1388};if(_0x411248[_0xd1e8('0x1a')]){_0xd1c35c['user']=_0x411248[_0xd1e8('0x1b')];if(_0x411248[_0xd1e8('0x1c')]){_0xd1c35c[_0xd1e8('0x1d')]=utils[_0xd1e8('0x1e')](_0x411248['CloudProvider'][_0xd1e8('0x1f')],_0x411248[_0xd1e8('0x1b')]);}else{_0xd1c35c['password']=_0x411248[_0xd1e8('0x20')];}}else{if(_0x411248['authentication']){_0xd1c35c[_0xd1e8('0x1b')]=_0x411248['user'];_0xd1c35c[_0xd1e8('0x20')]=_0x411248[_0xd1e8('0x20')];}}_0xd1c35c[_0xd1e8('0x21')]=_0x105ef4||_0x411248['user']||_0xd1e8('0x22');_0xd1c35c[_0xd1e8('0x23')]=_0x411248[_0xd1e8('0x23')];_0xd1c35c['port']=_0x411248[_0xd1e8('0x24')]||0x3e1;if(_0x411248[_0xd1e8('0x25')]){_0xd1c35c['tls']=!![];}else{_0xd1c35c[_0xd1e8('0x25')]=![];_0xd1c35c[_0xd1e8('0x26')]=_0xd1e8('0x27');}return _0xd1c35c;};EmailImap[_0xd1e8('0x17')]['handleReady']=function(){this[_0xd1e8('0x2')]['openBox'](this['mailbox'],![],this[_0xd1e8('0x28')]['bind'](this));};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x28')]=function(_0x69a542,_0x28635a){if(_0x69a542){this['handleError'](_0x69a542,!![]);}else{this[_0xd1e8('0x29')](_0xd1e8('0x2a'),_0x28635a);this[_0xd1e8('0x2b')]();}};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x2c')]=function(_0x4351a0){if(_0x4351a0){this['handleError'](_0x4351a0,!![]);}else{this[_0xd1e8('0x2')][_0xd1e8('0x10')]();}};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x2b')]=function(){this['imap'][_0xd1e8('0x2d')]([_0xd1e8('0x2e')],this[_0xd1e8('0x2f')][_0xd1e8('0x12')](this));};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x2f')]=function(_0xcd8347,_0x52f351){if(_0xcd8347){this[_0xd1e8('0x14')](_0xcd8347,!![]);}else{if(_0x52f351['length']){try{var _0x2fef33=this[_0xd1e8('0x2')]['fetch'](_0x52f351[_0xd1e8('0x30')](0x0,0xa),{'bodies':'','markSeen':!![]});_0x2fef33['on'](_0xd1e8('0x31'),this[_0xd1e8('0x32')][_0xd1e8('0x12')](this));_0x2fef33[_0xd1e8('0xd')]('error',this[_0xd1e8('0x33')][_0xd1e8('0x12')](this));_0x2fef33[_0xd1e8('0xd')](_0xd1e8('0x10'),this['handleFetchEnd'][_0xd1e8('0x12')](this));}catch(_0x4ed319){logger[_0xd1e8('0x13')](_0x4ed319);}}else{this[_0xd1e8('0x2')][_0xd1e8('0x34')](!![],this[_0xd1e8('0x2c')]['bind'](this));}}};EmailImap['prototype']['handleFetchMessage']=function(_0x4cec0e){_0x4cec0e['on']('body',this[_0xd1e8('0x35')][_0xd1e8('0x12')](this));_0x4cec0e['once']('attributes',this['handleMessageAttributes'][_0xd1e8('0x12')](this));_0x4cec0e['once'](_0xd1e8('0x10'),this[_0xd1e8('0x36')][_0xd1e8('0x12')](this));};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x35')]=function(_0x38c0ba){simpleParser(_0x38c0ba,{'Iconv':Iconv},this[_0xd1e8('0x37')][_0xd1e8('0x12')](this));};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x38')]=function(){};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x36')]=function(){};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x37')]=function(_0x13d296,_0x30f603){if(_0x13d296){logger[_0xd1e8('0x13')](_0x13d296);}else{var _0x146fec=this;utils['parser'](this[_0xd1e8('0xb')],_0x30f603,function(_0x23c05d){_0x146fec[_0xd1e8('0x29')](_0xd1e8('0x21'),_0x23c05d);});}};EmailImap['prototype'][_0xd1e8('0x39')]=function(){this[_0xd1e8('0x2')][_0xd1e8('0x34')](!![],this['handleCloseBox'][_0xd1e8('0x12')](this));};EmailImap[_0xd1e8('0x17')]['handleFetchError']=function(_0x4e385e){this[_0xd1e8('0x14')](_0x4e385e);this[_0xd1e8('0x2')][_0xd1e8('0x34')](!![],this[_0xd1e8('0x2c')][_0xd1e8('0x12')](this));};EmailImap['prototype'][_0xd1e8('0x11')]=function(){this[_0xd1e8('0x29')]('end');};EmailImap[_0xd1e8('0x17')][_0xd1e8('0x14')]=function(_0x30b06c,_0x32ec8e){logger['error'](_0xd1e8('0x14'),this['account'][_0xd1e8('0x21')],JSON[_0xd1e8('0x3a')](_0x30b06c));var _0x150221='';if(_0x30b06c){if(_0x30b06c['textCode']){_0x150221=_0x30b06c[_0xd1e8('0x3b')];}else{for(var _0x43d7ca in _0x30b06c){if(_0x30b06c['hasOwnProperty'](_0x43d7ca)){_0x150221+=_['upperFirst'](_0x43d7ca)+':\x20'+_0x30b06c[_0x43d7ca]+'\x0a';}}}}if(_0x32ec8e){this[_0xd1e8('0x2')]['end']();}else{this[_0xd1e8('0x29')](_0xd1e8('0x13'),_0x150221);this[_0xd1e8('0x29')](_0xd1e8('0x10'));}};module[_0xd1e8('0x3c')]=EmailImap;