ef71e73d118cf021dfa09dad4c27642d05c0ed2f
[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 _0xa6b0=['error','handleError','connect','connTimeout','authTimeout','service','user','modernAuthentication','generateXOAuth2Token','CloudProvider','data5','password','authentication','host','port','tls','autotls','required','prototype','openBox','handleOpenBox','emit','box','readUnseen','handleCloseBox','search','UNSEEN','handleReadUnseen','length','fetch','message','handleFetchMessage','handleFetchEnd','body','handleMessageBody','attributes','handleMessageAttributes','handleMessageEnd','handleSimpleParser','parser','closeBox','handleFetchError','stringify','textCode','exports','lodash','util','simpleParser','iconv','Iconv','EventEmitter','./utils','../../../config/logger','email','mailbox','INBOX','account','getAccount','imap','ready','handleReady','bind','once','end','handleEnd'];(function(_0x14865c,_0x43f60b){var _0x527126=function(_0x4a1b1d){while(--_0x4a1b1d){_0x14865c['push'](_0x14865c['shift']());}};_0x527126(++_0x43f60b);}(_0xa6b0,0xaf));var _0x0a6b=function(_0x598f92,_0x417d09){_0x598f92=_0x598f92-0x0;var _0x1c09b8=_0xa6b0[_0x598f92];return _0x1c09b8;};'use strict';var _=require(_0x0a6b('0x0'));var util=require(_0x0a6b('0x1'));var Imap=require('imap');var simpleParser=require('mailparser')[_0x0a6b('0x2')];var Iconv=require(_0x0a6b('0x3'))[_0x0a6b('0x4')];var EventEmitter=require('events')[_0x0a6b('0x5')];var utils=require(_0x0a6b('0x6'));var logger=require(_0x0a6b('0x7'))(_0x0a6b('0x8'));function EmailImap(_0x3dc2b0,_0x3b6967){this[_0x0a6b('0x9')]=_0x0a6b('0xa');this[_0x0a6b('0xb')]=this[_0x0a6b('0xc')](_0x3dc2b0,_0x3b6967);this[_0x0a6b('0xd')]=new Imap(this[_0x0a6b('0xb')]);this['imap']['once'](_0x0a6b('0xe'),this[_0x0a6b('0xf')][_0x0a6b('0x10')](this));this[_0x0a6b('0xd')][_0x0a6b('0x11')](_0x0a6b('0x12'),this[_0x0a6b('0x13')][_0x0a6b('0x10')](this));this[_0x0a6b('0xd')]['on'](_0x0a6b('0x14'),this[_0x0a6b('0x15')][_0x0a6b('0x10')](this));this[_0x0a6b('0xd')][_0x0a6b('0x16')]();}util['inherits'](EmailImap,EventEmitter);EmailImap['prototype'][_0x0a6b('0xc')]=function(_0x2f27d6,_0x2abc4e){var _0x177e87={'connTimeout':_0x2f27d6[_0x0a6b('0x17')]?_0x2f27d6[_0x0a6b('0x17')]*0x3e8:0x2710,'authTimeout':_0x2f27d6[_0x0a6b('0x18')]?_0x2f27d6[_0x0a6b('0x18')]*0x3e8:0x1388};if(_0x2f27d6[_0x0a6b('0x19')]){_0x177e87[_0x0a6b('0x1a')]=_0x2f27d6['user'];_0x177e87[_0x0a6b('0x8')]=_0x2abc4e||_0x2f27d6[_0x0a6b('0x1a')]||'anonymous@xcally.com';if(_0x2f27d6[_0x0a6b('0x1b')]){_0x177e87['xoauth2']=utils[_0x0a6b('0x1c')](_0x2f27d6[_0x0a6b('0x1d')][_0x0a6b('0x1e')],_0x2f27d6[_0x0a6b('0x1a')]);}else{_0x177e87['password']=_0x2f27d6[_0x0a6b('0x1f')];}}else{if(_0x2f27d6[_0x0a6b('0x20')]){_0x177e87['user']=_0x2f27d6[_0x0a6b('0x1a')];_0x177e87[_0x0a6b('0x1f')]=_0x2f27d6[_0x0a6b('0x1f')];}}_0x177e87['host']=_0x2f27d6[_0x0a6b('0x21')];_0x177e87[_0x0a6b('0x22')]=_0x2f27d6[_0x0a6b('0x22')]||0x3e1;if(_0x2f27d6['tls']){_0x177e87[_0x0a6b('0x23')]=!![];}else{_0x177e87[_0x0a6b('0x23')]=![];_0x177e87[_0x0a6b('0x24')]=_0x0a6b('0x25');}return _0x177e87;};EmailImap[_0x0a6b('0x26')]['handleReady']=function(){this[_0x0a6b('0xd')][_0x0a6b('0x27')](this[_0x0a6b('0x9')],![],this['handleOpenBox'][_0x0a6b('0x10')](this));};EmailImap[_0x0a6b('0x26')][_0x0a6b('0x28')]=function(_0x1b8b78,_0x3b12a2){if(_0x1b8b78){this['handleError'](_0x1b8b78,!![]);}else{this[_0x0a6b('0x29')](_0x0a6b('0x2a'),_0x3b12a2);this[_0x0a6b('0x2b')]();}};EmailImap['prototype'][_0x0a6b('0x2c')]=function(_0x51e760){if(_0x51e760){this[_0x0a6b('0x15')](_0x51e760,!![]);}else{this[_0x0a6b('0xd')][_0x0a6b('0x12')]();}};EmailImap['prototype']['readUnseen']=function(){this[_0x0a6b('0xd')][_0x0a6b('0x2d')]([_0x0a6b('0x2e')],this[_0x0a6b('0x2f')][_0x0a6b('0x10')](this));};EmailImap[_0x0a6b('0x26')]['handleReadUnseen']=function(_0xa7ab7c,_0x439d27){if(_0xa7ab7c){this['handleError'](_0xa7ab7c,!![]);}else{if(_0x439d27[_0x0a6b('0x30')]){try{var _0x554145=this['imap'][_0x0a6b('0x31')](_0x439d27,{'bodies':'','markSeen':!![]});_0x554145['on'](_0x0a6b('0x32'),this[_0x0a6b('0x33')][_0x0a6b('0x10')](this));_0x554145[_0x0a6b('0x11')]('error',this['handleFetchError'][_0x0a6b('0x10')](this));_0x554145[_0x0a6b('0x11')](_0x0a6b('0x12'),this[_0x0a6b('0x34')]['bind'](this));}catch(_0xe2b82b){logger[_0x0a6b('0x14')](_0xe2b82b);}}else{this['imap']['closeBox'](!![],this[_0x0a6b('0x2c')][_0x0a6b('0x10')](this));}}};EmailImap['prototype'][_0x0a6b('0x33')]=function(_0x395bc3){_0x395bc3['on'](_0x0a6b('0x35'),this[_0x0a6b('0x36')][_0x0a6b('0x10')](this));_0x395bc3[_0x0a6b('0x11')](_0x0a6b('0x37'),this[_0x0a6b('0x38')][_0x0a6b('0x10')](this));_0x395bc3[_0x0a6b('0x11')](_0x0a6b('0x12'),this[_0x0a6b('0x39')]['bind'](this));};EmailImap[_0x0a6b('0x26')][_0x0a6b('0x36')]=function(_0x50036b){simpleParser(_0x50036b,{'Iconv':Iconv},this[_0x0a6b('0x3a')][_0x0a6b('0x10')](this));};EmailImap[_0x0a6b('0x26')][_0x0a6b('0x38')]=function(){};EmailImap[_0x0a6b('0x26')][_0x0a6b('0x39')]=function(){};EmailImap[_0x0a6b('0x26')][_0x0a6b('0x3a')]=function(_0x4cf002,_0x5d0a6a){if(_0x4cf002){logger[_0x0a6b('0x14')](_0x4cf002);}else{var _0x251a43=this;utils[_0x0a6b('0x3b')](this[_0x0a6b('0xb')],_0x5d0a6a,function(_0x3dd4df){_0x251a43[_0x0a6b('0x29')](_0x0a6b('0x8'),_0x3dd4df);});}};EmailImap[_0x0a6b('0x26')][_0x0a6b('0x34')]=function(){this['imap'][_0x0a6b('0x3c')](!![],this[_0x0a6b('0x2c')][_0x0a6b('0x10')](this));};EmailImap[_0x0a6b('0x26')][_0x0a6b('0x3d')]=function(_0x1af182){this[_0x0a6b('0x15')](_0x1af182);this[_0x0a6b('0xd')][_0x0a6b('0x3c')](!![],this['handleCloseBox'][_0x0a6b('0x10')](this));};EmailImap['prototype']['handleEnd']=function(){this[_0x0a6b('0x29')]('end');};EmailImap['prototype'][_0x0a6b('0x15')]=function(_0x1f756d,_0x307d4c){logger['error'](_0x0a6b('0x15'),this['account']['email'],JSON[_0x0a6b('0x3e')](_0x1f756d));var _0x5b5854='';if(_0x1f756d){if(_0x1f756d[_0x0a6b('0x3f')]){_0x5b5854=_0x1f756d['textCode'];}else{for(var _0x3879cb in _0x1f756d){if(_0x1f756d['hasOwnProperty'](_0x3879cb)){_0x5b5854+=_['upperFirst'](_0x3879cb)+':\x20'+_0x1f756d[_0x3879cb]+'\x0a';}}}}if(_0x307d4c){this[_0x0a6b('0xd')][_0x0a6b('0x12')]();}else{this[_0x0a6b('0x29')](_0x0a6b('0x14'),_0x5b5854);this['emit'](_0x0a6b('0x12'));}};module[_0x0a6b('0x40')]=EmailImap;