Built motion from commit 753c950e.|2.5.42
[motion2.git] / server / api / mailMessage / mailMessage.controller.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 _0xa45f=['destroy','end','stack','name','index','map','MailMessage','rawAttributes','fieldName','type','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','includeAll','findAll','rows','params','include','options','find','describe','create','body','direction','secret','split','MailAccount','findOne','MailAccountId','List','CmContact','Contacts','DESC','Smtp','service','host','port','secure','authentication','auth','user','pass','attachments','join','root','server/files/attachments','basename','ListId','SendMail','sendMail','accept','read','readAt','YYYY-MM-DD\x20HH:mm:ss','UserId','agent','get','MailInteraction','MailInteractionId','isNil','read1stAt','interaction','[MAILMESSAGE:ACCEPT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s','role','null','acceptMessage','SELECT\x20*\x20FROM\x20cm_contacts\x20WHERE\x20id\x20=\x20','ContactId','Sequelize','QueryTypes','SELECT','User','fullname','email','interface','format','SIP/%s','channel','motionChannel','event','fromString','EventManager','reject','mail','RejectMessage','sequelize','contact','internal','rejectmessage','download','unix','server','files','tmp','message-%s.eml','from','bcc','createdAt','Attachment','Attachments','decode','ddd,\x20DD\x20MMM\x20YYYY\x20HH:mm:ss\x20ZZ','headers','readFileSync','build','writeFileSync','unlinkSync','eml-format','bluebird','util','path','lodash','../../components/parsers/qs','api','../../config/logger','mail-interactions','../../config/environment','../../mysqldb','redis','defaults','html-entities','./mailMessage.socket','register','jayson/promise','client','http','request','then','info','MailMessage,\x20%s,\x20%s','request\x20sent','debug','MailMessage,\x20%s,\x20%s,\x20%s','stringify','error','message','result','catch','status','json','undefined','limit','count','offset','set','Content-Range','update'];(function(_0x505184,_0x9f670d){var _0x1f741e=function(_0x3d833f){while(--_0x3d833f){_0x505184['push'](_0x505184['shift']());}};_0x1f741e(++_0x9f670d);}(_0xa45f,0x11a));var _0xfa45=function(_0x3ec803,_0x343601){_0x3ec803=_0x3ec803-0x0;var _0xfcebc8=_0xa45f[_0x3ec803];return _0xfcebc8;};'use strict';var emlformat=require(_0xfa45('0x0'));var moment=require('moment');var BPromise=require(_0xfa45('0x1'));var util=require(_0xfa45('0x2'));var path=require(_0xfa45('0x3'));var fs=require('fs');var _=require(_0xfa45('0x4'));var Redis=require('ioredis');var htmlToText=require('html-to-text');var qs=require(_0xfa45('0x5'));var logger=require('../../config/logger')(_0xfa45('0x6'));var interaction_log=require(_0xfa45('0x7'))(_0xfa45('0x8'));var config=require(_0xfa45('0x9'));var db=require(_0xfa45('0xa'))['db'];config[_0xfa45('0xb')]=_[_0xfa45('0xc')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xfa45('0xb')]));var htmlEntities=require(_0xfa45('0xd'));require(_0xfa45('0xe'))[_0xfa45('0xf')](socket);var jayson=require(_0xfa45('0x10'));var client=jayson[_0xfa45('0x11')][_0xfa45('0x12')]({'port':0x232c});var client9003=jayson[_0xfa45('0x11')][_0xfa45('0x12')]({'port':0x232b});var client9002=jayson[_0xfa45('0x11')]['http']({'port':0x232a});function respondWithRpcPromise(_0x4b3f7e,_0x47824a,_0x8855f4,_0x1bd22b){return new BPromise(function(_0x30509e,_0x6ec847){var _0x4ac838=_0x1bd22b||client;return _0x4ac838[_0xfa45('0x13')](_0x4b3f7e,_0x8855f4)[_0xfa45('0x14')](function(_0x2181e5){logger[_0xfa45('0x15')](_0xfa45('0x16'),_0x47824a,_0xfa45('0x17'));logger[_0xfa45('0x18')](_0xfa45('0x19'),_0x47824a,'request\x20sent',JSON[_0xfa45('0x1a')](_0x2181e5));if(_0x2181e5['error']){if(_0x2181e5['error']['code']===0x1f4){logger[_0xfa45('0x1b')]('MailMessage,\x20%s,\x20%s',_0x47824a,_0x2181e5[_0xfa45('0x1b')][_0xfa45('0x1c')]);return _0x6ec847(_0x2181e5[_0xfa45('0x1b')]['message']);}logger[_0xfa45('0x1b')](_0xfa45('0x16'),_0x47824a,_0x2181e5[_0xfa45('0x1b')][_0xfa45('0x1c')]);return _0x30509e(_0x2181e5['error'][_0xfa45('0x1c')]);}else{logger['info'](_0xfa45('0x16'),_0x47824a,'request\x20sent');_0x30509e(_0x2181e5[_0xfa45('0x1d')]['message']);}})[_0xfa45('0x1e')](function(_0x565b64){logger[_0xfa45('0x1b')](_0xfa45('0x16'),_0x47824a,_0x565b64);_0x6ec847(_0x565b64);});});}function respondWithResult(_0x241436,_0x104c10){_0x104c10=_0x104c10||0xc8;return function(_0x4c4e6d){if(_0x4c4e6d){return _0x241436[_0xfa45('0x1f')](_0x104c10)[_0xfa45('0x20')](_0x4c4e6d);}};}function respondWithFilteredResult(_0x24470a,_0x583a66){return function(_0x135530){if(_0x135530){var _0xf4c58b=typeof _0x583a66['offset']===_0xfa45('0x21')&&typeof _0x583a66[_0xfa45('0x22')]===_0xfa45('0x21');var _0x509330=_0x135530[_0xfa45('0x23')];var _0x2193bb=_0xf4c58b?0x0:_0x583a66[_0xfa45('0x24')];var _0x372849=_0xf4c58b?_0x135530[_0xfa45('0x23')]:_0x583a66[_0xfa45('0x24')]+_0x583a66['limit'];var _0x3a75f9;if(_0x372849>=_0x509330){_0x372849=_0x509330;_0x3a75f9=0xc8;}else{_0x3a75f9=0xce;}_0x24470a[_0xfa45('0x1f')](_0x3a75f9);return _0x24470a[_0xfa45('0x25')](_0xfa45('0x26'),_0x2193bb+'-'+_0x372849+'/'+_0x509330)[_0xfa45('0x20')](_0x135530);}return null;};}function saveUpdates(_0x2d0af4){return function(_0x3d9fe1){if(_0x3d9fe1){return _0x3d9fe1[_0xfa45('0x27')](_0x2d0af4)[_0xfa45('0x14')](function(_0x315c91){return _0x315c91;});}return null;};}function removeEntity(_0x32ee7f){return function(_0x2fd69e){if(_0x2fd69e){return _0x2fd69e[_0xfa45('0x28')]()[_0xfa45('0x14')](function(){_0x32ee7f['status'](0xcc)[_0xfa45('0x29')]();});}};}function handleEntityNotFound(_0x13a0cf){return function(_0x4b6cf3){if(!_0x4b6cf3){_0x13a0cf['sendStatus'](0x194);}return _0x4b6cf3;};}function handleError(_0x2366de,_0x5a5105){_0x5a5105=_0x5a5105||0x1f4;return function(_0x5c36dd){logger[_0xfa45('0x1b')](_0x5c36dd[_0xfa45('0x2a')]);if(_0x5c36dd[_0xfa45('0x2b')]){delete _0x5c36dd[_0xfa45('0x2b')];}_0x2366de[_0xfa45('0x1f')](_0x5a5105)['send'](_0x5c36dd);};}exports[_0xfa45('0x2c')]=function(_0x5417d0,_0x176d6c){var _0x10d682={},_0x4f57df={},_0x5f2c8b={'count':0x0,'rows':[]};var _0x710cdf=_[_0xfa45('0x2d')](db[_0xfa45('0x2e')][_0xfa45('0x2f')],function(_0x466692){return{'name':_0x466692[_0xfa45('0x30')],'type':_0x466692[_0xfa45('0x31')]['key']};});_0x4f57df[_0xfa45('0x32')]=_['map'](_0x710cdf,_0xfa45('0x2b'));_0x4f57df[_0xfa45('0x33')]=_[_0xfa45('0x34')](_0x5417d0['query']);_0x4f57df[_0xfa45('0x35')]=_[_0xfa45('0x36')](_0x4f57df[_0xfa45('0x32')],_0x4f57df[_0xfa45('0x33')]);_0x10d682[_0xfa45('0x37')]=_[_0xfa45('0x36')](_0x4f57df[_0xfa45('0x32')],qs[_0xfa45('0x38')](_0x5417d0[_0xfa45('0x33')][_0xfa45('0x38')]));_0x10d682[_0xfa45('0x37')]=_0x10d682[_0xfa45('0x37')][_0xfa45('0x39')]?_0x10d682[_0xfa45('0x37')]:_0x4f57df[_0xfa45('0x32')];if(!_0x5417d0['query'][_0xfa45('0x3a')](_0xfa45('0x3b'))){_0x10d682[_0xfa45('0x22')]=qs[_0xfa45('0x22')](_0x5417d0[_0xfa45('0x33')][_0xfa45('0x22')]);_0x10d682[_0xfa45('0x24')]=qs[_0xfa45('0x24')](_0x5417d0[_0xfa45('0x33')]['offset']);}_0x10d682[_0xfa45('0x3c')]=qs['sort'](_0x5417d0[_0xfa45('0x33')][_0xfa45('0x3d')]);_0x10d682[_0xfa45('0x3e')]=qs[_0xfa45('0x35')](_[_0xfa45('0x3f')](_0x5417d0[_0xfa45('0x33')],_0x4f57df[_0xfa45('0x35')]),_0x710cdf);if(_0x5417d0['query'][_0xfa45('0x40')]){_0x10d682[_0xfa45('0x3e')]=_[_0xfa45('0x41')](_0x10d682[_0xfa45('0x3e')],{'$or':_[_0xfa45('0x2d')](_0x710cdf,function(_0x318016){if(_0x318016[_0xfa45('0x31')]!==_0xfa45('0x42')){var _0x1d07ef={};_0x1d07ef[_0x318016[_0xfa45('0x2b')]]={'$like':'%'+_0x5417d0[_0xfa45('0x33')][_0xfa45('0x40')]+'%'};return _0x1d07ef;}})});}_0x10d682=_[_0xfa45('0x41')]({},_0x10d682,_0x5417d0['options']);var _0x986c03={'where':_0x10d682[_0xfa45('0x3e')]};return db[_0xfa45('0x2e')][_0xfa45('0x23')](_0x986c03)[_0xfa45('0x14')](function(_0x23cb6a){_0x5f2c8b['count']=_0x23cb6a;if(_0x5417d0[_0xfa45('0x33')][_0xfa45('0x43')]){_0x10d682['include']=[{'all':!![]}];}return db['MailMessage'][_0xfa45('0x44')](_0x10d682);})['then'](function(_0x5a8378){_0x5f2c8b[_0xfa45('0x45')]=_0x5a8378;return _0x5f2c8b;})[_0xfa45('0x14')](respondWithFilteredResult(_0x176d6c,_0x10d682))['catch'](handleError(_0x176d6c,null));};exports['show']=function(_0x384d14,_0x4c0de7){var _0x5edc68={'raw':!![],'where':{'id':_0x384d14[_0xfa45('0x46')]['id']}},_0x4c43c6={};_0x4c43c6['model']=_[_0xfa45('0x34')](db[_0xfa45('0x2e')][_0xfa45('0x2f')]);_0x4c43c6['query']=_[_0xfa45('0x34')](_0x384d14[_0xfa45('0x33')]);_0x4c43c6[_0xfa45('0x35')]=_['intersection'](_0x4c43c6['model'],_0x4c43c6['query']);_0x5edc68['attributes']=_['intersection'](_0x4c43c6[_0xfa45('0x32')],qs[_0xfa45('0x38')](_0x384d14[_0xfa45('0x33')][_0xfa45('0x38')]));_0x5edc68[_0xfa45('0x37')]=_0x5edc68[_0xfa45('0x37')][_0xfa45('0x39')]?_0x5edc68['attributes']:_0x4c43c6[_0xfa45('0x32')];if(_0x384d14[_0xfa45('0x33')][_0xfa45('0x43')]){_0x5edc68[_0xfa45('0x47')]=[{'all':!![]}];}_0x5edc68=_[_0xfa45('0x41')]({},_0x5edc68,_0x384d14[_0xfa45('0x48')]);return db[_0xfa45('0x2e')]['find'](_0x5edc68)[_0xfa45('0x14')](handleEntityNotFound(_0x4c0de7,null))['then'](respondWithResult(_0x4c0de7,null))[_0xfa45('0x1e')](handleError(_0x4c0de7,null));};exports[_0xfa45('0x27')]=function(_0x1491af,_0x5d36d8){if(_0x1491af['body']['id']){delete _0x1491af['body']['id'];}return db[_0xfa45('0x2e')][_0xfa45('0x49')]({'where':{'id':_0x1491af[_0xfa45('0x46')]['id']}})[_0xfa45('0x14')](handleEntityNotFound(_0x5d36d8,null))[_0xfa45('0x14')](saveUpdates(_0x1491af['body'],null))[_0xfa45('0x14')](respondWithResult(_0x5d36d8,null))['catch'](handleError(_0x5d36d8,null));};exports['destroy']=function(_0x23740f,_0x43be6b){return db['MailMessage'][_0xfa45('0x49')]({'where':{'id':_0x23740f[_0xfa45('0x46')]['id']}})[_0xfa45('0x14')](handleEntityNotFound(_0x43be6b,null))[_0xfa45('0x14')](removeEntity(_0x43be6b,null))[_0xfa45('0x1e')](handleError(_0x43be6b,null));};exports[_0xfa45('0x4a')]=function(_0x1f2f44,_0x3914fa){return db[_0xfa45('0x2e')]['describe']()[_0xfa45('0x14')](respondWithResult(_0x3914fa,null))[_0xfa45('0x1e')](handleError(_0x3914fa,null));};exports['create']=function(_0x367644,_0x47770f){return db['MailMessage'][_0xfa45('0x4b')](_0x367644[_0xfa45('0x4c')])[_0xfa45('0x14')](function(_0x127bcd){if(_0x127bcd){if(_0x127bcd[_0xfa45('0x4d')]==='out'&&!_0x127bcd[_0xfa45('0x4e')]){var _0x42c378,_0x49dcb0,_0x2a6cf5;if(_0x367644['body']['to']){_0x49dcb0=_0x367644[_0xfa45('0x4c')]['to'][_0xfa45('0x4f')](',')[0x0];}return db[_0xfa45('0x50')][_0xfa45('0x51')]({'where':{'id':_0x127bcd[_0xfa45('0x52')]},'include':[{'model':db['CmList'],'as':_0xfa45('0x53'),'include':[{'model':db[_0xfa45('0x54')],'as':_0xfa45('0x55'),'where':{'email':_0x49dcb0},'limit':0x1,'order':[['updatedAt',_0xfa45('0x56')]]}]},{'model':db['MailServerOut'],'as':'Smtp','raw':!![]}]})[_0xfa45('0x14')](function(_0x38176f){_0x42c378=_0x38176f;if(_0x42c378&&_0x42c378[_0xfa45('0x57')]){var _0x242ea2={'tls':{'rejectUnauthorized':![]}};if(_['isNil'](_0x42c378[_0xfa45('0x57')][_0xfa45('0x58')])){_0x242ea2[_0xfa45('0x59')]=_0x42c378[_0xfa45('0x57')][_0xfa45('0x59')];_0x242ea2[_0xfa45('0x5a')]=_0x42c378[_0xfa45('0x57')][_0xfa45('0x5a')];_0x242ea2[_0xfa45('0x5b')]=_0x42c378[_0xfa45('0x57')][_0xfa45('0x5b')];if(_0x42c378[_0xfa45('0x57')][_0xfa45('0x5c')]){_0x242ea2[_0xfa45('0x5d')]={'user':_0x42c378[_0xfa45('0x57')][_0xfa45('0x5e')],'pass':_0x42c378[_0xfa45('0x57')][_0xfa45('0x5f')]};}}else{_0x242ea2[_0xfa45('0x58')]=_0x42c378[_0xfa45('0x57')][_0xfa45('0x58')];_0x242ea2[_0xfa45('0x5d')]={'user':_0x42c378['Smtp']['user'],'pass':_0x42c378[_0xfa45('0x57')]['pass']};_0x242ea2[_0xfa45('0x5b')]=_0x42c378[_0xfa45('0x57')][_0xfa45('0x5b')];}if(_0x367644[_0xfa45('0x4c')]['attachments']&&_0x367644['body']['attachments'][_0xfa45('0x39')]){for(var _0x3404fc=0x0;_0x3404fc<_0x367644[_0xfa45('0x4c')][_0xfa45('0x60')][_0xfa45('0x39')];_0x3404fc+=0x1){_0x367644[_0xfa45('0x4c')][_0xfa45('0x60')][_0x3404fc]={'filename':_0x367644[_0xfa45('0x4c')][_0xfa45('0x60')][_0x3404fc]['name'],'path':path[_0xfa45('0x61')](config[_0xfa45('0x62')],_0xfa45('0x63'),_0x367644[_0xfa45('0x4c')][_0xfa45('0x60')][_0x3404fc][_0xfa45('0x64')])};}}_0x2a6cf5={'account':_0x242ea2,'message':_0x367644[_0xfa45('0x4c')]};return;}})[_0xfa45('0x14')](function(){if(_0x42c378[_0xfa45('0x53')]){if(_0x42c378['List']['Contacts']['length']){return _0x42c378[_0xfa45('0x53')][_0xfa45('0x55')][0x0];}else{return db['CmContact'][_0xfa45('0x4b')](_[_0xfa45('0xc')](_0x367644[_0xfa45('0x4c')],{'firstName':_0x367644[_0xfa45('0x4c')]['to'],'email':_0x367644[_0xfa45('0x4c')]['to'],'phone':_0x367644['body']['to'],'ListId':_0x42c378[_0xfa45('0x65')]}));}}else{throw new Error('List\x20not\x20found');}})['then'](function(){if(_0x2a6cf5){return respondWithRpcPromise(_0xfa45('0x66'),_0xfa45('0x67'),_0x2a6cf5,client9003)[_0xfa45('0x14')](function(_0x3281b1){return _0x127bcd[_0xfa45('0x27')]({'read':!![],'messageId':_0x3281b1['messageId'],'sentAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss')});})['catch'](function(_0x547516){logger[_0xfa45('0x1b')]('%s',JSON[_0xfa45('0x1a')](_0x547516));return _0x127bcd;});}return _0x127bcd;});}return _0x127bcd;}})[_0xfa45('0x14')](respondWithResult(_0x47770f,0xc9))[_0xfa45('0x1e')](handleError(_0x47770f,null));};exports[_0xfa45('0x68')]=function(_0x226e0f,_0x233c3e){var _0x1bd431={'agent':{},'channel':'mail'};if(_0x226e0f[_0xfa45('0x4c')]['id']){delete _0x226e0f['body']['id'];}_0x226e0f[_0xfa45('0x4c')][_0xfa45('0x69')]=!![];_0x226e0f[_0xfa45('0x4c')][_0xfa45('0x6a')]=moment()['format'](_0xfa45('0x6b'));_0x226e0f[_0xfa45('0x4c')][_0xfa45('0x6c')]=_0x226e0f[_0xfa45('0x4c')][_0xfa45('0x6c')]||_0x226e0f['user']['id'];_0x1bd431[_0xfa45('0x6d')]['id']=_0x226e0f[_0xfa45('0x4c')][_0xfa45('0x6c')];return db['MailMessage'][_0xfa45('0x49')]({'where':{'id':_0x226e0f[_0xfa45('0x46')]['id'],'UserId':null}})[_0xfa45('0x14')](handleEntityNotFound(_0x233c3e,null))['then'](saveUpdates(_0x226e0f[_0xfa45('0x4c')],null))['then'](function(_0x4467a7){if(_0x4467a7){_0x1bd431['message']=_0x4467a7[_0xfa45('0x6e')]({'plain':!![]});return db[_0xfa45('0x6f')]['find']({'where':{'id':_0x4467a7[_0xfa45('0x70')]}});}return null;})[_0xfa45('0x14')](handleEntityNotFound(_0x233c3e,null))[_0xfa45('0x14')](function(_0xfc4c27){if(_0xfc4c27){return _0xfc4c27[_0xfa45('0x27')]({'UserId':_0x226e0f[_0xfa45('0x4c')][_0xfa45('0x6c')],'read1stAt':_[_0xfa45('0x71')](_0xfc4c27[_0xfa45('0x72')])?moment()['format'](_0xfa45('0x6b')):undefined});}return null;})[_0xfa45('0x14')](function(_0xa12307){if(_0xa12307){_0x1bd431[_0xfa45('0x73')]=_0xa12307['get']({'plain':!![]});interaction_log['info'](_0xfa45('0x74'),_0x226e0f[_0xfa45('0x5e')]['id'],_0x226e0f[_0xfa45('0x5e')][_0xfa45('0x2b')],_0x226e0f[_0xfa45('0x5e')][_0xfa45('0x75')],_0x1bd431[_0xfa45('0x73')]['id'],_0x226e0f[_0xfa45('0x4c')]?JSON['stringify'](_0x226e0f[_0xfa45('0x4c')]):_0xfa45('0x76'));return respondWithRpcPromise('AcceptMessage',_0xfa45('0x77'),_0x1bd431);}return null;})['then'](function(_0x3795cf){var _0x46a7d4=_0xfa45('0x78')+_0x3795cf[_0xfa45('0x1c')][_0xfa45('0x79')];return db['sequelize'][_0xfa45('0x33')](_0x46a7d4,{'type':db[_0xfa45('0x7a')][_0xfa45('0x7b')][_0xfa45('0x7c')],'raw':!![]})[_0xfa45('0x14')](function(_0x56d1e7){return _0x56d1e7;});})[_0xfa45('0x14')](function(_0x228e8f){if(_0x228e8f){_0x1bd431[_0xfa45('0x1c')]['contact']=_0x228e8f[0x0];if(!_0x226e0f[_0xfa45('0x4c')]['UserId'])return;return db[_0xfa45('0x7d')]['find']({'where':{'id':_0x226e0f[_0xfa45('0x4c')][_0xfa45('0x6c')],'role':_0xfa45('0x6d')},'attributes':['id',_0xfa45('0x2b'),_0xfa45('0x7e'),_0xfa45('0x7f'),'internal'],'raw':!![]});}return null;})[_0xfa45('0x14')](function(_0x354c13){if(!_0x354c13)return null;_0x1bd431[_0xfa45('0x1c')]['agent']=_0x354c13;return _0x1bd431;})[_0xfa45('0x14')](function(_0x1ae82d){if(_0x1ae82d){if(_0x226e0f[_0xfa45('0x4c')]['manual']){_0x1bd431[_0xfa45('0x1c')][_0xfa45('0x80')]=util[_0xfa45('0x81')](_0xfa45('0x82'),_0x226e0f[_0xfa45('0x5e')][_0xfa45('0x2b')]);_0x1bd431['message'][_0xfa45('0x83')]=_0x1bd431[_0xfa45('0x83')];_0x1bd431[_0xfa45('0x1c')][_0xfa45('0x84')]=_0x1bd431[_0xfa45('0x83')];_0x1bd431['message'][_0xfa45('0x85')]=_0xfa45('0x68');_0x1bd431[_0xfa45('0x1c')][_0xfa45('0x4c')]=htmlToText[_0xfa45('0x86')](_0x1bd431['message'][_0xfa45('0x4c')]);return respondWithRpcPromise(_0xfa45('0x87'),_0xfa45('0x87'),{'event':'acceptmessage','message':_0x1bd431[_0xfa45('0x1c')]},client9002)['then'](function(){return _0x1bd431;});}return _0x1bd431;}return null;})['then'](respondWithResult(_0x233c3e,null))[_0xfa45('0x1e')](handleError(_0x233c3e,null));};exports[_0xfa45('0x88')]=function(_0x286ab6,_0x1daf15){var _0x4c6536={'agent':{},'channel':_0xfa45('0x89')};if(_0x286ab6[_0xfa45('0x4c')]['id']){delete _0x286ab6[_0xfa45('0x4c')]['id'];}_0x286ab6[_0xfa45('0x4c')][_0xfa45('0x6c')]=_0x286ab6[_0xfa45('0x4c')][_0xfa45('0x6c')]||_0x286ab6[_0xfa45('0x5e')]['id'];_0x4c6536[_0xfa45('0x6d')]['id']=_0x286ab6[_0xfa45('0x4c')][_0xfa45('0x6c')];return db[_0xfa45('0x2e')][_0xfa45('0x49')]({'where':{'id':_0x286ab6['params']['id'],'UserId':null}})[_0xfa45('0x14')](handleEntityNotFound(_0x1daf15,null))[_0xfa45('0x14')](function(_0x2859ac){if(_0x2859ac){_0x4c6536[_0xfa45('0x1c')]=_0x2859ac['get']({'plain':!![]});return db['MailInteraction'][_0xfa45('0x49')]({'where':{'id':_0x2859ac[_0xfa45('0x70')]}});}return null;})['then'](handleEntityNotFound(_0x1daf15,null))[_0xfa45('0x14')](function(_0x11fe68){if(_0x11fe68){_0x4c6536[_0xfa45('0x73')]=_0x11fe68[_0xfa45('0x6e')]({'plain':!![]});interaction_log[_0xfa45('0x15')]('[MAILMESSAGE:REJECT]\x20id=%s\x20name=%s\x20role=%s\x20interaction=%s\x20body=%s',_0x286ab6[_0xfa45('0x5e')]['id'],_0x286ab6[_0xfa45('0x5e')][_0xfa45('0x2b')],_0x286ab6['user'][_0xfa45('0x75')],_0x4c6536[_0xfa45('0x73')]['id'],_0x286ab6['body']?JSON['stringify'](_0x286ab6['body']):_0xfa45('0x76'));return respondWithRpcPromise(_0xfa45('0x8a'),'rejectMessage',_0x4c6536);}return null;})[_0xfa45('0x14')](function(_0x503764){var _0x2e1ea9=_0xfa45('0x78')+_0x503764['message'][_0xfa45('0x79')];return db[_0xfa45('0x8b')][_0xfa45('0x33')](_0x2e1ea9,{'type':db[_0xfa45('0x7a')][_0xfa45('0x7b')][_0xfa45('0x7c')],'raw':!![]})[_0xfa45('0x14')](function(_0x34d20c){return _0x34d20c;});})[_0xfa45('0x14')](function(_0x19e681){if(_0x19e681){_0x4c6536[_0xfa45('0x1c')][_0xfa45('0x8c')]=_0x19e681[0x0];if(!_0x286ab6[_0xfa45('0x4c')]['UserId'])return;return db[_0xfa45('0x7d')][_0xfa45('0x49')]({'where':{'id':_0x286ab6[_0xfa45('0x4c')][_0xfa45('0x6c')],'role':'agent'},'attributes':['id','name',_0xfa45('0x7e'),_0xfa45('0x7f'),_0xfa45('0x8d')],'raw':!![]});}return null;})[_0xfa45('0x14')](function(_0x5ccd21){if(!_0x5ccd21)return null;_0x4c6536[_0xfa45('0x1c')][_0xfa45('0x6d')]=_0x5ccd21;return _0x4c6536;})['then'](function(_0x2457d4){if(_0x2457d4){_0x2457d4[_0xfa45('0x1c')][_0xfa45('0x80')]=util['format']('SIP/%s',_0x286ab6['user'][_0xfa45('0x2b')]);_0x2457d4[_0xfa45('0x1c')][_0xfa45('0x83')]=_0x4c6536[_0xfa45('0x83')];_0x2457d4['message']['motionChannel']=_0x4c6536[_0xfa45('0x83')];_0x2457d4[_0xfa45('0x1c')][_0xfa45('0x85')]='reject';_0x2457d4[_0xfa45('0x1c')][_0xfa45('0x4c')]=htmlToText['fromString'](_0x4c6536['message'][_0xfa45('0x4c')]);respondWithRpcPromise(_0xfa45('0x87'),_0xfa45('0x87'),{'event':_0xfa45('0x8e'),'message':_0x2457d4['message']},client9002);return _0x4c6536;}return null;})['then'](respondWithResult(_0x1daf15,null))[_0xfa45('0x1e')](handleError(_0x1daf15,null));};exports[_0xfa45('0x8f')]=function(_0x11fae0,_0x2c00f8){var _0x50500b=moment()[_0xfa45('0x90')]()['toString']();var _0x5a01b1=path[_0xfa45('0x61')](config[_0xfa45('0x62')],_0xfa45('0x91'),_0xfa45('0x92'),_0xfa45('0x93'));var _0x46995f=path[_0xfa45('0x61')](config[_0xfa45('0x62')],'server',_0xfa45('0x92'),'attachments');var _0x199b67=util['format'](_0xfa45('0x94'),_0x50500b);return db['MailMessage'][_0xfa45('0x49')]({'where':{'id':_0x11fae0['params']['id']},'attributes':['id',_0xfa45('0x4c'),_0xfa45('0x95'),'to','cc',_0xfa45('0x96'),'subject',_0xfa45('0x97')],'include':_0x11fae0[_0xfa45('0x33')]['attachments']?[{'model':db[_0xfa45('0x98')],'as':_0xfa45('0x99')}]:undefined})[_0xfa45('0x14')](handleEntityNotFound(_0x2c00f8,null))[_0xfa45('0x14')](function(_0x270589){if(_0x270589){var _0x5f396c=_0x270589;var _0x58e078={'from':_0x5f396c['from'],'to':_0x5f396c['to'],'subject':_0x5f396c['subject'],'html':htmlEntities[_0xfa45('0x9a')](_0x5f396c[_0xfa45('0x4c')]),'headers':{'Date':moment(_0x5f396c['createdAt'])[_0xfa45('0x81')](_0xfa45('0x9b'))},'attachments':[]};if(_0x270589['cc']){_0x58e078['headers']['Cc']=_0x270589['cc'];}if(_0x270589[_0xfa45('0x96')]){_0x58e078[_0xfa45('0x9c')]['Bcc']=_0x270589[_0xfa45('0x96')];}return new BPromise(function(_0x8603f9,_0x2b8b3c){if(_0x270589['Attachments']){for(var _0x1e7fc6=0x0;_0x1e7fc6<_0x270589[_0xfa45('0x99')][_0xfa45('0x39')];_0x1e7fc6++){var _0x335c85=_0x270589[_0xfa45('0x99')][_0x1e7fc6];var _0x581efe=path[_0xfa45('0x61')](_0x46995f,_0x335c85[_0xfa45('0x64')]);if(fs['existsSync'](_0x581efe)){_0x58e078['attachments']['push']({'name':_0x335c85[_0xfa45('0x2b')],'contentType':_0x335c85[_0xfa45('0x31')],'data':fs[_0xfa45('0x9d')](_0x581efe)});}}}emlformat[_0xfa45('0x9e')](_0x58e078,function(_0x364d77,_0x3af283){if(_0x364d77)return _0x2b8b3c(_0x364d77);fs[_0xfa45('0x9f')](path[_0xfa45('0x61')](_0x5a01b1,_0x199b67),_0x3af283);return _0x8603f9(_0x3af283);});});}})['then'](function(_0x41ce06){if(_0x41ce06){return _0x2c00f8[_0xfa45('0x8f')](path[_0xfa45('0x61')](_0x5a01b1,_0x199b67),_0x199b67,function(_0x1888e8){if(!_0x1888e8)fs[_0xfa45('0xa0')](path[_0xfa45('0x61')](_0x5a01b1,_0x199b67));});}})[_0xfa45('0x1e')](handleError(_0x2c00f8,null));};