mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

mutt_bgrun (3872B)


      1 #!/bin/sh
      2 # @(#) mutt_bgrun $Revision: 1.4 $
      3 
      4 #   mutt_bgrun - run an attachment viewer from mutt in the background
      5 #   Copyright (C) 1999-2002 Gary A. Johnson
      6 #
      7 #   This program is free software; you can redistribute it and/or modify
      8 #   it under the terms of the GNU General Public License as published by
      9 #   the Free Software Foundation; either version 2 of the License, or
     10 #   (at your option) any later version.
     11 #
     12 #   This program is distributed in the hope that it will be useful,
     13 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 #   GNU General Public License for more details.
     16 #
     17 #   You should have received a copy of the GNU General Public License
     18 #   along with this program; if not, write to the Free Software
     19 #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     20 
     21 # SYNOPSIS
     22 #	mutt_bgrun viewer [viewer options] file
     23 #
     24 # DESCRIPTION
     25 #	Mutt invokes external attachment viewers by writing the
     26 #	attachment to a temporary file, executing the pipeline specified
     27 #	for that attachment type in the mailcap file, waiting for the
     28 #	pipeline to terminate, writing nulls over the temporary file,
     29 #	then deleting it.  This causes problems when using graphical
     30 #	viewers such as qvpview and acroread to view attachments. 
     31 #
     32 #	If qvpview, for example, is executed in the foreground, the mutt
     33 #	user interface is hung until qvpview exits, so the user can't do
     34 #	anything else with mutt until he or she finishes reading the
     35 #	attachment and exits qvpview.  This is especially annoying when
     36 #	a message contains several MS Office attachments--one would like
     37 #	to have them all open at once. 
     38 #
     39 #	If qvpview is executed in the background, it must be given
     40 #	enough time to completely read the file before returning control
     41 #	to mutt, since mutt will then obliterate the file.  Qvpview is
     42 #	so slow that this time can exceed 20 seconds, and the bound is
     43 #	unknown.  So this is again annoying. 
     44 #
     45 #	The solution provided here is to invoke the specified viewer
     46 #	from this script after first copying mutt's temporary file to
     47 #	another temporary file.  This script can then quickly return
     48 #	control to mutt while the viewer can take as much time as it
     49 #	needs to read and render the attachment. 
     50 #
     51 # EXAMPLE
     52 #	To use qvpview to view MS Office attachments from mutt, add the
     53 #	following lines to mutt's mailcap file.
     54 #
     55 #	application/msword;             mutt_bgrun qvpview %s
     56 #	application/vnd.ms-excel;       mutt_bgrun qvpview %s
     57 #	application/vnd.ms-powerpoint;  mutt_bgrun qvpview %s
     58 #
     59 # AUTHOR
     60 #	Gary A. Johnson
     61 #	<garyjohn@spk.agilent.com>
     62 #
     63 # ACKNOWLEDGEMENTS
     64 #	My thanks to the people who have commented on this script and
     65 #	offered solutions to shortcomings and bugs, especially Edmund
     66 #	GRIMLEY EVANS <edmundo@rano.org> and Andreas Somogyi
     67 #	<aso@somogyi.nu>.
     68 
     69 prog=${0##*/}
     70 
     71 # Check the arguments first.
     72 
     73 if [ "$#" -lt "2" ]
     74 then
     75     echo "usage: $prog viewer [viewer options] file" >&2
     76     exit 1
     77 fi
     78 
     79 # Separate the arguments.  Assume the first is the viewer, the last is
     80 # the file, and all in between are options to the viewer.
     81 
     82 viewer="$1"
     83 shift
     84 
     85 while [ "$#" -gt "1" ]
     86 do
     87     options="$options $1"
     88     shift
     89 done
     90 
     91 file=$1
     92 
     93 # Create a temporary directory for our copy of the temporary file.
     94 #
     95 # This is more secure than creating a temporary file in an existing
     96 # directory.
     97 
     98 tmpdir=~/temp/$LOGNAME$$
     99 umask 077
    100 mkdir "$tmpdir" || exit 1
    101 tmpfile="$tmpdir/${file##*/}"
    102 
    103 # Copy mutt's temporary file to our temporary directory so that we can
    104 # let mutt overwrite and delete it when we exit.
    105 
    106 cp "$file" "$tmpfile"
    107 
    108 # Run the viewer in the background and delete the temporary files when done. 
    109 # add some grace time to reload the file with a different encoding in fx
    110 
    111 (
    112     "$viewer" $options "$tmpfile"
    113     sleep 30
    114     rm -f "$tmpfile"
    115     rmdir "$tmpdir"
    116 ) &