flexbiz-server
Version:
Flexible Server
14 lines (13 loc) • 6.11 kB
JavaScript
const MailListener=require("mail-listener2"),Mailreceived=global.getModel("mailreceived"),Mailaccount=global.getModel("mailaccount"),User=global.getModel("user"),Link=global.getModel("link"),Customer=global.getModel("customer"),Lienhe=global.getModel("lienhe"),async=require("async"),underscore=require("underscore"),fs=require("fs"),dns=require("dns"),schedule=require("./schedule"),management=function(){this.accounts={}};
management.prototype.start=function(){const $self$$=this;Mailaccount.find({status:!0}).lean().then(function($accounts$$){$accounts$$.forEach(function($account$$){$account$$.id_app&&$self$$.startAccount($account$$)})}).catch($e$$=>{Logger.error($e$$)})};
management.prototype.stopAccount=function($account$$,$callback$$){this.accounts[$account$$._id.toString()]&&(this.accounts[$account$$._id.toString()].stop(),delete this.accounts[$account$$._id.toString()]);Mailaccount.findOne({_id:$account$$._id.toString()},function($e$$,$rs$$){$rs$$?($rs$$.error=null,$rs$$.status_string="\u0110\u00e3 ng\u1eaft k\u1ebft n\u1ed1i",$rs$$.status_code=0,$rs$$.save(function($e$$){$e$$&&Logger.info($e$$);$callback$$&&$callback$$($e$$)})):$callback$$&&$callback$$($e$$)})};
management.prototype.startAccount=function($account$$,$callback$$){if($account$$.imap&&$account$$.imap.host){var $self$$=this;dns.lookup($account$$.imap.host,function($error$$){if($error$$)return Logger.error($error$$);$self$$.stopAccount($account$$,function($e$$){if(!$e$$&&$account$$.status){$e$$=new Date;$e$$.setDate($e$$.getDate()-1);$e$$=new MailListener({username:$account$$.username,password:$account$$.password,host:$account$$.imap.host,port:$account$$.imap.port,tls:$account$$.imap.secure,tlsOptions:{rejectUnauthorized:!1},
mailbox:"INBOX",searchFilter:["ALL",["SINCE",$e$$]],markSeen:!1,fetchUnreadOnStart:!0,mailParserOptions:{streamAttachments:!1},attachments:!0,attachmentOptions:{directory:"attachments/"}});$e$$.start();$self$$.accounts[$account$$._id.toString()]=$e$$;$e$$.on("server:connected",function(){Mailaccount.findOne({_id:$account$$._id.toString()},function($e$$,$rs$$){$rs$$&&($rs$$.error=null,$rs$$.status_string="\u0110ang k\u1ebft n\u1ed1i",$rs$$.status_code=1,$rs$$.save(function($e$$){$e$$&&Logger.info($e$$)}))});
$callback$$&&$callback$$(null)});$e$$.on("server:disconnected",function(){Mailaccount.findOne({_id:$account$$._id.toString()},function($e$$,$rs$$){$rs$$&&($rs$$.error=null,$rs$$.status_string="\u0110\u00e3 ng\u1eaft k\u1ebft n\u1ed1i",$rs$$.status_code=0,$rs$$.save(function($e$$){$e$$&&Logger.info($e$$)}))});schedule.ConnectImapServer($account$$,1)});$e$$.on("error",function($err$$){Mailaccount.findOne({_id:$account$$._id.toString()},function($e$$,$rs$$){$rs$$&&($rs$$.error=$err$$,$rs$$.status_string=
"Kh\u00f4ng th\u1ec3 k\u1ebft n\u1ed1i",$rs$$.status_code=-1,$rs$$.save(function($e$$){$e$$&&Logger.info($e$$)}))});schedule.ConnectImapServer($account$$,10);$callback$$&&$callback$$($err$$)});$e$$.on("mail",function($mail$$){$mail$$&&Mailreceived.findOne({messageId:$mail$$.messageId,id_app:$account$$.id_app},{_id:1},function($e$$,$rs$$){if(!$rs$$){var $received$$={account_id:$account$$._id.toString(),subject:$mail$$.subject,date_created:$mail$$.receivedDate,user_created:$account$$.user_created};
$received$$.date_updated=new Date;$received$$.to=$mail$$.to;$received$$.from=$mail$$.from;$received$$.id_app=$account$$.id_app;$received$$.from.forEach(function($from$$){$from$$.name||($from$$.name=$from$$.address)});$received$$.mail={text:$mail$$.text,html:$mail$$.html};$received$$.messageId=$mail$$.messageId;$mail$$.attachments&&($received$$.attachments=[],$mail$$.attachments.forEach(function($attachment$$){var $path$$="attachments/"+$attachment$$.contentId+"_"+$attachment$$.fileName;underscore.isString($attachment$$.path)?
(fs.createReadStream($attachment$$.path).pipe(fs.createWriteStream($path$$)),$received$$.attachments.push({contentType:$attachment$$.contentType,fileName:$attachment$$.fileName,contentId:$attachment$$.contentId,path:$path$$})):Logger.info("attachment path",$attachment$$.path)}));!$mail$$.text&&$mail$$.html&&($e$$=$mail$$.html.replace(/<(?:.|\n)*?>/gm,""),$received$$.mail.text=$e$$);$received$$.mail.text&&($received$$.small_text=$received$$.mail.text.length>128?$received$$.mail.text.substring(0,128)+
"...":$received$$.mail.text);(new Mailreceived($received$$)).save(function($e$$,$rs$$){if($e$$)return Logger.info($e$$);$rs$$&&(underscore.extend($received$$,$rs$$),$pushEmail$$($rs$$),async.mapSeries($rs$$.from,function($from$$,$callback$$){async.series({contact:function($callback$$){Lienhe.find({email:$from$$.address,id_app:$received$$.id_app}).lean().then(function($lhs$$){$lhs$$?async.mapLimit($lhs$$,10,function($lh$$,$callback$$){(new Link({id_app:$received$$.id_app,collection_a:"lienhe",id_a:$lh$$._id,
collection_b:"mailreceived",id_b:$received$$._id})).save(function($e$$){$e$$&&Logger.info($e$$);$callback$$()})},function(){$callback$$()}):$callback$$()}).catch($e$$=>{Logger.error($e$$);$callback$$()})},cust:function($callback$$){Customer.find({email:$from$$.address,id_app:$received$$.id_app}).lean().then(function($lhs$$){$lhs$$?async.mapLimit($lhs$$,100,function($lh$$,$callback$$){(new Link({id_app:$received$$.id_app,collection_a:"customer",id_a:$lh$$._id,collection_b:"mailreceived",id_b:$received$$._id})).save(function($e$$){$e$$&&
Logger.info($e$$);$callback$$()})},function(){$callback$$()}):$callback$$()}).catch($e$$=>{Logger.error($e$$);$callback$$()})}},function($e$$,$r$$){$callback$$($e$$,$r$$)})},function($e$$,$r$$){$e$$&&Logger.info($r$$)}))})}})});var $pushEmail$$=function($obj$$){var $m_now$$=new Date;$m_now$$.setDate($m_now$$.getDate()-1);$obj$$.date_created.getTime()<$m_now$$.getTime()||($m_now$$={},$obj$$.toObject?$m_now$$=$obj$$.toObject():underscore.extend($m_now$$,$obj$$),$m_now$$.code="mailreceived",$m_now$$.action=
"view",$m_now$$.title="Email m\u1edbi t\u1eeb "+($obj$$.from[0].name?$obj$$.from[0].name:$obj$$.from[0].address),$m_now$$.body=$obj$$.subject,User.emitEvent($account$$.user_created,"email",$m_now$$))};$e$$.on("attachment",function(){})}})})}};module.exports=management;