28403b96bb5633052251f86da5a05c392ea353fb
[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 _0x0279=['util','simpleParser','iconv','Iconv','EventEmitter','./utils','../../../config/logger','email','mailbox','imap','bind','once','end','error','handleError','inherits','prototype','connTimeout','authTimeout','service','user','modernAuthentication','data5','password','authentication','host','port','tls','autotls','required','handleReady','handleOpenBox','emit','box','handleCloseBox','readUnseen','search','UNSEEN','handleReadUnseen','fetch','message','handleFetchMessage','handleFetchError','handleFetchEnd','closeBox','body','handleMessageBody','attributes','handleMessageAttributes','handleMessageEnd','handleSimpleParser','account','handleEnd','stringify','textCode','hasOwnProperty','upperFirst','exports','lodash'];(function(_0x366388,_0x4595a6){var _0x41c97e=function(_0x4b9fff){while(--_0x4b9fff){_0x366388['push'](_0x366388['shift']());}};_0x41c97e(++_0x4595a6);}(_0x0279,0x75));var _0x9027=function(_0x3b0eac,_0x503c4e){_0x3b0eac=_0x3b0eac-0x0;var _0xdf4967=_0x0279[_0x3b0eac];return _0xdf4967;};'use strict';var _=require(_0x9027('0x0'));var util=require(_0x9027('0x1'));var Imap=require('imap');var simpleParser=require('mailparser')[_0x9027('0x2')];var Iconv=require(_0x9027('0x3'))[_0x9027('0x4')];var EventEmitter=require('events')[_0x9027('0x5')];var utils=require(_0x9027('0x6'));var logger=require(_0x9027('0x7'))(_0x9027('0x8'));function EmailImap(_0x6e8a45,_0x1900e3){this[_0x9027('0x9')]=_0x6e8a45[_0x9027('0x9')];this['account']=this['getAccount'](_0x6e8a45,_0x1900e3);this['imap']=new Imap(this['account']);this[_0x9027('0xa')]['once']('ready',this['handleReady'][_0x9027('0xb')](this));this[_0x9027('0xa')][_0x9027('0xc')](_0x9027('0xd'),this['handleEnd']['bind'](this));this[_0x9027('0xa')]['on'](_0x9027('0xe'),this[_0x9027('0xf')][_0x9027('0xb')](this));this[_0x9027('0xa')]['connect']();}util[_0x9027('0x10')](EmailImap,EventEmitter);EmailImap[_0x9027('0x11')]['getAccount']=function(_0x579868,_0xba7428){var _0x113864={'connTimeout':_0x579868[_0x9027('0x12')]?_0x579868[_0x9027('0x12')]*0x3e8:0x2710,'authTimeout':_0x579868[_0x9027('0x13')]?_0x579868[_0x9027('0x13')]*0x3e8:0x1388};if(_0x579868[_0x9027('0x14')]){_0x113864[_0x9027('0x15')]=_0x579868[_0x9027('0x15')];if(_0x579868[_0x9027('0x16')]){_0x113864['xoauth2']=utils['generateXOAuth2Token'](_0x579868['CloudProvider'][_0x9027('0x17')],_0x579868['user']);}else{_0x113864['password']=_0x579868[_0x9027('0x18')];}}else{if(_0x579868[_0x9027('0x19')]){_0x113864['user']=_0x579868[_0x9027('0x15')];_0x113864[_0x9027('0x18')]=_0x579868[_0x9027('0x18')];}}_0x113864[_0x9027('0x8')]=_0xba7428||_0x579868[_0x9027('0x15')]||'anonymous@xcally.com';_0x113864[_0x9027('0x1a')]=_0x579868[_0x9027('0x1a')];_0x113864['port']=_0x579868[_0x9027('0x1b')]||0x3e1;if(_0x579868['tls']){_0x113864[_0x9027('0x1c')]=!![];}else{_0x113864[_0x9027('0x1c')]=![];_0x113864[_0x9027('0x1d')]=_0x9027('0x1e');}return _0x113864;};EmailImap['prototype'][_0x9027('0x1f')]=function(){this[_0x9027('0xa')]['openBox'](this[_0x9027('0x9')],![],this['handleOpenBox'][_0x9027('0xb')](this));};EmailImap[_0x9027('0x11')][_0x9027('0x20')]=function(_0x454644,_0x410f52){if(_0x454644){this[_0x9027('0xf')](_0x454644,!![]);}else{this[_0x9027('0x21')](_0x9027('0x22'),_0x410f52);this['readUnseen']();}};EmailImap['prototype'][_0x9027('0x23')]=function(_0x51c151){if(_0x51c151){this[_0x9027('0xf')](_0x51c151,!![]);}else{this['imap']['end']();}};EmailImap[_0x9027('0x11')][_0x9027('0x24')]=function(){this[_0x9027('0xa')][_0x9027('0x25')]([_0x9027('0x26')],this[_0x9027('0x27')]['bind'](this));};EmailImap[_0x9027('0x11')][_0x9027('0x27')]=function(_0x280ed,_0x45adca){if(_0x280ed){this[_0x9027('0xf')](_0x280ed,!![]);}else{if(_0x45adca['length']){try{var _0x429f7f=this['imap'][_0x9027('0x28')](_0x45adca,{'bodies':'','markSeen':!![]});_0x429f7f['on'](_0x9027('0x29'),this[_0x9027('0x2a')][_0x9027('0xb')](this));_0x429f7f[_0x9027('0xc')]('error',this[_0x9027('0x2b')][_0x9027('0xb')](this));_0x429f7f[_0x9027('0xc')](_0x9027('0xd'),this[_0x9027('0x2c')][_0x9027('0xb')](this));}catch(_0x21982e){logger['error'](_0x21982e);}}else{this['imap'][_0x9027('0x2d')](!![],this[_0x9027('0x23')]['bind'](this));}}};EmailImap[_0x9027('0x11')][_0x9027('0x2a')]=function(_0x2c8b2a){_0x2c8b2a['on'](_0x9027('0x2e'),this[_0x9027('0x2f')][_0x9027('0xb')](this));_0x2c8b2a[_0x9027('0xc')](_0x9027('0x30'),this[_0x9027('0x31')][_0x9027('0xb')](this));_0x2c8b2a[_0x9027('0xc')](_0x9027('0xd'),this['handleMessageEnd'][_0x9027('0xb')](this));};EmailImap[_0x9027('0x11')][_0x9027('0x2f')]=function(_0x4f8456){simpleParser(_0x4f8456,{'Iconv':Iconv},this['handleSimpleParser']['bind'](this));};EmailImap[_0x9027('0x11')][_0x9027('0x31')]=function(){};EmailImap['prototype'][_0x9027('0x32')]=function(){};EmailImap[_0x9027('0x11')][_0x9027('0x33')]=function(_0x4adccf,_0x175171){if(_0x4adccf){logger[_0x9027('0xe')](_0x4adccf);}else{var _0x4738b1=this;utils['parser'](this[_0x9027('0x34')],_0x175171,function(_0x9be79f){_0x4738b1[_0x9027('0x21')](_0x9027('0x8'),_0x9be79f);});}};EmailImap[_0x9027('0x11')][_0x9027('0x2c')]=function(){this[_0x9027('0xa')][_0x9027('0x2d')](!![],this['handleCloseBox'][_0x9027('0xb')](this));};EmailImap['prototype'][_0x9027('0x2b')]=function(_0x2979c6){this['handleError'](_0x2979c6);this['imap'][_0x9027('0x2d')](!![],this[_0x9027('0x23')][_0x9027('0xb')](this));};EmailImap[_0x9027('0x11')][_0x9027('0x35')]=function(){this[_0x9027('0x21')](_0x9027('0xd'));};EmailImap['prototype']['handleError']=function(_0x5071a0,_0x4757c6){logger[_0x9027('0xe')](_0x9027('0xf'),this[_0x9027('0x34')][_0x9027('0x8')],JSON[_0x9027('0x36')](_0x5071a0));var _0x447539='';if(_0x5071a0){if(_0x5071a0['textCode']){_0x447539=_0x5071a0[_0x9027('0x37')];}else{for(var _0x31f548 in _0x5071a0){if(_0x5071a0[_0x9027('0x38')](_0x31f548)){_0x447539+=_[_0x9027('0x39')](_0x31f548)+':\x20'+_0x5071a0[_0x31f548]+'\x0a';}}}}if(_0x4757c6){this[_0x9027('0xa')][_0x9027('0xd')]();}else{this[_0x9027('0x21')](_0x9027('0xe'),_0x447539);this[_0x9027('0x21')](_0x9027('0xd'));}};module[_0x9027('0x3a')]=EmailImap;