1
0
mirror of https://github.com/MailHops/mailhops-plugin.git synced 2025-05-21 00:20:21 -07:00

Added better error handling and cleaned up display

This commit is contained in:
Andrew Van Tassel 2011-03-04 10:53:50 -07:00
parent 0b1e072540
commit 71a662577f
8 changed files with 118 additions and 49 deletions

View File

@ -2,13 +2,13 @@
* @author: Andrew Van Tassel * @author: Andrew Van Tassel
* @email: andrew@andrewvantassel.com * @email: andrew@andrewvantassel.com
* @website: http://mailhops.com * @website: http://mailhops.com
* @TODO: Add caching of lookup, display country flag in column * @TODO: cache result and display country flag in column
*/ */
//import nativeJSON //import nativeJSON
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);
//IP regex //IP regex
var gIPRegEx=/(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}$/; var gIPRegEx=/(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}$/;
var gAllIPRegEx = /((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])/g; var gAllIPRegEx = /(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)\.(1\d{0,2}|2(?:[0-4]\d{0,1}|[6789]|5[0-5]?)?|[3-9]\d?|0)(\/(?:[012]\d?|3[012]?|[456789])){0,1}/g;
var mailHops = var mailHops =
{ {
@ -19,7 +19,7 @@ var mailHops =
isLoaded: false, isLoaded: false,
map: 'goog', map: 'goog',
unit: 'mi', unit: 'mi',
appVersion: 'MailHops Thunderbird 0.4' appVersion: 'MailHops Thunderbird 0.4.1'
} }
mailHops.startLoading = function() mailHops.startLoading = function()
@ -31,8 +31,7 @@ mailHops.startLoading = function()
mailHops.resultText = document.getElementById ( "mailhopsResultText" ) ; mailHops.resultText = document.getElementById ( "mailhopsResultText" ) ;
//get preferences //get preferences
mailHops.map = mailHops.getCharPref('mail.mailHops.map','goog'); mailHops.map = mailHops.getCharPref('mail.mailHops.map','goog');
mailHops.unit = mailHops.getCharPref('mail.mailHops.unit','mi'); mailHops.unit = mailHops.getCharPref('mail.mailHops.unit','mi');
} ; } ;
mailHops.StreamListener = mailHops.StreamListener =
@ -107,26 +106,29 @@ mailHops.dispRoute = function()
//get the originating IP address //get the originating IP address
if(headXOrigIP){ if(headXOrigIP){
var ip = headXOrigIP.match(gAllIPRegEx); var ip = headXOrigIP.match(gAllIPRegEx);
if(ip != null && ip.length>0) if(ip != null && ip.length != 0)
all_ips.push(ip[0]) all_ips.push( ip[0] );
} }
//loop through the received headers and parse for IP addresses //loop through the received headers and parse for IP addresses
if ( headReceived.length > 0 ){ if ( headReceived.length != 0 ){
for ( var i = 0 ; i < headReceived.length ; i++ ) { for ( var h=0; h<headReceived.length; h++ ) {
received_ips = headReceived[i].match(gAllIPRegEx); received_ips = headReceived[h].match(gAllIPRegEx);
//maybe multiple IPs in one Received: line //maybe multiple IPs in one Received: line
if(received_ips != null){ if(received_ips != null){
for (var p=0; p < received_ips.length; p++){ for ( var r=0; r<received_ips.length; r++ ) {
//if we don't already have the IP then add it to the array //if we don't already have the IP then add it to the array
if(gIPRegEx.test(received_ips[p]) && all_ips.indexOf(received_ips[p]) == -1 && mailHops.testIP(received_ips[p],headReceived[i])) if(gIPRegEx.test(received_ips[r]) && all_ips.indexOf(received_ips[r]) == -1 && mailHops.testIP(received_ips[r],headReceived[h])){
all_ips.push(received_ips[p]); all_ips.push( received_ips[r] );
}
} }
} }
} }
} }
if ( all_ips.length > 0 ){ if ( all_ips.length != 0 ){
mailHops.lookup ( all_ips ) ; mailHops.lookup ( all_ips ) ;
} } else {
mailHops.displayResult('chrome://mailhops/content/images/local.png',null,null,null,null,null);
}
}; };
//another ip check, dates will throw off the regex //another ip check, dates will throw off the regex
mailHops.testIP = function(ip,header){ mailHops.testIP = function(ip,header){
@ -151,26 +153,31 @@ mailHops.testIP = function(ip,header){
else else
retval = true; retval = true;
} }
catch(ex) catch(ex) {
{
retval = true; retval = true;
} }
return retval; return retval;
}; };
mailHops.displayResult = function ( distance, image, city, state, countryName, route ) mailHops.displayResult = function ( image, distance, city, state, countryName, route )
{ {
var displayText=''; var displayText='';
if(city && state) if(image.indexOf('error')!=-1) {
displayText = ' There was a problem connecting to MailHops.';
mailHops.container.removeAttribute("onclick");
}
else if(image.indexOf('local')!=-1) {
displayText = ' Local message.';
mailHops.container.setAttribute("onclick","launchMap('"+route.toString()+"');");
}
else {
if(city && state)
displayText = city+', '+state; displayText = city+', '+state;
else if(countryName) else if(countryName)
displayText = countryName; displayText = countryName;
if(distance && distance.miles > 0){
if(distance)
{
if(distance.miles > 0){
if(mailHops.unit=='mi') if(mailHops.unit=='mi')
displayText +=' ( '+addCommas(Math.round(distance.miles))+' mi traveled )'; displayText +=' ( '+addCommas(Math.round(distance.miles))+' mi traveled )';
else else
@ -180,11 +187,7 @@ mailHops.displayResult = function ( distance, image, city, state, countryName, r
displayText = ' Local message.'; displayText = ' Local message.';
mailHops.container.setAttribute("onclick","launchMap('"+route.toString()+"');"); mailHops.container.setAttribute("onclick","launchMap('"+route.toString()+"');");
} }
else
{
displayText = ' There was a problem.';
mailHops.container.removeAttribute("onclick");
}
mailHops.resultText.textContent = displayText; mailHops.resultText.textContent = displayText;
mailHops.resultImage.src=image; mailHops.resultImage.src=image;
} ; } ;
@ -268,23 +271,28 @@ mailHops.lookup = function(route){
xmlhttp.open("GET", 'http://api.mailhops.com/v1/lookup/?tb&app='+mailHops.appVersion+'&r='+route.toString(),true); xmlhttp.open("GET", 'http://api.mailhops.com/v1/lookup/?tb&app='+mailHops.appVersion+'&r='+route.toString(),true);
xmlhttp.onreadystatechange=function() { xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) { if (xmlhttp.readyState==4) {
var data = nativeJSON.decode(xmlhttp.responseText); try{
if(data && data.meta.code==200){ var data = nativeJSON.decode(xmlhttp.responseText);
for(var i=0; i<data.response.route.length;i++){ if(data && data.meta.code==200){
if(!data.response.route[i].private && !data.response.route[i].client){ for(var i=0; i<data.response.route.length;i++){
if(data.response.route[i].countryCode) if(!data.response.route[i].private && !data.response.route[i].client){
flag='chrome://mailhops/content/images/flags/'+data.response.route[i].countryCode.toLowerCase()+'.png'; if(data.response.route[i].countryCode)
city=data.response.route[i].city; flag='chrome://mailhops/content/images/flags/'+data.response.route[i].countryCode.toLowerCase()+'.png';
state=data.response.route[i].state; city=data.response.route[i].city;
countryName=data.response.route[i].countryName; state=data.response.route[i].state;
break; countryName=data.response.route[i].countryName;
} break;
} }
//display the result }
mailHops.displayResult(data.response.distance,flag,city,state,countryName,route); //display the result
} else { mailHops.displayResult(flag,data.response.distance,city,state,countryName,route);
//display the error } else {
mailHops.displayResult(null,'chrome://mailhops/content/images/error.png',null,null,null); //display the error
mailHops.displayResult('chrome://mailhops/content/images/error.png',null,null,null,null,null);
}
}
catch (ex){
mailHops.displayResult('chrome://mailhops/content/images/error.png',null,null,null,null,null);
} }
} }
}; };

View File

@ -0,0 +1,28 @@
if (!pref)
{
var pref = Components.classes["@mozilla.org/preferences-service;1"].getService ( Components.interfaces.nsIPrefBranch ) ;
}
var mailHopPreferences =
{
loadPreferences: function()
{
if(mailHops.getCharPref("mail.mailHops.map",'goog')=='goog')
document.getElementById("mailhop.map").selectedIndex = 0;
else
document.getElementById("mailhop.map").selectedIndex = 1;
if(mailHops.getCharPref("mail.mailHops.unit",'mi')=='mi')
document.getElementById("mailhop.unit").selectedIndex = 0;
else
document.getElementById("mailhop.unit").selectedIndex = 1;
} ,
savePreferences: function()
{
mailHopPreferences.setCharPref("mail.mailHops.map", document.getElementById("mailhop.map").selectedItem.value) ;
mailHopPreferences.setCharPref("mail.mailHops.unit", document.getElementById("mailhop.unit").selectedItem.value) ;
} ,
setCharPref: function( strName , strValue )
{
pref.setCharPref ( strName , strValue ) ;
}
}

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>
<?xml-stylesheet href="chrome://mailhops/skin/mailhops.css" type="text/css"?>
<dialog id="mailHopsPreferences"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
buttons="accept,cancel"
onload="mailHopPreferences.loadPreferences();"
ondialogaccept="mailHopPreferences.savePreferences();"
title="MailHops Preferences">
<script type="application/x-javascript" src="chrome://mailhops/content/mailhops.js"/>
<script type="application/x-javascript" src="chrome://mailhops/content/preferences.js"/>
<vbox width="200px">
<groupbox>
<caption label="Default Map"/>
<radiogroup id="mailhop.map">
<radio value="goog" label="Google" selected="true"/>
<radio value="bing" label="Bing"/>
</radiogroup>
</groupbox>
<groupbox>
<caption label="Default Unit"/>
<radiogroup id="mailhop.unit">
<radio value="mi" label="Miles" selected="true"/>
<radio value="km" label="Kilometers"/>
</radiogroup>
</groupbox>
</vbox>
</dialog>

View File

@ -8,7 +8,7 @@
<em:type>2</em:type> <em:type>2</em:type>
<em:name>MailHops</em:name> <em:name>MailHops</em:name>
<em:version>0.4</em:version> <em:version>0.4.1</em:version>
<em:description>MailHops maps the route an email took to get to you.</em:description> <em:description>MailHops maps the route an email took to get to you.</em:description>
<em:iconURL>chrome://mailhops/content/images/mailhops32.png</em:iconURL> <em:iconURL>chrome://mailhops/content/images/mailhops32.png</em:iconURL>

BIN
mailhops-0.2.xpi Normal file

Binary file not shown.

BIN
mailhops-0.3.xpi Normal file

Binary file not shown.

BIN
mailhops-0.4.1.xpi Normal file

Binary file not shown.

BIN
mailhops-0.4.xpi Normal file

Binary file not shown.