textviewer.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env bash
  2. # textviewer.sh
  3. # Copyright 2003 Luke Plant <L.Plant.98@cantab.net>
  4. # and Johann Koenig <johann@mental-graffiti.com>
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 3
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  18. # 02111-1307, USA.
  19. ##############################################################################
  20. #
  21. # This script is a text viewer designed to be used with claws-mail actions
  22. # Set up an action with the command line: textviewer.sh %p |
  23. #
  24. # The script will try to detect file type automatically, and then
  25. # invokes a relevant program to print the file in plain text to
  26. # the standard output.
  27. #
  28. # From v 0.9.7claws7, claws-mail sets the temporary file
  29. # of a part to XXXXXX.mimetmp.[filename of attachment]
  30. # This means we can use the extension of the filename for checking.
  31. # Also use the program 'file' if that fails.
  32. #
  33. # To extend the script just follow the patterns that already exist, or
  34. # contact the author if you have problems.
  35. ##############################################################################
  36. #
  37. # Change Log
  38. #
  39. # 2003-03-25
  40. # - make extension matching case insensitive
  41. #
  42. # 2003-03-23
  43. # - Support for MS Excel (xlhtml) and Powerpoint (ppthtml)
  44. #
  45. # 2004-03-09
  46. # - Support for HTML (html2text)
  47. #
  48. # 2004-02-13
  49. # - added support for perl and shell scripts, and recognize that
  50. # 'file' will always return 'text' somewhere in its output for
  51. # files that, well, contain text
  52. #
  53. # 2004-01-25
  54. # - added brief messages describing whats going on
  55. #
  56. # 2004-01-23
  57. # - added support for 'pdftotext,' from xpdf-utils debian package
  58. #
  59. # 2004-01-05
  60. # - added matcher and action for OpenOffice Writer documents
  61. # (requires ooo2txt)
  62. #
  63. # 2004-01-05
  64. # - changed page width parameter for antiword
  65. # - fixed matcher for 'diffs'
  66. # - added a matcher and action for bzip2 - bzip2 files
  67. # are decompressed and textviewer.sh run on the result
  68. # - similarly decompress gzip files and run textviewer.sh
  69. # on the result, insteading of doing 'gzip -l'
  70. #
  71. # 2003-12-30
  72. # added the script to claws-mail/tools
  73. #
  74. # 2003-12-30
  75. # - use 'fold' after 'unrtf' to wrap to a nice width
  76. # - added basic file sanity checks
  77. #
  78. # 2003-12-29
  79. # Added recognition for "Zip " from 'file' output
  80. #
  81. # 2003-12-19
  82. # Initial public release
  83. #
  84. ###############################################################################
  85. if [ $# -eq 0 ]
  86. then
  87. echo "No filename supplied." >&2
  88. echo "Usage: textviewer.sh FILE" >&2
  89. exit 1
  90. fi
  91. [ -f "$1" ] ||
  92. {
  93. echo "File \"$1\" does not exist or is not a regular file." >&2
  94. exit 1
  95. }
  96. [ -r "$1" ] ||
  97. {
  98. echo "Cannot read file \"$1\"." >&2
  99. exit 1
  100. }
  101. FILETYPE=`file --brief "$1"` ||
  102. {
  103. echo "Please install the command 'file' to use this script." >&2
  104. exit 1
  105. };
  106. FNAME=`echo "$1" | tr [A-Z] [a-z]`
  107. case "$FNAME" in
  108. *.doc) TYPE=MSWORD ;;
  109. *.ppt) TYPE=POWERPOINT ;;
  110. *.zip) TYPE=ZIP ;;
  111. *.tar.gz|*.tgz) TYPE=TARGZ ;;
  112. *.tar.bz2|*.tar.bz) TYPE=TARBZ ;;
  113. *.gz) TYPE=GZIP ;;
  114. *.bz2|*.bz) TYPE=BZIP ;;
  115. *.tar) TYPE=TAR ;;
  116. *.diff) TYPE=TEXT ;;
  117. *.txt) TYPE=TEXT ;;
  118. *.rtf) TYPE=RTF ;;
  119. *.sxw) TYPE=OOWRITER ;;
  120. *.pdf) TYPE=PDF ;;
  121. *.sh) TYPE=TEXT ;;
  122. *.pl) TYPE=TEXT ;;
  123. *.html|*.htm) TYPE=HTML ;;
  124. *.xls) TYPE=EXCEL ;;
  125. esac
  126. if [ "$TYPE" = "" ]
  127. then
  128. case $FILETYPE in
  129. *"HTML"*) TYPE=HTML ;;
  130. *"text"*) TYPE=TEXT ;;
  131. gzip*) TYPE=GZIP ;;
  132. bzip2*) TYPE=BZIP ;;
  133. "POSIX tar archive"*) TYPE=TAR ;;
  134. "Zip "*) TYPE=ZIP ;;
  135. "Rich Text Format"*)
  136. TYPE=RTF ;;
  137. esac
  138. fi
  139. case $TYPE in
  140. TARGZ) echo -e "Gzip'd tarball contents:\n" ;
  141. tar -tzvf "$1" ;;
  142. TARBZ) echo -e "Bzip'd tarball contents:\n" ;
  143. tar -tjvf "$1" ;;
  144. BZIP) TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
  145. bunzip2 -c "$1" > "$TMP" || exit 1;
  146. echo -e "Re-running \"$0\" on bunzip'd contents of \"$1\":\n";
  147. "$0" "$TMP";
  148. rm "$TMP" ;;
  149. GZIP) TMP=`mktemp "$1".temp.XXXXXXX` || exit 1;
  150. gunzip -c "$1" > "$TMP" || exit 1;
  151. echo "Re-running \"$0\" on gunzip'd contents of \"$1\":\n";
  152. "$0" "$TMP";
  153. rm "$TMP" ;;
  154. TAR) echo -e "Tar archive contents:\n" ;
  155. tar -tvf "$1" ;;
  156. ZIP) echo -e "Zip file contents:\n" ;
  157. unzip -l "$1" ;;
  158. RTF) which unrtf > /dev/null 2>&1 ||
  159. {
  160. echo "Program 'unrtf' for displaying RTF files not found" >&2
  161. exit 1
  162. };
  163. echo -e "Displaying \"$1\" using \"unrtf\":\n";
  164. unrtf -t text "$1" 2>/dev/null | egrep -v '^### ' | fold -s -w 72 ;;
  165. TEXT) cat "$1" ;;
  166. MSWORD) which antiword > /dev/null 2>&1 ||
  167. {
  168. echo "Program 'antiword' for displaying MS Word files not found" >&2
  169. exit 1
  170. };
  171. echo -e "Displaying \"$1\" using \"antiword\":\n";
  172. antiword -w 72 "$1" ;;
  173. POWERPOINT) which ppthtml > /dev/null 2>&1 ||
  174. {
  175. echo "Program 'ppthtml' for displaying Powerpoint files not found" >&2
  176. exit 1
  177. };
  178. which html2text > /dev/null 2>&1 ||
  179. {
  180. echo "Program 'html2text' for displaying Powerpoint files not found" >&2
  181. exit 1
  182. };
  183. echo -e "Displaying \"$1\" using \"ppthtml\" and \"html2text\":\n";
  184. ppthtml "$1" | html2text ;;
  185. EXCEL) which xlhtml > /dev/null 2>&1 ||
  186. {
  187. echo "Program 'xlhtml' for displaying Excel files not found" >&2
  188. exit 1
  189. };
  190. which html2text > /dev/null 2>&1 ||
  191. {
  192. echo "Program 'html2text' for displaying Excel files not found" >&2
  193. exit 1
  194. };
  195. echo -e "Displaying \"$1\" using \"xlhtml\" and \"html2text\":\n";
  196. xlhtml "$1" | html2text ;;
  197. OOWRITER) which ooo2txt > /dev/null 2>&1 ||
  198. {
  199. echo "Program 'ooo2txt' for converting OpenOffice Writer files not files not found" >&2
  200. exit 1
  201. };
  202. echo -e "Displaying \"$1\" using \"ooo2txt\":\n";
  203. ooo2txt "$1" ;;
  204. PDF) which pdftotext > /dev/null 2>&1 ||
  205. {
  206. echo "Program 'pdftotext' for converting Adobe Portable Document Format to text not found" >&2
  207. exit 1
  208. };
  209. echo -e "Displaying \"$1\" using \"pdftotext\":\n";
  210. pdftotext "$1" - ;;
  211. HTML) which html2text > /dev/null 2>&1 ||
  212. {
  213. echo "Program 'html2text' for converting HTML files not found" >&2
  214. exit 1
  215. };
  216. html2text -nobs "$1" ;;
  217. *) echo "Unsupported file type \"$FILETYPE\", cannot display.";;
  218. esac