Added -header-field switch to mhmail.
authorDavid Levine <levinedl@acm.org>
Tue, 19 Jun 2012 02:28:26 +0000 (21:28 -0500)
committerDavid Levine <levinedl@acm.org>
Tue, 19 Jun 2012 02:28:26 +0000 (21:28 -0500)
man/mhmail.man
test/post/test-mhmail
uip/mhmail

index fb271e1..3abd6d3 100644 (file)
@@ -17,6 +17,8 @@ mhmail \- send or read mail
 \&...]
 .RB [ \-from
 .IR addr ]
+.RB [ \-header\-field
+.IR name:body ]
 .RB [ \-subject
 .IR subject ]
 .RB [ \-resent ]
@@ -80,7 +82,11 @@ will be resent, as
 .B dist
 would do.  For example:
 .PP
-    mhmail u2@example.com -resent < `mhpath cur`
+.RS 5
+.nf
+mhmail u2@example.com -resent < `mhpath cur`
+.fi
+.RE
 .PP
 By default,
 .B mhmail
@@ -117,6 +123,27 @@ will fill\-in the \*(lqSender:\*(rq
 header correctly.
 .PP
 The
+.B \-header\-field
+.I name:body
+switch adds a header field with the specified name and body to the
+message.  There need not be a space after the colon that separates the
+name and body in the argument to this switch.  It is usually best to
+enclose the argument in quotes to protect it from the shell.  The
+.B \-header\-field
+switch may be given multiple times to add multiple header fields,
+for example,
+.PP
+.RS 5
+.nf
+mhmail -from sender@example.com \\
+-header\-field 'MIME-Version:1.0' \\
+-header\-field 'Content-Type:text/plain; charset=utf-8' \\
+-header\-field 'Content-Transfer-Encoding:8bit' \\
+recipient@example.com
+.fi
+.RE
+.PP
+The
 .B \-profile
 switch instructs
 .B mhmail
index 0d0943e..5cabc1a 100755 (executable)
@@ -77,13 +77,14 @@ Usage: mhmail [addrs ... [switches]]
   -b(ody) text
   -c(c) addrs ...
   -f(rom) addr
+  -hea(der-field) name:field [-hea(der-field) name:field] ...
   -su(bject) text
   -r(esent)
   -pr(ofile)
   -se(nd)
   -nose(nd)
   -v(ersion)
-  -h(elp)
+  -hel(p)
   and all post(8)/send(1) switches
 EOF
 
@@ -603,11 +604,11 @@ test_mhmail "$expected" '-from sender@localhost' -b "here's some text
 # Verified same behavior as compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
-MAIL FROM:<recipient@example.com>
+MAIL FROM:<orig_recipient@example.com>
 RCPT TO:<recipient@example.com>
 DATA
 Resent-To: recipient@example.com
-Resent-From: recipient@example.com
+Resent-From: orig_recipient@example.com
 To: recipient@example.com
 From: sender@localhost
 Date:
@@ -618,7 +619,7 @@ please resend this message, #1
 QUIT
 EOF
 
-test_mhmail "$expected" '-from recipient@example.com -resent' \
+test_mhmail "$expected" '-from orig_recipient@example.com -resent' \
   -b 'To: recipient@example.com
 From: sender@localhost
 Date: Sat Jun 16 18:35:15 -0500
@@ -631,14 +632,14 @@ please resend this message, #1'
 # Not supported by compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
-MAIL FROM:<recipient@example.com>
+MAIL FROM:<orig_recipient@example.com>
 RCPT TO:<recipient@example.com>
 DATA
 To: recipient@example.com
 From: sender@localhost
 Date:
 Resent-To: recipient@example.com
-Resent-From: recipient@example.com
+Resent-From: orig_recipient@example.com
 Resent-Date:
 
 please resend this message, #2
@@ -647,7 +648,7 @@ QUIT
 EOF
 
 test_mhmail "$expected" \
-  '-from recipient@example.com -resent -profile -nomsgid' \
+  '-from orig_recipient@example.com -resent -profile -nomsgid' \
   '|' 'To: recipient@example.com
 From: sender@localhost
 Date: Sat Jun 16 18:35:15 -0500
@@ -661,14 +662,14 @@ please resend this message, #2'
 # Not supported by compiled mhmail.
 cat > "$expected" <<EOF
 EHLO nosuchhost.example.com
-MAIL FROM:<recipient@example.com>
+MAIL FROM:<orig_recipient@example.com>
 RCPT TO:<recipient@example.com>
 DATA
 To: recipient@example.com
 From: sender@localhost
 Date:
 Resent-To: recipient@example.com
-Resent-From: recipient@example.com
+Resent-From: orig_recipient@example.com
 Resent-Date:
 
 please resend this message, #3
@@ -677,7 +678,7 @@ QUIT
 EOF
 
 test_mhmail "$expected" \
-  '-from recipient@example.com -resent -profile -nomsgid' \
+  '-from orig_recipient@example.com -resent -profile -nomsgid' \
   -b 'To: recipient@example.com
 From: sender@localhost
 Date: Sat Jun 16 18:35:15 -0500
@@ -687,4 +688,56 @@ please resend this message, #3'
 [ ${failed:-0} -eq 0 ] || exit ${failed:-0}
 
 
+# check -header-field.
+# Not supported by compiled mhmail.
+cat > "$expected" <<EOF
+EHLO nosuchhost.example.com
+MAIL FROM:<sender@example.com>
+RCPT TO:<recipient@example.com>
+DATA
+To: recipient@example.com
+From: sender@example.com
+User-Agent: nmh
+Date:
+
+with added header field
+.
+QUIT
+EOF
+
+test_mhmail "$expected" \
+  '-from sender@example.com -header-field User-Agent:nmh' \
+  -b 'with added header field'
+
+[ ${failed:-0} -eq 0 ] || exit ${failed:-0}
+
+
+# check multiple -header-fields.
+# Not supported by compiled mhmail.
+cat > "$expected" <<EOF
+EHLO nosuchhost.example.com
+MAIL FROM:<sender@example.com>
+RCPT TO:<recipient@example.com>
+DATA
+To: recipient@example.com
+From: sender@example.com
+MIME-Version: 1.0
+Content-Type: text/plain;charset=utf-8
+Content-Transfer-Encoding: 8bit
+Date:
+
+with added header fields
+.
+QUIT
+EOF
+
+test_mhmail "$expected" \
+  "-from sender@example.com -header-field MIME-Version:1.0 \
+-header-field Content-Type:text/plain;charset=utf-8 \
+-header-field Content-Transfer-Encoding:8bit" \
+  -b 'with added header fields'
+
+[ ${failed:-0} -eq 0 ] || exit ${failed:-0}
+
+
 exit ${failed:-0}
index 6e03f15..235eeba 100755 (executable)
 #   (with -profile) options.
 # * Optionally (with -profile) obeys the users profile, including
 #   AliasFile and send entries.
-# * Adds -nosend option, which displays the draft but does not send it.
+# * Adds -send/-nosend and -header-field options.
 # * Drops support for undocumented -queue option.
 #
 # To do:
-# * add -header-field name:body switch
 # * add -attach file ... switch
 
 usage='Usage: mhmail [addrs ... [switches]]
@@ -26,13 +25,14 @@ usage='Usage: mhmail [addrs ... [switches]]
   -b(ody) text
   -c(c) addrs ...
   -f(rom) addr
+  -hea(der-field) name:field [-hea(der-field) name:field] ...
   -su(bject) text
   -r(esent)
   -pr(ofile)
   -se(nd)
   -nose(nd)
   -v(ersion)
-  -h(elp)
+  -hel(p)
   and all post(8)/send(1) switches'
 
 bindir=`dirname $0`
@@ -50,6 +50,8 @@ else
   ccarg=0
   from=
   fromarg=0
+  headerfieldlist=
+  headerfieldarg=0
   subject=
   subjectarg=0
   resent=0
@@ -59,7 +61,7 @@ else
   sendsw=1
   for arg in "$@"; do
     case "${arg}" in
-      -*) switcharg=0
+      -*) switcharg=0; headerfieldarg=0 ;;
     esac
 
     case "${arg}" in
@@ -71,7 +73,8 @@ else
       -b|-bo|-bod|-body) bodyarg=1 ;;
       -c|-cc) ccarg=1 ;;
       -f|-fr|-fro|-from) fromarg=1 ;;
-      -h|-he|-hel|-help) printf "%s\n" "${usage}"; exit ;;
+      -hea|-head|-heade|-header|-header-|-header-f|-header-fi|-header-fie|-header-fiel|-header-field) headerfieldarg=1 ;;
+      -hel|-help) printf "%s\n" "${usage}"; exit ;;
       -nose|-nosen|-nosend) sendsw=0 ;;
       -p|-pr|-pro|-prof|-profi|-profil|-profile) use_send=1 ;;
       -resend) printf "mhmail: did you mean -resent instead of -resend?\n" 1>&2
@@ -85,14 +88,25 @@ else
       -*) postsendargs="${postsendargs:+${postsendargs} }${arg}"; switcharg=1 ;;
       *) if [ ${bodyarg} -eq 1 ]; then
            body="${arg}
-"; bodyarg=0
+"
+           bodyarg=0
            #### Allow -body "" by using just a newline for the body.
            [ "${body}"x = x ]  &&  body='
 '
          elif [ ${fromarg} -eq 1 ]; then
-           from="${arg}"; fromarg=0
+           from="${arg}"
+           fromarg=0
          elif [ ${subjectarg} -eq 1 ]; then
-           subject="${arg}"; subjectarg=0
+           subject="${arg}"
+           subjectarg=0
+         elif [ ${headerfieldarg} -eq 1 ]; then
+           #### It's not strictly necessary to have one space after
+           #### the : that separates the header field name from the
+           #### body, but do it to avoid surprising someone.
+           add=`printf "${arg}" | sed -e 's/:/: /' -e 's/:  /: /'`
+           headerfieldlist="${headerfieldlist:+${headerfieldlist}}${add}
+"
+           headerfieldarg=0
          elif [ ${switcharg} -eq 1 ]; then
            postsendargs="${postsendargs:+${postsendargs} }${arg}"
          elif [ ${ccarg} -eq 1 ]; then
@@ -101,7 +115,7 @@ else
          else
            #### An address.
            tolist="${tolist:+${tolist}, }${arg}"
-         fi
+         fi ;;
     esac
   done
 
@@ -126,6 +140,10 @@ else
   [ "${from}"x = x ]  ||  header="${header}${prefix}From: ${from}
 ";
 
+  if [ "${headerfieldlist}" ]; then
+    header="${header}${headerfieldlist}";
+  fi
+
   #### Set up a file to supply as a draft to send/post.  And set a
   #### trap to remove it.  send moves the file to a backup, so it will
   #### remove that, too.