(calendar-other-dates): New function.
[emacs.git] / lisp / mail / unrmail.el
blobd1989a85862f7cff01ba789d2f5d91d8964c56d7
1 ;;; unrmail.el --- convert Rmail files to mailbox files
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
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, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;;; Code:
30 (defvar command-line-args-left) ;Avoid 'free variable' warning
32 ;;;###autoload
33 (defun batch-unrmail ()
34 "Convert Rmail files to system inbox format.
35 Specify the input Rmail file names as command line arguments.
36 For each Rmail file, the corresponding output file name
37 is made by adding `.mail' at the end.
38 For example, invoke `emacs -batch -f batch-unrmail RMAIL'."
39 ;; command-line-args-left is what is left of the command line (from startup.el)
40 (if (not noninteractive)
41 (error "`batch-unrmail' is to be used only with -batch"))
42 (let ((error nil))
43 (while command-line-args-left
44 (or (unrmail (car command-line-args-left)
45 (concat (car command-line-args-left) ".mail"))
46 (setq error t))
47 (setq command-line-args-left (cdr command-line-args-left)))
48 (message "Done")
49 (kill-emacs (if error 1 0))))
51 (declare-function mail-strip-quoted-names "mail-utils" (address))
53 ;;;###autoload
54 (defun unrmail (file to-file)
55 "Convert Rmail file FILE to system inbox format file TO-FILE."
56 (interactive "fUnrmail (rmail file): \nFUnrmail into (new mailbox file): ")
57 (with-temp-buffer
58 ;; Read in the old Rmail file with no decoding.
59 (let ((coding-system-for-read 'raw-text))
60 (insert-file-contents file))
61 ;; But make it multibyte.
62 (set-buffer-multibyte t)
64 (if (not (looking-at "BABYL OPTIONS"))
65 (error "This file is not in Babyl format"))
67 ;; Decode the file contents just as Rmail did.
68 (let ((modifiedp (buffer-modified-p))
69 (coding-system rmail-file-coding-system)
70 from to)
71 (goto-char (point-min))
72 (search-forward "\n\^_" nil t) ; Skip BABYL header.
73 (setq from (point))
74 (goto-char (point-max))
75 (search-backward "\n\^_" from 'mv)
76 (setq to (point))
77 (unless (and coding-system
78 (coding-system-p coding-system))
79 (setq coding-system
80 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
81 ;; earlier versions did that with the current buffer's encoding.
82 ;; So we want to favor detection of emacs-mule (whose normal
83 ;; priority is quite low), but still allow detection of other
84 ;; encodings if emacs-mule won't fit. The call to
85 ;; detect-coding-with-priority below achieves that.
86 (car (detect-coding-with-priority
87 from to
88 '((coding-category-emacs-mule . emacs-mule))))))
89 (unless (memq coding-system
90 '(undecided undecided-unix))
91 (set-buffer-modified-p t) ; avoid locking when decoding
92 (let ((buffer-undo-list t))
93 (decode-coding-region from to coding-system))
94 (setq coding-system last-coding-system-used))
96 (setq buffer-file-coding-system nil)
98 ;; We currently don't use this value, but maybe we should.
99 (setq save-buffer-coding-system
100 (or coding-system 'undecided)))
102 ;; Default the directory of TO-FILE based on where FILE is.
103 (setq to-file (expand-file-name to-file default-directory))
104 (condition-case ()
105 (delete-file to-file)
106 (file-error nil))
107 (message "Writing messages to %s..." to-file)
108 (goto-char (point-min))
110 (let ((temp-buffer (get-buffer-create " unrmail"))
111 (from-buffer (current-buffer)))
113 ;; Process the messages one by one.
114 (while (search-forward "\^_\^l" nil t)
115 (let ((beg (point))
116 (end (save-excursion
117 (if (search-forward "\^_" nil t)
118 (1- (point)) (point-max))))
119 (coding 'raw-text)
120 label-line attrs keywords
121 mail-from reformatted)
122 (with-current-buffer temp-buffer
123 (setq buffer-undo-list t)
124 (erase-buffer)
125 (setq buffer-file-coding-system coding)
126 (insert-buffer-substring from-buffer beg end)
127 (goto-char (point-min))
128 (forward-line 1)
129 ;; Record whether the header is reformatted.
130 (setq reformatted (= (following-char) ?1))
132 ;; Collect the label line, then get the attributes
133 ;; and the keywords from it.
134 (setq label-line
135 (buffer-substring (point)
136 (save-excursion (forward-line 1)
137 (point))))
138 (search-forward ",,")
139 (unless (eolp)
140 (setq keywords
141 (buffer-substring (point)
142 (progn (end-of-line)
143 (1- (point)))))
144 (setq keywords
145 (replace-regexp-in-string ", " "," keywords)))
147 (setq attrs
148 (list
149 (if (string-match ", answered," label-line) ?A ?-)
150 (if (string-match ", deleted," label-line) ?D ?-)
151 (if (string-match ", edited," label-line) ?E ?-)
152 (if (string-match ", filed," label-line) ?F ?-)
153 (if (string-match ", resent," label-line) ?R ?-)
154 (if (string-match ", unseen," label-line) ?\ ?-)
155 (if (string-match ", stored," label-line) ?S ?-)))
157 ;; Delete the special Babyl lines at the start,
158 ;; and the ***EOOH*** line, and the reformatted header if any.
159 (goto-char (point-min))
160 (if reformatted
161 (progn
162 (forward-line 2)
163 ;; Delete Summary-Line headers.
164 (let ((case-fold-search t))
165 (while (looking-at "Summary-Line:")
166 (forward-line 1)))
167 (delete-region (point-min) (point))
168 ;; Delete the old reformatted header.
169 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
170 (forward-line -1)
171 (let ((start (point)))
172 (search-forward "\n\n")
173 (delete-region start (point))))
174 ;; Not reformatted. Delete the special
175 ;; lines before the real header.
176 (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
177 (delete-region (point-min) (point)))
179 ;; Some operations on the message header itself.
180 (goto-char (point-min))
181 (save-restriction
182 (narrow-to-region
183 (point-min)
184 (save-excursion (search-forward "\n\n" nil 'move) (point)))
186 ;; Fetch or construct what we should use in the `From ' line.
187 (setq mail-from
188 (or (mail-fetch-field "Mail-From")
189 (concat "From "
190 (mail-strip-quoted-names (or (mail-fetch-field "from")
191 (mail-fetch-field "really-from")
192 (mail-fetch-field "sender")
193 "unknown"))
194 " " (current-time-string))))
196 ;; If the message specifies a coding system, use it.
197 (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
198 (if maybe-coding
199 (setq coding (intern maybe-coding))))
201 ;; Delete the Mail-From: header field if any.
202 (when (re-search-forward "^Mail-from:" nil t)
203 (beginning-of-line)
204 (delete-region (point)
205 (progn (forward-line 1) (point)))))
207 (goto-char (point-min))
208 ;; Insert the `From ' line.
209 (insert mail-from "\n")
210 ;; Record the keywords and attributes in our special way.
211 (insert "X-BABYL-V6-ATTRIBUTES: " (apply 'string attrs) "\n")
212 (when keywords
213 (insert "X-BABYL-V6-KEYWORDS: " keywords "\n"))
214 (goto-char (point-min))
215 ;; ``Quote'' "\nFrom " as "\n>From "
216 ;; (note that this isn't really quoting, as there is no requirement
217 ;; that "\n[>]+From " be quoted in the same transparent way.)
218 (let ((case-fold-search nil))
219 (while (search-forward "\nFrom " nil t)
220 (forward-char -5)
221 (insert ?>)))
222 ;; Write it to the output file.
223 (write-region (point-min) (point-max) to-file t
224 'nomsg))))
225 (kill-buffer temp-buffer))
226 (message "Writing messages to %s...done" to-file)))
228 (provide 'unrmail)
230 ;;; unrmail.el ends here
232 ;;; arch-tag: 14c6290d-60b2-456f-8909-5c2387de6acb