aboutsummaryrefslogtreecommitdiffstats
path: root/_mutt/mutt_octet_view.sh
blob: a1e6c6f2af771031e113443e2fe5bc4be9a0d6e6 (plain)
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
#!/bin/sh
# @(#) mutt_octet_view $Revision: 1.1 $

#   mutt_octet_view - select octet-stream e-mail attachment viewer
#   Copyright (C) 1997,1998,1999,2000 David A Pearson
#   Copyright (C) 2000-2001 Gary A. Johnson
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Mutt_octet_view is derived from Dave Pearson's mutt.octet.filter
# script (http://www.hagbard.demon.co.uk/archives/mutt.octet.filter).
# Mutt.octet.filter was designed to convert octet-stream e-mail
# attachments to text for use with mutt's auto_view feature.
# Mutt_octet_view extends this capability to use different viewers from
# mutt's attachment menu, including graphical viewers if X is available.
#
# To use mutt_octet_view, put the following lines in your mailcap file:
#
#   application/octet-stream; mutt_octet_view -x %s; test=RunningX
#   application/octet-stream; mutt_octet_view -v %s
#   application/octet-stream; mutt_octet_view %s; copiousoutput
#
# and the following line in your muttrc file:
#
#   auto_view application/octet-stream 
#
# Mutt_octet_view is not a viewer itself; it only selects external
# viewers based on the attachments' file name suffixes (extensions).
# In addition to any viewers, you will need a copy of the author's
# mutt_bgrun script in order to run X viewers in the background.
#
# Please direct any comments to:
#
#   Gary Johnson <garyjohn@spk.agilent.com>
#


ShowTAR()
{
    tar tvvf "$1" 2> /dev/null
}

ShowTGZ()
{
    tar tzvvf "$1" 2> /dev/null
}

ShowTBZ()
{
    bzip2 -dc "$1" | tar -tvv -f- 2> /dev/null
}

ShowGZIP()
{
    gzip -dc "$1" 2> /dev/null
}

ShowZIP()
{
    unzip -l "$1" 2> /dev/null
}

ShowARJ()
{
    unarj l "$1" 2> /dev/null
}

ShowVCard()
{
    cat "$1" | mutt.vcard.filter
}

ShowTIF()
{
    tiffinfo "$1"
}

ShowObject()
{
    nm "$1"
}

ShowHTML()
{
    w3m -dump "$1"
}

ShowPDF()
{
    pdftotext "$1" -
}

Show()
{
    case "$Mode" in
    SHOW)
	if [ "$2" ]
	then
	    $2 "$1"
	elif [ "$3" -o "$4" ]
	then
	    echo "[-- $Prog: file type is unsupported for autoview (use 'v' to view this part) --]"
	else
	    echo "[-- $Prog: file type is unsupported --]"
	fi
	;;
    VIEW)
	if [ "$3" ]
	then
	    $3 "$1"
	elif [ "$2" ]
	then
	    $2 "$1"
	else
	    echo "$Prog: file type is unsupported" >&2
	    exit 1
	fi
	;;
    XVIEW)
	if [ "$4" ]
	then
	    mutt_bgrun $4 "$1" || $4 "$1"
	elif [ "$3" ]
	then
	    $3 "$1"
	elif [ "$2" ]
	then
	    $2 "$1"
	else
	    echo "$Prog: file type is unsupported" >&2
	    exit 1
	fi
	;;
    *)	echo "$Prog: internal error: invalid Mode $Mode" >&2
	exit 2;;
    esac
}

# Initialize variables

Prog=$(basename "$0")
Mode=SHOW

# Check option arguments

while [ $# -gt 0 ]
do
    case "$1" in
    -t)	Mode=SHOW; shift;;
    -v)	Mode=VIEW; shift;;
    -x)	Mode=XVIEW; shift;;
    -*)	echo "$Prog: invalid option $1" >&2; exit 2;;
    *)	break;;
    esac
done

# Check file argument

if [ $# -ne 1 ]
then
    echo "usage: $Prog [-t|-v|-x] file"
    exit 2
fi

File=$1

# Process file

# The behavior of mutt_octet_view is governed primarily by the 'Show'
# function, the option argument, and the table below.  'Show' searches
# its arguments for a command that it can use to display File.  It
# begins in the table column specified by the option argument -t (the
# default), -v or -x, and searches to the left until it finds a command
# (non-empty string), which it executes.  Commands in the right-most
# column are executed in the background and are left to handle their own
# output.  Commands  in the other two columns are executed in the
# foreground and send their output to stdout.
#
# Here are some examples.
#
#  Autoview     Text View     X View
#    (from        (from        (from
#    index     attachment   attachment
#    menu,        menu,        menu,
#  in-line)    foreground)  background)
#    (-t)         (-v)         (-x)
# -----------  -----------  -----------
# Func1        ""           ""           Func1 will be used to display
#                                        file in all cases.  This is
#                                        useful when all that is
#                                        required to display the file is
#                                        a simple text transformation.
#
# ""           Func2        ""           The file will not be displayed
#                                        in the pager output from the
#                                        index menu.  Func2 will be used
#                                        to display the file from the
#                                        attachment menu.  This is
#                                        useful when the file is to be
#                                        displayed as text, but is not
#                                        normally desired to view it
#                                        along with the rest of the
#                                        message.
#
# ""           ""           Func3        The file contains graphical
#                                        material that cannot be viewed
#                                        as text.  Func3 will be used to
#                                        display the file only from the
#                                        attachment menu and only when X is
#                                        available.  Func3  will be run in
#                                        the background so that the user
#                                        can continue to use the mailer
#                                        while the file is displayed.
#
# ""           Func2        Func3        The file contents are not
#                                        displayed in-line along with
#                                        the rest of the message because
#                                        the file is typically large.
#                                        Func2 has only text output and
#                                        is used when only a terminal is
#                                        available for output.  Func3
#                                        produces a nicer display when X
#                                        is available.

case "$File" in
		#                Autoview     Text View      X View
		#                  (from        (from         (from
		#                  index     attachment    attachment
		#                  menu,        menu,         menu,
		#                in-line)    foreground)   background)
		#                  (-t)         (-v)          (-x)
	        #               -----------  -----------  -------------
*.arj  | *.ARJ  ) Show "$File"  ""           ShowARJ      ""             ;;
*.doc  | *.DOC  ) Show "$File"  word2text    word2text    qvpview        ;;
*.gif  | *.GIF  ) Show "$File"  ""           ""           xv             ;;
*.htm  | *.HTM  ) Show "$File"  ShowHTML     w3m          mutt_netscape  ;;
*.html | *.HTML ) Show "$File"  ShowHTML     w3m          mutt_netscape  ;;
*.jpeg | *.JPEG ) Show "$File"  ""           ""           xv             ;;
*.jpg  | *.JPG  ) Show "$File"  ""           ""           xv             ;;
*.log  | *.LOG  ) Show "$File"  cat          less         ""             ;;
*.o             ) Show "$File"  ""           ShowObject   ""             ;;
*.pdf  | *.PDF  ) Show "$File"  ""           ShowPDF      acroread       ;;
*.ppt  | *.PPT  ) Show "$File"  ""           ""           qvpview        ;;
*.ps   | *.PS   ) Show "$File"  ""           ""           ghostview      ;;
*.rtf  | *.RTF  ) Show "$File"  ""           rtf2text     qvpview        ;;
*.tar           ) Show "$File"  ""           ShowTAR      ""             ;;
*.tar.bz2       ) Show "$File"  ""           ShowTBZ      ""             ;;
*.tar.gz        ) Show "$File"  ""           ShowTGZ      ""             ;;
*.tar.Z         ) Show "$File"  ""           ShowTGZ      ""             ;;
*.tar.z         ) Show "$File"  ""           ShowTGZ      ""             ;;
*.tbz2          ) Show "$File"  ""           ShowTBZ      ""             ;;
*.tgz           ) Show "$File"  ""           ShowTGZ      ""             ;;
*.tif  | *.TIF  ) Show "$File"  ""           ShowTIF      xv             ;;
*.txt  | *.TXT  ) Show "$File"  cat          less         ""             ;;
*.url  | *.URL  ) Show "$File"  cat          w3m          ""             ;;
*.vcf           ) Show "$File"  ""           ShowVCard    ""             ;;
*.xls  | *.XLS  ) Show "$File"  excel2text   excel2text   qvpview        ;;
*.Z             ) Show "$File"  ""           ShowGZIP     ""             ;;
*.z             ) Show "$File"  ""           ShowGZIP     ""             ;;
*.zip  | *.ZIP  ) Show "$File"  ""           ShowZIP      ""             ;;
*.gz            ) Show "$File"  ""           ShowGZIP     ""             ;;
		# The *.gz entry must follow any other entries ending in
		# .gz.
*               ) echo "$Prog: file type is unsupported" >&2; exit 1     ;;

esac