#!/usr/bin/perl ##--------------------------------------------------------------------------## ## File: ## $Id: extract-mesg.cgi.in.dist,v 1.5 2002/09/20 03:29:28 ehood Exp $ ## Author: ## Earl Hood earl@earlhood.com ## Description: ## POD at end-of-file. ##--------------------------------------------------------------------------## ## Copyright (C) 2002 Earl Hood ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA ##--------------------------------------------------------------------------## package MHArc::extract_mesg_cgi; use lib '/home/mharc/lib'; use CGI::Carp; use MHArc::CGI; ############################################################################# ## BEGIN: Config Section ############################################################################# ## Full pathname to where raw archives are located. my $mbox_archive_root = '/home/mharc/mbox'; ## Message media-type: This is the media-type this script will return ## to the client when serving up the raw mail message. Note, some ## browsers actually support message/rfc822, but this could potentially ## cause XSS HTML email attacks, so use with caution. my $message_media_type = 'text/plain'; ############################################################################# ## END: Config Section ############################################################################# $ENV{'PATH'} = '/usr/local/bin:/bin:/usr/bin'; ## Query argument name to contain name of archive my $argname_archive = 'a'; ## Query argument name to contain month my $argname_month = 'm'; ## Query argument name to contain message-id my $argname_id = 'i'; ## Mbox message separator: Try to be more strict than '^From ', but ## not too strict to deal with possible variations. my $msgsep = '^From \S+.*\d+:\d+:\d+'; MAIN: { my $form = MHArc::CGI::parse_input(); my $archive = $form->{$argname_archive} || ""; my $month = $form->{$argname_month} || ""; my $id = $form->{$argname_id} || ""; my $list_dir; if (($month !~ /^\d{4}(?:-\d{2})?$/) || ($id !~ /.\@./) || ($archive !~ /\S/) || ($archive =~ /\.\./) || (! -d ($list_dir = join('/', $mbox_archive_root,$archive)))) { warn qq/Invalid arguments: a=$archive, m=$month, i=$id\n/; MHArc::CGI::print_input_error(); last MAIN; } # Check if list has raw archive access disabled. if (-e join('/', $list_dir, '.noraw')) { MHArc::CGI::print_forbidden(); last MAIN; } my $gzipped = 0; my $mbox_file = join('/', $list_dir, $month); if (! -e $mbox_file) { $mbox_file .= '.gz'; $gzipped = 1; } if (! -e $mbox_file) { warn qq/"$mbox_file" does not exist\n/; MHArc::CGI::print_input_error(); last MAIN; } local(*MBOX); if ($gzipped) { if (!open(MBOX, "gzip -dc '$mbox_file' |")) { warn qq/Unable to exec "gzip -dc '$mbox_file'": $!\n/; MHArc::CGI::print_error(); last MAIN; } } else { if (!open(MBOX, $mbox_file)) { warn qq/Unable to open "$mbox_file": $!\n/; MHArc::CGI::print_error(); last MAIN; } } local $_; my $cache = ''; my $in_header = 1; my $msg_id = ''; my $found = 0; SCAN: while () { if (/$msgsep/o) { $cache = ''; $in_header = 1; next SCAN; } next SCAN unless $in_header; if (/^\r?$/) { $cache = ''; $in_header = 0; next SCAN; } $cache .= $_; if (s/^message-id:\s*//i) { s/\s+\Z//; s/[<>]//g; if ($_ eq $id) { $found = 1; last SCAN; } $cache = ''; $in_header = 0; } } if (!$found) { MHArc::CGI::print_not_found_error(); close(MBOX); last MAIN; } MHArc::CGI::print_content_type($message_media_type); print STDOUT $cache; while () { last if /$msgsep/o; print STDOUT $_; } close(MBOX); } ######################################################################## __END__ =head1 NAME extract-mesg.cgi - mharc CGI program to retrieve raw version of a message =head1 SYNOPSIS http://.../cgi-bin/extract-mesg.cgi?a=&m=&i= =head1 DESCRIPTION This CGI program retrieves the raw version of a message from an archive archived at a specified period and with a specified message-id. The CGI program will output the retrieved message to the web client. =head1 CGI OPTIONS =over =item C The name of the archive. Archive names are defined by C. =item C The message-id. =item C The period in YYYY-MM or YYYY format. =back =head1 VERSION C<$Id: extract-mesg.cgi.in.dist,v 1.5 2002/09/20 03:29:28 ehood Exp $> =head1 AUTHOR Earl Hood, earl@earlhood.com This module is part of the mharc archiving system and comes with ABSOLUTELY NO WARRANTY and may be copied only under the terms of the GNU General Public License, which may be found in the MHArc distribution. =cut