Built motion from commit 4e7c323b.|2.5.34
[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 _0xa0b2=['handleOpenBox','emit','box','handleCloseBox','search','UNSEEN','handleReadUnseen','fetch','message','handleFetchMessage','handleFetchError','handleFetchEnd','closeBox','body','handleMessageBody','attributes','handleMessageAttributes','handleMessageEnd','handleSimpleParser','parser','textCode','hasOwnProperty','upperFirst','exports','path','util','imap','lodash','simpleParser','events','EventEmitter','./utils','../../../config/logger','email','mailbox','INBOX','account','getAccount','once','ready','handleReady','bind','end','handleEnd','error','handleError','connect','inherits','prototype','connTimeout','authTimeout','isNil','service','authentication','user','password','tls','autotls','required','getHost','port','anonymous@xcally.com'];(function(_0x5ca6b7,_0x1a4f32){var _0x2d7743=function(_0x569d42){while(--_0x569d42){_0x5ca6b7['push'](_0x5ca6b7['shift']());}};_0x2d7743(++_0x1a4f32);}(_0xa0b2,0x18c));var _0x2a0b=function(_0x58fbb0,_0xe497b5){_0x58fbb0=_0x58fbb0-0x0;var _0x1783d2=_0xa0b2[_0x58fbb0];return _0x1783d2;};'use strict';var fs=require('fs');var path=require(_0x2a0b('0x0'));var util=require(_0x2a0b('0x1'));var Imap=require(_0x2a0b('0x2'));var _=require(_0x2a0b('0x3'));var rs=require('randomstring');var simpleParser=require('mailparser')[_0x2a0b('0x4')];var Iconv=require('iconv')['Iconv'];var EventEmitter=require(_0x2a0b('0x5'))[_0x2a0b('0x6')];var utils=require(_0x2a0b('0x7'));var logger=require(_0x2a0b('0x8'))(_0x2a0b('0x9'));function EmailImap(_0x5ef522,_0x23473f){this[_0x2a0b('0xa')]=_0x2a0b('0xb');this[_0x2a0b('0xc')]=this[_0x2a0b('0xd')](_0x5ef522,_0x23473f);this[_0x2a0b('0x2')]=new Imap(this[_0x2a0b('0xc')]);this[_0x2a0b('0x2')][_0x2a0b('0xe')](_0x2a0b('0xf'),this[_0x2a0b('0x10')][_0x2a0b('0x11')](this));this[_0x2a0b('0x2')][_0x2a0b('0xe')](_0x2a0b('0x12'),this[_0x2a0b('0x13')][_0x2a0b('0x11')](this));this[_0x2a0b('0x2')]['on'](_0x2a0b('0x14'),this[_0x2a0b('0x15')]['bind'](this));this[_0x2a0b('0x2')][_0x2a0b('0x16')]();}util[_0x2a0b('0x17')](EmailImap,EventEmitter);EmailImap[_0x2a0b('0x18')][_0x2a0b('0xd')]=function(_0x292ed4,_0x4d20ab){var _0x16ce27={'connTimeout':_0x292ed4[_0x2a0b('0x19')]?_0x292ed4[_0x2a0b('0x19')]*0x3e8:0x2710,'authTimeout':_0x292ed4[_0x2a0b('0x1a')]?_0x292ed4[_0x2a0b('0x1a')]*0x3e8:0x1388};if(_[_0x2a0b('0x1b')](_0x292ed4[_0x2a0b('0x1c')])){if(_0x292ed4[_0x2a0b('0x1d')]){_0x16ce27[_0x2a0b('0x1e')]=_0x292ed4['user'];_0x16ce27[_0x2a0b('0x1f')]=_0x292ed4[_0x2a0b('0x1f')];}_0x16ce27['host']=_0x292ed4['host'];_0x16ce27['port']=_0x292ed4['port']||0x3e1;if(_0x292ed4['tls']){_0x16ce27[_0x2a0b('0x20')]=!![];}else{_0x16ce27[_0x2a0b('0x20')]=![];_0x16ce27[_0x2a0b('0x21')]=_0x2a0b('0x22');}}else{_0x16ce27[_0x2a0b('0x1e')]=_0x292ed4[_0x2a0b('0x1e')];_0x16ce27['password']=_0x292ed4['password'];_0x16ce27['host']=utils[_0x2a0b('0x23')](_0x292ed4[_0x2a0b('0x1c')]);_0x16ce27[_0x2a0b('0x24')]=0x3e1;_0x16ce27[_0x2a0b('0x20')]=!![];}_0x16ce27[_0x2a0b('0x9')]=_0x4d20ab||_0x292ed4['user']||_0x2a0b('0x25');return _0x16ce27;};EmailImap[_0x2a0b('0x18')][_0x2a0b('0x10')]=function(){this[_0x2a0b('0x2')]['openBox'](this[_0x2a0b('0xa')],![],this[_0x2a0b('0x26')][_0x2a0b('0x11')](this));};EmailImap[_0x2a0b('0x18')][_0x2a0b('0x26')]=function(_0x4b316c,_0x525b37){if(_0x4b316c){this['handleError'](_0x4b316c,!![]);}else{this[_0x2a0b('0x27')](_0x2a0b('0x28'),_0x525b37);this['readUnseen']();}};EmailImap[_0x2a0b('0x18')][_0x2a0b('0x29')]=function(_0x442a9e){if(_0x442a9e){this[_0x2a0b('0x15')](_0x442a9e,!![]);}else{this['imap'][_0x2a0b('0x12')]();}};EmailImap[_0x2a0b('0x18')]['readUnseen']=function(){this[_0x2a0b('0x2')][_0x2a0b('0x2a')]([_0x2a0b('0x2b')],this[_0x2a0b('0x2c')][_0x2a0b('0x11')](this));};EmailImap[_0x2a0b('0x18')]['handleReadUnseen']=function(_0x2d445a,_0x292d7a){if(_0x2d445a){this[_0x2a0b('0x15')](_0x2d445a,!![]);}else{if(_0x292d7a['length']){try{var _0x5b0c05=this['imap'][_0x2a0b('0x2d')](_0x292d7a,{'bodies':'','markSeen':!![]});_0x5b0c05['on'](_0x2a0b('0x2e'),this[_0x2a0b('0x2f')][_0x2a0b('0x11')](this));_0x5b0c05[_0x2a0b('0xe')](_0x2a0b('0x14'),this[_0x2a0b('0x30')]['bind'](this));_0x5b0c05[_0x2a0b('0xe')]('end',this[_0x2a0b('0x31')]['bind'](this));}catch(_0x166704){logger['error'](_0x166704);}}else{this[_0x2a0b('0x2')][_0x2a0b('0x32')](!![],this[_0x2a0b('0x29')][_0x2a0b('0x11')](this));}}};EmailImap['prototype'][_0x2a0b('0x2f')]=function(_0x429730,_0x36d3e9){_0x429730['on'](_0x2a0b('0x33'),this[_0x2a0b('0x34')][_0x2a0b('0x11')](this));_0x429730[_0x2a0b('0xe')](_0x2a0b('0x35'),this[_0x2a0b('0x36')][_0x2a0b('0x11')](this));_0x429730[_0x2a0b('0xe')]('end',this[_0x2a0b('0x37')][_0x2a0b('0x11')](this));};EmailImap[_0x2a0b('0x18')]['handleMessageBody']=function(_0x2b1111,_0x5eaf23){simpleParser(_0x2b1111,{'Iconv':Iconv},this[_0x2a0b('0x38')][_0x2a0b('0x11')](this));};EmailImap['prototype']['handleMessageAttributes']=function(_0x26954a){};EmailImap['prototype']['handleMessageEnd']=function(){};EmailImap[_0x2a0b('0x18')][_0x2a0b('0x38')]=function(_0x4697ae,_0x4b7d64){if(_0x4697ae){logger[_0x2a0b('0x14')](_0x4697ae);}else{var _0x2be60a=this;utils[_0x2a0b('0x39')](this['account'],_0x4b7d64,function(_0x1f9522){_0x2be60a[_0x2a0b('0x27')]('email',_0x1f9522);});}};EmailImap[_0x2a0b('0x18')][_0x2a0b('0x31')]=function(){this[_0x2a0b('0x2')][_0x2a0b('0x32')](!![],this['handleCloseBox']['bind'](this));};EmailImap[_0x2a0b('0x18')][_0x2a0b('0x30')]=function(_0xbdd7d4){this[_0x2a0b('0x15')](_0xbdd7d4);this[_0x2a0b('0x2')][_0x2a0b('0x32')](!![],this[_0x2a0b('0x29')]['bind'](this));};EmailImap['prototype']['handleEnd']=function(){this[_0x2a0b('0x27')](_0x2a0b('0x12'));};EmailImap[_0x2a0b('0x18')]['handleError']=function(_0x20d0cc,_0x2d47c5){logger[_0x2a0b('0x14')](_0x2a0b('0x15'),this[_0x2a0b('0xc')][_0x2a0b('0x9')],JSON['stringify'](_0x20d0cc));var _0x5a6349='';if(_0x20d0cc){if(_0x20d0cc['textCode']){_0x5a6349=_0x20d0cc[_0x2a0b('0x3a')];}else{for(var _0x25f0ed in _0x20d0cc){if(_0x20d0cc[_0x2a0b('0x3b')](_0x25f0ed)){_0x5a6349+=_[_0x2a0b('0x3c')](_0x25f0ed)+':\x20'+_0x20d0cc[_0x25f0ed]+'\x0a';}}}}if(_0x2d47c5){this[_0x2a0b('0x2')][_0x2a0b('0x12')]();}else{this[_0x2a0b('0x27')](_0x2a0b('0x14'),_0x5a6349);this[_0x2a0b('0x27')](_0x2a0b('0x12'));}};module[_0x2a0b('0x3d')]=EmailImap;