ContactForm/MHonArc.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 91 CC 8 SCAR 38.1
CC
8
Cov
0%
getmsgvalues
72.0
BEGIN
2.0
BEGIN
2.0
CRAP 45.1
1 package CGI::ContactForm::MHonArc;
2  
3 $VERSION = '1.02';
4 # $Id: MHonArc.pm,v 1.7 2006/12/07 00:39:31 gunnarh Exp $
5  
6 =head1 NAME
7  
8 CGI::ContactForm::MHonArc - Contact the message authors in a MHonArc archive
9  
10 =head1 SYNOPSIS
11  
12 use CGI::ContactForm;
13 use CGI::ContactForm::MHonArc;
14  
15 my $msg = getmsgvalues();
16  
17 contactform (
18 recname => $msg->{fromname},
19 recmail => $msg->{fromaddr},
20 styleurl => '/style/ContactForm.css',
21 returnlinktext => 'Messages by Date',
22 returnlinkurl => '/archive/maillist.html',
23 subject => $msg->{subject},
24 bouncetosender => 1,
25 );
26  
27 =head1 DESCRIPTION
28  
29 This module makes it easy to use L<CGI::ContactForm|CGI::ContactForm> for
30 contacting the message authors in a mail archive that was created by the
31 L<MHonArc|mhonarc> Email-to-HTML converter. Out from the message ID and the path
32 to the archive, it retrieves the necessary data from the archive database.
33  
34 The converted messages are supposed to include an HTML form for the purpose, so
35 you need to set one of the MHonArc page layout resources. This is an example:
36  
37 <form action="/cgi-bin/mhacontact.pl" method="get">
38 <input type="hidden" name="msgid" value="$MSGID$">
39 <input type="hidden" name="outdir" value="$OUTDIR$">
40 <input type="submit" value="Contact Author">
41 </form>
42  
43 The form controls shall be named C<msgid> and C<outdir> (case matters), and their
44 values are set dynamically via the MHonArc C<$MSGID$> respective C<$OUTDIR$>
45 resource variables when the messages are converted.
46  
47 Note that it is a C<GET> request that shall be submitted.
48  
49 The C<SYNOPSIS> section above is an example of what the CGI script, here named
50 C<mhacontact.pl>, may contain. The C<getmsgvalues()> function returns a reference
51 to a hash with the keys C<fromname>, C<fromaddr> and C<subject>, and the hash
52 values are passed to L<CGI::ContactForm|CGI::ContactForm> when the
53 C<contactform()> function is called.
54  
55 If the name of the archive database is not the default name, you need to pass
56 the database name to C<CGI::ContactForm::MHonArc>:
57  
58 my $msg = getmsgvalues('myfilename');
59 my $msg = getmsgvalues('/absolute/path/to/myfilename');
60  
61 If you pass the absolute pathname, the C<outdir> control can be excluded from
62 the HTML form.
63  
64 The C<Return-Path> message header controls where bounced messages are sent.
65 By default, L<CGI::ContactForm|CGI::ContactForm> sets the C<Return-Path> to
66 the recipient address. However, when sending messages to archived addresses,
67 there is an obvious risk that they are no longer valid, and it makes more sense
68 that possible bounces are sent to the sender address instead. That is
69 accomplished by passing a true value to C<contactform()> with the
70 C<bouncetosender> argument (see the C<SYNOPSIS> example).
71  
72 =head1 AUTHOR, COPYRIGHT AND LICENSE
73  
74 Copyright (c) 2004-2006 Gunnar Hjalmarsson
75 http://www.gunnar.cc/cgi-bin/contact.pl
76  
77 This module is free software; you can redistribute it and/or modify it
78 under the same terms as Perl itself.
79  
80 =head1 SEE ALSO
81  
82 L<CGI::ContactForm|CGI::ContactForm>, L<MHonArc|mhonarc>
83  
84 =cut
85  
86 use strict;
87 use CGI::ContactForm 1.40 'CFdie';
88 use vars qw($VERSION @ISA @EXPORT %MsgId %From %Subject);
89 use Exporter;
90 @ISA = 'Exporter';
91 @EXPORT = 'getmsgvalues';
92  
93 sub getmsgvalues {
94 local $^W = 1;
95 my %msg;
96  
97 if ($ENV{REQUEST_METHOD} eq 'GET') {
98 my $q = new CGI;
99 my $id = $q->param('msgid') or CFdie("Message ID is missing.\n");
100 my %nodot;
101 @nodot{ qw/MSWin32 dos os2 VMS/ } = ();
102 my $defaultdb = exists $nodot{$^O} ? 'mhonarc.db' : '.mhonarc.db';
103 my $dbfile = (shift or $defaultdb);
104 unless ( File::Spec->file_name_is_absolute($dbfile) ) {
105 my $outdir = $q->param('outdir')
106 or CFdie("Path to the archive directory is missing.\n");
107 $outdir = $1 if $outdir =~ /^([-+@\w.\/\\: \[\]]+)$/;
108 $dbfile = File::Spec->catfile($outdir, $dbfile);
109 }
110  
111 eval { require $dbfile };
112 CFdie( CGI::escapeHTML(my $msg = $@) ) if $@;
113  
114 my $from = $From{ $MsgId{$id} }
115 or CFdie("Message ID \"$id\" not found in database.\n");
116 if ( $from =~ /("?)(.+)\1 <([^>]+)>/ ) {
117 @msg{ qw/fromname fromaddr/ } = ($2, $3);
118 } else {
119 ( $msg{fromname} ) = $from =~ /(.+)@/;
120 $msg{fromaddr} = $from;
121 }
122 ( $msg{subject} = $Subject{ $MsgId{$id} } ) =~ s/^Re:\s+//i;
123 $msg{subject} = "Re: $msg{subject}" if $msg{subject};
124  
125 print 'Set-cookie: M2H=', ( join ':',
126 map { (my $elem = $_) =~ s/([^-\w.!~*'()])/sprintf '%%%02X', ord $1/eg; $elem }
127 @msg{ qw/fromname fromaddr subject/ } ), "\n";
128  
129 } else {
130 $ENV{HTTP_COOKIE} && ( (my $cookie) = $ENV{HTTP_COOKIE} =~ /\bM2H=([^;]+)/ )
131 or CFdie("Your browser is set to refuse cookies.<br>\n"
132 ."Change that setting to accept at least session cookies, and try again.\n");
133 @msg{ qw/fromname fromaddr subject/ } =
134 map { s/%(..)/chr(hex $1)/eg; $_ } split /:/, $cookie;
135 }
136  
137 \%msg;
138 }
139  
140 1;
141