You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

128 lines
3.9 KiB

  1. #!/bin/bash
  2. #
  3. # Author: Eric Gebhart
  4. #
  5. # Purpose: To be called by mutt as indicated by .mailcap to handle mail attachments.
  6. #
  7. # Function: Copy the given file to a temporary directory so mutt
  8. # Won't delete it before it is read by the application.
  9. #
  10. # Along the way, discern the file type or use the type
  11. # That is given.
  12. #
  13. # Finally use 'open' or 'open -a' if the third argument is
  14. # given.
  15. #
  16. #
  17. # Arguments:
  18. #
  19. # $1 is the file
  20. # $2 is the type - for those times when file magic isn't enough.
  21. # I frequently get html mail that has no extension
  22. # and file can't figure out what it is.
  23. #
  24. # Set to '-' if you don't want the type to be discerned.
  25. # Many applications can sniff out the type on their own.
  26. # And they do a better job of it too.
  27. #
  28. # Open Office and MS Office for example.
  29. #
  30. # $3 is open with. as in open -a 'open with this .app' foo.xls
  31. #
  32. # Examples: These are typical .mailcap entries which use this program.
  33. #
  34. # Image/JPEG; /Users/vdanen/.mutt/view_attachment %s
  35. # Image/PNG; /Users/vdanen/.mutt/view_attachment %s
  36. # Image/GIF; /Users/vdanen/.mutt/view_attachment %s
  37. #
  38. # Application/PDF; /Users/vdanen/.mutt/view_attachment %s
  39. #
  40. # #This HTML example passes the type because file doesn't always work and
  41. # #there aren't always extensions.
  42. #
  43. # text/html; /Users/vdanen/.mutt/view_attachment %s html
  44. #
  45. # # If your Start OpenOffice.org.app is spelled with a space like this one, <--
  46. # # then you'll need to precede the space with a \ . I found that too painful
  47. # # and renamed it with an _.
  48. #
  49. # Application/vnd.ms-excel; /Users/vdanen/.mutt/view_attachment %s "-" '/Applications/OpenOffice.org1.1.2/Start_OpenOffice.org.app'
  50. # Application/msword; /Users/vdanen/.mutt/view_attachment %s "-" '/Applications/OpenOffice.org1.1.2/Start_OpenOffice.org.app'
  51. #
  52. #
  53. # Debugging: If you have problems set debug to 'yes'. That will cause a debug file
  54. # be written to /tmp/mutt_attach/debug so you can see what is going on.
  55. #
  56. # See Also: The man pages for open, file, basename
  57. #
  58. # the tmp directory to use.
  59. tmpdir="$HOME/.tmp/mutt_attach"
  60. # the name of the debug file if debugging is turned on.
  61. debug_file=$tmpdir/debug
  62. # debug. yes or no.
  63. #debug="no"
  64. debug="yes"
  65. type=$2
  66. open_with=$3
  67. # make sure the tmpdir exists.
  68. mkdir -p $tmpdir
  69. # clean it out. Remove this if you want the directory
  70. # to accumulate attachment files.
  71. rm -f $tmpdir/*
  72. # Mutt puts everything in /tmp by default.
  73. # This gets the basic filename from the full pathname.
  74. filename=`basename $1`
  75. # get rid of the extenson and save the name for later.
  76. file=`echo $filename | cut -d"." -f1`
  77. if [ $debug = "yes" ]; then
  78. echo "1:" $1 " 2:" $2 " 3:" $3 > $debug_file
  79. echo "Filename:"$filename >> $debug_file
  80. echo "File:"$file >> $debug_file
  81. echo "===========================" >> $debug_file
  82. fi
  83. # if the type is empty then try to figure it out.
  84. if [ -z $type ]; then
  85. file $1
  86. type=`file -bi $1 | cut -d"/" -f2`
  87. fi
  88. # if the type is '-' then we don't want to mess with type.
  89. # Otherwise we are rebuilding the name. Either from the
  90. # type that was passed in or from the type we discerned.
  91. if [ $type = "-" ]; then
  92. newfile=$filename
  93. else
  94. newfile=$file.$type
  95. fi
  96. newfile=$tmpdir/$newfile
  97. # Copy the file to our new spot so mutt can't delete it
  98. # before the app has a chance to view it.
  99. cp $1 $newfile
  100. if [ $debug = "yes" ]; then
  101. echo "File:" $file "TYPE:" $type >> $debug_file
  102. echo "Newfile:" $newfile >> $debug_file
  103. echo "Open With:" $open_with >> $debug_file
  104. fi
  105. # If there's no 'open with' then we can let preview do it's thing.
  106. # Otherwise we've been told what to use. So do an open -a.
  107. if [ -z $open_with ]; then
  108. open $newfile
  109. else
  110. open -a "$open_with" $newfile
  111. fi