]> code.delx.au - gnu-emacs/blob - lisp/mail/rmailout.el
(rmail-mbox-from): Move to mail-utils.
[gnu-emacs] / lisp / mail / rmailout.el
1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file
2
3 ;; Copyright (C) 1985, 1987, 1993, 1994, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'rmail)
29 (provide 'rmailout)
30
31 (defcustom rmail-output-decode-coding nil
32 "If non-nil, do coding system decoding when outputting message as Babyl."
33 :type 'boolean
34 :group 'rmail-output)
35
36 (defcustom rmail-output-file-alist nil
37 "Alist matching regexps to suggested output Rmail files.
38 This is a list of elements of the form (REGEXP . NAME-EXP).
39 The suggestion is taken if REGEXP matches anywhere in the message buffer.
40 NAME-EXP may be a string constant giving the file name to use,
41 or more generally it may be any kind of expression that returns
42 a file name as a string."
43 :type '(repeat (cons regexp
44 (choice :value ""
45 (string :tag "File Name")
46 sexp)))
47 :group 'rmail-output)
48
49 (defcustom rmail-fields-not-to-output nil
50 "Regexp describing fields to exclude when outputting a message to a file.
51 The function `rmail-delete-unwanted-fields' uses this, ignoring case."
52 :type '(choice (const :tag "None" nil)
53 regexp)
54 :group 'rmail-output)
55
56 (defun rmail-output-read-file-name ()
57 "Read the file name to use for `rmail-output'.
58 Set `rmail-default-file' to this name as well as returning it."
59 (let ((default-file
60 (let (answer tail)
61 (setq tail rmail-output-file-alist)
62 ;; Suggest a file based on a pattern match.
63 (while (and tail (not answer))
64 (save-excursion
65 (goto-char (point-min))
66 (if (re-search-forward (car (car tail)) nil t)
67 (setq answer (eval (cdr (car tail)))))
68 (setq tail (cdr tail))))
69 ;; If no suggestion, use same file as last time.
70 (or answer rmail-default-file))))
71 (let ((read-file
72 (expand-file-name
73 (read-file-name
74 (concat "Output message to mail file (default "
75 (file-name-nondirectory default-file)
76 "): ")
77 (file-name-directory default-file)
78 (abbreviate-file-name default-file))
79 (file-name-directory default-file))))
80 (setq rmail-default-file
81 (if (file-directory-p read-file)
82 (expand-file-name (file-name-nondirectory default-file)
83 read-file)
84 (expand-file-name
85 (or read-file (file-name-nondirectory default-file))
86 (file-name-directory default-file)))))))
87
88 (defun rmail-delete-unwanted-fields (preserve)
89 "Delete all headers matching `rmail-fields-not-to-output'.
90 Retains headers matching the regexp PRESERVE. Ignores case.
91 The buffer should be narrowed to just the header."
92 (if rmail-fields-not-to-output
93 (save-excursion
94 (goto-char (point-min))
95 (let ((case-fold-search t))
96 (while (re-search-forward rmail-fields-not-to-output nil t)
97 (beginning-of-line)
98 (unless (looking-at preserve)
99 (delete-region (point) (line-beginning-position 2))))))))
100 \f
101 (defun rmail-output-as-babyl (file-name nomsg)
102 "Convert the current buffer's text to Babyl and output to FILE-NAME.
103 Alters the current buffer's text, so it should be a temporary buffer.
104 If a buffer is visiting FILE-NAME, adds the text to that buffer
105 rather than saving the file directly. If the buffer is an Rmail buffer,
106 updates it accordingly. If no buffer is visiting FILE-NAME, appends
107 the text directly to FILE-NAME, and displays a \"Wrote file\" message
108 unless NOMSG is a symbol (neither nil nor t)."
109 (let ((coding-system-for-write 'emacs-mule-unix))
110 (save-restriction
111 (goto-char (point-min))
112 (search-forward "\n\n" nil 'move)
113 (narrow-to-region (point-min) (point))
114 (if rmail-fields-not-to-output
115 (rmail-delete-unwanted-fields nil)))
116
117 ;; Convert to Babyl format.
118 (rmail-convert-to-babyl-format)
119 ;; Write it into the file, or its buffer.
120 (let ((buf (find-buffer-visiting file-name))
121 (tembuf (current-buffer)))
122 (if (null buf)
123 (write-region (point-min) (point-max) file-name t nomsg)
124 (if (eq buf (current-buffer))
125 (error "Can't output message to same file it's already in"))
126 ;; File has been visited, in buffer BUF.
127 (set-buffer buf)
128 (let ((inhibit-read-only t)
129 (msg (bound-and-true-p rmail-current-message)))
130 ;; If MSG is non-nil, buffer is in RMAIL mode.
131 (if msg
132 (rmail-output-to-babyl-buffer tembuf msg)
133 ;; Output file not in rmail mode => just insert at the end.
134 (narrow-to-region (point-min) (1+ (buffer-size)))
135 (goto-char (point-max))
136 (insert-buffer-substring tembuf)))))))
137
138 ;; Called only if rmail-summary-exists, which means rmailsum is loaded.
139 (declare-function rmail-update-summary "rmailsum" (&rest ignore))
140
141 (defun rmail-output-to-babyl-buffer (tembuf msg)
142 "Copy message in TEMBUF into the current Babyl Rmail buffer.
143 Do what is necessary to make Rmail know about the new message, then
144 display message number MSG."
145 ;; Turn on Auto Save mode, if it's off in this buffer but enabled by default.
146 (and (not buffer-auto-save-file-name)
147 auto-save-default
148 (auto-save-mode t))
149 (rmail-maybe-set-message-counters)
150 (widen)
151 (narrow-to-region (point-max) (point-max))
152 (insert-buffer-substring tembuf)
153 (goto-char (point-min))
154 (widen)
155 (search-backward "\n\^_")
156 (narrow-to-region (point) (point-max))
157 (rmail-count-new-messages t)
158 (if (rmail-summary-exists)
159 (rmail-select-summary (rmail-update-summary)))
160 (rmail-show-message-1 msg))
161 \f
162 (defun rmail-convert-to-babyl-format ()
163 "Convert the mbox message in the current buffer to Babyl format."
164 (let ((count 0) (start (point-min))
165 (case-fold-search nil)
166 (buffer-undo-list t))
167 (goto-char (point-min))
168 (save-restriction
169 (unless (looking-at "^From ")
170 (error "Invalid mbox message"))
171 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
172 (rmail-nuke-pinhead-header)
173 ;; Decode base64 or quoted printable contents, Rmail style.
174 (let* ((header-end (save-excursion
175 (and (re-search-forward "\n\n" nil t)
176 (1- (point)))))
177 (case-fold-search t)
178 (quoted-printable-header-field-end
179 (save-excursion
180 (re-search-forward
181 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
182 header-end t)))
183 (base64-header-field-end
184 (and
185 ;; Don't decode non-text data.
186 (save-excursion
187 (re-search-forward
188 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
189 header-end t))
190 (save-excursion
191 (re-search-forward
192 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
193 header-end t)))))
194
195 (goto-char (point-max))
196 (if quoted-printable-header-field-end
197 (save-excursion
198 (unless (mail-unquote-printable-region
199 header-end (point) nil t t)
200 (message "Malformed MIME quoted-printable message"))
201 ;; Change "quoted-printable" to "8bit",
202 ;; to reflect the decoding we just did.
203 (goto-char quoted-printable-header-field-end)
204 (delete-region (point) (search-backward ":"))
205 (insert ": 8bit")))
206 (if base64-header-field-end
207 (save-excursion
208 (when (condition-case nil
209 (progn
210 (base64-decode-region
211 (1+ header-end)
212 (save-excursion
213 ;; Prevent base64-decode-region
214 ;; from removing newline characters.
215 (skip-chars-backward "\n\t ")
216 (point)))
217 t)
218 (error nil))
219 ;; Change "base64" to "8bit", to reflect the
220 ;; decoding we just did.
221 (goto-char base64-header-field-end)
222 (delete-region (point) (search-backward ":"))
223 (insert ": 8bit")))))
224 ;; Transform anything within the message text
225 ;; that might appear to be the end of a Babyl-format message.
226 (save-excursion
227 (save-restriction
228 (narrow-to-region start (point))
229 (goto-char (point-min))
230 (while (search-forward "\n\^_" nil t) ; single char
231 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
232 ;; This is for malformed messages that don't end in newline.
233 ;; There shouldn't be any, but some users say occasionally
234 ;; there are some.
235 (or (bolp) (newline))
236 (insert ?\^_)
237 (setq last-coding-system-used nil)
238 ;; Decode coding system, following specs in the message header,
239 ;; and record what coding system was decoded.
240 (if rmail-output-decode-coding
241 (let ((mime-charset
242 (if (save-excursion
243 (goto-char start)
244 (search-forward "\n\n" nil t)
245 (let ((case-fold-search t))
246 (re-search-backward
247 rmail-mime-charset-pattern
248 start t)))
249 (intern (downcase (match-string 1))))))
250 (rmail-decode-region start (point) mime-charset)))
251 (save-excursion
252 (goto-char start)
253 (forward-line 3)
254 (insert "X-Coding-System: "
255 (symbol-name last-coding-system-used)
256 "\n")))))
257
258 (defun rmail-nuke-pinhead-header ()
259 "Delete the \"From \" line in the current mbox message.
260 The variable `rmail-unix-mail-delimiter' specifies the From line format.
261 Replaces the From line with a \"Mail-from\" header. Adds \"Date\" and
262 \"From\" headers if they are not already present."
263 (save-excursion
264 (save-restriction
265 (let ((start (point))
266 (end (progn
267 (condition-case ()
268 (search-forward "\n\n")
269 (error
270 (goto-char (point-max))
271 (insert "\n\n")))
272 (point)))
273 has-from has-date)
274 (narrow-to-region start end)
275 (let ((case-fold-search t))
276 (goto-char start)
277 (setq has-from (search-forward "\nFrom:" nil t))
278 (goto-char start)
279 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
280 (goto-char start))
281 (let ((case-fold-search nil))
282 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
283 (replace-match
284 (concat
285 "Mail-from: \\&"
286 ;; Keep and reformat the date if we don't
287 ;; have a Date: field.
288 (if has-date
289 ""
290 (concat
291 "Date: \\2, \\4 \\3 \\9 \\5 "
292
293 ;; The timezone could be matched by group 7 or group 10.
294 ;; If neither of them matched, assume EST, since only
295 ;; Easterners would be so sloppy.
296 ;; It's a shame the substitution can't use "\\10".
297 (cond
298 ((/= (match-beginning 7) (match-end 7)) "\\7")
299 ((/= (match-beginning 10) (match-end 10))
300 (buffer-substring (match-beginning 10)
301 (match-end 10)))
302 (t "EST"))
303 "\n"))
304 ;; Keep and reformat the sender if we don't
305 ;; have a From: field.
306 (if has-from
307 ""
308 "From: \\1\n"))
309 t)))))))
310 \f
311 (autoload 'mail-mbox-from "mail-utils")
312
313 (defun rmail-output-as-mbox (file-name nomsg &optional as-seen)
314 "Convert the current buffer's text to mbox and output to FILE-NAME.
315 Alters the current buffer's text, so it should be a temporary buffer.
316 If a buffer is visiting FILE-NAME, adds the text to that buffer
317 rather than saving the file directly. If the buffer is an Rmail buffer,
318 updates it accordingly. If no buffer is visiting FILE-NAME, appends
319 the text directly to FILE-NAME, and displays a \"Wrote file\" message
320 unless NOMSG is a symbol (neither nil nor t).
321 AS-SEEN is non-nil if we are copying the message \"as seen\"."
322 (let ((case-fold-search t)
323 from date)
324 (goto-char (point-min))
325 ;; Preserve the Mail-From and MIME-Version fields
326 ;; even if they have been pruned.
327 (search-forward "\n\n" nil 'move)
328 (narrow-to-region (point-min) (point))
329 (rmail-delete-unwanted-fields
330 (if rmail-enable-mime "Mail-From"
331 "Mail-From\\|MIME-Version\\|Content-type"))
332 (goto-char (point-min))
333 (or (looking-at "From ")
334 (insert (mail-mbox-from)))
335 (widen)
336 ;; Make sure message ends with blank line.
337 (goto-char (point-max))
338 (unless (bolp)
339 (insert "\n"))
340 (unless (looking-back "\n\n")
341 (insert "\n"))
342 (goto-char (point-min))
343 (let ((buf (find-buffer-visiting file-name))
344 (tembuf (current-buffer)))
345 (if (null buf)
346 (let ((coding-system-for-write 'raw-text-unix))
347 (write-region (point-min) (point-max) file-name t nomsg))
348 (if (eq buf (current-buffer))
349 (error "Can't output message to same file it's already in"))
350 ;; File has been visited, in buffer BUF.
351 (set-buffer buf)
352 (let ((inhibit-read-only t)
353 (msg (and (boundp 'rmail-current-message)
354 rmail-current-message)))
355 (and msg as-seen
356 (error "Can't output \"as seen\" to a visited Rmail file"))
357 (if msg
358 (rmail-output-to-rmail-buffer tembuf msg)
359 ;; Output file not in Rmail mode => just insert at the end.
360 (narrow-to-region (point-min) (1+ (buffer-size)))
361 (goto-char (point-max))
362 (insert-buffer-substring tembuf)))))))
363
364 (defun rmail-output-to-rmail-buffer (tembuf msg)
365 "Copy message in TEMBUF into the current Rmail buffer.
366 Do what is necessary to make Rmail know about the new message. then
367 display message number MSG."
368 (save-excursion
369 (rmail-swap-buffers-maybe)
370 ;; Turn on Auto Save mode, if it's off in this
371 ;; buffer but enabled by default.
372 (and (not buffer-auto-save-file-name)
373 auto-save-default
374 (auto-save-mode t))
375 (rmail-maybe-set-message-counters)
376 (narrow-to-region (point-max) (point-max))
377 (insert-buffer-substring tembuf)
378 (rmail-count-new-messages t)
379 (if (rmail-summary-exists)
380 (rmail-select-summary (rmail-update-summary)))
381 (rmail-show-message-1 msg)))
382 \f
383 ;;; There are functions elsewhere in Emacs that use this function;
384 ;;; look at them before you change the calling method.
385 ;;;###autoload
386 (defun rmail-output (file-name &optional count noattribute not-rmail)
387 "Append this message to mail file FILE-NAME.
388 Writes mbox format, unless FILE-NAME exists and is Babyl format, in which
389 case it writes Babyl.
390
391 Interactively, the default file name comes from `rmail-default-file',
392 which is updated to the name you use in this command. In all uses, if
393 FILE-NAME is not absolute, it is expanded with the directory part of
394 `rmail-default-file'.
395
396 If a buffer is visiting FILE-NAME, adds the text to that buffer
397 rather than saving the file directly. If the buffer is an Rmail
398 buffer, updates it accordingly.
399
400 This command always outputs the complete message header, even if
401 the header display is currently pruned.
402
403 Optional prefix argument COUNT (default 1) says to output that
404 many consecutive messages, starting with the current one (ignoring
405 deleted messages). If `rmail-delete-after-output' is non-nil, deletes
406 messages after output.
407
408 The optional third argument NOATTRIBUTE, if non-nil, says not to
409 set the `filed' attribute, and not to display a \"Wrote file\"
410 message (if writing a file directly).
411
412 Set the optional fourth argument NOT-RMAIL non-nil if you call this
413 from a non-Rmail buffer. In this case, COUNT is ignored."
414 (interactive
415 (list (rmail-output-read-file-name)
416 (prefix-numeric-value current-prefix-arg)))
417 (or count (setq count 1))
418 (setq file-name
419 (expand-file-name file-name
420 (and rmail-default-file
421 (file-name-directory rmail-default-file))))
422 ;; Warn about creating new file.
423 (or (find-buffer-visiting file-name)
424 (file-exists-p file-name)
425 (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
426 (error "Output file does not exist"))
427 (if noattribute (setq noattribute 'nomsg))
428 (let ((babyl-format (and (file-readable-p file-name)
429 (mail-file-babyl-p file-name)))
430 (cur (current-buffer)))
431 (if not-rmail ; eg via message-fcc-handler-function
432 (with-temp-buffer
433 (insert-buffer-substring cur)
434 ;; Output in the appropriate format.
435 (if babyl-format
436 (progn
437 (goto-char (point-min))
438 ;; rmail-convert-to-babyl-format errors if no From line,
439 ;; whereas rmail-output-as-mbox inserts one.
440 (or (looking-at "From ")
441 (insert (mail-mbox-from)))
442 (rmail-output-as-babyl file-name noattribute))
443 (rmail-output-as-mbox file-name noattribute)))
444 ;; Called from an Rmail buffer.
445 (if rmail-buffer
446 (set-buffer rmail-buffer)
447 (error "There is no Rmail buffer"))
448 (let ((orig-count count)
449 beg end)
450 (while (> count 0)
451 (setq beg (rmail-msgbeg rmail-current-message)
452 end (rmail-msgend rmail-current-message))
453 ;; All access to the buffer's local variables is now finished...
454 (save-excursion
455 ;; ... so it is ok to go to a different buffer.
456 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer))
457 (setq cur (current-buffer))
458 (save-restriction
459 (widen)
460 (with-temp-buffer
461 (insert-buffer-substring cur beg end)
462 (if babyl-format
463 (rmail-output-as-babyl file-name noattribute)
464 (rmail-output-as-mbox file-name noattribute)))))
465 (or noattribute ; mark message as "filed"
466 (rmail-set-attribute rmail-filed-attr-index t))
467 (setq count (1- count))
468 (let ((next-message-p
469 (if rmail-delete-after-output
470 (rmail-delete-forward)
471 (if (> count 0)
472 (rmail-next-undeleted-message 1))))
473 (num-appended (- orig-count count)))
474 (if (and (> count 0) (not next-message-p))
475 (error "Only %d message%s appended" num-appended
476 (if (= num-appended 1) "" "s")))))))))
477
478 ;; FIXME nothing outside uses this, so NOT-RMAIL could be dropped.
479 ;; FIXME this duplicates code from rmail-output.
480 (defun rmail-output-as-seen (file-name &optional count noattribute not-rmail)
481 "Append this message to mbox file named FILE-NAME.
482 The details are as for `rmail-output', except that:
483 i) the header is output as currently seen
484 ii) this function cannot write to Babyl files
485 iii) an Rmail buffer cannot be visiting FILE-NAME
486
487 Note that if NOT-RMAIL is non-nil, there is no difference between this
488 function and `rmail-output'. This argument may be removed in future,
489 so you should call `rmail-output' directly in that case."
490 (interactive
491 (list (rmail-output-read-file-name)
492 (prefix-numeric-value current-prefix-arg)))
493 (if not-rmail
494 (rmail-output file-name count noattribute not-rmail)
495 (or count (setq count 1))
496 (setq file-name
497 (expand-file-name file-name
498 (and rmail-default-file
499 (file-name-directory rmail-default-file))))
500 ;; Warn about creating new file.
501 (or (find-buffer-visiting file-name)
502 (file-exists-p file-name)
503 (yes-or-no-p (concat "\"" file-name "\" does not exist, create it? "))
504 (error "Output file does not exist"))
505 ;; FIXME why not?
506 (if (and (file-readable-p file-name) (mail-file-babyl-p file-name))
507 (error "Cannot output `as seen' to a Babyl file"))
508 (if noattribute (setq noattribute 'nomsg))
509 (if rmail-buffer
510 (set-buffer rmail-buffer)
511 (error "There is no Rmail buffer"))
512 (let ((orig-count count)
513 (cur (current-buffer)))
514 (while (> count 0)
515 (let (beg end)
516 ;; If operating from whole-mbox buffer, get message bounds.
517 (or (rmail-buffers-swapped-p)
518 (setq beg (rmail-msgbeg rmail-current-message)
519 end (rmail-msgend rmail-current-message)))
520 (save-restriction
521 (widen)
522 ;; If operating from the view buffer, get the bounds.
523 (or beg
524 (setq beg (point-min)
525 end (point-max)))
526 (with-temp-buffer
527 (insert-buffer-substring cur beg end)
528 (rmail-output-as-mbox file-name noattribute t))))
529 (or noattribute ; mark message as "filed"
530 (rmail-set-attribute rmail-filed-attr-index t))
531 (setq count (1- count))
532 (let ((next-message-p
533 (if rmail-delete-after-output
534 (rmail-delete-forward)
535 (if (> count 0)
536 (rmail-next-undeleted-message 1))))
537 (num-appended (- orig-count count)))
538 (if (and (> count 0) (not next-message-p))
539 (error "Only %d message%s appended" num-appended
540 (if (= num-appended 1) "" "s"))))))))
541
542 \f
543 ;;;###autoload
544 (defun rmail-output-body-to-file (file-name)
545 "Write this message body to the file FILE-NAME.
546 Interactively, the default file name comes from either the message
547 \"Subject\" header, or from `rmail-default-body-file'. Updates the value
548 of `rmail-default-body-file' accordingly. In all uses, if FILE-NAME
549 is not absolute, it is expanded with the directory part of
550 `rmail-default-body-file'.
551
552 Note that this overwrites FILE-NAME (after confirmation), rather
553 than appending to it. Deletes the message after writing if
554 `rmail-delete-after-output' is non-nil."
555 (interactive
556 (let ((default-file
557 (or (mail-fetch-field "Subject")
558 rmail-default-body-file)))
559 (setq default-file
560 (replace-regexp-in-string ":" "-" default-file))
561 (setq default-file
562 (replace-regexp-in-string " " "-" default-file))
563 (list (setq rmail-default-body-file
564 (read-file-name
565 "Output message body to file: "
566 (and default-file (file-name-directory default-file))
567 default-file
568 nil default-file)))))
569 (setq file-name
570 (expand-file-name file-name
571 (and rmail-default-body-file
572 (file-name-directory rmail-default-body-file))))
573 (save-excursion
574 (goto-char (point-min))
575 (search-forward "\n\n")
576 (and (file-exists-p file-name)
577 (not (y-or-n-p (format "File %s exists; overwrite? " file-name)))
578 (error "Operation aborted"))
579 (write-region (point) (point-max) file-name))
580 (if rmail-delete-after-output
581 (rmail-delete-forward)))
582
583 ;; arch-tag: 4059abf0-f249-4be4-8e0d-602d370d01d1
584 ;;; rmailout.el ends here