UNPKG

flexbiz-server

Version:

Flexible Server

15 lines (14 loc) 6.53 kB
'use strict';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().exec(function($e$$,$accounts$$){if($e$$)return console.log($e$$);$accounts$$.forEach(function($account$$){$account$$.id_app&&$self$$.startAccount($account$$)})})}; 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$jscomp$0$$,$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$$&&console.log($e$$);$callback$$&&$callback$$($e$$)})):$callback$$&&$callback$$($e$jscomp$0$$)})}; management.prototype.startAccount=function($account$$,$callback$jscomp$2$$){if($account$$.imap&&$account$$.imap.host){var $self$$=this;dns.lookup($account$$.imap.host,function($error$$,$addresses$$){if($error$$)return console.error($error$$);$self$$.stopAccount($account$$,function($e$jscomp$3$$){if(!$e$jscomp$3$$&&$account$$.status){$e$jscomp$3$$=new Date;$e$jscomp$3$$.setDate($e$jscomp$3$$.getDate()-1);$e$jscomp$3$$=new MailListener({username:$account$$.username,password:$account$$.password,host:$account$$.imap.host, port:$account$$.imap.port,tls:$account$$.imap.tls,tlsOptions:{rejectUnauthorized:!1},mailbox:"INBOX",searchFilter:["ALL",["SINCE",$e$jscomp$3$$]],markSeen:!1,fetchUnreadOnStart:!0,mailParserOptions:{streamAttachments:!1},attachments:!0,attachmentOptions:{directory:"attachments/"}});$e$jscomp$3$$.start();$self$$.accounts[$account$$._id.toString()]=$e$jscomp$3$$;$e$jscomp$3$$.on("server:connected",function(){Mailaccount.findOne({_id:$account$$._id.toString()},function($e$jscomp$0$$,$rs$$){$rs$$&&($rs$$.error= null,$rs$$.status_string="\u0110ang k\u1ebft n\u1ed1i",$rs$$.status_code=1,$rs$$.save(function($e$$){$e$$&&console.log($e$$)}))});$callback$jscomp$2$$&&$callback$jscomp$2$$(null)});$e$jscomp$3$$.on("server:disconnected",function(){Mailaccount.findOne({_id:$account$$._id.toString()},function($e$jscomp$0$$,$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$$&&console.log($e$$)}))});schedule.ConnectImapServer($account$$, 1)});$e$jscomp$3$$.on("error",function($err$$){Mailaccount.findOne({_id:$account$$._id.toString()},function($e$jscomp$0$$,$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$$&&console.log($e$$)}))});schedule.ConnectImapServer($account$$,10);$callback$jscomp$2$$&&$callback$jscomp$2$$($err$$)});$e$jscomp$3$$.on("mail",function($mail$$,$seqno$$,$attributes$$){$mail$$&&Mailreceived.findOne({messageId:$mail$$.messageId, id_app:$account$$.id_app},{_id:1},function($e$jscomp$2$$,$rs$jscomp$1$$){if(!$rs$jscomp$1$$){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$$})):console.log("attachment path", $attachment$$.path)}));!$mail$$.text&&$mail$$.html&&($e$jscomp$2$$=$mail$$.html.replace(/<(?:.|\n)*?>/gm,""),$received$$.mail.text=$e$jscomp$2$$);$received$$.mail.text&&($received$$.small_text=128<$received$$.mail.text.length?$received$$.mail.text.substring(0,128)+"...":$received$$.mail.text);(new Mailreceived($received$$)).save(function($e$jscomp$1$$,$rs$jscomp$0$$){if($e$jscomp$1$$)return console.log($e$jscomp$1$$);$rs$jscomp$0$$&&(underscore.extend($received$$,$rs$jscomp$0$$),$pushEmail$$($rs$jscomp$0$$), async.mapSeries($rs$jscomp$0$$.from,function($from$$,$callback$jscomp$1$$){async.series({contact:function($callback$jscomp$0$$){Lienhe.find({email:$from$$.address,id_app:$received$$.id_app}).lean().exec(function($e$jscomp$0$$,$lhs$$){$lhs$$?async.mapSeries($lhs$$,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$$&&console.log($e$$);$callback$$()})},function($e$$,$rs$$){$callback$jscomp$0$$()}): $callback$jscomp$0$$()})},cust:function($callback$jscomp$0$$){Customer.find({email:$from$$.address,id_app:$received$$.id_app}).lean().exec(function($e$jscomp$0$$,$lhs$$){$lhs$$?async.mapSeries($lhs$$,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$$&&console.log($e$$);$callback$$()})},function($e$$,$rs$$){$callback$jscomp$0$$()}):$callback$jscomp$0$$()})}},function($e$$, $r$$){$callback$jscomp$1$$($e$$,$r$$)})},function($e$$,$r$$){$e$$&&console.log($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$jscomp$3$$.on("attachment",function($attachment$$){})}})})}};module.exports=management;