1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # EMAILPlug.pm - a plugin for parsing email files
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | # EMAILPlug
|
---|
30 | #
|
---|
31 | # by Gordon Paynter ([email protected])
|
---|
32 | #
|
---|
33 | # Email plug reads email files. These are named with a simple
|
---|
34 | # number (i.e. as they appear in maildir folders) or with the
|
---|
35 | # extension .mbx (for mbox mail file format)
|
---|
36 | #
|
---|
37 | # Document text:
|
---|
38 | # The document text consists of all the text
|
---|
39 | # after the first blank line in the document.
|
---|
40 | #
|
---|
41 | # Metadata:
|
---|
42 | # $Headers All the header content
|
---|
43 | # $Subject Subject: header
|
---|
44 | # $To To: header
|
---|
45 | # $From From: header - this will be stored as Creator
|
---|
46 | # $FromName Name of sender (where available)
|
---|
47 | # $FromAddr E-mail address of sender
|
---|
48 | # $DateText Date: header
|
---|
49 | # $Date Date: header in GSDL format (eg: 19990924)
|
---|
50 | #
|
---|
51 | #
|
---|
52 | # John McPherson - June/July 2001
|
---|
53 | # added (basic) MIME support and quoted-printable and base64 decodings.
|
---|
54 | # Minor fixes for names that are actually email addresses (ie <...> was lost)
|
---|
55 | #
|
---|
56 | # See: * RFC 822 - ARPA Internet Text Messages
|
---|
57 | # * RFC 2045 - Multipurpose Internet Mail Extensions (MIME) -part1
|
---|
58 | # * RFC 2046 - MIME (part 2) Media Types (and multipart messages)
|
---|
59 | # * RFC 1806 - Content Dispositions (ie inline/attachment)
|
---|
60 | package EMAILPlug;
|
---|
61 |
|
---|
62 | use SplitPlug;
|
---|
63 |
|
---|
64 | use sorttools;
|
---|
65 | use util;
|
---|
66 |
|
---|
67 |
|
---|
68 | # EMAILPlug is a sub-class of SplitPlug.
|
---|
69 |
|
---|
70 | sub BEGIN {
|
---|
71 | @ISA = ('SplitPlug');
|
---|
72 | }
|
---|
73 |
|
---|
74 | # Create a new EMAILPlug object with which to parse a file.
|
---|
75 | # Accomplished by creating a new BasPlug and using bless to
|
---|
76 | # turn it into an EMAILPlug.
|
---|
77 |
|
---|
78 | sub new {
|
---|
79 | my ($class) = @_;
|
---|
80 | my $self = new BasPlug ("EMAILPlug", @_);
|
---|
81 | # make sure we don't run textcat (defaults to "auto");
|
---|
82 | $self->{'input_encoding'}="ascii"; # this might not be good enough...
|
---|
83 | return bless $self, $class;
|
---|
84 | }
|
---|
85 |
|
---|
86 | sub get_default_process_exp {
|
---|
87 | my $self = shift (@_);
|
---|
88 | # mbx/email for mailbox file format, \d+ for maildir (each message is
|
---|
89 | # in a separate file, with a unique number for filename)
|
---|
90 | return q@([\\/]\d+|\.(mbx|email))$@;
|
---|
91 | }
|
---|
92 |
|
---|
93 | # This plugin splits the mbox mail files at lines starting with From<sp>
|
---|
94 | sub get_default_split_exp {
|
---|
95 | return q^\nFrom .*\n^;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | # do plugin specific processing of doc_obj
|
---|
100 | sub process {
|
---|
101 |
|
---|
102 | my $self = shift (@_);
|
---|
103 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
|
---|
104 | my $outhandle = $self->{'outhandle'};
|
---|
105 |
|
---|
106 | # Check that we're dealing with a valid mail file
|
---|
107 | return undef unless (($$textref =~ /From:/m) || ($$textref =~ /To:/m));
|
---|
108 |
|
---|
109 | # slightly more strict validity check, to prevent us from matching
|
---|
110 | # .so.x files ...
|
---|
111 | return undef unless (($$textref =~ /^From /) ||
|
---|
112 | ($$textref =~ /^[-A-Za-z]{2,100}:/m));
|
---|
113 |
|
---|
114 | print $outhandle "EMAILPlug: processing $file\n"
|
---|
115 | if $self->{'verbosity'} > 1;
|
---|
116 |
|
---|
117 | my $cursection = $doc_obj->get_top_section();
|
---|
118 |
|
---|
119 | #
|
---|
120 | # Parse the document's text and extract metadata
|
---|
121 | #
|
---|
122 |
|
---|
123 | # Protect backslashes
|
---|
124 | $$textref =~ s@\\@\\\\@g;
|
---|
125 |
|
---|
126 | # Separate header from body of message
|
---|
127 | my $Headers = $$textref;
|
---|
128 | $Headers =~ s/\r?\n\r?\n(.*)$//s;
|
---|
129 | $$textref = $1;
|
---|
130 |
|
---|
131 | # Unfold headers - see rfc822
|
---|
132 | $Headers =~ s/\r?\n[\t\ ]+/ /gs;
|
---|
133 | # Extract basic metadata from header
|
---|
134 | my @headers = ("From", "To", "Subject", "Date");
|
---|
135 | my %raw;
|
---|
136 | foreach my $name (@headers) {
|
---|
137 | $raw{$name} = "No $name value";
|
---|
138 | }
|
---|
139 |
|
---|
140 | # Examine each line of the headers
|
---|
141 | my ($line, $name, $value);
|
---|
142 | my @parts;
|
---|
143 | foreach $line (split(/\n/, $Headers)) {
|
---|
144 |
|
---|
145 | # Ignore lines with no content or which begin with whitespace
|
---|
146 | next unless ($line =~ /:/);
|
---|
147 | next if ($line =~ /^\s/);
|
---|
148 |
|
---|
149 | # Find out what metadata is on this line
|
---|
150 | @parts = split(/:/, $line);
|
---|
151 | $name = shift @parts;
|
---|
152 | # uppercase the first character according to the current locale
|
---|
153 | $name=~s/(.+)/\u$1/;
|
---|
154 | next unless $name;
|
---|
155 | next unless ($raw{$name});
|
---|
156 |
|
---|
157 | # Find the value of that metadata
|
---|
158 | $value = join(":", @parts);
|
---|
159 | $value =~ s/^\s+//;
|
---|
160 | $value =~ s/\s+$//;
|
---|
161 |
|
---|
162 | # Store the metadata
|
---|
163 | $raw{$name} = $value;
|
---|
164 | }
|
---|
165 |
|
---|
166 | # Extract the name and e-mail address from the From metadata
|
---|
167 | $frommeta = $raw{"From"};
|
---|
168 | $frommeta =~ m/(.*)<(.*)>/;
|
---|
169 | my $fromnamemeta=$1;
|
---|
170 | my $fromaddrmeta=$2;
|
---|
171 | if (!defined($fromaddrmeta)) {
|
---|
172 | $fromaddrmeta=$frommeta;
|
---|
173 | }
|
---|
174 | $doc_obj->add_utf8_metadata ($cursection, "FromAddr", $fromaddrmeta);
|
---|
175 |
|
---|
176 | if (defined($fromnamemeta)) {
|
---|
177 | $fromnamemeta =~ s/\"//g;
|
---|
178 | $fromnamemeta =~ s/(.*) /$1/; # Remove trailing space
|
---|
179 | }
|
---|
180 | else {
|
---|
181 | $fromnamemeta = $fromaddrmeta;
|
---|
182 | }
|
---|
183 | # if name is an address
|
---|
184 | $fromnamemeta =~ s/<//g; $fromnamemeta =~ s/>//g;
|
---|
185 | $doc_obj->add_utf8_metadata ($cursection, "FromName", $fromnamemeta);
|
---|
186 |
|
---|
187 | # Escape < and > in the whole From field;
|
---|
188 | $frommeta =~ s/</</g; $frommeta =~ s/>/>/g;
|
---|
189 | $raw{"From"}=$frommeta;
|
---|
190 |
|
---|
191 | # Process Date information
|
---|
192 | if ($raw{"Date"} !~ /No Date/) {
|
---|
193 | $raw{"DateText"} = $raw{"Date"};
|
---|
194 |
|
---|
195 | # Convert the date text to internal date format
|
---|
196 | $value = $raw{"Date"};
|
---|
197 | my ($day, $month, $year) = $value =~ /(\d?\d)\s([A-Z][a-z][a-z])\s(\d\d\d?\d?)/;
|
---|
198 | if ($year < 100) { $year += 1900; }
|
---|
199 | $raw{"Date"} = &sorttools::format_date($day, $month, $year);
|
---|
200 |
|
---|
201 | } else {
|
---|
202 | # We have not extracted a date
|
---|
203 | $raw{"DateText"} = "Unknown.";
|
---|
204 | $raw{"Date"} = "19000000";
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | # Add extracted metadata to document object
|
---|
209 | foreach my $name (keys %raw) {
|
---|
210 | $value = $raw{$name};
|
---|
211 | if ($value) {
|
---|
212 | $value = &text_into_html($value);
|
---|
213 | } else {
|
---|
214 | $value = "No $name field";
|
---|
215 | }
|
---|
216 | $doc_obj->add_utf8_metadata ($cursection, $name, $value);
|
---|
217 | }
|
---|
218 |
|
---|
219 | my $mimetype="text/plain";
|
---|
220 | my $mimeinfo="";
|
---|
221 | # Do MIME and encoding stuff
|
---|
222 | if ($Headers =~ /^content\-type:\s*([\w\/\-]+)\s*\;?\s*([^\s]+)\s*$/mi)
|
---|
223 | {
|
---|
224 | $mimetype=$1;
|
---|
225 | $mimetype =~ tr/[A-Z]/[a-z]/;
|
---|
226 | $mimeinfo=$2;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if ($mimetype ne "text/plain") {
|
---|
230 | $$textref=text_from_mime_message($mimetype,$mimeinfo,$$textref);
|
---|
231 | } # end of not text/plain
|
---|
232 |
|
---|
233 |
|
---|
234 | # Add "All headers" metadata
|
---|
235 | $Headers = &text_into_html($Headers);
|
---|
236 | $Headers = "No headers" unless ($Headers =~ /\w/);
|
---|
237 | $doc_obj->add_utf8_metadata ($cursection, "Headers", $Headers);
|
---|
238 |
|
---|
239 | # Add text to document object
|
---|
240 | if ($mimetype eq "text/plain") {
|
---|
241 | $$textref = &text_into_html($$textref);
|
---|
242 | }
|
---|
243 | $$textref = "No message" unless ($$textref =~ /\w/);
|
---|
244 | $doc_obj->add_utf8_text($cursection, $$textref);
|
---|
245 |
|
---|
246 | return 1;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | # Convert a text string into HTML.
|
---|
251 | #
|
---|
252 | # The HTML is going to be inserted into a GML file, so
|
---|
253 | # we have to be careful not to use symbols like ">",
|
---|
254 | # which ocurs frequently in email messages (and use
|
---|
255 | # > instead.
|
---|
256 | #
|
---|
257 | # This function also turns links and email addresses into hyperlinks,
|
---|
258 | # and replaces carriage returns with <BR> tags (and multiple carriage
|
---|
259 | # returns with <P> tags).
|
---|
260 |
|
---|
261 |
|
---|
262 | sub text_into_html {
|
---|
263 | my ($text) = @_;
|
---|
264 |
|
---|
265 | # Convert problem characters into HTML symbols
|
---|
266 | $text =~ s/&/&/go;
|
---|
267 | $text =~ s/</</go;
|
---|
268 | $text =~ s/>/>/go;
|
---|
269 | $text =~ s/\"/"/go;
|
---|
270 |
|
---|
271 | # convert email addresses and URIs into links
|
---|
272 | # don't markup email addresses for now
|
---|
273 | # $text =~ s/([\w\d\.\-]+@[\w\d\.\-]+)/<a href=\"mailto:$1\">$1<\/a>/g;
|
---|
274 |
|
---|
275 | # assume hostnames are \.\w\d\- only, then might have a trailing '/.*'
|
---|
276 | # URI can't finish with a '.'
|
---|
277 | $text =~ s/((http|ftp|https):\/\/[\w\d\-]+(\.[\w\d\-]+)*\/?((&|\.)?[\w\d\?\=\-_\/~]+)*)/<a href=\"$1\">$1<\/a>/g;
|
---|
278 |
|
---|
279 |
|
---|
280 | # Clean up whitespace and convert \n charaters to <BR> or <P>
|
---|
281 | $text =~ s/ +/ /go;
|
---|
282 | $text =~ s/\s*$//o;
|
---|
283 | $text =~ s/^\s*//o;
|
---|
284 | $text =~ s/\n/\n<BR>/go;
|
---|
285 | $text =~ s/<BR>\s*<BR>/<P>/go;
|
---|
286 |
|
---|
287 | return $text;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 | #Process a MIME message.
|
---|
294 | # the textref we are given DOES NOT include the header.
|
---|
295 | sub text_from_mime_message {
|
---|
296 | my ($mimetype,$mimeinfo,$text)=(@_);
|
---|
297 |
|
---|
298 | # Check for multiparts - $mimeinfo will be a boundary
|
---|
299 | if ($mimetype =~ /multipart/) {
|
---|
300 | $boundary="";
|
---|
301 | if ($mimeinfo =~ /boundary="?([^\s]+?)"?\s*$/im) {
|
---|
302 | $boundary=$1;
|
---|
303 | }
|
---|
304 | # parts start with "--$boundary"
|
---|
305 | # message ends with "--$boundary--"
|
---|
306 | # RFC says boundary is <70 chars, [A-Za-z'()+_,-./:=?], so escape any
|
---|
307 | # that perl might want to interpolate.
|
---|
308 |
|
---|
309 | $boundary=~s/\\/\\\\/g;
|
---|
310 | $boundary=~s/([\?\+\.\(\)\:\/\'])/\\$1/g;
|
---|
311 | my @message_parts = split("\r?\n\-\-$boundary", $text);
|
---|
312 | # remove first "part" and last "part" (final --)
|
---|
313 | shift @message_parts;
|
---|
314 | my $last=pop @message_parts;
|
---|
315 | # make sure it is only -- and whitespace
|
---|
316 | if ($last !~ /^\-\-\s*$/ms) {
|
---|
317 | print $outhandle "EMAILPlug: (warning) last part of MIME message isn't empty\n";
|
---|
318 | }
|
---|
319 | foreach my $message_part (@message_parts) {
|
---|
320 | # remove the leading newline left from split.
|
---|
321 | $message_part=~s/^\r?\n//;
|
---|
322 | }
|
---|
323 | if ($mimetype eq "multipart/alternative") {
|
---|
324 | # check for an HTML version first, then TEXT, otherwise use first.
|
---|
325 | my $part_text="";
|
---|
326 | foreach my $message_part (@message_parts) {
|
---|
327 | if ($message_part =~ m@\s*content\-type:\s*text/html@mis)
|
---|
328 | {
|
---|
329 | # Use the HTML version
|
---|
330 | $part_text=text_from_part($message_part);
|
---|
331 | $mimetype="text/html";
|
---|
332 | last;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | if ($part_text eq "") { # try getting a text part instead
|
---|
336 | foreach my $message_part (@message_parts) {
|
---|
337 | if ($message_part =~ m@^content\-type:\s*text/plain@mis)
|
---|
338 | {
|
---|
339 | # Use the plain version
|
---|
340 | $part_text=text_from_part($message_part);
|
---|
341 | $mimetype="text/plain";
|
---|
342 | last;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 | if ($part_text eq "") { # use first part
|
---|
347 | $part_text=text_from_part(shift @message_parts);
|
---|
348 | }
|
---|
349 | if ($part_text eq "") { # we couldn't get anything!!!
|
---|
350 | # or it was an empty message...
|
---|
351 | # do nothing...
|
---|
352 | print $outhandle "EMAILPlug: no text - empty body?\n";
|
---|
353 | } else {
|
---|
354 | $text=$part_text;
|
---|
355 | }
|
---|
356 | } elsif ($mimetype =~ m@multipart/(mixed|digest|related)@) {
|
---|
357 | $text="";
|
---|
358 | foreach my $message_part (@message_parts) {
|
---|
359 | my $part_header=$message_part;
|
---|
360 | $part_header=~s/\r?\n\r?\n(.*)$//sg;
|
---|
361 | my $part_body=$1;
|
---|
362 | $part_header =~ s/\r?\n[\t\ ]+/ /gs; #unfold
|
---|
363 | my $part_content_type="";
|
---|
364 | my $part_content_info="";
|
---|
365 | if ($mimetype eq "multipart/digest") {
|
---|
366 | # default type - RTFRFC!!
|
---|
367 | $part_content_type="message/rfc822";
|
---|
368 | }
|
---|
369 | if ($part_header =~ m@^content\-type:\s*([\w+/\-]+)\s*\;?\s*([^\s]+)@mi) {
|
---|
370 | $part_content_type=$1; $part_content_type =~ tr/A-Z/a-z/;
|
---|
371 | $part_content_info=$2;
|
---|
372 | }
|
---|
373 | my $filename="";
|
---|
374 | if ($part_header =~ m@name=\"?([\w\.\-\\/]+)\"?@mis) {
|
---|
375 | $filename=$1;
|
---|
376 | }
|
---|
377 |
|
---|
378 | # disposition - either inline or attachment.
|
---|
379 | # NOT CURRENTLY USED - we display all text types instead...
|
---|
380 | # $part_header =~ /^content\-disposition:\s*([\w+])/mis;
|
---|
381 |
|
---|
382 | # add <<attachment>> to each part except the first...
|
---|
383 | if ($text ne "") {
|
---|
384 | $text.="<p><hr><strong><<attachment>></>";
|
---|
385 | # add part info header
|
---|
386 | $text.="<br>Type: $part_content_type<br>\n";
|
---|
387 | if ($filename ne "") {
|
---|
388 | $text.="Filename: $filename\n";
|
---|
389 | }
|
---|
390 | $text.="</strong></p>\n";
|
---|
391 | }
|
---|
392 |
|
---|
393 | if ($part_content_type =~ m@text/@)
|
---|
394 | {
|
---|
395 | my $part_text=text_from_part($message_part);
|
---|
396 | if ($part_content_type !~ m@text/(ht|x)ml@) {
|
---|
397 | $part_text=text_into_html($part_text);
|
---|
398 | }
|
---|
399 | if ($part_text eq "") {
|
---|
400 | $part_text='<<empty message>>';
|
---|
401 | }
|
---|
402 | $text.=$part_text;
|
---|
403 | } elsif ($part_content_type =~ m@message/rfc822@) {
|
---|
404 | # This is a forwarded message
|
---|
405 | my $message_part_headers=$part_body;
|
---|
406 | $message_part_headers =~ s/\r?\n[\t\ ]+/ /gs; #unfold
|
---|
407 | $message_part_headers=~s/\r?\n\r?\n(.*)$//s;
|
---|
408 | my $message_part_body=$1;
|
---|
409 |
|
---|
410 | my $rfc822_formatted_body=""; # put result in here
|
---|
411 | if ($message_part_headers =~
|
---|
412 | /^content\-type:\s*([\w\/\-]+)\s*\;?\s*?([^\s]+)?\s*$/ims)
|
---|
413 | {
|
---|
414 | # The message header uses MIME flags
|
---|
415 | my $message_content_type=$1;
|
---|
416 | my $message_content_info=$2;
|
---|
417 | if (!defined($message_content_info)) {
|
---|
418 | $message_content_info="";
|
---|
419 | }
|
---|
420 | $message_content_type =~ tr/A-Z/a-z/;
|
---|
421 | if ($message_content_type =~ /multipart/) {
|
---|
422 | $rfc822_formatted_body=
|
---|
423 | text_from_mime_message($message_content_type,
|
---|
424 | $message_content_info,
|
---|
425 | $message_part_body);
|
---|
426 | } else {
|
---|
427 | $message_part_body=text_from_part($part_body);
|
---|
428 | $rfc822_formatted_body=text_into_html($message_part_body);
|
---|
429 | }
|
---|
430 | } else {
|
---|
431 | # message doesn't use MIME flags
|
---|
432 | $rfc822_formatted_body=text_into_html($message_part_body);
|
---|
433 | }
|
---|
434 | # Add the returned text to the output
|
---|
435 | # don't put all the headers...
|
---|
436 | $message_part_headers =~ s/^(X\-.*|received|message\-id|return\-path):.*\n//img;
|
---|
437 | $text.=text_into_html($message_part_headers);
|
---|
438 | $text.="<p>\n";
|
---|
439 | $text.=$rfc822_formatted_body;
|
---|
440 | # end of message/rfc822
|
---|
441 | } elsif ($part_content_type =~ /multipart/) {
|
---|
442 | # recurse again
|
---|
443 | $tmptext=text_from_mime_message($part_content_type,
|
---|
444 | $part_content_info,
|
---|
445 | $part_body);
|
---|
446 | $text.=$tmptext;
|
---|
447 | } elsif ($text eq "") {
|
---|
448 | # we can't do anything with this part, but if it's the first
|
---|
449 | # part then make sure it is mentioned..
|
---|
450 |
|
---|
451 | $text.="<p><hr><strong><<attachment>></>";
|
---|
452 | # add part info header
|
---|
453 | $text.="<br>Type: $part_content_type<br>\n";
|
---|
454 | if ($filename ne "") {
|
---|
455 | $text.="Filename: $filename\n";
|
---|
456 | }
|
---|
457 | $text.="</strong></p>\n";
|
---|
458 | }
|
---|
459 | } # foreach message part.
|
---|
460 | } else {
|
---|
461 | # we can't handle this multipart type (not mixed or alternative)
|
---|
462 | # the RFC also mentions "parallel".
|
---|
463 | }
|
---|
464 | } # end of multipart
|
---|
465 | return $text;
|
---|
466 | }
|
---|
467 |
|
---|
468 |
|
---|
469 |
|
---|
470 |
|
---|
471 |
|
---|
472 |
|
---|
473 | # Process a MIME part. Return "" if we can't decode it.
|
---|
474 | sub text_from_part {
|
---|
475 | my $text=shift;
|
---|
476 | my $part_header=$text;
|
---|
477 | $part_header =~ s/\r?\n\r?\n(.*)$//s;
|
---|
478 | $text=$1; if (!defined($text)) {$text="";}
|
---|
479 | $part_header =~ s/\r?\n[\t ]+/ /gs; #unfold
|
---|
480 | $part_header =~ /content\-type:\s*([\w\/]+)/is;
|
---|
481 | my $type=$1; if (!defined($type)) {$type="";}
|
---|
482 | my $encoding="";
|
---|
483 | if ($part_header =~ /^content\-transfer\-encoding:\s*([^\s]+)/mis) {
|
---|
484 | $encoding=$1; $encoding=~tr/A-Z/a-z/;
|
---|
485 | }
|
---|
486 | # Content-Transfer-Encoding is per-part
|
---|
487 | if ($encoding ne "") {
|
---|
488 | if ($encoding =~ /quoted\-printable/) {
|
---|
489 | $text=qp_decode($text);
|
---|
490 | } elsif ($encoding =~ /base64/) {
|
---|
491 | $text=base64_decode($text);
|
---|
492 | } elsif ($encoding !~ /[78]bit/) { # leave 7/8 bit as is.
|
---|
493 | # rfc2045 also allows binary, which we ignore (for now).
|
---|
494 | # maybe this shouldn't go to stderr, but anyway...
|
---|
495 | print STDERR "EMAILPlug: unknown encoding: $encoding\n";
|
---|
496 | return "";
|
---|
497 | }
|
---|
498 | }
|
---|
499 |
|
---|
500 | if ($type eq "text/html") {
|
---|
501 | # only get stuff between <body> tags, or <html> tags.
|
---|
502 | $text =~ s/^.*?<(html|HTML)[^>]*>//s;
|
---|
503 | $text =~ s/<\/(html|HTML)>.*$//s;
|
---|
504 |
|
---|
505 | $text =~ s/^.*?<(body|BODY)[^>]*>//s;
|
---|
506 | $text =~ s/<\/(body|BODY)>.*$//s;
|
---|
507 | }
|
---|
508 | elsif ($type eq "text/xml") {
|
---|
509 | $text=~s/</</g;$text=~s/>/>/g;
|
---|
510 | $text="<pre>\n$text\n</pre>\n";
|
---|
511 | }
|
---|
512 | return $text;
|
---|
513 | }
|
---|
514 |
|
---|
515 |
|
---|
516 | # decode quoted-printable text
|
---|
517 | sub qp_decode {
|
---|
518 | my $text=shift;
|
---|
519 |
|
---|
520 | my @lines=split('\n', $text);
|
---|
521 |
|
---|
522 | # if a line ends with "=\s*", it is a soft line break, otherwise
|
---|
523 | # keep in any newline characters.
|
---|
524 | foreach my $line (@lines) {
|
---|
525 | if ($line =~ s/=\s*$//) {}
|
---|
526 | else {$line.="\n";}
|
---|
527 |
|
---|
528 | if ($line =~ /=[0-9A-Fa-f]{2}/) { # it contains an escaped char
|
---|
529 | my @hexcode_segments=split('=',$line);
|
---|
530 | shift @hexcode_segments;
|
---|
531 | my @hexcodes;
|
---|
532 | foreach my $hexcode (@hexcode_segments) {
|
---|
533 | $hexcode =~ s/^(..).*$/$1/; # only need first 2 chars
|
---|
534 | chomp($hexcode); # just in case...
|
---|
535 | my $char=chr (hex "0x$hexcode");
|
---|
536 | $line =~ s/=$hexcode/$char/g;
|
---|
537 | }
|
---|
538 | }
|
---|
539 | }
|
---|
540 | $text= join('', @lines);
|
---|
541 | return $text;
|
---|
542 | }
|
---|
543 |
|
---|
544 | # decode base64 text. This is fairly slow (since it's interpreted perl rather
|
---|
545 | # than compiled XS stuff like in the ::MIME modules, but this is more portable
|
---|
546 | # for us at least).
|
---|
547 | # see rfc2045 for description, but basically, bits 7 and 8 are set to zero;
|
---|
548 | # 4 bytes of encoded text become 3 bytes of binary - remove 2 highest bits
|
---|
549 | # from each encoded byte.
|
---|
550 |
|
---|
551 |
|
---|
552 | sub base64_decode {
|
---|
553 | my $enc_text = shift;
|
---|
554 | # A=>0, B=>1, ..., '+'=>62, '/'=>63
|
---|
555 | # also '=' is used for padding at the end, but we remove it anyway.
|
---|
556 | my $mimechars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
---|
557 | # map each MIME char into it's value, for more efficient lookup.
|
---|
558 | my %index;
|
---|
559 | map { $index{$_} = index ($mimechars, $_) } (split ('', $mimechars));
|
---|
560 | # remove all non-base64 chars. eval to get variable in transliteration...
|
---|
561 | # also remove '=' - we'll assume (!!) that there are no errors in the encoding
|
---|
562 | eval "\$enc_text =~ tr|$mimechars||cd";
|
---|
563 | my $decoded="";
|
---|
564 | while (length ($enc_text)>3)
|
---|
565 | {
|
---|
566 | my $fourchars=substr($enc_text,0,4,"");
|
---|
567 | my @chars=(split '',$fourchars);
|
---|
568 | $decoded.=chr( $index{$chars[0]} << 2 | $index{$chars[1]} >> 4);
|
---|
569 | $decoded.=chr( ($index{$chars[1]} & 15) << 4 | $index{$chars[2]} >> 2);
|
---|
570 | $decoded.=chr( ($index{$chars[2]} & 3 ) << 6 | $index{$chars[3]});
|
---|
571 | }
|
---|
572 | # if there are any input chars left, there are either
|
---|
573 | # 2 encoded bytes (-> 1 raw byte) left or 3 encoded (-> 2 raw) bytes left.
|
---|
574 | my @chars=(split '',$enc_text);
|
---|
575 | if (length($enc_text)) {
|
---|
576 | $decoded.=chr($index{$chars[0]} << 2 | (int $index{$chars[1]} >> 4));
|
---|
577 | }
|
---|
578 | if (length($enc_text)==3) {
|
---|
579 | $decoded.=chr( ($index{$chars[1]} & 15) << 4 | $index{$chars[2]} >> 2);
|
---|
580 | }
|
---|
581 | return $decoded;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | # Perl packages have to return true if they are run.
|
---|
586 | 1;
|
---|