#!/bin/bash
#
# This is a sample script for encapsulating incoming attachments in 
# .ZIP files on the fly, based on the ideas expressed in this post:
#
#  http://mailtools.anomy.net/archives/anomy-list/2002-10/0008.shtml
#
# Enable by adding to your sanitizer configuration the following
# lines:
#
#  file_list_12_scanner = 0:::/path/to/zip_script %FILENAME %ATTNAME
#  file_list_12_policy  = accept:unknown:unknown:unknown
#  file_list_12 = .*
#
# Using some other list (other than 12) will alter the priority of the
# zip rule - using 12 will by default ZIP any incoming Office documents,
# the "Miscellanious" category and unknown attachments - archives and
# static data (gifs, jpgs, text, html, etc) will not be zipped.
#
# Similar tricks can be used to convert from one data type to another.
#
# Paranoid admins could use the -P flag for the zip command to put a
# password on the created zip files...
#
ZIP=$(which zip)
[ "$ZIP" = "" ] && exit 2
SED=$(which sed)
[ "$SED" = "" ] && exit 3

FILE=$1
NAME=$(echo "$2" | sed -e 's/\//_/g');
[ -f "$FILE"   ] || exit 1
[ "$NAME" = "" ] && NAME="$FILE"

# Do stuff within a temporary directory...
mkdir -p "/tmp/zip_script.$$" || exit 5
cd "/tmp/zip_script.$$"       || exit 6
ln "$FILE" "$NAME"

FILE2=$(echo "$FILE" | sed -e 's/\./_/g').zip
NAME2=$(echo "$NAME" | sed -e 's/\./_/g').zip
zip -Dkm $FILE2 "$NAME" 2>/dev/null 1>/dev/null || exit 4

# Cleanup
cd /
rm -rf "/tmp/zip_script.$$" "$FILE"

echo Anomy-FileScan-NewFile: $FILE2
echo Anomy-FileScan-NewName: $NAME2
echo Anomy-FileScan-NewType: application/octet-stream
echo Anomy-FileScan-NewEnc:  Base64
echo
exit 0
