/
mailcap_bg
executable file
·130 lines (111 loc) · 3.59 KB
/
mailcap_bg
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
#!/bin/bash
# $Id$
#
# Mutt mailcap helper -- this program copies the file to be viewed
# prior to starting the viewer in the background. This allows mutt to
# delete the temporary file without disturbing the viewer.
#
# Syntax: mailcap_bg filename prog [args]
# mailcap_test filename prog (determines if mailcap_bg can run)
#
# Copyright 2003, 2006 Aron Griffis <agriffis n01se.net>
# released under the GNU General Public License version 2
#
# Tweaked by Adam Spiers <mutt@adamspiers.org>
#
# Upstream version:
# http://agriffis.n01se.net/skel.hg/?file/tip/bin/mailcap_bg
PS4="+\${BASH_SOURCE/\$HOME/\~}@\${LINENO}(\${FUNCNAME[0]}): "
exec >>~/.log/mailcap_bg.log 2>&1
cat <<EOF
----------------------
`date`
pid $$, invoked as $0 $@
EOF
set -x
# Test mode
testing=false
if [[ $1 == -test ]]; then
testing=true
shift
fi
if [[ $1 == -test ]]; then
testing=true
shift
fi
# Setup global variables.
need_wait=no
fname="$1" ; shift
prog="$1" ; shift
declare -a args=("${@}")
# Test only if called as mailcap_test
if $testing || [[ $0 == *_test ]]; then
# Note regarding mailcap %s usage in test= section: This will only
# resolve to a value when there is a filename specified in the
# attachment, for example
#
# Content-Type: application/x-msdownload; name="pack276.exe"
#
# Otherwise it will be blank. That is the reason for the
# following rearrangement of variables. There is some further
# explanation and discussion in .mailcap
if [[ -z "$prog" ]]; then
prog=$fname
fname=
fi
# make sure the requested program exists
type -P "$prog" &>/dev/null || exit 1
# and we have a display
[[ -n "$DISPLAY" ]] || exit 1
# no other restrictions of which we're aware at this point
exit 0
fi
if [[ -z "$prog" ]]; then
echo "mailcap_bg: No program argument provided; aborting." >&2
alertme "mailcap_bg: No program argument provided; aborting."
exit 1
fi
# Copy the file so that mutt can do whatever it wants to the original.
# Using a temp dir instead of a temp file preserves the original
# extension. Note that fname should always be an absolute path.
#
# If mutt is providing a temporary file, it will zero out the file
# from the disk prior to deleting it. Therefore we can't use a hard
# link as it would be zero'd before the viewer gets a chance to access
# it. We can't mv either, since if mutt is providing a file attached
# to a draft mail, it's not a temporary file. Therefore copy is the
# only safe solution which will work in every scenario.
#template="${fname%/*}/mailcap_bg.XXXXXX"
# Since we're not hard linking, these can go in /tmp now
template="/tmp/mailcap_bg.XXXXXX"
tname=$(mktemp -d "$template") \
&& cp "${fname}" "${tname}/${fname##*/}" \
&& tname="${tname}/${fname##*/}"
if [[ $? != 0 ]]; then
# Can't background, unfortunately
need_wait=yes
tname=${fname}
fi
# replace @s if it exists in the viewer string
if [[ "${args[*]}" == *@s* ]]; then
args=("${args[@]//@s/$tname}")
# Alternative method follows; do them one at a time
#for ((i = 0; i < ${#args[@]}; i = i+1)); do
# args[i] = "${args[i]//%s/$tname}"
#done
else
args=("${args[@]}" "${tname}")
fi
# Run the viewer in the background
nohup "$prog" "${args[@]}" </dev/null &
viewer_pid=$!
echo "Viewer pid is $viewer_pid"
if [[ $tname != $fname ]]; then
nohup tmp-cleaner "$tname" </dev/null >&/dev/null &
cleaner_pid=$!
echo "Cleaner is $cleaner_pid"
fi
echo "Viewer $viewer_pid still running?"
ps -fp $viewer_pid
# If we exit non-zero, mutt will do its wait_key prompt which is annoying.
exit 0