attachments_parser 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. # Kopano script which is executed to convert an attachment to plain text.
  3. # Description:
  4. # This script accepts attachment data through the standard input
  5. # and sends the parsed information as plain-text back through standard output.
  6. # Argument validation:
  7. # $1: content-type of the second argument
  8. # valid values are "ext" (for file extension)
  9. # and "mime" (for mime-type).
  10. #
  11. # $2: File extension or mime-type of the data passed
  12. # through the standard input.
  13. TYPE=$1
  14. ARG=$2
  15. # Both arguments are mandatory
  16. if [ -z "$TYPE" -o -z "$ARG" ]; then
  17. exit 1
  18. fi
  19. # For file extension we should strip the prefixed '.'
  20. # For mime-type we must escape the '/' character
  21. if [ "${TYPE}" == "ext" ]; then
  22. ARG=`echo $ARG | tr '[:upper:]' '[:lower:]'`
  23. ARG="${ARG##*.}"
  24. elif [ "${TYPE}" == "mime" ]; then
  25. ARG=${ARG/\//\\\/}
  26. else
  27. exit 1
  28. fi
  29. # Some attachment parsers do not support input from standard input.
  30. # For these parsers we provide a temporary directory in which all
  31. # temp files can be stored.
  32. # To make sure `mktemp` and `tempfile` will automatically place
  33. # file in this directory if we properly export the TMPDIR variable.
  34. TMPBASE="`mktemp -d`"
  35. if [ -z ${TMPBASE} -o $? != 0 ]; then
  36. exit 1
  37. fi
  38. export TMPDIR=${TMPBASE}
  39. # This might be a bit nasty, but PATH must be prefixed with the location
  40. # of this script. The reason is that we provide additional scripts to help
  41. # with parsing.
  42. export SCRIPTPATH="`dirname $0`"
  43. export PATH="${SCRIPTPATH}:${PATH}"
  44. # Use awk to obtain and execute the command for this attachment
  45. CMD="`awk -F'\`' '/^([^#]*;)*'${ARG}'[ \t;]+([^#]*;)*/ { print \$2 }' ${SCRIPTPATH}/attachments_parser.db | head -n 1`"
  46. eval "${CMD}"
  47. RV=$?
  48. # Clean up temporary directory including all files generated during parsing.
  49. if [ -d ${TMPBASE} ]; then
  50. rm -rf ${TMPBASE}
  51. fi
  52. exit $RV