/* * @author: Andrew Van Tassel * @email: andrew@andrewvantassel.com * @website: http://mailhops.com* */ var mailHops = { resultTextDataPane: null, resultTextDataPane2: null, resultContainerDataPane: null, resultDetailsLink: null, resultContainerDetails: null, resultDetails: null, resultMapLink: null, mailhopsDataPaneSPF: null, mailhopsDataPaneDKIM: null, mailhopsDataPaneMailer: null, mailhopsDataPaneDNSBL: null, isLoaded: false, showDetails: false, showWeather: false, map: 'goog', unit: 'mi', appVersion: 'MailHops Postbox 0.6.1' } 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.resultMapLink = document.getElementById ( "mailhopsDataPaneMapLink"); mailHops.mailhopsDataPaneSPF = document.getElementById ( "mailhopsDataPaneSPF"); mailHops.mailhopsDataPaneDKIM = document.getElementById ( "mailhopsDataPaneDKIM"); mailHops.mailhopsDataPaneMailer = document.getElementById ( "mailhopsDataPaneMailer"); mailHops.mailhopsDataPaneDNSBL = document.getElementById ( "mailhopsDataPaneDNSBL"); //event listner for route click to launch map mailHops.resultMapLink.addEventListener("click", function () { var route = this.getAttribute("route"); if(route) mailHops.launchMap(String(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 () { var ip = this.getAttribute('ip'); mailHops.launchSpamHausURL(ip); } , false); }; mailHops.loadPref = function() { //get preferences mailHops.map = mailHops.getCharPref('mail.mailHops.map','goog'); mailHops.unit = mailHops.getCharPref('mail.mailHops.unit','mi'); mailHops.showDetails = mailHops.getCharPref('mail.mailHops.show_details','false')=='true'?true:false; mailHops.showWeather = mailHops.getCharPref('mail.mailHops.show_weather','false')=='true'?true:false; }; 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 ; } 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 ) ; var headXMailer = mailHops.headers.extractHeader ( "X-Mailer" , false ) ; var headUserAgent = mailHops.headers.extractHeader ( "User-Agent" , false ) ; var headReceivedSPF = mailHops.headers.extractHeader ( "Received-SPF" , false ) ; var headAuth = mailHops.headers.extractHeader ( "Authentication-Results" , false ) ; //display auth mailHops.displayResultAuth(headXMailer,headUserAgent,headAuth,headReceivedSPF); var received_ips; var all_ips = new Array(); var rline=''; //get the originating IP address if(headXOrigIP){ var ip = headXOrigIP.match(regexAllIp); if(ip != null && ip.length != 0) all_ips.push( ip[0] ); } //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 0){ if(mailHops.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.'; } //add event for route api map mailHops.resultMapLink.setAttribute("route", header_route); mailHops.resultTextDataPane.style.backgroundImage = 'url('+image+')'; mailHops.resultTextDataPane.value = displayText; mailHops.resultTextDataPane.setAttribute('tooltiptext',displayText+' '+distanceText); mailHops.resultTextDataPane2.value = distanceText; mailHops.resultTextDataPane2.setAttribute('tooltiptext',displayText+' '+distanceText); //show the detail link mailHops.resultDetailsLink.style.display = 'block'; mailHops.resultMapLink.style.display = 'block'; //show details by default if(mailHops.showDetails){ 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.isDay = function(){ var d = new Date(); if(d.getHours()>7 && d.getHours()<19) return true; else return false; } //display the connection error message mailHops.displayError = function(){ mailHops.resultMapLink.removeAttribute("route"); mailHops.resultTextDataPane.style.backgroundImage = 'url(chrome://mailhops/content/images/auth/error.png)'; mailHops.resultTextDataPane.value = ' MailHops Failed.'; mailHops.resultTextDataPane.setAttribute('tooltiptext',' Could not connect to MailHops.'); mailHops.resultTextDataPane2.value = ''; mailHops.resultTextDataPane2.style.backgroundImage = ''; mailHops.resultTextDataPane2.setAttribute('tooltiptext',''); }; mailHops.clearRoute = function(){ 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" ){ return ; } //load preferences mailHops.loadPref(); }; mailHops.getCharPref = function ( strName , strDefault ){ var value; try { value = pref.getCharPref ( strName ) ; } catch ( exception ) { value = strDefault ; } return ( value ) ; }; //mailhops lookup mailHops.lookup = function(header_route){ //setup loading mailHops.clearRoute(); //import nativeJSON var nativeJSON = Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); //call mailhops api for lookup var xmlhttp = new XMLHttpRequest(); var lookupURL='http://api.mailhops.com/v1/lookup/?pb&app='+mailHops.appVersion+'&r='+String(header_route); if(mailHops.showWeather) lookupURL+='&w=1'; xmlhttp.open("GET", lookupURL ,true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { try{ var data = nativeJSON.decode(xmlhttp.responseText); if(data && data.meta.code==200){ //display the result mailHops.displayResult(header_route,data.response); } else { //display the error mailHops.displayError(); } } 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.launchSpamHausURL = function(ip){ var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance(); messenger = messenger.QueryInterface(Components.interfaces.nsIMessenger); messenger.launchExternalURL('http://www.spamhaus.org/query/bl?ip='+ip); }; mailHops.launchMap = function(route){ //launch mailhops api map var lookupURL='http://api.mailhops.com/v1/map/?pb&app='+mailHops.appVersion+'&m='+mailHops.map+'&u='+mailHops.unit+'&r='+String(route); if(mailHops.showWeather) lookupURL+='&w=1'; var openwin = window.openDialog(lookupURL,"MailHops",'toolbar=no,location=no,directories=no,menubar=yes,scrollbars=yes,close=yes,width=732,height=332'); openwin.focus(); }; addEventListener ( "messagepane-loaded" , mailHops.setupEventListener , true ) ;