1
0
mirror of https://github.com/MailHops/mailhops-plugin.git synced 2025-05-17 06:40:08 -07:00

Added mark Junk by travel time exceeding 10 seconds

Cleaned up settings
This commit is contained in:
Andrew Van Tassel 2016-09-28 01:09:21 -06:00
parent 2b16798252
commit 73bd80c966
6 changed files with 148 additions and 82 deletions

View File

@ -16,11 +16,12 @@ var mailHops =
'api_host':'api.mailhops.com', 'api_host':'api.mailhops.com',
'debug':false, 'debug':false,
'country_tag':false, 'country_tag':false,
'travel_time_junk':false,
'country_filter':[] 'country_filter':[]
}, },
message: { message: {
secure:[] secure:[]
,time: 0 ,time: null
} }
}; };
@ -86,6 +87,8 @@ mailHops.loadPref = function()
mailHops.options.country_tag = mailHops.getCharPref('mail.mailHops.country_tag','false')=='true'?true:false; mailHops.options.country_tag = mailHops.getCharPref('mail.mailHops.country_tag','false')=='true'?true:false;
mailHops.options.travel_time_junk = mailHops.getCharPref('mail.mailHops.travel_time_junk','false')=='true'?true:false;
mailHops.options.country_filter = mailHops.getCharPref('mail.mailHops.country_filter',[]); mailHops.options.country_filter = mailHops.getCharPref('mail.mailHops.country_filter',[]);
//init display //init display
@ -162,9 +165,10 @@ mailHops.getRoute = function(){
var regexIPV6 = /(::|(([a-fA-F0-9]{1,4}):){7}(([a-fA-F0-9]{1,4}))|(:(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){1,6}:)|((([a-fA-F0-9]{1,4}):)(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){2}(:([a-fA-F0-9]{1,4})){1,5})|((([a-fA-F0-9]{1,4}):){3}(:([a-fA-F0-9]{1,4})){1,4})|((([a-fA-F0-9]{1,4}):){4}(:([a-fA-F0-9]{1,4})){1,3})|((([a-fA-F0-9]{1,4}):){5}(:([a-fA-F0-9]{1,4})){1,2}))/; var regexIPV6 = /(::|(([a-fA-F0-9]{1,4}):){7}(([a-fA-F0-9]{1,4}))|(:(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){1,6}:)|((([a-fA-F0-9]{1,4}):)(:([a-fA-F0-9]{1,4})){1,6})|((([a-fA-F0-9]{1,4}):){2}(:([a-fA-F0-9]{1,4})){1,5})|((([a-fA-F0-9]{1,4}):){3}(:([a-fA-F0-9]{1,4})){1,4})|((([a-fA-F0-9]{1,4}):){4}(:([a-fA-F0-9]{1,4})){1,3})|((([a-fA-F0-9]{1,4}):){5}(:([a-fA-F0-9]{1,4})){1,2}))/;
var headReceived = mailHops.headers.extractHeader ( "Received" , true ); var headReceived = mailHops.headers.extractHeader ( "Received" , true );
var headDate = mailHops.headers.extractHeader ( "Date" , true );
var headXReceived = mailHops.headers.extractHeader ( "X-Received" , false ); var headXReceived = mailHops.headers.extractHeader ( "X-Received" , false );
var headXOrigIP = mailHops.headers.extractHeader ( "X-Originating-IP" , false ); var headXOrigIP = mailHops.headers.extractHeader ( "X-Originating-IP" , false );
//auth box // auth box
var headXMailer = (mailHops.options.show_auth && mailHops.options.show_mailer) ? mailHops.headers.extractHeader ( "X-Mailer" , false ) : null; var headXMailer = (mailHops.options.show_auth && mailHops.options.show_mailer) ? mailHops.headers.extractHeader ( "X-Mailer" , false ) : null;
var headUserAgent = (mailHops.options.show_auth && mailHops.options.show_mailer) ? mailHops.headers.extractHeader ( "User-Agent" , false ) : null; var headUserAgent = (mailHops.options.show_auth && mailHops.options.show_mailer) ? mailHops.headers.extractHeader ( "User-Agent" , false ) : null;
var headXMimeOLE = (mailHops.options.show_auth && mailHops.options.show_mailer) ? mailHops.headers.extractHeader ( "X-MimeOLE" , false ) : null; var headXMimeOLE = (mailHops.options.show_auth && mailHops.options.show_mailer) ? mailHops.headers.extractHeader ( "X-MimeOLE" , false ) : null;
@ -174,10 +178,10 @@ mailHops.getRoute = function(){
var headListUnsubscribe = mailHops.options.show_lists ? mailHops.headers.extractHeader ( "List-Unsubscribe" , false ) : null; var headListUnsubscribe = mailHops.options.show_lists ? mailHops.headers.extractHeader ( "List-Unsubscribe" , false ) : null;
var all_ips = new Array(); var all_ips = new Array();
var rline = '',firstDate,lastDate; var rline = '',firstDate=headDate,lastDate;
//empty secure and time //empty secure and time
mailHops.message.secure = []; mailHops.message.secure = [];
mailHops.message.time = 0; mailHops.message.time = null;
if(mailHops.options.show_lists){ if(mailHops.options.show_lists){
mailHopsDisplay.lists( headListUnsubscribe ); mailHopsDisplay.lists( headListUnsubscribe );
@ -197,11 +201,12 @@ mailHops.getRoute = function(){
if(headReceivedArr[h].indexOf(';')==-1) if(headReceivedArr[h].indexOf(';')==-1)
continue; continue;
// first and last dates are used to calculate time Traveled // first and last dates are used to calculate time traveled
if(rline.indexOf(';')!==-1){ if(rline.indexOf(';')!==-1){
if(!lastDate) if(!lastDate)
lastDate = rline.substring(rline.indexOf(';')+1).trim(); lastDate = rline.substring(rline.indexOf(';')+1).trim();
firstDate = rline.substring(rline.indexOf(';')+1).trim(); else
firstDate = rline.substring(rline.indexOf(';')+1).trim();
} }
// parse IPs out of Received line // parse IPs out of Received line
@ -224,17 +229,21 @@ mailHops.getRoute = function(){
} }
// parse dates // parse dates
if(firstDate.indexOf('(')!==-1) if(firstDate && firstDate.indexOf('(')!==-1)
firstDate = firstDate.substring(0,firstDate.indexOf('(')).trim(); firstDate = firstDate.substring(0,firstDate.indexOf('(')).trim();
if(lastDate.indexOf('(')!==-1) if(lastDate && lastDate.indexOf('(')!==-1)
lastDate = lastDate.substring(0,lastDate.indexOf('(')).trim(); lastDate = lastDate.substring(0,lastDate.indexOf('(')).trim();
if(firstDate && lastDate){
try { try {
firstDate = new Date(firstDate); firstDate = new Date(firstDate);
lastDate = new Date(lastDate); lastDate = new Date(lastDate);
mailHops.message.time = lastDate-firstDate; mailHops.message.time = lastDate-firstDate;
} catch(e){ } catch(e){
mailHops.LOG('parse time traveled dates Error: '+JSON.stringify(e)); mailHops.LOG('travel dates parse Error: '+JSON.stringify(e));
mailHops.message.time = null;
}
} else {
mailHops.message.time = null;
} }
//get the originating IP address //get the originating IP address
@ -359,9 +368,9 @@ mailHops.lookupRoute = function(header_route){
if(mailHops.options.fkey != '') if(mailHops.options.fkey != '')
lookupURL += '&fkey='+mailHops.options.fkey; lookupURL += '&fkey='+mailHops.options.fkey;
if(mailHops.message.time > 0) if(mailHops.message.time != null)
lookupURL += '&t='+mailHops.message.time; lookupURL += '&t='+mailHops.message.time;
//check for cache //check for cache
var cached_results = mailHops.getResults(); var cached_results = mailHops.getResults();
@ -424,7 +433,7 @@ mailHops.saveResults = function(results,route){
//Add tag //Add tag
if(!!route){ if(!!route){
try{ try{
var countryCode = mailHopsUtils.getXOriginatingCountryCode(route); var countryCode = mailHopsUtils.getOriginatingCountryCode(route);
var msg = Components.classes["@mozilla.org/array;1"].createInstance(Components.interfaces.nsIMutableArray); var msg = Components.classes["@mozilla.org/array;1"].createInstance(Components.interfaces.nsIMutableArray);
msg.clear(); msg.clear();
msg.appendElement(msgHdr, false); msg.appendElement(msgHdr, false);
@ -446,6 +455,10 @@ mailHops.saveResults = function(results,route){
msgHdr.folder.setJunkScoreForMessages(msg, "100"); msgHdr.folder.setJunkScoreForMessages(msg, "100");
} }
} }
// tag as junk if travel time is longer than 10 seconds
if(!!mailHops.options.travel_time_junk && mailHops.message.time != null && mailHops.message.time > 10000){
msgHdr.folder.setJunkScoreForMessages(msg, "100");
}
} catch(e){ } catch(e){
mailHops.LOG( "Error adding CountryCode tag: "+e ); mailHops.LOG( "Error adding CountryCode tag: "+e );

View File

@ -269,19 +269,39 @@ var mailHopsDisplay =
} }
//append meta //append meta
if(this.options.show_meta){ try {
for(var index in meta){ if(this.options.show_meta){
var mlabel = document.createElement('label'); for(var i in meta){
mlabel.setAttribute('value',index+': '+meta[index]); var mlabel = document.createElement('label');
this.resultMeta.appendChild(mlabel); if(typeof meta[i] == 'object'){
} mlabel.setAttribute('value',i+': ');
var mlabel = document.createElement('label'); this.resultMeta.appendChild(mlabel);
mlabel.setAttribute('value','api url'); for(var ii in meta[i]){
mlabel.setAttribute('class','text-link'); var mlabel = document.createElement('label');
mlabel.setAttribute('href',lookup_url); if(ii=='reset'){
this.resultMeta.appendChild(mlabel); if(parseInt(meta[i][ii])/60 < 60)
mlabel.setAttribute('value',' '+ii+': '+Math.round(parseInt(meta[i][ii])/60).toString()+' min.');
} else
mlabel.setAttribute('value',' '+ii+': '+Math.round(parseInt(meta[i][ii])/60/60).toString()+' hr.');
} else {
mlabel.setAttribute('value',' '+ii+': '+meta[i][ii]);
}
this.resultMeta.appendChild(mlabel);
}
} else {
mlabel.setAttribute('value',i+': '+meta[i]);
this.resultMeta.appendChild(mlabel);
}
}
var mlabel = document.createElement('label');
mlabel.setAttribute('value','api url');
mlabel.setAttribute('class','text-link');
mlabel.setAttribute('href',lookup_url);
this.resultMeta.appendChild(mlabel);
}
} catch(e){
// parseInt error?
}
if(response && response.route && response.route.length){ if(response && response.route && response.route.length){
@ -438,9 +458,9 @@ var mailHopsDisplay =
displayText = ' Local message.'; displayText = ' Local message.';
} }
if(message.time>0){ if(message.time != null){
message.time = message.time/1000; message.time = message.time/1000;
if(message.time<60) if(message.time < 60)
distanceText += ' in '+message.time+' sec.'; distanceText += ' in '+message.time+' sec.';
else if(message.time<3600) //something is wrong if it takes this long else if(message.time<3600) //something is wrong if it takes this long
distanceText += ' in '+Math.round(message.time/60)+' min.'; distanceText += ' in '+Math.round(message.time/60)+' min.';
@ -455,7 +475,7 @@ var mailHopsDisplay =
this.resultTextDataPane.style.backgroundImage = 'url('+image+')'; this.resultTextDataPane.style.backgroundImage = 'url('+image+')';
this.resultTextDataPane.value = displayText; this.resultTextDataPane.value = displayText;
if(distanceText){ if(distanceText){
this.resultTextDataPane2.style.display = 'block'; this.resultTextDataPane2.style.display = 'block';
this.resultTextDataPane2.value = ' ( '+distanceText+' )'; this.resultTextDataPane2.value = ' ( '+distanceText+' )';

View File

@ -110,6 +110,11 @@ var mailHopPreferences = {
else else
document.getElementById("mailhop.country_tag").checked = true; document.getElementById("mailhop.country_tag").checked = true;
if(pref.getCharPref("mail.mailHops.travel_time_junk",'false')=='false')
document.getElementById("mailhop.travel_time_junk").checked = false;
else
document.getElementById("mailhop.travel_time_junk").checked = true;
document.getElementById("mailhops-membership-link").addEventListener("click", function () { document.getElementById("mailhops-membership-link").addEventListener("click", function () {
mailHopsUtils.launchExternalURL(this.getAttribute('data-account-url')); mailHopsUtils.launchExternalURL(this.getAttribute('data-account-url'));
}); });
@ -134,7 +139,7 @@ var mailHopPreferences = {
pref.setCharPref("mail.mailHops.debug", String(document.getElementById("mailhop.debug").checked)); pref.setCharPref("mail.mailHops.debug", String(document.getElementById("mailhop.debug").checked));
//API vars //API vars
if(document.getElementById("key_details").getAttribute("valid") == "false") if(document.getElementById("plan").getAttribute("valid") == "false")
this.api_key.value=''; this.api_key.value='';
pref.setCharPref("mail.mailHops.api_key", this.api_key.value); pref.setCharPref("mail.mailHops.api_key", this.api_key.value);
pref.setCharPref("mail.mailHops.api_http", this.api_http.value); pref.setCharPref("mail.mailHops.api_http", this.api_http.value);
@ -150,6 +155,7 @@ var mailHopPreferences = {
} }
pref.setCharPref("mail.mailHops.country_filter", String(JSON.stringify(this.country_filter))); pref.setCharPref("mail.mailHops.country_filter", String(JSON.stringify(this.country_filter)));
pref.setCharPref("mail.mailHops.country_tag", String(document.getElementById("mailhop.country_tag").checked)); pref.setCharPref("mail.mailHops.country_tag", String(document.getElementById("mailhop.country_tag").checked));
pref.setCharPref("mail.mailHops.travel_time_junk", String(document.getElementById("mailhop.travel_time_junk").checked));
return true; return true;
}, },
@ -159,7 +165,17 @@ var mailHopPreferences = {
document.getElementById("country_"+mailHopsUtils.countries[c]).checked=all; document.getElementById("country_"+mailHopsUtils.countries[c]).checked=all;
} }
}, },
planError: function(error){
document.getElementById("plan-error").style.display = 'block';
document.getElementById("plan-error").value=error;
document.getElementById("plan").value='';
document.getElementById("plan").setAttribute("valid","false");
document.getElementById("rate-limit").value='';
document.getElementById("rate-remaining").value='';
document.getElementById("rate-reset").value='';
document.getElementById("mailhops-membership-link").value='Join MailHops';
document.getElementById("mailhops-membership-link").setAttribute('data-account-url','https://mailhops.com');
},
saveAPIKey: function() { saveAPIKey: function() {
if(!!this.api_key && this.api_key.value != ''){ if(!!this.api_key && this.api_key.value != ''){
@ -167,35 +183,38 @@ var mailHopPreferences = {
var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON);
var apiBase = this.api_http.value+this.api_host.value, var apiBase = this.api_http.value+this.api_host.value,
accountURL = '/v2/accounts/?api_key='+this.api_key.value, accountURL = '/v2/accounts/?api_key='+this.api_key.value,
api_key = this.api_key.value; api_key = this.api_key.value,
self = this;
xmlhttp.open("GET", apiBase+accountURL,true); xmlhttp.open("GET", apiBase+accountURL,true);
xmlhttp.onreadystatechange=function() { xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState===4) { if (xmlhttp.readyState===4) {
try{ try {
var data = JSON.parse(xmlhttp.responseText); var data = JSON.parse(xmlhttp.responseText);
if(xmlhttp.status===200){ if(xmlhttp.status===200){
document.getElementById("plan-error").style.display = 'none';
// set plan info
document.getElementById("plan").value = "Plan: "+data.account.subscriptions.data[0].plan.id;
document.getElementById("plan").setAttribute("valid","true");
document.getElementById("rate-limit").value = "Limit: "+data.account.rate.limit;
document.getElementById("rate-remaining").value = "Remaining: "+data.account.rate.remaining;
if(data.account.rate.reset/60 < 60)
document.getElementById("rate-reset").value = "Resets in: "+Math.round(data.account.rate.reset/60)+" min.";
else
document.getElementById("rate-reset").value = "Resets in: "+Math.round(data.account.rate.reset/60/60)+" hr.";
document.getElementById("mailhops-membership-link").value='My Account'; document.getElementById("mailhops-membership-link").value='My Account';
document.getElementById("mailhops-membership-link").setAttribute('data-account-url','https://mailhops.com/account/'+api_key); document.getElementById("mailhops-membership-link").setAttribute('data-account-url','https://mailhops.com/account/'+api_key);
document.getElementById("key_details").innerHTML = JSON.stringify(data.account).replace(/\,/g,'\n');
document.getElementById("key_details").setAttribute("valid","true");
} else if(!!data.error){ } else if(!!data.error){
document.getElementById("mailhops-membership-link").value='Join MailHops'; self.planError(xmlhttp.status+': '+data.error.message);
document.getElementById("mailhops-membership-link").setAttribute('data-account-url','https://mailhops.com');
document.getElementById("key_details").innerHTML = xmlhttp.status+': '+data.error.message;
document.getElementById("key_details").setAttribute("valid","false");
} }
} } catch (e){
catch (ex){ self.planError('Connection Failed to\n '+apiBase+'!');
document.getElementById("key_details").innerHTML = 'Connection Failed to\n '+apiBase+'!';
document.getElementById("key_details").setAttribute("valid","false");
} }
} }
}; };
xmlhttp.send(null); xmlhttp.send(null);
} else { } else {
document.getElementById("key_details").innerHTML = 'Enter a valid API key above.'; this.planError('Enter a valid API key above.');
document.getElementById("key_details").setAttribute("valid","false");
} }
}, },

View File

@ -32,23 +32,35 @@
<description> <description>
API Key <label id="mailhops-membership-link" class="text-link plain" value="Join MailHops" data-account-url="https://mailhops.com"/> API Key <label id="mailhops-membership-link" class="text-link plain" value="Join MailHops" data-account-url="https://mailhops.com"/>
</description> </description>
<hbox align="center">
<grid flex="1"> <textbox id="mailhop.api_key" placeholder="Enter your MailHops API Member Key" width="500"/>
<columns> <button onclick="mailHopPreferences.saveAPIKey();">Save Key</button>
<column flex="1"/> </hbox>
<column/> <hbox align="center">
</columns> <description>
<rows> <label id="plan-error" class="text-error plain" value=""></label>
<row> </description>
<textbox id="mailhop.api_key" placeholder="Enter API Key"/> </hbox>
<button onclick="mailHopPreferences.saveAPIKey();">Save Key</button> <hbox align="center">
</row> <description>
<row> <label id="plan" class="plain" value=""></label>
<description id="key_details"></description> </description>
</row> </hbox>
</rows> <hbox align="center">
</grid> <description>
<label id="rate-limit" class="plain" value=""></label>
</description>
</hbox>
<hbox align="center">
<description>
<label id="rate-remaining" class="plain" value=""></label>
</description>
</hbox>
<hbox align="center">
<description>
<label id="rate-reset" class="plain" value=""></label>
</description>
</hbox>
</groupbox> </groupbox>
</tabpanel> </tabpanel>
@ -56,6 +68,8 @@
<groupbox flex="1" style="height:400px;overflow: scroll;"> <groupbox flex="1" style="height:400px;overflow: scroll;">
<caption label="Security"/> <caption label="Security"/>
<checkbox id="mailhop.country_tag" label="Tag messages with country origin" checked="false"/> <checkbox id="mailhop.country_tag" label="Tag messages with country origin" checked="false"/>
<checkbox id="mailhop.travel_time_junk" label="Tag messages that take longer than 10 seconds as Junk" checked="false"/>
<description>Mark messages as Junk from Countries checked below. <description>Mark messages as Junk from Countries checked below.
<label class="text-link plain" value="All " onclick="mailHopPreferences.countryListSelectAll(true);"/> <label class="text-link plain" value="All " onclick="mailHopPreferences.countryListSelectAll(true);"/>
<label class="text-link plain" value=" None" onclick="mailHopPreferences.countryListSelectAll(false);"/> <label class="text-link plain" value=" None" onclick="mailHopPreferences.countryListSelectAll(false);"/>
@ -482,13 +496,13 @@
<tabpanel> <tabpanel>
<groupbox> <groupbox>
<caption label="MailHops API Hosting"/> <caption label="MailHops API &amp; Hosting"/>
<!-- <description> <!-- <description>
MailHops API hosting is sponsored by MailHops API hosting is sponsored by
<image src="https://mailhops.com/images/sponsor.png" width="200"/> <image src="https://mailhops.com/images/sponsor.png" width="200"/>
</description> --> </description> -->
<grid flex="1"> <grid>
<columns> <columns>
<column/> <column/>
<column flex="1"/> <column flex="1"/>
@ -501,7 +515,7 @@
<menuitem value="http://" label="http://" /> <menuitem value="http://" label="http://" />
</menupopup> </menupopup>
</menulist> </menulist>
<textbox id="mailhop.api_host" value="api.mailhops.com" flex="1"/> <textbox id="mailhop.api_host" value="api.mailhops.com" flex="1" placeholder="Enter MailHops API host"/>
</row> </row>
<row> <row>
<hbox/> <hbox/>
@ -513,11 +527,13 @@
</rows> </rows>
</grid> </grid>
<spacer/>
<description> <description>
MailHops API uses <label id="forecastio" class="text-link plain" value="Forecast.IO"/> to get the weather of the sender, register for an API Key to enable this feature. MailHops API uses <label id="forecastio" class="text-link plain" value="Forecast.IO"/> to get the weather of the sender, register for an API Key to enable this feature.
</description> </description>
<textbox id="mailhop.fkey" value=""/> <textbox id="mailhop.fkey" value="" placeholder="Enter ForecastIO API Key"/>
<checkbox id="mailhop.debug" label="Debug" checked="false"/> <checkbox id="mailhop.debug" label="Debug" checked="false"/>
</groupbox> </groupbox>

View File

@ -301,16 +301,16 @@ var mailHopsDisplay =
displayText = ' Local message.'; displayText = ' Local message.';
} }
if(message.time>0){ if(message.time != null){
message.time = message.time/1000; message.time = message.time/1000;
if(message.time<60) if(message.time < 60)
distanceText += ' in '+message.time+' sec.'; distanceText += ' in '+message.time+' sec.';
else if(message.time<3600) //something is wrong if it takes this long else if(message.time<3600) //something is wrong if it takes this long
distanceText += ' in '+Math.round(message.time/60)+' min.'; distanceText += ' in '+Math.round(message.time/60)+' min.';
else //something is wrong if it takes this long else //something is wrong if it takes this long
distanceText += ' in '+Math.round(message.time/60/60)+' hrs.'; distanceText += ' in '+Math.round(message.time/60/60)+' hrs.';
} }
if(header_route) if(header_route)
this.mapLink.setAttribute("data-route", header_route); this.mapLink.setAttribute("data-route", header_route);
else else

View File

@ -202,17 +202,15 @@ getDistance: function(from, to, unit) {
return dist; return dist;
}, },
getXOriginatingCountryCode: function(route) { getOriginatingCountryCode: function(route) {
var countryCode = '';
if(route && route.length){ if(route && route.length){
route.forEach(function(e,i,arr){ for(var r=0; r<route.length; r++){
if(!route[i].local && !!route[i].countryCode){ if(typeof route[r].local == 'undefined' && typeof route[r].client == 'undefined' && !!route[r].countryCode){
countryCode = route[i].countryCode; return route[r].countryCode;
return;
} }
}); };
} }
return countryCode; return '';
} }
}; };