1 ;;; unrmail.el --- convert Rmail Babyl files to mbox files
3 ;; Copyright (C) 1992, 2001-2013 Free Software Foundation, Inc.
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 <http://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 `mxbord' 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. The call to
98 ;; detect-coding-with-priority below achieves that.
99 (car (detect-coding-with-priority
101 '((coding-category-emacs-mule . emacs-mule
))))))
102 (unless (memq coding-system
103 '(undecided undecided-unix
))
104 (set-buffer-modified-p t
) ; avoid locking when decoding
105 (let ((buffer-undo-list t
))
106 (decode-coding-region from to coding-system
))
107 (setq coding-system last-coding-system-used
))
109 (setq buffer-file-coding-system nil
)
111 ;; We currently don't use this value, but maybe we should.
112 (setq save-buffer-coding-system
113 (or coding-system
'undecided
)))
115 ;; Default the directory of TO-FILE based on where FILE is.
116 (setq to-file
(expand-file-name to-file default-directory
))
118 (delete-file to-file
)
120 (message "Writing messages to %s..." to-file
)
121 (goto-char (point-min))
123 (let ((temp-buffer (get-buffer-create " unrmail"))
124 (from-buffer (current-buffer)))
126 ;; Process the messages one by one.
127 (while (re-search-forward "^\^_\^l" nil t
)
130 (if (re-search-forward "^\^_\\(\^l\\|\\'\\)" nil t
)
134 label-line attrs keywords
135 mail-from reformatted
)
136 (with-current-buffer temp-buffer
137 (setq buffer-undo-list t
)
139 (setq buffer-file-coding-system coding
)
140 (insert-buffer-substring from-buffer beg end
)
141 (goto-char (point-min))
143 ;; Record whether the header is reformatted.
144 (setq reformatted
(= (following-char) ?
1))
146 ;; Collect the label line, then get the attributes
147 ;; and the keywords from it.
149 (buffer-substring (point)
150 (save-excursion (forward-line 1)
152 (re-search-forward ",, ?")
155 (buffer-substring (point)
158 ;; Mbox rmail needs the spaces. Bug#2303.
160 ;;; (replace-regexp-in-string ", " "," keywords))
165 (if (string-match ", answered," label-line
) ?A ?-
)
166 (if (string-match ", deleted," label-line
) ?D ?-
)
167 (if (string-match ", edited," label-line
) ?E ?-
)
168 (if (string-match ", filed," label-line
) ?F ?-
)
169 (if (string-match ", retried," label-line
) ?R ?-
)
170 (if (string-match ", forwarded," label-line
) ?S ?-
)
171 (if (string-match ", unseen," label-line
) ?U ?-
)
172 (if (string-match ", resent," label-line
) ?r ?-
)))
174 ;; Delete the special Babyl lines at the start,
175 ;; and the ***EOOH*** line, and the reformatted header if any.
176 (goto-char (point-min))
180 ;; Delete Summary-Line headers.
181 (let ((case-fold-search t
))
182 (while (looking-at "Summary-Line:")
184 (delete-region (point-min) (point))
185 ;; Delete the old reformatted header.
186 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
188 (let ((start (point)))
189 (search-forward "\n\n")
190 (delete-region start
(point))))
191 ;; Not reformatted. Delete the special
192 ;; lines before the real header.
193 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
194 (delete-region (point-min) (point)))
196 ;; Handle rmime formatting.
197 (when (require 'rmime nil t
)
198 (let ((start (point)))
199 (while (search-forward rmime-magic-string nil t
))
200 (delete-region start
(point))))
202 ;; Some operations on the message header itself.
203 (goto-char (point-min))
207 (save-excursion (search-forward "\n\n" nil
'move
) (point)))
209 ;; Fetch or construct what we should use in the `From ' line.
210 (setq mail-from
(or (let ((from (mail-fetch-field "Mail-From")))
211 ;; mail-mbox-from (below) returns a
212 ;; string that ends in a newline, but
213 ;; but mail-fetch-field does not, so
214 ;; we append a newline here.
216 (format "%s\n" from
)))
219 ;; If the message specifies a coding system, use it.
220 (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
224 (coding-system-change-eol-conversion
225 (intern maybe-coding
) 0))
226 ;; If there's no X-Coding-System header, assume the
227 ;; message was never decoded.
228 (setq coding
'raw-text-unix
)))
230 ;; Delete the Mail-From: header field if any.
231 (when (re-search-forward "^Mail-from:" nil t
)
233 (delete-region (point)
234 (progn (forward-line 1) (point)))))
236 (goto-char (point-min))
237 ;; Insert the `From ' line.
239 ;; Record the keywords and attributes in our special way.
240 (insert "X-RMAIL-ATTRIBUTES: " (apply 'string attrs
) "\n")
242 (insert "X-RMAIL-KEYWORDS: " keywords
"\n"))
243 ;; Convert From to >From, etc.
244 (let ((case-fold-search nil
)
245 (fromline (if (eq 'mboxrd unrmail-mbox-format
)
248 (while (re-search-forward fromline nil t
)
252 (goto-char (point-max))
253 ;; Add terminator blank line to message.
255 ;; Write it to the output file, suitably encoded.
256 (let ((coding-system-for-write coding
))
257 (write-region (point-min) (point-max) to-file t
259 (kill-buffer temp-buffer
))
260 (message "Writing messages to %s...done" to-file
)))
264 ;;; unrmail.el ends here