85d3800bcc8b16ab3ee72c0c9a0ee9738c9bf0f5
[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 #       - changed page width parameter for antiword
44 #       - fixed matcher for 'diffs'
45 #       - added a matcher and action for bzip2 - bzip2 files
46 #         are decompressed and textviewer.sh run on the result
47 #       - similarly decompress gzip files and run textviewer.sh
48 #         on the result, insteading of doing 'gzip -l'
49 #
50 # 2003-12-30
51 #       added the script to sylpheed-claws/tools
52 #
53 # 2003-12-30
54 #       - use 'fold' after 'unrtf' to wrap to a nice width
55 #       - added basic file sanity checks
56 #
57 # 2003-12-29
58 #       Added recognition for "Zip " from 'file' output
59 #
60 # 2003-12-19
61 #       Initial public release
62 #
63 ###############################################################################
64
65 if [ $# -eq 0 ]
66 then
67         echo "No filename supplied." >&2 
68         echo "Usage: textviewer.sh FILE" >&2 
69         exit 1
70 fi
71
72 [ -f "$1" ] ||
73 {
74         echo "File \"$1\" does not exist or is not a regular file." >&2
75         exit 1
76 }
77
78 [ -r "$1" ] ||
79 {       
80         echo "Cannot read file \"$1\"." >&2
81         exit 1
82 }
83
84 FILETYPE=`file --brief "$1"` || 
85 {
86         echo "Please install the command 'file' to use this script." >&2
87         exit 1 
88 };
89
90 case "$1" in 
91         *.doc)  TYPE=MSWORD     ;;
92         *.zip)  TYPE=ZIP        ;;
93         *.tar.gz|*.tgz) TYPE=TARGZ ;;
94         *.tar.bz)       TYPE=TARBZ ;;
95         *.gz)   TYPE=GZIP       ;;
96         *.bz)   TYPE=BZIP       ;;
97         *.tar)  TYPE=TAR        ;;
98         *.diff) TYPE=TEXT       ;;
99         *.txt)  TYPE=TEXT       ;;
100         *.rtf)  TYPE=RTF        ;;
101 esac
102
103 if [ "$TYPE" == "" ]    
104 then
105         case $FILETYPE in 
106                 "'diff'"*)      TYPE=TEXT       ;;
107                 gzip*)          TYPE=GZIP ;;
108                 bzip2*)         TYPE=BZIP ;;
109                 "Zip "*)        TYPE=ZIP  ;;
110                 ASCII*)         TYPE=TEXT       ;;
111                 "Rich Text Format"*)    
112                                 TYPE=RTF  ;;
113                 "smtp mail text"* | "RFC 822 mail text"*)       
114                                 TYPE=TEXT       ;;
115                 "Bourne shell script"* | "Bourne-Again shell script"*)
116                                 TYPE=TEXT       ;;
117         esac
118 fi
119
120 case $TYPE in
121         TARGZ)  echo -e "Tarball contents:\n"           ; 
122                 tar -tzvf "$1"                          ;;
123
124         TARBZ)  echo -e "Tarball contents:\n"           ; 
125                 tar -tjvf "$1"                          ;;
126
127         BZIP)   TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
128                 bunzip2 -c "$1" > "$TMP"  || exit 1;
129                 "$0" "$TMP";
130                 rm "$TMP"                               ;;
131
132         GZIP)   TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
133                 gunzip -c "$1" > "$TMP"  || exit 1;
134                 "$0" "$TMP";
135                 rm "$TMP"                               ;;
136
137         TAR)    echo -e "Tar archive contents:\n"       ; 
138                 tar -tvf "$1"                           ;;
139
140         ZIP)    unzip -l "$1"                           ;;
141
142         RTF)    which unrtf > /dev/null  2>&1 || 
143                 {
144                         echo "Program 'unrtf' for displaying RTF files not found" >&2
145                         exit 1
146                 };
147                 unrtf -t text "$1" 2>/dev/null | egrep  -v '^### ' | fold -s -w 72  ;;
148
149         TEXT)   cat "$1"                                ;;
150
151         MSWORD) which antiword  > /dev/null  2>&1 || 
152                 {
153                         echo "Program 'antiword' for displaying MS Word files not found" >&2
154                         exit 1 
155                 };
156                 antiword -w 72 "$1"                             ;;
157
158         *)      echo "Unsupported file type \"$FILETYPE\", cannot display.";;
159 esac