Built motion from commit (unavailable).|2.3.9
[motion2.git] / server / services / email / lib / email.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 _0x6d11=['mailAccount:update','prototype','hasOwnProperty','bind','end','handleImapEnd','error','handleImapError','isNil','disabled','handleImapBox','connected','messages','info','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','notify','then','Mail\x20accepted\x20from\x20routing','catch','lastSync','send','verifySmtp','Smtp','verify','verified','exports','lodash','moment','bluebird','ioredis','../../../config/logger','email','redis','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','mailAccounts','imapAccounts','smtp','emit','name','key','active','status','Imap','messageStatus','total','stringify','md5'];(function(_0x332099,_0x3302be){var _0x469f0c=function(_0x256a86){while(--_0x256a86){_0x332099['push'](_0x332099['shift']());}};_0x469f0c(++_0x3302be);}(_0x6d11,0xbf));var _0x16d1=function(_0x3dc14c,_0x45643e){_0x3dc14c=_0x3dc14c-0x0;var _0x5deccc=_0x6d11[_0x3dc14c];return _0x5deccc;};'use strict';var _=require(_0x16d1('0x0'));var moment=require(_0x16d1('0x1'));var BPromise=require(_0x16d1('0x2'));var md5=require('md5');var Redis=require(_0x16d1('0x3'));var config=require('../../../config/environment');var logger=require(_0x16d1('0x4'))(_0x16d1('0x5'));config['redis']=_['defaults'](config[_0x16d1('0x6')],{'host':_0x16d1('0x7'),'port':0x18eb});var io=require(_0x16d1('0x8'))(new Redis(config[_0x16d1('0x6')]));var EmailImap=require(_0x16d1('0x9'));var EmailSmtp=require(_0x16d1('0xa'));var mailAccount=require(_0x16d1('0xb'));var md5Accounts={};function EmailChannel(_0x5d0e3f){logger['info']('start\x20email\x20channel');this[_0x16d1('0xc')]=_0x5d0e3f[_0x16d1('0xc')];this[_0x16d1('0xd')]={};this[_0x16d1('0xe')]=new EmailSmtp();}EmailChannel['prototype'][_0x16d1('0xf')]=function(_0x2c360f){var _0x159539=this[_0x16d1('0xc')][_0x2c360f];var _0x28188f={'id':_0x159539['id'],'name':_0x159539[_0x16d1('0x10')],'key':_0x159539[_0x16d1('0x11')],'active':_0x159539[_0x16d1('0x12')],'Imap':{'status':_0x159539['Imap'][_0x16d1('0x13')],'messageStatus':_0x159539[_0x16d1('0x14')][_0x16d1('0x15')],'total':_0x159539[_0x16d1('0x14')][_0x16d1('0x16')]}};if(_['isNil'](md5Accounts[_0x2c360f])){md5Accounts[_0x2c360f]={};}var _0xc034b7=md5(JSON[_0x16d1('0x17')](_0x28188f));if(md5Accounts[_0x2c360f][_0x16d1('0x18')]!==_0xc034b7){md5Accounts[_0x2c360f][_0x16d1('0x18')]=_0xc034b7;io[_0x16d1('0xf')](_0x16d1('0x19'),_0x28188f);}};EmailChannel[_0x16d1('0x1a')]['refresh']=function(_0x169e76){for(var _0x2b399c in _0x169e76){if(_0x169e76['hasOwnProperty'](_0x2b399c)){if(_0x169e76[_0x2b399c]['Imap']&&_0x169e76[_0x2b399c][_0x16d1('0x12')]){var _0x47c245=_0x169e76[_0x2b399c][_0x16d1('0x14')];if(!this['imapAccounts'][_0x16d1('0x1b')](_0x47c245['id'])){this['imapAccounts'][_0x47c245['id']]=new EmailImap(_0x47c245,_0x169e76[_0x2b399c][_0x16d1('0x5')]);this[_0x16d1('0xd')][_0x47c245['id']]['on']('box',this['handleImapBox']['bind'](this,_0x2b399c));this['imapAccounts'][_0x47c245['id']]['on'](_0x16d1('0x5'),this['handleImapEmail'][_0x16d1('0x1c')](this,_0x2b399c));this[_0x16d1('0xd')][_0x47c245['id']]['on'](_0x16d1('0x1d'),this[_0x16d1('0x1e')]['bind'](this,_0x2b399c));this[_0x16d1('0xd')][_0x47c245['id']]['on'](_0x16d1('0x1f'),this[_0x16d1('0x20')]['bind'](this,_0x2b399c));}}else{if(_[_0x16d1('0x21')](this[_0x16d1('0xc')][_0x2b399c][_0x16d1('0x14')])){this[_0x16d1('0xc')][_0x2b399c][_0x16d1('0x14')]={};}this[_0x16d1('0xc')][_0x2b399c][_0x16d1('0x14')][_0x16d1('0x13')]=_0x16d1('0x22');this[_0x16d1('0xc')][_0x2b399c][_0x16d1('0x14')][_0x16d1('0x15')]='';this[_0x16d1('0xf')](_0x2b399c);}}}};EmailChannel[_0x16d1('0x1a')][_0x16d1('0x23')]=function(_0x2b8403,_0x491f65){if(this[_0x16d1('0xc')][_0x2b8403]){this[_0x16d1('0xc')][_0x2b8403][_0x16d1('0x14')]['status']=_0x16d1('0x24');this[_0x16d1('0xc')][_0x2b8403][_0x16d1('0x14')][_0x16d1('0x15')]=_0x491f65[_0x16d1('0x10')];if(_0x491f65[_0x16d1('0x25')]){this[_0x16d1('0xc')][_0x2b8403]['Imap'][_0x16d1('0x16')]=_0x491f65[_0x16d1('0x25')][_0x16d1('0x16')];}return this[_0x16d1('0xf')](_0x2b8403);}};EmailChannel['prototype']['handleImapEmail']=function(_0x2f16ee,_0x4ab3d4){logger[_0x16d1('0x26')](_0x16d1('0x27'),_0x4ab3d4[_0x16d1('0x28')],_0x4ab3d4[_0x16d1('0x29')],_0x4ab3d4[_0x16d1('0x2a')],_0x2f16ee);if(this[_0x16d1('0xc')][_0x2f16ee]){return mailAccount[_0x16d1('0x2b')](_0x2f16ee,_0x4ab3d4)[_0x16d1('0x2c')](function(_0x366a28){if(_0x366a28){logger[_0x16d1('0x26')](_0x16d1('0x2d'));}})[_0x16d1('0x2e')](function(_0x5f12a8){logger[_0x16d1('0x1f')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0x16d1('0x17')](_0x5f12a8));});}};EmailChannel[_0x16d1('0x1a')][_0x16d1('0x1e')]=function(_0x3e9bac){if(this[_0x16d1('0xc')][_0x3e9bac]&&this[_0x16d1('0xc')][_0x3e9bac]['Imap']){if(this['imapAccounts'][this['mailAccounts'][_0x3e9bac][_0x16d1('0x14')]['id']]){delete this[_0x16d1('0xd')][this[_0x16d1('0xc')][_0x3e9bac][_0x16d1('0x14')]['id']];}this['mailAccounts'][_0x3e9bac][_0x16d1('0x14')][_0x16d1('0x2f')]=moment();this['emit'](_0x3e9bac);}};EmailChannel['prototype']['handleImapError']=function(_0x2f49b0,_0xb491bd){if(this[_0x16d1('0xc')][_0x2f49b0]){this['mailAccounts'][_0x2f49b0][_0x16d1('0x14')][_0x16d1('0x13')]=_0x16d1('0x1f');this[_0x16d1('0xc')][_0x2f49b0][_0x16d1('0x14')][_0x16d1('0x15')]=_0xb491bd;return this[_0x16d1('0xf')](_0x2f49b0);}};EmailChannel['prototype']['sendMail']=function(_0x502d9b,_0x22fa2e){return this[_0x16d1('0xe')][_0x16d1('0x30')](_0x502d9b,_0x22fa2e);};EmailChannel[_0x16d1('0x1a')][_0x16d1('0x31')]=function(_0x2740a7){var _0x364c39=this;if(this[_0x16d1('0xc')][_0x2740a7['id']]){if(_['isNil'](this[_0x16d1('0xc')][_0x2740a7['id']]['Smtp'])){this['mailAccounts'][_0x2740a7['id']][_0x16d1('0x32')]={};}this[_0x16d1('0xc')][_0x2740a7['id']][_0x16d1('0x32')][_0x16d1('0x2f')]=moment();return new BPromise(function(_0x20d374,_0x1994a5){return _0x364c39['smtp'][_0x16d1('0x33')](_0x2740a7)['then'](function(_0x4833f7){_0x364c39['mailAccounts'][_0x2740a7['id']]['Smtp'][_0x16d1('0x13')]=_0x16d1('0x34');_0x364c39[_0x16d1('0xc')][_0x2740a7['id']][_0x16d1('0x32')]['messageStatus']='';_0x364c39[_0x16d1('0xf')](_0x2740a7['id']);_0x20d374(_0x4833f7);})['catch'](function(_0x1dd886){_0x364c39[_0x16d1('0xc')][_0x2740a7['id']][_0x16d1('0x32')][_0x16d1('0x13')]=_0x16d1('0x1f');_0x364c39[_0x16d1('0xc')][_0x2740a7['id']][_0x16d1('0x32')][_0x16d1('0x15')]=_0x1dd886;_0x364c39[_0x16d1('0xf')](_0x2740a7['id']);_0x1994a5(_0x1dd886);});});}return this[_0x16d1('0xe')][_0x16d1('0x33')](_0x2740a7);};module[_0x16d1('0x35')]=EmailChannel;