]> code.delx.au - gnu-emacs/blob - lisp/mail/unrmail.el
(proced-after-send-signal-hook): Use defcustom.
[gnu-emacs] / lisp / mail / unrmail.el
1 ;;; unrmail.el --- convert Rmail Babyl files to mailbox files
2
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 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 ;;;###autoload
29 (defun batch-unrmail ()
30 "Convert old-style Rmail Babyl files to system inbox format.
31 Specify the input Rmail Babyl file names as command line arguments.
32 For each Rmail file, the corresponding output file name
33 is made by adding `.mail' at the end.
34 For example, invoke `emacs -batch -f batch-unrmail RMAIL'."
35 (if (not noninteractive)
36 (error "`batch-unrmail' is to be used only with -batch"))
37 (let ((error nil))
38 (while command-line-args-left
39 (or (unrmail (car command-line-args-left)
40 (concat (car command-line-args-left) ".mail"))
41 (setq error t))
42 (setq command-line-args-left (cdr command-line-args-left)))
43 (message "Done")
44 (kill-emacs (if error 1 0))))
45
46 (declare-function mail-strip-quoted-names "mail-utils" (address))
47
48 ;;;###autoload
49 (defun unrmail (file to-file)
50 "Convert old-style Rmail Babyl file FILE to system inbox format file TO-FILE."
51 (interactive "fUnrmail (babyl file): \nFUnrmail into (new mailbox file): ")
52 (with-temp-buffer
53 ;; Read in the old Rmail file with no decoding.
54 (let ((coding-system-for-read 'raw-text))
55 (insert-file-contents file))
56 ;; But make it multibyte.
57 (set-buffer-multibyte t)
58 (setq buffer-file-coding-system 'raw-text-unix)
59
60 (if (not (looking-at "BABYL OPTIONS"))
61 (error "This file is not in Babyl format"))
62
63 ;; Decode the file contents just as Rmail did.
64 (let ((modifiedp (buffer-modified-p))
65 (coding-system rmail-file-coding-system)
66 from to)
67 (goto-char (point-min))
68 (search-forward "\n\^_" nil t) ; Skip BABYL header.
69 (setq from (point))
70 (goto-char (point-max))
71 (search-backward "\n\^_" from 'mv)
72 (setq to (point))
73 (unless (and coding-system
74 (coding-system-p coding-system))
75 (setq coding-system
76 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
77 ;; earlier versions did that with the current buffer's encoding.
78 ;; So we want to favor detection of emacs-mule (whose normal
79 ;; priority is quite low), but still allow detection of other
80 ;; encodings if emacs-mule won't fit. The call to
81 ;; detect-coding-with-priority below achieves that.
82 (car (detect-coding-with-priority
83 from to
84 '((coding-category-emacs-mule . emacs-mule))))))
85 (unless (memq coding-system
86 '(undecided undecided-unix))
87 (set-buffer-modified-p t) ; avoid locking when decoding
88 (let ((buffer-undo-list t))
89 (decode-coding-region from to coding-system))
90 (setq coding-system last-coding-system-used))
91
92 (setq buffer-file-coding-system nil)
93
94 ;; We currently don't use this value, but maybe we should.
95 (setq save-buffer-coding-system
96 (or coding-system 'undecided)))
97
98 ;; Default the directory of TO-FILE based on where FILE is.
99 (setq to-file (expand-file-name to-file default-directory))
100 (condition-case ()
101 (delete-file to-file)
102 (file-error nil))
103 (message "Writing messages to %s..." to-file)
104 (goto-char (point-min))
105
106 (let ((temp-buffer (get-buffer-create " unrmail"))
107 (from-buffer (current-buffer)))
108
109 ;; Process the messages one by one.
110 (while (re-search-forward "^\^_\^l" nil t)
111 (let ((beg (point))
112 (end (save-excursion
113 (if (re-search-forward "^\^_\\(\^l\\|\\'\\)" nil t)
114 (match-beginning 0)
115 (point-max))))
116 (coding 'raw-text)
117 label-line attrs keywords
118 mail-from reformatted)
119 (with-current-buffer temp-buffer
120 (setq buffer-undo-list t)
121 (erase-buffer)
122 (setq buffer-file-coding-system coding)
123 (insert-buffer-substring from-buffer beg end)
124 (goto-char (point-min))
125 (forward-line 1)
126 ;; Record whether the header is reformatted.
127 (setq reformatted (= (following-char) ?1))
128
129 ;; Collect the label line, then get the attributes
130 ;; and the keywords from it.
131 (setq label-line
132 (buffer-substring (point)
133 (save-excursion (forward-line 1)
134 (point))))
135 (search-forward ",,")
136 (unless (eolp)
137 (setq keywords
138 (buffer-substring (point)
139 (progn (end-of-line)
140 (1- (point)))))
141 (setq keywords
142 (replace-regexp-in-string ", " "," keywords)))
143
144 (setq attrs
145 (list
146 (if (string-match ", answered," label-line) ?A ?-)
147 (if (string-match ", deleted," label-line) ?D ?-)
148 (if (string-match ", edited," label-line) ?E ?-)
149 (if (string-match ", filed," label-line) ?F ?-)
150 (if (string-match ", retried," label-line) ?R ?-)
151 (if (string-match ", forwarded," label-line) ?S ?-)
152 (if (string-match ", unseen," label-line) ?U ?-)
153 (if (string-match ", resent," label-line) ?r ?-)))
154
155 ;; Delete the special Babyl lines at the start,
156 ;; and the ***EOOH*** line, and the reformatted header if any.
157 (goto-char (point-min))
158 (if reformatted
159 (progn
160 (forward-line 2)
161 ;; Delete Summary-Line headers.
162 (let ((case-fold-search t))
163 (while (looking-at "Summary-Line:")
164 (forward-line 1)))
165 (delete-region (point-min) (point))
166 ;; Delete the old reformatted header.
167 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
168 (forward-line -1)
169 (let ((start (point)))
170 (search-forward "\n\n")
171 (delete-region start (point))))
172 ;; Not reformatted. Delete the special
173 ;; lines before the real header.
174 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
175 (delete-region (point-min) (point)))
176
177 ;; Handle rmime formatting.
178 (when (require 'rmime nil t)
179 (let ((start (point)))
180 (while (search-forward rmime-magic-string nil t))
181 (delete-region start (point))))
182
183 ;; Some operations on the message header itself.
184 (goto-char (point-min))
185 (save-restriction
186 (narrow-to-region
187 (point-min)
188 (save-excursion (search-forward "\n\n" nil 'move) (point)))
189
190 ;; Fetch or construct what we should use in the `From ' line.
191 (setq mail-from
192 (or (mail-fetch-field "Mail-From")
193 (concat "From "
194 (mail-strip-quoted-names
195 (or (mail-fetch-field "from")
196 (mail-fetch-field "really-from")
197 (mail-fetch-field "sender")
198 "unknown"))
199 " "
200 (let ((date (mail-fetch-field "date")))
201 (or
202 (and date
203 (ignore-errors
204 (current-time-string
205 (date-to-time date))))
206 (current-time-string))))))
207
208 ;; If the message specifies a coding system, use it.
209 (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
210 (if maybe-coding
211 (setq coding
212 ;; Force Unix EOLs.
213 (coding-system-change-eol-conversion
214 (intern maybe-coding) 0))
215 ;; If there's no X-Coding-System header, assume the
216 ;; message was never decoded.
217 (setq coding 'raw-text-unix)))
218
219 ;; Delete the Mail-From: header field if any.
220 (when (re-search-forward "^Mail-from:" nil t)
221 (beginning-of-line)
222 (delete-region (point)
223 (progn (forward-line 1) (point)))))
224
225 (goto-char (point-min))
226 ;; Insert the `From ' line.
227 (insert mail-from "\n")
228 ;; Record the keywords and attributes in our special way.
229 (insert "X-RMAIL-ATTRIBUTES: " (apply 'string attrs) "\n")
230 (when keywords
231 (insert "X-RMAIL-KEYWORDS: " keywords "\n"))
232 (goto-char (point-min))
233 ;; ``Quote'' "\nFrom " as "\n>From "
234 ;; (note that this isn't really quoting, as there is no requirement
235 ;; that "\n[>]+From " be quoted in the same transparent way.)
236 (let ((case-fold-search nil))
237 (while (search-forward "\nFrom " nil t)
238 (forward-char -5)
239 (insert ?>)))
240 ;; Write it to the output file, suitably encoded.
241 (let ((coding-system-for-write coding))
242 (write-region (point-min) (point-max) to-file t
243 'nomsg)))))
244 (kill-buffer temp-buffer))
245 (message "Writing messages to %s...done" to-file)))
246
247 (provide 'unrmail)
248
249 ;; arch-tag: 14c6290d-60b2-456f-8909-5c2387de6acb
250 ;;; unrmail.el ends here