summary refs log tree commit diff
path: root/mcomp
blob: 6fdb43310a31c49e50addd45a8294420d189238d (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/sh
# mcomp [TO] - compose mail

commajoin() {
	sed ':a;N;s/\n/, /;$!b a'
}

reffmt() {
	sed 's/^[^<]*//g;s/[^>]*$//g;s/>[^<]*</>\n</g' | uniq | sed 's/^/ /'
}

msgid() {
	mgenmid 2>/dev/null | sed 's/.*/Message-Id: &/'
}

outbox=$(mhdr -h outbox ~/.mblaze/profile)
if [ -z "$outbox" ]; then
	i=0
	while [ -f "snd.$i" ]; do
		i=$((i+1))
	done
	draft="snd.$i"
	draftmime="./snd.$i.mime"
else
	draft="$(true | mdeliver -v -XD "$outbox")"
	if [ -z "$draft" ]; then
		echo "$0: failed to create draft in outbox $outbox." 1>&2
		exit 1
	fi
	draftmime="$(echo $draft | sed 's,\(.*\)/cur/,\1/tmp/mime-,')"
fi

{
	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: "
		from=$(mhdr -h local-mailbox ~/.mblaze/profile)
		[ "$from" ] && echo "From: $from"
		cat ~/.mblaze/headers 2>/dev/null
		msgid
		echo
		echo
		;;
	*mrepl*)
		[ "$#" -eq 0 ] && set -- .
		to=$(mhdr -h reply-to "$1")
		[ -z "$to" ] && to=$(mhdr -h from "$1")
		echo "To: $to"
		echo "Cc: $(mhdr -d -A -h to:cc: "$1" | commajoin)"
		echo "Bcc: "
		s=$(mhdr -d -h 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 ~/.mblaze/headers 2>/dev/null
		echo -n "References:"
		{
			mhdr -h references "$1"
			mhdr -h message-id "$1"
		} | reffmt
		echo "In-Reply-To: $(mhdr -h message-id "$1")"
		msgid
		echo

		mquote "$1"
		echo
	esac

	if [ -f ~/.signature ]; then
		printf '%s\n' '-- '
		cat ~/.signature
	fi
} | msed '/date/a' /dev/stdin >$draft

c=e
while :; do
	case "$c" in
	s|send)
		if [ -e $draftmime ]; then
			if [ $draft -ot $draftmime ]; then
				if sendmail -t <$draftmime; then
					if [ "$outbox" ]; then
						mv $draftmime $draft
						mflag -d $draft
					else
						rm $draft $draftmime
					fi
					exit 0
				else
					echo "mcomp: sendmail failed, kept draft $draft"
					exit 2
				fi
			else
				echo "mcomp: re-run mmime first."
				c=
			fi
		else
			if mmime -c <$draft; then
				if sendmail -t <$draft; then
					if [ "$outbox" ]; then
						mflag -d $draft
					else
						rm $draft
					fi
					exit 0
				else
					echo "mcomp: sendmail failed, kept draft $draft"
					exit 2
				fi
			else
				echo "mcomp: message needs to be MIME-encoded first."
				c=
			fi
		fi
		;;
	c|cancel)
		echo "mcomp: cancelled draft $draft"
		exit 1
		;;
	m|mime)
		(
			IFS='
'
			msed '/attach/d' $draft
			for f in $(mhdr -M -h attach $draft); do
				echo "#$(file -Lbi $f | sed 's/ //g') $f"
			done
		) | tee /tmp/v | mmime >$draftmime
		mshow -t $draftmime
		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