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 _0xd89f=['./smtp','../rpc/mailAccount','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','key','active','Imap','messageStatus','isNil','stringify','mailAccount:update','refresh','hasOwnProperty','box','handleImapBox','bind','handleImapEmail','handleImapEnd','error','handleImapError','status','connected','name','messages','total','info','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','notify','then','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','send','verifySmtp','Smtp','verify','exports','lodash','moment','bluebird','md5','ioredis','email','redis','defaults','localhost','socket.io-emitter','./imap'];(function(_0x261ff0,_0x470af0){var _0x33f33f=function(_0x36d26f){while(--_0x36d26f){_0x261ff0['push'](_0x261ff0['shift']());}};_0x33f33f(++_0x470af0);}(_0xd89f,0xd1));var _0xfd89=function(_0x55bc9d,_0x1840e5){_0x55bc9d=_0x55bc9d-0x0;var _0x51a72d=_0xd89f[_0x55bc9d];return _0x51a72d;};'use strict';var _=require(_0xfd89('0x0'));var moment=require(_0xfd89('0x1'));var BPromise=require(_0xfd89('0x2'));var md5=require(_0xfd89('0x3'));var Redis=require(_0xfd89('0x4'));var config=require('../../../config/environment');var logger=require('../../../config/logger')(_0xfd89('0x5'));config[_0xfd89('0x6')]=_[_0xfd89('0x7')](config['redis'],{'host':_0xfd89('0x8'),'port':0x18eb});var io=require(_0xfd89('0x9'))(new Redis(config[_0xfd89('0x6')]));var EmailImap=require(_0xfd89('0xa'));var EmailSmtp=require(_0xfd89('0xb'));var mailAccount=require(_0xfd89('0xc'));var md5Accounts={};function EmailChannel(_0x4ee406){logger['info'](_0xfd89('0xd'));this[_0xfd89('0xe')]=_0x4ee406[_0xfd89('0xe')];this[_0xfd89('0xf')]={};this[_0xfd89('0x10')]=new EmailSmtp();}EmailChannel[_0xfd89('0x11')][_0xfd89('0x12')]=function(_0x1f6dcc){var _0x17ca02=this[_0xfd89('0xe')][_0x1f6dcc];var _0x1a762c={'id':_0x17ca02['id'],'name':_0x17ca02['name'],'key':_0x17ca02[_0xfd89('0x13')],'active':_0x17ca02[_0xfd89('0x14')],'Imap':{'status':_0x17ca02[_0xfd89('0x15')]['status'],'messageStatus':_0x17ca02[_0xfd89('0x15')][_0xfd89('0x16')],'total':_0x17ca02[_0xfd89('0x15')]['total']}};if(_[_0xfd89('0x17')](md5Accounts[_0x1f6dcc])){md5Accounts[_0x1f6dcc]={};}var _0x562ae6=md5(JSON[_0xfd89('0x18')](_0x1a762c));if(md5Accounts[_0x1f6dcc]['md5']!==_0x562ae6){md5Accounts[_0x1f6dcc][_0xfd89('0x3')]=_0x562ae6;io[_0xfd89('0x12')](_0xfd89('0x19'),_0x1a762c);}};EmailChannel[_0xfd89('0x11')][_0xfd89('0x1a')]=function(_0x212b90){for(var _0xdc015d in _0x212b90){if(_0x212b90[_0xfd89('0x1b')](_0xdc015d)){if(_0x212b90[_0xdc015d]['Imap']&&_0x212b90[_0xdc015d][_0xfd89('0x14')]){var _0x5e7bb5=_0x212b90[_0xdc015d][_0xfd89('0x15')];if(!this[_0xfd89('0xf')][_0xfd89('0x1b')](_0x5e7bb5['id'])){this[_0xfd89('0xf')][_0x5e7bb5['id']]=new EmailImap(_0x5e7bb5,_0x212b90[_0xdc015d][_0xfd89('0x5')]);this[_0xfd89('0xf')][_0x5e7bb5['id']]['on'](_0xfd89('0x1c'),this[_0xfd89('0x1d')][_0xfd89('0x1e')](this,_0xdc015d));this[_0xfd89('0xf')][_0x5e7bb5['id']]['on'](_0xfd89('0x5'),this[_0xfd89('0x1f')]['bind'](this,_0xdc015d));this[_0xfd89('0xf')][_0x5e7bb5['id']]['on']('end',this[_0xfd89('0x20')][_0xfd89('0x1e')](this,_0xdc015d));this[_0xfd89('0xf')][_0x5e7bb5['id']]['on'](_0xfd89('0x21'),this[_0xfd89('0x22')][_0xfd89('0x1e')](this,_0xdc015d));}}else{if(_['isNil'](this[_0xfd89('0xe')][_0xdc015d][_0xfd89('0x15')])){this[_0xfd89('0xe')][_0xdc015d]['Imap']={};}this[_0xfd89('0xe')][_0xdc015d][_0xfd89('0x15')][_0xfd89('0x23')]='disabled';this['mailAccounts'][_0xdc015d][_0xfd89('0x15')][_0xfd89('0x16')]='';this[_0xfd89('0x12')](_0xdc015d);}}}};EmailChannel[_0xfd89('0x11')][_0xfd89('0x1d')]=function(_0x3e83db,_0x2ca60c){if(this[_0xfd89('0xe')][_0x3e83db]){this['mailAccounts'][_0x3e83db][_0xfd89('0x15')]['status']=_0xfd89('0x24');this['mailAccounts'][_0x3e83db][_0xfd89('0x15')][_0xfd89('0x16')]=_0x2ca60c[_0xfd89('0x25')];if(_0x2ca60c[_0xfd89('0x26')]){this[_0xfd89('0xe')][_0x3e83db]['Imap'][_0xfd89('0x27')]=_0x2ca60c[_0xfd89('0x26')]['total'];}return this[_0xfd89('0x12')](_0x3e83db);}};EmailChannel['prototype'][_0xfd89('0x1f')]=function(_0x2f1e25,_0x4c5123){logger[_0xfd89('0x28')](_0xfd89('0x29'),_0x4c5123[_0xfd89('0x2a')],_0x4c5123[_0xfd89('0x2b')],_0x4c5123[_0xfd89('0x2c')],_0x2f1e25);if(this[_0xfd89('0xe')][_0x2f1e25]){return mailAccount[_0xfd89('0x2d')](_0x2f1e25,_0x4c5123)[_0xfd89('0x2e')](function(_0x1a8db1){if(_0x1a8db1){logger[_0xfd89('0x28')]('Mail\x20accepted\x20from\x20routing');}})[_0xfd89('0x2f')](function(_0x3cfabd){logger['error'](_0xfd89('0x30'),JSON[_0xfd89('0x18')](_0x3cfabd));});}};EmailChannel[_0xfd89('0x11')][_0xfd89('0x20')]=function(_0x3a9440){if(this['mailAccounts'][_0x3a9440]&&this[_0xfd89('0xe')][_0x3a9440][_0xfd89('0x15')]){if(this[_0xfd89('0xf')][this['mailAccounts'][_0x3a9440][_0xfd89('0x15')]['id']]){delete this[_0xfd89('0xf')][this[_0xfd89('0xe')][_0x3a9440][_0xfd89('0x15')]['id']];}this[_0xfd89('0xe')][_0x3a9440]['Imap'][_0xfd89('0x31')]=moment();this['emit'](_0x3a9440);}};EmailChannel[_0xfd89('0x11')]['handleImapError']=function(_0x213259,_0x197715){if(this[_0xfd89('0xe')][_0x213259]){this[_0xfd89('0xe')][_0x213259][_0xfd89('0x15')]['status']=_0xfd89('0x21');this[_0xfd89('0xe')][_0x213259][_0xfd89('0x15')][_0xfd89('0x16')]=_0x197715;return this['emit'](_0x213259);}};EmailChannel[_0xfd89('0x11')]['sendMail']=function(_0x25262b,_0x294682){return this[_0xfd89('0x10')][_0xfd89('0x32')](_0x25262b,_0x294682);};EmailChannel[_0xfd89('0x11')][_0xfd89('0x33')]=function(_0x23dd81){var _0xb77f10=this;if(this['mailAccounts'][_0x23dd81['id']]){if(_[_0xfd89('0x17')](this[_0xfd89('0xe')][_0x23dd81['id']]['Smtp'])){this['mailAccounts'][_0x23dd81['id']]['Smtp']={};}this[_0xfd89('0xe')][_0x23dd81['id']][_0xfd89('0x34')][_0xfd89('0x31')]=moment();return new BPromise(function(_0x2f7a6e,_0xea0144){return _0xb77f10[_0xfd89('0x10')][_0xfd89('0x35')](_0x23dd81)['then'](function(_0x221981){_0xb77f10['mailAccounts'][_0x23dd81['id']]['Smtp'][_0xfd89('0x23')]='verified';_0xb77f10[_0xfd89('0xe')][_0x23dd81['id']][_0xfd89('0x34')][_0xfd89('0x16')]='';_0xb77f10[_0xfd89('0x12')](_0x23dd81['id']);_0x2f7a6e(_0x221981);})[_0xfd89('0x2f')](function(_0x25344){_0xb77f10[_0xfd89('0xe')][_0x23dd81['id']][_0xfd89('0x34')]['status']=_0xfd89('0x21');_0xb77f10[_0xfd89('0xe')][_0x23dd81['id']][_0xfd89('0x34')][_0xfd89('0x16')]=_0x25344;_0xb77f10[_0xfd89('0x12')](_0x23dd81['id']);_0xea0144(_0x25344);});});}return this['smtp'][_0xfd89('0x35')](_0x23dd81);};module[_0xfd89('0x36')]=EmailChannel;