Built motion from commit (unavailable).|2.5.6
[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 _0xfe9f=['Imap','messageStatus','total','isNil','stringify','mailAccount:update','refresh','hasOwnProperty','email','box','handleImapBox','handleImapEmail','handleImapEnd','bind','disabled','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','lastName','from','notify','then','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','handleImapError','error','send','verifySmtp','Smtp','verified','verify','lodash','moment','bluebird','md5','ioredis','../../../config/environment','defaults','redis','../rpc/mailAccount','info','start\x20email\x20channel','imapAccounts','smtp','prototype','emit','mailAccounts','name','key','active','status'];(function(_0x22ffc4,_0x3297d9){var _0x455220=function(_0x1fd6db){while(--_0x1fd6db){_0x22ffc4['push'](_0x22ffc4['shift']());}};_0x455220(++_0x3297d9);}(_0xfe9f,0x151));var _0xffe9=function(_0x2c1cd1,_0x7269b4){_0x2c1cd1=_0x2c1cd1-0x0;var _0x23b45c=_0xfe9f[_0x2c1cd1];return _0x23b45c;};'use strict';var _=require(_0xffe9('0x0'));var moment=require(_0xffe9('0x1'));var BPromise=require(_0xffe9('0x2'));var md5=require(_0xffe9('0x3'));var Redis=require(_0xffe9('0x4'));var config=require(_0xffe9('0x5'));var logger=require('../../../config/logger')('email');config['redis']=_[_0xffe9('0x6')](config[_0xffe9('0x7')],{'host':'localhost','port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xffe9('0x7')]));var EmailImap=require('./imap');var EmailSmtp=require('./smtp');var mailAccount=require(_0xffe9('0x8'));var md5Accounts={};function EmailChannel(_0x5d1092){logger[_0xffe9('0x9')](_0xffe9('0xa'));this['mailAccounts']=_0x5d1092['mailAccounts'];this[_0xffe9('0xb')]={};this[_0xffe9('0xc')]=new EmailSmtp();}EmailChannel[_0xffe9('0xd')][_0xffe9('0xe')]=function(_0x1e0007){var _0x3d366c=this[_0xffe9('0xf')][_0x1e0007];var _0x11d5e2={'id':_0x3d366c['id'],'name':_0x3d366c[_0xffe9('0x10')],'key':_0x3d366c[_0xffe9('0x11')],'active':_0x3d366c[_0xffe9('0x12')],'Imap':{'status':_0x3d366c['Imap'][_0xffe9('0x13')],'messageStatus':_0x3d366c[_0xffe9('0x14')][_0xffe9('0x15')],'total':_0x3d366c['Imap'][_0xffe9('0x16')]}};if(_[_0xffe9('0x17')](md5Accounts[_0x1e0007])){md5Accounts[_0x1e0007]={};}var _0x347402=md5(JSON[_0xffe9('0x18')](_0x11d5e2));if(md5Accounts[_0x1e0007][_0xffe9('0x3')]!==_0x347402){md5Accounts[_0x1e0007]['md5']=_0x347402;io['emit'](_0xffe9('0x19'),_0x11d5e2);}};EmailChannel[_0xffe9('0xd')][_0xffe9('0x1a')]=function(_0x158c6d){for(var _0x9ed22c in _0x158c6d){if(_0x158c6d[_0xffe9('0x1b')](_0x9ed22c)){if(_0x158c6d[_0x9ed22c]['Imap']&&_0x158c6d[_0x9ed22c][_0xffe9('0x12')]){var _0x88778f=_0x158c6d[_0x9ed22c][_0xffe9('0x14')];if(!this[_0xffe9('0xb')][_0xffe9('0x1b')](_0x88778f['id'])){this['imapAccounts'][_0x88778f['id']]=new EmailImap(_0x88778f,_0x158c6d[_0x9ed22c][_0xffe9('0x1c')]);this[_0xffe9('0xb')][_0x88778f['id']]['on'](_0xffe9('0x1d'),this[_0xffe9('0x1e')]['bind'](this,_0x9ed22c));this[_0xffe9('0xb')][_0x88778f['id']]['on'](_0xffe9('0x1c'),this[_0xffe9('0x1f')]['bind'](this,_0x9ed22c));this[_0xffe9('0xb')][_0x88778f['id']]['on']('end',this[_0xffe9('0x20')]['bind'](this,_0x9ed22c));this[_0xffe9('0xb')][_0x88778f['id']]['on']('error',this['handleImapError'][_0xffe9('0x21')](this,_0x9ed22c));}}else{if(_[_0xffe9('0x17')](this[_0xffe9('0xf')][_0x9ed22c]['Imap'])){this[_0xffe9('0xf')][_0x9ed22c][_0xffe9('0x14')]={};}this['mailAccounts'][_0x9ed22c][_0xffe9('0x14')]['status']=_0xffe9('0x22');this[_0xffe9('0xf')][_0x9ed22c][_0xffe9('0x14')]['messageStatus']='';this[_0xffe9('0xe')](_0x9ed22c);}}}};EmailChannel[_0xffe9('0xd')][_0xffe9('0x1e')]=function(_0x5a1b22,_0x26e681){if(this[_0xffe9('0xf')][_0x5a1b22]){this[_0xffe9('0xf')][_0x5a1b22][_0xffe9('0x14')][_0xffe9('0x13')]=_0xffe9('0x23');this[_0xffe9('0xf')][_0x5a1b22][_0xffe9('0x14')][_0xffe9('0x15')]=_0x26e681['name'];if(_0x26e681[_0xffe9('0x24')]){this['mailAccounts'][_0x5a1b22]['Imap'][_0xffe9('0x16')]=_0x26e681['messages'][_0xffe9('0x16')];}return this['emit'](_0x5a1b22);}};EmailChannel[_0xffe9('0xd')]['handleImapEmail']=function(_0x529e76,_0x22fe69){logger['info'](_0xffe9('0x25'),_0x22fe69['firstName'],_0x22fe69[_0xffe9('0x26')],_0x22fe69[_0xffe9('0x27')],_0x529e76);if(this[_0xffe9('0xf')][_0x529e76]){return mailAccount[_0xffe9('0x28')](_0x529e76,_0x22fe69)[_0xffe9('0x29')](function(_0x40df35){if(_0x40df35){logger['info']('Mail\x20accepted\x20from\x20routing');}})['catch'](function(_0x26c587){logger['error'](_0xffe9('0x2a'),JSON['stringify'](_0x26c587));});}};EmailChannel[_0xffe9('0xd')][_0xffe9('0x20')]=function(_0x329f32){if(this[_0xffe9('0xf')][_0x329f32]&&this[_0xffe9('0xf')][_0x329f32][_0xffe9('0x14')]){if(this[_0xffe9('0xb')][this['mailAccounts'][_0x329f32][_0xffe9('0x14')]['id']]){delete this['imapAccounts'][this[_0xffe9('0xf')][_0x329f32][_0xffe9('0x14')]['id']];}this['mailAccounts'][_0x329f32][_0xffe9('0x14')][_0xffe9('0x2b')]=moment();this[_0xffe9('0xe')](_0x329f32);}};EmailChannel[_0xffe9('0xd')][_0xffe9('0x2c')]=function(_0x513222,_0x427a82){if(this['mailAccounts'][_0x513222]){this[_0xffe9('0xf')][_0x513222]['Imap'][_0xffe9('0x13')]=_0xffe9('0x2d');this[_0xffe9('0xf')][_0x513222][_0xffe9('0x14')][_0xffe9('0x15')]=_0x427a82;return this[_0xffe9('0xe')](_0x513222);}};EmailChannel[_0xffe9('0xd')]['sendMail']=function(_0x102f95,_0x174732){return this[_0xffe9('0xc')][_0xffe9('0x2e')](_0x102f95,_0x174732);};EmailChannel[_0xffe9('0xd')][_0xffe9('0x2f')]=function(_0x3f4563){var _0x46a17c=this;if(this[_0xffe9('0xf')][_0x3f4563['id']]){if(_['isNil'](this['mailAccounts'][_0x3f4563['id']][_0xffe9('0x30')])){this[_0xffe9('0xf')][_0x3f4563['id']][_0xffe9('0x30')]={};}this['mailAccounts'][_0x3f4563['id']][_0xffe9('0x30')][_0xffe9('0x2b')]=moment();return new BPromise(function(_0xbc675,_0x2bd8c6){return _0x46a17c['smtp']['verify'](_0x3f4563)[_0xffe9('0x29')](function(_0x562a60){_0x46a17c[_0xffe9('0xf')][_0x3f4563['id']][_0xffe9('0x30')]['status']=_0xffe9('0x31');_0x46a17c['mailAccounts'][_0x3f4563['id']][_0xffe9('0x30')]['messageStatus']='';_0x46a17c[_0xffe9('0xe')](_0x3f4563['id']);_0xbc675(_0x562a60);})['catch'](function(_0x49f4f0){_0x46a17c[_0xffe9('0xf')][_0x3f4563['id']][_0xffe9('0x30')][_0xffe9('0x13')]=_0xffe9('0x2d');_0x46a17c[_0xffe9('0xf')][_0x3f4563['id']][_0xffe9('0x30')][_0xffe9('0x15')]=_0x49f4f0;_0x46a17c[_0xffe9('0xe')](_0x3f4563['id']);_0x2bd8c6(_0x49f4f0);});});}return this[_0xffe9('0xc')][_0xffe9('0x32')](_0x3f4563);};module['exports']=EmailChannel;