unrmail.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. ;;; unrmail.el --- convert Rmail Babyl files to mailbox files
  2. ;; Copyright (C) 1992, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: mail
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;;;###autoload
  19. (defun batch-unrmail ()
  20. "Convert old-style Rmail Babyl files to system inbox format.
  21. Specify the input Rmail Babyl file names as command line arguments.
  22. For each Rmail file, the corresponding output file name
  23. is made by adding `.mail' at the end.
  24. For example, invoke `emacs -batch -f batch-unrmail RMAIL'."
  25. (if (not noninteractive)
  26. (error "`batch-unrmail' is to be used only with -batch"))
  27. (let ((error nil))
  28. (while command-line-args-left
  29. (or (unrmail (car command-line-args-left)
  30. (concat (car command-line-args-left) ".mail"))
  31. (setq error t))
  32. (setq command-line-args-left (cdr command-line-args-left)))
  33. (message "Done")
  34. (kill-emacs (if error 1 0))))
  35. (declare-function mail-mbox-from "mail-utils" ())
  36. (defvar rmime-magic-string) ; in rmime.el, if you have it
  37. ;;;###autoload
  38. (defun unrmail (file to-file)
  39. "Convert old-style Rmail Babyl file FILE to system inbox format file TO-FILE."
  40. (interactive "fUnrmail (babyl file): \nFUnrmail into (new mailbox file): ")
  41. (with-temp-buffer
  42. ;; Read in the old Rmail file with no decoding.
  43. (let ((coding-system-for-read 'raw-text))
  44. (insert-file-contents file))
  45. ;; But make it multibyte.
  46. (set-buffer-multibyte t)
  47. (setq buffer-file-coding-system 'raw-text-unix)
  48. (if (not (looking-at "BABYL OPTIONS"))
  49. (error "This file is not in Babyl format"))
  50. ;; Decode the file contents just as Rmail did.
  51. (let ((modifiedp (buffer-modified-p))
  52. (coding-system rmail-file-coding-system)
  53. from to)
  54. (goto-char (point-min))
  55. (search-forward "\n\^_" nil t) ; Skip BABYL header.
  56. (setq from (point))
  57. (goto-char (point-max))
  58. (search-backward "\n\^_" from 'mv)
  59. (if (= from (setq to (point)))
  60. (error "The input file contains no messages"))
  61. (unless (and coding-system
  62. (coding-system-p coding-system))
  63. (setq coding-system
  64. ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
  65. ;; earlier versions did that with the current buffer's encoding.
  66. ;; So we want to favor detection of emacs-mule (whose normal
  67. ;; priority is quite low), but still allow detection of other
  68. ;; encodings if emacs-mule won't fit. The call to
  69. ;; detect-coding-with-priority below achieves that.
  70. (car (detect-coding-with-priority
  71. from to
  72. '((coding-category-emacs-mule . emacs-mule))))))
  73. (unless (memq coding-system
  74. '(undecided undecided-unix))
  75. (set-buffer-modified-p t) ; avoid locking when decoding
  76. (let ((buffer-undo-list t))
  77. (decode-coding-region from to coding-system))
  78. (setq coding-system last-coding-system-used))
  79. (setq buffer-file-coding-system nil)
  80. ;; We currently don't use this value, but maybe we should.
  81. (setq save-buffer-coding-system
  82. (or coding-system 'undecided)))
  83. ;; Default the directory of TO-FILE based on where FILE is.
  84. (setq to-file (expand-file-name to-file default-directory))
  85. (condition-case ()
  86. (delete-file to-file)
  87. (file-error nil))
  88. (message "Writing messages to %s..." to-file)
  89. (goto-char (point-min))
  90. (let ((temp-buffer (get-buffer-create " unrmail"))
  91. (from-buffer (current-buffer)))
  92. ;; Process the messages one by one.
  93. (while (re-search-forward "^\^_\^l" nil t)
  94. (let ((beg (point))
  95. (end (save-excursion
  96. (if (re-search-forward "^\^_\\(\^l\\|\\'\\)" nil t)
  97. (match-beginning 0)
  98. (point-max))))
  99. (coding 'raw-text)
  100. label-line attrs keywords
  101. mail-from reformatted)
  102. (with-current-buffer temp-buffer
  103. (setq buffer-undo-list t)
  104. (erase-buffer)
  105. (setq buffer-file-coding-system coding)
  106. (insert-buffer-substring from-buffer beg end)
  107. (goto-char (point-min))
  108. (forward-line 1)
  109. ;; Record whether the header is reformatted.
  110. (setq reformatted (= (following-char) ?1))
  111. ;; Collect the label line, then get the attributes
  112. ;; and the keywords from it.
  113. (setq label-line
  114. (buffer-substring (point)
  115. (save-excursion (forward-line 1)
  116. (point))))
  117. (re-search-forward ",, ?")
  118. (unless (eolp)
  119. (setq keywords
  120. (buffer-substring (point)
  121. (progn (end-of-line)
  122. (1- (point)))))
  123. ;; Mbox rmail needs the spaces. Bug#2303.
  124. ;;; (setq keywords
  125. ;;; (replace-regexp-in-string ", " "," keywords))
  126. )
  127. (setq attrs
  128. (list
  129. (if (string-match ", answered," label-line) ?A ?-)
  130. (if (string-match ", deleted," label-line) ?D ?-)
  131. (if (string-match ", edited," label-line) ?E ?-)
  132. (if (string-match ", filed," label-line) ?F ?-)
  133. (if (string-match ", retried," label-line) ?R ?-)
  134. (if (string-match ", forwarded," label-line) ?S ?-)
  135. (if (string-match ", unseen," label-line) ?U ?-)
  136. (if (string-match ", resent," label-line) ?r ?-)))
  137. ;; Delete the special Babyl lines at the start,
  138. ;; and the ***EOOH*** line, and the reformatted header if any.
  139. (goto-char (point-min))
  140. (if reformatted
  141. (progn
  142. (forward-line 2)
  143. ;; Delete Summary-Line headers.
  144. (let ((case-fold-search t))
  145. (while (looking-at "Summary-Line:")
  146. (forward-line 1)))
  147. (delete-region (point-min) (point))
  148. ;; Delete the old reformatted header.
  149. (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
  150. (forward-line -1)
  151. (let ((start (point)))
  152. (search-forward "\n\n")
  153. (delete-region start (point))))
  154. ;; Not reformatted. Delete the special
  155. ;; lines before the real header.
  156. (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
  157. (delete-region (point-min) (point)))
  158. ;; Handle rmime formatting.
  159. (when (require 'rmime nil t)
  160. (let ((start (point)))
  161. (while (search-forward rmime-magic-string nil t))
  162. (delete-region start (point))))
  163. ;; Some operations on the message header itself.
  164. (goto-char (point-min))
  165. (save-restriction
  166. (narrow-to-region
  167. (point-min)
  168. (save-excursion (search-forward "\n\n" nil 'move) (point)))
  169. ;; Fetch or construct what we should use in the `From ' line.
  170. (setq mail-from (or (let ((from (mail-fetch-field "Mail-From")))
  171. ;; mail-mbox-from (below) returns a
  172. ;; string that ends in a newline, but
  173. ;; but mail-fetch-field does not, so
  174. ;; we append a newline here.
  175. (if from
  176. (format "%s\n" from)))
  177. (mail-mbox-from)))
  178. ;; If the message specifies a coding system, use it.
  179. (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
  180. (if maybe-coding
  181. (setq coding
  182. ;; Force Unix EOLs.
  183. (coding-system-change-eol-conversion
  184. (intern maybe-coding) 0))
  185. ;; If there's no X-Coding-System header, assume the
  186. ;; message was never decoded.
  187. (setq coding 'raw-text-unix)))
  188. ;; Delete the Mail-From: header field if any.
  189. (when (re-search-forward "^Mail-from:" nil t)
  190. (beginning-of-line)
  191. (delete-region (point)
  192. (progn (forward-line 1) (point)))))
  193. (goto-char (point-min))
  194. ;; Insert the `From ' line.
  195. (insert mail-from)
  196. ;; Record the keywords and attributes in our special way.
  197. (insert "X-RMAIL-ATTRIBUTES: " (apply 'string attrs) "\n")
  198. (when keywords
  199. (insert "X-RMAIL-KEYWORDS: " keywords "\n"))
  200. (goto-char (point-min))
  201. ;; ``Quote'' "\nFrom " as "\n>From "
  202. ;; (note that this isn't really quoting, as there is no requirement
  203. ;; that "\n[>]+From " be quoted in the same transparent way.)
  204. (let ((case-fold-search nil))
  205. (while (search-forward "\nFrom " nil t)
  206. (forward-char -5)
  207. (insert ?>)))
  208. (goto-char (point-max))
  209. ;; Add terminator blank line to message.
  210. (insert "\n")
  211. ;; Write it to the output file, suitably encoded.
  212. (let ((coding-system-for-write coding))
  213. (write-region (point-min) (point-max) to-file t
  214. 'nomsg)))))
  215. (kill-buffer temp-buffer))
  216. (message "Writing messages to %s...done" to-file)))
  217. (provide 'unrmail)
  218. ;;; unrmail.el ends here