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

Last change on this file since 1424 was 1424, checked in by sjboddie, 24 years ago

Added a -out option to most of the perl building scripts to allow output
debug information to be directed to a file.

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