Built motion from commit (unavailable).|2.3.4
[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 _0x524e=['sendMail','send','verifySmtp','Smtp','verify','verified','exports','bluebird','md5','ioredis','../../../config/environment','../../../config/logger','email','redis','defaults','localhost','./imap','./smtp','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','name','key','active','Imap','messageStatus','total','isNil','stringify','hasOwnProperty','handleImapBox','bind','handleImapEmail','end','handleImapEnd','error','status','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','lastName','notify','then','catch','lastSync'];(function(_0x4cf98e,_0xba4b79){var _0x1623ac=function(_0x459bc0){while(--_0x459bc0){_0x4cf98e['push'](_0x4cf98e['shift']());}};_0x1623ac(++_0xba4b79);}(_0x524e,0x15e));var _0xe524=function(_0xefa016,_0xf36638){_0xefa016=_0xefa016-0x0;var _0x1726b1=_0x524e[_0xefa016];return _0x1726b1;};'use strict';var _=require('lodash');var moment=require('moment');var BPromise=require(_0xe524('0x0'));var md5=require(_0xe524('0x1'));var Redis=require(_0xe524('0x2'));var config=require(_0xe524('0x3'));var logger=require(_0xe524('0x4'))(_0xe524('0x5'));config[_0xe524('0x6')]=_[_0xe524('0x7')](config[_0xe524('0x6')],{'host':_0xe524('0x8'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xe524('0x6')]));var EmailImap=require(_0xe524('0x9'));var EmailSmtp=require(_0xe524('0xa'));var mailAccount=require('../rpc/mailAccount');var md5Accounts={};function EmailChannel(_0x384935){logger[_0xe524('0xb')](_0xe524('0xc'));this[_0xe524('0xd')]=_0x384935[_0xe524('0xd')];this[_0xe524('0xe')]={};this[_0xe524('0xf')]=new EmailSmtp();}EmailChannel[_0xe524('0x10')][_0xe524('0x11')]=function(_0x1641ec){var _0x1e4459=this[_0xe524('0xd')][_0x1641ec];var _0x1184df={'id':_0x1e4459['id'],'name':_0x1e4459[_0xe524('0x12')],'key':_0x1e4459[_0xe524('0x13')],'active':_0x1e4459[_0xe524('0x14')],'Imap':{'status':_0x1e4459['Imap']['status'],'messageStatus':_0x1e4459[_0xe524('0x15')][_0xe524('0x16')],'total':_0x1e4459[_0xe524('0x15')][_0xe524('0x17')]}};if(_[_0xe524('0x18')](md5Accounts[_0x1641ec])){md5Accounts[_0x1641ec]={};}var _0x57c1b6=md5(JSON[_0xe524('0x19')](_0x1184df));if(md5Accounts[_0x1641ec]['md5']!==_0x57c1b6){md5Accounts[_0x1641ec][_0xe524('0x1')]=_0x57c1b6;io[_0xe524('0x11')]('mailAccount:update',_0x1184df);}};EmailChannel[_0xe524('0x10')]['refresh']=function(_0x109795){for(var _0x538754 in _0x109795){if(_0x109795[_0xe524('0x1a')](_0x538754)){if(_0x109795[_0x538754]['Imap']&&_0x109795[_0x538754]['active']){var _0x5a5c0d=_0x109795[_0x538754][_0xe524('0x15')];if(!this['imapAccounts'][_0xe524('0x1a')](_0x5a5c0d['id'])){this[_0xe524('0xe')][_0x5a5c0d['id']]=new EmailImap(_0x5a5c0d,_0x109795[_0x538754][_0xe524('0x5')]);this[_0xe524('0xe')][_0x5a5c0d['id']]['on']('box',this[_0xe524('0x1b')][_0xe524('0x1c')](this,_0x538754));this['imapAccounts'][_0x5a5c0d['id']]['on'](_0xe524('0x5'),this[_0xe524('0x1d')][_0xe524('0x1c')](this,_0x538754));this[_0xe524('0xe')][_0x5a5c0d['id']]['on'](_0xe524('0x1e'),this[_0xe524('0x1f')][_0xe524('0x1c')](this,_0x538754));this[_0xe524('0xe')][_0x5a5c0d['id']]['on'](_0xe524('0x20'),this['handleImapError'][_0xe524('0x1c')](this,_0x538754));}}else{if(_['isNil'](this['mailAccounts'][_0x538754][_0xe524('0x15')])){this[_0xe524('0xd')][_0x538754][_0xe524('0x15')]={};}this[_0xe524('0xd')][_0x538754]['Imap'][_0xe524('0x21')]='disabled';this[_0xe524('0xd')][_0x538754][_0xe524('0x15')][_0xe524('0x16')]='';this[_0xe524('0x11')](_0x538754);}}}};EmailChannel[_0xe524('0x10')][_0xe524('0x1b')]=function(_0x2141db,_0x583cc3){if(this['mailAccounts'][_0x2141db]){this[_0xe524('0xd')][_0x2141db][_0xe524('0x15')][_0xe524('0x21')]=_0xe524('0x22');this[_0xe524('0xd')][_0x2141db]['Imap']['messageStatus']=_0x583cc3[_0xe524('0x12')];if(_0x583cc3[_0xe524('0x23')]){this[_0xe524('0xd')][_0x2141db][_0xe524('0x15')][_0xe524('0x17')]=_0x583cc3['messages'][_0xe524('0x17')];}return this[_0xe524('0x11')](_0x2141db);}};EmailChannel['prototype']['handleImapEmail']=function(_0x20688e,_0x376324){logger['info'](_0xe524('0x24'),_0x376324['firstName'],_0x376324[_0xe524('0x25')],_0x376324['from'],_0x20688e);if(this[_0xe524('0xd')][_0x20688e]){return mailAccount[_0xe524('0x26')](_0x20688e,_0x376324)[_0xe524('0x27')](function(_0x5600d2){if(_0x5600d2){logger[_0xe524('0xb')]('Mail\x20accepted\x20from\x20routing');}})[_0xe524('0x28')](function(_0x16eff9){logger[_0xe524('0x20')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0xe524('0x19')](_0x16eff9));});}};EmailChannel[_0xe524('0x10')]['handleImapEnd']=function(_0x27817e){if(this[_0xe524('0xd')][_0x27817e]&&this[_0xe524('0xd')][_0x27817e][_0xe524('0x15')]){if(this['imapAccounts'][this[_0xe524('0xd')][_0x27817e][_0xe524('0x15')]['id']]){delete this[_0xe524('0xe')][this[_0xe524('0xd')][_0x27817e][_0xe524('0x15')]['id']];}this[_0xe524('0xd')][_0x27817e][_0xe524('0x15')][_0xe524('0x29')]=moment();this['emit'](_0x27817e);}};EmailChannel[_0xe524('0x10')]['handleImapError']=function(_0x1d463b,_0x53eeee){if(this['mailAccounts'][_0x1d463b]){this[_0xe524('0xd')][_0x1d463b]['Imap'][_0xe524('0x21')]=_0xe524('0x20');this[_0xe524('0xd')][_0x1d463b][_0xe524('0x15')][_0xe524('0x16')]=_0x53eeee;return this[_0xe524('0x11')](_0x1d463b);}};EmailChannel[_0xe524('0x10')][_0xe524('0x2a')]=function(_0x5e8e64,_0x311aec){return this[_0xe524('0xf')][_0xe524('0x2b')](_0x5e8e64,_0x311aec);};EmailChannel[_0xe524('0x10')][_0xe524('0x2c')]=function(_0x3885f6){var _0x1d9648=this;if(this[_0xe524('0xd')][_0x3885f6['id']]){if(_['isNil'](this[_0xe524('0xd')][_0x3885f6['id']][_0xe524('0x2d')])){this[_0xe524('0xd')][_0x3885f6['id']]['Smtp']={};}this[_0xe524('0xd')][_0x3885f6['id']]['Smtp'][_0xe524('0x29')]=moment();return new BPromise(function(_0x217e85,_0x43c314){return _0x1d9648[_0xe524('0xf')][_0xe524('0x2e')](_0x3885f6)[_0xe524('0x27')](function(_0xbeb2a9){_0x1d9648['mailAccounts'][_0x3885f6['id']][_0xe524('0x2d')][_0xe524('0x21')]=_0xe524('0x2f');_0x1d9648[_0xe524('0xd')][_0x3885f6['id']][_0xe524('0x2d')][_0xe524('0x16')]='';_0x1d9648['emit'](_0x3885f6['id']);_0x217e85(_0xbeb2a9);})[_0xe524('0x28')](function(_0x2982a0){_0x1d9648[_0xe524('0xd')][_0x3885f6['id']]['Smtp'][_0xe524('0x21')]='error';_0x1d9648['mailAccounts'][_0x3885f6['id']][_0xe524('0x2d')][_0xe524('0x16')]=_0x2982a0;_0x1d9648['emit'](_0x3885f6['id']);_0x43c314(_0x2982a0);});});}return this[_0xe524('0xf')][_0xe524('0x2e')](_0x3885f6);};module[_0xe524('0x30')]=EmailChannel;