aa4ca9829a179f3ff44933a7cf5f1b0fd23c99af
[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 _0x08cc=['handleMessageAttributes','handleSimpleParser','handleMessageEnd','stringify','textCode','hasOwnProperty','upperFirst','exports','util','imap','lodash','mailparser','iconv','events','EventEmitter','./utils','../../../config/logger','email','mailbox','INBOX','account','getAccount','ready','handleReady','bind','once','end','handleEnd','error','handleError','prototype','connTimeout','authTimeout','isNil','service','authentication','user','password','host','port','tls','autotls','getHost','openBox','handleOpenBox','emit','box','readUnseen','handleCloseBox','search','UNSEEN','handleReadUnseen','length','message','handleFetchMessage','handleFetchError','closeBox','handleMessageBody','attributes'];(function(_0x2238be,_0x2a33a9){var _0x245b96=function(_0x51e3ec){while(--_0x51e3ec){_0x2238be['push'](_0x2238be['shift']());}};_0x245b96(++_0x2a33a9);}(_0x08cc,0xb9));var _0xc08c=function(_0x152826,_0x5d01db){_0x152826=_0x152826-0x0;var _0x51cc1c=_0x08cc[_0x152826];return _0x51cc1c;};'use strict';var fs=require('fs');var path=require('path');var util=require(_0xc08c('0x0'));var Imap=require(_0xc08c('0x1'));var _=require(_0xc08c('0x2'));var rs=require('randomstring');var simpleParser=require(_0xc08c('0x3'))['simpleParser'];var Iconv=require(_0xc08c('0x4'))['Iconv'];var EventEmitter=require(_0xc08c('0x5'))[_0xc08c('0x6')];var utils=require(_0xc08c('0x7'));var logger=require(_0xc08c('0x8'))(_0xc08c('0x9'));function EmailImap(_0x4fba18,_0x593c91){this[_0xc08c('0xa')]=_0xc08c('0xb');this[_0xc08c('0xc')]=this[_0xc08c('0xd')](_0x4fba18,_0x593c91);this[_0xc08c('0x1')]=new Imap(this['account']);this[_0xc08c('0x1')]['once'](_0xc08c('0xe'),this[_0xc08c('0xf')][_0xc08c('0x10')](this));this[_0xc08c('0x1')][_0xc08c('0x11')](_0xc08c('0x12'),this[_0xc08c('0x13')]['bind'](this));this[_0xc08c('0x1')]['on'](_0xc08c('0x14'),this[_0xc08c('0x15')][_0xc08c('0x10')](this));this[_0xc08c('0x1')]['connect']();}util['inherits'](EmailImap,EventEmitter);EmailImap[_0xc08c('0x16')]['getAccount']=function(_0x47cc21,_0x4dd521){var _0x54c5d5={'connTimeout':_0x47cc21[_0xc08c('0x17')]?_0x47cc21[_0xc08c('0x17')]*0x3e8:0x2710,'authTimeout':_0x47cc21['authTimeout']?_0x47cc21[_0xc08c('0x18')]*0x3e8:0x1388};if(_[_0xc08c('0x19')](_0x47cc21[_0xc08c('0x1a')])){if(_0x47cc21[_0xc08c('0x1b')]){_0x54c5d5['user']=_0x47cc21[_0xc08c('0x1c')];_0x54c5d5[_0xc08c('0x1d')]=_0x47cc21[_0xc08c('0x1d')];}_0x54c5d5[_0xc08c('0x1e')]=_0x47cc21[_0xc08c('0x1e')];_0x54c5d5[_0xc08c('0x1f')]=_0x47cc21[_0xc08c('0x1f')]||0x3e1;if(_0x47cc21[_0xc08c('0x20')]){_0x54c5d5[_0xc08c('0x20')]=!![];}else{_0x54c5d5[_0xc08c('0x20')]=![];_0x54c5d5[_0xc08c('0x21')]='required';}}else{_0x54c5d5[_0xc08c('0x1c')]=_0x47cc21[_0xc08c('0x1c')];_0x54c5d5[_0xc08c('0x1d')]=_0x47cc21[_0xc08c('0x1d')];_0x54c5d5[_0xc08c('0x1e')]=utils[_0xc08c('0x22')](_0x47cc21[_0xc08c('0x1a')]);_0x54c5d5[_0xc08c('0x1f')]=0x3e1;_0x54c5d5[_0xc08c('0x20')]=!![];}_0x54c5d5[_0xc08c('0x9')]=_0x4dd521||_0x47cc21['user']||'anonymous@xcally.com';return _0x54c5d5;};EmailImap[_0xc08c('0x16')][_0xc08c('0xf')]=function(){this['imap'][_0xc08c('0x23')](this[_0xc08c('0xa')],![],this[_0xc08c('0x24')]['bind'](this));};EmailImap[_0xc08c('0x16')][_0xc08c('0x24')]=function(_0x1052c9,_0x1568eb){if(_0x1052c9){this[_0xc08c('0x15')](_0x1052c9,!![]);}else{this[_0xc08c('0x25')](_0xc08c('0x26'),_0x1568eb);this[_0xc08c('0x27')]();}};EmailImap[_0xc08c('0x16')][_0xc08c('0x28')]=function(_0x3e42d8){if(_0x3e42d8){this['handleError'](_0x3e42d8,!![]);}else{this['imap'][_0xc08c('0x12')]();}};EmailImap[_0xc08c('0x16')][_0xc08c('0x27')]=function(){this[_0xc08c('0x1')][_0xc08c('0x29')]([_0xc08c('0x2a')],this[_0xc08c('0x2b')][_0xc08c('0x10')](this));};EmailImap[_0xc08c('0x16')][_0xc08c('0x2b')]=function(_0x18ba64,_0x554cd6){if(_0x18ba64){this[_0xc08c('0x15')](_0x18ba64,!![]);}else{if(_0x554cd6[_0xc08c('0x2c')]){try{var _0x3e28b9=this[_0xc08c('0x1')]['fetch'](_0x554cd6,{'bodies':'','markSeen':!![]});_0x3e28b9['on'](_0xc08c('0x2d'),this[_0xc08c('0x2e')][_0xc08c('0x10')](this));_0x3e28b9[_0xc08c('0x11')](_0xc08c('0x14'),this[_0xc08c('0x2f')]['bind'](this));_0x3e28b9[_0xc08c('0x11')](_0xc08c('0x12'),this['handleFetchEnd'][_0xc08c('0x10')](this));}catch(_0x5a48f0){logger[_0xc08c('0x14')](_0x5a48f0);}}else{this['imap'][_0xc08c('0x30')](!![],this['handleCloseBox'][_0xc08c('0x10')](this));}}};EmailImap[_0xc08c('0x16')][_0xc08c('0x2e')]=function(_0x4e46a2,_0x4fc422){_0x4e46a2['on']('body',this[_0xc08c('0x31')][_0xc08c('0x10')](this));_0x4e46a2[_0xc08c('0x11')](_0xc08c('0x32'),this[_0xc08c('0x33')][_0xc08c('0x10')](this));_0x4e46a2[_0xc08c('0x11')](_0xc08c('0x12'),this['handleMessageEnd'][_0xc08c('0x10')](this));};EmailImap[_0xc08c('0x16')]['handleMessageBody']=function(_0x57b932,_0x4812e0){simpleParser(_0x57b932,{'Iconv':Iconv},this[_0xc08c('0x34')][_0xc08c('0x10')](this));};EmailImap[_0xc08c('0x16')]['handleMessageAttributes']=function(_0xd5a605){};EmailImap[_0xc08c('0x16')][_0xc08c('0x35')]=function(){};EmailImap[_0xc08c('0x16')][_0xc08c('0x34')]=function(_0x32b3a5,_0x4d4235){if(_0x32b3a5){logger['error'](_0x32b3a5);}else{var _0x386959=this;utils['parser'](this[_0xc08c('0xc')],_0x4d4235,function(_0x31db4b){_0x386959[_0xc08c('0x25')](_0xc08c('0x9'),_0x31db4b);});}};EmailImap['prototype']['handleFetchEnd']=function(){this[_0xc08c('0x1')][_0xc08c('0x30')](!![],this[_0xc08c('0x28')]['bind'](this));};EmailImap[_0xc08c('0x16')]['handleFetchError']=function(_0xc5ce29){this[_0xc08c('0x15')](_0xc5ce29);this[_0xc08c('0x1')][_0xc08c('0x30')](!![],this['handleCloseBox'][_0xc08c('0x10')](this));};EmailImap['prototype'][_0xc08c('0x13')]=function(){this[_0xc08c('0x25')]('end');};EmailImap['prototype'][_0xc08c('0x15')]=function(_0x3d7288,_0x54962a){logger[_0xc08c('0x14')](_0xc08c('0x15'),this[_0xc08c('0xc')][_0xc08c('0x9')],JSON[_0xc08c('0x36')](_0x3d7288));var _0x5dbbcf='';if(_0x3d7288){if(_0x3d7288[_0xc08c('0x37')]){_0x5dbbcf=_0x3d7288[_0xc08c('0x37')];}else{for(var _0x4f3668 in _0x3d7288){if(_0x3d7288[_0xc08c('0x38')](_0x4f3668)){_0x5dbbcf+=_[_0xc08c('0x39')](_0x4f3668)+':\x20'+_0x3d7288[_0x4f3668]+'\x0a';}}}}if(_0x54962a){this['imap'][_0xc08c('0x12')]();}else{this['emit']('error',_0x5dbbcf);this[_0xc08c('0x25')](_0xc08c('0x12'));}};module[_0xc08c('0x3a')]=EmailImap;