48 lines
1.2 KiB
Bash
Executable file
48 lines
1.2 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
temp_dir="$(mktemp --tmpdir -d "imap-download-XXXXXXX")";
|
|
on_exit() {
|
|
rm -rf "${temp_dir}";
|
|
}
|
|
trap on_exit EXIT;
|
|
|
|
do_fetchmail() {
|
|
fetchmail --mda "/usr/bin/procmail -m /srv/procmail.conf";
|
|
}
|
|
|
|
log_msg() {
|
|
echo "$(date -u +"%Y-%m-%d %H:%M:%S") imap-download: $*";
|
|
}
|
|
|
|
dir_newmail="/tmp/maildir/Mail/new";
|
|
target_dir="/mnt/output";
|
|
|
|
do_attachments() {
|
|
while :; do # : = infinite loop
|
|
# Wait for an update
|
|
# inotifywait's non-0 exit code forces an exit for some reason :-/
|
|
inotifywait -qr --event create --format '%:e %f' "${dir_newmail}";
|
|
|
|
while read -r filename; do
|
|
log_msg "Processing email ${filename}";
|
|
|
|
# Move the email to a temporary directory for processing
|
|
mv "${filename}" "${temp_dir}";
|
|
|
|
# Unpack the attachments
|
|
munpack -C "${temp_dir}" "${filename}";
|
|
# Delete the original email file and any description files
|
|
rm "${filename}";
|
|
find "${temp_dir}" -iname '*.desc' -delete;
|
|
|
|
# Move the attachment files to the output directory
|
|
while read -r attachment; do
|
|
log_msg "Extracted attachment ${attachment}";
|
|
mv "${attachment}" "${target_dir}";
|
|
done < <(find "${temp_dir}" -type f);
|
|
done < <(find "${dir_newmail}" -type f);
|
|
done
|
|
}
|
|
|
|
do_fetchmail &
|
|
do_attachments
|