Mercurial > emacs
annotate lisp/mail/unrmail.el @ 88281:3c661fd46ca7
*** empty log message ***
author | Henrik Enberg <henrik.enberg@telia.com> |
---|---|
date | Mon, 23 Jan 2006 03:45:49 +0000 |
parents | 6e97bfe9f7f0 |
children |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
18383
diff
changeset
|
1 ;;; unrmail.el --- convert Rmail files to mailbox files |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
982
diff
changeset
|
2 |
88155 | 3 ;;; Copyright (C) 1992, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
982 | 4 |
38695 | 5 ;; Maintainer: FSF |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2233
diff
changeset
|
6 ;; Keywords: mail |
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2233
diff
changeset
|
7 |
982 | 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 2, or (at your option) | |
13 ;; 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 | |
14169 | 21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
88155 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
982 | 24 |
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
18383
diff
changeset
|
25 ;;; Commentary: |
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
18383
diff
changeset
|
26 |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
982
diff
changeset
|
27 ;;; Code: |
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
982
diff
changeset
|
28 |
982 | 29 (defvar command-line-args-left) ;Avoid 'free variable' warning |
30 | |
31 ;;;###autoload | |
88155 | 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 | |
982 | 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")) | |
41 (let ((error nil)) | |
42 (while command-line-args-left | |
43 (or (unrmail (car command-line-args-left) | |
44 (concat (car command-line-args-left) ".mail")) | |
45 (setq error t)) | |
46 (setq command-line-args-left (cdr command-line-args-left))) | |
47 (message "Done") | |
48 (kill-emacs (if error 1 0)))) | |
49 | |
50 ;;;###autoload | |
88155 | 51 (defun unrmail (file to-file) |
52 "Convert Rmail file FILE to system inbox format file TO-FILE." | |
4266
5a7266ff9af1
(unrmail): Total rewrite.
Richard M. Stallman <rms@gnu.org>
parents:
2247
diff
changeset
|
53 (interactive "fUnrmail (rmail file): \nFUnrmail into (new mailbox file): ") |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
54 (with-temp-buffer |
88155 | 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) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
60 |
88155 | 61 (if (not (looking-at "BABYL OPTIONS")) |
62 (error "This file is not in Babyl format")) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
63 |
88155 | 64 ;; Decode the file contents just as Rmail did. |
65 (let ((modifiedp (buffer-modified-p)) | |
66 (coding-system rmail-file-coding-system) | |
67 from to) | |
68 (goto-char (point-min)) | |
69 (search-forward "\n\^_" nil t) ; Skip BABYL header. | |
70 (setq from (point)) | |
71 (goto-char (point-max)) | |
72 (search-backward "\n\^_" from 'mv) | |
73 (setq to (point)) | |
74 (unless (and coding-system | |
75 (coding-system-p coding-system)) | |
76 (setq 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 | |
84 from to | |
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)) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
92 |
88155 | 93 (setq buffer-file-coding-system nil) |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
94 |
88155 | 95 ;; We currently don't use this value, but maybe we should. |
96 (setq save-buffer-coding-system | |
97 (or coding-system 'undecided))) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
98 |
88155 | 99 ;; Default the directory of TO-FILE based on where FILE is. |
100 (setq to-file (expand-file-name to-file default-directory)) | |
101 (condition-case () | |
102 (delete-file to-file) | |
103 (file-error nil)) | |
104 (message "Writing messages to %s..." to-file) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
105 (goto-char (point-min)) |
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
106 |
88155 | 107 (let ((temp-buffer (get-buffer-create " unrmail")) |
108 (from-buffer (current-buffer))) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
109 |
88155 | 110 ;; Process the messages one by one. |
88161
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
111 (while (re-search-forward "^\^_\^l" nil t) |
88155 | 112 (let ((beg (point)) |
113 (end (save-excursion | |
88161
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
114 (if (re-search-forward "^\^_\\(\^l\\|\\'\\)" nil t) |
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
115 (match-beginning 0) |
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
116 (point-max)))) |
88155 | 117 (coding 'raw-text) |
118 label-line attrs keywords | |
119 mail-from reformatted) | |
120 (with-current-buffer temp-buffer | |
121 (setq buffer-undo-list t) | |
122 (erase-buffer) | |
123 (setq buffer-file-coding-system coding) | |
124 (insert-buffer-substring from-buffer beg end) | |
125 (goto-char (point-min)) | |
126 (forward-line 1) | |
127 ;; Record whether the header is reformatted. | |
128 (setq reformatted (= (following-char) ?1)) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
129 |
88155 | 130 ;; Collect the label line, then get the attributes |
131 ;; and the keywords from it. | |
132 (setq label-line | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
133 (buffer-substring (point) |
88155 | 134 (save-excursion (forward-line 1) |
135 (point)))) | |
136 (search-forward ",,") | |
137 (unless (eolp) | |
138 (setq keywords | |
139 (buffer-substring (point) | |
140 (progn (end-of-line) | |
141 (1- (point))))) | |
142 (setq keywords | |
143 (replace-regexp-in-string ", " "," keywords))) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
144 |
88155 | 145 (setq attrs |
146 (list | |
147 (if (string-match ", answered," label-line) ?A ?-) | |
148 (if (string-match ", deleted," label-line) ?D ?-) | |
149 (if (string-match ", edited," label-line) ?E ?-) | |
150 (if (string-match ", filed," label-line) ?F ?-) | |
151 (if (string-match ", resent," label-line) ?R ?-) | |
152 (if (string-match ", unseen," label-line) ?\ ?-) | |
153 (if (string-match ", stored," label-line) ?S ?-))) | |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
982
diff
changeset
|
154 |
88155 | 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))) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
176 |
88161
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
177 ;; Handle rmime formatting. |
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
178 (when (require 'rmime nil t) |
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
179 (let ((start (point))) |
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
180 (while (search-forward rmime-magic-string nil t)) |
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
181 (delete-region start (point)))) |
6e97bfe9f7f0
(unrmail): Use regular expression search to find
Alex Schroeder <alex@gnu.org>
parents:
88155
diff
changeset
|
182 |
88155 | 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))) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
189 |
88155 | 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 (or (mail-fetch-field "from") | |
195 (mail-fetch-field "really-from") | |
196 (mail-fetch-field "sender") | |
197 "unknown")) | |
198 " " (current-time-string)))) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
199 |
88155 | 200 ;; If the message specifies a coding system, use it. |
201 (let ((maybe-coding (mail-fetch-field "X-Coding-System"))) | |
202 (if maybe-coding | |
203 (setq coding (intern maybe-coding)))) | |
47631
433ae412d00f
(unrmail): Do the work directly,
Richard M. Stallman <rms@gnu.org>
parents:
38695
diff
changeset
|
204 |
88155 | 205 ;; Delete the Mail-From: header field if any. |
206 (when (re-search-forward "^Mail-from:" nil t) | |
207 (beginning-of-line) | |
208 (delete-region (point) | |
209 (progn (forward-line 1) (point))))) | |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
210 |
88155 | 211 (goto-char (point-min)) |
212 ;; Insert the `From ' line. | |
213 (insert mail-from "\n") | |
214 ;; Record the keywords and attributes in our special way. | |
215 (insert "X-BABYL-V6-ATTRIBUTES: " (apply 'string attrs) "\n") | |
216 (when keywords | |
217 (insert "X-BABYL-V6-KEYWORDS: " keywords "\n")) | |
218 (goto-char (point-min)) | |
219 ;; ``Quote'' "\nFrom " as "\n>From " | |
220 ;; (note that this isn't really quoting, as there is no requirement | |
221 ;; that "\n[>]+From " be quoted in the same transparent way.) | |
222 (let ((case-fold-search nil)) | |
223 (while (search-forward "\nFrom " nil t) | |
224 (forward-char -5) | |
225 (insert ?>))) | |
226 ;; Write it to the output file. | |
227 (write-region (point-min) (point-max) to-file t | |
228 'nomsg)))) | |
229 (kill-buffer temp-buffer)) | |
230 (message "Writing messages to %s...done" to-file))) | |
47631
433ae412d00f
(unrmail): Do the work directly,
Richard M. Stallman <rms@gnu.org>
parents:
38695
diff
changeset
|
231 |
18383 | 232 (provide 'unrmail) |
233 | |
2233
fb0ed5a1d0f3
Add standard library headers.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
982
diff
changeset
|
234 ;;; unrmail.el ends here |
47631
433ae412d00f
(unrmail): Do the work directly,
Richard M. Stallman <rms@gnu.org>
parents:
38695
diff
changeset
|
235 |
88151
ef41980f1588
Mostly rewritten. Parses the file directly and converts.
Richard M. Stallman <rms@gnu.org>
parents:
88131
diff
changeset
|
236 ;;; arch-tag: 14c6290d-60b2-456f-8909-5c2387de6acb |