]> git.madduck.net Git - etc/mutt.git/blob - .mutt/bgrun

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

bgrun: fixup cleaning up
[etc/mutt.git] / .mutt / bgrun
1 #!/bin/sh
2
3 SELF="${0##*/}"
4
5 if [ -z "$TMPDIR" ]; then
6   TMPDIR=/tmp
7 elif [ -d "${TMPDIR}/volatile" ]; then
8   TMPDIR="${TMPDIR}/volatile"
9 fi
10 export TMPDIR
11 TMPDIR=$(mktemp -dp "$TMPDIR" mutt.XXXXXXXXXX)
12
13 cleanup() {
14   cd /
15   echo Cleaning up $TMPDIR… >> $TMPDIR/output.stderr
16   notify_output
17   rm -rf "$TMPDIR"
18   trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
19 }
20 trap cleanup 1 2 3 4 5 6 7 8 10 11 12 13 14 15
21
22 cd "$TMPDIR"
23
24 notify() {
25   if [ -x "$(command -v awesome-client)" ]; then
26     awesome-client <<-_eof
27         local naughty = require("naughty")
28         naughty.notify({ preset = naughty.config.presets.low,
29                 title  = "$3",
30                 text   = [[stdout:
31                 $(sed -e 's,\",\\\",g' "$1")
32                 stderr:
33                 $(sed -e 's,\",\\\",g' "$2")]]
34                 })
35         _eof
36   fi
37 }
38
39 notify_output() {
40   if [ -s "$TMPDIR/output.stdout" ] || [ -s "$TMPDIR/output.stderr" ]; then
41     notify $TMPDIR/output.stdout $TMPDIR/output.stderr \
42       "Output from mutt/$SELF on $BASENAME"
43   fi
44 }
45
46 guess_extension() {
47   python -c "import mimetypes; print(mimetypes.guess_extension('$1'))"
48 }
49
50 launch_viewer() {
51   run-mailcap --action=view "$1":"$2" > output.stdout 2> output.stderr
52 }
53
54 MIMETYPE="$1"; shift
55
56 get_file() {
57   local t
58   if [ -z "$1" ]; then
59     t=$(tempfile -d . -p mutt -s $(guess_extension "$MIMETYPE"))
60     cat > "$t"
61     echo "$t"
62   else
63     t="$(echo ${1##*/} | sed -re 's![^[:alnum:],.@%^+=_-]!_!gi')"
64     cp "$1" "$t"
65     echo "$t"
66   fi
67 }
68
69 case "$SELF" in
70   (bgrun)
71     # make a copy of the file, then launch a shell process in the background
72     # to divert to run-mailcap, after which the temporary directory gets
73     # cleaned up.
74     FILE="$(get_file "${1:-}")"
75     (
76       launch_viewer "$MIMETYPE" "$FILE"
77       sleep 1
78       cleanup
79     ) &
80     trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
81     ;;
82   (bgrun-fifo)
83     # hack to stay around until the viewer has read the file: make a fifo and
84     # wait for the cat process to finish writing to it, which means that it
85     # must have been consumed on the other end.
86     FILE="$(get_file "${1:-}")"
87     FIFO="${FILE%/*}/fifo-${FILE##*/}"
88     mkfifo "$FIFO"
89     cat "$FILE" > "$FIFO" &
90     # For some reason, we do have to write a tempfile and cannot seem to
91     # redirect stdin directly to the fifo, i.e. this does not work instead of
92     # the previous three lines:
93     ## cat > "$FIFO" &
94     launch_viewer "$MIMETYPE" "${FIFO}"
95     wait
96     cleanup
97     ;;
98   (bgrun-delay)
99     # hack to stay around for a fixed period of time after the viewer process
100     # returns control to the caller, so that we can clean up. This is for
101     # cases when the FIFO method doesn't work, because e.g. Firefox randomly
102     # chooses it needs to read HTML files twice.
103     FILE="$(get_file "${1:-}")"
104     (launch_viewer "$MIMETYPE" "${FILE}") &
105     (
106       sleep 1m
107       echo Cleaning up $TMPDIR… > $TMPDIR/output.stderr
108       cleanup
109     ) &
110     trap - 1 2 3 4 5 6 7 8 10 11 12 13 14 15
111     ;;
112 esac