ebc8a15c86e6f9db449f77d91b5c30ef4696431f
[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 _0x25b6=['emit','readUnseen','handleCloseBox','search','UNSEEN','length','fetch','handleFetchMessage','handleFetchEnd','body','handleMessageBody','attributes','handleMessageAttributes','handleSimpleParser','handleMessageEnd','parser','closeBox','handleFetchError','textCode','upperFirst','path','util','imap','lodash','randomstring','simpleParser','iconv','Iconv','events','EventEmitter','./utils','email','mailbox','INBOX','account','getAccount','once','handleReady','end','handleEnd','bind','error','handleError','connect','inherits','connTimeout','authTimeout','isNil','password','host','port','tls','autotls','required','user','getHost','service','prototype','openBox','handleOpenBox'];(function(_0x4ad743,_0x3efe65){var _0x32e948=function(_0x51b2e6){while(--_0x51b2e6){_0x4ad743['push'](_0x4ad743['shift']());}};_0x32e948(++_0x3efe65);}(_0x25b6,0xc8));var _0x625b=function(_0x1138c4,_0x841d33){_0x1138c4=_0x1138c4-0x0;var _0x200cc1=_0x25b6[_0x1138c4];return _0x200cc1;};'use strict';var fs=require('fs');var path=require(_0x625b('0x0'));var util=require(_0x625b('0x1'));var Imap=require(_0x625b('0x2'));var _=require(_0x625b('0x3'));var rs=require(_0x625b('0x4'));var simpleParser=require('mailparser')[_0x625b('0x5')];var Iconv=require(_0x625b('0x6'))[_0x625b('0x7')];var EventEmitter=require(_0x625b('0x8'))[_0x625b('0x9')];var utils=require(_0x625b('0xa'));var logger=require('../../../config/logger')(_0x625b('0xb'));function EmailImap(_0x22f758,_0x2f476b){this[_0x625b('0xc')]=_0x625b('0xd');this[_0x625b('0xe')]=this[_0x625b('0xf')](_0x22f758,_0x2f476b);this[_0x625b('0x2')]=new Imap(this[_0x625b('0xe')]);this['imap'][_0x625b('0x10')]('ready',this[_0x625b('0x11')]['bind'](this));this[_0x625b('0x2')][_0x625b('0x10')](_0x625b('0x12'),this[_0x625b('0x13')][_0x625b('0x14')](this));this['imap']['on'](_0x625b('0x15'),this[_0x625b('0x16')]['bind'](this));this[_0x625b('0x2')][_0x625b('0x17')]();}util[_0x625b('0x18')](EmailImap,EventEmitter);EmailImap['prototype'][_0x625b('0xf')]=function(_0x4b6b55,_0x5ef90c){var _0x588a9c={'connTimeout':_0x4b6b55[_0x625b('0x19')]?_0x4b6b55[_0x625b('0x19')]*0x3e8:0x2710,'authTimeout':_0x4b6b55[_0x625b('0x1a')]?_0x4b6b55[_0x625b('0x1a')]*0x3e8:0x1388};if(_[_0x625b('0x1b')](_0x4b6b55['service'])){if(_0x4b6b55['authentication']){_0x588a9c['user']=_0x4b6b55['user'];_0x588a9c[_0x625b('0x1c')]=_0x4b6b55['password'];}_0x588a9c[_0x625b('0x1d')]=_0x4b6b55[_0x625b('0x1d')];_0x588a9c[_0x625b('0x1e')]=_0x4b6b55['port']||0x3e1;if(_0x4b6b55['tls']){_0x588a9c[_0x625b('0x1f')]=!![];}else{_0x588a9c['tls']=![];_0x588a9c[_0x625b('0x20')]=_0x625b('0x21');}}else{_0x588a9c[_0x625b('0x22')]=_0x4b6b55[_0x625b('0x22')];_0x588a9c[_0x625b('0x1c')]=_0x4b6b55[_0x625b('0x1c')];_0x588a9c[_0x625b('0x1d')]=utils[_0x625b('0x23')](_0x4b6b55[_0x625b('0x24')]);_0x588a9c[_0x625b('0x1e')]=0x3e1;_0x588a9c['tls']=!![];}_0x588a9c[_0x625b('0xb')]=_0x5ef90c||_0x4b6b55['user']||'anonymous@xcally.com';return _0x588a9c;};EmailImap[_0x625b('0x25')][_0x625b('0x11')]=function(){this[_0x625b('0x2')][_0x625b('0x26')](this[_0x625b('0xc')],![],this[_0x625b('0x27')][_0x625b('0x14')](this));};EmailImap['prototype']['handleOpenBox']=function(_0x1aaf5a,_0x48231f){if(_0x1aaf5a){this[_0x625b('0x16')](_0x1aaf5a,!![]);}else{this[_0x625b('0x28')]('box',_0x48231f);this[_0x625b('0x29')]();}};EmailImap[_0x625b('0x25')][_0x625b('0x2a')]=function(_0x4a38e3){if(_0x4a38e3){this[_0x625b('0x16')](_0x4a38e3,!![]);}else{this[_0x625b('0x2')][_0x625b('0x12')]();}};EmailImap[_0x625b('0x25')][_0x625b('0x29')]=function(){this['imap'][_0x625b('0x2b')]([_0x625b('0x2c')],this['handleReadUnseen'][_0x625b('0x14')](this));};EmailImap['prototype']['handleReadUnseen']=function(_0x4cf200,_0x3b49c8){if(_0x4cf200){this['handleError'](_0x4cf200,!![]);}else{if(_0x3b49c8[_0x625b('0x2d')]){try{var _0x2245db=this[_0x625b('0x2')][_0x625b('0x2e')](_0x3b49c8,{'bodies':'','markSeen':!![]});_0x2245db['on']('message',this[_0x625b('0x2f')][_0x625b('0x14')](this));_0x2245db['once'](_0x625b('0x15'),this['handleFetchError'][_0x625b('0x14')](this));_0x2245db[_0x625b('0x10')]('end',this[_0x625b('0x30')][_0x625b('0x14')](this));}catch(_0x19234a){logger[_0x625b('0x15')](_0x19234a);}}else{this[_0x625b('0x2')]['closeBox'](!![],this[_0x625b('0x2a')][_0x625b('0x14')](this));}}};EmailImap[_0x625b('0x25')][_0x625b('0x2f')]=function(_0x578e95,_0x104004){_0x578e95['on'](_0x625b('0x31'),this[_0x625b('0x32')][_0x625b('0x14')](this));_0x578e95[_0x625b('0x10')](_0x625b('0x33'),this[_0x625b('0x34')][_0x625b('0x14')](this));_0x578e95[_0x625b('0x10')]('end',this['handleMessageEnd']['bind'](this));};EmailImap[_0x625b('0x25')][_0x625b('0x32')]=function(_0x3d8e68,_0x2ab408){simpleParser(_0x3d8e68,{'Iconv':Iconv},this[_0x625b('0x35')][_0x625b('0x14')](this));};EmailImap['prototype'][_0x625b('0x34')]=function(_0x1c307b){};EmailImap['prototype'][_0x625b('0x36')]=function(){};EmailImap['prototype'][_0x625b('0x35')]=function(_0x2a1f2a,_0x40b55b){if(_0x2a1f2a){logger[_0x625b('0x15')](_0x2a1f2a);}else{var _0x2f29ff=this;utils[_0x625b('0x37')](this[_0x625b('0xe')],_0x40b55b,function(_0x33c513){_0x2f29ff['emit'](_0x625b('0xb'),_0x33c513);});}};EmailImap[_0x625b('0x25')][_0x625b('0x30')]=function(){this[_0x625b('0x2')][_0x625b('0x38')](!![],this[_0x625b('0x2a')][_0x625b('0x14')](this));};EmailImap[_0x625b('0x25')][_0x625b('0x39')]=function(_0x3992aa){this[_0x625b('0x16')](_0x3992aa);this[_0x625b('0x2')][_0x625b('0x38')](!![],this[_0x625b('0x2a')][_0x625b('0x14')](this));};EmailImap[_0x625b('0x25')]['handleEnd']=function(){this[_0x625b('0x28')]('end');};EmailImap[_0x625b('0x25')]['handleError']=function(_0x545331,_0x58e941){logger[_0x625b('0x15')](_0x625b('0x16'),this[_0x625b('0xe')][_0x625b('0xb')],JSON['stringify'](_0x545331));var _0x3e6932='';if(_0x545331){if(_0x545331[_0x625b('0x3a')]){_0x3e6932=_0x545331[_0x625b('0x3a')];}else{for(var _0x360871 in _0x545331){if(_0x545331['hasOwnProperty'](_0x360871)){_0x3e6932+=_[_0x625b('0x3b')](_0x360871)+':\x20'+_0x545331[_0x360871]+'\x0a';}}}}if(_0x58e941){this[_0x625b('0x2')][_0x625b('0x12')]();}else{this['emit']('error',_0x3e6932);this[_0x625b('0x28')](_0x625b('0x12'));}};module['exports']=EmailImap;