1 ;;; unrmail.el --- convert Rmail files to mailbox files
3 ;;; Copyright (C) 1992, 2002, 2003, 2004, 2005 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 2, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
29 (defvar command-line-args-left
) ;Avoid 'free variable' warning
32 (defun batch-unrmail ()
33 "Convert Rmail files to system inbox format.
34 Specify the input Rmail file names as command line arguments.
35 For each Rmail file, the corresponding output file name
36 is made by adding `.mail' at the end.
37 For example, invoke `emacs -batch -f batch-unrmail RMAIL'."
38 ;; command-line-args-left is what is left of the command line (from startup.el)
39 (if (not noninteractive
)
40 (error "`batch-unrmail' is to be used only with -batch"))
42 (while command-line-args-left
43 (or (unrmail (car command-line-args-left
)
44 (concat (car command-line-args-left
) ".mail"))
46 (setq command-line-args-left
(cdr command-line-args-left
)))
48 (kill-emacs (if error
1 0))))
51 (defun unrmail (file to-file
)
52 "Convert Rmail file FILE to system inbox format file TO-FILE."
53 (interactive "fUnrmail (rmail file): \nFUnrmail into (new mailbox file): ")
55 ;; Read in the old Rmail file with no decoding.
56 (let ((coding-system-for-read 'raw-text
))
57 (insert-file-contents file
))
58 ;; But make it multibyte.
59 (set-buffer-multibyte t
)
61 (if (not (looking-at "BABYL OPTIONS"))
62 (error "This file is not in Babyl format"))
64 ;; Decode the file contents just as Rmail did.
65 (let ((modifiedp (buffer-modified-p))
66 (coding-system rmail-file-coding-system
)
68 (goto-char (point-min))
69 (search-forward "\n\^_" nil t
) ; Skip BABYL header.
71 (goto-char (point-max))
72 (search-backward "\n\^_" from
'mv
)
74 (unless (and coding-system
75 (coding-system-p coding-system
))
77 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
78 ;; earlier versions did that with the current buffer's encoding.
79 ;; So we want to favor detection of emacs-mule (whose normal
80 ;; priority is quite low), but still allow detection of other
81 ;; encodings if emacs-mule won't fit. The call to
82 ;; detect-coding-with-priority below achieves that.
83 (car (detect-coding-with-priority
85 '((coding-category-emacs-mule . emacs-mule
))))))
86 (unless (memq coding-system
87 '(undecided undecided-unix
))
88 (set-buffer-modified-p t
) ; avoid locking when decoding
89 (let ((buffer-undo-list t
))
90 (decode-coding-region from to coding-system
))
91 (setq coding-system last-coding-system-used
))
93 (setq buffer-file-coding-system nil
)
95 ;; We currently don't use this value, but maybe we should.
96 (setq save-buffer-coding-system
97 (or coding-system
'undecided
)))
99 ;; Default the directory of TO-FILE based on where FILE is.
100 (setq to-file
(expand-file-name to-file default-directory
))
102 (delete-file to-file
)
104 (message "Writing messages to %s..." to-file
)
105 (goto-char (point-min))
107 (let ((temp-buffer (get-buffer-create " unrmail"))
108 (from-buffer (current-buffer)))
110 ;; Process the messages one by one.
111 (while (search-forward "\^_\^l" nil t
)
114 (if (search-forward "\^_" nil t
)
115 (1- (point)) (point-max))))
117 label-line attrs keywords
118 mail-from reformatted
)
119 (with-current-buffer temp-buffer
120 (setq buffer-undo-list t
)
122 (setq buffer-file-coding-system coding
)
123 (insert-buffer-substring from-buffer beg end
)
124 (goto-char (point-min))
126 ;; Record whether the header is reformatted.
127 (setq reformatted
(= (following-char) ?
1))
129 ;; Collect the label line, then get the attributes
130 ;; and the keywords from it.
132 (buffer-substring (point)
133 (save-excursion (forward-line 1)
135 (search-forward ",,")
138 (buffer-substring (point)
142 (replace-regexp-in-string ", " "," keywords
)))
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 ", resent," label-line
) ?R ?-
)
151 (if (string-match ", unseen," label-line
) ?\ ?-
)
152 (if (string-match ", stored," label-line
) ?S ?-
)))
154 ;; Delete the special Babyl lines at the start,
155 ;; and the ***EOOH*** line, and the reformatted header if any.
156 (goto-char (point-min))
160 ;; Delete Summary-Line headers.
161 (let ((case-fold-search t
))
162 (while (looking-at "Summary-Line:")
164 (delete-region (point-min) (point))
165 ;; Delete the old reformatted header.
166 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
168 (let ((start (point)))
169 (search-forward "\n\n")
170 (delete-region start
(point))))
171 ;; Not reformatted. Delete the special
172 ;; lines before the real header.
173 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
174 (delete-region (point-min) (point)))
176 ;; Some operations on the message header itself.
177 (goto-char (point-min))
181 (save-excursion (search-forward "\n\n" nil
'move
) (point)))
183 ;; Fetch or construct what we should use in the `From ' line.
185 (or (mail-fetch-field "Mail-From")
187 (mail-strip-quoted-names (or (mail-fetch-field "from")
188 (mail-fetch-field "really-from")
189 (mail-fetch-field "sender")
191 " " (current-time-string))))
193 ;; If the message specifies a coding system, use it.
194 (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
196 (setq coding
(intern maybe-coding
))))
198 ;; Delete the Mail-From: header field if any.
199 (when (re-search-forward "^Mail-from:" nil t
)
201 (delete-region (point)
202 (progn (forward-line 1) (point)))))
204 (goto-char (point-min))
205 ;; Insert the `From ' line.
206 (insert mail-from
"\n")
207 ;; Record the keywords and attributes in our special way.
208 (insert "X-BABYL-V6-ATTRIBUTES: " (apply 'string attrs
) "\n")
210 (insert "X-BABYL-V6-KEYWORDS: " keywords
"\n"))
211 (goto-char (point-min))
212 ;; ``Quote'' "\nFrom " as "\n>From "
213 ;; (note that this isn't really quoting, as there is no requirement
214 ;; that "\n[>]+From " be quoted in the same transparent way.)
215 (let ((case-fold-search nil
))
216 (while (search-forward "\nFrom " nil t
)
219 ;; Write it to the output file.
220 (write-region (point-min) (point-max) to-file t
222 (kill-buffer temp-buffer
))
223 (message "Writing messages to %s...done" to-file
)))
227 ;;; unrmail.el ends here
229 ;;; arch-tag: 14c6290d-60b2-456f-8909-5c2387de6acb