summaryrefslogtreecommitdiff
path: root/parsemail.js
diff options
context:
space:
mode:
authorerdgeist <>2008-06-09 09:50:14 +0000
committererdgeist <>2008-06-09 09:50:14 +0000
commit9fdcbd4b3d3cfcbacd524cef699277a8d0ef9a6b (patch)
treeb4143fa54c5ca33ddbec4e800091be022f0370b2 /parsemail.js
Mailreader kickoff
Diffstat (limited to 'parsemail.js')
-rw-r--r--parsemail.js256
1 files changed, 256 insertions, 0 deletions
diff --git a/parsemail.js b/parsemail.js
new file mode 100644
index 0000000..361716e
--- /dev/null
+++ b/parsemail.js
@@ -0,0 +1,256 @@
1window.onload=function() {
2 var url = '/'+window.location.search.substring(1);
3
4 document.getElementById( "content" ).innerHTML = "<p><b>Fetching Mailbox... (this may take a while, please be patient...)</b><br/></p>";
5
6 new Ajax.Request(url, { method: 'get', onSuccess: function(transport) { parsemail(transport.responseText); } });
7}
8
9var myStates = {
10 nothingRead : 0,
11 inSinglePartMailHeader : 1,
12 inMultiPartMailHeader : 2,
13 inSinglePartMailBody : 3,
14 inMultiPartMailBody : 4,
15 inMultiPartHeader : 5,
16 inMultiPartBody : 6
17};
18
19function enlarge( id ) {
20 var img = document.getElementById( id );
21 var w = img.getAttribute( "width" );
22
23 if( w == "200" || w == "200px" ) {
24 img.setAttribute( "width", null );
25 } else {
26 img.setAttribute( "width", "200px" );
27 }
28}
29
30function parsemail(mail) {
31 var imgNr = 0, mailNr = 0, myState = myStates.nothingRead;
32
33 var myContentType, myContentEncoding, myContentTransferEncoding;
34 var multiPartDivider = "";
35 var myMultiContentEncoding, myMultiContentTransferEncoding;
36 var myMultiContentType, myMultiFilename;
37
38 var myMail, myMailHeader, myMailBody;
39 var myBodyContent = "";
40 var myContainer = document.createDocumentFragment( );
41
42 var lines = mail.split( "\n" );
43
44 for(var i = 0; i < lines.length; i++ ) {
45 line=lines[i];
46
47 if( "From " == line.substring(0,5) ) {
48 switch( myState ) {
49 case myStates.inSinglePartMailBody:
50 myMail.appendChild( myMailBody ); // Fall through
51 default:
52 myContainer.appendChild( myMail );
53 break;
54 case myStates.nothingRead:
55 break;
56 }
57
58 myState = myStates.inSinglePartMailHeader;
59 mailNr++;
60 myMail = document.createElement( "div" );
61 myMail.setAttribute( "id", "eMail" + mailNr );
62 myMail.setAttribute( "class", "eMail" );
63 myMailHeader = document.createElement( "div" );
64 myMailHeader.setAttribute( "id", "eMailHeader" + mailNr );
65 myMailHeader.setAttribute( "class", "eMailHeader" );
66 }
67
68 if( myState == myStates.inSinglePartMailHeader || myState == myStates.inMultiPartMailHeader ) {
69
70 if( line == "" ) {
71 myState += 2; // in*PartMailHeader -> in*PartMailBody
72 myMail.appendChild( myMailHeader );
73 myMailBody = document.createElement( "div" );
74 myMailBody.setAttribute( "id", "eMailBody" + mailNr );
75 myMailBody.setAttribute( "class", "eMailBody" );
76 myBodyContent = "";
77 continue;
78 }
79
80 // Header unfolding
81 while( i < lines.length-1 && lines[i+1].match( /^\s+/ ) ) {
82 line += lines[++i];
83 }
84
85 // Header lines may contain encoded words
86 line = decode_header( line );
87
88 // Display all header lines
89 var textElement = document.createElement( "div" );
90 textElement.appendChild( document.createTextNode( line ) );
91
92 // Highlight important header lines
93 if( line.match( /^From:|^To:/ ) ) {
94 var refElement = document.createElement( "a" );
95 refElement.setAttribute( "href", "mailto:" + line.replace( /^From:|^To: (.*)/, "$1" ) );
96 refElement.appendChild( textElement );
97 textElement = refElement;
98 textElement.setAttribute( "class", "eMailHeaderVisible" );
99 } else if ( line.match( /^Subject:|^Date:/ ) ) {
100 textElement.setAttribute( "class", "eMailHeaderVisible" );
101 } else {
102 textElement.setAttribute( "class", "eMailHeaderInvisible" );
103 }
104 myMailHeader.appendChild( textElement );
105
106 // Detect Content-Type and Content-Transfer-Encoding
107 if( line.match( /Content-Type: / ) ) {
108 myContentType = line.replace( /^Content-Type: (.*?);?.*$/i, "$1" );
109 myContentEncoding = line.replace( /^Content-Type: .*?;.*charset=\"?(.*?)\"?(?:;|$).*/i, "$1" ).toLowerCase();
110 } else if( line.match( /Content-Transfer-Encoding: /i ) ) {
111 myContentTransferEncoding = line.replace( /Content-Transfer-Encoding: (.*)/i, "$1" ).toLowerCase();
112 }
113
114 // Check for MIME Mail
115 if( line.match( /Content-Type: multipart.*boundary=.*/i ) ) {
116 myState = myStates.inMultiPartMailHeader;
117 multiPartDivider = line.replace( /.*boundary=\"?(.*?)\"?$/gi, "--$1" );
118 }
119 }
120
121 if( myState >= myStates.inMultiPartMailBody ) {
122 if( line == multiPartDivider || line == multiPartDivider + "--" ) {
123 if( myState == myStates.inMultiPartBody ) {
124 if( myMultiContentType.match( /image*/i ) ) {
125 var myImage = document.createElement( "img" );
126 var myImageId = "img"+imgNr;
127 var myImageHint = document.createElement( "p" );
128
129 var myDataURIPrefix = "data:" + myMultiContentType + ";";
130 if( myMultiContentTransferEncoding == "base64" ) {
131 myDataURIPrefix += "base64";
132 }
133
134 imgNr++;
135
136 myImageHint.appendChild( document.createTextNode( "Click to enlarge image " + myMultiFilename + ":" ) );
137 myImageHint.setAttribute( "class", "data-uri-image-hint" );
138
139 myImage.setAttribute( "src", myDataURIPrefix + "," + myBodyContent );
140 myImage.setAttribute( "alt", myMultiFilename );
141 myImage.setAttribute( "class", "data-uri-image" );
142 myImage.setAttribute( "width", "200px" );
143 myImage.setAttribute( "id", myImageId );
144 myImage.setAttribute( "onClick", 'enlarge( "'+ myImage.id + '" )' );
145
146 myMailBody.appendChild( myImageHint );
147 myMailBody.appendChild( myImage );
148
149 } else if( myMultiContentType.match( /text.plain/i ) ) {
150 var myText = document.createElement( "pre" );
151 myText.appendChild( document.createTextNode( myBodyContent ) );
152 myText.setAttribute( "class", "eMailPlaintext" );
153 myMailBody.appendChild( myText );
154 } else if( myMultiContentType.match( /text.html/i ) ) {
155 var myHTML = document.createElement( "div" );
156 myHTML.innerHTML = myBodyContent;
157 myMailBody.appendChild( myHTML );
158 } else if( myMultiContentType.match( /text*/i ) ) {
159 var myText = document.createElement( "div" );
160 myText.appendChild( document.createTextNode( myBodyContent ) );
161 myMailBody.appendChild( myText );
162 } else {
163 var myDownload = document.createElement( "div" );
164 myDownload.setAttribute( "class", "data-uri-download" );
165 myDownload.appendChild( document.createTextNode( "This email contains an attachement of the type " + myMultiContentType +". Right-Click to download it. ") );
166
167 var myDataURIPrefix = "data:" + myMultiContentType + ";";
168 if( myMultiContentTransferEncoding == "base64" ) {
169 myDataURIPrefix += "base64";
170 }
171
172 var myRef = document.createElement( "a" );
173 myRef.setAttribute( "href", myDataURIPrefix + "," + myBodyContent );
174 myRef.setAttribute( "type", myMultiContentType );
175 myRef.appendChild( document.createTextNode( myMultiFilename ) );
176
177 myDownload.appendChild( myRef );
178 myMailBody.appendChild( myDownload );
179 }
180 myMail.appendChild( myMailBody );
181 }
182
183 myState = myStates.inMultiPartHeader;
184 myMultiContentType = "text/plain;"
185 myMultiContentTransferEncoding = "";
186 myMultiFilename = "document";
187
188 } else {
189 if( myState == myStates.inMultiPartHeader ) {
190 if( line == "" ) {
191 myState = myStates.inMultiPartBody;
192 myBodyContent = "";
193 continue;
194 }
195
196 // Header unfolding
197 while( i < lines.length-1 && lines[i+1].match( /^\s+/ ) ) {
198 line += lines[++i];
199 }
200
201 // Header lines may contain encoded words
202 line = decode_header( line );
203
204 if( line.match( /Content-Type: / ) ) {
205 myMultiContentType = line.replace( /Content-Type: (.*?);.*/, "$1" ).toLowerCase();
206 myMultiContentEncoding = line.replace( /Content-Type: .*?;.*charset=\"?(.*?)\"?(?:;|$).*/i, "$1" ).toLowerCase();
207 } else if( line.match( /Content-Transfer-Encoding: /i ) ) {
208 myMultiContentTransferEncoding = line.replace( /Content-Transfer-Encoding: (.*?)/, "$1" ).toLowerCase();
209 }
210
211 if( line.match( /.*name=/ ) ) {
212 myMultiFilename = line.replace( /.*name="(.*)"/, "$1" );
213 }
214 } else {
215 if( myMultiContentTransferEncoding == "quoted-printable" ) {
216 line = decode_quotedprintable( line );
217 }
218
219 myBodyContent += decode_charsets( line, myMultiContentEncoding ) + "\n";
220 }
221 }
222 }
223
224 if( myState == myStates.inSinglePartMailBody ) {
225 var textElement = document.createElement( "div" );
226
227 if( myContentTransferEncoding == "quoted-printable" ) {
228 line = decode_quotedprintable( line );
229 }
230 textElement.appendChild( document.createTextNode( decode_charsets( line, myContentEncoding ) ) );
231 myMailBody.appendChild( textElement );
232 }
233 }
234
235 switch( myState ) {
236 case myStates.inMultiPartBody:
237 case myStates.inSinglePartMailBody:
238 myMail.appendChild( myMailBody );
239 default:
240 myContainer.appendChild( myMail );
241 break;
242 case myStates.nothingRead:
243 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." ) );
244 break;
245 }
246
247 var div = document.createElement( "div" );
248 div.setAttribute( "id", "content" );
249 div.appendChild( myContainer );
250 var vspace = document.createElement( "div" );
251 vspace.setAttribute( "class", "vspace" );
252 div.appendChild( vspace );
253
254 var old_content = document.getElementById( "content" );
255 old_content.parentNode.replaceChild( div, old_content );
256}