File Coverage

blib/lib/App/Open/Backend/MailCap.pm
Criterion Covered Total %
statement 40 40 100.0
branch 11 12 91.6
condition 9 11 81.8
subroutine 12 12 100.0
pod 8 8 100.0
total 80 83 96.3


line stmt bran cond sub pod time code
1             #
2             #===============================================================================
3             #
4             # FILE: MailCap.pm
5             #
6             # DESCRIPTION: mailcap backend for App::Open
7             #
8             # FILES: ---
9             # BUGS: ---
10             # NOTES: ---
11             # AUTHOR: Erik Hollensbe (),
12             # COMPANY:
13             # VERSION: 1.0
14             # CREATED: 06/03/2008 05:28:27 AM PDT
15             # REVISION: ---
16             #===============================================================================
17              
18             package App::Open::Backend::MailCap;
19              
20 2     2   38226 use strict;
  2         4  
  2         134  
21 2     2   11 use warnings;
  2         5  
  2         56  
22              
23 2     2   10 use Mail::Cap;
  2         3  
  2         45  
24 2     2   9 use MIME::Types;
  2         3  
  2         947  
25              
26             =head1 NAME
27              
28             App::Open::Backend::MailCap: A backend for using the mailcap system to lookup programs.
29              
30             =head1 SYNOPSIS
31              
32             Please read App::Open::Backend for information on how to use backends.
33              
34             =head1 METHODS
35              
36             Read App::Open::Backend for what the interface provides, method descriptions
37             here will only cover implementation.
38              
39             =over 4
40              
41             =item new
42              
43             Takes two args, the filename and a "take" argument that corresponds to the
44             Mail::Cap constructor argument of the same name, specifying to take the first
45             mailcap file it finds, or all of them. There is some decision made when either
46             of these arguments is undef, see load_definitions() for more information.
47              
48             =cut
49              
50             sub new {
51 9     9 1 7756 my ($class, $args) = @_;
52              
53 9 100 100     100 die "BACKEND_CONFIG_ERROR" if ($args && ref($args) ne 'ARRAY');
54              
55 7   100     66 $args ||= [];
56              
57 7         46 my $self = bless {
58             mailcap_file => $args->[0],
59             mailcap_take => $args->[1]
60             }, $class;
61              
62 7         34 $self->load_definitions;
63              
64 7         248 return $self;
65             }
66              
67             =item mailcap_file
68              
69             Return the mailcap filename supplied to the constructor.
70              
71             =cut
72              
73 2     2 1 3286 sub mailcap_file { $_[0]->{mailcap_file} }
74              
75             =item mailcap_take
76              
77             Return the 'take' argument supplied to the constructor.
78              
79             =cut
80              
81 2     2 1 127 sub mailcap_take { $_[0]->{mailcap_take} }
82              
83             =item mailcap
84              
85             Return the Mail::Cap object.
86              
87             =cut
88              
89 2     2 1 597 sub mailcap { $_[0]->{mailcap} }
90              
91             =item mime
92              
93             Return the Mime::Types object.
94              
95             =cut
96              
97 3     3 1 33 sub mime { $_[0]->{mime} }
98              
99             =item load_definitions
100              
101             Load the mailcap definitions and construct Mail::Cap and Mime::Types objects.
102             This method is called from the constructor; there is no reason to call it
103             directly.
104              
105             This method will generate defaults for the `take` argument depending on what is
106             supplied to the constructor. Basically, if you omit both arguments it will
107             swallow all mailcap files, if you provide a take argument it will use that. If
108             you provide a filename it will just use that, and if you supply `ALL` as the
109             take method and a filename, it will search that file first, then cascade to the
110             rest of the files on the system.
111              
112             It could be better.
113              
114             =cut
115              
116             sub load_definitions {
117 7     7 1 14 my $self = shift;
118              
119 7         11 my %mailcap_args;
120              
121 7         39 foreach my $arg ([qw(mailcap_file filename)],[qw(mailcap_take take)]) {
122 14 100       70 $mailcap_args{$arg->[1]} = $self->{$arg->[0]} if ($self->{$arg->[0]});
123             }
124              
125             #
126             # here's a quick rundown:
127             #
128             # if there are no arguments, "take" is set to "ALL", and the filename is unset.
129             # if there is a filename, "take" is set to FIRST unless set otherwise.
130             #
131             # I think this is the expected behavior when setting a filename; that it be
132             # the only one consulted.
133             #
134              
135 7 100 66     73 $mailcap_args{take} = "ALL" unless($mailcap_args{take} || $mailcap_args{filename});
136 7 100 66     59 $mailcap_args{take} = "FIRST" if($mailcap_args{filename} && !$mailcap_args{take});
137              
138 7         19 $self->{mailcap_take} = $mailcap_args{take}; # keep the accessor fresh
139              
140 7         65 $self->{mailcap} = new Mail::Cap(%mailcap_args);
141 7         120670 $self->{mime} = new MIME::Types;
142              
143 7         141340 return;
144             }
145              
146             =item lookup_file($extension)
147              
148             Given an extension, it will locate the MIME type for that extension via the
149             MIME::Types database, and locate the `view` mailcap entry for it, sanitizing it
150             for templating later.
151              
152             =cut
153              
154             sub lookup_file {
155 3     3 1 19 my ($self, $extension) = @_;
156              
157 3         6 my $program;
158              
159 3         26 my $type = $self->mime->mimeTypeOf($extension);
160              
161 3 100       1486 if ($type) {
162 2         545 $program = $self->mailcap->viewCmd($type, '%s');
163              
164             # since we're using the list form of system() underneath, we don't need the
165             # quotes... in fact, they'll cause problems.
166              
167 2 50       437 $program =~ s/['"]%s['"]/%s/g if ($program);
168             }
169              
170 3         58 return $program;
171             }
172              
173             =item lookup_url
174              
175             Always returns undef. AFAICT mailcap does not support URLs.
176              
177             =cut
178              
179 1     1 1 9 sub lookup_url { undef }
180              
181             =back
182              
183             =head1 LICENSE
184              
185             This file and all portions of the original package are (C) 2008 Erik Hollensbe.
186             Please see the file COPYING in the package for more information.
187              
188             =head1 BUGS AND PATCHES
189              
190             Probably a lot of them. Report them to if you're feeling
191             kind. Report them to CPAN RT if you'd prefer they never get seen.
192              
193             =cut
194              
195             1;