undigest.el 11 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; undigest.el --- digest-cracking support for the RMAIL mail reader

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1985, 1986, 1994, 1996, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2005, 2006, 2007 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
7
;; Keywords: mail
Eric S. Raymond's avatar
Eric S. Raymond committed
8

Jim Blandy's avatar
Jim Blandy committed
9 10 11 12
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; 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)
Jim Blandy's avatar
Jim Blandy committed
14 15 16 17 18 19 20 21
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
22
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
23 24
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Jim Blandy's avatar
Jim Blandy committed
25

Eric S. Raymond's avatar
Eric S. Raymond committed
26 27
;;; Commentary:

Francesco Potortì's avatar
Francesco Potortì committed
28
;; See Internet RFC 934 and RFC 1153
29
;; Also limited support for MIME digest encapsulation
Eric S. Raymond's avatar
Eric S. Raymond committed
30 31

;;; Code:
Jim Blandy's avatar
Jim Blandy committed
32

33 34
(require 'rmail)

35 36 37 38
(defconst rmail-mail-separator
  "\^_\^L\n0, unseen,,\n*** EOOH ***\n"
  "String for separating messages in an rmail file.")

39 40 41 42
(defcustom rmail-forward-separator-regex
  "^----.*\\([Ff]orwarded\\|[Oo]riginal\\).*[Mm]essage"
  "*Regexp to match the string that introduces forwarded messages.
This is not a header, but a string contained in the body of the message.
43
You may need to customize it for local needs."
44 45 46
  :type 'regexp
  :group 'rmail-headers)

47

Francesco Potortì's avatar
Francesco Potortì committed
48 49 50 51 52
(defconst rmail-digest-methods
  '(rmail-digest-parse-mime
    rmail-digest-parse-rfc1153strict
    rmail-digest-parse-rfc1153sloppy
    rmail-digest-parse-rfc934)
53
  "List of digest parsing functions, first tried first.
Francesco Potortì's avatar
Francesco Potortì committed
54

55 56
These functions operate on the current narrowing, and take no argument.
A function returns nil if it cannot parse the digest.  If it can, it
Francesco Potortì's avatar
Francesco Potortì committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
returns a list of cons pairs containing the start and end positions of
each undigestified message as markers.")

(defun rmail-digest-parse-mime ()
  (goto-char (point-min))
  (when (let ((head-end (progn (search-forward "\n\n" nil t) (point))))
	  (goto-char (point-min))
	  (and head-end
	       (re-search-forward
		(concat
		 "^Content-type: multipart/digest;"
		 "\\s-* boundary=\"?\\([^\";\n]+\\)[\";\n]") head-end t)
	       (search-forward (match-string 1) nil t)))
    ;; Ok, prolog separator found
    (let ((start (make-marker))
	  (end (make-marker))
	  (separator (concat "\n--" (match-string 0) "\n\n"))
	  result)
      (while (search-forward separator nil t)
	(move-marker start (match-beginning 0))
	(move-marker end (match-end 0))
	(add-to-list 'result (cons (copy-marker start) (copy-marker end t))))
      ;; Return the list of marker pairs
      (nreverse result))))

(defun rmail-digest-parse-rfc1153strict ()
  "Parse following strictly the method defined in RFC 1153.
See rmail-digest-methods."
 (rmail-digest-rfc1153
  "^-\\{70\\}\n\n"
  "^\n-\\{30\\}\n\n"
  "^\n-\\{30\\}\n\nEnd of .* Digest.*\n\\*\\{15,\\}\n+\'"))

(defun rmail-digest-parse-rfc1153sloppy ()
  "Parse using the method defined in RFC 1153, allowing for some sloppiness.
See rmail-digest-methods."
 (rmail-digest-rfc1153
  "^-\\{55,\\}\n\n"
  "^\n-\\{27,\\}\n\n"
96 97 98 99 100 101 102 103 104
  ;; GNU Mailman knowingly (see comment at line 353 of ToDigest.py in
  ;; Mailman source) produces non-conformant rfc 1153 digests, in that
  ;; the trailer contains a "digest footer" like this:
  ;; _______________________________________________
  ;; <one or more lines of list blurb>
  ;;
  ;; End of Foo Digest...
  ;; **************************************
  "^\nEnd of"))
Francesco Potortì's avatar
Francesco Potortì committed
105 106 107 108 109 110 111 112 113 114

(defun rmail-digest-rfc1153 (prolog-sep message-sep trailer-sep)
  (goto-char (point-min))
  (when (re-search-forward prolog-sep nil t)
    ;; Ok, prolog separator found
    (let ((start (make-marker))
	  (end (make-marker))
	  separator result)
      (move-marker start (match-beginning 0))
      (move-marker end (match-end 0))
115
      (setq result (list (cons (copy-marker start) (copy-marker end t))))
Francesco Potortì's avatar
Francesco Potortì committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      (when (re-search-forward message-sep nil t)
	;; Ok, at least one message separator found
	(setq separator (match-string 0))
	(when (re-search-forward trailer-sep nil t)
	  ;; Wonderful, we found a trailer, too.  Now, go on splitting
	  ;; the digest into separate rmail messages
	  (goto-char (cdar result))
	  (while (search-forward separator nil t)
	    (move-marker start (match-beginning 0))
	    (move-marker end (match-end 0))
	    (add-to-list 'result
			 (cons (copy-marker start) (copy-marker end t))))
	  ;; Undo masking of separators inside digestified messages
	  (goto-char (point-min))
	  (while (search-forward
		  (replace-regexp-in-string "\n-" "\n " separator) nil t)
	    (replace-match separator))
	  ;; Return the list of marker pairs
	  (nreverse result))))))

(defun rmail-digest-parse-rfc934 ()
  (goto-char (point-min))
  (when (re-search-forward "^\n?-[^ ].*\n\n?" nil t)
    ;; Message separator found
    (let ((start (make-marker))
	  (end (make-marker))
	  (separator (match-string 0))
	  result)
      (goto-char (point-min))
      (while (search-forward separator nil t)
	(move-marker start (match-beginning 0))
	(move-marker end (match-end 0))
	(add-to-list 'result (cons (copy-marker start) (copy-marker end t))))
      ;; Undo masking of separators inside digestified messages
      (goto-char (point-min))
      (while (search-forward "\n- -" nil t)
	(replace-match "\n-"))
      ;; Return the list of marker pairs
      (nreverse result))))

156 157
(declare-function rmail-update-summary "rmailsum" (&rest ignore))

158
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
159 160 161 162
(defun undigestify-rmail-message ()
  "Break up a digest message into its constituent messages.
Leaves original message, deleted, before the undigestified messages."
  (interactive)
163 164 165 166
  (with-current-buffer rmail-buffer
    (widen)
    (let ((error t)
	  (buffer-read-only nil))
Francesco Potortì's avatar
Francesco Potortì committed
167 168 169 170 171 172
      (goto-char (rmail-msgend rmail-current-message))
      (let ((msg-copy (buffer-substring (rmail-msgbeg rmail-current-message)
					(rmail-msgend rmail-current-message))))
	(narrow-to-region (point) (point))
	(insert msg-copy))
      (narrow-to-region (point-min) (1- (point-max)))
173 174 175 176 177
      (unwind-protect
	  (progn
	    (save-restriction
	      (goto-char (point-min))
	      (delete-region (point-min)
Francesco Potortì's avatar
Francesco Potortì committed
178
			     (progn (search-forward "\n*** EOOH ***\n" nil t)
179
				    (point)))
180
	      (insert "\n" rmail-mail-separator)
181 182
	      (narrow-to-region (point)
				(point-max))
Francesco Potortì's avatar
Francesco Potortì committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	      (let ((fill-prefix "")
		    (case-fold-search t)
		    digest-name type start end separator fun-list sep-list)
		(setq digest-name (mail-strip-quoted-names
				   (save-restriction
				     (search-forward "\n\n" nil 'move)
				     (setq start (point))
				     (narrow-to-region (point-min) start)
				     (or (mail-fetch-field "Reply-To")
					 (mail-fetch-field "To")
					 (mail-fetch-field "Apparently-To")
					 (mail-fetch-field "From")))))
		(unless digest-name
		  (error "Message is not a digest--bad header"))

		(setq fun-list rmail-digest-methods)
		(while (and fun-list
			    (null (setq sep-list (funcall (car fun-list)))))
		  (setq fun-list (cdr fun-list)))
		(unless sep-list
		  (error "Message is not a digest--no messages found"))

		;;; Split the digest into separate rmail messages
		(while sep-list
		  (let ((start (caar sep-list))
			(end (cdar sep-list)))
		    (delete-region start end)
		    (goto-char start)
211
		    (insert rmail-mail-separator)
Francesco Potortì's avatar
Francesco Potortì committed
212 213 214 215 216 217 218 219 220 221
		    (search-forward "\n\n" (caar (cdr sep-list)) 'move)
		    (save-restriction
		      (narrow-to-region end (point))
		      (unless (mail-fetch-field "To")
			(goto-char start)
			(insert "To: " digest-name "\n")))
		    (set-marker start nil)
		    (set-marker end nil))
		  (setq sep-list (cdr sep-list)))))

222 223 224 225 226 227 228 229 230 231 232 233 234
	    (setq error nil)
	    (message "Message successfully undigestified")
	    (let ((n rmail-current-message))
	      (rmail-forget-messages)
	      (rmail-show-message n)
	      (rmail-delete-forward)
	      (if (rmail-summary-exists)
		  (rmail-select-summary
		   (rmail-update-summary)))))
	(cond (error
	       (narrow-to-region (point-min) (1+ (point-max)))
	       (delete-region (point-min) (point-max))
	       (rmail-show-message rmail-current-message)))))))
235

236
;;;###autoload
237 238 239 240 241
(defun unforward-rmail-message ()
  "Extract a forwarded message from the containing message.
This puts the forwarded message into a separate rmail message
following the containing message."
  (interactive)
242
  ;; If we are in a summary buffer, switch to the Rmail buffer.
243 244 245
  (unwind-protect
      (with-current-buffer rmail-buffer
	(goto-char (point-min))
246 247
	(narrow-to-region (point)
			  (save-excursion (search-forward "\n\n") (point)))
248
	(let ((buffer-read-only nil)
249 250 251 252
	      (old-fwd-from (mail-fetch-field "Forwarded-From" nil nil t))
	      (old-fwd-date (mail-fetch-field "Forwarded-Date" nil nil t))
	      (fwd-from (mail-fetch-field "From"))
	      (fwd-date (mail-fetch-field "Date"))
253
	      beg end prefix forward-msg)
254 255 256
	  (narrow-to-region (rmail-msgbeg rmail-current-message)
			    (rmail-msgend rmail-current-message))
	  (goto-char (point-min))
257
	  (cond ((re-search-forward rmail-forward-separator-regex nil t)
258
		 (forward-line 1)
259
		 (skip-chars-forward "\n")
260 261 262 263 264 265
		 (setq beg (point))
		 (setq end (if (re-search-forward "^----.*[^- \t\n]" nil t)
			       (match-beginning 0) (point-max)))
		 (setq forward-msg
		       (replace-regexp-in-string
			"^- -" "-" (buffer-substring beg end))))
266
		((and (re-search-forward "^\\(> ?\\)[a-zA-Z-]+: .*\n" nil t)
267
		      (setq beg (match-beginning 0))
268
		      (setq prefix (match-string-no-properties 1))
269
		      (goto-char beg)
270
		      (looking-at (concat "\\(" prefix ".+\n\\)*"
271
					  prefix "Date: ."))
272 273 274
		      (looking-at (concat "\\(" prefix ".+\n\\)*"
					  prefix "From: .+\n"
					  "\\(" prefix ".+\n\\)*"
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
					  "\\(> ?\\)?\n" prefix)))
		 (re-search-forward "^[^>\n]" nil 'move)
		 (backward-char)
		 (skip-chars-backward " \t\n")
		 (forward-line 1)
		 (setq end (point))
		 (setq forward-msg
		       (replace-regexp-in-string
			(if (string= prefix ">") "^>" "> ?")
			"" (buffer-substring beg end))))
		(t
		 (error "No forwarded message found")))
	  (widen)
	  (goto-char (rmail-msgend rmail-current-message))
	  (narrow-to-region (point) (point))
	  (insert rmail-mail-separator)
	  (narrow-to-region (point) (point))
292 293 294 295 296 297 298
	  (while old-fwd-from
	    (insert "Forwarded-From: " (car old-fwd-from) "\n")
	    (insert "Forwarded-Date: " (car old-fwd-date) "\n")
	    (setq old-fwd-from (cdr old-fwd-from))
	    (setq old-fwd-date (cdr old-fwd-date)))
	  (insert "Forwarded-From: " fwd-from "\n")
	  (insert "Forwarded-Date: " fwd-date "\n")
299 300 301 302 303 304 305 306 307 308 309
	  (insert forward-msg)
	  (save-restriction
	    (goto-char (point-min))
	    (re-search-forward "\n$" nil 'move)
	    (narrow-to-region (point-min) (point))
	    (goto-char (point-min))
	    (while (not (eobp))
	      (unless (looking-at "^[a-zA-Z-]+: ")
		(insert "\t"))
	      (forward-line)))
	  (goto-char (point-min))))
310 311 312
    (let ((n rmail-current-message))
      (rmail-forget-messages)
      (rmail-show-message n))
313 314 315 316
    (if (rmail-summary-exists)
	(rmail-select-summary
	 (rmail-update-summary)))))

317

Richard M. Stallman's avatar
Richard M. Stallman committed
318 319
(provide 'undigest)

Miles Bader's avatar
Miles Bader committed
320
;;; arch-tag: 3a28b9fb-c1f5-43ef-9278-285f3e4b874d
Eric S. Raymond's avatar
Eric S. Raymond committed
321
;;; undigest.el ends here