21a3e2007cbbddc01844331ac884718a244d5655
[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 _0xc5db=['required','handleOpenBox','emit','readUnseen','search','UNSEEN','handleReadUnseen','length','fetch','handleFetchMessage','handleFetchError','body','attributes','handleMessageAttributes','handleMessageEnd','handleMessageBody','handleSimpleParser','handleFetchEnd','closeBox','stringify','textCode','hasOwnProperty','exports','lodash','util','imap','Iconv','events','EventEmitter','./utils','mailbox','account','getAccount','handleReady','once','end','bind','error','handleError','prototype','connTimeout','authTimeout','service','user','modernAuthentication','xoauth2','generateXOAuth2Token','CloudProvider','data5','password','email','anonymous@xcally.com','port','tls','autotls'];(function(_0x5b3e5a,_0xf8648f){var _0x5e24fd=function(_0x970818){while(--_0x970818){_0x5b3e5a['push'](_0x5b3e5a['shift']());}};_0x5e24fd(++_0xf8648f);}(_0xc5db,0x1cf));var _0xbc5d=function(_0x19e910,_0x2f376b){_0x19e910=_0x19e910-0x0;var _0x5f0dd5=_0xc5db[_0x19e910];return _0x5f0dd5;};'use strict';var _=require(_0xbc5d('0x0'));var util=require(_0xbc5d('0x1'));var Imap=require(_0xbc5d('0x2'));var simpleParser=require('mailparser')['simpleParser'];var Iconv=require('iconv')[_0xbc5d('0x3')];var EventEmitter=require(_0xbc5d('0x4'))[_0xbc5d('0x5')];var utils=require(_0xbc5d('0x6'));var logger=require('../../../config/logger')('email');function EmailImap(_0x11101c,_0x1572b9){this['mailbox']=_0x11101c[_0xbc5d('0x7')];this[_0xbc5d('0x8')]=this[_0xbc5d('0x9')](_0x11101c,_0x1572b9);this['imap']=new Imap(this[_0xbc5d('0x8')]);this['imap']['once']('ready',this[_0xbc5d('0xa')]['bind'](this));this[_0xbc5d('0x2')][_0xbc5d('0xb')](_0xbc5d('0xc'),this['handleEnd'][_0xbc5d('0xd')](this));this[_0xbc5d('0x2')]['on'](_0xbc5d('0xe'),this[_0xbc5d('0xf')][_0xbc5d('0xd')](this));this['imap']['connect']();}util['inherits'](EmailImap,EventEmitter);EmailImap[_0xbc5d('0x10')][_0xbc5d('0x9')]=function(_0x361299,_0x535e76){var _0x2aa56e={'connTimeout':_0x361299[_0xbc5d('0x11')]?_0x361299[_0xbc5d('0x11')]*0x3e8:0x2710,'authTimeout':_0x361299[_0xbc5d('0x12')]?_0x361299[_0xbc5d('0x12')]*0x3e8:0x1388};if(_0x361299[_0xbc5d('0x13')]){_0x2aa56e[_0xbc5d('0x14')]=_0x361299[_0xbc5d('0x14')];if(_0x361299[_0xbc5d('0x15')]){_0x2aa56e[_0xbc5d('0x16')]=utils[_0xbc5d('0x17')](_0x361299[_0xbc5d('0x18')][_0xbc5d('0x19')],_0x361299['user']);}else{_0x2aa56e['password']=_0x361299[_0xbc5d('0x1a')];}}else{if(_0x361299['authentication']){_0x2aa56e['user']=_0x361299[_0xbc5d('0x14')];_0x2aa56e[_0xbc5d('0x1a')]=_0x361299['password'];}}_0x2aa56e[_0xbc5d('0x1b')]=_0x535e76||_0x361299[_0xbc5d('0x14')]||_0xbc5d('0x1c');_0x2aa56e['host']=_0x361299['host'];_0x2aa56e[_0xbc5d('0x1d')]=_0x361299[_0xbc5d('0x1d')]||0x3e1;if(_0x361299['tls']){_0x2aa56e[_0xbc5d('0x1e')]=!![];}else{_0x2aa56e['tls']=![];_0x2aa56e[_0xbc5d('0x1f')]=_0xbc5d('0x20');}return _0x2aa56e;};EmailImap[_0xbc5d('0x10')]['handleReady']=function(){this[_0xbc5d('0x2')]['openBox'](this['mailbox'],![],this[_0xbc5d('0x21')][_0xbc5d('0xd')](this));};EmailImap[_0xbc5d('0x10')][_0xbc5d('0x21')]=function(_0xf65381,_0x4a866b){if(_0xf65381){this['handleError'](_0xf65381,!![]);}else{this[_0xbc5d('0x22')]('box',_0x4a866b);this[_0xbc5d('0x23')]();}};EmailImap[_0xbc5d('0x10')]['handleCloseBox']=function(_0x2d1969){if(_0x2d1969){this[_0xbc5d('0xf')](_0x2d1969,!![]);}else{this['imap'][_0xbc5d('0xc')]();}};EmailImap[_0xbc5d('0x10')][_0xbc5d('0x23')]=function(){this[_0xbc5d('0x2')][_0xbc5d('0x24')]([_0xbc5d('0x25')],this['handleReadUnseen'][_0xbc5d('0xd')](this));};EmailImap['prototype'][_0xbc5d('0x26')]=function(_0x1a43e1,_0x37c688){if(_0x1a43e1){this[_0xbc5d('0xf')](_0x1a43e1,!![]);}else{if(_0x37c688[_0xbc5d('0x27')]){try{var _0x48ef3e=this[_0xbc5d('0x2')][_0xbc5d('0x28')](_0x37c688,{'bodies':'','markSeen':!![]});_0x48ef3e['on']('message',this[_0xbc5d('0x29')]['bind'](this));_0x48ef3e[_0xbc5d('0xb')](_0xbc5d('0xe'),this[_0xbc5d('0x2a')][_0xbc5d('0xd')](this));_0x48ef3e[_0xbc5d('0xb')](_0xbc5d('0xc'),this['handleFetchEnd'][_0xbc5d('0xd')](this));}catch(_0x66c899){logger[_0xbc5d('0xe')](_0x66c899);}}else{this[_0xbc5d('0x2')]['closeBox'](!![],this['handleCloseBox'][_0xbc5d('0xd')](this));}}};EmailImap[_0xbc5d('0x10')]['handleFetchMessage']=function(_0x5467f6){_0x5467f6['on'](_0xbc5d('0x2b'),this['handleMessageBody'][_0xbc5d('0xd')](this));_0x5467f6['once'](_0xbc5d('0x2c'),this[_0xbc5d('0x2d')][_0xbc5d('0xd')](this));_0x5467f6[_0xbc5d('0xb')](_0xbc5d('0xc'),this[_0xbc5d('0x2e')][_0xbc5d('0xd')](this));};EmailImap['prototype'][_0xbc5d('0x2f')]=function(_0x367951){simpleParser(_0x367951,{'Iconv':Iconv},this[_0xbc5d('0x30')][_0xbc5d('0xd')](this));};EmailImap[_0xbc5d('0x10')][_0xbc5d('0x2d')]=function(){};EmailImap[_0xbc5d('0x10')][_0xbc5d('0x2e')]=function(){};EmailImap['prototype']['handleSimpleParser']=function(_0x20b158,_0x8f12){if(_0x20b158){logger[_0xbc5d('0xe')](_0x20b158);}else{var _0x4992b1=this;utils['parser'](this[_0xbc5d('0x8')],_0x8f12,function(_0x18ccb4){_0x4992b1[_0xbc5d('0x22')](_0xbc5d('0x1b'),_0x18ccb4);});}};EmailImap[_0xbc5d('0x10')][_0xbc5d('0x31')]=function(){this[_0xbc5d('0x2')][_0xbc5d('0x32')](!![],this['handleCloseBox'][_0xbc5d('0xd')](this));};EmailImap['prototype'][_0xbc5d('0x2a')]=function(_0x165a4b){this[_0xbc5d('0xf')](_0x165a4b);this[_0xbc5d('0x2')]['closeBox'](!![],this['handleCloseBox'][_0xbc5d('0xd')](this));};EmailImap['prototype']['handleEnd']=function(){this[_0xbc5d('0x22')](_0xbc5d('0xc'));};EmailImap[_0xbc5d('0x10')]['handleError']=function(_0x10a785,_0x198901){logger[_0xbc5d('0xe')]('handleError',this[_0xbc5d('0x8')][_0xbc5d('0x1b')],JSON[_0xbc5d('0x33')](_0x10a785));var _0x32ce66='';if(_0x10a785){if(_0x10a785['textCode']){_0x32ce66=_0x10a785[_0xbc5d('0x34')];}else{for(var _0x32097f in _0x10a785){if(_0x10a785[_0xbc5d('0x35')](_0x32097f)){_0x32ce66+=_['upperFirst'](_0x32097f)+':\x20'+_0x10a785[_0x32097f]+'\x0a';}}}}if(_0x198901){this[_0xbc5d('0x2')][_0xbc5d('0xc')]();}else{this[_0xbc5d('0x22')](_0xbc5d('0xe'),_0x32ce66);this[_0xbc5d('0x22')](_0xbc5d('0xc'));}};module[_0xbc5d('0x36')]=EmailImap;