diff --git a/chrome/content/mailhops.js b/chrome/content/mailhops.js index 0bba154..6d78bff 100644 --- a/chrome/content/mailhops.js +++ b/chrome/content/mailhops.js @@ -6,11 +6,21 @@ var mailHops = { - msgURI: null - , isLoaded: false - , options: {'version':'MailHops Plugin 2.0.0','lan':'en','unit':'mi','api_url':'https://api.mailhops.com','debug':false} - , message: { secure:[] } - , client_location: null + msgURI: null, + isLoaded: false, + options: { + 'version':'MailHops Plugin 2.0.0', + 'lan':'en', + 'unit':'mi', + 'api_url':'https://api.mailhops.com', + 'debug':false, + 'country_tag':false, + 'country_filter':[] + }, + message: { + secure:[] + }, + client_location: null }; mailHops.LOG = function(msg) { @@ -72,6 +82,10 @@ mailHops.loadPref = function() mailHops.options.map_provider = mailHops.getCharPref('mail.mailHops.map_provider','OpenStreetMap.Mapnik'); + mailHops.options.country_tag = mailHops.getCharPref('mail.mailHops.country_tag','false')=='true'?true:false; + + mailHops.options.country_filter = mailHops.getCharPref('mail.mailHops.country_filter',[]); + if(mailHops.options.client_location == ''){ mailHops.setClientLocation(function(response){ mailHops.options.client_location=response; @@ -322,22 +336,25 @@ mailHops.setClientLocation = function(cb){ xmlhttp.open("GET", mailHopsUtils.getAPIUrl(mailHops.options)+'/lookup/?'+mailHopsUtils.getAPIUrlParams(mailHops.options)+'&r=&c=1',true); xmlhttp.onreadystatechange=function() { - if (xmlhttp.readyState==4) { - try { + 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(''); - } - } + 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); }; @@ -419,20 +436,30 @@ mailHops.saveResults = function(results,route){ //Add tag if(!!route){ try{ - var tagService = Components.classes["@mozilla.org/messenger/tagservice;1"].getService(Components.interfaces.nsIMsgTagService); - if(!tagService) - return; var countryCode = mailHopsUtils.getXOriginatingCountryCode(route); - mailHops.LOG(tagService.getKeyForTag(countryCode)) - if(!tagService.getKeyForTag(countryCode)) - tagService.addTag(countryCode,'',0); - var msg = Components.classes["@mozilla.org/array;1"].createInstance(Components.interfaces.nsIMutableArray); - msg.clear(); - msg.appendElement(msgHdr, false); - msgHdr.folder.addKeywordsToMessages(msg, countryCode ); - mailHops.LOG( "Added CountryCode tag: "+countryCode ); - ReloadMessage(); + msg.clear(); + msg.appendElement(msgHdr, false); + + if(!!mailHops.options.country_tag) + { + var tagService = Components.classes["@mozilla.org/messenger/tagservice;1"].getService(Components.interfaces.nsIMsgTagService); + if(!tagService) + return; + mailHops.LOG(tagService.getKeyForTag(countryCode)) + if(!tagService.getKeyForTag(countryCode)) + tagService.addTag(countryCode,'',0); + + msgHdr.folder.addKeywordsToMessages(msg, countryCode ); + mailHops.LOG( "Added CountryCode tag: "+countryCode ); + } + + if(!!mailHops.options.country_filter && mailHops.options.country_filter.length){ + if(mailHops.options.country_filter.indexOf(countryCode.toLowerCase()) !== -1){ + msgHdr.folder.setJunkScoreForMessages(msg, "100"); + } + } + } catch(e){ mailHops.LOG( "Error adding CountryCode tag: "+e ); } diff --git a/chrome/content/preferences.js b/chrome/content/preferences.js index 275e171..0029be4 100644 --- a/chrome/content/preferences.js +++ b/chrome/content/preferences.js @@ -3,19 +3,21 @@ if (!pref) { } var mailHopPreferences = { - api_url: null, - api_ssl: null, + api_url: '', //mailhops api url + api_ssl: '', //ssl? + api_key: '', //api key fkey: '', //forecast.io api key + country_filter: [], loadPreferences: function(){ - this.api_url = document.getElementById("mailhop.api_url"); + this.api_url = document.getElementById("mailhop.api_url").value; - this.api_ssl = document.getElementById("mailhop.api_ssl"); + this.api_ssl = document.getElementById("mailhop.api_ssl").value; - this.api_key = document.getElementById("mailhop.api_key"); + this.api_key = document.getElementById("mailhop.api_key").value; - this.fkey = document.getElementById("mailhop.fkey"); + this.fkey = document.getElementById("mailhop.fkey").value; document.getElementById("mailhop.api_ssl").value = "true"; @@ -81,18 +83,34 @@ var mailHopPreferences = { else document.getElementById("mailhop.debug").checked = false; - this.api_key.value = pref.getCharPref("mail.mailHops.api_key",''); + // API info + this.api_key = pref.getCharPref("mail.mailHops.api_key",''); - this.api_url.value = pref.getCharPref("mail.mailHops.api_url",'https://api.mailhops.com'); + this.api_url = pref.getCharPref("mail.mailHops.api_url",'https://api.mailhops.com'); - if(this.api_url.value.indexOf('https')!==-1) - this.api_ssl.value = "true"; + if(this.api_url.indexOf('https')===0) + this.api_ssl = "true"; else - this.api_ssl.value = "false"; + this.api_ssl = "false"; - this.api_url.value = this.api_url.value.replace('http://','').replace('https://',''); + this.api_url = this.api_url.replace('http://','').replace('https://',''); - this.fkey.value = 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) || []); + if(this.country_filter.length){ + for(c in this.country_filter){ + document.getElementById("country_"+this.country_filter[c]).checked=true; + } + } + + if(pref.getCharPref("mail.mailHops.country_tag",'false')=='false') + document.getElementById("mailhop.country_tag").checked = false; + else + document.getElementById("mailhop.country_tag").checked = true; + + saveAPIKey(); ResetLocation(document.getElementById("mailhop.refresh_location")); }, @@ -112,33 +130,87 @@ var mailHopPreferences = { pref.setCharPref("mail.mailHops.debug", String(document.getElementById("mailhop.debug").checked)); //API vars - pref.setCharPref("mail.mailHops.api_key", this.api_key.value); + pref.setCharPref("mail.mailHops.api_key", this.api_key); - this.api_url.value = this.api_url.value.replace('http://','').replace('https://',''); - if(this.api_ssl.value=="true") - pref.setCharPref("mail.mailHops.api_url", 'https://'+this.api_url.value); + 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.value); + pref.setCharPref("mail.mailHops.api_url", 'http://'+this.api_url); - pref.setCharPref("mail.mailHops.fkey", String(document.getElementById("mailhop.fkey").value)); + pref.setCharPref("mail.mailHops.fkey", String(this.fkey)); + + // Country Filter and tagging + this.country_filter = []; + for(c in mailHopsUtils.countries){ + if(document.getElementById("country_"+mailHopsUtils.countries[c]).checked) + this.country_filter.push(document.getElementById("country_"+mailHopsUtils.countries[c]).getAttribute('value')); + } + 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)); return true; - } + }, + countryListSelectAll: function(all){ + for(c in mailHopsUtils.countries){ + document.getElementById("country_"+mailHopsUtils.countries[c]).checked=all; + } + } }; +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; + + xmlhttp.open("GET", apiBase+accountURL,true); + xmlhttp.onreadystatechange=function() { + if (xmlhttp.readyState==4 && !!xmlhttp.responseText) { + try{ + var data = JSON.parse(xmlhttp.responseText); + if(!!data && data.meta.code==200){ + document.getElementById("key_details").innerHTML = JSON.stringify(data.account).replace(/\,/g,'\n'); + } else if(!!data.meta.message){ + document.getElementById("key_details").innerHTML = data.meta.message; + } else { + document.getElementById("key_details").innerHTML = 'Invalid API Key'; + } + } + 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); + } else { + document.getElementById("key_details").innerHTML = 'Enter a valid API key above.'; + } +} + function TestConnection(e){ var xmlhttp = new XMLHttpRequest(); var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); - var apiBase = mailHopPreferences.api_url.value, lookupURL = '/v1/lookup/?app='+mailHops.options.version+'&healthcheck'; + var apiBase = mailHopPreferences.api_url || 'https://api.mailhops.com', + lookupURL = '/v1/lookup/?app='+mailHops.options.version+'&healthcheck'; - if(mailHopPreferences.api_ssl.value=="true") + if(mailHopPreferences.api_ssl=="true") apiBase='https://'+apiBase; else apiBase='http://'+apiBase; - if(mailHopPreferences.api_key.value != '') - lookupURL = '/v2/lookup/?app='+mailHops.options.version+'&healthcheck&api_key='+mailHopPreferences.api_key.value; + if(mailHopPreferences.api_key != '') + lookupURL = '/v2/lookup/?app='+mailHops.options.version+'&healthcheck&api_key='+mailHopPreferences.api_key; xmlhttp.open("GET", apiBase+lookupURL,true); xmlhttp.onreadystatechange=function() { @@ -171,7 +243,7 @@ function ResetLocation(e){ document.getElementById("mailhop.client_location_host").value = ''; document.getElementById("mailhop.client_location_whois").value = ''; - var MH_APIURL = mailHopPreferences.api_ssl.value=="true"?'https://'+mailHopPreferences.api_url.value:'http://'+mailHopPreferences.api_url.value; + var MH_APIURL = mailHopPreferences.api_ssl=="true"?'https://'+mailHopPreferences.api_url:'http://'+mailHopPreferences.api_url; mailHops.setClientLocation(function(response){ @@ -210,7 +282,7 @@ function ResetLocation(e){ } function ResetConnection(){ - mailHopPreferences.api_ssl.value=="true"; + mailHopPreferences.api_ssl=="true"; mailHopPreferences.api_ssl.selectedIndex = 0; - mailHopPreferences.api_url.value='api.mailhops.com'; + mailHopPreferences.api_url='api.mailhops.com'; } diff --git a/chrome/content/preferences.xul b/chrome/content/preferences.xul index b09589e..cfd046c 100644 --- a/chrome/content/preferences.xul +++ b/chrome/content/preferences.xul @@ -21,6 +21,7 @@ + @@ -31,33 +32,318 @@ - MailHops - + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + Mark messages as Junk from Countries checked below. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/chrome/content/utils.js b/chrome/content/utils.js index 18842d3..26b52e9 100644 --- a/chrome/content/utils.js +++ b/chrome/content/utils.js @@ -1,5 +1,7 @@ var mailHopsUtils = { +countries: ["ad","ae","af","ag","ai","al","am","an","ao","ar","as","at","au","aw","ax","az","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","catalonia","cc","cd","cf","cg","ch","ci","ck","cl","cm","cn","co","cr","cs","cu","cv","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","england","er","es","et","europeanunion","fam","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","in","io","iq","ir","is","it","jm","jo","jp","ke","kg","kh","ki","km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","me","mg","mh","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","rs","ru","rw","sa","sb","sc","scotland","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","sv","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tr","tt","tv","tw","tz","ua","ug","um","us","uy","uz","va","vc","ve","vg","vi","vn","vu","wales","wf","ws","ye","yt","za","zm","zw"], + dkim: function(result){ switch(result){