1 ;;; unrmail.el --- convert Rmail Babyl files to mbox files
3 ;; Copyright (C) 1992, 2001-2017 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
28 (defun batch-unrmail ()
29 "Convert old-style Rmail Babyl files to mbox format.
30 Specify the input Rmail Babyl file names as command line arguments.
31 For each Rmail file, the corresponding output file name
32 is made by adding `.mail' at the end.
33 For example, invoke `emacs -batch -f batch-unrmail RMAIL'."
34 (if (not noninteractive
)
35 (error "`batch-unrmail' is to be used only with -batch"))
37 (while command-line-args-left
38 (or (unrmail (car command-line-args-left
)
39 (concat (car command-line-args-left
) ".mail"))
41 (setq command-line-args-left
(cdr command-line-args-left
)))
43 (kill-emacs (if error
1 0))))
45 (declare-function mail-mbox-from
"mail-utils" ())
46 (defvar rmime-magic-string
) ; in rmime.el, if you have it
48 (defcustom unrmail-mbox-format
'mboxrd
49 "The mbox format that `unrmail' should produce.
50 These formats separate messages using lines that start with \"From \".
51 Therefore any lines in the message bodies that start with \"From \"
52 must be quoted. The `mboxo' format just prepends a \">\" to such lines.
53 This is not reversible, because given a line starting with \">From \" in
54 an mboxo file, it is not possible to know whether the original had a \">\"
55 or not. The `mboxrd' format avoids this by also quoting \">From \" as
56 \">>From \", and so on. For this reason, mboxrd is recommended.
58 See also `rmail-mbox-format'."
59 :type
'(choice (const mboxrd
)
65 (defun unrmail (file to-file
)
66 "Convert old-style Rmail Babyl file FILE to mbox format file TO-FILE.
67 The variable `unrmail-mbox-format' controls which mbox format to use."
68 (interactive "fUnrmail (babyl file): \nFUnrmail into (new mailbox file): ")
70 ;; Read in the old Rmail file with no decoding.
71 (let ((coding-system-for-read 'raw-text
))
72 (insert-file-contents file
))
73 ;; But make it multibyte.
74 (set-buffer-multibyte t
)
75 (setq buffer-file-coding-system
'raw-text-unix
)
77 (if (not (looking-at "BABYL OPTIONS"))
78 (error "This file is not in Babyl format"))
80 ;; Decode the file contents just as Rmail did.
81 (let ((coding-system rmail-file-coding-system
)
83 (goto-char (point-min))
84 (search-forward "\n\^_" nil t
) ; Skip BABYL header.
86 (goto-char (point-max))
87 (search-backward "\n\^_" from
'mv
)
88 (if (= from
(setq to
(point)))
89 (error "The input file contains no messages"))
90 (unless (and coding-system
91 (coding-system-p coding-system
))
93 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
94 ;; earlier versions did that with the current buffer's encoding.
95 ;; So we want to favor detection of emacs-mule (whose normal
96 ;; priority is quite low), but still allow detection of other
97 ;; encodings if emacs-mule won't fit.
98 (car (with-coding-priority '(emacs-mule)
99 (detect-coding-region from to
)))))
100 (unless (memq coding-system
101 '(undecided undecided-unix
))
102 (set-buffer-modified-p t
) ; avoid locking when decoding
103 (let ((buffer-undo-list t
))
104 (decode-coding-region from to coding-system
))
105 (setq coding-system last-coding-system-used
))
107 (setq buffer-file-coding-system nil
)
109 ;; We currently don't use this value, but maybe we should.
110 (setq save-buffer-coding-system
111 (or coding-system
'undecided
)))
113 ;; Default the directory of TO-FILE based on where FILE is.
114 (setq to-file
(expand-file-name to-file default-directory
))
116 (delete-file to-file
)
118 (message "Writing messages to %s..." to-file
)
119 (goto-char (point-min))
121 (let ((temp-buffer (get-buffer-create " unrmail"))
122 (from-buffer (current-buffer)))
124 ;; Process the messages one by one.
125 (while (re-search-forward "^\^_\^l" nil t
)
128 (if (re-search-forward "^\^_\\(\^l\\|\\'\\)" nil t
)
132 label-line attrs keywords
133 mail-from reformatted
)
134 (with-current-buffer temp-buffer
135 (setq buffer-undo-list t
)
137 (setq buffer-file-coding-system coding
)
138 (insert-buffer-substring from-buffer beg end
)
139 (goto-char (point-min))
141 ;; Record whether the header is reformatted.
142 (setq reformatted
(= (following-char) ?
1))
144 ;; Collect the label line, then get the attributes
145 ;; and the keywords from it.
147 (buffer-substring (point)
148 (save-excursion (forward-line 1)
150 (re-search-forward ",, ?")
153 (buffer-substring (point)
156 ;; Mbox rmail needs the spaces. Bug#2303.
158 ;;; (replace-regexp-in-string ", " "," keywords))
163 (if (string-match ", answered," label-line
) ?A ?-
)
164 (if (string-match ", deleted," label-line
) ?D ?-
)
165 (if (string-match ", edited," label-line
) ?E ?-
)
166 (if (string-match ", filed," label-line
) ?F ?-
)
167 (if (string-match ", retried," label-line
) ?R ?-
)
168 (if (string-match ", forwarded," label-line
) ?S ?-
)
169 (if (string-match ", unseen," label-line
) ?U ?-
)
170 (if (string-match ", resent," label-line
) ?r ?-
)))
172 ;; Delete the special Babyl lines at the start,
173 ;; and the ***EOOH*** line, and the reformatted header if any.
174 (goto-char (point-min))
178 ;; Delete Summary-Line headers.
179 (let ((case-fold-search t
))
180 (while (looking-at "Summary-Line:")
182 (delete-region (point-min) (point))
183 ;; Delete the old reformatted header.
184 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
186 (let ((start (point)))
187 (search-forward "\n\n")
188 (delete-region start
(point))))
189 ;; Not reformatted. Delete the special
190 ;; lines before the real header.
191 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
192 (delete-region (point-min) (point)))
194 ;; Handle rmime formatting.
195 (when (require 'rmime nil t
)
196 (let ((start (point)))
197 (while (search-forward rmime-magic-string nil t
))
198 (delete-region start
(point))))
200 ;; Some operations on the message header itself.
201 (goto-char (point-min))
205 (save-excursion (search-forward "\n\n" nil
'move
) (point)))
207 ;; Fetch or construct what we should use in the `From ' line.
208 (setq mail-from
(or (let ((from (mail-fetch-field "Mail-From")))
209 ;; mail-mbox-from (below) returns a
210 ;; string that ends in a newline, but
211 ;; but mail-fetch-field does not, so
212 ;; we append a newline here.
214 (format "%s\n" from
)))
217 ;; If the message specifies a coding system, use it.
218 (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
222 (coding-system-change-eol-conversion
223 (intern maybe-coding
) 0))
224 ;; If there's no X-Coding-System header, assume the
225 ;; message was never decoded.
226 (setq coding
'raw-text-unix
)))
228 ;; Delete the Mail-From: header field if any.
229 (when (re-search-forward "^Mail-from:" nil t
)
231 (delete-region (point)
232 (progn (forward-line 1) (point)))))
234 (goto-char (point-min))
235 ;; Insert the `From ' line.
237 ;; Record the keywords and attributes in our special way.
238 (insert "X-RMAIL-ATTRIBUTES: " (apply 'string attrs
) "\n")
240 (insert "X-RMAIL-KEYWORDS: " keywords
"\n"))
241 ;; Convert From to >From, etc.
242 (let ((case-fold-search nil
)
243 (fromline (if (eq 'mboxrd unrmail-mbox-format
)
246 (while (re-search-forward fromline nil t
)
250 (goto-char (point-max))
251 ;; Add terminator blank line to message.
253 ;; Write it to the output file, suitably encoded.
254 (let ((coding-system-for-write coding
))
255 (write-region (point-min) (point-max) to-file t
257 (kill-buffer temp-buffer
))
258 (message "Writing messages to %s...done" to-file
)))
262 ;;; unrmail.el ends here