3 Jon Steinhart's (jon@fourwinds.com) Attachment Handling Mods
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 A bunch of changes have been made to improve the nmh user interface for
7 handling MIME attachments.
12 Although nmh contains the necessary functionality for MIME message handing,
13 the interface to this functionality is pretty obtuse. There's no way that
14 I'm ever going to convince my partner to write mhbuild composition files!
15 And even though I know how to write them, I often screw up when sending a
16 message that contains a shell script because I forget that I have to double
17 any # at the start of a line, which shell scripts have galore.
19 These changes simplify the task of managing attachments on draft files.
20 They allow attachments to be added, listed, and deleted. MIME messages are
21 automatically created when drafts with attachments are sent.
26 The gory details are all given below. Here's how to set things up simply. This
27 only works if you're using the "standard" interface, i.e., whatnow. Life is more
28 complicated if you run mh-e.
30 Add the following to your .mh_profile:
32 send: -attach X-MH-Attachment
33 whatnow: -attach X-MH-Attachment
35 You may already have send and whatnow lines in your .mh_profile; if you do, just add
36 the new stuff to the end of the line. For example, mine looks like:
38 send: -alias aliases -attach X-MH-Attachment
39 whatnow: -attach X-MH-Attachment
41 That's it. Next time you want to send an attachment, say "attach filename" at the
42 whatnow prompt and you're done. You can check your attachments by saying "alist".
44 Did I Do This Correctly?
45 ~~~~~~~~~~~~~~~~~~~~~~~~
47 Hard to say. Despite lots of time looking at the nmh code, I can't say that
48 I get the philosophy behind its structure.
50 I am aware of two deviations from what I saw in the nmh code.
52 1. I commented my changes.
54 2. It's been years since I've used a VT-100, so I don't try to make code
55 fit into 80 columns anymore. Seems silly to me.
60 I made changes to the following files:
74 viamail.c (needed change for new sendsbr argument)
77 Attachments are associated with messages using header fields. For example, a
78 draft that looks like this
81 Subject: test of attachments
82 X-MH-Attachment: /export/home/jon/foo
83 X-MH-Attachment: /export/home/jon/bar
84 X-MH-Attachment: /export/home/jon/test/foo
87 has the files "foo", "bar", and foo as attachments.
89 Although I use the header field name "X-MH-Attachment" to indicate
90 attachments, the implementation allows any header field name.
92 The advantage of using header fields is that the list of attachments
93 travels with the draft so it remains valid across editing sessions.
95 Note that the header fields for attachments are removed from the message
98 Since I was adding header fields to messages, it seemed sensible to use the
99 existing anno program to do this for me. This required several changes to
102 o I added a -draft option that permits annotations (header fields) to
103 be added to the draft instead of a message sequence.
105 o I added a -delete option that allows annotations to be deleted.
107 o I added a -list option that allows annotations to be listed.
109 o I added a -number option that modifies the behavior of -delete and -list.
111 o I added a -append option so that annotations are appended to the headers
112 instead of the default prepend. Without this, attachments come out in
115 Using the modified anno, the example above could be created (assuming that the
118 prompt% anno -draft -comp X-MH-Attachment -text /export/home/jon/foo -nodate -append
119 prompt% anno -draft -comp X-MH-Attachment -text /export/home/jon/bar -nodate -append
120 prompt% anno -draft -comp X-MH-Attachment -text /export/home/jon/test/foo -nodate -append
122 One can quite easily make an "attach" command using shell scripts, aliases or functions.
123 For example, here's a bash function that does the job:
125 function attach() { for i in $*; do anno -append -nodate -draft -comp X-MH-Attachment -text "$i"; done; }
127 The following examples show the different ways in which attachments can be
130 prompt% anno -list -draft -comp X-MH-Attachment
135 prompt% anno -list -draft -comp X-MH-Attachment -text /
138 /export/home/jon/test/foo
140 prompt% anno -list -draft -comp X-MH-Attachment -number
145 prompt% anno -list -draft -comp X-MH-Attachment -text / -number
146 1 /export/home/jon/foo
147 2 /export/home/jon/bar
148 3 /export/home/jon/test/foo
152 Why all these listing options?
154 I feel that the listing as produced by the first example is what most people
155 would want most of the time.
157 The listing as produced by the second example seemed necessary for situations
158 where there were several attachments with the same file name in different
161 I included the numbering option so that attachments could be deleted by number
162 which might be easier in situations where there were several attachments with
163 the same file name in different directories, as in the above example.
165 Attachments are deleted using the -delete option.
167 prompt% anno -delete -draft -comp X-MH-Attachment -text foo
169 deletes the first attachment since the foo matches the basename of the attachment
172 prompt% anno -delete -draft -comp X-MH-Attachment -text /export/home/jon/test/foo
174 deletes the third attachment since the text is a full path name and matches.
176 prompt% anno -delete -draft -comp X-MH-Attachment -number 2
178 deletes the second attachment.
180 The attachment annotations are converted to a MIME message by send. I'm not
181 completely sure that this is the right place to do it, as opposed to doing
182 it in post, but both would work. It seemed to me to make more sense to do
183 it in send so that all of the various post options would apply to the MIME
184 message instead of the original draft file.
186 I added an -attach option to send that specifies the header field name used
187 for attachments. Send performs the following additional steps if this option
190 o It scans the header of the draft for attachments. Normal behavior applies
193 o A temporary mhbuild composition file is created if there are attachments.
195 o All non-attachment headers are copied from the draft file to the
198 o The body of the draft is copied to a temporary body file if it contains at
199 least one non-whitespace character. A mhbuild directive for this file is
200 appended to the composition file. Note that this eliminates the problem
201 of lines beginning with the # character in the message body.
203 o A mhbuild directive is appended to the composition file for each attachment
206 o mhbuild is run on the composition file, converting it to a MIME message.
208 o The converted composition file is substituted for the original draft file
209 and run through the rest of send.
211 o The original draft file is renamed instead of the converted composition
212 file. This preserves the original message instead of the composition file
213 which is really what a user would want.
215 o The ,file.orig file created by mhbuild is removed as it's a nuisance.
217 The mhbuild directives appended to the composition file are constructed as
220 o The content-type a file with a dot-suffix is obtained from the list of
221 mhshow-suffix- entries in the profile.
223 o A file with no dot-suffix or no entry in the profile is assigned a
224 content-type of application/octet-stream if it contains any non-ASCII
227 o A file with no dot-suffix or no entry in the profile is assigned a
228 content-type of text/plain if it contains only ASCII characters.
230 o The directive is of the form:
232 #content-type; name="basename"; x-unix-mode=mode [ description ] filename
234 The content type is derived as discussed above. The basename is the
235 last component of the pathname given in the body of the attachment header
236 field. The mode is file permissions. The description is obtained by
237 running the file command on the attachment file. The filename is the
238 field body from the attachment header field.
240 I added an -attach option to whatnow that specifies the header field name for
243 I added to the commands available at the whatnow prompt to provide an interface
244 to the attachment mechanism.
246 I'm not completely happy with some of these additions because they duplicate
247 shell functionality. I'm not sure that there's a good way around it other than
250 The first three additions (the ones I'm not happy with) are cd, ls, and pwd.
251 These do the same thing as their system counterparts. As a matter of fact,
252 these are implemented by running the commands in a subshell. I did this because
253 I wanted proper interpretation of shell-specific things like ~ and wildcard
256 The next command is attach. This takes a list of files and adds them to the draft
257 as attachments using the same code as the modified anno. The list of files is
258 run through ls using the user's shell, so wildcard expansion and all that works.
260 The alist command lists the attachments on the current draft using listing function
261 that I added to anno. It takes two optional options, -l for a long listing meaning
262 full path names, and -n for a numbered listing.
264 The detach command removes attachments from the current draft, again using the
265 modified anno. The arguments are interpreted as numbers if the -n option is used,
266 otherwise they're interpreted as file names. File names are shoveled through ls
267 using the user's shell in the directory containing the file for wildcard expansion
268 and such. File names are matched against the last pathname component unless they
269 begin with a / in which case they're matched against the entire name.
274 Nothing on this stuff. When I get time I'd like to improve the interface
275 for reading messages with attachments. It's my opinion that, because of the
276 command line nature of nmh, it makes the most sense to treat attachments as
277 separate messages. In other words one should be able to read the next
278 attachment using next, and the previous one using prev. One should be able
279 to show and scan attachments. This would probably involve a major change
280 in the message numbering scheme to allow something like 123.4 to indicate
281 attachment 4 on message 123.