Explanation of resend

The Section Use forw to Resend a Returned Message gives an overview of this script. You'll be installing the file resend.fixmsg; see the Section Programs in This Book's Archive. Here is the resend.fixmsg script (you might want to open it in a separate browser window). After you install resend.fixmsg, make a version of forw named resend (for directions, see the Section Making a New Command Version). Finally, put one of the following entries in your MH profile. The first one is for nmh version 0.26 and above. The second entry is for earlier nmh versions, as well as MH:

    resend: -editor resend.fixmsg -nodashstuffing

    resend: -editor resend.fixmsg -nodashmunging -filter mhl.noformat
    
The Section Leave My Dashes Alone explains the nmh -nodashstuffing switch and the undocumented MH -nodashmunging switch. The mhl.noformat file, shown in the same section, is there because -nodashstuffing requires either -filter or -format. (If you normally use forw -mime, any forw: -mime entry in your MH profile won't affect resend because resend has its own profile entry.)

How does this mess work? When you type resend, it runs the same executable code that the forw program uses. This makes a draft message that's a copy of your forwcomps file with the returned message below it. (In the draft, the returned message will have lines around it that say Forwarded Message and End of Forwarded Message.) We could have used a special draft template form file and an mhl filter file to delete a lot of the garbage lines, but we'd still end up with some text to delete. The executing code sees that it was called with the name resend (not forw), so it reads your resend: profile entry.

Your profile entry has -editor resend.fixmsg; this starts your editor script to clean up the draft. First, the ed(1) editor reads a list of editing commands (from its standard input) redirected with a here document (<< "END"). These remove most of the boilerplate error text added by the system MTA when it bounced the message. Of course, your MTA may be different. And if you get a bounced message from a remote system, it will probably have some other format. So you should customize these editing commands for your local MTA. (Eventually, as more systems generate standard delivery failure notices in MIME format, the resend.fixmsg script can be simplified to extract the returned message with mhn -store -part. If you're using nmh, that's mhstore -part.)

After the ed script finishes, an interactive editor starts to let you clean up the stuff that ed didn't. Because the shell running resend.fixmsg would exit after the interactive editor finishes, and do nothing in the meantime, an exec command overlays the shell process with the interactive editor. That trick isn't especially useful on modern UNIX systems, but it saved precious memory on older UNIXes.

Here's a sample returned mail message that the resend version of forw has made into a draft. The resend.fixmsg editor edits this. The draft is shown with line numbers (actually, letters) to the left of each line; they are not part of the draft, but are for reference only.

     A> To:
     B> cc:
     C> Subject:
     D> -------
     E> 
     F> ------- Forwarded Message
     G> 
     H> Date: Mon, 09 Jan 1995 06:57:36 EST
     I> From: MAILER-DAEMON (Mail Delivery Subsystem)
     J> Received: by mysun.csa.syr.edu (5.54/CSA)
     K>           id AA04361; Mon, 09 Jan 1995 06:57:36 EST
     L> Subject: Returned mail: Host unknown
     M> Message-Id: <9003071157.AA04361@mysun.csa.syr.edu>
     N> To: <ehuser>
     O> 
     P>    ----- Transcript of session follows -----
     Q> 550 rtp@gxa.nsq.edu... Host unknown
     R> 
     S>    ----- Unsent message follows -----
     T> Received: by mysun.csa.syr.edu (5.54/CSA)
     U>           id AA04355; Mon, 09 Jan 1995 06:57:36 EST
     V> Message-Id: <9003071157.AA04355@mysun.csa.syr.edu>
     W> To: rtp@gxa.nsq.edu, tko@gax.nsq.edu
     X> Subject: Incredibly boring seminar
     Y> Date: Mon, 09 Jan 1995 06:57:35 -0600
     Z> From: "Emma H. User" <ehuser>
    AA> 
    BB>     ...Body of message not shown here...
    XX> 
    YY> ------- End of Forwarded Message
    ZZ> 
    
Lines 14-22 of resend.fixmsg are ed editor commands; they're fed to the standard input of ed by the redirection on line 13. Because the terminator in line 13 has quotes around it ("END"), none of the special characters in lines 14-20 have to be quoted to protect them from the shell.

There's not space here to explain every detail of the ed editor script. But here's what the lines do:

Finally, line 26 puts you into an editor. It starts the editor in the VISUAL environment variable (if there is one). Next, it tries the EDITOR environment variable. If neither of those variables are set, it starts vi. The exec tells the shell to replace itself with a copy of the editor program. This is a good thing to do in cases when there's nothing else to do in a shell program -- no trap to spring -- and the exit status of the command you exec should be the exit status of the shell script.

Don't use exec if your editor returns a nonzero status, though, because your draft could be deleted before the What now? prompt. See the Section Writing Your Own Draft Message Editor(s).