about summary refs log tree commit diff
path: root/git-fetch-email.sh
blob: 80b3bb7666931be500d9ab0b998cdc0c33c66523 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/sh
# Copyright (C) 2019  C. McEnroe <june@causal.agency>
#
# 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 <https://www.gnu.org/licenses/>.

set -u

host=$(git config fetchemail.imapServer)
port=$(git config fetchemail.imapServerPort)
user=$(git config fetchemail.imapUser)
pass=$(git config fetchemail.imapPass)
mailbox=$(git config fetchemail.imapMailbox)
subject=$(git config fetchemail.subject)
from=$(git config fetchemail.from)
to=$(git config fetchemail.to)
cc=$(git config fetchemail.cc)

while getopts 'C:F:S:T:h:m:p:' opt; do
	case "$opt" in
		(C) cc=$OPTARG;;
		(F) from=$OPTARG;;
		(S) subject=$OPTARG;;
		(T) to=$OPTARG;;
		(h) host=$OPTARG;;
		(m) mailbox=$OPTARG;;
		(p) port=$OPTARG;;
		(?) exit 1;;
	esac
done
shift $(($OPTIND - 1))
[ $# -ne 0 ] && user=$1
if [ -z "${user:-}" ]; then
	echo "$0: username required"
	exit 1
fi

description() {
	cat <<-EOF
		protocol=imaps
		host=${user#*@}
		username=${user%@*}
		${pass:+password=${pass}}
	EOF
}

pass=$(description | git credential fill | grep '^password=')
pass=${pass#*=}

echo "${pass}" | imbox -w \
	${host:+-h "${host}"} \
	${port:+-p "${port}"} \
	${mailbox:+-m "${mailbox}"} \
	${subject:+-S "${subject}"} \
	${from:+-F "${from}"} \
	${to:+-T "${to}"} \
	${cc:+-C "${cc}"} \
	${user}
status=$?

if [ "${status}" -ne 78 ]; then
	description | git credential approve
else
	description | git credential reject
fi
exit "${status}"