diff --git a/chrome/content/mailhops.js b/chrome/content/mailhops.js index 6d78bff..927740d 100644 --- a/chrome/content/mailhops.js +++ b/chrome/content/mailhops.js @@ -12,15 +12,15 @@ var mailHops = 'version':'MailHops Plugin 2.0.0', 'lan':'en', 'unit':'mi', - 'api_url':'https://api.mailhops.com', + 'api_http':'https://', + 'api_host':'api.mailhops.com', 'debug':false, 'country_tag':false, 'country_filter':[] }, message: { secure:[] - }, - client_location: null + } }; mailHops.LOG = function(msg) { @@ -51,6 +51,7 @@ mailHops.init = function() { mailHops.loadPref = function() { + mailHops.LOG('load MailHops prefs'); //get preferences mailHops.options.lan = mailHops.getCharPref('mail.mailHops.lang','en'); mailHops.options.unit = mailHops.getCharPref('mail.mailHops.unit','mi'); @@ -74,9 +75,9 @@ mailHops.loadPref = function() mailHops.options.debug = mailHops.getCharPref('mail.mailHops.debug','false')=='true'?true:false; - mailHops.options.client_location = mailHops.getCharPref('mail.mailHops.client_location',''); + mailHops.options.api_host = mailHops.getCharPref('mail.mailHops.api_host','api.mailhops.com'); - mailHops.options.api_url = mailHops.getCharPref('mail.mailHops.api_url','https://api.mailhops.com'); + mailHops.options.api_http = mailHops.getCharPref('mail.mailHops.api_http','https://'); mailHops.options.api_key = mailHops.getCharPref('mail.mailHops.api_key',''); @@ -86,12 +87,6 @@ mailHops.loadPref = function() mailHops.options.country_filter = mailHops.getCharPref('mail.mailHops.country_filter',[]); - if(mailHops.options.client_location == ''){ - mailHops.setClientLocation(function(response){ - mailHops.options.client_location=response; - }); - } - //init display mailHopsDisplay.init( mailHops.options ); }; @@ -123,12 +118,12 @@ mailHops.StreamListener = //failed to read input stream mailHops.LOG('StreamListener Error: '+JSON.stringify(e)); } - } , + }, onStartRequest: function ( request , context ) { this.content = "" ; this.found = false ; - } , + }, onStopRequest: function ( aRequest , aContext , aStatusCode ) { mailHops.headers = Components.classes["@mozilla.org/messenger/mimeheaders;1"].createInstance ( Components.interfaces.nsIMimeHeaders ) ; @@ -326,39 +321,6 @@ mailHops.getCharPref = function ( strName , strDefault ){ return ( value ) ; }; -mailHops.setClientLocation = function(cb){ - - var xmlhttp = new XMLHttpRequest(); - if (!pref){ - var pref = Components.classes["@mozilla.org/preferences-service;1"].getService( Components.interfaces.nsIPrefBranch ) ; - } - - xmlhttp.open("GET", mailHopsUtils.getAPIUrl(mailHops.options)+'/lookup/?'+mailHopsUtils.getAPIUrlParams(mailHops.options)+'&r=&c=1',true); - - xmlhttp.onreadystatechange=function() { - if (xmlhttp.readyState==4 && !!xmlhttp.responseText) { - try { - var data = JSON.parse(xmlhttp.responseText); - if(data && data.meta.code==200){ - //display the result - pref.setCharPref("mail.mailHops.client_location", JSON.stringify(data.response)) ; - cb(data.response); - } else { - pref.setCharPref("mail.mailHops.client_location", '') ; - cb(''); - } - } catch(e){ - pref.setCharPref("mail.mailHops.client_location", '') ; - cb(''); - } - } else { - pref.setCharPref("mail.mailHops.client_location", '') ; - cb(''); - } - }; - xmlhttp.send(null); -}; - //mailhops lookup mailHops.lookupRoute = function(header_route){ @@ -370,11 +332,6 @@ mailHops.lookupRoute = function(header_route){ if(mailHops.options.fkey != '') lookupURL += '&fkey='+mailHops.options.fkey; - if(mailHops.options.client_location != '') - lookupURL+='&c=0'; - - mailHops.LOG(lookupURL); - //check for cache var cached_results = mailHops.getResults(); @@ -389,6 +346,8 @@ mailHops.lookupRoute = function(header_route){ } } +mailHops.LOG("Calling MailHops API: "+lookupURL); + //call mailhops api for lookup var xmlhttp = new XMLHttpRequest(); @@ -407,12 +366,12 @@ mailHops.lookupRoute = function(header_route){ //display the result mailHopsDisplay.route(header_route, mailHops.message, data.response, data.meta, lookupURL); } else { - mailHops.LOG(JSON.stringify(data)); + mailHops.LOG("lookupRoute: "+JSON.stringify(data)); //display the error mailHopsDisplay.error(data); } } catch(e){ - mailHops.LOG(JSON.stringify(e)); + mailHops.LOG("lookupRoute: Error "+JSON.stringify(e)); mailHopsDisplay.error(); } } diff --git a/chrome/content/preferences.js b/chrome/content/preferences.js index 0029be4..b5f2af2 100644 --- a/chrome/content/preferences.js +++ b/chrome/content/preferences.js @@ -3,24 +3,22 @@ if (!pref) { } var mailHopPreferences = { - api_url: '', //mailhops api url - api_ssl: '', //ssl? + api_host: 'api.mailhops.com', //mailhops api url + api_http: 'https://', //ssl? api_key: '', //api key fkey: '', //forecast.io api key country_filter: [], loadPreferences: function(){ - this.api_url = document.getElementById("mailhop.api_url").value; + this.api_host = document.getElementById("mailhop.api_host").value; - this.api_ssl = document.getElementById("mailhop.api_ssl").value; + this.api_http = document.getElementById("mailhop.api_http"); this.api_key = document.getElementById("mailhop.api_key").value; this.fkey = document.getElementById("mailhop.fkey").value; - document.getElementById("mailhop.api_ssl").value = "true"; - document.getElementById("mailhop.lang").value = pref.getCharPref("mail.mailHops.lang",'en'); document.getElementById("mailhop.map_provider").value = pref.getCharPref("mail.mailHops.map_provider",'OpenStreetMap.Mapnik'); @@ -86,16 +84,15 @@ var mailHopPreferences = { // API info this.api_key = pref.getCharPref("mail.mailHops.api_key",''); - this.api_url = pref.getCharPref("mail.mailHops.api_url",'https://api.mailhops.com'); - - if(this.api_url.indexOf('https')===0) - this.api_ssl = "true"; + this.api_http.value = pref.getCharPref("mail.mailHops.api_http",'https://'); + if(this.api_http.value=='https://') + this.api_http.selectedIndex = 0; else - this.api_ssl = "false"; + this.api_http.selectedIndex = 1; - this.api_url = this.api_url.replace('http://','').replace('https://',''); + this.api_host = pref.getCharPref("mail.mailHops.api_host",'api.mailhops.com'); - this.fkey = pref.getCharPref("mail.mailHops.fkey",''); + this.fkey = pref.getCharPref("mail.mailHops.fkey",''); // Country Filter and tagging this.country_filter = JSON.parse(pref.getCharPref("mail.mailHops.country_filter",null) || []); @@ -111,8 +108,6 @@ var mailHopPreferences = { document.getElementById("mailhop.country_tag").checked = true; saveAPIKey(); - - ResetLocation(document.getElementById("mailhop.refresh_location")); }, savePreferences: function() { pref.setCharPref("mail.mailHops.lang", document.getElementById("mailhop.lang").selectedItem.value); @@ -131,12 +126,8 @@ var mailHopPreferences = { //API vars pref.setCharPref("mail.mailHops.api_key", this.api_key); - - this.api_url = this.api_url.replace('http://','').replace('https://',''); - if(this.api_ssl=="true") - pref.setCharPref("mail.mailHops.api_url", 'https://'+this.api_url); - else - pref.setCharPref("mail.mailHops.api_url", 'http://'+this.api_url); + pref.setCharPref("mail.mailHops.api_http", this.api_http.value); + pref.setCharPref("mail.mailHops.api_host", this.api_host); pref.setCharPref("mail.mailHops.fkey", String(this.fkey)); @@ -164,17 +155,12 @@ function saveAPIKey() { if(!!mailHopPreferences.api_key && mailHopPreferences.api_key != ''){ var xmlhttp = new XMLHttpRequest(); var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); - var apiBase = mailHopPreferences.api_url, - accountURL = '/v2/accounts/?app='+mailHops.options.version+'&api_key='+mailHopPreferences.api_key; - - if(mailHopPreferences.api_ssl=="true") - apiBase='https://'+apiBase; - else - apiBase='http://'+apiBase; + var apiBase = mailHopPreferences.api_http.value+mailHopPreferences.api_host, + accountURL = '/v2/accounts/?api_key='+mailHopPreferences.api_key; xmlhttp.open("GET", apiBase+accountURL,true); xmlhttp.onreadystatechange=function() { - if (xmlhttp.readyState==4 && !!xmlhttp.responseText) { + if (xmlhttp.readyState==4) { try{ var data = JSON.parse(xmlhttp.responseText); if(!!data && data.meta.code==200){ @@ -188,8 +174,6 @@ function saveAPIKey() { catch (ex){ document.getElementById("key_details").innerHTML = 'Connection Failed to\n '+apiBase+'!'; } - } else { - document.getElementById("key_details").innerHTML = 'Connection Failed to\n '+apiBase+'!'; } }; xmlhttp.send(null); @@ -198,91 +182,37 @@ function saveAPIKey() { } } -function TestConnection(e){ +function TestConnection(){ var xmlhttp = new XMLHttpRequest(); var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); - var apiBase = mailHopPreferences.api_url || 'https://api.mailhops.com', - lookupURL = '/v1/lookup/?app='+mailHops.options.version+'&healthcheck'; - - if(mailHopPreferences.api_ssl=="true") - apiBase='https://'+apiBase; - else - apiBase='http://'+apiBase; + var apiBase = mailHopPreferences.api_http.value+mailHopPreferences.api_host, + lookupURL = '/v1/lookup/?healthcheck'; if(mailHopPreferences.api_key != '') - lookupURL = '/v2/lookup/?app='+mailHops.options.version+'&healthcheck&api_key='+mailHopPreferences.api_key; + lookupURL = '/v2/lookup/?healthcheck&api_key='+mailHopPreferences.api_key; xmlhttp.open("GET", apiBase+lookupURL,true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { - try{ - var data = JSON.parse(xmlhttp.responseText); - if(!!data && data.meta.code==200){ - e.style.backgroundImage=''; - alert('Connection Succeeded to\n '+apiBase+'!'); - } else { - //display the error - e.style.backgroundImage=''; - alert('Connection Failed to\n '+apiBase+'!'); - } - } - catch (ex){ - e.style.backgroundImage=''; - alert('Connection Failed to\n '+apiBase+'!'); - } - } + try{ + var data = JSON.parse(xmlhttp.responseText); + if(!!data && data.meta.code==200){ + alert('Connection Succeeded to\n '+apiBase+'!'); + } else { + //display the error + alert('Connection Failed to\n '+apiBase+'!'); + } + } + catch (ex){ + alert('Connection Failed to\n '+apiBase+'! '+JSON.stringify(ex)); + } + } }; xmlhttp.send(null); } -function ResetLocation(e){ - - //clear the location - document.getElementById("mailhop.client_location").value='Getting your location...'; - document.getElementById("mailhop.client_location_ip").value = ''; - document.getElementById("mailhop.client_location_host").value = ''; - document.getElementById("mailhop.client_location_whois").value = ''; - - var MH_APIURL = mailHopPreferences.api_ssl=="true"?'https://'+mailHopPreferences.api_url:'http://'+mailHopPreferences.api_url; - - mailHops.setClientLocation(function(response){ - - if(response){ - var location = ''; - if(response.route[0].city) - location+=response.route[0].city; - if(response.route[0].state) - location+=', '+response.route[0].state; - if(response.route[0].countryName) - location+=' ( '+response.route[0].countryName+' )'; - else if(response.route[0].countryCode) - location+=' ( '+response.route[0].countryCode+' )'; - - //set location - document.getElementById("mailhop.client_location").value=location; - - //set ip - document.getElementById("mailhop.client_location_ip").value='IP: '+response.route[0].ip; - - //set host - if(response.route[0].host) - document.getElementById("mailhop.client_location_host").value='Host: '+response.route[0].host; - - document.getElementById("mailhop.client_location_whois").value = 'whois'; - document.getElementById("mailhop.client_location_whois").setAttribute('href', 'https://www.mailhops.com/whois/'+response.route[0].ip); - - //set country flag - if(response.route[0].countryCode) - document.getElementById("mailhop.client_location").style.backgroundImage='url(chrome://mailhops/content/images/flags/'+response.route[0].countryCode.toLowerCase()+'.png)'; - - } else { - document.getElementById("mailhop.client_location").value='Failed connecting...'; - } - },MH_APIURL); -} - function ResetConnection(){ - mailHopPreferences.api_ssl=="true"; - mailHopPreferences.api_ssl.selectedIndex = 0; - mailHopPreferences.api_url='api.mailhops.com'; + mailHopPreferences.api_http.value=="https://"; + mailHopPreferences.api_http.selectedIndex = 0; + mailHopPreferences.api_host='api.mailhops.com'; } diff --git a/chrome/content/preferences.xul b/chrome/content/preferences.xul index cfd046c..eb09e11 100644 --- a/chrome/content/preferences.xul +++ b/chrome/content/preferences.xul @@ -11,7 +11,6 @@ title="MailHops Preferences">