1 ;;; rmailout.el --- "RMAIL" mail reader for Emacs: output message to a file
3 ;; Copyright (C) 1985, 1987, 1993-1994, 2001-2016 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (defcustom rmail-output-decode-coding nil
33 "If non-nil, do coding system decoding when outputting message as Babyl."
37 (defcustom rmail-output-file-alist nil
38 "Alist matching regexps to suggested output Rmail files.
39 This is a list of elements of the form (REGEXP . NAME-EXP).
40 The suggestion is taken if REGEXP matches anywhere in the message buffer.
41 NAME-EXP may be a string constant giving the file name to use,
42 or more generally it may be any kind of expression that returns
43 a file name as a string."
44 :type
'(repeat (cons regexp
46 (string :tag
"File Name")
49 ;; This is risky because NAME-EXP gets evalled.
50 ;;;###autoload(put 'rmail-output-file-alist 'risky-local-variable t)
52 (defcustom rmail-fields-not-to-output nil
53 "Regexp describing fields to exclude when outputting a message to a file.
54 The function `rmail-delete-unwanted-fields' uses this, ignoring case."
55 :type
'(choice (const :tag
"None" nil
)
59 (defun rmail-output-read-file-name ()
60 "Read the file name to use for `rmail-output'.
61 Set `rmail-default-file' to this name as well as returning it.
62 This uses `rmail-output-file-alist'."
65 (when rmail-output-file-alist
66 (or rmail-buffer
(error "There is no Rmail buffer"))
68 (set-buffer rmail-buffer
)
69 (let ((beg (rmail-msgbeg rmail-current-message
))
70 (end (rmail-msgend rmail-current-message
)))
71 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer
))
75 (narrow-to-region beg end
)
76 (let ((tail rmail-output-file-alist
)
78 ;; Suggest a file based on a pattern match.
79 (while (and tail
(not answer
))
80 (goto-char (point-min))
81 (if (re-search-forward (caar tail
) nil t
)
89 Error evaluating `rmail-output-file-alist' element:
93 (caar tail
) (cdar tail
) err
)
96 (setq tail
(cdr tail
)))
98 ;; If no suggestion, use same file as last time.
103 (concat "Output message to mail file (default "
104 (file-name-nondirectory default-file
)
106 (file-name-directory default-file
)
107 (abbreviate-file-name default-file
))
108 (file-name-directory default-file
))))
109 (setq rmail-default-file
110 (if (file-directory-p read-file
)
111 (expand-file-name (file-name-nondirectory default-file
)
114 (or read-file
(file-name-nondirectory default-file
))
115 (file-name-directory default-file
))))))
117 (defun rmail-delete-unwanted-fields (preserve)
118 "Delete all headers matching `rmail-fields-not-to-output'.
119 Retains headers matching the regexp PRESERVE. Ignores case.
120 The buffer should be narrowed to just the header."
121 (if rmail-fields-not-to-output
123 (goto-char (point-min))
124 (let ((case-fold-search t
))
125 (while (re-search-forward rmail-fields-not-to-output nil t
)
127 (unless (looking-at preserve
)
128 (delete-region (point) (line-beginning-position 2))))))))
130 (defun rmail-output-as-babyl (file-name nomsg
)
131 "Convert the current buffer's text to Babyl and output to FILE-NAME.
132 Alters the current buffer's text, so it should be a temporary buffer.
133 If a buffer is visiting FILE-NAME, adds the text to that buffer
134 rather than saving the file directly. If the buffer is an Rmail buffer,
135 updates it accordingly. If no buffer is visiting FILE-NAME, appends
136 the text directly to FILE-NAME, and displays a \"Wrote file\" message
137 unless NOMSG is a symbol (neither nil nor t)."
138 (let ((coding-system-for-write 'emacs-mule-unix
))
140 (goto-char (point-min))
141 (search-forward "\n\n" nil
'move
)
142 (narrow-to-region (point-min) (point))
143 (if rmail-fields-not-to-output
144 (rmail-delete-unwanted-fields nil
)))
146 ;; Convert to Babyl format.
147 (rmail-convert-to-babyl-format)
148 ;; Write it into the file, or its buffer.
149 (let ((buf (find-buffer-visiting file-name
))
150 (tembuf (current-buffer)))
152 (write-region (point-min) (point-max) file-name t nomsg
)
153 (if (eq buf
(current-buffer))
154 (error "Can't output message to same file it's already in"))
155 ;; File has been visited, in buffer BUF.
157 (let ((inhibit-read-only t
)
158 (msg (bound-and-true-p rmail-current-message
)))
159 ;; If MSG is non-nil, buffer is in RMAIL mode.
161 (rmail-output-to-babyl-buffer tembuf msg
)
162 ;; Output file not in rmail mode => just insert at the end.
163 (narrow-to-region (point-min) (1+ (buffer-size)))
164 (goto-char (point-max))
165 (insert-buffer-substring tembuf
)))))))
167 ;; Called only if rmail-summary-exists, which means rmailsum is loaded.
168 (declare-function rmail-update-summary
"rmailsum" (&rest ignore
))
170 (defun rmail-output-to-babyl-buffer (tembuf msg
)
171 "Copy message in TEMBUF into the current Babyl Rmail buffer.
172 Do what is necessary to make Rmail know about the new message, then
173 display message number MSG."
174 ;; Turn on Auto Save mode, if it's off in this buffer but enabled by default.
175 (and (not buffer-auto-save-file-name
)
178 (rmail-maybe-set-message-counters)
180 (narrow-to-region (point-max) (point-max))
181 (insert-buffer-substring tembuf
)
182 (goto-char (point-min))
184 (search-backward "\n\^_")
185 (narrow-to-region (point) (point-max))
186 (rmail-count-new-messages t
)
187 (if (rmail-summary-exists)
188 (rmail-select-summary (rmail-update-summary)))
189 (rmail-show-message-1 msg
))
191 (defun rmail-convert-to-babyl-format ()
192 "Convert the mbox message in the current buffer to Babyl format."
193 (let ((count 0) (start (point-min))
194 (case-fold-search nil
)
195 (buffer-undo-list t
))
196 (goto-char (point-min))
198 (unless (looking-at "^From ")
199 (error "Invalid mbox message"))
200 (insert "\^L\n0,,\n*** EOOH ***\n")
201 (rmail-nuke-pinhead-header)
202 ;; Decode base64 or quoted printable contents, Rmail style.
203 (let* ((header-end (save-excursion
204 (and (re-search-forward "\n\n" nil t
)
207 (quoted-printable-header-field-end
210 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
212 (base64-header-field-end
214 ;; Don't decode non-text data.
217 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
221 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
224 (goto-char (point-max))
225 (if quoted-printable-header-field-end
227 (unless (mail-unquote-printable-region
228 header-end
(point) nil t t
)
229 (message "Malformed MIME quoted-printable message"))
230 ;; Change "quoted-printable" to "8bit",
231 ;; to reflect the decoding we just did.
232 (goto-char quoted-printable-header-field-end
)
233 (delete-region (point) (search-backward ":"))
235 (if base64-header-field-end
237 (when (condition-case nil
239 (base64-decode-region
242 ;; Prevent base64-decode-region
243 ;; from removing newline characters.
244 (skip-chars-backward "\n\t ")
248 ;; Change "base64" to "8bit", to reflect the
249 ;; decoding we just did.
250 (goto-char base64-header-field-end
)
251 (delete-region (point) (search-backward ":"))
252 (insert ": 8bit")))))
253 ;; Transform anything within the message text
254 ;; that might appear to be the end of a Babyl-format message.
257 (narrow-to-region start
(point))
258 (goto-char (point-min))
259 (while (search-forward "\n\^_" nil t
) ; single char
260 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
261 ;; This is for malformed messages that don't end in newline.
262 ;; There shouldn't be any, but some users say occasionally
264 (or (bolp) (newline))
266 (setq last-coding-system-used nil
)
267 ;; Decode coding system, following specs in the message header,
268 ;; and record what coding system was decoded.
269 (if rmail-output-decode-coding
273 (search-forward "\n\n" nil t
)
274 (let ((case-fold-search t
))
276 rmail-mime-charset-pattern
278 (intern (downcase (match-string 1))))))
279 (rmail-decode-region start
(point) mime-charset
)))
283 (insert "X-Coding-System: "
284 (symbol-name last-coding-system-used
)
287 (defun rmail-nuke-pinhead-header ()
288 "Delete the \"From \" line in the current mbox message.
289 The variable `rmail-unix-mail-delimiter' specifies the From line format.
290 Replaces the From line with a \"Mail-from\" header. Adds \"Date\" and
291 \"From\" headers if they are not already present."
294 (let ((start (point))
297 (search-forward "\n\n")
299 (goto-char (point-max))
303 (narrow-to-region start end
)
304 (let ((case-fold-search t
))
306 (setq has-from
(search-forward "\nFrom:" nil t
))
308 (setq has-date
(and (search-forward "\nDate:" nil t
) (point)))
310 (let ((case-fold-search nil
))
311 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter
) nil t
)
315 ;; Keep and reformat the date if we don't
316 ;; have a Date: field.
320 "Date: \\2, \\4 \\3 \\9 \\5 "
322 ;; The timezone could be matched by group 7 or group 10.
323 ;; If neither of them matched, assume EST, since only
324 ;; Easterners would be so sloppy.
325 ;; It's a shame the substitution can't use "\\10".
327 ((/= (match-beginning 7) (match-end 7)) "\\7")
328 ((/= (match-beginning 10) (match-end 10))
329 (buffer-substring (match-beginning 10)
333 ;; Keep and reformat the sender if we don't
334 ;; have a From: field.
340 (autoload 'mail-mbox-from
"mail-utils")
342 (defun rmail-output-as-mbox (file-name nomsg
&optional as-seen
)
343 "Convert the current buffer's text to mbox and output to FILE-NAME.
344 Alters the current buffer's text, so it should be a temporary buffer.
345 If a buffer is visiting FILE-NAME, adds the text to that buffer
346 rather than saving the file directly. If the buffer is an Rmail buffer,
347 updates it accordingly. If no buffer is visiting FILE-NAME, appends
348 the text directly to FILE-NAME, and displays a \"Wrote file\" message
349 unless NOMSG is a symbol (neither nil nor t).
350 AS-SEEN is non-nil if we are copying the message \"as seen\"."
351 (let ((case-fold-search t
)
354 (goto-char (point-min))
355 ;; Preserve the Mail-From and MIME-Version fields
356 ;; even if they have been pruned.
357 (search-forward "\n\n" nil
'move
)
358 (narrow-to-region (point-min) (point))
359 (rmail-delete-unwanted-fields
360 (if rmail-enable-mime
"Mail-From"
361 "Mail-From\\|MIME-Version\\|Content-type"))
362 (goto-char (point-min))
363 (or (looking-at "From ")
364 (insert (mail-mbox-from)))
366 ;; Make sure message ends with blank line.
367 (goto-char (point-max))
368 (rmail-ensure-blank-line)
369 (goto-char (point-min))
370 (let ((buf (find-buffer-visiting file-name
))
371 (tembuf (current-buffer)))
372 (when (string-match "[.]gpg\\'" file-name
)
373 (setq encrypted-file-name file-name
374 file-name
(substring file-name
0 (match-beginning 0))))
376 (let ((coding-system-for-write 'raw-text-unix
)
377 (coding-system-for-read 'raw-text-unix
))
378 ;; If the specified file is encrypted, decrypt it.
379 (when encrypted-file-name
381 (insert-file-contents encrypted-file-name
)
382 (write-region 1 (point-max) file-name nil
'nomsg
)))
383 ;; FIXME should ensure existing file ends with a blank line.
384 (write-region (point-min) (point-max) file-name t
385 (if (or nomsg encrypted-file-name
)
387 ;; If the specified file was encrypted, re-encrypt it.
388 (when encrypted-file-name
389 ;; Save the old encrypted file as a backup.
390 (rename-file encrypted-file-name
391 (make-backup-file-name encrypted-file-name
)
394 (call-process "gpg" nil nil
395 "--use-agent" "--batch" "--no-tty"
399 ;; Delete the unencrypted file if encryption succeeded.
400 (delete-file file-name
)
401 ;; If encrypting failed, put back the original
402 ;; encrypted file and signal an error.
403 (rename-file (make-backup-file-name encrypted-file-name
)
406 (error "Encryption failed; %s unchanged"
407 encrypted-file-name
))
409 (message "Added to %s" encrypted-file-name
)))
411 (if (eq buf
(current-buffer))
412 (error "Can't output message to same file it's already in"))
413 ;; File has been visited, in buffer BUF.
415 (let ((inhibit-read-only t
)
416 (msg (and (boundp 'rmail-current-message
)
417 rmail-current-message
)))
419 (error "Can't output \"as seen\" to a visited Rmail file"))
421 (rmail-output-to-rmail-buffer tembuf msg
)
422 ;; Output file not in Rmail mode => just insert at the end.
423 (narrow-to-region (point-min) (1+ (buffer-size)))
424 (goto-char (point-max))
425 (insert-buffer-substring tembuf
)))))))
427 (defun rmail-output-to-rmail-buffer (tembuf msg
)
428 "Copy message in TEMBUF into the current Rmail buffer.
429 Do what is necessary to make Rmail know about the new message. then
430 display message number MSG."
432 (rmail-swap-buffers-maybe)
433 (rmail-modify-format)
434 ;; Turn on Auto Save mode, if it's off in this buffer but enabled
436 (and (not buffer-auto-save-file-name
)
439 (rmail-maybe-set-message-counters)
440 ;; Insert the new message after the last old message.
442 (unless (zerop (buffer-size))
443 ;; Make sure the last old message ends with a blank line.
444 (goto-char (point-max))
445 (rmail-ensure-blank-line)
446 ;; Insert the new message at the end.
447 (narrow-to-region (point-max) (point-max)))
448 (insert-buffer-substring tembuf
)
449 (rmail-count-new-messages t
)
450 ;; FIXME should re-use existing windows.
451 (if (rmail-summary-exists)
452 (rmail-select-summary (rmail-update-summary)))
453 (rmail-show-message-1 msg
)))
455 ;;; There are functions elsewhere in Emacs that use this function;
456 ;;; look at them before you change the calling method.
458 (defun rmail-output (file-name &optional count noattribute not-rmail
)
459 "Append this message to mail file FILE-NAME.
460 Writes mbox format, unless FILE-NAME exists and is Babyl format, in which
461 case it writes Babyl.
463 Interactively, the default file name comes from `rmail-default-file',
464 which is updated to the name you use in this command. In all uses, if
465 FILE-NAME is not absolute, it is expanded with the directory part of
466 `rmail-default-file'.
468 If a buffer is visiting FILE-NAME, adds the text to that buffer
469 rather than saving the file directly. If the buffer is an Rmail
470 buffer, updates it accordingly.
472 This command always outputs the complete message header, even if
473 the header display is currently pruned.
475 Optional prefix argument COUNT (default 1) says to output that
476 many consecutive messages, starting with the current one (ignoring
477 deleted messages). If `rmail-delete-after-output' is non-nil, deletes
478 messages after output.
480 The optional third argument NOATTRIBUTE, if non-nil, says not to
481 set the `filed' attribute, and not to display a \"Wrote file\"
482 message (if writing a file directly).
484 Set the optional fourth argument NOT-RMAIL non-nil if you call this
485 from a non-Rmail buffer. In this case, COUNT is ignored."
487 (list (rmail-output-read-file-name)
488 (prefix-numeric-value current-prefix-arg
)))
489 (or count
(setq count
1))
491 (expand-file-name file-name
492 (and rmail-default-file
493 (file-name-directory rmail-default-file
))))
494 ;; Warn about creating new file.
495 (or (find-buffer-visiting file-name
)
496 (file-exists-p file-name
)
497 (yes-or-no-p (concat "\"" file-name
"\" does not exist, create it? "))
498 (error "Output file does not exist"))
499 (if noattribute
(setq noattribute
'nomsg
))
500 (let ((babyl-format (and (file-readable-p file-name
)
501 (mail-file-babyl-p file-name
)))
502 (cur (current-buffer))
503 (buf (find-buffer-visiting file-name
)))
505 ;; If a babyl file is visited in a buffer, is it visited as babyl
507 (and babyl-format buf
508 (with-current-buffer buf
512 (goto-char (point-min))
514 (looking-at "BABYL OPTIONS:"))))))
516 (if not-rmail
; eg via message-fcc-handler-function
518 (insert-buffer-substring cur
)
519 ;; Output in the appropriate format.
522 (goto-char (point-min))
523 ;; rmail-convert-to-babyl-format errors if no From line,
524 ;; whereas rmail-output-as-mbox inserts one.
525 (or (looking-at "From ")
526 (insert (mail-mbox-from)))
527 (rmail-output-as-babyl file-name noattribute
))
528 (rmail-output-as-mbox file-name noattribute
)))
529 ;; Called from an Rmail buffer.
531 (set-buffer rmail-buffer
)
532 (error "There is no Rmail buffer"))
533 (if (zerop rmail-total-messages
)
534 (error "No messages to output"))
535 (let ((orig-count count
)
538 (setq beg
(rmail-msgbeg rmail-current-message
)
539 end
(rmail-msgend rmail-current-message
))
540 ;; All access to the buffer's local variables is now finished...
542 ;; ... so it is ok to go to a different buffer.
543 (if (rmail-buffers-swapped-p) (set-buffer rmail-view-buffer
))
544 (setq cur
(current-buffer))
548 (insert-buffer-substring cur beg end
)
550 (rmail-output-as-babyl file-name noattribute
)
551 (rmail-output-as-mbox file-name noattribute
)))))
552 (or noattribute
; mark message as "filed"
553 (rmail-set-attribute rmail-filed-attr-index t
))
554 (setq count
(1- count
))
555 (let ((next-message-p
556 (if rmail-delete-after-output
557 (rmail-delete-forward)
559 (rmail-next-undeleted-message 1))))
560 (num-appended (- orig-count count
)))
561 (if (and (> count
0) (not next-message-p
))
562 (error "Only %d message%s appended" num-appended
563 (if (= num-appended
1) "" "s")))))))))
565 ;; FIXME nothing outside uses this, so NOT-RMAIL could be dropped.
566 ;; FIXME this duplicates code from rmail-output.
568 (defun rmail-output-as-seen (file-name &optional count noattribute not-rmail
)
569 "Append this message to mbox file named FILE-NAME.
570 The details are as for `rmail-output', except that:
571 i) the header is output as currently seen
572 ii) this function cannot write to Babyl files
573 iii) an Rmail buffer cannot be visiting FILE-NAME
575 Note that if NOT-RMAIL is non-nil, there is no difference between this
576 function and `rmail-output'. This argument may be removed in future,
577 so you should call `rmail-output' directly in that case."
579 (list (rmail-output-read-file-name)
580 (prefix-numeric-value current-prefix-arg
)))
582 (rmail-output file-name count noattribute not-rmail
)
583 (or count
(setq count
1))
585 (expand-file-name file-name
586 (and rmail-default-file
587 (file-name-directory rmail-default-file
))))
588 ;; Warn about creating new file.
589 (or (find-buffer-visiting file-name
)
590 (file-exists-p file-name
)
591 (yes-or-no-p (concat "\"" file-name
"\" does not exist, create it? "))
592 (error "Output file does not exist"))
594 (if (and (file-readable-p file-name
) (mail-file-babyl-p file-name
))
595 (error "Cannot output `as seen' to a Babyl file"))
596 (if noattribute
(setq noattribute
'nomsg
))
598 (set-buffer rmail-buffer
)
599 (error "There is no Rmail buffer"))
600 (if (zerop rmail-total-messages
)
601 (error "No messages to output"))
602 (let ((orig-count count
)
603 (cur (current-buffer)))
606 ;; If operating from whole-mbox buffer, get message bounds.
607 (or (rmail-buffers-swapped-p)
608 (setq beg
(rmail-msgbeg rmail-current-message
)
609 end
(rmail-msgend rmail-current-message
)))
612 ;; If operating from the view buffer, get the bounds.
614 (setq beg
(point-min)
617 (insert-buffer-substring cur beg end
)
618 (rmail-output-as-mbox file-name noattribute t
))))
619 (or noattribute
; mark message as "filed"
620 (rmail-set-attribute rmail-filed-attr-index t
))
621 (setq count
(1- count
))
622 (let ((next-message-p
623 (if rmail-delete-after-output
624 (rmail-delete-forward)
626 (rmail-next-undeleted-message 1))))
627 (num-appended (- orig-count count
)))
628 (if (and (> count
0) (not next-message-p
))
629 (error "Only %d message%s appended" num-appended
630 (if (= num-appended
1) "" "s"))))))))
634 (defun rmail-output-body-to-file (file-name)
635 "Write this message body to the file FILE-NAME.
636 Interactively, the default file name comes from either the message
637 \"Subject\" header, or from `rmail-default-body-file'. Updates the value
638 of `rmail-default-body-file' accordingly. In all uses, if FILE-NAME
639 is not absolute, it is expanded with the directory part of
640 `rmail-default-body-file'.
642 Note that this overwrites FILE-NAME (after confirmation), rather
643 than appending to it. Deletes the message after writing if
644 `rmail-delete-after-output' is non-nil."
647 (or (mail-fetch-field "Subject")
648 rmail-default-body-file
)))
650 (replace-regexp-in-string ":" "-" default-file
))
652 (replace-regexp-in-string " " "-" default-file
))
653 (list (setq rmail-default-body-file
655 "Output message body to file: "
656 (and default-file
(file-name-directory default-file
))
658 nil default-file
)))))
660 (expand-file-name file-name
661 (and rmail-default-body-file
662 (file-name-directory rmail-default-body-file
))))
663 (if (zerop rmail-current-message
)
664 (error "No message to output"))
666 (goto-char (point-min))
667 (search-forward "\n\n")
668 (and (file-exists-p file-name
)
669 (not (y-or-n-p (format "File %s exists; overwrite? " file-name
)))
670 (error "Operation aborted"))
671 (write-region (point) (point-max) file-name
))
672 (if rmail-delete-after-output
673 (rmail-delete-forward)))
675 ;;; rmailout.el ends here