This commit was manufactured by cvs2svn to create branch 'gtk2'.
[claws.git] / tools / textviewer.sh
1 #!/bin/bash
2
3 # textviewer.sh
4 # Copyright 2003 Luke Plant <L.Plant.98@cantab.net>
5 # and Johann Koenig <johann@mental-graffiti.com>
6
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 2
10 # of the License, or (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
20 # 02111-1307, USA.
21
22 ##############################################################################
23 #
24 # This script is a text viewer designed to be used with sylpheed-claws actions
25 # Set up an action with the command line:  textviewer.sh %p |
26 #
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.
30 #
31 # From v 0.9.7claws7, sylpheed-claws 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.
35 #
36 # To extend the script just follow the patterns that already exist, or
37 # contact the author if you have problems.
38
39 ##############################################################################
40 #
41 # Change Log
42 #
43 # 2004-01-25
44 #       - added brief messages describing whats going on
45 #
46 # 2004-01-23
47 #       - added support for 'pdftotext,' from xpdf-utils debian package
48 #
49 # 2004-01-05
50 #       - added matcher and action for OpenOffice Writer documents
51 #         (requires ooo2txt)
52 #
53 # 2004-01-05
54 #       - changed page width parameter for antiword
55 #       - fixed matcher for 'diffs'
56 #       - added a matcher and action for bzip2 - bzip2 files
57 #         are decompressed and textviewer.sh run on the result
58 #       - similarly decompress gzip files and run textviewer.sh
59 #         on the result, insteading of doing 'gzip -l'
60 #
61 # 2003-12-30
62 #       added the script to sylpheed-claws/tools
63 #
64 # 2003-12-30
65 #       - use 'fold' after 'unrtf' to wrap to a nice width
66 #       - added basic file sanity checks
67 #
68 # 2003-12-29
69 #       Added recognition for "Zip " from 'file' output
70 #
71 # 2003-12-19
72 #       Initial public release
73 #
74 ###############################################################################
75
76 if [ $# -eq 0 ]
77 then
78         echo "No filename supplied." >&2 
79         echo "Usage: textviewer.sh FILE" >&2 
80         exit 1
81 fi
82
83 [ -f "$1" ] ||
84 {
85         echo "File \"$1\" does not exist or is not a regular file." >&2
86         exit 1
87 }
88
89 [ -r "$1" ] ||
90 {       
91         echo "Cannot read file \"$1\"." >&2
92         exit 1
93 }
94
95 FILETYPE=`file --brief "$1"` || 
96 {
97         echo "Please install the command 'file' to use this script." >&2
98         exit 1 
99 };
100
101 case "$1" in 
102         *.doc)  TYPE=MSWORD     ;;
103         *.zip)  TYPE=ZIP        ;;
104         *.tar.gz|*.tgz) TYPE=TARGZ ;;
105         *.tar.bz2|*.tar.bz)     TYPE=TARBZ ;;
106         *.gz)   TYPE=GZIP       ;;
107         *.bz2|*.bz)     TYPE=BZIP       ;;
108         *.tar)  TYPE=TAR        ;;
109         *.diff) TYPE=TEXT       ;;
110         *.txt)  TYPE=TEXT       ;;
111         *.rtf)  TYPE=RTF        ;;
112         *.sxw)  TYPE=OOWRITER   ;;
113         *.pdf)  TYPE=PDF        ;;
114 esac
115
116 if [ "$TYPE" == "" ]    
117 then
118         case $FILETYPE in 
119                 "'diff'"*)      TYPE=TEXT       ;;
120                 gzip*)          TYPE=GZIP ;;
121                 bzip2*)         TYPE=BZIP ;;
122                 "POSIX tar archive"*)   TYPE=TAR        ;;
123                 "Zip "*)        TYPE=ZIP  ;;
124                 ASCII*)         TYPE=TEXT       ;;
125                 "Rich Text Format"*)    
126                                 TYPE=RTF  ;;
127                 "smtp mail text"* | "RFC 822 mail text"*)       
128                                 TYPE=TEXT       ;;
129                 "Bourne shell script"* | "Bourne-Again shell script"*)
130                                 TYPE=TEXT       ;;
131         esac
132 fi
133
134 case $TYPE in
135         TARGZ)  echo -e "Gzip'd tarball contents:\n"    ; 
136                 tar -tzvf "$1"                          ;;
137
138         TARBZ)  echo -e "Bzip'd tarball contents:\n"    ; 
139                 tar -tjvf "$1"                          ;;
140
141         BZIP)   TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
142                 bunzip2 -c "$1" > "$TMP"  || exit 1;
143                 echo -e "Re-running \"$0\" on bunzip'd contents of \"$1\":\n";
144                 "$0" "$TMP";
145                 rm "$TMP"                                       ;;
146
147         GZIP)   TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
148                 gunzip -c "$1" > "$TMP"  || exit 1;
149                 echo "Re-running \"$0\" on gunzip'd contents of \"$1\":\n";
150                 "$0" "$TMP";
151                 rm "$TMP"                                       ;;
152
153         TAR)    echo -e "Tar archive contents:\n"       ; 
154                 tar -tvf "$1"                           ;;
155
156         ZIP)    echo -e "Zip file contents:\n"          ;
157                 unzip -l "$1"                           ;;
158
159         RTF)    which unrtf > /dev/null  2>&1 || 
160                 {
161                         echo "Program 'unrtf' for displaying RTF files not found" >&2
162                         exit 1
163                 };
164                 echo -e "Displaying \"$1\" using \"unrtf\":\n";
165                 unrtf -t text "$1" 2>/dev/null | egrep  -v '^### ' | fold -s -w 72  ;;
166
167         TEXT)   cat "$1"                                ;;
168
169         MSWORD) which antiword  > /dev/null  2>&1 || 
170                 {
171                         echo "Program 'antiword' for displaying MS Word files not found" >&2
172                         exit 1 
173                 };
174                 echo -e "Displaying \"$1\" using \"antiword\":\n";
175                 antiword -w 72 "$1"                             ;;
176
177         OOWRITER) which ooo2txt > /dev/null 2>&1 ||
178                 {
179                         echo "Program 'ooo2txt' for converting OpenOffice Writer files not files not found" >&2
180                         exit 1
181                 };
182                 echo -e "Displaying \"$1\" using \"ooo2txt\":\n";
183                 ooo2txt "$1"                                    ;;
184
185         PDF) which pdftotext > /dev/null 2>&1 ||
186                 {
187                         echo "Program 'pdftotext' for converting Adobe Portable Document Format to text not found" >&2
188                         exit 1
189                 };
190                 echo -e "Displaying \"$1\" using \"pdftotext\":\n";
191                 pdftotext "$1"  -                               ;;
192
193         *)      echo "Unsupported file type \"$FILETYPE\", cannot display.";;
194 esac