c00a44e14f9d41f61c4892ee5c78ee2bdba4ba80
[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 _0xad99=['isNil','service','authentication','user','password','port','tls','autotls','required','getHost','anonymous@xcally.com','openBox','handleOpenBox','emit','box','handleCloseBox','readUnseen','search','UNSEEN','handleReadUnseen','length','fetch','message','handleFetchMessage','handleFetchError','closeBox','body','attributes','handleMessageEnd','handleMessageBody','handleSimpleParser','error','parser','textCode','hasOwnProperty','exports','path','util','imap','randomstring','mailparser','simpleParser','Iconv','events','EventEmitter','./utils','../../../config/logger','email','mailbox','INBOX','account','ready','handleReady','bind','once','end','handleEnd','handleError','connect','inherits','prototype','getAccount','connTimeout','authTimeout'];(function(_0x105c75,_0x513044){var _0x5c7a82=function(_0x3d89b8){while(--_0x3d89b8){_0x105c75['push'](_0x105c75['shift']());}};_0x5c7a82(++_0x513044);}(_0xad99,0x64));var _0x9ad9=function(_0x203eec,_0x563e0f){_0x203eec=_0x203eec-0x0;var _0x4fa0e6=_0xad99[_0x203eec];return _0x4fa0e6;};'use strict';var fs=require('fs');var path=require(_0x9ad9('0x0'));var util=require(_0x9ad9('0x1'));var Imap=require(_0x9ad9('0x2'));var _=require('lodash');var rs=require(_0x9ad9('0x3'));var simpleParser=require(_0x9ad9('0x4'))[_0x9ad9('0x5')];var Iconv=require('iconv')[_0x9ad9('0x6')];var EventEmitter=require(_0x9ad9('0x7'))[_0x9ad9('0x8')];var utils=require(_0x9ad9('0x9'));var logger=require(_0x9ad9('0xa'))(_0x9ad9('0xb'));function EmailImap(_0x3dd3e7,_0x2b42b){this[_0x9ad9('0xc')]=_0x9ad9('0xd');this[_0x9ad9('0xe')]=this['getAccount'](_0x3dd3e7,_0x2b42b);this[_0x9ad9('0x2')]=new Imap(this[_0x9ad9('0xe')]);this[_0x9ad9('0x2')]['once'](_0x9ad9('0xf'),this[_0x9ad9('0x10')][_0x9ad9('0x11')](this));this['imap'][_0x9ad9('0x12')](_0x9ad9('0x13'),this[_0x9ad9('0x14')][_0x9ad9('0x11')](this));this['imap']['on']('error',this[_0x9ad9('0x15')][_0x9ad9('0x11')](this));this[_0x9ad9('0x2')][_0x9ad9('0x16')]();}util[_0x9ad9('0x17')](EmailImap,EventEmitter);EmailImap[_0x9ad9('0x18')][_0x9ad9('0x19')]=function(_0x458bd1,_0x28fa5c){var _0x40f117={'connTimeout':_0x458bd1[_0x9ad9('0x1a')]?_0x458bd1[_0x9ad9('0x1a')]*0x3e8:0x2710,'authTimeout':_0x458bd1[_0x9ad9('0x1b')]?_0x458bd1[_0x9ad9('0x1b')]*0x3e8:0x1388};if(_[_0x9ad9('0x1c')](_0x458bd1[_0x9ad9('0x1d')])){if(_0x458bd1[_0x9ad9('0x1e')]){_0x40f117[_0x9ad9('0x1f')]=_0x458bd1[_0x9ad9('0x1f')];_0x40f117[_0x9ad9('0x20')]=_0x458bd1[_0x9ad9('0x20')];}_0x40f117['host']=_0x458bd1['host'];_0x40f117['port']=_0x458bd1[_0x9ad9('0x21')]||0x3e1;if(_0x458bd1['tls']){_0x40f117[_0x9ad9('0x22')]=!![];}else{_0x40f117[_0x9ad9('0x22')]=![];_0x40f117[_0x9ad9('0x23')]=_0x9ad9('0x24');}}else{_0x40f117['user']=_0x458bd1[_0x9ad9('0x1f')];_0x40f117[_0x9ad9('0x20')]=_0x458bd1[_0x9ad9('0x20')];_0x40f117['host']=utils[_0x9ad9('0x25')](_0x458bd1[_0x9ad9('0x1d')]);_0x40f117['port']=0x3e1;_0x40f117[_0x9ad9('0x22')]=!![];}_0x40f117[_0x9ad9('0xb')]=_0x28fa5c||_0x458bd1[_0x9ad9('0x1f')]||_0x9ad9('0x26');return _0x40f117;};EmailImap[_0x9ad9('0x18')][_0x9ad9('0x10')]=function(){this[_0x9ad9('0x2')][_0x9ad9('0x27')](this['mailbox'],![],this[_0x9ad9('0x28')][_0x9ad9('0x11')](this));};EmailImap[_0x9ad9('0x18')]['handleOpenBox']=function(_0x209c73,_0x3526bd){if(_0x209c73){this[_0x9ad9('0x15')](_0x209c73,!![]);}else{this[_0x9ad9('0x29')](_0x9ad9('0x2a'),_0x3526bd);this['readUnseen']();}};EmailImap[_0x9ad9('0x18')][_0x9ad9('0x2b')]=function(_0x88560c){if(_0x88560c){this['handleError'](_0x88560c,!![]);}else{this[_0x9ad9('0x2')][_0x9ad9('0x13')]();}};EmailImap['prototype'][_0x9ad9('0x2c')]=function(){this[_0x9ad9('0x2')][_0x9ad9('0x2d')]([_0x9ad9('0x2e')],this['handleReadUnseen']['bind'](this));};EmailImap[_0x9ad9('0x18')][_0x9ad9('0x2f')]=function(_0x571748,_0x20a856){if(_0x571748){this[_0x9ad9('0x15')](_0x571748,!![]);}else{if(_0x20a856[_0x9ad9('0x30')]){try{var _0x483a7a=this['imap'][_0x9ad9('0x31')](_0x20a856,{'bodies':'','markSeen':!![]});_0x483a7a['on'](_0x9ad9('0x32'),this[_0x9ad9('0x33')][_0x9ad9('0x11')](this));_0x483a7a[_0x9ad9('0x12')]('error',this[_0x9ad9('0x34')][_0x9ad9('0x11')](this));_0x483a7a['once']('end',this['handleFetchEnd']['bind'](this));}catch(_0x1724ba){logger['error'](_0x1724ba);}}else{this[_0x9ad9('0x2')][_0x9ad9('0x35')](!![],this['handleCloseBox'][_0x9ad9('0x11')](this));}}};EmailImap[_0x9ad9('0x18')]['handleFetchMessage']=function(_0x59b2dc,_0x3c2fbe){_0x59b2dc['on'](_0x9ad9('0x36'),this['handleMessageBody'][_0x9ad9('0x11')](this));_0x59b2dc[_0x9ad9('0x12')](_0x9ad9('0x37'),this['handleMessageAttributes'][_0x9ad9('0x11')](this));_0x59b2dc[_0x9ad9('0x12')](_0x9ad9('0x13'),this[_0x9ad9('0x38')]['bind'](this));};EmailImap[_0x9ad9('0x18')][_0x9ad9('0x39')]=function(_0x492585,_0x2e1797){simpleParser(_0x492585,{'Iconv':Iconv},this[_0x9ad9('0x3a')][_0x9ad9('0x11')](this));};EmailImap[_0x9ad9('0x18')]['handleMessageAttributes']=function(_0x231633){};EmailImap[_0x9ad9('0x18')][_0x9ad9('0x38')]=function(){};EmailImap['prototype'][_0x9ad9('0x3a')]=function(_0x2cb7d9,_0x3950bb){if(_0x2cb7d9){logger[_0x9ad9('0x3b')](_0x2cb7d9);}else{var _0x26db07=this;utils[_0x9ad9('0x3c')](this[_0x9ad9('0xe')],_0x3950bb,function(_0x2b6b62){_0x26db07[_0x9ad9('0x29')](_0x9ad9('0xb'),_0x2b6b62);});}};EmailImap[_0x9ad9('0x18')]['handleFetchEnd']=function(){this['imap'][_0x9ad9('0x35')](!![],this[_0x9ad9('0x2b')][_0x9ad9('0x11')](this));};EmailImap[_0x9ad9('0x18')][_0x9ad9('0x34')]=function(_0x51f6fa){this['handleError'](_0x51f6fa);this[_0x9ad9('0x2')]['closeBox'](!![],this[_0x9ad9('0x2b')][_0x9ad9('0x11')](this));};EmailImap[_0x9ad9('0x18')][_0x9ad9('0x14')]=function(){this[_0x9ad9('0x29')](_0x9ad9('0x13'));};EmailImap['prototype'][_0x9ad9('0x15')]=function(_0x1bb8d2,_0xa07b31){logger['error']('handleError',this[_0x9ad9('0xe')][_0x9ad9('0xb')],JSON['stringify'](_0x1bb8d2));var _0x4a47cb='';if(_0x1bb8d2){if(_0x1bb8d2[_0x9ad9('0x3d')]){_0x4a47cb=_0x1bb8d2[_0x9ad9('0x3d')];}else{for(var _0x59438b in _0x1bb8d2){if(_0x1bb8d2[_0x9ad9('0x3e')](_0x59438b)){_0x4a47cb+=_['upperFirst'](_0x59438b)+':\x20'+_0x1bb8d2[_0x59438b]+'\x0a';}}}}if(_0xa07b31){this[_0x9ad9('0x2')][_0x9ad9('0x13')]();}else{this[_0x9ad9('0x29')]('error',_0x4a47cb);this[_0x9ad9('0x29')](_0x9ad9('0x13'));}};module[_0x9ad9('0x3f')]=EmailImap;