summaryrefslogtreecommitdiff
path: root/parsemail.js
blob: 1a5ceb2d837d5af40bbe74e600d399f9bbfc50c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
window.onload=function() {
  var url = window.location.search.substring(1);

  document.getElementById( "content" ).innerHTML = "<p><b>Fetching Mailbox... (this may take a while, please be patient...)</b><br/></p>";

  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:\s*(.*?);?.*$/i, "$1" );
        myContentEncoding = line.replace( /^Content-Type:\s*.*?;.*charset=\"?(.*?)\"?(?:;|$).*/i, "$1" ).toLowerCase();
      } else if( line.match( /Content-Transfer-Encoding:/i ) ) {
        myContentTransferEncoding = line.replace( /Content-Transfer-Encoding:\s*(.*)/i, "$1" ).toLowerCase();
      }

      // Check for MIME Mail
      if( line.match( /Content-Type:\s*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:\s*(.*?);.*/, "$1" ).toLowerCase();
            myMultiContentEncoding = line.replace( /Content-Type:\s*.*?;.*charset=\"?(.*?)\"?(?:;|$).*/i, "$1" ).toLowerCase();
          } else if( line.match( /Content-Transfer-Encoding:/i ) ) {
            myMultiContentTransferEncoding = line.replace( /Content-Transfer-Encoding:\s*(.*?)/, "$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 );
}