| 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 (), <erik@hollensbe.org> | ||||||||||||||||||||||||||||
| 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 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Mail::Cap; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use MIME::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 9 | my ($class, $args) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 9 | ▶ | die "BACKEND_CONFIG_ERROR" if ($args && ref($args) ne 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args and ref $args ne "ARRAY"
MC/DC100%2/2
$args and ref $args ne "ARRAY"
$args
ref $args ne "ARRAY"
Truth table100%3/3
A and B
A= $argsB= ref $args ne "ARRAY"
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 7 | ▶ | $args ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args ||= []
MC/DC100%1/1
$args ||= []
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | my $self = bless { | |||||||||||||||||||||||||||
| 58 | mailcap_file => $args->[0], | ||||||||||||||||||||||||||||
| 59 | mailcap_take => $args->[1] | ||||||||||||||||||||||||||||
| 60 | }, $class; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 7 | $self->load_definitions; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | return $self; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item mailcap_file | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Return the mailcap filename supplied to the constructor. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | sub mailcap_file { $_[0]->{mailcap_file} } | ||||||||||||||||||||||||||
|
Subroutine
mailcap_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =item mailcap_take | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Return the 'take' argument supplied to the constructor. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | sub mailcap_take { $_[0]->{mailcap_take} } | ||||||||||||||||||||||||||
|
Subroutine
mailcap_take: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item mailcap | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Return the Mail::Cap object. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | sub mailcap { $_[0]->{mailcap} } | ||||||||||||||||||||||||||
|
Subroutine
mailcap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item mime | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Return the Mime::Types object. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | sub mime { $_[0]->{mime} } | ||||||||||||||||||||||||||
|
Subroutine
mime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | 7 | ▶ | sub load_definitions { | ||||||||||||||||||||||||||
|
Subroutine
load_definitions: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 7 | my %mailcap_args; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 7 | foreach my $arg ([qw(mailcap_file filename)],[qw(mailcap_take take)]) { | |||||||||||||||||||||||||||
| 122 | 14 | ▶ | $mailcap_args{$arg->[1]} = $self->{$arg->[0]} if ($self->{$arg->[0]}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | $mailcap_args{take} = "ALL" unless($mailcap_args{take} || $mailcap_args{filename}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mailcap_args{"take"} or $mailcap_args{"filename"}
MC/DC50%1/2
$mailcap_args{"take"} or $mailcap_args{"filename"}
$mailcap_args{"take"}
$mailcap_args{"filename"}
Truth table66%2/3
A or B
A= $mailcap_args{"take"}B= $mailcap_args{"filename"}
| |||||||||||||||||||||||||||||
| 136 | 7 | ▶ | $mailcap_args{take} = "FIRST" if($mailcap_args{filename} && !$mailcap_args{take}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mailcap_args{"filename"} and not $mailcap_args{"take"}
MC/DC50%1/2
$mailcap_args{"filename"} and not $mailcap_args{"take"}
$mailcap_args{"filename"}
not $mailcap_args{"take"}
Truth table66%2/3
A and not(B)
A= $mailcap_args{"filename"}B= not $mailcap_args{"take"}
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 7 | $self->{mailcap_take} = $mailcap_args{take}; # keep the accessor fresh | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 7 | $self->{mailcap} = new Mail::Cap(%mailcap_args); | |||||||||||||||||||||||||||
| 141 | 7 | $self->{mime} = new MIME::Types; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 7 | 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 | 3 | ▶ | sub lookup_file { | ||||||||||||||||||||||||||
|
Subroutine
lookup_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 3 | my ($self, $extension) = @_; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 3 | my $program; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 3 | my $type = $self->mime->mimeTypeOf($extension); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | ▶ | if ($type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 2 | $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 | ▶ | $program =~ s/['"]%s['"]/%s/g if ($program); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 3 | 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 | ▶ | sub lookup_url { undef } | ||||||||||||||||||||||||||
|
Subroutine
lookup_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 <erik@hollensbe.org> if you're feeling | ||||||||||||||||||||||||||||
| 191 | kind. Report them to CPAN RT if you'd prefer they never get seen. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 3 | 1; | |||||||||||||||||||||||||||