ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen#!/bin/sh
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# Example attachment decoder script. The attachment comes from stdin, and
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# the script is expected to output UTF-8 data to stdout. (If the output isn't
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# UTF-8, everything except valid UTF-8 sequences are dropped from it.)
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# The attachment decoding is enabled by setting:
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen#
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# plugin {
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# fts_decoder = decode2text
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# }
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# service decode2text {
2f971d239f217826f6eae8b16ef74ed1420209dfTimo Sirainen# executable = script /usr/local/libexec/dovecot/decode2text.sh
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# user = dovecot
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# unix_listener decode2text {
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# mode = 0666
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# }
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# }
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
dfd297466b615ada5d49c26504843e8bb30afa07Timo Sirainenlibexec_dir=`dirname $0`
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainencontent_type=$1
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# The second parameter is the format's filename extension, which is used when
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# found from a filename of application/octet-stream. You can also add more
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# extensions by giving more parameters.
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenformats='application/pdf pdf
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenapplication/x-pdf pdf
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenapplication/msword doc
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenapplication/mspowerpoint ppt
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenapplication/vnd.ms-powerpoint ppt
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenapplication/ms-excel xls
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenapplication/x-msexcel xls
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenapplication/vnd.ms-excel xls
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenapplication/vnd.openxmlformats-officedocument.wordprocessingml.document docx
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenapplication/vnd.openxmlformats-officedocument.presentationml.presentation pptx
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenapplication/vnd.oasis.opendocument.text odt
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenapplication/vnd.oasis.opendocument.spreadsheet ods
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenapplication/vnd.oasis.opendocument.presentation odp
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen'
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenif [ "$content_type" = "" ]; then
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen echo "$formats"
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen exit 0
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenfi
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenfmt=`echo "$formats" | grep -w "^$content_type" | cut -d ' ' -f 2`
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenif [ "$fmt" = "" ]; then
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen echo "Content-Type: $content_type not supported" >&2
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen exit 1
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenfi
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# most decoders can't handle stdin directly, so write the attachment
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen# to a temp file
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenpath=`mktemp`
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainentrap "rm -f $path" 0 1 2 3 14 15
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainencat > $path
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenxmlunzip() {
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen name=$1
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen tempdir=`mktemp -d`
57b19a7fa45e9cb76aa45eb211ff2bbfdcc5b514Timo Sirainen if [ "$tempdir" = "" ]; then
57b19a7fa45e9cb76aa45eb211ff2bbfdcc5b514Timo Sirainen exit 1
57b19a7fa45e9cb76aa45eb211ff2bbfdcc5b514Timo Sirainen fi
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen trap "rm -rf $path $tempdir" 0 1 2 3 14 15
57b19a7fa45e9cb76aa45eb211ff2bbfdcc5b514Timo Sirainen cd $tempdir || exit 1
5c1fdc9aafb6c6a881910f5ade6ef32d75285758Timo Sirainen unzip -q "$path" 2>/dev/null || exit 0
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen find . -name "$name" -print0 | xargs -0 cat |
dfd297466b615ada5d49c26504843e8bb30afa07Timo Sirainen $libexec_dir/xml2text
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen}
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainenwait_timeout() {
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen childpid=$!
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen trap "kill -9 $childpid; rm -f $path" 1 2 3 14 15
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen wait $childpid
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen}
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo SirainenLANG=en_US.UTF-8
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenexport LANG
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenif [ $fmt = "pdf" ]; then
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen /usr/bin/pdftotext $path - 2>/dev/null&
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen wait_timeout 2>/dev/null
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenelif [ $fmt = "doc" ]; then
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen (/usr/bin/catdoc $path; true) 2>/dev/null&
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen wait_timeout 2>/dev/null
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenelif [ $fmt = "ppt" ]; then
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen (/usr/bin/catppt $path; true) 2>/dev/null&
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen wait_timeout 2>/dev/null
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenelif [ $fmt = "xls" ]; then
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen (/usr/bin/xls2csv $path; true) 2>/dev/null&
c46d921c475c1f99d12c5e469a6f6862d25a5bb8Timo Sirainen wait_timeout 2>/dev/null
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenelif [ $fmt = "odt" -o $fmt = "ods" -o $fmt = "odp" ]; then
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen xmlunzip "content.xml"
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenelif [ $fmt = "docx" ]; then
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen xmlunzip "document.xml"
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenelif [ $fmt = "xlsx" ]; then
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen xmlunzip "sharedStrings.xml"
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainenelif [ $fmt = "pptx" ]; then
c4ba1974c6ef3c9725b26c155c14ed9e47d21518Timo Sirainen xmlunzip "slide*.xml"
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenelse
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen echo "Buggy decoder script: $fmt not handled" >&2
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainen exit 1
ecc5a1a440799a0966c26da956f5d6e2d8073e03Timo Sirainenfi
dded61056a8913a52194f46915060532ea6be041Timo Sirainenexit 0