-
Notifications
You must be signed in to change notification settings - Fork 0
/
eml2mbox
executable file
·60 lines (51 loc) · 1.85 KB
/
eml2mbox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
help() { cat <</help
Convert a list of files (via STDIN or command line) to mbox format on STDOUT
Usage: eml2mbox [OPTIONS] [FILE...]
--rfc822 FILEs are all RFC 822 (RFC 5322, .eml) (default: check 1st FILE)
--email=EMAIL Use EMAIL in \`From\` delimiter instead of the default address:
$delim
FILEs may be either .eml (RFC 5322) file or it may be a list of such files.
eml2mbox will assume everything is the same format as the first input file.
Part of net-scripts: https://github.com/adamhotep/net-scripts
eml2mbox 0.4.20170408.1, Copyright 2010+ by Adam Katz, GPLv2+
/help
exit
}
die() {
echo "$*" >&2
exit 2
}
mkmbox() {
awk -v DELIM="$(date +"From $delim %c")" '
FNR == 1 { print DELIM }
{ print }' "$@"
}
delim='[email protected]'
while getopts h-: opt; do
case "$opt" in
( h ) help ;;
( - ) case "$OPTARG" in
( email=?* ) delim="$OPTARG" ;;
( email* ) die "No arg for --email option ... Try --help" ;;
( help* ) help ;;
( rfc*22 ) RFC822=1 ;;
( * ) die "Illegal option --$OPTARG ... Try --help" ;; esac ;;
( \? ) die "Try --help" ;;
esac
done
shift $((OPTIND-1))
if [ -n "$RFC822" ] || [ -r "$1" ] && file "$1" |grep -liw mail >/dev/null; then
mkmbox "$@"
elif [ -n "$RFC822" ]; then
echo "Not sure what to do; your input file(s) were supposed to contain" >&2
echo "either .eml files (rfc822) or else a list of .eml file paths." >&2
echo "Since your input(s) were not .eml, we parsed them line by line" >&2
echo "and tried to read each full line as a file, but that didn't work." >&2
exit 2
else
# Assume one file per line, null-delimit to preserve spaces
# (Using xargs -d "\n" requires GNU xargs, so this is more reliable)
awk '{printf "%s%c", $0, 0}' "$@" |xargs -0 \
"$(which "$0" 2>/dev/null || echo "$0")" --rfc822
fi