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 | #
|
---|
49 | # Version history
|
---|
50 | #
|
---|
51 | # 1.2 (2000 Jun 12) Major rewrite.
|
---|
52 | # (The new version of Greenstone breaks some of the metadata.)
|
---|
53 | # 1.1.1 Compensated for two-digit years like "95"
|
---|
54 | # 1.1 (1999 Sep 20) Introduced the various metadata fileds
|
---|
55 | # 1.0 Based on the original HTMLPlug code
|
---|
56 | #
|
---|
57 |
|
---|
58 |
|
---|
59 | package EMAILPlug;
|
---|
60 |
|
---|
61 | use BasPlug;
|
---|
62 | use sorttools;
|
---|
63 | use util;
|
---|
64 |
|
---|
65 |
|
---|
66 | # EMAILPlug is a sub-class of BasPlug.
|
---|
67 |
|
---|
68 | sub BEGIN {
|
---|
69 | @ISA = ('BasPlug');
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | # Create a new EMAILPlug object with which to parse a file.
|
---|
74 | # Accomplished by creating a new BasPlug and using bless to
|
---|
75 | # turn it into an EMAILPlug.
|
---|
76 |
|
---|
77 | sub new {
|
---|
78 | my ($class) = @_;
|
---|
79 | $self = new BasPlug ();
|
---|
80 | return bless $self, $class;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | # Is EMAILPlug recursive? No.
|
---|
85 |
|
---|
86 | sub is_recursive {
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | # Read a file and store its contents in a new document object.
|
---|
92 | # First, we check to see if it is an email message we're dealing
|
---|
93 | # with, then we extract the text and metadata, then we store
|
---|
94 | # all this information.
|
---|
95 | #
|
---|
96 | # Returns: number of files processed or undef if it can't process
|
---|
97 | # a file. This plugin only processes one file at a time.
|
---|
98 |
|
---|
99 | sub read {
|
---|
100 | my $self = shift (@_);
|
---|
101 | my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
|
---|
102 |
|
---|
103 | #
|
---|
104 | # Check that we're dealig with a valid mail file
|
---|
105 | #
|
---|
106 |
|
---|
107 | # Make sure file exists
|
---|
108 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
109 | return undef unless (-e $filename);
|
---|
110 | return undef unless ($filename =~ /\d+(\.email)?$/);
|
---|
111 |
|
---|
112 | # Read the text and make sure it is an email message
|
---|
113 | open (FILE, $filename) || die "EMAILPlug::read - can't open $filename\n";
|
---|
114 | my @text = <FILE>;
|
---|
115 | my $text = join("", @text);
|
---|
116 | return undef unless (($text =~ /From:/) || ($text =~ /To:/));
|
---|
117 |
|
---|
118 | print STDERR "EMAILPlug: processing $filename\n" if $processor->{'verbosity'};
|
---|
119 |
|
---|
120 | #
|
---|
121 | # Parse the document's text and extract metadata
|
---|
122 | #
|
---|
123 |
|
---|
124 | # Separate header from body of message
|
---|
125 | my $Headers = $text;
|
---|
126 | $Headers =~ s/\n\n.*//s;
|
---|
127 | $text = substr $text, (length $Headers);
|
---|
128 |
|
---|
129 | # Extract basic metadata from header
|
---|
130 | my @headers = ("From", "To", "Subject", "Date");
|
---|
131 | my $value = "";
|
---|
132 | my %raw;
|
---|
133 |
|
---|
134 | foreach my $name (@headers) {
|
---|
135 | $value = $Headers;
|
---|
136 | $value =~ s/.*$name://s;
|
---|
137 | $value =~ s/\S*:.*//s;
|
---|
138 | $value =~ s/\s*$//;
|
---|
139 | $value =~ s/\s+/ /g;
|
---|
140 | $raw{$name} = $value;
|
---|
141 | }
|
---|
142 |
|
---|
143 | # Process Date information
|
---|
144 | if ($raw{"Date"}) {
|
---|
145 | $raw{"DateText"} = $raw{"Date"};
|
---|
146 |
|
---|
147 | # Convert the date text to internal date format
|
---|
148 | $value = $raw{"Date"};
|
---|
149 | my ($day, $month, $year) = $value =~ /(\d?\d)\s([A-Z][a-z][a-z])\s(\d\d\d?\d?)/;
|
---|
150 | if ($year < 100) { $year += 1900; }
|
---|
151 | $raw{"Date"} = &sorttools::format_date($day, $month, $year);
|
---|
152 |
|
---|
153 | } else {
|
---|
154 | # We have not extracted a date
|
---|
155 | $raw{"DateText"} = "Unknown.";
|
---|
156 | $raw{"Date"} = "19000000";
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | #
|
---|
161 | # Create a new document object
|
---|
162 | #
|
---|
163 |
|
---|
164 | my $doc_obj = new doc ($file, "indexed_doc");
|
---|
165 | my $cursection = $doc_obj->get_top_section();
|
---|
166 |
|
---|
167 | # Add specilised metadata
|
---|
168 | foreach my $name (keys %raw) {
|
---|
169 | $value = $raw{$name};
|
---|
170 | if ($value) {
|
---|
171 | $value = &text_into_html($value);
|
---|
172 | } else {
|
---|
173 | $value = "No $name field";
|
---|
174 | }
|
---|
175 | $doc_obj->add_metadata ($cursection, $name, $value);
|
---|
176 | }
|
---|
177 |
|
---|
178 | # Add "All headers" metadata
|
---|
179 | $Headers = &text_into_html($Headers);
|
---|
180 | $Headers = "No headers" unless ($Headers =~ /\w/);
|
---|
181 | $doc_obj->add_metadata ($cursection, "Headers", $Headers);
|
---|
182 |
|
---|
183 | # Add document text
|
---|
184 | $text = &text_into_html($text);
|
---|
185 | $text = "No message" unless ($text =~ /\w/);
|
---|
186 | $doc_obj->add_text ($cursection, $text);
|
---|
187 |
|
---|
188 | # Add the OID - that is, the big HASH value used as a unique ID
|
---|
189 | $doc_obj->set_OID ();
|
---|
190 |
|
---|
191 | # Process the document
|
---|
192 | $processor->process($doc_obj);
|
---|
193 |
|
---|
194 | # Return the number of documents processed
|
---|
195 | return 1;
|
---|
196 |
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | # Convert a text string into HTML.
|
---|
201 | #
|
---|
202 | # The HTML is going to be inserted into a GML file, so
|
---|
203 | # we have to be careful not to use symbols like ">",
|
---|
204 | # which ocurs frequently in email messages (and use
|
---|
205 | # > instead.
|
---|
206 | #
|
---|
207 | # This function also turns links and email addresses into hyperlinks,
|
---|
208 | # and replaces carriage returns with <BR> tags (and multiple carriage
|
---|
209 | # returns with <P> tags).
|
---|
210 |
|
---|
211 |
|
---|
212 | sub text_into_html {
|
---|
213 | my ($text) = @_;
|
---|
214 |
|
---|
215 | # Convert problem charaters into HTML symbols
|
---|
216 | $text =~ s/&/&/go;
|
---|
217 | $text =~ s/</</go;
|
---|
218 | $text =~ s/>/>/go;
|
---|
219 | $text =~ s/\"/"/go;
|
---|
220 |
|
---|
221 | # convert email addresses and URLs into links
|
---|
222 | $text =~ s/([\w\d\.\-]+@[\w\d\.\-]+)/<a href=\"mailto:$1\">$1<\/a>/g;
|
---|
223 | $text =~ s/(http:\/\/[\w\d\.\-]+[\/\w\d\.\-~]*)/<a href=\"$1\">$1<\/a>/g;
|
---|
224 |
|
---|
225 | # Clean up whitespace and convert \n charaters to <BR> or <P>
|
---|
226 | $text =~ s/ +/ /go;
|
---|
227 | $text =~ s/\s*$//o;
|
---|
228 | $text =~ s/^\s*//o;
|
---|
229 | $text =~ s/\n/\n<BR>/go;
|
---|
230 | $text =~ s/<BR>\s*<BR>/<P>/go;
|
---|
231 |
|
---|
232 | return $text;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | # Perl packages have to return true if they are run.
|
---|
237 | 1;
|
---|
238 |
|
---|
239 |
|
---|
240 |
|
---|
241 |
|
---|
242 |
|
---|
243 |
|
---|
244 |
|
---|