1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
package Mail::Cap;
use strict;
use vars qw($VERSION $useCache);
$VERSION = "1.60";
sub Version { $VERSION; }
=head1 NAME
Mail::Cap - Parse mailcap files
=head1 SYNOPSIS
my $mc = new Mail::Cap;
$desc = $mc->description('image/gif');
print "GIF desc: $desc\n";
$cmd = $mc->viewCmd('text/plain; charset=iso-8859-1', 'file.txt');
=head1 DESCRIPTION
Parse mailcap files as specified in RFC 1524 - I<A User Agent
Configuration Mechanism For Multimedia Mail Format Information>. In
the description below C<$type> refers to the MIME type as specified in
the I<Content-Type> header of mail or HTTP messages. Examples of
types are:
image/gif
text/html
text/plain; charset=iso-8859-1
=cut
$useCache = 1; # don't evaluate tests every time
my @path;
if($^O eq "MacOS") {
@path = split(/,/, $ENV{MAILCAPS} ||
"$ENV{HOME}mailcap");
} else {
@path = split(/:/, $ENV{MAILCAPS} ||
# this path is specified under RFC 1524 appendix A
( defined($ENV{HOME})
? "$ENV{HOME}/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap"
: "/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap"));
}
=head1 METHODS
=head2 new(OPTIONS)
$mcap = new Mail::Cap;
$mcap = new Mail::Cap "/mydir/mailcap";
$mcap = new Mail::Cap filename => "/mydir/mailcap";
$mcap = new Mail::Cap take => 'ALL';
$mcap = Mail::Cap->new(take => 'ALL');
Create and initialize a new Mail::Cap object. If you give it an
argument it will try to parse the specified file. Without any
arguments it will search for the mailcap file using the standard
mailcap path, or the MAILCAPS environment variable if it is defined.
There is currently two OPTION implemented:
=over 4
=item * take =E<gt> 'ALL'|'FIRST'
Include all mailcap files you can find. By default, only the first
file is parsed, however the RFC tells us to include ALL. To maintain
backwards compatibility, the default only takes the FIRST.
=item * filename =E<gt> FILENAME
Add the specified file to the list to standard locations. This file
is tried first.
=back
=cut
sub new
{
my $class = shift;
if(@_ % 2 == 1) {unshift @_, 'filename'}
my %args = @_;
my $take_all = $args{take} && uc $args{take} eq 'ALL';
my $self = bless {}, $class;
$self->{_count} = 0;
if (defined($args{filename}) && -r $args{filename}) {
$self->_process_file($args{filename});
}
if ( !defined($args{filename}) || $take_all)
{ my $fname;
foreach $fname (@path) {
if (-r $fname) {
$self->_process_file($fname);
last unless $take_all;
}
}
}
unless ($self->{_count}) {
# Set up default mailcap
$self->{'audio/*'} = [{'view' => "showaudio %s"}];
$self->{'image/*'} = [{'view' => "xv %s"}];
$self->{'message/rfc822'} = [{'view' => "xterm -e metamail %s"}];
}
$self;
}
sub _process_file
{
my $self = shift;
my $file = shift;
unless($file) { return;}
local *MAILCAP;
if(open(MAILCAP, $file)) {
$self->{'_file'} = $file;
local($_);
while (<MAILCAP>) {
next if /^\s*#/; # comment
next if /^\s*$/; # blank line
while (s/\\\s*$//) { # continuation line
$_ .= <MAILCAP>;
}
chomp;
s/\0//g; # ensure no NULs in the line
s/([^\\]);/$1\0/g; # make field separator NUL
my @parts = split(/\s*\0\s*/, $_);
my $type = shift(@parts);
$type .= "/*" unless $type =~ m,/,;
my $view = shift(@parts);
$view =~ s/\\;/;/g;
my %field = ('view' => $view);
for (@parts) {
my($key,$val) = split(/\s*=\s*/, $_, 2);
if (defined $val) {
$val =~ s/\\;/;/g;
} else {
$val = 1;
}
$field{$key} = $val;
}
if ($field{'test'}) {
my $test = $field{'test'};
unless ($test =~ /%/) {
# No parameters in test, can perform it right away
system $test;
next if $?;
}
}
# record this entry
unless (exists $self->{$type}) {
$self->{$type} = [];
$self->{_count}++;
}
push(@{$self->{$type}}, \%field);
}
close(MAILCAP);
}
}
=head2 view($type, $file)
=head2 compose($type, $file)
=head2 edit($type, $file)
=head2 print($type, $file)
These methods invoke a suitable progam presenting or manipulating the
media object in the specified file. They all return C<1> if a command
was found, and C<0> otherwise. You might test C<$?> for the outcome
of the command.
=cut
sub view { my $self = shift; $self->_run($self->viewCmd(@_)); }
sub compose { my $self = shift; $self->_run($self->composeCmd(@_)); }
sub edit { my $self = shift; $self->_run($self->editCmd(@_)); }
sub print { my $self = shift; $self->_run($self->printCmd(@_)); }
=head2 viewCmd($type, $file)
=head2 composeCmd($type, $file)
=head2 editCmd($type, $file)
=head2 printCmd($type, $file)
These methods return a string that is suitable for feeding to system()
in order to invoke a suitable progam presenting or manipulating the
media object in the specified file. It will return C<undef> if no
suitable specification exists.
=cut
sub viewCmd { shift->_createCommand('view', @_); }
sub composeCmd { shift->_createCommand('compose', @_); }
sub editCmd { shift->_createCommand('edit', @_); }
sub printCmd { shift->_createCommand('print', @_); }
sub _createCommand
{
my($self, $method, $type, $file) = @_;
my $entry = $self->getEntry($type, $file);
return undef unless $entry;
if (exists $entry->{$method}) {
return $self->expandPercentMacros($entry->{$method}, $type, $file);
} else {
return undef;
}
}
sub _run
{
my($self, $cmd) = @_;
if (defined $cmd) {
system $cmd;
return 1;
}
0;
}
sub makeName
{
my($self, $type, $basename) = @_;
my $template = $self->nametemplate($type);
return $basename unless $template;
$template =~ s/%s/$basename/g;
$template;
}
=head2 field($type, $field)
Returns the specified field for the type. Returns undef if no
specification exsists.
=cut
sub field
{
my($self, $type, $field) = @_;
my $entry = $self->getEntry($type);
$entry->{$field};
}
=head2 description($type)
=head2 textualnewlines($type)
=head2 x11_bitmap($type)
=head2 nametemplate($type)
These methods return the corresponding mailcap field for the type.
These methods should be more convenient to use than the field() method
for the same fields.
=cut
sub description { shift->field(shift, 'description'); }
sub textualnewlines { shift->field(shift, 'textualnewlines'); }
sub x11_bitmap { shift->field(shift, 'x11-bitmap'); }
sub nametemplate { shift->field(shift, 'nametemplate'); }
sub getEntry
{
my($self, $origtype, $file) = @_;
if ($useCache) {
if (exists $self->{'_cache'}{$origtype}) {
return $self->{'_cache'}{$origtype};
}
}
my($fulltype, @params) = split(/\s*;\s*/, $origtype);
my($type, $subtype) = split(/\//, $fulltype, 2);
$subtype = "" unless defined $subtype;
my $entry;
for (@{$self->{"$type/$subtype"}}, @{$self->{"$type/*"}}) {
if (exists $_->{'test'}) {
# must run test to see if it applies
my $test = $self->expandPercentMacros($_->{'test'},
$origtype, $file);
system $test;
next if $?;
}
$entry = { %$_ }; # make copy
last;
}
$self->{'_cache'}{$origtype} = $entry if $useCache;
$entry;
}
sub expandPercentMacros
{
my($self,$text,$type,$file) = @_;
return $text unless defined $type;
$file = "" unless defined $file;
my($fulltype, @params) = split(/\s*;\s*/, $type);
my $subtype;
($type, $subtype) = split(/\//, $fulltype, 2);
my %params;
for (@params) {
my($key,$val) = split(/\s*=\s*/, $_, 2);
$params{$key} = $val;
}
$text =~ s/\\%/\0/g; # hide all escaped %'s
$text =~ s/%t/$fulltype/g; # expand %t
$text =~ s/%s/$file/g; # expand %s
{ # expand %{field}
local($^W) = 0; # avoid warnings when expanding %params
$text =~ s/%\{\s*(.*?)\s*\}/$params{$1}/g;
}
$text =~ s/\0/%/g;
$text;
}
# This following procedures can be useful for debugging purposes
sub dumpEntry
{
my($hash, $prefix) = @_;
$prefix = "" unless defined $prefix;
for (sort keys %$hash) {
print "$prefix$_ = $hash->{$_}\n";
}
}
sub dump
{
my($self) = @_;
for (keys %$self) {
next if /^_/;
print "$_\n";
for (@{$self->{$_}}) {
dumpEntry($_, "\t");
print "\n";
}
}
if (exists $self->{'_cache'}) {
print "Cached types\n";
for (keys %{$self->{'_cache'}}) {
print "\t$_\n";
}
}
}
=head1 COPYRIGHT
Copyright (c) 1995 Gisle Aas. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 AUTHOR
Gisle Aas <aas@oslonett.no>
Modified by Graham Barr <gbarr@pobox.com>
Maintained by Mark Overmeer <mailtools@overmeer.net>
=cut
1;
|