Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / tools / textviewer.sh
blob38c8894310ffa9e9d5eda99aa61698a6302967d8
1 #!/usr/bin/env bash
3 # textviewer.sh
4 # Copyright 2003 Luke Plant <L.Plant.98@cantab.net>
5 # and Johann Koenig <johann@mental-graffiti.com>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
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.
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
20 # 02111-1307, USA.
22 ##############################################################################
24 # This script is a text viewer designed to be used with claws-mail actions
25 # Set up an action with the command line: textviewer.sh %p |
27 # The script will try to detect file type automatically, and then
28 # invokes a relevant program to print the file in plain text to
29 # the standard output.
31 # From v 0.9.7claws7, claws-mail sets the temporary file
32 # of a part to XXXXXX.mimetmp.[filename of attachment]
33 # This means we can use the extension of the filename for checking.
34 # Also use the program 'file' if that fails.
36 # To extend the script just follow the patterns that already exist, or
37 # contact the author if you have problems.
39 ##############################################################################
41 # Change Log
43 # 2003-03-25
44 # - make extension matching case insensitive
46 # 2003-03-23
47 # - Support for MS Excel (xlhtml) and Powerpoint (ppthtml)
49 # 2004-03-09
50 # - Support for HTML (html2text)
52 # 2004-02-13
53 # - added support for perl and shell scripts, and recognize that
54 # 'file' will always return 'text' somewhere in its output for
55 # files that, well, contain text
57 # 2004-01-25
58 # - added brief messages describing whats going on
60 # 2004-01-23
61 # - added support for 'pdftotext,' from xpdf-utils debian package
63 # 2004-01-05
64 # - added matcher and action for OpenOffice Writer documents
65 # (requires ooo2txt)
67 # 2004-01-05
68 # - changed page width parameter for antiword
69 # - fixed matcher for 'diffs'
70 # - added a matcher and action for bzip2 - bzip2 files
71 # are decompressed and textviewer.sh run on the result
72 # - similarly decompress gzip files and run textviewer.sh
73 # on the result, insteading of doing 'gzip -l'
75 # 2003-12-30
76 # added the script to claws-mail/tools
78 # 2003-12-30
79 # - use 'fold' after 'unrtf' to wrap to a nice width
80 # - added basic file sanity checks
82 # 2003-12-29
83 # Added recognition for "Zip " from 'file' output
85 # 2003-12-19
86 # Initial public release
88 ###############################################################################
90 if [ $# -eq 0 ]
91 then
92 echo "No filename supplied." >&2
93 echo "Usage: textviewer.sh FILE" >&2
94 exit 1
97 [ -f "$1" ] ||
99 echo "File \"$1\" does not exist or is not a regular file." >&2
100 exit 1
103 [ -r "$1" ] ||
105 echo "Cannot read file \"$1\"." >&2
106 exit 1
109 FILETYPE=`file --brief "$1"` ||
111 echo "Please install the command 'file' to use this script." >&2
112 exit 1
115 FNAME=`echo "$1" | tr [A-Z] [a-z]`
116 case "$FNAME" in
117 *.doc) TYPE=MSWORD ;;
118 *.ppt) TYPE=POWERPOINT ;;
119 *.zip) TYPE=ZIP ;;
120 *.tar.gz|*.tgz) TYPE=TARGZ ;;
121 *.tar.bz2|*.tar.bz) TYPE=TARBZ ;;
122 *.gz) TYPE=GZIP ;;
123 *.bz2|*.bz) TYPE=BZIP ;;
124 *.tar) TYPE=TAR ;;
125 *.diff) TYPE=TEXT ;;
126 *.txt) TYPE=TEXT ;;
127 *.rtf) TYPE=RTF ;;
128 *.sxw) TYPE=OOWRITER ;;
129 *.pdf) TYPE=PDF ;;
130 *.sh) TYPE=TEXT ;;
131 *.pl) TYPE=TEXT ;;
132 *.html|*.htm) TYPE=HTML ;;
133 *.xls) TYPE=EXCEL ;;
134 esac
136 if [ "$TYPE" = "" ]
137 then
138 case $FILETYPE in
139 *"HTML"*) TYPE=HTML ;;
140 *"text"*) TYPE=TEXT ;;
141 gzip*) TYPE=GZIP ;;
142 bzip2*) TYPE=BZIP ;;
143 "POSIX tar archive"*) TYPE=TAR ;;
144 "Zip "*) TYPE=ZIP ;;
145 "Rich Text Format"*)
146 TYPE=RTF ;;
147 esac
150 case $TYPE in
151 TARGZ) echo -e "Gzip'd tarball contents:\n" ;
152 tar -tzvf "$1" ;;
154 TARBZ) echo -e "Bzip'd tarball contents:\n" ;
155 tar -tjvf "$1" ;;
157 BZIP) TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
158 bunzip2 -c "$1" > "$TMP" || exit 1;
159 echo -e "Re-running \"$0\" on bunzip'd contents of \"$1\":\n";
160 "$0" "$TMP";
161 rm "$TMP" ;;
163 GZIP) TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
164 gunzip -c "$1" > "$TMP" || exit 1;
165 echo "Re-running \"$0\" on gunzip'd contents of \"$1\":\n";
166 "$0" "$TMP";
167 rm "$TMP" ;;
169 TAR) echo -e "Tar archive contents:\n" ;
170 tar -tvf "$1" ;;
172 ZIP) echo -e "Zip file contents:\n" ;
173 unzip -l "$1" ;;
175 RTF) which unrtf > /dev/null 2>&1 ||
177 echo "Program 'unrtf' for displaying RTF files not found" >&2
178 exit 1
180 echo -e "Displaying \"$1\" using \"unrtf\":\n";
181 unrtf -t text "$1" 2>/dev/null | egrep -v '^### ' | fold -s -w 72 ;;
183 TEXT) cat "$1" ;;
185 MSWORD) which antiword > /dev/null 2>&1 ||
187 echo "Program 'antiword' for displaying MS Word files not found" >&2
188 exit 1
190 echo -e "Displaying \"$1\" using \"antiword\":\n";
191 antiword -w 72 "$1" ;;
193 POWERPOINT) which ppthtml > /dev/null 2>&1 ||
195 echo "Program 'ppthtml' for displaying Powerpoint files not found" >&2
196 exit 1
198 which html2text > /dev/null 2>&1 ||
200 echo "Program 'html2text' for displaying Powerpoint files not found" >&2
201 exit 1
203 echo -e "Displaying \"$1\" using \"ppthtml\" and \"html2text\":\n";
204 ppthtml "$1" | html2text ;;
206 EXCEL) which xlhtml > /dev/null 2>&1 ||
208 echo "Program 'xlhtml' for displaying Excel files not found" >&2
209 exit 1
211 which html2text > /dev/null 2>&1 ||
213 echo "Program 'html2text' for displaying Excel files not found" >&2
214 exit 1
216 echo -e "Displaying \"$1\" using \"xlhtml\" and \"html2text\":\n";
217 xlhtml "$1" | html2text ;;
219 OOWRITER) which ooo2txt > /dev/null 2>&1 ||
221 echo "Program 'ooo2txt' for converting OpenOffice Writer files not files not found" >&2
222 exit 1
224 echo -e "Displaying \"$1\" using \"ooo2txt\":\n";
225 ooo2txt "$1" ;;
227 PDF) which pdftotext > /dev/null 2>&1 ||
229 echo "Program 'pdftotext' for converting Adobe Portable Document Format to text not found" >&2
230 exit 1
232 echo -e "Displaying \"$1\" using \"pdftotext\":\n";
233 pdftotext "$1" - ;;
235 HTML) which html2text > /dev/null 2>&1 ||
237 echo "Program 'html2text' for converting HTML files not found" >&2
238 exit 1
240 html2text -nobs "$1" ;;
242 *) echo "Unsupported file type \"$FILETYPE\", cannot display.";;
243 esac