File: mail.in

package info (click to toggle)
quilt 0.65-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,980 kB
  • sloc: sh: 5,898; perl: 1,256; lisp: 492; makefile: 430
file content (709 lines) | stat: -rw-r--r-- 16,340 bytes parent folder | download | duplicates (2)
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#! @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:=sensible-editor}

# 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] [-M file] [--prefix prefix] [--sender ...] [--from ...] [--to ...] [--cc ...] [--bcc ...] [--subject ...] [--reply-to message] [--charset ...] [--signature file] [first_patch [last_patch]]\n"
	if [ x$1 = x-h ]
	then
		printf $"
Create mail messages from a specified range of patches, or 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.
When specifying a range of patches, a first patch name of \`-' denotes the
first, and a last patch name of \`-' denotes the last patch in the series.

-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.

-M file
	Like the -m option, but read the introduction from file.

--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.

--charset
	Specify a particular message encoding on systems which don't use
	UTF-8 or ISO-8859-15. This character encoding must match the one
	used in the patches.

--signature file
	Append the specified signature to messages (defaults to ~/.signature
	if found; use \`-' for no signature).

--reply-to message
	Add the appropriate headers to reply to the specified message.
" "@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_address#*@}"
}

# Extract RFC 2822 compliant header values, including Long Header Fields,
# from messages

extract_header_value()
{
      local header=$1

      # Long Header Fields may span multiple lines, in which case CRLF
      # is followed by space or tab (RFC 2822)
      sed -ne "/^${header}/I,/^[^[:blank:]]/ { /^${header}/I { s/^${header}//I; p; n; }; /^[^[:blank:]]/q; /^$/q; p; }"
}

# See RFC 2822 Internet Message Format for how the In-Reply-To and
# References headers are generated...

in_reply_to_header()
{
	local message=$1 message_id

	message_id=$(extract_header_value Message-ID: < "$message")
	message_id=${message_id# }
	[ -n "$message_id" ] && echo "In-Reply-To: $message_id"
}

references_header()
{
	local message=$1 message_id references in_reply_to

	message_id=$(extract_header_value Message-ID: < "$message")
	message_id=${message_id# }

	references=$(extract_header_value References: < "$message")
	references=${references# }
	if [ -z "$references" ]
	then
		in_reply_to=$(extract_header_value In-Reply-To: < "$message")
		in_reply_to=${in_reply_to# }
		if [ -n "$in_reply_to" ]
		then
			case "$in_reply_to" in
			*@*@*)
				;;
			*)	references=$in_reply_to
				;;
			esac
		fi
	fi
	if [ -z "$references" ]
	then
		references=$message_id
	elif [ -n "$message_id" ]
	then
		references="$references"$'\n '"$message_id"
	fi
	[ -n "$references" ] && echo "References: $references"
}

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 ${QUILT_SENDMAIL:-sendmail} \
			${QUILT_SENDMAIL_ARGS--f "$opt_sender"} "$@"
		$QUILT_DIR/scripts/edmail --charset $opt_charset \
				 --remove-header Bcc < $tmpfile \
		| ${QUILT_SENDMAIL:-sendmail} \
			${QUILT_SENDMAIL_ARGS--f "$opt_sender"} "$@"
	else
		local from_date=$(LC_ALL=POSIX date "+%a %b %e %H:%M:%S %Y")
		echo "From $opt_sender_address $from_date"
		sed -e 's/^From />From /' $tmpfile
		echo
	fi
	rm -f $tmpfile
}

join_lines()
{
	awk '
		BEGIN { RS="\n\n" }
		{ gsub(/[ \t\n]+/, " ")
		  sub(/ $/, "")
		  sub(/^ /, "")
		  print }
	'
}

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

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

opt_prefix=patch
if [ -r $HOME/.signature ]
then
	opt_signature="$(< $HOME/.signature)"
fi

while true
do
	case "$1" in
	-m)
		if [ -n "$opt_message" ]
		then
			echo $"Introduction message already specified" >&2
			exit 1
		fi
		opt_message=$2
		shift 2 ;;
	-M)
		if [ -n "$opt_message" ]
		then
			echo $"Introduction message already specified" >&2
			exit 1
		fi
		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 ;;
	--reply-to)
		opt_reply_to=$2
		shift 2 ;;
	--signature)
		if [ "$2" = - ]
		then
			opt_signature=
		else
			opt_signature=$(< "$2")
		fi
		shift 2 ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

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

if [ $# -ge 1 ]
then
	if [ "$1" = - ]
	then
		first_patch="$(find_first_patch)" || exit 1
	else
		first_patch="$(find_patch "$1")" || exit 1
	fi

	if [ $# -ge 2 ]
	then
		if [ "$2" = - ]
		then
			last_patch="$(find_last_patch)" || exit 1
		else
			last_patch="$(find_patch "$2")" || exit 1
		fi
	else
		last_patch=$first_patch
	fi
fi

if [ -z "$opt_sender" ]
then
	if [ -e /etc/mailname ]
	then
		hostname=$(< /etc/mailname)
	else
		hostname=$(hostname -f 2>/dev/null)
	fi

	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
opt_sender_address=$(echo "$opt_sender" | sed -re 's:.*<([^<>]+)>.*:\1:')

if [ -z "$opt_charset" ]
then
	case "${LC_ALL:-$ORIGINAL_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 tmpdir=$(gen_tempfile -d)
		cat > $tmpdir/patch
		patch_header < $tmpdir/patch > $tmpdir/header
		local subject
		local -a mh

		# Does this patch have a Subject: line?
		subject=$(extract_header_value Subject: < $tmpdir/header)
		if [ -n "$subject" ]
		then
			awk '
			in_body		{ print }
			/^$/		{ in_body = 1 }
			' $tmpdir/patch > $tmpdir/body
		fi

		# Does this patch have a Description: block?
		if [ -z "$subject" ]
		then
			subject=$(formail -x Description: < $tmpdir/header |head -n 1)
			if [ -n "$subject" ]
			then
				# Get all dep3 pseudo-headers (but the first line of the description, already used as a subject)
				# into the body of the generated mail, plus the patch.
				sed -e 's/^Description:.*$/Description:/' $tmpdir/patch > $tmpdir/body
			fi
		fi

		# Does this patch have DESC // subject // EDESC?
		if [ -z "$subject" ]
		then
			local desc=$(awk '
				/^EDESC/	{ desc = 0 }
				desc		{ print }
				/^DESC/	{ desc = 1 }
				' $tmpdir/header)
			if [ -n "$desc" ]
			then
				subject=$(echo "$desc" | join_lines)
				awk '
				/^DESC/		{ desc = 1 }
				! desc		{ print }
				/^EDESC/	{ desc = 0 }
				' $tmpdir/patch > $tmpdir/body
			fi
		fi

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

		if [ -z "$subject" ]
		then
			rm -rf $tmpdir
			return 1
		fi

		subject=$(echo "$subject" \
			  | sed -e $'s/^\\(\\(\\[[^]]*\\]\\|fwd:\\|fw:\\|re:\\|aw:\\|tr:\\)[ \t]*\\)*//i')
		echo "Replace-Subject: $subject"

		# Add recipients defined by some recognized keywords

		# In Signed-off-by: and Acked-by: lines, try to recognize email
		# addresses that contain commas and add quotes, e.g.,
		#   Last, First <email>    =>    "Last, First" <email>

		set -- Signed-off-by Acked-by Suggested-by Reviewed-by Requested-by Reported-by Tested-by Cc
		set -- "$*"
		set -- ${*// /\\|}

		sed -n -e "/\<${LOGNAME:-$(whoami)}@/d" \
		       -e 's/^\(\(To\|'"$*"'\):[ '$'\t'']*\)\([^"]*\(,[^"]*\)\+[^" '$'\t'']\)\([ '$'\t'']*<.*>\)/\1"\3"\5/I' \
		       -e 's/^To:\(.*@.*\)/Recipient-To:\1/Ip' \
		       -e 's/^\('"$*"'\):\(.*@.*\)/Recipient-Cc:\2/Ip' \
		    $tmpdir/header

		echo
		cat $tmpdir/body
		rm -rf $tmpdir
	}
fi

if [ -n "$first_patch" ]
then
	if [ -n "$last_patch" ]
	then
		set -- $(patches_before "$last_patch") "$last_patch"
		while [ $# -ne 0 ]
		do
			[ "$1" = "$first_patch" ] && break
			shift
		done
		if [ $# -eq 0 ]
		then
			printf $"Patch %s not applied before patch %s\n" \
			       "$(print_patch $first_patch)" \
			       "$(print_patch $last_patch)" >&2
			exit 1
		fi
		patches=( "$@" )
	else
		patches=( "$first_patch" $(patches_after "$first_patch") )
	fi
else
	patches=( $(cat_series) )
fi
total=${#patches[@]}

tmpdir=$(gen_tempfile -d)
add_exit_handler "rm -rf $tmpdir"

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=$(extract_header_value Replace-Subject: < "$tmpdir/$patch" | join_lines)
	if [ $status -ne 0 -o -z "$subject" ]
	then
		subject=$(formail -x Description: < "$tmpdir/$patch" | head -n 1)
		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
	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
		print_patch $patch
	  done
	) )
if [ ${#dup_subjects[@]} -ne 0 ]
then
	printf $"Patches %s have duplicate subject headers.\n" \
	       "$(array_join ", " "${dup_subjects[@]}")"
	exit 1
fi

if [ -n "$opt_reply_to" ]
then
	if [ ! -e "$opt_reply_to" ]
	then
		printf $"File %s does not exist\n" "$opt_reply_to"
		exit 1
	fi

	if [ -z "$opt_subject" ]
	then
		opt_subject="Re: $(extract_header_value Subject: < "$opt_reply_to" \
			      | sed -e 's/^ *\([rR][eE]: *\)*//')"
	fi
fi

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

	if [ -n "$opt_reply_to" ]
	then
		in_reply_to_header "$opt_reply_to"
		references_header "$opt_reply_to"
	fi

	cat <<-EOF
	Subject-Prefix: [$opt_prefix @num@/@total@]
	Subject: $opt_subject

	EOF
	if [ -n "$opt_message" ]
	then
		echo "$opt_message"
		echo
	fi
	if [ -n "$opt_signature" ]
	then
		echo "-- "
		echo "$opt_signature"
	fi
) | $QUILT_DIR/scripts/edmail --charset $opt_charset > $introduction

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

subject=$(extract_header_value Subject: < $introduction | join_lines)
if [ -z "$subject" ]
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=$(extract_header_value Subject-Prefix: < $introduction | join_lines)
[ -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 '
	in_header	{ if (/^[ \t]/) {
			    headers[n] = headers[n] "\n" $0
			    next }
			  in_header = 0 }
	sub(/^Recipient-/, "")	{ headers[++n] = $0
				  options[n] = "--add-good-recipient"
				  in_header = 1
				  next }
	sub(/^Replace-/, "")	{ headers[++n] = $0
				  options[n] = "--replace-header"
				  in_header = 1
				  next }
	END			{ for(n = 1; n in headers; n++) {
				    r = headers[n]
				    sub(/:.*/, "", r)
				    s = headers[n]
				    sub(/^[^:]*:[ \t]*/, "", s)
				    gsub(/'\''/, "'\'\''", s)
				    print options[n] " " r "='\''" s "'\'' \\" } }
	' $body)"
#	echo "$modify" | sed -e 's/^/>> /' >&2
	p=${subject_prefix//@num@/$(printf %0*d ${#total} $num)}
	p=${p//@total@/$total}

	# Make pipes fail if any of their commands fail (requires bash 3):
	set -o pipefail

	( (	echo "Message-Id: <$(msgid)>"
		awk '
		    /^$/	{ exit }
		    tolower($0) !~ /^(message-id|references|in-reply-to):/ \
				{ print }
		' $introduction
		references_header $introduction
		echo "MIME-Version: 1.0"
		echo "Content-Type: text/plain; charset=$opt_charset"
		echo "Content-Disposition: inline; filename=$patch"
		awk '
		    kill_header { if (/^[ \t]/) next ; kill_header = 0 }
		    !in_body && tolower($0) ~ /^(recipient|replace)-.*:/ \
				{ kill_header = 1 ; next }
		    /^$/	{ in_body = 1 }
				{ print }
		    ' $body
		echo
		#if [ -n "$opt_signature" ]
		#then
		#	echo '-- '
		#	echo "$opt_signature"
		#fi
	) | 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 ) 2> $tmpdir/err

	status=$?
	if [ -s $tmpdir/err ]
	then
		sed -e "s/^/${patch//\//\\/}: /" <  $tmpdir/err >&2
	fi
	if [ $status -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