source: trunk/gsdl/perllib/plugins/EMAILPlug.pm@ 1658

Last change on this file since 1658 was 1658, checked in by paynter, 23 years ago

Fixed a bug reading the headers that confused "To" with "In-Reply-To".

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
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 mh_mail folders) or with the
35# extension .email
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# $DateText Date: header
47# $Date Date: header in GSDL format (eg: 19990924)
48
49package EMAILPlug;
50
51use BasPlug;
52use sorttools;
53use util;
54
55
56# EMAILPlug is a sub-class of BasPlug.
57
58sub BEGIN {
59 @ISA = ('BasPlug');
60}
61
62# Create a new EMAILPlug object with which to parse a file.
63# Accomplished by creating a new BasPlug and using bless to
64# turn it into an EMAILPlug.
65
66sub new {
67 my ($class) = @_;
68 my $self = new BasPlug ("EMAILPlug", @_);
69
70 return bless $self, $class;
71}
72
73sub get_default_process_exp {
74 my $self = shift (@_);
75
76 return q^\d+(\.email)?$^;
77}
78
79# do plugin specific processing of doc_obj
80sub process {
81 my $self = shift (@_);
82 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
83 my $outhandle = $self->{'outhandle'};
84
85 # Check that we're dealing with a valid mail file
86 return undef unless (($$textref =~ /From:/) || ($$textref =~ /To:/));
87
88 print $outhandle "EMAILPlug: processing $file\n"
89 if $self->{'verbosity'} > 1;
90
91 my $cursection = $doc_obj->get_top_section();
92
93 #
94 # Parse the document's text and extract metadata
95 #
96
97 # Separate header from body of message
98 my $Headers = $$textref;
99 $Headers =~ s/\n\n.*//s;
100 $$textref = substr $$textref, (length $Headers);
101
102 # Extract basic metadata from header
103 my @headers = ("From", "To", "Subject", "Date");
104 my %raw;
105 foreach my $name (@headers) {
106 $raw{$name} = "No $name value";
107 }
108
109 # Examine each line of the headers
110 my ($line, $name, $value);
111 my @parts;
112 foreach $line (split(/\n/, $Headers)) {
113
114 # Ignore lines with no content or which begin with whitespace
115 next unless ($line =~ /:/);
116 next if ($line =~ /^\s/);
117
118 # Find out what metadata is on this line
119 @parts = split(/:/, $line);
120 $name = shift @parts;
121 next unless $name;
122 next unless ($raw{$name});
123
124 # Find the value of that metadata
125 $value = join(":", @parts);
126 $value =~ s/^\s+//;
127 $value =~ s/\s+$//;
128
129 # Store the metadata
130 $raw{$name} = $value;
131 }
132
133 # Process Date information
134 if ($raw{"Date"} !~ /No Date/) {
135 $raw{"DateText"} = $raw{"Date"};
136
137 # Convert the date text to internal date format
138 $value = $raw{"Date"};
139 my ($day, $month, $year) = $value =~ /(\d?\d)\s([A-Z][a-z][a-z])\s(\d\d\d?\d?)/;
140 if ($year < 100) { $year += 1900; }
141 $raw{"Date"} = &sorttools::format_date($day, $month, $year);
142
143 } else {
144 # We have not extracted a date
145 $raw{"DateText"} = "Unknown.";
146 $raw{"Date"} = "19000000";
147 }
148
149
150 # Add extracted metadata to document object
151 foreach my $name (keys %raw) {
152 $value = $raw{$name};
153 if ($value) {
154 $value = &text_into_html($value);
155 } else {
156 $value = "No $name field";
157 }
158 $doc_obj->add_utf8_metadata ($cursection, $name, $value);
159 }
160
161 # Add "All headers" metadata
162 $Headers = &text_into_html($Headers);
163 $Headers = "No headers" unless ($Headers =~ /\w/);
164 $doc_obj->add_utf8_metadata ($cursection, "Headers", $Headers);
165
166 # Add text to document object
167 $$textref = &text_into_html($$textref);
168 $$textref = "No message" unless ($$textref =~ /\w/);
169 $doc_obj->add_utf8_text($cursection, $$textref);
170
171 return 1;
172}
173
174
175# Convert a text string into HTML.
176#
177# The HTML is going to be inserted into a GML file, so
178# we have to be careful not to use symbols like ">",
179# which ocurs frequently in email messages (and use
180# &gt instead.
181#
182# This function also turns links and email addresses into hyperlinks,
183# and replaces carriage returns with <BR> tags (and multiple carriage
184# returns with <P> tags).
185
186
187sub text_into_html {
188 my ($text) = @_;
189
190 # Convert problem characters into HTML symbols
191 $text =~ s/&/&amp;/go;
192 $text =~ s/</&lt;/go;
193 $text =~ s/>/&gt;/go;
194 $text =~ s/\"/&quot;/go;
195
196 # convert email addresses and URLs into links
197 $text =~ s/([\w\d\.\-]+@[\w\d\.\-]+)/<a href=\"mailto:$1\">$1<\/a>/g;
198 $text =~ s/(http:\/\/[\w\d\.\-]+[\/\w\d\.\-~]*)/<a href=\"$1\">$1<\/a>/g;
199
200 # Clean up whitespace and convert \n charaters to <BR> or <P>
201 $text =~ s/ +/ /go;
202 $text =~ s/\s*$//o;
203 $text =~ s/^\s*//o;
204 $text =~ s/\n/\n<BR>/go;
205 $text =~ s/<BR>\s*<BR>/<P>/go;
206
207 return $text;
208}
209
210
211# Perl packages have to return true if they are run.
2121;
Note: See TracBrowser for help on using the repository browser.