/* * @author: Andrew Van Tassel * @email: andrew@andrewvantassel.com * @website: http://mailhops.com */ var mailHops = { msgURI: null, resultTextDataPane: null, resultTextDataPane2: null, resultContainerDataPane: null, resultDetailsLink: null, resultContainerDetails: null, resultDetails: null, resultMapLink: null, mailhopsDataPaneSPF: null, mailhopsDataPaneDKIM: null, mailhopsDataPaneMailer: null, mailhopsDataPaneDNSBL: null, mailhopsListContainer: null, mailhopsAuthContainer: null, resultListDataPane: null, resultMeta: null, isLoaded: false, options: {'map':'goog','unit':'mi','api_url':'http://api.mailhops.com'}, appVersion: 'MailHops Postbox 0.8', message: {secure:[]}, client_location: null } mailHops.init = function() { //load preferences mailHops.loadPref(); mailHops.isLoaded = true; mailHops.resultContainerDataPane = document.getElementById ( "mailhopsDataPane"); mailHops.resultTextDataPane = document.getElementById ( "mailhopsDataPaneText"); mailHops.resultTextDataPane2 = document.getElementById ( "mailhopsDataPaneText2"); mailHops.resultDetailsLink = document.getElementById ( "mailhopsDataPaneDetailsLink"); mailHops.resultContainerDetails = document.getElementById ( "mailhopsDetailsContainer"); mailHops.resultDetails = document.getElementById ( "mailhopsDataPaneDetails"); mailHops.resultMeta = document.getElementById ( "mailhopsDataPaneMeta"); mailHops.resultMapLink = document.getElementById ( "mailhopsDataPaneMapLink"); //auth mailHops.mailhopsAuthContainer = document.getElementById ( "dataPaneMailHopsAuthContainer"); mailHops.mailhopsDataPaneSPF = document.getElementById ( "mailhopsDataPaneSPF"); mailHops.mailhopsDataPaneDKIM = document.getElementById ( "mailhopsDataPaneDKIM"); mailHops.mailhopsDataPaneMailer = document.getElementById ( "mailhopsDataPaneMailer"); mailHops.mailhopsDataPaneDNSBL = document.getElementById ( "mailhopsDataPaneDNSBL"); //list mailHops.mailhopsListContainer = document.getElementById ( "dataPaneMailHopsListContainer"); mailHops.resultListDataPane = document.getElementById ( "mailhopsListDataPane"); //event listner for route click to launch map mailHops.resultMapLink.addEventListener("click", function () { if(this.hasAttribute("data-route")) mailHops.launchMap(String(this.getAttribute("data-route"))); } , false); mailHops.resultDetailsLink.addEventListener("click", function () { if(mailHops.resultContainerDetails.style.display=='none'){ mailHops.resultContainerDetails.style.display = 'block'; mailHops.resultDetailsLink.setAttribute('class','text-link dataPaneMoreLink active'); } else{ mailHops.resultContainerDetails.style.display = 'none'; mailHops.resultDetailsLink.setAttribute('class','text-link dataPaneMoreLink'); } } , false); mailHops.mailhopsDataPaneDNSBL.addEventListener("click", function () { if(this.hasAttribute('data-ip')) mailHops.launchSpamHausURL(this.getAttribute('data-ip')); } , false); document.getElementById("mailhopsDataPanePrefsLink").addEventListener("click", function () { window.openDialog("chrome://mailhops/content/preferences.xul","mailHopsPreferences",null,null); } , false); }; mailHops.loadPref = function() { //get preferences mailHops.options.map = mailHops.getCharPref('mail.mailHops.map','goog'); mailHops.options.unit = mailHops.getCharPref('mail.mailHops.unit','mi'); //Display Boxes mailHops.options.show_details = mailHops.getCharPref('mail.mailHops.show_details','false')=='true'?true:false; mailHops.options.show_meta = mailHops.getCharPref('mail.mailHops.show_meta','false')=='true'?true:false; mailHops.options.show_auth = mailHops.getCharPref('mail.mailHops.show_auth','true')=='true'?true:false; mailHops.options.show_lists = mailHops.getCharPref('mail.mailHops.show_lists','true')=='true'?true:false; //Details options mailHops.options.show_host = mailHops.getCharPref('mail.mailHops.show_host','false')=='true'?true:false; mailHops.options.show_weather = mailHops.getCharPref('mail.mailHops.show_weather','false')=='true'?true:false; mailHops.options.show_secure = mailHops.getCharPref('mail.mailHops.show_secure','false')=='true'?true:false; //Auth options mailHops.options.show_dkim = mailHops.getCharPref('mail.mailHops.show_dkim','true')=='true'?true:false; mailHops.options.show_spf = mailHops.getCharPref('mail.mailHops.show_spf','true')=='true'?true:false; mailHops.options.show_mailer = mailHops.getCharPref('mail.mailHops.show_mailer','true')=='true'?true:false; mailHops.options.show_dnsbl = mailHops.getCharPref('mail.mailHops.show_dnsbl','true')=='true'?true:false; //Hosting mailHops.options.use_private = mailHops.getCharPref('mail.mailHops.use_private','false')=='true'?true:false; mailHops.options.hosting = mailHops.getCharPref('mail.mailHops.hosting','personal'); mailHops.options.client_location = mailHops.getCharPref('mail.mailHops.client_location',''); if(mailHops.options.use_private) mailHops.options.api_url = mailHops.getCharPref('mail.mailHops.api_url','http://api.mailhops.com'); else mailHops.options.api_url='http://api.mailhops.com'; if(mailHops.options.client_location == ''){ mailHops.setClientLocation(); } }; mailHops.StreamListener = { content: "" , found: false , onDataAvailable: function ( request , context , inputStream , offset , count ) { try { var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance ( Components.interfaces.nsIScriptableInputStream ) ; sis.init ( inputStream ) ; if ( ! this.found ) { this.content += sis.read ( count ) ; this.content = this.content.replace ( /\r/g , "" ) ; var pos = this.content.indexOf ( "\n\n" ) ; if ( pos > -1 ) { // last header line must end with LF -> pos+1 !!! this.content = this.content.substr ( 0 , pos + 1 ) ; this.found = true ; } } } catch ( ex ) { } } , 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 ) ; mailHops.headers.initialize ( this.content , this.content.length ) ; mailHops.getRoute() ; } }; /** * loop through the header, find out if we have received-from headers */ mailHops.loadHeaderData = function() { var msgURI = null ; if ( gDBView ) { msgURI = gDBView.URIForFirstSelectedMessage ; } if ( msgURI == null ) { return ; } mailHops.msgURI = msgURI; var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance ( Components.interfaces.nsIMessenger ) ; var msgService = messenger.messageServiceFromURI ( msgURI ) ; msgService.CopyMessage ( msgURI , mailHops.StreamListener , false , null , msgWindow , {} ) ; }; mailHops.getRoute = function(){ //IP regex var regexIp=/(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 regexAllIp = /(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 headReceived = mailHops.headers.extractHeader ( "Received" , true ) ; var headXOrigIP = mailHops.headers.extractHeader ( "X-Originating-IP" , false ) ; //auth box 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 headXMimeOLE = (mailHops.options.show_auth && mailHops.options.show_mailer) ? mailHops.headers.extractHeader ( "X-MimeOLE" , false ) : null; var headReceivedSPF = (mailHops.options.show_auth && mailHops.options.show_spf) ? mailHops.headers.extractHeader ( "Received-SPF" , false ) : null; var headAuth = mailHops.options.show_auth ? mailHops.headers.extractHeader ( "Authentication-Results" , false ) : null; //lists box var headListUnsubscribe = mailHops.options.show_lists ? mailHops.headers.extractHeader ( "List-Unsubscribe" , false ) : null; //display auth if(mailHops.options.show_auth) mailHops.displayResultAuth(headXMailer,headUserAgent,headXMimeOLE,headAuth,headReceivedSPF); else mailHops.mailhopsAuthContainer.style.display='none'; //display unsubscribe link if(mailHops.options.show_lists) mailHops.displayResultLists(headListUnsubscribe); else mailHops.mailhopsListContainer.style.display='none'; var received_ips; var all_ips = new Array(); var rline=''; //empty secure mailHops.message.secure = []; //loop through the received headers and parse for IP addresses if ( headReceived ){ var headReceivedArr = headReceived.split('\n'); if(headReceivedArr.length != 0){ for ( var h=0; h',''); var label = document.createElement('label'); label.setAttribute('class','text-link dataPaneURLitem'); if(href.indexOf('mailto:')!=-1){ label.setAttribute('value','Unsubscribe via Email'); if(href.toLowerCase().indexOf('subject=')==-1){ if(href.indexOf('?')==-1) href+='?subject=Unsubscribe'; else href+='&subject=Unsubscribe'; } } else{ label.setAttribute('value','Unsubscribe via Web'); } label.setAttribute('tooltiptext',href); label.setAttribute('href',href); mailHops.resultListDataPane.appendChild(label); } } } }; mailHops.displayResultAuth = function( header_xmailer, header_useragent, header_xmimeole, header_auth, header_spf ){ mailHops.mailhopsAuthContainer.style.display=''; //SPF if(header_spf){ header_spf=header_spf.replace(/^\s+/,""); var headerSPFArr=header_spf.split(' '); mailHops.mailhopsDataPaneSPF.setAttribute('value','SPF: '+headerSPFArr[0]); mailHops.mailhopsDataPaneSPF.style.backgroundImage = 'url(chrome://mailhops/content/images/auth/'+headerSPFArr[0]+'.png)'; mailHops.mailhopsDataPaneSPF.setAttribute('tooltiptext',header_spf+'\n'+mailHops.authExplainSPF(headerSPFArr[0])); mailHops.mailhopsDataPaneSPF.style.display='block'; } else{ mailHops.mailhopsDataPaneSPF.style.display='none'; } //Authentication-Results //http://tools.ietf.org/html/rfc5451 if(header_auth){ var headerAuthArr=header_auth.split(';'); var dkim_result; var spf_result; for(var h=0;h 0){ if(mailHops.options.client_location){ var client_location = JSON.parse(mailHops.options.client_location); response.route.push(client_location.route[0]); } for(var i=0; i 0){ if(mailHops.options.unit=='mi') distanceText =' ( '+mailHops.addCommas(Math.round(response.distance.miles))+' mi traveled )'; else distanceText =' ( '+mailHops.addCommas(Math.round(response.distance.kilometers))+' km traveled )'; } else if(displayText=='') displayText = ' Local message.'; } if(header_route) mailHops.resultMapLink.setAttribute("data-route", header_route); else mailHops.resultMapLink.removeAttribute("data-route"); mailHops.resultTextDataPane.style.backgroundImage = 'url('+image+')'; mailHops.resultTextDataPane.value = displayText; mailHops.resultTextDataPane.setAttribute('tooltiptext',displayText+' '+distanceText); if(distanceText){ mailHops.resultTextDataPane2.style.display = 'block'; mailHops.resultTextDataPane2.value = distanceText; mailHops.resultTextDataPane2.setAttribute('tooltiptext',displayText+' '+distanceText); } else { mailHops.resultTextDataPane2.style.display = 'none'; } //show the detail link mailHops.resultDetailsLink.style.display = 'block'; mailHops.resultMapLink.style.display = 'block'; //show details by default if(mailHops.options.show_details){ mailHops.resultContainerDetails.style.display = 'block'; mailHops.resultDetailsLink.setAttribute('class','text-link dataPaneMoreLink active'); } else{ mailHops.resultContainerDetails.style.display = 'none'; mailHops.resultDetailsLink.setAttribute('class','text-link dataPaneMoreLink'); } }; mailHops.getSecureTrans = function(ip){ for(var i=0; i7 && d.getHours()<19) return true; else return false; }; //display the connection error message mailHops.displayError = function(data){ mailHops.resultMapLink.removeAttribute("route"); if(data && data.meta.code==410) mailHops.resultTextDataPane.style.backgroundImage = 'url(chrome://mailhops/content/images/info.png)'; else mailHops.resultTextDataPane.style.backgroundImage = 'url(chrome://mailhops/content/images/auth/error.png)'; if(data && data.error){ mailHops.resultTextDataPane.value = mailHops.getErrorTitle(data.meta.code); mailHops.resultTextDataPane.setAttribute('tooltiptext',data.error.message); }else{ mailHops.resultTextDataPane.value = ' Service Unavailable.'; mailHops.resultTextDataPane.setAttribute('tooltiptext',' Could not connect to MailHops.'); } mailHops.resultTextDataPane2.style.display = 'none'; mailHops.resultTextDataPane2.value = ''; mailHops.resultTextDataPane2.style.backgroundImage = ''; mailHops.resultTextDataPane2.setAttribute('tooltiptext',''); }; mailHops.getErrorTitle = function(error_code){ switch(error_code){ case 400: return 'Missing route parameter'; case 410: return 'Down for Maintenance'; case 500: return 'Server Error'; default: return 'Service Unavailable'; } }; mailHops.clearRoute = function(){ mailHops.resultTextDataPane2.style.display = 'none'; mailHops.resultContainerDetails.style.display = 'none'; mailHops.resultDetailsLink.style.display = 'none'; mailHops.resultMapLink.style.display = 'none'; mailHops.mailhopsDataPaneDNSBL.style.display = 'none'; mailHops.resultTextDataPane.style.backgroundImage = 'url(chrome://mailhops/content/images/loader.gif)'; mailHops.resultTextDataPane.value = ' Looking Up Route'; mailHops.resultTextDataPane.setAttribute('tooltiptext','Looking Up Route'); mailHops.resultTextDataPane2.value = ''; mailHops.resultTextDataPane2.style.backgroundImage = ''; mailHops.resultTextDataPane2.setAttribute('tooltiptext',''); //remove child details while(mailHops.resultDetails.firstChild) { mailHops.resultDetails.removeChild(mailHops.resultDetails.firstChild); } }; mailHops.setupEventListener = function(){ if ( mailHops.isLoaded ){ return ; } mailHops.init() ; mailHops.registerObserver() ; var listener = {} ; listener.onStartHeaders = function() { mailHops.clearRoute() ; } ; listener.onEndHeaders = mailHops.loadHeaderData ; gMessageListeners.push ( listener ) ; }; //preferences observers mailHops.registerObserver = function(){ var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService ( Components.interfaces.nsIPrefService ) ; mailHops._branch = prefService.getBranch ( "mail.mailHops." ) ; mailHops._branch.QueryInterface ( Components.interfaces.nsIPrefBranchInternal ) ; mailHops._branch.addObserver ( "" , mailHops , false ) ; }; mailHops.unregisterObserver = function(){ if ( !mailHops._branch ){ return ; } mailHops._branch.removeObserver ( "" , mailHops ) ; }; mailHops.observe = function ( aSubject , aTopic , aData ) { if ( aTopic == "nsPref:changed" ) mailHops.loadPref(); }; mailHops.getCharPref = function ( strName , strDefault ){ var value; try { value = pref.getCharPref ( strName ) ; } catch ( exception ) { value = strDefault ; } return ( value ) ; }; mailHops.setClientLocation = function(){ var xmlhttp = new XMLHttpRequest(); if (!pref){ var pref = Components.classes["@mozilla.org/preferences-service;1"].getService ( Components.interfaces.nsIPrefBranch ) ; } xmlhttp.open("GET", mailHops.options.api_url+'/v1/lookup/?tb&app='+mailHops.appVersion+'&r=&c=1',true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { 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)) ; } else { pref.setCharPref("mail.mailHops.client_location", '') ; } } catch (e){ pref.setCharPref("mail.mailHops.client_location", '') ; } } }; xmlhttp.send(null); }; //mailhops lookup mailHops.lookupRoute = function(header_route){ //setup loading mailHops.clearRoute(); //import nativeJSON var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); //check for cache var cached_results=mailHops.getResults(); if(cached_results){ cached_results = JSON.parse(cached_results); mailHops.displayResult(header_route,cached_results.response); return; } //call mailhops api for lookup var xmlhttp = new XMLHttpRequest(); var lookupURL=mailHops.options.api_url+'/v1/lookup/?pb&app='+mailHops.appVersion+'&r='+String(header_route); if(mailHops.options.show_weather) lookupURL+='&w=1'; if(mailHops.options.client_location != '') lookupURL+='&c=0'; xmlhttp.open("GET", lookupURL ,true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { try{ var data = JSON.parse(xmlhttp.responseText); if(data && data.meta.code==200){ //save the result mailHops.saveResults(JSON.stringify(data)); //display the result mailHops.displayResult(header_route,data.response,data.meta, lookupURL); } else { //display the error mailHops.displayError(data); } } catch (ex){ mailHops.displayError(); } } }; xmlhttp.send(null); }; mailHops.addCommas = function(nStr){ nStr += ''; var x = nStr.split('.'); var x1 = x[0]; var x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; }; mailHops.launchWhoIs = function(ip){ var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance().QueryInterface(Components.interfaces.nsIMessenger); messenger.launchExternalURL('http://www.mailhops.com/whois/'+ip); }; mailHops.launchSpamHausURL = function(ip){ var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance().QueryInterface(Components.interfaces.nsIMessenger); messenger.launchExternalURL('http://www.spamhaus.org/query/bl?ip='+ip); }; mailHops.launchMap = function(route){ if(route != '') { var lookupURL=mailHops.options.api_url+'/v1/map/?pb&app='+mailHops.appVersion+'&m='+mailHops.options.map+'&u='+mailHops.options.unit+'&r='+String(route); if(mailHops.options.show_weather) lookupURL+='&w=1'; window.openDialog("chrome://mailhops/content/mailhopsMap.xul","MailHops",'toolbar=no,location=no,directories=no,menubar=yes,scrollbars=yes,close=yes,width=742,height=385', {src: lookupURL}); } }; mailHops.saveResults = function(results){ if(!mailHops.msgURI) return false; var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance().QueryInterface(Components.interfaces.nsIMessenger); var msgHdr = messenger.messageServiceFromURI(mailHops.msgURI).messageURIToMsgHdr(mailHops.msgURI); if(!msgHdr) return false; msgHdr.setStringProperty( "MH-Route", results ); }; mailHops.getResults = function(){ if(!mailHops.msgURI) return false; var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance().QueryInterface(Components.interfaces.nsIMessenger); var msgHdr = messenger.messageServiceFromURI(mailHops.msgURI).messageURIToMsgHdr(mailHops.msgURI); if(!msgHdr) return false; return msgHdr.getStringProperty( "MH-Route" ); }; addEventListener ( "messagepane-loaded" , mailHops.setupEventListener , true ) ;