2 ###############################################################################
4 # Run-Mailcap: Run a program specified in the mailcap file based on a mime
7 # Written by Brian White <bcwhite@pobox.com>
8 # This file has been placed in the public domain (the only true "free").
10 ###############################################################################
16 $etcmimetyp="/etc/mime.types";
17 $shrmimetyp="/usr/share/etc/mime.types";
18 $locmimetyp="/usr/local/etc/mime.types";
19 $usrmimetyp="$ENV{HOME}/.mime.types";
20 $xtermprgrm="/usr/bin/x-terminal-emulator"; # xterm?
21 $defmimetyp="application/octet-stream";
29 '(^|/)crontab[^/]+$' => 'text/x-crontab', #'
30 '/man\d*/' => 'application/x-troff-man', #'
31 '\.\d[^\.]*$' => 'application/x-troff-man', #'
38 print STDERR $error,"\n\n" if $error;
40 print STDERR "Use: $0 <--action=VAL> [--debug] [MIME-TYPE:[ENCODING:]]FILE [...]\n\n";
41 print STDERR "Options:\n";
42 print STDERR " action specify what action to do on these files (default=view)\n";
43 print STDERR " debug be verbose about what's going on\n";
44 print STDERR " pager ignore any \"copiousoutput\" directives and use a \"pager\"\n";
45 print STDERR " norun just print but don't execute the command (useful with --debug)\n";
47 print STDERR "Mime-Type:\n";
48 print STDERR " any standard mime type designation in the form <class>/<subtype> -- if\n";
49 print STDERR " not specified, it will be determined from the filename extension\n\n";
50 print STDERR "Encoding:\n";
51 print STDERR " how the file (and type) has been encoded (only \"gzip\", \"bzip\", \"bzip2\"\n";
52 print STDERR " and \"compress\" are supported) -- if not specified, it will be determined\n";
53 print STDERR " from the filename extension\n\n";
55 exit ($error ? 1 : 0);
64 if ($file =~ m/\.gz$/) { $encoding = "gzip"; }
65 if ($file =~ m/\.bz$/) { $encoding = "bzip"; }
66 if ($file =~ m/\.bz2$/) { $encoding = "bzip2"; }
67 if ($file =~ m/\.Z$/) { $encoding = "compress"; }
69 print STDERR " - file \"$file\" has encoding \"$encoding\"\n" if $debug && $encoding;
79 return unless -r $file;
81 print STDERR " - Reading mime.types file \"$file\"...\n" if $debug;
82 open(MIMETYPES,"<$file") || die "Error: could not read \"$file\" -- $!\n";
89 my($type,@exts) = split;
92 $mimetypes{$_} = $type unless exists $mimetypes{$_};
104 return unless -r $file;
106 print STDERR " - Reading mailcap file \"$file\"...\n" if $debug;
107 open(MAILCAP,"<$file") || die "Error: could not read \"$file\" -- $!\n";
113 if ($line =~ m/^\s*\#/) {
117 if ($line =~ m/\\$/) {
120 $line =~ s/\\;/$quotedsemi/go;
121 $line =~ s/\\%/$quotedprct/go;
133 my($cmd,$head,$tail,$tmpfile);
134 $template = "" unless (defined $template);
136 ($head,$tail) = split(/%s/,$template,2);
138 # $tmpfile = POSIX::tmpnam($name);
141 $cmd = "mktemp -t "; # -t is depreciated, but --tmpdir would not be handled by older mktemp
142 $cmd .= "$head" if $head;
143 $cmd .= ".$tail" if $tail;
149 # $tmpfile = $ENV{TMPDIR};
150 # $tmpfile = "/tmp" unless $tmpfile;
151 # $tmpfile.= "/$name";
161 my($tmpfile,$amt,$buf);
163 $tmpfile = $1 if ($match =~ m/nametemplate=(.*?)\s*($|;)/);
164 $tmpfile = TempFile($tmpfile);
165 open(TMPFILE,">$tmpfile") || die "Error: could not write \"$tmpfile\" -- $!\n";
167 $amt = read(STDIN,$buf,102400);
168 print TMPFILE $buf if $amt;
178 my($efile,$encoding,$action) = @_;
182 $file =~ s!^.*/!!; # remove leading directories
183 $file =~ s!\.[^\.]*$!!; # remove encoding extension
184 $file =~ s!^\.?[^\.]*!%s!; # replace name with placeholder
185 $file = undef if ($efile eq '-');
186 my $tmpfile = TempFile($file);
188 print STDERR " - decoding \"$efile\" as \"$tmpfile\"\n" if $debug;
190 # unlink($tmpfile); # should still be acceptable for "compose" output even if exists
191 return $tmpfile if (($efile ne '-' && ! -e $efile) || $action eq 'compose');
193 if ($encoding eq "gzip") {
195 $res = system "gzip -d >\Q$tmpfile\E";
197 $res = system "gzip -dc \Q$efile\E >\Q$tmpfile\E";
199 } elsif ($encoding eq "bzip") {
201 $res = system "bzip -d >\Q$tmpfile\E";
203 $res = system "bzip -dc <\Q$efile\E >\Q$tmpfile\E";
205 } elsif ($encoding eq "bzip2") {
207 $res = system "bzip2 -d >\Q$tmpfile\E";
209 $res = system "bzip2 -dc <\Q$efile\E >\Q$tmpfile\E";
211 } elsif ($encoding eq "compress") {
213 $res = system "uncompress >\Q$tmpfile\E";
215 $res = system "uncompress <\Q$efile\E >\Q$tmpfile\E";
218 die "Fatal: unknown encoding \"$encoding\" at";
221 $res = int($res/256);
223 print STDERR "Error: could not decode \"$efile\" -- $!\n";
224 $retcode = 2 if ($retcode < 2);
229 # chmod 0600,$tmpfile; # done already by TempFile
236 my($dfile,$efile,$encoding) = @_;
239 print STDERR " - encoding \"$dfile\" as \"$efile\"\n";
241 if ($encoding eq "gzip") {
243 $res = system "gzip -c \Q$dfile\E";
245 $res = system "gzip -c \Q$dfile\E >\Q$efile\E";
247 } elsif ($encoding eq "compress") {
249 $res = system "compress <\Q$dfile\E";
251 $res = system "compress <\Q$dfile\E >\Q$efile\E";
254 die "Fatal: unknown encoding \"$encoding\" at";
257 $res = int($res/256);
259 print STDERR "Error: could not encode \"$efile\" (left as \"$dfile\")\n";
260 $retcode = 2 if ($retcode < 2);
269 sub ExtensionMimetype {
273 unless ($donemimetypes) {
274 ReadMimetypes($usrmimetyp);
275 ReadMimetypes($locmimetyp);
276 ReadMimetypes($shrmimetyp);
277 ReadMimetypes($etcmimetyp);
281 $typ = $mimetypes{lc($ext)};
283 print STDERR " - extension \"$ext\" maps to mime-type \"$typ\"\n" if $debug;
289 sub PatternMimetype {
293 while (($key,$val) = each %patterntypes) {
294 if ($file =~ m!$key!i) {
295 print STDERR " - file \"$file\" maps to mime-type \"$val\"\n" if $debug;
300 print STDERR " - file \"$file\" does not conform to any known pattern\n" if $debug;
308 my($ext) = ($file =~ m!\.([^/\.]+)$!);
312 $type = ExtensionMimetype($ext) if $ext;
313 $type = PatternMimetype($file) unless $type;
322 print STDERR " - parsing parameter \"$_\"\n" if $debug;
323 if (m!^(-h|--help)$!) {
326 } elsif (m!^--(.*?)=(.*)$!) {
327 print STDERR "Warning: definition of \"$1=$2\" overrides value \"${$1}\"\n" if ($ {$1} && $ {$1} != $2);
329 } elsif (m!^--(.*?)$!) {
330 print STDERR "Warning: definition of \"$1=$2\" overrides value \"${$1}\"\n" if ($ {$1} && $ {$1} != 1);
332 } elsif (m!^[^/:]+/[^/:]+:[^/:]+:!) {
334 } elsif (m!^([^/:]+/[^/:]+):(.*)! && ! -e $_) {
338 my $code = EncodingForFile($file);
339 push @files,"${type}:${code}:${file}";
340 print STDERR " - file \"$file\" does not exist -- assuming mime-type specification of \"${type}\"\n" if $debug;
343 my $code = EncodingForFile($file);
347 $efile =~ s/\.[^\.]+$//;
348 $type = FileMimetype($efile);
350 $type = FileMimetype($file);
353 push @files,"${type}:${code}:${file}";
355 print STDERR "Warning: unknown mime-type for \"$file\" -- using \"$defmimetyp\"\n";
356 push @files,"${defmimetyp}:${code}:${file}";
362 if ($0 =~ m!(^|/)view$!) { $action="view"; }
363 elsif ($0 =~ m!(^|/)see$!) { $action="view"; }
364 elsif ($0 =~ m!(^|/)cat$!) { $action="cat"; }
365 elsif ($0 =~ m!(^|/)edit$!) { $action="edit"; }
366 elsif ($0 =~ m!(^|/)change$!) { $action="edit"; }
367 elsif ($0 =~ m!(^|/)compose$!) { $action="compose";}
368 elsif ($0 =~ m!(^|/)print$!) { $action="print"; }
369 elsif ($0 =~ m!(^|/)create$!) { $action="compose";}
370 else { $action="view"; }
374 $mailcaps = $ENV{MAILCAPS};
375 $mailcaps = "$ENV{HOME}/.mailcap:/etc/mailcap:/usr/local/etc/mailcap:/usr/share/etc/mailcap:/usr/etc/mailcap" unless $mailcaps;
376 foreach (split(/:/,$mailcaps)) {
381 my($type,$code,$file) = m/^(.*?):(.*?):(.*)$/;
382 print STDERR "Processing file \"$file\" of type \"$type\" (encoding=",$code?$code:"none",")...\n" if $debug;
385 if ($action eq 'compose' || $action eq 'edit') {
388 print STDERR "Error: no write permission for file \"$file\"\n";
389 $retcode = 2 if ($retcode < 2);
393 if (open(TEST,">$file")) {
397 print STDERR "Error: no write permission for file \"$file\"\n";
398 $retcode = 2 if ($retcode < 2);
404 print STDERR "Error: no such file \"$file\"\n";
405 $retcode = 2 if ($retcode < 2);
409 print STDERR "Error: no read permission for file \"$file\"\n";
410 $retcode = 2 if ($retcode < 2);
416 my(@matches,$entry,$res,$efile);
419 $file = DecodeFile($efile,$code,$action);
423 foreach $entry (@mailcap) {
424 $entry =~ m/^(.*?)\s*;/;
425 $_ = "\Q$1\E"; s/\\\*/\.\*/g;
426 push @matches,$entry if ($type =~ m!^$_$!i);
428 @matches = grep(/\Q$action\E=/,@matches) unless ($action eq "view" || $action eq "cat");
432 foreach $match (@matches) {
434 print STDERR " - checking mailcap entry \"$match\"\n" if $debug;
435 if ($action eq "view" || $action eq "cat") {
436 ($comm) = ($match =~ m/^.*?;\s*(.*?)\s*($|;)/);
438 ($comm) = ($match =~ m/\Q$action\E=(.*?)\s*($|;)/);
440 next if (!$comm || $comm =~ m!(^|/)false$!i);
441 print STDERR " - program to execute: $comm\n" if $debug;
443 if ($action eq 'cat' && $match !~ m/;\s*copiousoutput\s*($|;)/) {
444 print STDERR " - \"copiousoutput\" is required for \"cat\" action\n" if $debug;
449 my($tmpfile,$tmplink);
450 if ($action ne 'print' && $match =~ m/;\s*needsterminal\s*($|;)/ && ! -t STDOUT) {
452 $comm = "$xtermprgrm -T '$file ($type)' -e $0 --action=$action '${type}:%s'";
454 print STDERR " - no terminal available for rule (needsterminal)\n" if $debug;
458 } elsif ($action eq 'view' && $pager && $match =~ m/;\s*copiousoutput\s*($|;)/ && $type ne 'text/plain') {
459 $comm .= " | $0 --action=$action text/plain:-";
462 if ($match =~ m/;\s*test=(.*?)\s*($|;)/) {
464 print STDERR " - running test: $1 " if $debug;
465 $test = system "$1 >/dev/null 2>&1";
467 print STDERR " (result=$test=",($test!=0?"false":"true"),")\n" if $debug;
475 if ($comm =~ m/[^%]%s/) {
476 if ($file =~ m![^ a-z0-9,.:/@%^+=_-]!i) {
477 $match =~ m/nametemplate=(.*?)\s*($|;)/;
481 $tmplink = TempFile($prefix);
483 if ($file =~ m!^/!) {
484 $linked = symlink($file,$tmplink);
486 my $pwd = `/bin/pwd`;
488 $linked = symlink("$pwd/$file",$tmplink);
491 print STDERR " - filename contains shell meta-characters; aliased to '$tmplink'\n" if $debug;
492 $comm =~ s/([^%])%s/$1$tmplink/g;
494 $comm =~ s/([^%])%s/$1"$file"/g;
497 if ($comm =~ m/\|/) {
498 $comm =~ s/\|/<\Q$file\E \|/;
500 $comm .= " <\Q$file\E";
502 if ($action eq 'edit' || $action eq 'compose') {
503 $comm .= " >\Q$file\E";
507 if ($comm =~ m/[^%]%s/) {
508 $tmpfile = SaveStdin($match);
509 $comm =~ s/([^%])%s/$1$tmpfile/g;
511 # no name means same as "-"... read from stdin
515 $comm =~ s!([^%])%t!$1$type!g;
516 $comm =~ s!([^%])%F!$1!g;
517 $comm =~ s!%{(.*?)}!$_="'$ENV{$1}'";s/\`//g;s/\'\'//g;$_!ge;
518 $comm =~ s!\\(.)!$1!g;
519 $comm =~ s!\'\'!\'!g;
520 $comm =~ s!$quotedsemi!;!go;
521 $comm =~ s!$quotedprct!%!go;
523 print STDERR " - executing: $comm\n" if $debug;
529 $res = int($res/256);
532 print STDERR "Warning: program returned non-zero exit code \#$res\n";
536 unlink $tmpfile if $tmpfile;
537 unlink $tmplink if $tmplink;
543 print STDERR "Error: no \"$action\" rule for type \"$type\" passed its test case\n";
544 print STDERR " (for more information, add \"--debug=1\" on the command line)\n";
545 $retcode = 3 if ($retcode < 3);
547 print STDERR "Error: no \"$action\" mailcap rules found for type \"$type\"\n";
548 $retcode = 3 if ($retcode < 3);
550 unlink $file if $code;
551 $retcode = 1 unless $retcode;
556 if ($action eq 'edit' || $action eq 'compose') {
557 my $file = EncodeFile($file,$efile,$code);
558 unlink $file if $file;