Built motion from commit (unavailable).|2.5.31
[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 _0xa265=['sendMail','smtp','send','Smtp','verify','verified','bluebird','ioredis','../../../config/environment','../../../config/logger','email','defaults','localhost','./imap','./smtp','../rpc/mailAccount','info','mailAccounts','imapAccounts','prototype','emit','name','key','active','status','Imap','messageStatus','isNil','md5','mailAccount:update','refresh','hasOwnProperty','box','bind','handleImapEmail','end','handleImapEnd','error','disabled','handleImapBox','connected','total','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','then','Mail\x20accepted\x20from\x20routing','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','handleImapError'];(function(_0x406e46,_0x5799cb){var _0x4a3742=function(_0x1fa435){while(--_0x1fa435){_0x406e46['push'](_0x406e46['shift']());}};_0x4a3742(++_0x5799cb);}(_0xa265,0x16b));var _0x5a26=function(_0x294df2,_0x5c96e6){_0x294df2=_0x294df2-0x0;var _0x2ef934=_0xa265[_0x294df2];return _0x2ef934;};'use strict';var _=require('lodash');var moment=require('moment');var BPromise=require(_0x5a26('0x0'));var md5=require('md5');var Redis=require(_0x5a26('0x1'));var config=require(_0x5a26('0x2'));var logger=require(_0x5a26('0x3'))(_0x5a26('0x4'));config['redis']=_[_0x5a26('0x5')](config['redis'],{'host':_0x5a26('0x6'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config['redis']));var EmailImap=require(_0x5a26('0x7'));var EmailSmtp=require(_0x5a26('0x8'));var mailAccount=require(_0x5a26('0x9'));var md5Accounts={};function EmailChannel(_0x2b1fa0){logger[_0x5a26('0xa')]('start\x20email\x20channel');this['mailAccounts']=_0x2b1fa0[_0x5a26('0xb')];this[_0x5a26('0xc')]={};this['smtp']=new EmailSmtp();}EmailChannel[_0x5a26('0xd')][_0x5a26('0xe')]=function(_0x4bf027){var _0x1aeda5=this['mailAccounts'][_0x4bf027];var _0x333f6c={'id':_0x1aeda5['id'],'name':_0x1aeda5[_0x5a26('0xf')],'key':_0x1aeda5[_0x5a26('0x10')],'active':_0x1aeda5[_0x5a26('0x11')],'Imap':{'status':_0x1aeda5['Imap'][_0x5a26('0x12')],'messageStatus':_0x1aeda5[_0x5a26('0x13')][_0x5a26('0x14')],'total':_0x1aeda5[_0x5a26('0x13')]['total']}};if(_[_0x5a26('0x15')](md5Accounts[_0x4bf027])){md5Accounts[_0x4bf027]={};}var _0x1a8160=md5(JSON['stringify'](_0x333f6c));if(md5Accounts[_0x4bf027][_0x5a26('0x16')]!==_0x1a8160){md5Accounts[_0x4bf027][_0x5a26('0x16')]=_0x1a8160;io['emit'](_0x5a26('0x17'),_0x333f6c);}};EmailChannel[_0x5a26('0xd')][_0x5a26('0x18')]=function(_0x4bbe7c){for(var _0x38bbad in _0x4bbe7c){if(_0x4bbe7c[_0x5a26('0x19')](_0x38bbad)){if(_0x4bbe7c[_0x38bbad][_0x5a26('0x13')]&&_0x4bbe7c[_0x38bbad][_0x5a26('0x11')]){var _0x2d1498=_0x4bbe7c[_0x38bbad]['Imap'];if(!this[_0x5a26('0xc')][_0x5a26('0x19')](_0x2d1498['id'])){this[_0x5a26('0xc')][_0x2d1498['id']]=new EmailImap(_0x2d1498,_0x4bbe7c[_0x38bbad][_0x5a26('0x4')]);this['imapAccounts'][_0x2d1498['id']]['on'](_0x5a26('0x1a'),this['handleImapBox'][_0x5a26('0x1b')](this,_0x38bbad));this[_0x5a26('0xc')][_0x2d1498['id']]['on'](_0x5a26('0x4'),this[_0x5a26('0x1c')][_0x5a26('0x1b')](this,_0x38bbad));this[_0x5a26('0xc')][_0x2d1498['id']]['on'](_0x5a26('0x1d'),this[_0x5a26('0x1e')][_0x5a26('0x1b')](this,_0x38bbad));this[_0x5a26('0xc')][_0x2d1498['id']]['on'](_0x5a26('0x1f'),this['handleImapError'][_0x5a26('0x1b')](this,_0x38bbad));}}else{if(_[_0x5a26('0x15')](this[_0x5a26('0xb')][_0x38bbad]['Imap'])){this[_0x5a26('0xb')][_0x38bbad][_0x5a26('0x13')]={};}this[_0x5a26('0xb')][_0x38bbad][_0x5a26('0x13')][_0x5a26('0x12')]=_0x5a26('0x20');this[_0x5a26('0xb')][_0x38bbad][_0x5a26('0x13')][_0x5a26('0x14')]='';this[_0x5a26('0xe')](_0x38bbad);}}}};EmailChannel['prototype'][_0x5a26('0x21')]=function(_0x26c1bc,_0x512514){if(this[_0x5a26('0xb')][_0x26c1bc]){this[_0x5a26('0xb')][_0x26c1bc][_0x5a26('0x13')][_0x5a26('0x12')]=_0x5a26('0x22');this[_0x5a26('0xb')][_0x26c1bc][_0x5a26('0x13')][_0x5a26('0x14')]=_0x512514[_0x5a26('0xf')];if(_0x512514['messages']){this[_0x5a26('0xb')][_0x26c1bc][_0x5a26('0x13')][_0x5a26('0x23')]=_0x512514['messages']['total'];}return this['emit'](_0x26c1bc);}};EmailChannel[_0x5a26('0xd')]['handleImapEmail']=function(_0x525806,_0x35fd78){logger[_0x5a26('0xa')](_0x5a26('0x24'),_0x35fd78[_0x5a26('0x25')],_0x35fd78[_0x5a26('0x26')],_0x35fd78['from'],_0x525806);if(this[_0x5a26('0xb')][_0x525806]){return mailAccount['notify'](_0x525806,_0x35fd78)[_0x5a26('0x27')](function(_0x697d13){if(_0x697d13){logger[_0x5a26('0xa')](_0x5a26('0x28'));}})[_0x5a26('0x29')](function(_0x429220){logger[_0x5a26('0x1f')](_0x5a26('0x2a'),JSON['stringify'](_0x429220));});}};EmailChannel[_0x5a26('0xd')][_0x5a26('0x1e')]=function(_0x57bdc4){if(this[_0x5a26('0xb')][_0x57bdc4]&&this[_0x5a26('0xb')][_0x57bdc4][_0x5a26('0x13')]){if(this[_0x5a26('0xc')][this[_0x5a26('0xb')][_0x57bdc4]['Imap']['id']]){delete this[_0x5a26('0xc')][this[_0x5a26('0xb')][_0x57bdc4][_0x5a26('0x13')]['id']];}this[_0x5a26('0xb')][_0x57bdc4][_0x5a26('0x13')][_0x5a26('0x2b')]=moment();this[_0x5a26('0xe')](_0x57bdc4);}};EmailChannel[_0x5a26('0xd')][_0x5a26('0x2c')]=function(_0x4faa78,_0x1be5ad){if(this['mailAccounts'][_0x4faa78]){this[_0x5a26('0xb')][_0x4faa78][_0x5a26('0x13')][_0x5a26('0x12')]=_0x5a26('0x1f');this[_0x5a26('0xb')][_0x4faa78][_0x5a26('0x13')][_0x5a26('0x14')]=_0x1be5ad;return this[_0x5a26('0xe')](_0x4faa78);}};EmailChannel[_0x5a26('0xd')][_0x5a26('0x2d')]=function(_0x459f68,_0x48dfab){return this[_0x5a26('0x2e')][_0x5a26('0x2f')](_0x459f68,_0x48dfab);};EmailChannel[_0x5a26('0xd')]['verifySmtp']=function(_0x15414b){var _0x18a076=this;if(this['mailAccounts'][_0x15414b['id']]){if(_['isNil'](this[_0x5a26('0xb')][_0x15414b['id']][_0x5a26('0x30')])){this['mailAccounts'][_0x15414b['id']][_0x5a26('0x30')]={};}this[_0x5a26('0xb')][_0x15414b['id']]['Smtp']['lastSync']=moment();return new BPromise(function(_0x266b3b,_0x4010b0){return _0x18a076['smtp'][_0x5a26('0x31')](_0x15414b)['then'](function(_0x42ec11){_0x18a076[_0x5a26('0xb')][_0x15414b['id']][_0x5a26('0x30')]['status']=_0x5a26('0x32');_0x18a076[_0x5a26('0xb')][_0x15414b['id']][_0x5a26('0x30')][_0x5a26('0x14')]='';_0x18a076[_0x5a26('0xe')](_0x15414b['id']);_0x266b3b(_0x42ec11);})[_0x5a26('0x29')](function(_0x1f0cc6){_0x18a076[_0x5a26('0xb')][_0x15414b['id']][_0x5a26('0x30')][_0x5a26('0x12')]='error';_0x18a076[_0x5a26('0xb')][_0x15414b['id']][_0x5a26('0x30')][_0x5a26('0x14')]=_0x1f0cc6;_0x18a076[_0x5a26('0xe')](_0x15414b['id']);_0x4010b0(_0x1f0cc6);});});}return this[_0x5a26('0x2e')][_0x5a26('0x31')](_0x15414b);};module['exports']=EmailChannel;