File Coverage

blib/lib/News/AutoReply.pm
Criterion Covered Total %
statement 9 43 20.9
branch 0 18 0.0
condition 0 9 0.0
subroutine 3 6 50.0
pod 1 3 33.3
total 13 79 16.4


line stmt bran cond sub pod time code
1             # -*- Perl -*-
2             ###########################################################################
3             # Written and maintained by Andrew Gierth
4             #
5             # Copyright 1997 Andrew Gierth. Redistribution terms at end of file.
6             #
7             # $Id: AutoReply.pm 1.10 2001/11/08 14:10:12 andrew Exp $
8             #
9             ###########################################################################
10             #
11             # Address, n. 1. A formal discourse, usually delivered to a person who has
12             # something by a person who wants something that he has.
13             # 2. The place at which one receives the delicate attentions
14             # of creditors.
15             # -- Ambrose Bierce
16             #
17              
18             =head1 NAME
19              
20             News::AutoReply - derivative of News::Article for generating autoreplies
21              
22             =head1 SYNOPSIS
23              
24             use News::AutoReply;
25              
26             $reply = News::AutoReply->new($message);
27              
28             =head1 DESCRIPTION
29              
30             Like News::Article, but must be given a reference to another article
31             at creation time - initialises To, In-Reply-To, References etc.
32             correctly as an automatic reply.
33              
34             =head1 USAGE
35              
36             use News::AutoReply;
37              
38             Exports nothing.
39              
40             =cut
41              
42             package News::AutoReply;
43              
44 2     2   6131 use News::Article;
  2         6  
  2         73  
45 2     2   16 use strict;
  2         5  
  2         71  
46 2     2   16 use vars qw(@ISA);
  2         4  
  2         1064  
47              
48             @ISA = qw(News::Article);
49              
50             =head1 Constructor
51              
52             =over 4
53              
54             =item new ( ORIGINAL )
55              
56             Construct an autoreply to a message, assuming that the Reply-To (if
57             present, otherwise the From) header of C is valid.
58              
59             Returns a new Article object with no body or envelope sender, but with
60             suitable headers.
61              
62             If an environment variable LOOP is defined, it is used as the contents
63             of an X-Loop header added to the reply (this is useful when using this
64             code in progs launched from a procmail recipe). Always preserves X-Loop
65             headers in the original.
66              
67             The reference-folding code could probably be improved.
68              
69             =cut
70              
71             sub new
72             {
73 0     0 1   my $class = shift;
74 0           my $src = shift;
75              
76 0           my $self = $class->SUPER::new(@_);
77 0 0         return undef unless $self;
78              
79 0           $self->reply_init($src);
80             }
81              
82             #--------------------------------------------------------------------------
83             # private. Factored out of new() so that FormReply etc. can inherit
84             # this.
85              
86             sub reply_init
87             {
88 0     0 0   my $self = shift;
89 0           my $src = shift;
90              
91 0   0       my $to = $src->header('reply-to') || $src->header('from');
92 0 0         return undef unless $to;
93              
94 0           $self->add_headers(to => $to);
95 0           $self->set_headers("x-loop" => [ $src->header("x-loop") ]);
96 0 0         $self->add_headers("x-loop" => $ENV{LOOP}) if defined($ENV{LOOP});
97              
98 0 0         if (!defined($self->header("subject")))
99             {
100 0   0       my $subj = $src->header("subject") || "(no subject)";
101 0           $subj =~ s/^(\s*[Rr][Ee]:\s+)?/Re: /;
102 0           $self->set_headers(subject => $subj);
103             }
104            
105 0           my $srcid = $src->header("message-id");
106 0 0         $self->set_headers("in-reply-to" => $srcid) if $srcid;
107              
108 0   0       my $refs = $src->header("references") || '';
109 0           my @refs = split(' ',$refs);
110 0 0         push @refs,$srcid if $srcid;
111 0 0         if ($refs = $self->fold_references(@refs))
112             {
113 0           $self->set_headers(references => $refs);
114             }
115              
116 0           return $self;
117             }
118              
119             #----------------------------------------------------------------------------
120             # private; called as a method to allow overriding if necessary.
121              
122             sub fold_references
123             {
124 0     0 0   my $self = shift;
125 0   0       my $refs = shift || '';
126 0           my $length = 4 + length($refs);
127              
128 0           while (@_)
129             {
130 0           my $ref = shift;
131 0           $length += 1 + length($ref);
132 0 0         $refs .= ($length < 72) ? ' ' : "\n\t";
133 0           $refs .= $ref;
134 0 0         $length = length($ref) unless $length < 72;
135             }
136              
137 0           $refs;
138             }
139              
140             1;
141              
142             __END__