2001-01-18 16:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / texi / emacs-mime.texi
1 \input texinfo                  @c -*-texinfo-*-
2
3 @setfilename emacs-mime
4 @settitle Emacs MIME Manual
5 @synindex fn cp
6 @synindex vr cp
7 @synindex pg cp
8 @dircategory Emacs
9 @direntry
10 * Emacs MIME: (emacs-mime).   The MIME de/composition library.
11 @end direntry
12 @iftex
13 @finalout
14 @end iftex
15 @setchapternewpage odd
16
17 @ifnottex
18
19 This file documents the Emacs MIME interface functionality.
20
21 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
22
23 Permission is granted to copy, distribute and/or modify this document
24 under the terms of the GNU Free Documentation License, Version 1.1 or
25 any later version published by the Free Software Foundation; with no
26 Invariant Sections, with the Front-Cover texts being ``A GNU
27 Manual'', and with the Back-Cover Texts as in (a) below.  A copy of the
28 license is included in the section entitled ``GNU Free Documentation
29 License'' in the Emacs manual.
30
31 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
32 this GNU Manual, like GNU software.  Copies published by the Free
33 Software Foundation raise funds for GNU development.''
34
35 This document is part of a collection distributed under the GNU Free
36 Documentation License.  If you want to distribute this document
37 separately from the collection, you can do so by adding a copy of the
38 license to the document, as described in section 6 of the license.
39 @end ifnottex
40
41 @tex
42
43 @titlepage
44 @title Emacs MIME Manual
45
46 @author by Lars Magne Ingebrigtsen
47 @page
48
49 @vskip 0pt plus 1filll
50 Copyright @copyright{} 1998, 1999, 2000 Free Software Foundation, Inc.
51
52 Permission is granted to copy, distribute and/or modify this document
53 under the terms of the GNU Free Documentation License, Version 1.1 or
54 any later version published by the Free Software Foundation; with the
55 Invariant Sections being none, with the Front-Cover texts being ``A GNU
56 Manual'', and with the Back-Cover Texts as in (a) below.  A copy of the
57 license is included in the section entitled ``GNU Free Documentation
58 License'' in the Emacs manual.
59
60 (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
61 this GNU Manual, like GNU software.  Copies published by the Free
62 Software Foundation raise funds for GNU development.''
63
64 This document is part of a collection distributed under the GNU Free
65 Documentation License.  If you want to distribute this document
66 separately from the collection, you can do so by adding a copy of the
67 license to the document, as described in section 6 of the license.
68 @end titlepage
69 @page
70
71 @end tex
72
73 @node Top
74 @top Emacs MIME
75
76 This manual documents the libraries used to compose and display
77 @sc{mime} messages.
78
79 This is not a manual meant for users; it's a manual directed at people
80 who want to write functions and commands that manipulate @sc{mime}
81 elements.
82
83 @sc{mime} is short for @dfn{Multipurpose Internet Mail Extensions}.
84 This standard is documented in a number of RFCs; mainly RFC2045 (Format
85 of Internet Message Bodies), RFC2046 (Media Types), RFC2047 (Message
86 Header Extensions for Non-ASCII Text), RFC2048 (Registration
87 Procedures), RFC2049 (Conformance Criteria and Examples).  It is highly
88 recommended that anyone who intends writing @sc{mime}-compliant software
89 read at least RFC2045 and RFC2047.
90
91 @menu
92 * Interface Functions::   An abstraction over the basic functions.
93 * Basic Functions::       Utility and basic parsing functions.
94 * Decoding and Viewing::  A framework for decoding and viewing.
95 * Composing::             MML; a language for describing MIME parts.
96 * Standards::             A summary of RFCs and working documents used.
97 * Index::                 Function and variable index.
98 @end menu
99
100
101 @node Interface Functions
102 @chapter Interface Functions
103 @cindex interface functions
104 @cindex mail-parse
105
106 The @code{mail-parse} library is an abstraction over the actual
107 low-level libraries that are described in the next chapter.
108
109 Standards change, and so programs have to change to fit in the new
110 mold.  For instance, RFC2045 describes a syntax for the
111 @code{Content-Type} header that only allows ASCII characters in the
112 parameter list.  RFC2231 expands on RFC2045 syntax to provide a scheme
113 for continuation headers and non-ASCII characters.
114
115 The traditional way to deal with this is just to update the library
116 functions to parse the new syntax.  However, this is sometimes the wrong
117 thing to do.  In some instances it may be vital to be able to understand
118 both the old syntax as well as the new syntax, and if there is only one
119 library, one must choose between the old version of the library and the
120 new version of the library.
121
122 The Emacs MIME library takes a different tack.  It defines a series of
123 low-level libraries (@file{rfc2047.el}, @file{rfc2231.el} and so on)
124 that parses strictly according to the corresponding standard.  However,
125 normal programs would not use the functions provided by these libraries
126 directly, but instead use the functions provided by the
127 @code{mail-parse} library.  The functions in this library are just
128 aliases to the corresponding functions in the latest low-level
129 libraries.  Using this scheme, programs get a consistent interface they
130 can use, and library developers are free to create write code that
131 handles new standards.
132
133 The following functions are defined by this library:
134
135 @table @code
136 @item mail-header-parse-content-type
137 @findex mail-header-parse-content-type
138 Parse a @code{Content-Type} header and return a list on the following
139 format:
140
141 @lisp
142 ("type/subtype"
143  (attribute1 . value1)
144  (attribute2 . value2)
145  ...)
146 @end lisp
147
148 Here's an example:
149
150 @example
151 (mail-header-parse-content-type
152  "image/gif; name=\"b980912.gif\"")
153 @result{} ("image/gif" (name . "b980912.gif"))
154 @end example
155
156 @item mail-header-parse-content-disposition
157 @findex mail-header-parse-content-disposition
158 Parse a @code{Content-Disposition} header and return a list on the same
159 format as the function above.
160
161 @item mail-content-type-get
162 @findex mail-content-type-get
163 Takes two parameters---a list on the format above, and an attribute.
164 Returns the value of the attribute.
165
166 @example
167 (mail-content-type-get
168  '("image/gif" (name . "b980912.gif")) 'name)
169 @result{} "b980912.gif"
170 @end example
171
172 @item mail-header-encode-parameter
173 @findex mail-header-encode-parameter
174 Takes a parameter string and returns an encoded version of the string.
175 This is used for parameters in headers like @code{Content-Type} and
176 @code{Content-Disposition}.
177
178 @item mail-header-remove-comments
179 @findex mail-header-remove-comments
180 Return a comment-free version of a header.
181
182 @example
183 (mail-header-remove-comments
184  "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
185 @result{} "Gnus/5.070027  "
186 @end example
187
188 @item mail-header-remove-whitespace
189 @findex mail-header-remove-whitespace
190 Remove linear white space from a header.  Space inside quoted strings
191 and comments is preserved.
192
193 @example
194 (mail-header-remove-whitespace
195  "image/gif; name=\"Name with spaces\"")
196 @result{} "image/gif;name=\"Name with spaces\""
197 @end example
198
199 @item mail-header-get-comment
200 @findex mail-header-get-comment
201 Return the last comment in a header.
202
203 @example
204 (mail-header-get-comment
205  "Gnus/5.070027 (Pterodactyl Gnus v0.27) (Finnish Landrace)")
206 @result{} "Finnish Landrace"
207 @end example
208
209 @item mail-header-parse-address
210 @findex mail-header-parse-address
211 Parse an address and return a list containing the mailbox and the
212 plaintext name.
213
214 @example
215 (mail-header-parse-address
216  "Hrvoje Niksic <hniksic@@srce.hr>")
217 @result{} ("hniksic@@srce.hr" . "Hrvoje Niksic")
218 @end example
219
220 @item mail-header-parse-addresses
221 @findex mail-header-parse-addresses
222 Parse a string with list of addresses and return a list of elements like
223 the one described above.
224
225 @example
226 (mail-header-parse-addresses
227  "Hrvoje Niksic <hniksic@@srce.hr>, Steinar Bang <sb@@metis.no>")
228 @result{} (("hniksic@@srce.hr" . "Hrvoje Niksic")
229      ("sb@@metis.no" . "Steinar Bang"))
230 @end example
231
232 @item mail-header-parse-date
233 @findex mail-header-parse-date
234 Parse a date string and return an Emacs time structure.
235
236 @item mail-narrow-to-head
237 @findex mail-narrow-to-head
238 Narrow the buffer to the header section of the buffer.  Point is placed
239 at the beginning of the narrowed buffer.
240
241 @item mail-header-narrow-to-field
242 @findex mail-header-narrow-to-field
243 Narrow the buffer to the header under point.
244
245 @item mail-encode-encoded-word-region
246 @findex mail-encode-encoded-word-region
247 Encode the non-ASCII words in the region.  For instance,
248 @samp{Naïve} is encoded as @samp{=?iso-8859-1?q?Na=EFve?=}.
249
250 @item mail-encode-encoded-word-buffer
251 @findex mail-encode-encoded-word-buffer
252 Encode the non-ASCII words in the current buffer.  This function is
253 meant to be called narrowed to the headers of a message.
254
255 @item mail-encode-encoded-word-string
256 @findex mail-encode-encoded-word-string
257 Encode the words that need encoding in a string, and return the result.
258
259 @example
260 (mail-encode-encoded-word-string
261  "This is naïve, baby")
262 @result{} "This is =?iso-8859-1?q?na=EFve,?= baby"
263 @end example
264
265 @item mail-decode-encoded-word-region
266 @findex mail-decode-encoded-word-region
267 Decode the encoded words in the region.
268
269 @item mail-decode-encoded-word-string
270 @findex mail-decode-encoded-word-string
271 Decode the encoded words in the string and return the result.
272
273 @example
274 (mail-decode-encoded-word-string
275  "This is =?iso-8859-1?q?na=EFve,?= baby")
276 @result{} "This is naïve, baby"
277 @end example
278
279 @end table
280
281 Currently, @code{mail-parse} is an abstraction over @code{ietf-drums},
282 @code{rfc2047}, @code{rfc2045} and @code{rfc2231}.  These are documented
283 in the subsequent sections.
284
285
286
287 @node Basic Functions
288 @chapter Basic Functions
289
290 This chapter describes the basic, ground-level functions for parsing and
291 handling.  Covered here is parsing @code{From} lines, removing comments
292 from header lines, decoding encoded words, parsing date headers and so
293 on.  High-level functionality is dealt with in the next chapter
294 (@pxref{Decoding and Viewing}).
295
296 @menu
297 * rfc2045::      Encoding @code{Content-Type} headers.
298 * rfc2231::      Parsing @code{Content-Type} headers.
299 * ietf-drums::   Handling mail headers defined by RFC822bis.
300 * rfc2047::      En/decoding encoded words in headers.
301 * time-date::    Functions for parsing dates and manipulating time.
302 * qp::           Quoted-Printable en/decoding.
303 * base64::       Base64 en/decoding.
304 * binhex::       Binhex decoding.
305 * uudecode::     Uuencode decoding.
306 * rfc1843::      Decoding HZ-encoded text.
307 * mailcap::      How parts are displayed is specified by the @file{.mailcap} file
308 @end menu
309
310
311 @node rfc2045
312 @section rfc2045
313
314 RFC2045 is the ``main'' @sc{mime} document, and as such, one would
315 imagine that there would be a lot to implement.  But there isn't, since
316 most of the implementation details are delegated to the subsequent
317 RFCs.
318
319 So @file{rfc2045.el} has only a single function:
320
321 @table @code
322 @item rfc2045-encode-string
323 @findex rfc2045-encode-string
324 Takes a parameter and a value and returns a @samp{PARAM=VALUE} string.
325 @var{value} will be quoted if there are non-safe characters in it.
326 @end table
327
328
329 @node rfc2231
330 @section rfc2231
331
332 RFC2231 defines a syntax for the @code{Content-Type} and
333 @code{Content-Disposition} headers.  Its snappy name is @dfn{MIME
334 Parameter Value and Encoded Word Extensions: Character Sets, Languages,
335 and Continuations}.
336
337 In short, these headers look something like this:
338
339 @example
340 Content-Type: application/x-stuff;
341  title*0*=us-ascii'en'This%20is%20even%20more%20;
342  title*1*=%2A%2A%2Afun%2A%2A%2A%20;
343  title*2="isn't it!"
344 @end example
345
346 They usually aren't this bad, though.
347
348 The following functions are defined by this library:
349
350 @table @code
351 @item rfc2231-parse-string
352 @findex rfc2231-parse-string
353 Parse a @code{Content-Type} header and return a list describing its
354 elements.
355
356 @example
357 (rfc2231-parse-string
358  "application/x-stuff;
359  title*0*=us-ascii'en'This%20is%20even%20more%20;
360  title*1*=%2A%2A%2Afun%2A%2A%2A%20;
361  title*2=\"isn't it!\"")
362 @result{} ("application/x-stuff"
363     (title . "This is even more ***fun*** isn't it!"))
364 @end example
365
366 @item rfc2231-get-value
367 @findex rfc2231-get-value
368 Takes one of the lists on the format above and returns
369 the value of the specified attribute.
370
371 @item rfc2231-encode-string
372 @findex rfc2231-encode-string
373 Encode a parameter in headers likes @code{Content-Type} and
374 @code{Content-Disposition}.
375
376 @end table
377
378
379 @node ietf-drums
380 @section ietf-drums
381
382 @dfn{drums} is an IETF working group that is working on the replacement
383 for RFC822.
384
385 The functions provided by this library include:
386
387 @table @code
388 @item ietf-drums-remove-comments
389 @findex ietf-drums-remove-comments
390 Remove the comments from the argument and return the results.
391
392 @item ietf-drums-remove-whitespace
393 @findex ietf-drums-remove-whitespace
394 Remove linear white space from the string and return the results.
395 Spaces inside quoted strings and comments are left untouched.
396
397 @item ietf-drums-get-comment
398 @findex ietf-drums-get-comment
399 Return the last most comment from the string.
400
401 @item ietf-drums-parse-address
402 @findex ietf-drums-parse-address
403 Parse an address string and return a list that contains the mailbox and
404 the plain text name.
405
406 @item ietf-drums-parse-addresses
407 @findex ietf-drums-parse-addresses
408 Parse a string that contains any number of comma-separated addresses and
409 return a list that contains mailbox/plain text pairs.
410
411 @item ietf-drums-parse-date
412 @findex ietf-drums-parse-date
413 Parse a date string and return an Emacs time structure.
414
415 @item ietf-drums-narrow-to-header
416 @findex ietf-drums-narrow-to-header
417 Narrow the buffer to the header section of the current buffer.
418
419 @end table
420
421
422 @node rfc2047
423 @section rfc2047
424
425 RFC2047 (Message Header Extensions for Non-ASCII Text) specifies how
426 non-ASCII text in headers are to be encoded.  This is actually rather
427 complicated, so a number of variables are necessary to tweak what this
428 library does.
429
430 The following variables are tweakable:
431
432 @table @code
433 @item rfc2047-default-charset
434 @vindex rfc2047-default-charset
435 Characters in this charset should not be decoded by this library.
436 This defaults to @code{iso-8859-1}.
437
438 @item rfc2047-header-encoding-list
439 @vindex rfc2047-header-encoding-list
440 This is an alist of header / encoding-type pairs.  Its main purpose is
441 to prevent encoding of certain headers.
442
443 The keys can either be header regexps, or @code{t}.
444
445 The values can be either @code{nil}, in which case the header(s) in
446 question won't be encoded, or @code{mime}, which means that they will be
447 encoded.
448
449 @item rfc2047-charset-encoding-alist
450 @vindex rfc2047-charset-encoding-alist
451 RFC2047 specifies two forms of encoding---@code{Q} (a
452 Quoted-Printable-like encoding) and @code{B} (base64).  This alist
453 specifies which charset should use which encoding.
454
455 @item rfc2047-encoding-function-alist
456 @vindex rfc2047-encoding-function-alist
457 This is an alist of encoding / function pairs.  The encodings are
458 @code{Q}, @code{B} and @code{nil}.
459
460 @item rfc2047-q-encoding-alist
461 @vindex rfc2047-q-encoding-alist
462 The @code{Q} encoding isn't quite the same for all headers.  Some
463 headers allow a narrower range of characters, and that is what this
464 variable is for.  It's an alist of header regexps / allowable character
465 ranges.
466
467 @item rfc2047-encoded-word-regexp
468 @vindex rfc2047-encoded-word-regexp
469 When decoding words, this library looks for matches to this regexp.
470
471 @end table
472
473 Those were the variables, and these are this functions:
474
475 @table @code
476 @item rfc2047-narrow-to-field
477 @findex rfc2047-narrow-to-field
478 Narrow the buffer to the header on the current line.
479
480 @item rfc2047-encode-message-header
481 @findex rfc2047-encode-message-header
482 Should be called narrowed to the header of a message.  Encodes according
483 to @code{rfc2047-header-encoding-alist}.
484
485 @item rfc2047-encode-region
486 @findex rfc2047-encode-region
487 Encodes all encodable words in the region specified.
488
489 @item rfc2047-encode-string
490 @findex rfc2047-encode-string
491 Encode a string and return the results.
492
493 @item rfc2047-decode-region
494 @findex rfc2047-decode-region
495 Decode the encoded words in the region.
496
497 @item rfc2047-decode-string
498 @findex rfc2047-decode-string
499 Decode a string and return the results.
500
501 @end table
502
503
504 @node time-date
505 @section time-date
506
507 While not really a part of the @sc{mime} library, it is convenient to
508 document this library here.  It deals with parsing @code{Date} headers
509 and manipulating time.  (Not by using tesseracts, though, I'm sorry to
510 say.)
511
512 These functions convert between five formats: A date string, an Emacs
513 time structure, a decoded time list, a second number, and a day number.
514
515 Here's a bunch of time/date/second/day examples:
516
517 @example
518 (parse-time-string "Sat Sep 12 12:21:54 1998 +0200")
519 @result{} (54 21 12 12 9 1998 6 nil 7200)
520
521 (date-to-time "Sat Sep 12 12:21:54 1998 +0200")
522 @result{} (13818 19266)
523
524 (time-to-seconds '(13818 19266))
525 @result{} 905595714.0
526
527 (seconds-to-time 905595714.0)
528 @result{} (13818 19266 0)
529
530 (time-to-days '(13818 19266))
531 @result{} 729644
532
533 (days-to-time 729644)
534 @result{} (961933 65536)
535
536 (time-since '(13818 19266))
537 @result{} (0 430)
538
539 (time-less-p '(13818 19266) '(13818 19145))
540 @result{} nil
541
542 (subtract-time '(13818 19266) '(13818 19145))
543 @result{} (0 121)
544
545 (days-between "Sat Sep 12 12:21:54 1998 +0200"
546               "Sat Sep 07 12:21:54 1998 +0200")
547 @result{} 5
548
549 (date-leap-year-p 2000)
550 @result{} t
551
552 (time-to-day-in-year '(13818 19266))
553 @result{} 255
554
555 (time-to-number-of-days
556  (time-since
557   (date-to-time "Mon, 01 Jan 2001 02:22:26 GMT")))
558 @result{} 4.146122685185185
559 @end example
560
561 And finally, we have @code{safe-date-to-time}, which does the same as
562 @code{date-to-time}, but returns a zero time if the date is
563 syntactically malformed.
564
565 The five data representations used are the following:
566
567 @table @var
568 @item date
569 An RFC822 (or similar) date string.  For instance: @code{"Sat Sep 12
570 12:21:54 1998 +0200"}.
571
572 @item time
573 An internal Emacs time.  For instance: @code{(13818 26466)}.
574
575 @item seconds
576 A floating point representation of the internal Emacs time.  For
577 instance: @code{905595714.0}.
578
579 @item days
580 An integer number representing the number of days since 00000101.  For
581 instance: @code{729644}.
582
583 @item decoded time
584 A list of decoded time.  For instance: @code{(54 21 12 12 9 1998 6 t
585 7200)}.
586 @end table
587
588 All the examples above represent the same moment.
589
590 These are the functions available:
591
592 @table @code
593 @item date-to-time
594 Take a date and return a time.
595
596 @item time-to-seconds
597 Take a time and return seconds.
598
599 @item seconds-to-time
600 Take seconds and return a time.
601
602 @item time-to-days
603 Take a time and return days.
604
605 @item days-to-time
606 Take days and return a time.
607
608 @item date-to-day
609 Take a date and return days.
610
611 @item time-to-number-of-days
612 Take a time and return the number of days that represents.
613
614 @item safe-date-to-time
615 Take a date and return a time.  If the date is not syntactically valid,
616 return a "zero" date.
617
618 @item time-less-p
619 Take two times and say whether the first time is less (i. e., earlier)
620 than the second time.
621
622 @item time-since
623 Take a time and return a time saying how long it was since that time.
624
625 @item subtract-time
626 Take two times and subtract the second from the first.  I. e., return
627 the time between the two times.
628
629 @item days-between
630 Take two days and return the number of days between those two days.
631
632 @item date-leap-year-p
633 Take a year number and say whether it's a leap year.
634
635 @item time-to-day-in-year
636 Take a time and return the day number within the year that the time is
637 in. 
638
639 @end table
640
641
642 @node qp
643 @section qp
644
645 This library deals with decoding and encoding Quoted-Printable text.
646
647 Very briefly explained, qp encoding means translating all 8-bit
648 characters (and lots of control characters) into things that look like
649 @samp{=EF}; that is, an equal sign followed by the byte encoded as a hex
650 string.
651
652 The following functions are defined by the library:
653
654 @table @code
655 @item quoted-printable-decode-region
656 @findex quoted-printable-decode-region
657 QP-decode all the encoded text in the specified region.
658
659 @item quoted-printable-decode-string
660 @findex quoted-printable-decode-string
661 Decode the QP-encoded text in a string and return the results.
662
663 @item quoted-printable-encode-region
664 @findex quoted-printable-encode-region
665 QP-encode all the encodable characters in the specified region.  The third
666 optional parameter @var{fold} specifies whether to fold long lines.
667 (Long here means 72.)
668
669 @item quoted-printable-encode-string
670 @findex quoted-printable-encode-string
671 QP-encode all the encodable characters in a string and return the
672 results.
673
674 @end table
675
676
677 @node base64
678 @section base64
679 @cindex base64
680
681 Base64 is an encoding that encodes three bytes into four characters,
682 thereby increasing the size by about 33%.  The alphabet used for
683 encoding is very resistant to mangling during transit.
684
685 The following functions are defined by this library:
686
687 @table @code
688 @item base64-encode-region
689 @findex base64-encode-region
690 base64 encode the selected region.  Return the length of the encoded
691 text.  Optional third argument @var{no-line-break} means do not break
692 long lines into shorter lines.
693
694 @item base64-encode-string
695 @findex base64-encode-string
696 base64 encode a string and return the result.
697
698 @item base64-decode-region
699 @findex base64-decode-region
700 base64 decode the selected region.  Return the length of the decoded
701 text.  If the region can't be decoded, return @code{nil} and don't
702 modify the buffer.
703
704 @item base64-decode-string
705 @findex base64-decode-string
706 base64 decode a string and return the result.  If the string can't be
707 decoded, @code{nil} is returned.
708
709 @end table
710
711
712 @node binhex
713 @section binhex
714 @cindex binhex
715 @cindex Apple
716 @cindex Macintosh
717
718 @code{binhex} is an encoding that originated in Macintosh environments.
719 The following function is supplied to deal with these:
720
721 @table @code
722 @item binhex-decode-region
723 @findex binhex-decode-region
724 Decode the encoded text in the region.  If given a third parameter, only
725 decode the @code{binhex} header and return the filename.
726
727 @end table
728
729
730 @node uudecode
731 @section uudecode
732 @cindex uuencode
733 @cindex uudecode
734
735 @code{uuencode} is probably still the most popular encoding of binaries
736 used on Usenet, although @code{base64} rules the mail world.
737
738 The following function is supplied by this package:
739
740 @table @code
741 @item uudecode-decode-region
742 @findex uudecode-decode-region
743 Decode the text in the region.
744 @end table
745
746
747 @node rfc1843
748 @section rfc1843
749 @cindex rfc1843
750 @cindex HZ
751 @cindex Chinese
752
753 RFC1843 deals with mixing Chinese and ASCII characters in messages.  In
754 essence, RFC1843 switches between ASCII and Chinese by doing this:
755
756 @example
757 This sentence is in ASCII.
758 The next sentence is in GB.~@{<:Ky2;S@{#,NpJ)l6HK!#~@}Bye.
759 @end example
760
761 Simple enough, and widely used in China.
762
763 The following functions are available to handle this encoding:
764
765 @table @code
766 @item rfc1843-decode-region
767 Decode HZ-encoded text in the region.
768
769 @item rfc1843-decode-string
770 Decode a HZ-encoded string and return the result.
771
772 @end table
773
774
775 @node mailcap
776 @section mailcap
777
778 The @file{~/.mailcap} file is parsed by most @sc{mime}-aware message
779 handlers and describes how elements are supposed to be displayed.
780 Here's an example file:
781
782 @example
783 image/*; gimp -8 %s
784 audio/wav; wavplayer %s
785 @end example
786
787 This says that all image files should be displayed with @code{gimp}, and
788 that realaudio files should be played by @code{rvplayer}.
789
790 The @code{mailcap} library parses this file, and provides functions for
791 matching types.
792
793 @table @code
794 @item mailcap-mime-data
795 @vindex mailcap-mime-data
796 This variable is an alist of alists containing backup viewing rules.
797
798 @end table
799
800 Interface functions:
801
802 @table @code
803 @item mailcap-parse-mailcaps
804 @findex mailcap-parse-mailcaps
805 Parse the @code{~/.mailcap} file.
806
807 @item mailcap-mime-info
808 Takes a @sc{mime} type as its argument and returns the matching viewer.
809
810 @end table
811
812
813
814
815 @node Decoding and Viewing
816 @chapter Decoding and Viewing
817
818 This chapter deals with decoding and viewing @sc{mime} messages on a
819 higher level.
820
821 The main idea is to first analyze a @sc{mime} article, and then allow
822 other programs to do things based on the list of @dfn{handles} that are
823 returned as a result of this analysis.
824
825 @menu
826 * Dissection::     Analyzing a @sc{mime} message.
827 * Handles::        Handle manipulations.
828 * Display::        Displaying handles.
829 * Customization::  Variables that affect display.
830 * New Viewers::    How to write your own viewers.
831 @end menu
832
833
834 @node Dissection
835 @section Dissection
836
837 The @code{mm-dissect-buffer} is the function responsible for dissecting
838 a @sc{mime} article.  If given a multipart message, it will recursively
839 descend the message, following the structure, and return a tree of
840 @sc{mime} handles that describes the structure of the message.
841
842
843 @node Handles
844 @section Handles
845
846 A @sc{mime} handle is a list that fully describes a @sc{mime}
847 component.
848
849 The following macros can be used to access elements in a handle:
850
851 @table @code
852 @item mm-handle-buffer
853 @findex mm-handle-buffer
854 Return the buffer that holds the contents of the undecoded @sc{mime}
855 part.
856
857 @item mm-handle-type
858 @findex mm-handle-type
859 Return the parsed @code{Content-Type} of the part.
860
861 @item mm-handle-encoding
862 @findex mm-handle-encoding
863 Return the @code{Content-Transfer-Encoding} of the part.
864
865 @item mm-handle-undisplayer
866 @findex mm-handle-undisplayer
867 Return the object that can be used to remove the displayed part (if it
868 has been displayed).
869
870 @item mm-handle-set-undisplayer
871 @findex mm-handle-set-undisplayer
872 Set the undisplayer object.
873
874 @item mm-handle-disposition
875 @findex mm-handle-disposition
876 Return the parsed @code{Content-Disposition} of the part.
877
878 @item mm-handle-disposition
879 @findex mm-handle-disposition
880 Return the description of the part.
881
882 @item mm-get-content-id
883 Returns the handle(s) referred to by @code{Content-ID}.
884
885 @end table
886
887
888 @node Display
889 @section Display
890
891 Functions for displaying, removing and saving.
892
893 @table @code
894 @item mm-display-part
895 @findex mm-display-part
896 Display the part.
897
898 @item mm-remove-part
899 @findex mm-remove-part
900 Remove the part (if it has been displayed).
901
902 @item mm-inlinable-p
903 @findex mm-inlinable-p
904 Say whether a @sc{mime} type can be displayed inline.
905
906 @item mm-automatic-display-p
907 @findex mm-automatic-display-p
908 Say whether a @sc{mime} type should be displayed automatically.
909
910 @item mm-destroy-part
911 @findex mm-destroy-part
912 Free all resources occupied by a part.
913
914 @item mm-save-part
915 @findex mm-save-part
916 Offer to save the part in a file.
917
918 @item mm-pipe-part
919 @findex mm-pipe-part
920 Offer to pipe the part to some process.
921
922 @item mm-interactively-view-part
923 @findex mm-interactively-view-part
924 Prompt for a mailcap method to use to view the part.
925
926 @end table
927
928
929 @node Customization
930 @section Customization
931
932 @table @code
933
934 @item mm-inline-media-tests
935 This is an alist where the key is a @sc{mime} type, the second element
936 is a function to display the part @dfn{inline} (i.e., inside Emacs), and 
937 the third element is a form to be @code{eval}ed to say whether the part
938 can be displayed inline.
939
940 This variable specifies whether a part @emph{can} be displayed inline,
941 and, if so, how to do it.  It does not say whether parts are
942 @emph{actually} displayed inline.
943
944 @item mm-inlined-types
945 This, on the other hand, says what types are to be displayed inline, if
946 they satisfy the conditions set by the variable above.  It's a list of
947 @sc{mime} media types.
948
949 @item mm-automatic-display
950 This is a list of types that are to be displayed ``automatically'', but
951 only if the above variable allows it.  That is, only inlinable parts can
952 be displayed automatically.
953
954 @item mm-attachment-override-types
955 Some @sc{mime} agents create parts that have a content-disposition of
956 @samp{attachment}.  This variable allows overriding that disposition and 
957 displaying the part inline.  (Note that the disposition is only
958 overridden if we are able to, and want to, display the part inline.)
959
960 @item mm-discouraged-alternatives
961 List of @sc{mime} types that are discouraged when viewing
962 @samp{multipart/alternative}.  Viewing agents are supposed to view the
963 last possible part of a message, as that is supposed to be the richest.
964 However, users may prefer other types instead, and this list says what
965 types are most unwanted.  If, for instance, @samp{text/html} parts are
966 very unwanted, and @samp{text/richtech} parts are somewhat unwanted,
967 then the value of this variable should be set to:
968
969 @lisp
970 ("text/html" "text/richtext")
971 @end lisp
972
973 @item mm-inline-large-images-p
974 When displaying inline images that are larger than the window, XEmacs
975 does not enable scrolling, which means that you cannot see the whole
976 image.  To prevent this, the library tries to determine the image size
977 before displaying it inline, and if it doesn't fit the window, the
978 library will display it externally (e.g. with @samp{ImageMagick} or
979 @samp{xv}).  Setting this variable to @code{t} disables this check and
980 makes the library display all inline images as inline, regardless of
981 their size.
982
983 @item mm-inline-override-p
984 @code{mm-inlined-types} may include regular expressions, for example to
985 specify that all @samp{text/.*} parts be displayed inline.  If a user
986 prefers to have a type that matches such a regular expression be treated
987 as an attachment, that can be accomplished by setting this variable to a
988 list containing that type.  For example assuming @code{mm-inlined-types}
989 includes @samp{text/.*}, then including @samp{text/html} in this
990 variable will cause @samp{text/html} parts to be treated as attachments.
991
992 @end table
993
994
995 @node New Viewers
996 @section New Viewers
997
998 Here's an example viewer for displaying @code{text/enriched} inline:
999
1000 @lisp
1001 (defun mm-display-enriched-inline (handle)
1002   (let (text)
1003     (with-temp-buffer
1004       (mm-insert-part handle)
1005       (save-window-excursion
1006         (enriched-decode (point-min) (point-max))
1007         (setq text (buffer-string))))
1008     (mm-insert-inline handle text)))
1009 @end lisp
1010
1011 We see that the function takes a @sc{mime} handle as its parameter.  It
1012 then goes to a temporary buffer, inserts the text of the part, does some 
1013 work on the text, stores the result, goes back to the buffer it was
1014 called from and inserts the result.
1015
1016 The two important helper functions here are @code{mm-insert-part} and
1017 @code{mm-insert-inline}.  The first function inserts the text of the
1018 handle in the current buffer.  It handles charset and/or content
1019 transfer decoding.  The second function just inserts whatever text you
1020 tell it to insert, but it also sets things up so that the text can be
1021 ``undisplayed' in a convenient manner.
1022
1023
1024 @node Composing
1025 @chapter Composing
1026 @cindex Composing
1027 @cindex MIME Composing
1028 @cindex MML
1029 @cindex MIME Meta Language
1030
1031 Creating a @sc{mime} message is boring and non-trivial.  Therefore, a
1032 library called @code{mml} has been defined that parses a language called
1033 MML (@sc{mime} Meta Language) and generates @sc{mime} messages.
1034
1035 @findex mml-generate-mime
1036 The main interface function is @code{mml-generate-mime}.  It will
1037 examine the contents of the current (narrowed-to) buffer and return a
1038 string containing the @sc{mime} message.
1039
1040 @menu
1041 * Simple MML Example::             An example MML document.
1042 * MML Definition::                 All valid MML elements.
1043 * Advanced MML Example::           Another example MML document.
1044 * Charset Translation::            How charsets are mapped from @sc{mule} to MIME.
1045 * Conversion::                     Going from @sc{mime} to MML and vice versa.
1046 @end menu
1047
1048
1049 @node Simple MML Example
1050 @section Simple MML Example
1051
1052 Here's a simple @samp{multipart/alternative}:
1053
1054 @example
1055 <#multipart type=alternative>
1056 This is a plain text part.
1057 <#part type=text/enriched>
1058 <center>This is a centered enriched part</center>
1059 <#/multipart>
1060 @end example
1061
1062 After running this through @code{mml-generate-mime}, we get this:
1063
1064 @example
1065 Content-Type: multipart/alternative; boundary="=-=-="
1066
1067
1068 --=-=-=
1069
1070
1071 This is a plain text part.
1072
1073 --=-=-=
1074 Content-Type: text/enriched
1075
1076
1077 <center>This is a centered enriched part</center>
1078
1079 --=-=-=--
1080 @end example
1081
1082
1083 @node MML Definition
1084 @section MML Definition
1085
1086 The MML language is very simple.  It looks a bit like an SGML
1087 application, but it's not.
1088
1089 The main concept of MML is the @dfn{part}.  Each part can be of a
1090 different type or use a different charset.  The way to delineate a part
1091 is with a @samp{<#part ...>} tag.  Multipart parts can be introduced
1092 with the @samp{<#multipart ...>} tag.  Parts are ended by the
1093 @samp{<#/part>} or @samp{<#/multipart>} tags.  Parts started with the
1094 @samp{<#part ...>} tags are also closed by the next open tag.
1095
1096 There's also the @samp{<#external ...>} tag.  These introduce
1097 @samp{external/message-body} parts.
1098
1099 Each tag can contain zero or more parameters on the form
1100 @samp{parameter=value}.  The values may be enclosed in quotation marks,
1101 but that's not necessary unless the value contains white space.  So
1102 @samp{filename=/home/user/#hello$^yes} is perfectly valid.
1103
1104 The following parameters have meaning in MML; parameters that have no
1105 meaning are ignored.  The MML parameter names are the same as the
1106 @sc{mime} parameter names; the things in the parentheses say which
1107 header it will be used in.
1108
1109 @table @samp
1110 @item type
1111 The @sc{mime} type of the part (@code{Content-Type}).
1112
1113 @item filename
1114 Use the contents of the file in the body of the part
1115 (@code{Content-Disposition}).
1116
1117 @item charset
1118 The contents of the body of the part are to be encoded in the character
1119 set speficied (@code{Content-Type}).
1120
1121 @item name
1122 Might be used to suggest a file name if the part is to be saved
1123 to a file (@code{Content-Type}).
1124
1125 @item disposition
1126 Valid values are @samp{inline} and @samp{attachment}
1127 (@code{Content-Disposition}).
1128
1129 @item encoding
1130 Valid values are @samp{7bit}, @samp{8bit}, @samp{quoted-printable} and
1131 @samp{base64} (@code{Content-Transfer-Encoding}).
1132
1133 @item description
1134 A description of the part (@code{Content-Description}).
1135
1136 @item creation-date
1137 RFC822 date when the part was created (@code{Content-Disposition}).
1138
1139 @item modification-date
1140 RFC822 date when the part was modified (@code{Content-Disposition}).
1141
1142 @item read-date
1143 RFC822 date when the part was read (@code{Content-Disposition}).
1144
1145 @item size
1146 The size (in octets) of the part (@code{Content-Disposition}).
1147
1148 @item sign
1149 What technology to sign this MML part with (@code{smime} or
1150 @code{pgpmime})
1151
1152 @item encrypt
1153 What technology to encrypt this MML part with (@code{smime} or
1154 @code{pgpmime})
1155
1156 @end table
1157
1158 Parameters for @samp{application/octet-stream}:
1159
1160 @table @samp
1161 @item type
1162 Type of the part; informal---meant for human readers
1163 (@code{Content-Type}).
1164 @end table
1165
1166 Parameters for @samp{message/external-body}:
1167
1168 @table @samp
1169 @item access-type
1170 A word indicating the supported access mechanism by which the file may
1171 be obtained.  Values include @samp{ftp}, @samp{anon-ftp}, @samp{tftp},
1172 @samp{localfile}, and @samp{mailserver}.  (@code{Content-Type}.)
1173
1174 @item expiration
1175 The RFC822 date after which the file may no longer be fetched.
1176 (@code{Content-Type}.)
1177
1178 @item size
1179 The size (in octets) of the file.  (@code{Content-Type}.)
1180
1181 @item permission
1182 Valid values are @samp{read} and @samp{read-write}
1183 (@code{Content-Type}).
1184
1185 @end table
1186
1187 Parameters for @samp{sign=smime}:
1188
1189 @table @samp
1190
1191 @item keyfile
1192 File containing key and certificate for signer.
1193
1194 @end table
1195
1196 Parameters for @samp{encrypt=smime}:
1197
1198 @table @samp
1199
1200 @item certfile
1201 File containing certificate for recipient.
1202
1203 @end table
1204
1205
1206 @node Advanced MML Example
1207 @section Advanced MML Example
1208
1209 Here's a complex multipart message.  It's a @samp{multipart/mixed} that
1210 contains many parts, one of which is a @samp{multipart/alternative}.
1211
1212 @example
1213 <#multipart type=mixed>
1214 <#part type=image/jpeg filename=~/rms.jpg disposition=inline>
1215 <#multipart type=alternative>
1216 This is a plain text part.
1217 <#part type=text/enriched name=enriched.txt>
1218 <center>This is a centered enriched part</center>
1219 <#/multipart>
1220 This is a new plain text part.
1221 <#part disposition=attachment>
1222 This plain text part is an attachment.
1223 <#/multipart>
1224 @end example
1225
1226 And this is the resulting @sc{mime} message:
1227
1228 @example
1229 Content-Type: multipart/mixed; boundary="=-=-="
1230
1231
1232 --=-=-=
1233
1234
1235
1236 --=-=-=
1237 Content-Type: image/jpeg;
1238  filename="~/rms.jpg"
1239 Content-Disposition: inline;
1240  filename="~/rms.jpg"
1241 Content-Transfer-Encoding: base64
1242
1243 /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRof
1244 Hh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCAAwADABAREA/8QAHwAA
1245 AQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQR
1246 BRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RF
1247 RkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ip
1248 qrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/9oACAEB
1249 AAA/AO/rifFHjldNuGsrDa0qcSSHkA+gHrXKw+LtWLrMb+RgTyhbr+HSug07xNqV9fQtZrNI
1250 AyiaE/NuBPOOOP0rvRNE880KOC8TbXXGCv1FPqjrF4LDR7u5L7SkTFT/ALWOP1xXgTuXfc7E
1251 sx6nua6rwp4IvvEM8chCxWxOdzn7wz6V9AaB4S07w9p5itow0rDLSY5Pt9K43xO66P4xs71m
1252 2QXiGCbA4yOVJ9+1aYORkdK434lyNH4ahCnG66VT9Nj15JFbPdX0MS43M4VQf5/yr2vSpLnw
1253 5ZW8dlCZ8KFXjOPX0/mK6rSPEGt3Angu44fNEReHYNvIH3TzXDeKNO8RX+kSX2ouZkicTIOc
1254 L+g7E810ulFjpVtv3bwgB3HJyK5L4quY/C9sVxk3ij/xx6850u7t1mtp/wDlpEw3An3Jr3Dw
1255 34gsbWza4nBlhC5LDsaW6+IFgupQyCF3iHH7gA7c9R9ay7zx6t7aX9jHC4smhfBkGCvHGfrm
1256 tLQ7hbnRrV1GPkAP1x1/Hr+Ncr8Vzjwrbf8AX6v/AKA9eQRyYlQk8Yx9K6XTNbkgia2ciSIn
1257 7p5Ga9Atte0LTLKO6it4i7dVRFJDcZ4PvXN+JvEMF9bILVGXJLSZ4zkjivRPDaeX4b08HOTC
1258 pOffmua+KkbS+GLVUGT9tT/0B68eeIpIFYjB70+OOVXyoOM9+M1eaWeCLzHPyHGO/NVWvJJm
1259 jQ8KGH1NfQWhXSXmh2c8eArRLwO3HSv/2Q==
1260
1261 --=-=-=
1262 Content-Type: multipart/alternative; boundary="==-=-="
1263
1264
1265 --==-=-=
1266
1267
1268 This is a plain text part.
1269
1270 --==-=-=
1271 Content-Type: text/enriched;
1272  name="enriched.txt"
1273
1274
1275 <center>This is a centered enriched part</center>
1276
1277 --==-=-=--
1278
1279 --=-=-=
1280
1281 This is a new plain text part.
1282
1283 --=-=-=
1284 Content-Disposition: attachment
1285
1286
1287 This plain text part is an attachment.
1288
1289 --=-=-=--
1290 @end example
1291
1292 @node Charset Translation
1293 @section Charset Translation
1294 @cindex charsets
1295
1296 During translation from MML to @sc{mime}, for each @sc{mime} part which
1297 has been composed inside Emacs, an appropriate charset has to be chosen.
1298
1299 @vindex mail-parse-charset
1300 If you are running a non-@sc{mule} Emacs, this process is simple: If the
1301 part contains any non-ASCII (8-bit) characters, the @sc{mime} charset
1302 given by @code{mail-parse-charset} (a symbol) is used.  (Never set this
1303 variable directly, though.  If you want to change the default charset,
1304 please consult the documentation of the package which you use to process
1305 @sc{mime} messages.
1306 @xref{Various Message Variables, , Various Message Variables, message, 
1307       Message Manual}, for example.)
1308 If there are only ASCII characters, the @sc{mime} charset US-ASCII is
1309 used, of course.
1310
1311 @cindex MULE
1312 @cindex UTF-8
1313 @cindex Unicode
1314 @vindex mm-mime-mule-charset-alist
1315 Things are slightly more complicated when running Emacs with @sc{mule}
1316 support.  In this case, a list of the @sc{mule} charsets used in the
1317 part is obtained, and the @sc{mule} charsets are translated to @sc{mime}
1318 charsets by consulting the variable @code{mm-mime-mule-charset-alist}.
1319 If this results in a single @sc{mime} charset, this is used to encode
1320 the part.  But if the resulting list of @sc{mime} charsets contains more
1321 than one element, two things can happen: If it is possible to encode the
1322 part via UTF-8, this charset is used.  (For this, Emacs must support
1323 the @code{utf-8} coding system, and the part must consist entirely of
1324 characters which have Unicode counterparts.)  If UTF-8 is not available
1325 for some reason, the part is split into several ones, so that each one
1326 can be encoded with a single @sc{mime} charset.  The part can only be
1327 split at line boundaries, though---if more than one @sc{mime} charset is
1328 required to encode a single line, it is not possible to encode the part.
1329
1330 @node Conversion
1331 @section Conversion
1332
1333 @findex mime-to-mml
1334 A (multipart) @sc{mime} message can be converted to MML with the
1335 @code{mime-to-mml} function.  It works on the message in the current
1336 buffer, and substitutes MML markup for @sc{mime} boundaries.
1337 Non-textual parts do not have their contents in the buffer, but instead
1338 have the contents in separate buffers that are referred to from the MML
1339 tags.
1340
1341 @findex mml-to-mime
1342 An MML message can be converted back to @sc{mime} by the
1343 @code{mml-to-mime} function.
1344
1345 These functions are in certain senses ``lossy''---you will not get back
1346 an identical message if you run @sc{mime-to-mml} and then
1347 @sc{mml-to-mime}.  Not only will trivial things like the order of the
1348 headers differ, but the contents of the headers may also be different.
1349 For instance, the original message may use base64 encoding on text,
1350 while @sc{mml-to-mime} may decide to use quoted-printable encoding, and
1351 so on.
1352
1353 In essence, however, these two functions should be the inverse of each
1354 other.  The resulting contents of the message should remain equivalent,
1355 if not identical.
1356
1357
1358 @node Standards
1359 @chapter Standards
1360
1361 The Emacs @sc{mime} library implements handling of various elements
1362 according to a (somewhat) large number of RFCs, drafts and standards
1363 documents.  This chapter lists the relevant ones.  They can all be
1364 fetched from @samp{http://quimby.gnus.org/notes/}.
1365
1366 @table @dfn
1367 @item RFC822
1368 @itemx STD11
1369 Standard for the Format of ARPA Internet Text Messages.
1370
1371 @item RFC1036
1372 Standard for Interchange of USENET Messages
1373
1374 @item RFC2045
1375 Format of Internet Message Bodies
1376
1377 @item RFC2046
1378 Media Types
1379
1380 @item RFC2047
1381 Message Header Extensions for Non-ASCII Text
1382
1383 @item RFC2048
1384 Registration Procedures
1385
1386 @item RFC2049
1387 Conformance Criteria and Examples
1388
1389 @item RFC2231
1390 MIME Parameter Value and Encoded Word Extensions: Character Sets,
1391 Languages, and Continuations
1392
1393 @item RFC1843
1394 HZ - A Data Format for Exchanging Files of Arbitrarily Mixed Chinese and
1395 ASCII characters
1396
1397 @item draft-ietf-drums-msg-fmt-05.txt
1398 Draft for the successor of RFC822
1399
1400 @item RFC2112
1401 The MIME Multipart/Related Content-type
1402
1403 @item RFC1892
1404 The Multipart/Report Content Type for the Reporting of Mail System
1405 Administrative Messages
1406
1407 @item RFC2183
1408 Communicating Presentation Information in Internet Messages: The
1409 Content-Disposition Header Field
1410
1411 @end table
1412
1413
1414 @node Index
1415 @chapter Index
1416 @printindex cp
1417
1418 @summarycontents
1419 @contents
1420 @bye
1421
1422 @c End: