Last active
December 26, 2015 10:29
-
-
Save targzeta/7137182 to your computer and use it in GitHub Desktop.
Traslating .eml file to a maildir filename.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (C) 2013 Emanuele Tomasi <[email protected]> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# Traslates .eml file to maildir format. | |
# -h help | |
SCRIPT_NAME=${0##*/} | |
SCRIPT_AUTHOR="Emanuele Tomasi <[email protected]>" | |
############# | |
# FUNCTIONS # | |
############# | |
function _exit | |
{ | |
echo -e "${SCRIPT_NAME}: ${1}" | |
exit $2 | |
} | |
function _help | |
{ | |
cat <<EOF | |
Use: ${SCRIPT_NAME} [option]... file destdir | |
Translates a .eml file to a maildir filename. For setting hostname it uses | |
HOST environment variable or, if it isn't setted, HOSTNAME. | |
The default filename is for new/ directory. | |
Options: | |
-c Setting the filename for a cur/ directory. You can set your flag by | |
setting the FLAG environment variable. Default FLAG is 'S'. | |
-d Dovecot extension. Sets the 'W' option. | |
-h Display this help and exit. | |
by ${SCRIPT_AUTHOR}" | |
EOF | |
} | |
function _check_extern_programs | |
{ | |
local error=0 | |
local string_error | |
if ! which --version >&/dev/null | |
then | |
error=1 | |
string_error="which : command not founds.\n"; | |
else | |
for progr in $@ | |
do | |
if ! which $progr >& /dev/null | |
then | |
error=1 | |
string_error=${string_error}"${progr} : command not founds.\n" | |
fi | |
done | |
fi | |
if (( $error )) | |
then | |
_exit "You need to install these commands:\n$string_error" 1 | |
fi | |
} | |
########## | |
# CHECKS # | |
########## | |
enable getopts echo exit | |
IFS=$'\n\t ' | |
OPT_C=0 | |
OPT_D=0 | |
while getopts :hcd opzione | |
do | |
case $opzione in | |
c) OPT_C=1;; | |
d) OPT_D=1;; | |
h) _help; exit;; | |
?) _exit "-${OPTARG} : not valid argument." 1;; | |
esac | |
done | |
while (( $OPTIND != 1 )) | |
do | |
shift | |
let --OPTIND | |
done | |
[ -z "$1" -o -z "$2" ] && _exit "missing argument. "\ | |
"${SCRIPT_NAME} -h for help." 1 | |
_check_extern_programs date fromdos head mktemp stat touch | |
######## | |
# MAIN # | |
######## | |
file=$1 | |
destdir=$2 | |
_TMPFILE=$(mktemp) | |
[ -z ${_TMPFILE} ] && exit 1 | |
fromdos < $file > $_TMPFILE | |
_HOST=${HOST:-$HOSTNAME} | |
_RANDOM=$(</dev/urandom tr -cd '[:digit:]' | head -c 6) | |
_DATE=$(sed -n '/^Date:/{s/Date: //;p;q}' $file) | |
_TIMESTAMP=$(date --date="$_DATE" '+%s') | |
_SIZE=$(stat $_TMPFILE --printf='%s') | |
_FILENAME="${_TIMESTAMP}.P${_RANDOM}Q2.${_HOST},S=${_SIZE}" | |
if (( $OPT_D == 1 )) | |
then | |
_WSIZE=$(stat $file --printf='%s') | |
_FILENAME+=",W=${_WSIZE}" | |
fi | |
if (( $OPT_C == 1 )) | |
then | |
_FLAG=${FLAG:-S} | |
_FILENAME+=":2,${_FLAG}" | |
fi | |
_OUTPUT_FILE=${destdir}/${_FILENAME} | |
mv $_TMPFILE $_OUTPUT_FILE | |
touch --date="${_DATE}" $_OUTPUT_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This fails pretty badly for files with spaces.
I'd recommend quoting all file and folder paths.
(and it said it could not find
which
- which was obviously wrong on my fresh Debian 8)