From b72261cd8fae5d522308a53bc09bdfb6e18a4629 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 8 Nov 2016 11:00:51 +0100 Subject: changes: Add e-mail notifications --- modules/changes/changes.js | 149 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 136 insertions(+), 13 deletions(-) (limited to 'modules/changes') diff --git a/modules/changes/changes.js b/modules/changes/changes.js index 04b7f6e..67e4e43 100644 --- a/modules/changes/changes.js +++ b/modules/changes/changes.js @@ -5,7 +5,8 @@ var cmn=require("../$common.js"), crypto=require("crypto"), http=require("http"), https=require("https"), - URL=require("url"); + URL=require("url"), + child_process=require("child_process"); var moddir=null; @@ -20,19 +21,53 @@ persist.initSync(); //url: String //URLobject: {url, timeline: [[Date, hash, contents]]} var urls=persist.getItemSync("urls"); -(function(){ - if(!urls){ - urls={}; - persist.setItemSync("urls",urls); - } else { +if(!urls){ + urls={}; + persist.setItemSync("urls",urls); +} else { + (function(){ var url,i; for(url in urls){ for(i=0;i"); + nwaiting++; fetch("GET",url,function(url,status,body){ + nwaiting--; console.log("Got <"+url+">; status = "+status); if(status==-1){ hashes[url]=[new Date(),null,null]; @@ -110,12 +148,14 @@ function refreshURLs(){ if(!urls[url]){ console.log("WARNING: url <"+url+"> from hashes not found in urls!"); - return; + } else { + //var last=urls[url].timeline[urls[url].timeline.length-1]; + //if(last==undefined||hashes[url][1]==null||hashes[url][1]!=last[1]){ + urls[url].timeline.push(hashes[url]); + //} } - //var last=urls[url].timeline[urls[url].timeline.length-1]; - //if(last==undefined||hashes[url][1]==null||hashes[url][1]!=last[1]){ - urls[url].timeline.push(hashes[url]); - //} + + if(nwaiting==0&&cb)process.nextTick(cb); if(refresh_persist_timeout!=null)clearTimeout(refresh_persist_timeout); refresh_persist_timeout=setTimeout(function(){ @@ -127,6 +167,89 @@ function refreshURLs(){ } } +function collectLastHashes(){ + var lasthashes={}; + for(url in urls){ + if(urls[url].timeline.length==0){ + lasthashes[url]=null; + } else { + lasthashes[url]=urls[url].timeline[urls[url].timeline.length-1][1]; + } + } + return lasthashes; +} + +function sendMail(tomail,body,cb){ + var proc=child_process.spawn("sendmail",[tomail],{stdio:["pipe",process.stdout,process.stderr]}); + proc.stdin.error(function(e){ + console.log(e); + cb(e); + }); + proc.stdin.write(body,function(){ + proc.stdin.end(); + cb(); + }); +} + +function constructMailBody(tomail,diffs){ + var body="To: "+tomail+"\n"+ + "From: changes \n"+ + "Subject: changes notification mail\n\n"+ + "The changes module recorded "+ + "some changes in watched webpages.\n"+ + "The URLs and their old and new content hashes are shown below.\n\n"; + var i; + for(i=0;i"+diffs[i].url+"\n"+ + " was: "+diffs[i].oldhash+"\n"+ + " now: "+diffs[i].newhash+"\n"; + } + body+="\n\n"+ + "- webserver\n"; + return body; +} + +function performNotify(settings,cb){ + var oldhashes=collectLastHashes(); + refreshURLs(function(){ + var newhashes=collectLastHashes(); + var diffs=[]; // [{url, oldhash, newhash}] + for(url in newhashes){ + if(newhashes[url]==oldhashes[url])continue; + diffs.push({ + url: url, + oldhash: oldhashes[url], + newhash: newhashes[url] + }); + } + sendMail(settings.to,constructMailBody(settings.to,diffs),cb); + }); +} + +function performCleanup(){ + //keep only the last hash and the last different hash intact + //(if there is no different hash, delete everything but the last) + for(url in urls){ + if(urls[url].timeline.length<=2)continue; + var i,lasthash,prevhash=null,prevhashidx=-1,tllen; + tllen=urls[url].timeline.length; + lasthash=urls[url].timeline[tllen-1][1]; + for(i=tllen-2;i>=0;i--){ + if(urls[url].timeline[i][1]!=lasthash){ + prevhash=urls[url].timeline[i][1]; + prevhashidx=i; + break; + } + } + if(prevhash==null){ + urls[url].timeline.splice(0,tllen-1); + } else { + urls[url].timeline.splice(prevhashidx+1,tllen-prevhashidx-2); + urls[url].timeline.splice(0,prevhashidx); + } + } +} + module.exports=function(app,io,_moddir){ moddir=_moddir; -- cgit v1.2.3-70-g09d2