Skip to content

Commit f54ff9f

Browse files
committed
feat: add mail-in-ctr util
1 parent df8008c commit f54ff9f

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

utils/mail-in-ctr.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/sh
2+
# Proxy to s-nail mail/mailx inside smtp container
3+
: ${CTR_NAME:=smtp-proxy}
4+
SELF_NAME=mail-in-ctr
5+
6+
SHORT_OPTS='dEhntvVa:b:c:q:r:s:T:'
7+
LONG_OPTS='debug,discard-empty-messages,help,template,verbose,version'$(\
8+
)',attach:,bcc:,cc:,quote-file:,from-address:,subject:,target:'
9+
10+
arg_quot () {
11+
# test'asdf"blah -> 'test'\''asdf"blah'
12+
printf '%s' "$@" | sed "s/'/'\\\\''/g; s/.*/'&'/"
13+
}
14+
15+
if [ "$(getopt -T > /dev/null; echo $?)" -eq 4 ]; then
16+
# sed is to strip the extra -- param
17+
orig_opts=$(getopt -- '' -- "$@" | sed 's/^ *-- *//')
18+
# convert -. and --end-options to --
19+
eval set -- "$(echo "$orig_opts" | sed -E "s/' *(-\.|--end-options) *'/--/")"
20+
opts=$(getopt -n $SELF_NAME -l "$LONG_OPTS" -- "$SHORT_OPTS" "$@") || exit
21+
eval set -- "$opts"
22+
out_opts=
23+
while true; do
24+
case "$1" in
25+
# safe flags
26+
-d|--debug|\
27+
-E|--discard-empty-messages|\
28+
-n|\
29+
-t|--template|\
30+
-v|--verbose|\
31+
-V|--version)
32+
out_opts="$out_opts $1"
33+
shift
34+
;;
35+
36+
# safe params
37+
-b|--bcc|\
38+
-c|--cc|\
39+
-r|--from-address|\
40+
-s|--subject|\
41+
-T|--target)
42+
out_opts="$out_opts $1 $(arg_quot "$2")"
43+
shift 2
44+
;;
45+
46+
# special
47+
-a|--attach)
48+
echo "ERROR: $SELF_NAME: attachments are unsupported" >&2
49+
exit 1
50+
;;
51+
-h|--help)
52+
out_opts='--help'
53+
break
54+
;;
55+
-q|--quote-file)
56+
if [ "$2" != '-' ]; then
57+
echo "ERROR: $SELF_NAME: only '-' for stdin is supported with '$1'" >&2
58+
exit 1
59+
fi
60+
out_opts="$out_opts -q -"
61+
shift 2
62+
;;
63+
64+
# end of options
65+
--) shift; break;;
66+
67+
*)
68+
echo "ERROR: $SELF_NAME: unhandled paramater '$1'" >&2
69+
exit 1
70+
;;
71+
esac
72+
done
73+
if [ "$out_opts" = '--help' ]; then
74+
eval set -- --help
75+
else
76+
out_opts=${out_opts# }
77+
eval set -- "$out_opts" -. '"$@"'
78+
fi
79+
else
80+
echo "WARNING: $SELF_NAME: falling back to insecure parameter passing because this getopt is unsupported" >&2
81+
fi
82+
83+
exec ${CTR_EXEC_CMD:-podman exec} -i "$CTR_NAME" mail "$@"

0 commit comments

Comments
 (0)