3f141f0a06e937b6d404c4e3d679170b4a325923
[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 _0xb0f8=['handleReady','handleEnd','error','bind','connect','inherits','prototype','getAccount','connTimeout','authTimeout','isNil','service','authentication','user','password','host','port','tls','autotls','getHost','openBox','handleOpenBox','handleError','box','readUnseen','search','UNSEEN','handleReadUnseen','length','fetch','handleFetchMessage','handleFetchError','end','handleFetchEnd','closeBox','handleCloseBox','handleMessageBody','handleMessageEnd','handleMessageAttributes','handleSimpleParser','parser','stringify','textCode','hasOwnProperty','upperFirst','emit','exports','util','lodash','randomstring','simpleParser','Iconv','events','../../../config/logger','email','mailbox','account','imap','once','ready'];(function(_0x64d8bd,_0x5a8313){var _0x337e0d=function(_0x158638){while(--_0x158638){_0x64d8bd['push'](_0x64d8bd['shift']());}};_0x337e0d(++_0x5a8313);}(_0xb0f8,0x15b));var _0x8b0f=function(_0x4b8ba0,_0x468e84){_0x4b8ba0=_0x4b8ba0-0x0;var _0x31fe29=_0xb0f8[_0x4b8ba0];return _0x31fe29;};'use strict';var fs=require('fs');var path=require('path');var util=require(_0x8b0f('0x0'));var Imap=require('imap');var _=require(_0x8b0f('0x1'));var rs=require(_0x8b0f('0x2'));var simpleParser=require('mailparser')[_0x8b0f('0x3')];var Iconv=require('iconv')[_0x8b0f('0x4')];var EventEmitter=require(_0x8b0f('0x5'))['EventEmitter'];var utils=require('./utils');var logger=require(_0x8b0f('0x6'))(_0x8b0f('0x7'));function EmailImap(_0x2ab961,_0x1e8a68){this[_0x8b0f('0x8')]='INBOX';this[_0x8b0f('0x9')]=this['getAccount'](_0x2ab961,_0x1e8a68);this[_0x8b0f('0xa')]=new Imap(this[_0x8b0f('0x9')]);this[_0x8b0f('0xa')][_0x8b0f('0xb')](_0x8b0f('0xc'),this[_0x8b0f('0xd')]['bind'](this));this[_0x8b0f('0xa')][_0x8b0f('0xb')]('end',this[_0x8b0f('0xe')]['bind'](this));this[_0x8b0f('0xa')]['on'](_0x8b0f('0xf'),this['handleError'][_0x8b0f('0x10')](this));this[_0x8b0f('0xa')][_0x8b0f('0x11')]();}util[_0x8b0f('0x12')](EmailImap,EventEmitter);EmailImap[_0x8b0f('0x13')][_0x8b0f('0x14')]=function(_0xdf8f9,_0x1f2771){var _0x41c90e={'connTimeout':_0xdf8f9[_0x8b0f('0x15')]?_0xdf8f9['connTimeout']*0x3e8:0x2710,'authTimeout':_0xdf8f9[_0x8b0f('0x16')]?_0xdf8f9[_0x8b0f('0x16')]*0x3e8:0x1388};if(_[_0x8b0f('0x17')](_0xdf8f9[_0x8b0f('0x18')])){if(_0xdf8f9[_0x8b0f('0x19')]){_0x41c90e[_0x8b0f('0x1a')]=_0xdf8f9['user'];_0x41c90e[_0x8b0f('0x1b')]=_0xdf8f9['password'];}_0x41c90e[_0x8b0f('0x1c')]=_0xdf8f9[_0x8b0f('0x1c')];_0x41c90e[_0x8b0f('0x1d')]=_0xdf8f9[_0x8b0f('0x1d')]||0x3e1;if(_0xdf8f9[_0x8b0f('0x1e')]){_0x41c90e[_0x8b0f('0x1e')]=!![];}else{_0x41c90e[_0x8b0f('0x1e')]=![];_0x41c90e[_0x8b0f('0x1f')]='required';}}else{_0x41c90e['user']=_0xdf8f9[_0x8b0f('0x1a')];_0x41c90e[_0x8b0f('0x1b')]=_0xdf8f9[_0x8b0f('0x1b')];_0x41c90e['host']=utils[_0x8b0f('0x20')](_0xdf8f9['service']);_0x41c90e[_0x8b0f('0x1d')]=0x3e1;_0x41c90e[_0x8b0f('0x1e')]=!![];}_0x41c90e[_0x8b0f('0x7')]=_0x1f2771||_0xdf8f9[_0x8b0f('0x1a')]||'anonymous@xcally.com';return _0x41c90e;};EmailImap['prototype'][_0x8b0f('0xd')]=function(){this[_0x8b0f('0xa')][_0x8b0f('0x21')](this[_0x8b0f('0x8')],![],this[_0x8b0f('0x22')][_0x8b0f('0x10')](this));};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x22')]=function(_0x48b82f,_0x4c884b){if(_0x48b82f){this[_0x8b0f('0x23')](_0x48b82f,!![]);}else{this['emit'](_0x8b0f('0x24'),_0x4c884b);this['readUnseen']();}};EmailImap['prototype']['handleCloseBox']=function(_0x50912f){if(_0x50912f){this[_0x8b0f('0x23')](_0x50912f,!![]);}else{this[_0x8b0f('0xa')]['end']();}};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x25')]=function(){this[_0x8b0f('0xa')][_0x8b0f('0x26')]([_0x8b0f('0x27')],this[_0x8b0f('0x28')]['bind'](this));};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x28')]=function(_0x5d7a8e,_0x70a18){if(_0x5d7a8e){this[_0x8b0f('0x23')](_0x5d7a8e,!![]);}else{if(_0x70a18[_0x8b0f('0x29')]){try{var _0xb3c118=this[_0x8b0f('0xa')][_0x8b0f('0x2a')](_0x70a18,{'bodies':'','markSeen':!![]});_0xb3c118['on']('message',this[_0x8b0f('0x2b')][_0x8b0f('0x10')](this));_0xb3c118[_0x8b0f('0xb')](_0x8b0f('0xf'),this[_0x8b0f('0x2c')][_0x8b0f('0x10')](this));_0xb3c118[_0x8b0f('0xb')](_0x8b0f('0x2d'),this[_0x8b0f('0x2e')]['bind'](this));}catch(_0x28eb24){logger[_0x8b0f('0xf')](_0x28eb24);}}else{this[_0x8b0f('0xa')][_0x8b0f('0x2f')](!![],this[_0x8b0f('0x30')]['bind'](this));}}};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x2b')]=function(_0x2a0019,_0x3beebd){_0x2a0019['on']('body',this[_0x8b0f('0x31')][_0x8b0f('0x10')](this));_0x2a0019[_0x8b0f('0xb')]('attributes',this['handleMessageAttributes']['bind'](this));_0x2a0019[_0x8b0f('0xb')](_0x8b0f('0x2d'),this[_0x8b0f('0x32')][_0x8b0f('0x10')](this));};EmailImap['prototype'][_0x8b0f('0x31')]=function(_0x4c97b1,_0x3fd00b){simpleParser(_0x4c97b1,{'Iconv':Iconv},this['handleSimpleParser'][_0x8b0f('0x10')](this));};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x33')]=function(_0x3b24d5){};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x32')]=function(){};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x34')]=function(_0x19516c,_0x4e9029){if(_0x19516c){logger[_0x8b0f('0xf')](_0x19516c);}else{var _0xc94f7a=this;utils[_0x8b0f('0x35')](this[_0x8b0f('0x9')],_0x4e9029,function(_0x1851fb){_0xc94f7a['emit']('email',_0x1851fb);});}};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x2e')]=function(){this['imap']['closeBox'](!![],this[_0x8b0f('0x30')][_0x8b0f('0x10')](this));};EmailImap[_0x8b0f('0x13')][_0x8b0f('0x2c')]=function(_0x4b065b){this['handleError'](_0x4b065b);this[_0x8b0f('0xa')][_0x8b0f('0x2f')](!![],this[_0x8b0f('0x30')]['bind'](this));};EmailImap[_0x8b0f('0x13')][_0x8b0f('0xe')]=function(){this['emit']('end');};EmailImap['prototype'][_0x8b0f('0x23')]=function(_0x93b4c,_0x4f9f5e){logger[_0x8b0f('0xf')](_0x8b0f('0x23'),this[_0x8b0f('0x9')][_0x8b0f('0x7')],JSON[_0x8b0f('0x36')](_0x93b4c));var _0x254780='';if(_0x93b4c){if(_0x93b4c[_0x8b0f('0x37')]){_0x254780=_0x93b4c[_0x8b0f('0x37')];}else{for(var _0x54da0f in _0x93b4c){if(_0x93b4c[_0x8b0f('0x38')](_0x54da0f)){_0x254780+=_[_0x8b0f('0x39')](_0x54da0f)+':\x20'+_0x93b4c[_0x54da0f]+'\x0a';}}}}if(_0x4f9f5e){this[_0x8b0f('0xa')][_0x8b0f('0x2d')]();}else{this[_0x8b0f('0x3a')]('error',_0x254780);this['emit']('end');}};module[_0x8b0f('0x3b')]=EmailImap;