about summary refs log tree commit diff
path: root/mcom
blob: c66abea244034a5fec1c173b974981a340cf47c3 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/bin/sh
# mcom [TO] - compose mail

NL='
'

commajoin() {
	awk 'NR==1 {l=$0; next}
	     {l=l", "$0}
	     END {print l}'
}

notmine() {
	mine="$(maddr -a -h local-mailbox:alternate-mailboxes: $MBLAZE/profile)"
	grep -Fv -e "$mine"
}

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

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

stampdate() {
	if ! mhdr -h date "$1" >/dev/null; then
		tmp=$(mktemp -t mcom.XXXXXX)
		{
			printf 'Date: '
			mdate
			cat "$1"		
		} >"$tmp" &&
		mv "$tmp" "$1"
	fi
}

stripempty() {
	tmp=$(mktemp -t mcom.XXXXXX)
	msed 's/^[ \t]*$//d' "$1" >"$tmp"
	mv "$tmp" "$1"
}

needs_multipart() {
	mhdr -h attach "$1" >/dev/null ||
		grep -q '^#[^ ]*/[^ ]* ' "$1"
}

do_mime() {
	if needs_multipart "$draft"; then
		(
			IFS=$NL
			msed '/attach/d' $draft
			for f in $(mhdr -M -h attach $draft); do
				printf '#%s %s\n' \
				       "$(file -Lbi $f | sed 's/ //g')" \
				       "$f"
			done
		) | mmime >$draftmime
	else
		mmime -r <"$draft" >"$draftmime"
	fi
}

MBLAZE=${MBLAZE:-$HOME/.mblaze}

sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
sendmail_args=$(mhdr -h sendmail-args "$MBLAZE/profile")
sendmail="${sendmail:-sendmail} ${sendmail_args:--t}"
default_from=$(mhdr -h local-mailbox "$MBLAZE/profile")

hdrs=
resume=
case "$0" in
*mcom*)
	hdr=to
	while [ $# -gt 0 ]; do
		case "$1" in
		--)
			# -- is like -to, really
			shift
			hdr=to
			;;
		-r)
			shift
			resume=1
			if [ "$#" -gt 0 ]; then
				echo "used draft $1"
				draft="$1"
				shift
			fi
			;;
		-??*)
			hdr=${1#-}
			shift;;
		[!-]*)
			hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
			shift;;
		*)
			printf 'mcom: invalid argument %s\n' "$1" 1>&2
			exit 1;;
		esac
	done
	;;
*mfwd*)
	hdr=
	raw=
 	while [ $# -gt 0 ]; do
		case "$1" in
		--)
			shift
			break;;
		-r)
			shift
			raw=1;;
		-??*)
			hdr=${1#-}
			shift;;
		[!-]*)
			[ -z "$hdr" ] && break
			hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
			shift;;
		*)
			printf 'mfwd: invalid argument %s\n' "$1" 1>&2
			exit 1;;
		esac
	done
	[ "$#" -eq 0 ] && set -- .
	;;
*mbnc*)
	hdr=
 	while [ $# -gt 0 ]; do
		case "$1" in
		--)
			shift
			break;;
		-??*)
			hdr=${1#-}
			shift;;
		[!-]*)
			[ -z "$hdr" ] && break
			hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
			shift;;
		*)
			printf 'mbnc: invalid argument %s\n' "$1" 1>&2
			exit 1;;
		esac
	done
	[ "$#" -eq 0 ] && set -- .
	;;
*mrep*)
	hdr=
 	while [ $# -gt 0 ]; do
		case "$1" in
		--)
			shift
			break;;
		-??*)
			hdr=${1#-}
			shift;;
		[!-]*)
			[ -z "$hdr" ] && break
			hdrs="$hdrs$NL$(printf '%s: %s\n' "${hdr}" "$1")"
			shift;;
		*)
			printf 'mrep: invalid argument %s\n' "$1" 1>&2
			exit 1;;
		esac
	done
	[ "$#" -eq 0 ] && set -- .
	;;
esac

hdrs="$(printf '%s\n' "${hdrs#$NL}" | mhdr /dev/stdin)"

outbox=$(mhdr -h outbox "$MBLAZE/profile")
if [ -z "$outbox" ]; then
	if [ -z "$resume" ]; then
		i=0
		while [ -f "snd.$i" ]; do
			i=$((i+1))
		done
		draft="./snd.$i"
	elif [ -z "$draft" ]; then
		draft=$(ls -1t ./snd.*[0-9] | sed 1q)
	fi
	draftmime="$draft.mime"
else
	if [ -z "$resume" ]; then
		draft="$(true | mdeliver -v -c -XD "$outbox")"
		if [ -z "$draft" ]; then
			printf '%s\n' "$0: failed to create draft in outbox $outbox." 1>&2
			exit 1
		fi
	elif [ -z "$draft" ]; then
		draft=$(mlist -D "$outbox" | msort -r -M | sed 1q)
	fi
	draftmime="$(printf '%s\n' "$draft" | sed 's,\(.*\)/cur/,\1/tmp/mime-,')"
fi

[ -z "$resume" ] &&
{
	case "$0" in
	*mcom*)
		{
			printf '%s' "$hdrs" | mhdr -M -h to /dev/stdin |
				commajoin | sed 's/^/To: /'
			printf '%s' "$hdrs" | mhdr -M -h cc /dev/stdin |
				commajoin | sed 's/^/Cc: /'
			printf '%s' "$hdrs" | mhdr -M -h bcc /dev/stdin |
				commajoin | sed 's/^/Bcc: /'
			printf '%s\n' "$hdrs" | awk '{ print }' |
				msed "/to/d; /cc/d; /bcc/d; /body/d" /dev/stdin
		} | msed "/cc/a//; /bcc/a//; /subject/a//; /from/a/$default_from/" /dev/stdin | sed '/^$/d'
		msgid
		museragent
		cat "$MBLAZE/headers" 2>/dev/null
		printf '\n'
		(
			IFS=$NL
			cat -- /dev/null $(printf '%s' "$hdrs" | mhdr -M -h body /dev/stdin)
		)
		printf '\n'
		;;
	*mfwd*)
		{
			printf '%s' "$hdrs" | mhdr -M -h to /dev/stdin |
				commajoin | sed 's/^/To: /'
			printf '%s' "$hdrs" | mhdr -M -h cc /dev/stdin |
				commajoin | sed 's/^/Cc: /'
			printf '%s' "$hdrs" | mhdr -M -h bcc /dev/stdin |
				commajoin | sed 's/^/Bcc: /'
			COLUMNS=10000 mscan -f 'Subject: [%f] %s' "$@" 2>/dev/null | sed 1q
			printf '%s\n' "$hdrs" | awk '{ print }' |
				msed "/to/d; /cc/d; /bcc/d" /dev/stdin
		} | msed "/cc/a//; /bcc/a//; /from/a/$default_from/" /dev/stdin | sed '/^$/d'
		msgid
		museragent
		cat "$MBLAZE/headers" 2>/dev/null
		printf '\n\n'
		if [ -z "$raw" ]; then
			mseq -r "$@" | sed 's:^:#message/rfc822#inline :; s:$:>:'
		else (
			SEP=-----
			IFS=$NL
			for f in $(mseq -r "$@"); do
				printf '%s Forwarded message from %s %s\n\n' \
				       $SEP "$(mhdr -d -h from "$f")" $SEP
				DISPLAY= mshow -n -N "$f" </dev/null |
					sed 's/^-/- &/'    # RFC 934
				printf '\n%s %s %s\n\n' \
				       $SEP 'End forwarded message' $SEP
			done
		) fi
		;;
	*mbnc*)
		{
			printf '%s' "$hdrs" | mhdr -M -h resent-to /dev/stdin |
				commajoin | sed 's/^/Resent-To: /'
			printf '%s' "$hdrs" | mhdr -M -h resent-cc /dev/stdin |
				commajoin | sed 's/^/Resent-Cc: /'
			printf '%s\n' "$hdrs" | awk '{ print }' |
				msed "/resent-to/d; /resent-cc/d" /dev/stdin
		} |
			msed "/resent-to/a//; /resent-from/a/$default_from/" /dev/stdin | sed '/^$/d'
		msgid | sed 's/^/Resent-/'
		printf 'Resent-Date: %s\n' "$(mdate)"
		(
			IFS=$NL
			cat $(mseq "${@:-.}")
		)
		;;
	*mrep*)
		ng=$(mhdr -h newsgroups "$1")
		if [ "$ng" ]; then
			printf 'Newsgroups: %s\n' "$ng"
		else
			to=$(mhdr -h reply-to "$1")
			[ -z "$to" ] && to=$(mhdr -h from "$1")
			printf 'To: %s\n' "$to"
			printf 'Cc: %s\n' \
			       "$(mhdr -d -A -h to:cc: "$1" |notmine |commajoin)"
			printf 'Bcc: \n'
			printf '%s\n' "$hdrs" | awk '{ print }' |
				msed "/body/d" /dev/stdin
		fi | sed '/^$/d'
		printf 'Subject: Re: %s\n' "$(COLUMNS=10000 mscan -f '%S' "$1")"
		from=$(mhdr -h local-mailbox "$MBLAZE/profile")
		[ "$from" ] && printf 'From: %s\n' "$from"
		mid=$(mhdr -h message-id "$1")
		if [ "$mid" ]; then
			printf 'References:'
			{
				mhdr -h references "$1"
				printf '%s\n' "$mid"
			} | reffmt
			printf 'In-Reply-To: %s\n' "$mid"
		fi
		msgid
		museragent
		cat "$MBLAZE/headers" 2>/dev/null
		printf '\n'

		mquote "$1"
		printf '\n'
		(
			IFS=$NL
			cat -- /dev/null $(printf '%s' "$hdrs" | mhdr -M -h body /dev/stdin)
		)
		printf '\n'
		;;
	esac

	case "$0" in
	*mbnc*) ;;
	*)
		if [ -f "$MBLAZE/signature" ]; then
			SIGNATURE="$MBLAZE/signature"
		elif [ -f ~/.signature ]; then
			SIGNATURE="$HOME/.signature"
		fi

		if [ -n "$SIGNATURE" ]; then
			printf '%s\n' '-- '
			cat "$SIGNATURE"
		fi
	esac
} >$draft

automime=
c=e
while :; do
	case "$c" in
	s|send)
		case "$(mhdr -h newsgroups "$draft")" in
			*gmane.*) sendmail="mblow -s news.gmane.org";;
			*.*) sendmail="mblow";;
		esac

		resent="$(maddr -h resent-to "$draft")"
		case "$resent" in
		?*)
			sendmail=$(mhdr -h sendmail "$MBLAZE/profile")
			sendmail="${sendmail:-sendmail} -- $resent"
			;;
		esac

		if [ -e $draftmime ]; then
			if [ $draft -ot $draftmime ] || [ "$automime" -eq 1 ]; then
				stampdate $draftmime
				if $sendmail <$draftmime; then
					if [ "$outbox" ]; then
						mv $draftmime $draft
						mflag -d -S $draft
					else
						rm $draft $draftmime
					fi
				else
					printf '%s\n' "mcom: $sendmail failed, kept draft $draft"
					exit 2
				fi
			else
				printf 'mcom: re-run mmime first.\n'
				c=
			fi
		else
			if mmime -c <$draft; then
				stampdate $draft
				if $sendmail <$draft; then
					if [ "$outbox" ]; then
						mflag -d -S $draft
					else
						rm $draft
					fi
				else
					printf '%s\n' "mcom: $sendmail failed, kept draft $draft"
					exit 2
				fi
			else
				printf '%s\n' "mcom: message needs to be MIME-encoded first."
				c=
			fi
		fi

		case "$0" in
			*mrep*) mflag -R -- "$1" ;;
			*mbnc*) mflag -P -- "$1" ;;
			*mfwd*) mflag -P -- "$@" ;;
		esac

		exit 0
		;;
	c|cancel)
		stampdate $draft
		printf '%s\n' "mcom: cancelled draft $draft"
		exit 1
		;;
	m|mime)
		do_mime
		mshow -t $draftmime
		c=
		;;
	e|edit)
		c=
		if ! ${EDITOR:-vi} $draft; then
			c=c
		fi
		stripempty $draft
		if mmime -c <$draft && ! needs_multipart $draft; then
			automime=
		else
			automime=1
			do_mime
		fi
		;;
	d|delete)
		rm -i $draft
		if ! [ -f $draft ]; then
			rm -f $draftmime
			printf '%s\n' "mcom: deleted draft $draft"
			exit 0
		fi
		c=
		;;
	sign)
		msign $draft >$draftmime
		mshow -t $draftmime
		c=
		;;
	encrypt)
		mencrypt $draft >$draftmime
		mshow -t $draftmime
		c=
		;;
	show)
		if [ -e $draftmime ]; then
			mshow "$draftmime"
		else
			mshow "$draft"
		fi
		c=
		;;
	*)
		printf 'What now? (%s[s]end, [c]ancel, [d]elete, [e]dit, [m]ime, sign, encrypt) ' "${automime:+mime and }"
		read -r c
		;;
	esac
done