summaryrefslogtreecommitdiffstats
path: root/quilt/mail.in
blob: 627fead52882388c890dff969eec15adad73a3dd (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#! @BASH@

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

: ${EDITOR:=vi}

# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r $QUILT_DIR/scripts/patchfns ]
	then
		echo "Cannot read library $QUILT_DIR/scripts/patchfns" >&2
		exit 1
	fi
	. $QUILT_DIR/scripts/patchfns
fi

usage()
{
	printf $"Usage: quilt mail {--mbox file|--send} [-m text] [--prefix prefix] [--sender ...] [--from ...] [--to ...] [--cc ...] [--bcc ...] [--subject ...]\n"
	if [ x$1 = x-h ]
	then
		printf $"
Create mail messages from all patches in the series file, and either store
them in a mailbox file, or send them immediately. The editor is opened
with a template for the introduction. Please see %s for details.

-m text
	Text to use as the text in the introduction. When this option is
	used, the editor will not be invoked, and the patches will be
	processed immediately.

--prefix prefix
	Use an alternate prefix in the bracketed part of the subjects
	generated. Defaults to \`patch'.

--mbox file
	Store all messages in the specified file in mbox format. The mbox
	can later be sent using formail, for example.

--send
	Send the messages directly.

--sender
	The envelope sender address to use. The address must be of the form
	\`user@domain.name'. No display name is allowed.

--from, --subject
	The values for the From and Subject headers to use. If no --from
	option is given, the value of the --sender option is used.

--to, --cc, --bcc
	Append a recipient to the To, Cc, or Bcc header.
" "@DOCSUBDIR@/README.MAIL"
		exit 0
	else
		exit 1
	fi
}

msgid()
{
	local timestamp=$(date --utc "+%Y%m%d%H%M%S.%N")
	echo "$timestamp@${opt_sender#*@}"
}

process_mail()
{
	local tmpfile=$(gen_tempfile)

	cat > $tmpfile
	set -- $($QUILT_DIR/scripts/edmail --charset $opt_charset \
				  --extract-recipients To \
				  --extract-recipients Cc \
				  --extract-recipients Bcc \
				  < $tmpfile)
	if [ -n "$opt_send" ]; then
		echo sendmail ${QUILT_SENDMAIL_ARGS--f "$opt_sender"} "$@"
		$QUILT_DIR/scripts/edmail --charset $opt_charset \
				 --remove-header Bcc "$@" < $tmpfile \
		| sendmail ${QUILT_SENDMAIL_ARGS--f "$opt_sender"} "$@"
	else
		local from_date=$(date "+%a %b %e %H:%M:%S %Y")
		echo "From $opt_sender $from_date"
		sed -e 's/^From />From /' $tmpfile
		echo
	fi
	rm -f $tmpfile
}

options=`getopt -o m:h --long from:,to:,cc:,bcc:,subject: \
		       --long send,mbox:,charset:,sender: \
		       --long prefix: -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

opt_prefix=patch
while true
do
	case "$1" in
	-m)
		opt_message=$2
		shift 2 ;;
	--sender)
		opt_sender=$2
		shift 2 ;;
	--from)
		opt_from=$2
		shift 2 ;;
	--to)
		opt_to[${#opt_to[@]}]=$2
		shift 2 ;;
	--cc)
		opt_cc[${#opt_cc[@]}]=$2
		shift 2 ;;
	--bcc)
		opt_bcc[${#opt_bcc[@]}]=$2
		shift 2 ;;
	--subject)
		opt_subject=$2
		shift 2 ;;
	--prefix)
		opt_prefix=$2
		shift 2 ;;
	--send)
		opt_send=1
		shift ;;
	--mbox)
		opt_mbox=$2
		shift 2 ;;
	--charset)
		opt_charset=$2
		shift 2 ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -gt 0 -o \( -z "$opt_send" -a -z "$opt_mbox" \) ]
then
	usage
fi

if [ -z "$opt_sender" ]; then
	hostname=$(hostname -f 2>/dev/null)
	if [ "$hostname" = "${hostname/.}" ]
	then
		hostname=$(hostname)
	fi
	opt_sender="${LOGNAME:-$(whoami)}@$hostname"
	case "$opt_sender" in
	*@*.*)	;;
	*)
		echo $"\
Could not determine the envelope sender address. Please use --sender." >&2
		exit 1
		;;
	esac
fi

if [ -z "$opt_charset" ]; then
	case "${LC_ALL:-$LANG}" in
	*.UTF-8)
		opt_charset=UTF-8
		;;
	*)
		opt_charset=ISO-8859-15
		;;
	esac
fi

if [ "$(type -t quilt_mail_patch_filter 2> /dev/null)" != function ]; then
	quilt_mail_patch_filter() {
		local patch=$(cat)
		local header=$(echo "$patch" | patch_header) body
		local -a mh

		# Does this patch have a Subject: line?
		local subject=$(echo $(echo "$header" \
				| sed -n -e '/^$/q' \
					 -e $'s/^Subject:[ \\t]//ip'))
		if [ -n "$subject" ]
		then
			mh=( "Replace-Subject: $subject" )
			body=$(echo "$patch" | awk '
				in_body		{ print }
				/^$/		{ in_body = 1 }
				')
		fi

		# Does this patch have DESC // subject // EDESC?
		if [ ${#mh[@]} -eq 0 ]
		then
			local desc=$(echo $(echo "$header" | awk '
				/^EDESC\>/	{ desc = 0 }
				desc		{ print }
				/^DESC\>/	{ desc = 1 }
				'))
			if [ -n "$desc" ]
			then
				mh=( "Replace-Subject: $desc" )
				body=$(echo "$patch" | awk '
					/^DESC/		{ desc = 1 }
					! desc		{ print }
					/^EDESC/	{ desc = 0 }
					')
			fi
		fi

		# Does the first paragraph look like a mail header?
		#if [ ${#mh[@]} -eq 0 ]
		#then
		#	if echo "$header" | awk '
		#		/^$/	{ exit (!mh || not_mh) }
		#		END	{ exit (!mh || not_mh) }
		#		{ if (tolower($0) ~ \
		#		      /^(from|to|reply-to|date|cc):[ \t]/)
		#		    mh = 1
		#		  else
		#		    not_mh = 1
		#		}'
		#	then
		#		return 1
		#	fi
		#fi

		# Is the first paragraph short enough to be used as the subject?
		if [ ${#mh[@]} -eq 0 ]
		then
			local para=$(echo $(echo "$header" \
					    | sed -e $'/^[ \t]*$/q'))
			if [ ${#para} -gt 0 -a ${#para} -lt 150 ]
			then
				mh=( "Replace-Subject: $para" )
				body="$(echo "$patch" | awk '
					in_body		{ print }
					/^[ \t]*$/	{ in_body = 1 }
					')"
			fi
		fi

		#if ${#mh[@]} -eq 0 ]
		#then
		#	# Use the patch name as the subject.
		#	mh=( "Replace-Subject: $1" )
		#	body=$patch
		#fi

		if [ ${#mh[@]} -eq 0 ]
		then
			return 1
		fi

		mh[0]=$(echo "${mh[0]}" \
			| sed -e $'s/\\[[^]]*\\][ \t]*//gi' \
			      -e $'s/\<\(fwd\|fw\|re\|aw\|tr\):[ \t]//gi')

		# Add recipients defined by some recognized keywords
		local saved_IFS=$IFS; IFS=$'\n'
		mh=( "${mh[@]}" 
		     $(echo "$header" \
		       | sed -n -e "/\<${LOGNAME:-$(whoami)}@/d" \
				-e 's/^\(To\|Cc\):/Recipient-\1:/ip' \
				-e 's/^\(Signed-off-by\|Acked-by\):/Recipient-Cc:/ip') )
		IFS=$saved_IFS

		printf "%s\n" "${mh[@]}"
		echo
		echo "$body"
	}
fi

patches=( $(cat_series) )
total=${#patches[@]}

tmpdir=$(gen_tempfile -d)
trap "rm -rf $tmpdir" EXIT

for patch in "${patches[@]}"
do
	mkdir -p "$tmpdir/$(dirname "$patch")"
	cat_file "$(patch_file_name "$patch")" \
	| quilt_mail_patch_filter "$patch" > "$tmpdir/$patch"
	status=${PIPESTATUS[1]}

	subject=$(echo $(sed -n -e '/^$/q' \
				-e $'s/^Replace-Subject:[ \t]*//ip' \
			     "$tmpdir/$patch"))
	if [ $status -ne 0 -o -z "$subject" ]
	then
		printf \
$"Unable to extract a subject header from %s\n" "$(print_patch "$patch")" >&2
		rm -rf $tmpdir
		exit 1
	fi
	subjects[${#subjects[@]}]="$patch"$'\t'"$subject"
done

dup_subjects=( $(
	printf "%s\n" "${subjects[@]}" \
	| sort -k2 \
	| awk '{ patch = $1 ; sub(/^[^\t]+\t/, "");
		 if ($0 in subjects) {
			if (subjects[$0] != "")
				print subjects[$0];
			print patch;
			subjects[$0] = "";
		}
		else subjects[$0] = patch }' \
	| while read patch; do
		echo "$(print_patch "$patch")"
	  done
	) )
if [ ${#dup_subjects[@]} -ne 0 ]; then
	printf $"Patches %s have duplicate subject headers.\n" \
	       "$(set -- "${dup_subjects[*]/%/, }"; echo ${1%, })"
	exit 1
fi

introduction="$(gen_tempfile)"
(
	cat <<-EOF
	Message-Id: <$(msgid)>
	User-Agent: quilt/@VERSION@-@RELEASE@
	Date: $(date --rfc-822)
	From: ${opt_from:-$opt_sender}
	To: $(IFS=,; echo "${opt_to[*]}")
	Cc: $(IFS=,; echo "${opt_cc[*]}")
	Bcc: $(IFS=,; echo "${opt_bcc[*]}")
	Subject-Prefix: [$opt_prefix @num@/@total@]
	Subject: $opt_subject

	EOF
	if [ -n "$opt_message" ]
	then
		echo "$opt_message"
		echo
	fi
	echo "--"
	[ -r $HOME/.signature ] && cat $HOME/.signature
) | $QUILT_DIR/scripts/edmail --charset $opt_charset > $introduction

if [ -z "$opt_message" ]
then
	if ! $EDITOR $introduction; then
		rm -f $introduction
		exit 1
	fi
fi

if [ -z "$(sed -n -e '/^$/q' \
		  -e $'s/^Subject:[ \t]*//ip' \
	       $introduction)" ]
then
	if [ -z "$opt_message" ]
	then
		printf $"Introduction has no subject header (saved as %s)\n" \
			"$introduction" >&2
	else
		printf $"Introduction has no subject header\n"
		rm -f $introduction
	fi
	exit 1
fi

if [ -n "$opt_mbox" ]; then
	exec 1> $opt_mbox
fi

subject_prefix=$(sed -ne $'s/^Subject-Prefix:[ \t]*//p' $introduction)
[ -n "$subject_prefix" ] && subject_prefix="$subject_prefix "

subject_prefix=${subject_prefix//\'/\'\'}
subject_prefix=${subject_prefix//\//\\\/}
p=${subject_prefix//@num@/$(printf %0*d ${#total} 0)}
p=${p//@total@/$total}
sed -e $'s/^\\(Subject:[ \t]\\)/\\1'"$p"'/' \
    -e '/^Subject-Prefix:/d' \
$introduction \
| $QUILT_DIR/scripts/edmail --charset $opt_charset \
		  --remove-empty-headers To Cc Bcc \
| process_mail

if [ -n "$opt_mbox" ]; then
	exec 1>> $opt_mbox
fi

# Remember the timestamp of the last message sent. For each message,
# increment the timestamp by one second and wait with sending until
# that time has arrived. This allows MUAs to show the messages in the
# correct order.
last_ts=$(date '+%s' -d "$(sed -ne $'s/^Date:[ \t]*//p' $introduction)")

num=1
for patch in "${patches[@]}"; do
	body=$tmpdir/$patch
	#echo -n '.' >&2
	# Timestamps that are a few seconds in the future don't hurt usually
	#while [ $(date '+%s') -le $last_ts ]; do
	#	sleep 1
	#done
	((last_ts++))
	new_date="$(date --rfc-822 -d "1970/01/01 UTC $last_ts seconds")"

	modify="$(awk '
	sub(/^Recipient-/, "")	{ r = $0
				  sub(/:.*/, "", r)
				  s = $0
				  sub(/^[^:]*:[ \t]*/, "", s)
				  gsub(/'\''/, "'\'\''", s)
				  print "--add-recipient " r "='\''" s "'\''"
				}
	sub(/^Replace-/, "")	{ r = $0
				  sub(/:.*/, "", r)
				  s = $0
				  sub(/^[^:]*:[ \t]*/, "", s)
				  gsub(/'\''/, "'\'\''", s)
				  print "--replace-header " r "='\''" s "'\''"
				}
	' $body)"
	p=${subject_prefix//@num@/$(printf %0*d ${#total} $num)}
	p=${p//@total@/$total}
	(	echo "Message-Id: <$(msgid)>"
		sed -n -e '/^$/q' \
		    -e 's/^Message-Id:/References:/' \
		    -e p \
		    $introduction
		echo "Content-Disposition: inline; filename=$patch"
		sed -n -e '/^$/q' \
		    -e '/^Recipient-.*:/d' \
		    -e '/^Replace-.*:/d' \
		    -e p \
		    $body
		awk '
		/^$/	{ in_body = 1 }
		in_body	{ print }
		' $body
		echo -e '\n--'
		[ -r $HOME/.signature ] && cat $HOME/.signature
	) | eval $QUILT_DIR/scripts/edmail --charset $opt_charset \
		--replace-header Date="\"$new_date\"" \
		To Cc Bcc \
		$modify \
	| sed -e $'s/^\\(Subject:[ \t]\\)/\\1'"$p"'/' \
	      -e '/^Subject-Prefix:/d' \
	| $QUILT_DIR/scripts/edmail --remove-empty-headers \
	| process_mail

	if [ ${PIPESTATUS[1]} -ne 0 ]; then
		printf $"Introduction saved as %s\n" "$introduction" >&2
		exit 1
	fi

	# If the character set is UTF-8, check for invalid byte
	# sequences.

	#content_length=${#body}
	#if [ -n "$(echo "$body" | tr -d '\0-\177')" ]; then
	#	charset=UTF-8
	#fi
	# Content-Transfer-Encoding: 7bit
	# Content-Transfer-Encoding: 8bit
	# Content-Type: text/plain; charset=ISO-8859-1
	# Content-Type: text/plain; charset=UTF-8

	((num++))
done
rm -f $introduction
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh