tools/textviewer.sh
[claws.git] / tools / textviewer.sh
1 #!/bin/bash
2
3 # textviewer.sh
4 # Copyright 2003 Luke Plant <L.Plant.98@cantab.net>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
20
21 ##############################################################################
22 #
23 # This script is a text viewer designed to be used with sylpheed-claws actions
24 # Set up an action with the command line:  textviewer.sh %p |
25 #
26 # The script will try to detect file type automatically, and then
27 # invokes a relevant program to print the file in plain text to
28 # the standard output.
29 #
30 # From v 0.9.7claws7, sylpheed-claws sets the temporary file
31 # of a part to XXXXXX.mimetmp.[filename of attachment]
32 # This means we can use the extension of the filename for checking.
33 # Also use the program 'file' if that fails.
34 #
35 # To extend the script just follow the patterns that already exist, or
36 # contact the author if you have problems.
37
38 ##############################################################################
39 #
40 # Change Log
41 #
42 # 2004-01-05
43 #       - added matcher and action for OpenOffice Writer documents
44 #         (requires ooo2txt)
45 #
46 # 2004-01-05
47 #       - changed page width parameter for antiword
48 #       - fixed matcher for 'diffs'
49 #       - added a matcher and action for bzip2 - bzip2 files
50 #         are decompressed and textviewer.sh run on the result
51 #       - similarly decompress gzip files and run textviewer.sh
52 #         on the result, insteading of doing 'gzip -l'
53 #
54 # 2003-12-30
55 #       added the script to sylpheed-claws/tools
56 #
57 # 2003-12-30
58 #       - use 'fold' after 'unrtf' to wrap to a nice width
59 #       - added basic file sanity checks
60 #
61 # 2003-12-29
62 #       Added recognition for "Zip " from 'file' output
63 #
64 # 2003-12-19
65 #       Initial public release
66 #
67 ###############################################################################
68
69 if [ $# -eq 0 ]
70 then
71         echo "No filename supplied." >&2 
72         echo "Usage: textviewer.sh FILE" >&2 
73         exit 1
74 fi
75
76 [ -f "$1" ] ||
77 {
78         echo "File \"$1\" does not exist or is not a regular file." >&2
79         exit 1
80 }
81
82 [ -r "$1" ] ||
83 {       
84         echo "Cannot read file \"$1\"." >&2
85         exit 1
86 }
87
88 FILETYPE=`file --brief "$1"` || 
89 {
90         echo "Please install the command 'file' to use this script." >&2
91         exit 1 
92 };
93
94 case "$1" in 
95         *.doc)  TYPE=MSWORD     ;;
96         *.zip)  TYPE=ZIP        ;;
97         *.tar.gz|*.tgz) TYPE=TARGZ ;;
98         *.tar.bz)       TYPE=TARBZ ;;
99         *.gz)   TYPE=GZIP       ;;
100         *.bz)   TYPE=BZIP       ;;
101         *.tar)  TYPE=TAR        ;;
102         *.diff) TYPE=TEXT       ;;
103         *.txt)  TYPE=TEXT       ;;
104         *.rtf)  TYPE=RTF        ;;
105         *.sxw)  TYPE=OOWRITER   ;;
106 esac
107
108 if [ "$TYPE" == "" ]    
109 then
110         case $FILETYPE in 
111                 "'diff'"*)      TYPE=TEXT       ;;
112                 gzip*)          TYPE=GZIP ;;
113                 bzip2*)         TYPE=BZIP ;;
114                 "Zip "*)        TYPE=ZIP  ;;
115                 ASCII*)         TYPE=TEXT       ;;
116                 "Rich Text Format"*)    
117                                 TYPE=RTF  ;;
118                 "smtp mail text"* | "RFC 822 mail text"*)       
119                                 TYPE=TEXT       ;;
120                 "Bourne shell script"* | "Bourne-Again shell script"*)
121                                 TYPE=TEXT       ;;
122         esac
123 fi
124
125 case $TYPE in
126         TARGZ)  echo -e "Tarball contents:\n"           ; 
127                 tar -tzvf "$1"                          ;;
128
129         TARBZ)  echo -e "Tarball contents:\n"           ; 
130                 tar -tjvf "$1"                          ;;
131
132         BZIP)   TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
133                 bunzip2 -c "$1" > "$TMP"  || exit 1;
134                 "$0" "$TMP";
135                 rm "$TMP"                               ;;
136
137         GZIP)   TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
138                 gunzip -c "$1" > "$TMP"  || exit 1;
139                 "$0" "$TMP";
140                 rm "$TMP"                               ;;
141
142         TAR)    echo -e "Tar archive contents:\n"       ; 
143                 tar -tvf "$1"                           ;;
144
145         ZIP)    unzip -l "$1"                           ;;
146
147         RTF)    which unrtf > /dev/null  2>&1 || 
148                 {
149                         echo "Program 'unrtf' for displaying RTF files not found" >&2
150                         exit 1
151                 };
152                 unrtf -t text "$1" 2>/dev/null | egrep  -v '^### ' | fold -s -w 72  ;;
153
154         TEXT)   cat "$1"                                ;;
155
156         MSWORD) which antiword  > /dev/null  2>&1 || 
157                 {
158                         echo "Program 'antiword' for displaying MS Word files not found" >&2
159                         exit 1 
160                 };
161                 antiword -w 72 "$1"                             ;;
162
163         OOWRITER) which ooo2txt > /dev/null 2>&1 ||
164                 {
165                         echo "Program 'ooo2txt' for converting OpenOffice Writer files not files not found" >&2
166                         exit 1
167                 };
168                 ooo2txt "$1"                                    ;;
169
170         *)      echo "Unsupported file type \"$FILETYPE\", cannot display.";;
171 esac