123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- (defun batch-unrmail ()
- "Convert old-style Rmail Babyl files to system inbox format.
- Specify the input Rmail Babyl file names as command line arguments.
- For each Rmail file, the corresponding output file name
- is made by adding `.mail' at the end.
- For example, invoke `emacs -batch -f batch-unrmail RMAIL'."
- (if (not noninteractive)
- (error "`batch-unrmail' is to be used only with -batch"))
- (let ((error nil))
- (while command-line-args-left
- (or (unrmail (car command-line-args-left)
- (concat (car command-line-args-left) ".mail"))
- (setq error t))
- (setq command-line-args-left (cdr command-line-args-left)))
- (message "Done")
- (kill-emacs (if error 1 0))))
- (declare-function mail-mbox-from "mail-utils" ())
- (defvar rmime-magic-string)
- (defun unrmail (file to-file)
- "Convert old-style Rmail Babyl file FILE to system inbox format file TO-FILE."
- (interactive "fUnrmail (babyl file): \nFUnrmail into (new mailbox file): ")
- (with-temp-buffer
-
- (let ((coding-system-for-read 'raw-text))
- (insert-file-contents file))
-
- (set-buffer-multibyte t)
- (setq buffer-file-coding-system 'raw-text-unix)
- (if (not (looking-at "BABYL OPTIONS"))
- (error "This file is not in Babyl format"))
-
- (let ((modifiedp (buffer-modified-p))
- (coding-system rmail-file-coding-system)
- from to)
- (goto-char (point-min))
- (search-forward "\n\^_" nil t)
- (setq from (point))
- (goto-char (point-max))
- (search-backward "\n\^_" from 'mv)
- (if (= from (setq to (point)))
- (error "The input file contains no messages"))
- (unless (and coding-system
- (coding-system-p coding-system))
- (setq coding-system
-
-
-
-
-
-
- (car (detect-coding-with-priority
- from to
- '((coding-category-emacs-mule . emacs-mule))))))
- (unless (memq coding-system
- '(undecided undecided-unix))
- (set-buffer-modified-p t)
- (let ((buffer-undo-list t))
- (decode-coding-region from to coding-system))
- (setq coding-system last-coding-system-used))
- (setq buffer-file-coding-system nil)
-
- (setq save-buffer-coding-system
- (or coding-system 'undecided)))
-
- (setq to-file (expand-file-name to-file default-directory))
- (condition-case ()
- (delete-file to-file)
- (file-error nil))
- (message "Writing messages to %s..." to-file)
- (goto-char (point-min))
- (let ((temp-buffer (get-buffer-create " unrmail"))
- (from-buffer (current-buffer)))
-
- (while (re-search-forward "^\^_\^l" nil t)
- (let ((beg (point))
- (end (save-excursion
- (if (re-search-forward "^\^_\\(\^l\\|\\'\\)" nil t)
- (match-beginning 0)
- (point-max))))
- (coding 'raw-text)
- label-line attrs keywords
- mail-from reformatted)
- (with-current-buffer temp-buffer
- (setq buffer-undo-list t)
- (erase-buffer)
- (setq buffer-file-coding-system coding)
- (insert-buffer-substring from-buffer beg end)
- (goto-char (point-min))
- (forward-line 1)
-
- (setq reformatted (= (following-char) ?1))
-
-
- (setq label-line
- (buffer-substring (point)
- (save-excursion (forward-line 1)
- (point))))
- (re-search-forward ",, ?")
- (unless (eolp)
- (setq keywords
- (buffer-substring (point)
- (progn (end-of-line)
- (1- (point)))))
-
-
-
- )
- (setq attrs
- (list
- (if (string-match ", answered," label-line) ?A ?-)
- (if (string-match ", deleted," label-line) ?D ?-)
- (if (string-match ", edited," label-line) ?E ?-)
- (if (string-match ", filed," label-line) ?F ?-)
- (if (string-match ", retried," label-line) ?R ?-)
- (if (string-match ", forwarded," label-line) ?S ?-)
- (if (string-match ", unseen," label-line) ?U ?-)
- (if (string-match ", resent," label-line) ?r ?-)))
-
-
- (goto-char (point-min))
- (if reformatted
- (progn
- (forward-line 2)
-
- (let ((case-fold-search t))
- (while (looking-at "Summary-Line:")
- (forward-line 1)))
- (delete-region (point-min) (point))
-
- (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
- (forward-line -1)
- (let ((start (point)))
- (search-forward "\n\n")
- (delete-region start (point))))
-
-
- (re-search-forward "^[*][*][*] EOOH [*][*][*]\n")
- (delete-region (point-min) (point)))
-
- (when (require 'rmime nil t)
- (let ((start (point)))
- (while (search-forward rmime-magic-string nil t))
- (delete-region start (point))))
-
- (goto-char (point-min))
- (save-restriction
- (narrow-to-region
- (point-min)
- (save-excursion (search-forward "\n\n" nil 'move) (point)))
-
- (setq mail-from (or (let ((from (mail-fetch-field "Mail-From")))
-
-
-
-
- (if from
- (format "%s\n" from)))
- (mail-mbox-from)))
-
- (let ((maybe-coding (mail-fetch-field "X-Coding-System")))
- (if maybe-coding
- (setq coding
-
- (coding-system-change-eol-conversion
- (intern maybe-coding) 0))
-
-
- (setq coding 'raw-text-unix)))
-
- (when (re-search-forward "^Mail-from:" nil t)
- (beginning-of-line)
- (delete-region (point)
- (progn (forward-line 1) (point)))))
- (goto-char (point-min))
-
- (insert mail-from)
-
- (insert "X-RMAIL-ATTRIBUTES: " (apply 'string attrs) "\n")
- (when keywords
- (insert "X-RMAIL-KEYWORDS: " keywords "\n"))
- (goto-char (point-min))
-
-
-
- (let ((case-fold-search nil))
- (while (search-forward "\nFrom " nil t)
- (forward-char -5)
- (insert ?>)))
- (goto-char (point-max))
-
- (insert "\n")
-
- (let ((coding-system-for-write coding))
- (write-region (point-min) (point-max) to-file t
- 'nomsg)))))
- (kill-buffer temp-buffer))
- (message "Writing messages to %s...done" to-file)))
- (provide 'unrmail)
|