]> code.delx.au - gnu-emacs/blob - lisp/mail/unrmail.el
* net/tramp-sh.el (tramp-sh-handle-set-file-acl): Add argument to
[gnu-emacs] / lisp / mail / unrmail.el
1 ;;; unrmail.el --- convert Rmail Babyl files to mbox files
2
3 ;; Copyright (C) 1992, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 ;;;###autoload
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"))
36 (let ((error nil))
37 (while command-line-args-left
38 (or (unrmail (car command-line-args-left)
39 (concat (car command-line-args-left) ".mail"))
40 (setq error t))
41 (setq command-line-args-left (cdr command-line-args-left)))
42 (message "Done")
43 (kill-emacs (if error 1 0))))
44
45 (declare-function mail-mbox-from "mail-utils" ())
46 (defvar rmime-magic-string) ; in rmime.el, if you have it
47
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.
57
58 See also `rmail-mbox-format'."
59 :type '(choice (const mboxrd)
60 (const mboxro))
61 :version "24.4"
62 :group 'rmail-files)
63
64 ;;;###autoload
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): ")
69 (with-temp-buffer
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)
76
77 (if (not (looking-at "BABYL OPTIONS"))
78 (error "This file is not in Babyl format"))
79
80 ;; Decode the file contents just as Rmail did.
81 (let ((coding-system rmail-file-coding-system)
82 from to)
83 (goto-char (point-min))
84 (search-forward "\n\^_" nil t) ; Skip BABYL header.
85 (setq from (point))
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))
92 (setq 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
100 from to
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))
108
109 (setq buffer-file-coding-system nil)
110
111 ;; We currently don't use this value, but maybe we should.
112 (setq save-buffer-coding-system
113 (or coding-system 'undecided)))
114
115 ;; Default the directory of TO-FILE based on where FILE is.
116 (setq to-file (expand-file-name to-file default-directory))
117 (condition-case ()
118 (delete-file to-file)
119 (file-error nil))
120 (message "Writing messages to %s..." to-file)
121 (goto-char (point-min))
122
123 (let ((temp-buffer (get-buffer-create " unrmail"))
124 (from-buffer (current-buffer)))
125
126 ;; Process the messages one by one.
127 (while (re-search-forward "^\^_\^l" nil t)
128 (let ((beg (point))
129 (end (save-excursion
130 (if (re-search-forward "^\^_\\(\^l\\|\\'\\)" nil t)
131 (match-beginning 0)
132 (point-max))))
133 (coding 'raw-text)
134 label-line attrs keywords
135 mail-from reformatted)
136 (with-current-buffer temp-buffer
137 (setq buffer-undo-list t)
138 (erase-buffer)
139 (setq buffer-file-coding-system coding)
140 (insert-buffer-substring from-buffer beg end)
141 (goto-char (point-min))
142 (forward-line 1)
143 ;; Record whether the header is reformatted.
144 (setq reformatted (= (following-char) ?1))
145
146 ;; Collect the label line, then get the attributes
147 ;; and the keywords from it.
148 (setq label-line
149 (buffer-substring (point)
150 (save-excursion (forward-line 1)
151 (point))))
152 (re-search-forward ",, ?")
153 (unless (eolp)
154 (setq keywords
155 (buffer-substring (point)
156 (progn (end-of-line)
157 (1- (point)))))
158 ;; Mbox rmail needs the spaces. Bug#2303.
159 ;;; (setq keywords
160 ;;; (replace-regexp-in-string ", " "," keywords))
161 )
162
163 (setq attrs
164 (list
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 ?-)))
173
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))
177 (if reformatted
178 (progn
179 (forward-line 2)
180 ;; Delete Summary-Line headers.
181 (let ((case-fold-search t))
182 (while (looking-at "Summary-Line:")
183 (forward-line 1)))
184 (delete-region (point-min) (point))
185 ;; Delete the old reformatted header.
186 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
187 (forward-line -1)
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)))
195
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))))
201
202 ;; Some operations on the message header itself.
203 (goto-char (point-min))
204 (save-restriction
205 (narrow-to-region
206 (point-min)
207 (save-excursion (search-forward "\n\n" nil 'move) (point)))
208
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.
215 (if from
216 (format "%s\n" from)))
217 (mail-mbox-from)))
218
219 ;; If the message specifies a coding system, use it.
220 (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
221 (if maybe-coding
222 (setq coding
223 ;; Force Unix EOLs.
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)))
229
230 ;; Delete the Mail-From: header field if any.
231 (when (re-search-forward "^Mail-from:" nil t)
232 (beginning-of-line)
233 (delete-region (point)
234 (progn (forward-line 1) (point)))))
235
236 (goto-char (point-min))
237 ;; Insert the `From ' line.
238 (insert mail-from)
239 ;; Record the keywords and attributes in our special way.
240 (insert "X-RMAIL-ATTRIBUTES: " (apply 'string attrs) "\n")
241 (when keywords
242 (insert "X-RMAIL-KEYWORDS: " keywords "\n"))
243 (goto-char (point-min))
244 ;; Convert From to >From, etc.
245 (let ((case-fold-search nil)
246 (fromline (if (eq 'mboxrd unrmail-mbox-format)
247 "^>*From "
248 "^From ")))
249 (while (re-search-forward fromline nil t)
250 (beginning-of-line)
251 (insert ?>)
252 (forward-line 1)))
253 (goto-char (point-max))
254 ;; Add terminator blank line to message.
255 (insert "\n")
256 ;; Write it to the output file, suitably encoded.
257 (let ((coding-system-for-write coding))
258 (write-region (point-min) (point-max) to-file t
259 'nomsg)))))
260 (kill-buffer temp-buffer))
261 (message "Writing messages to %s...done" to-file)))
262
263 (provide 'unrmail)
264
265 ;;; unrmail.el ends here