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

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

Regular expression fix.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 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 # slightly more strict validity check, to prevent us from matching
89 # .so.x files ...
90 return undef unless (($$textref =~ /^From /) ||
91 ($$textref =~ /^[-A-Za-z]{2,100}:/));
92
93 print $outhandle "EMAILPlug: processing $file\n"
94 if $self->{'verbosity'} > 1;
95
96 my $cursection = $doc_obj->get_top_section();
97
98 #
99 # Parse the document's text and extract metadata
100 #
101
102 # Separate header from body of message
103 my $Headers = $$textref;
104 $Headers =~ s/\n\n.*//s;
105 $$textref = substr $$textref, (length $Headers);
106
107 # Extract basic metadata from header
108 my @headers = ("From", "To", "Subject", "Date");
109 my %raw;
110 foreach my $name (@headers) {
111 $raw{$name} = "No $name value";
112 }
113
114 # Examine each line of the headers
115 my ($line, $name, $value);
116 my @parts;
117 foreach $line (split(/\n/, $Headers)) {
118
119 # Ignore lines with no content or which begin with whitespace
120 next unless ($line =~ /:/);
121 next if ($line =~ /^\s/);
122
123 # Find out what metadata is on this line
124 @parts = split(/:/, $line);
125 $name = shift @parts;
126 next unless $name;
127 next unless ($raw{$name});
128
129 # Find the value of that metadata
130 $value = join(":", @parts);
131 $value =~ s/^\s+//;
132 $value =~ s/\s+$//;
133
134 # Store the metadata
135 $raw{$name} = $value;
136 }
137
138 # Process Date information
139 if ($raw{"Date"} !~ /No Date/) {
140 $raw{"DateText"} = $raw{"Date"};
141
142 # Convert the date text to internal date format
143 $value = $raw{"Date"};
144 my ($day, $month, $year) = $value =~ /(\d?\d)\s([A-Z][a-z][a-z])\s(\d\d\d?\d?)/;
145 if ($year < 100) { $year += 1900; }
146 $raw{"Date"} = &sorttools::format_date($day, $month, $year);
147
148 } else {
149 # We have not extracted a date
150 $raw{"DateText"} = "Unknown.";
151 $raw{"Date"} = "19000000";
152 }
153
154
155 # Add extracted metadata to document object
156 foreach my $name (keys %raw) {
157 $value = $raw{$name};
158 if ($value) {
159 $value = &text_into_html($value);
160 } else {
161 $value = "No $name field";
162 }
163 $doc_obj->add_utf8_metadata ($cursection, $name, $value);
164 }
165
166 # Add "All headers" metadata
167 $Headers = &text_into_html($Headers);
168 $Headers = "No headers" unless ($Headers =~ /\w/);
169 $doc_obj->add_utf8_metadata ($cursection, "Headers", $Headers);
170
171 # Add text to document object
172 $$textref = &text_into_html($$textref);
173 $$textref = "No message" unless ($$textref =~ /\w/);
174 $doc_obj->add_utf8_text($cursection, $$textref);
175
176 return 1;
177}
178
179
180# Convert a text string into HTML.
181#
182# The HTML is going to be inserted into a GML file, so
183# we have to be careful not to use symbols like ">",
184# which ocurs frequently in email messages (and use
185# &gt instead.
186#
187# This function also turns links and email addresses into hyperlinks,
188# and replaces carriage returns with <BR> tags (and multiple carriage
189# returns with <P> tags).
190
191
192sub text_into_html {
193 my ($text) = @_;
194
195 # Convert problem characters into HTML symbols
196 $text =~ s/&/&amp;/go;
197 $text =~ s/</&lt;/go;
198 $text =~ s/>/&gt;/go;
199 $text =~ s/\"/&quot;/go;
200
201 # convert email addresses and URLs into links
202 $text =~ s/([\w\d\.\-]+@[\w\d\.\-]+)/<a href=\"mailto:$1\">$1<\/a>/g;
203 $text =~ s/(http:\/\/[\w\d\.\-]+[\/\w\d\.\-~]*)/<a href=\"$1\">$1<\/a>/g;
204
205 # Clean up whitespace and convert \n charaters to <BR> or <P>
206 $text =~ s/ +/ /go;
207 $text =~ s/\s*$//o;
208 $text =~ s/^\s*//o;
209 $text =~ s/\n/\n<BR>/go;
210 $text =~ s/<BR>\s*<BR>/<P>/go;
211
212 return $text;
213}
214
215
216# Perl packages have to return true if they are run.
2171;
Note: See TracBrowser for help on using the repository browser.