From 9fdcbd4b3d3cfcbacd524cef699277a8d0ef9a6b Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Mon, 9 Jun 2008 09:50:14 +0000 Subject: Mailreader kickoff --- parsemail.js | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 parsemail.js (limited to 'parsemail.js') diff --git a/parsemail.js b/parsemail.js new file mode 100644 index 0000000..361716e --- /dev/null +++ b/parsemail.js @@ -0,0 +1,256 @@ +window.onload=function() { + var url = '/'+window.location.search.substring(1); + + document.getElementById( "content" ).innerHTML = "

Fetching Mailbox... (this may take a while, please be patient...)

"; + + new Ajax.Request(url, { method: 'get', onSuccess: function(transport) { parsemail(transport.responseText); } }); +} + +var myStates = { + nothingRead : 0, + inSinglePartMailHeader : 1, + inMultiPartMailHeader : 2, + inSinglePartMailBody : 3, + inMultiPartMailBody : 4, + inMultiPartHeader : 5, + inMultiPartBody : 6 +}; + +function enlarge( id ) { + var img = document.getElementById( id ); + var w = img.getAttribute( "width" ); + + if( w == "200" || w == "200px" ) { + img.setAttribute( "width", null ); + } else { + img.setAttribute( "width", "200px" ); + } +} + +function parsemail(mail) { + var imgNr = 0, mailNr = 0, myState = myStates.nothingRead; + + var myContentType, myContentEncoding, myContentTransferEncoding; + var multiPartDivider = ""; + var myMultiContentEncoding, myMultiContentTransferEncoding; + var myMultiContentType, myMultiFilename; + + var myMail, myMailHeader, myMailBody; + var myBodyContent = ""; + var myContainer = document.createDocumentFragment( ); + + var lines = mail.split( "\n" ); + + for(var i = 0; i < lines.length; i++ ) { + line=lines[i]; + + if( "From " == line.substring(0,5) ) { + switch( myState ) { + case myStates.inSinglePartMailBody: + myMail.appendChild( myMailBody ); // Fall through + default: + myContainer.appendChild( myMail ); + break; + case myStates.nothingRead: + break; + } + + myState = myStates.inSinglePartMailHeader; + mailNr++; + myMail = document.createElement( "div" ); + myMail.setAttribute( "id", "eMail" + mailNr ); + myMail.setAttribute( "class", "eMail" ); + myMailHeader = document.createElement( "div" ); + myMailHeader.setAttribute( "id", "eMailHeader" + mailNr ); + myMailHeader.setAttribute( "class", "eMailHeader" ); + } + + if( myState == myStates.inSinglePartMailHeader || myState == myStates.inMultiPartMailHeader ) { + + if( line == "" ) { + myState += 2; // in*PartMailHeader -> in*PartMailBody + myMail.appendChild( myMailHeader ); + myMailBody = document.createElement( "div" ); + myMailBody.setAttribute( "id", "eMailBody" + mailNr ); + myMailBody.setAttribute( "class", "eMailBody" ); + myBodyContent = ""; + continue; + } + + // Header unfolding + while( i < lines.length-1 && lines[i+1].match( /^\s+/ ) ) { + line += lines[++i]; + } + + // Header lines may contain encoded words + line = decode_header( line ); + + // Display all header lines + var textElement = document.createElement( "div" ); + textElement.appendChild( document.createTextNode( line ) ); + + // Highlight important header lines + if( line.match( /^From:|^To:/ ) ) { + var refElement = document.createElement( "a" ); + refElement.setAttribute( "href", "mailto:" + line.replace( /^From:|^To: (.*)/, "$1" ) ); + refElement.appendChild( textElement ); + textElement = refElement; + textElement.setAttribute( "class", "eMailHeaderVisible" ); + } else if ( line.match( /^Subject:|^Date:/ ) ) { + textElement.setAttribute( "class", "eMailHeaderVisible" ); + } else { + textElement.setAttribute( "class", "eMailHeaderInvisible" ); + } + myMailHeader.appendChild( textElement ); + + // Detect Content-Type and Content-Transfer-Encoding + if( line.match( /Content-Type: / ) ) { + myContentType = line.replace( /^Content-Type: (.*?);?.*$/i, "$1" ); + myContentEncoding = line.replace( /^Content-Type: .*?;.*charset=\"?(.*?)\"?(?:;|$).*/i, "$1" ).toLowerCase(); + } else if( line.match( /Content-Transfer-Encoding: /i ) ) { + myContentTransferEncoding = line.replace( /Content-Transfer-Encoding: (.*)/i, "$1" ).toLowerCase(); + } + + // Check for MIME Mail + if( line.match( /Content-Type: multipart.*boundary=.*/i ) ) { + myState = myStates.inMultiPartMailHeader; + multiPartDivider = line.replace( /.*boundary=\"?(.*?)\"?$/gi, "--$1" ); + } + } + + if( myState >= myStates.inMultiPartMailBody ) { + if( line == multiPartDivider || line == multiPartDivider + "--" ) { + if( myState == myStates.inMultiPartBody ) { + if( myMultiContentType.match( /image*/i ) ) { + var myImage = document.createElement( "img" ); + var myImageId = "img"+imgNr; + var myImageHint = document.createElement( "p" ); + + var myDataURIPrefix = "data:" + myMultiContentType + ";"; + if( myMultiContentTransferEncoding == "base64" ) { + myDataURIPrefix += "base64"; + } + + imgNr++; + + myImageHint.appendChild( document.createTextNode( "Click to enlarge image " + myMultiFilename + ":" ) ); + myImageHint.setAttribute( "class", "data-uri-image-hint" ); + + myImage.setAttribute( "src", myDataURIPrefix + "," + myBodyContent ); + myImage.setAttribute( "alt", myMultiFilename ); + myImage.setAttribute( "class", "data-uri-image" ); + myImage.setAttribute( "width", "200px" ); + myImage.setAttribute( "id", myImageId ); + myImage.setAttribute( "onClick", 'enlarge( "'+ myImage.id + '" )' ); + + myMailBody.appendChild( myImageHint ); + myMailBody.appendChild( myImage ); + + } else if( myMultiContentType.match( /text.plain/i ) ) { + var myText = document.createElement( "pre" ); + myText.appendChild( document.createTextNode( myBodyContent ) ); + myText.setAttribute( "class", "eMailPlaintext" ); + myMailBody.appendChild( myText ); + } else if( myMultiContentType.match( /text.html/i ) ) { + var myHTML = document.createElement( "div" ); + myHTML.innerHTML = myBodyContent; + myMailBody.appendChild( myHTML ); + } else if( myMultiContentType.match( /text*/i ) ) { + var myText = document.createElement( "div" ); + myText.appendChild( document.createTextNode( myBodyContent ) ); + myMailBody.appendChild( myText ); + } else { + var myDownload = document.createElement( "div" ); + myDownload.setAttribute( "class", "data-uri-download" ); + myDownload.appendChild( document.createTextNode( "This email contains an attachement of the type " + myMultiContentType +". Right-Click to download it. ") ); + + var myDataURIPrefix = "data:" + myMultiContentType + ";"; + if( myMultiContentTransferEncoding == "base64" ) { + myDataURIPrefix += "base64"; + } + + var myRef = document.createElement( "a" ); + myRef.setAttribute( "href", myDataURIPrefix + "," + myBodyContent ); + myRef.setAttribute( "type", myMultiContentType ); + myRef.appendChild( document.createTextNode( myMultiFilename ) ); + + myDownload.appendChild( myRef ); + myMailBody.appendChild( myDownload ); + } + myMail.appendChild( myMailBody ); + } + + myState = myStates.inMultiPartHeader; + myMultiContentType = "text/plain;" + myMultiContentTransferEncoding = ""; + myMultiFilename = "document"; + + } else { + if( myState == myStates.inMultiPartHeader ) { + if( line == "" ) { + myState = myStates.inMultiPartBody; + myBodyContent = ""; + continue; + } + + // Header unfolding + while( i < lines.length-1 && lines[i+1].match( /^\s+/ ) ) { + line += lines[++i]; + } + + // Header lines may contain encoded words + line = decode_header( line ); + + if( line.match( /Content-Type: / ) ) { + myMultiContentType = line.replace( /Content-Type: (.*?);.*/, "$1" ).toLowerCase(); + myMultiContentEncoding = line.replace( /Content-Type: .*?;.*charset=\"?(.*?)\"?(?:;|$).*/i, "$1" ).toLowerCase(); + } else if( line.match( /Content-Transfer-Encoding: /i ) ) { + myMultiContentTransferEncoding = line.replace( /Content-Transfer-Encoding: (.*?)/, "$1" ).toLowerCase(); + } + + if( line.match( /.*name=/ ) ) { + myMultiFilename = line.replace( /.*name="(.*)"/, "$1" ); + } + } else { + if( myMultiContentTransferEncoding == "quoted-printable" ) { + line = decode_quotedprintable( line ); + } + + myBodyContent += decode_charsets( line, myMultiContentEncoding ) + "\n"; + } + } + } + + if( myState == myStates.inSinglePartMailBody ) { + var textElement = document.createElement( "div" ); + + if( myContentTransferEncoding == "quoted-printable" ) { + line = decode_quotedprintable( line ); + } + textElement.appendChild( document.createTextNode( decode_charsets( line, myContentEncoding ) ) ); + myMailBody.appendChild( textElement ); + } + } + + switch( myState ) { + case myStates.inMultiPartBody: + case myStates.inSinglePartMailBody: + myMail.appendChild( myMailBody ); + default: + myContainer.appendChild( myMail ); + break; + case myStates.nothingRead: + myContainer.appendChild( document.createTextNode( "Your mailbox is empty. This can either mean that no email has been received yet, or that your email has been fetched and deleted already. You may want to try to reload." ) ); + break; + } + + var div = document.createElement( "div" ); + div.setAttribute( "id", "content" ); + div.appendChild( myContainer ); + var vspace = document.createElement( "div" ); + vspace.setAttribute( "class", "vspace" ); + div.appendChild( vspace ); + + var old_content = document.getElementById( "content" ); + old_content.parentNode.replaceChild( div, old_content ); +} -- cgit v1.2.3