summary refs log tree commit diff
path: root/mcomp
blob: 037dc96e94624f504649d4d8a9dd920e20e8ddd6 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/sh
# mcomp [TO] - compose mail

i=0
while [ -f "snd.$i" ]; do
	i=$((i+1))
done
draft="snd.$i"

{
	case "$0" in
	*mcomp*)
		echo -n "To: $1"
		[ "$#" -ge 1 ] && shift
		for rcpt; do
			echo -n ", $rcpt"
		done
		echo
		echo "Cc: "
		echo "Bcc: "
		echo "Subject: "
		cat ~/.santoku/headers 2>/dev/null
		echo
		echo
		;;
	*mrepl*)
		[ "$#" -eq 0 ] && set -- .
		# XXX reply-all
		echo "To: $(mhdr -from "$1")"
		echo "Cc: "
		echo "Bcc: "
		s=$(mhdr -subject "$1")
		os=
		while [ "$os" != "$s" ]; do
			os=$s
			s=${s% }
			s=${s%[Rr][Ee]:}
			s=${s%[Aa][Ww]:}
			s=${s%[Ff][Ww][Dd]:}
		done
		echo "Subject: Re: $s"
		cat ~/.santoku/headers 2>/dev/null
		echo -n "References:"
		{
			mhdr -references "$1"
			mhdr -message-id "$1"
		} | sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\n</g' |
			uniq | sed 's/^/ /'
		echo "In-Reply-To: $(mhdr -message-id "$1")"
		echo

		echo "$(mhdr -from "$1") wrote:"
		mshow -R "$1" |
			sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' |
			sed 's/^/> /'
		echo
	esac

	if [ -f ~/.signature ]; then
		printf '%s\n' '-- '
		cat ~/.signature
	fi
} >$draft

c=e
while :; do
	case "$c" in
	s|send)
		if [ -e $draft.mime ]; then
			if [ $draft -ot $draft.mime ]; then
				if sendmail -t <$draft.mime; then
					rm $draft $draft.mime
					exit 0
				else
					echo "mcomp: sendmail failed, kept draft $draft"
					exit 2
				fi
			else
				echo "mcomp: re-run mmime first."
				c=
			fi
		else
			if sendmail -t <$draft; then
				rm $draft
				exit 0
			else
				echo "mcomp: sendmail failed, kept draft $draft"
				exit 2
			fi
		fi
		;;
	c|cancel)
		echo "mcomp: cancelled draft $draft"
		exit 1
		;;
	m|mime)
		mmime <$draft >$draft.mime
		mshow -t ./$draft.mime
		c=
		;;
	e|edit)
		c=
		if ! ${EDITOR:-vi} $draft; then
			c=c
		fi
		;;
	*)
		echo -n "What now? ([s]end, [c]ancel, [e]dit, [m]ime) "
		read -r c
		;;
	esac
done