Whitespace change.
[bpt/emacs.git] / lisp / mail / undigest.el
CommitLineData
d501f516
ER
1;;; undigest.el --- digest-cracking support for the RMAIL mail reader
2
9596811a 3;; Copyright (C) 1985, 1986, 1994, 1996 Free Software Foundation, Inc.
58142744 4
e5167999 5;; Maintainer: FSF
d7b4d18f 6;; Keywords: mail
e5167999 7
0d20f9a0
JB
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
0d20f9a0
JB
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
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
0d20f9a0 24
e5167999
ER
25;;; Commentary:
26
27;; See Internet RFC 934
28
29;;; Code:
0d20f9a0 30
4fe9b563
KH
31(require 'rmail)
32
372a91d7 33;;;###autoload
0d20f9a0
JB
34(defun undigestify-rmail-message ()
35 "Break up a digest message into its constituent messages.
36Leaves original message, deleted, before the undigestified messages."
37 (interactive)
38 (widen)
39 (let ((buffer-read-only nil)
40 (msg-string (buffer-substring (rmail-msgbeg rmail-current-message)
41 (rmail-msgend rmail-current-message))))
42 (goto-char (rmail-msgend rmail-current-message))
43 (narrow-to-region (point) (point))
44 (insert msg-string)
45 (narrow-to-region (point-min) (1- (point-max))))
46 (let ((error t)
47 (buffer-read-only nil))
48 (unwind-protect
49 (progn
50 (save-restriction
51 (goto-char (point-min))
52 (delete-region (point-min)
53 (progn (search-forward "\n*** EOOH ***\n")
54 (point)))
55 (insert "\^_\^L\n0, unseen,,\n*** EOOH ***\n")
56 (narrow-to-region (point)
57 (point-max))
58 (let* ((fill-prefix "")
59 (case-fold-search t)
5d2e6ef5 60 start
0d20f9a0
JB
61 (digest-name
62 (mail-strip-quoted-names
63 (or (save-restriction
64 (search-forward "\n\n")
5d2e6ef5 65 (setq start (point))
0d20f9a0
JB
66 (narrow-to-region (point-min) (point))
67 (goto-char (point-max))
68 (or (mail-fetch-field "Reply-To")
69 (mail-fetch-field "To")
70 (mail-fetch-field "Apparently-To")
71 (mail-fetch-field "From")))
67977ece 72 (error "Message is not a digest--bad header")))))
0d20f9a0
JB
73 (save-excursion
74 (goto-char (point-max))
75 (skip-chars-backward " \t\n")
5d2e6ef5 76 (let (found)
0d20f9a0 77 ;; compensate for broken un*x digestifiers. Sigh Sigh.
5d2e6ef5 78 (while (and (> (point) start) (not found))
0d20f9a0 79 (forward-line -1)
0d20f9a0
JB
80 (if (looking-at (concat "End of.*Digest.*\n"
81 (regexp-quote "*********") "*"
82 "\\(\n------*\\)*"))
83 (setq found t)))
67977ece
RS
84 (if (not found)
85 (error "Message is not a digest--no end line"))))
0d20f9a0
JB
86 (re-search-forward (concat "^" (make-string 55 ?-) "-*\n*"))
87 (replace-match "\^_\^L\n0, unseen,,\n*** EOOH ***\n")
88 (save-restriction
89 (narrow-to-region (point)
90 (progn (search-forward "\n\n")
91 (point)))
92 (if (mail-fetch-field "To") nil
93 (goto-char (point-min))
94 (insert "To: " digest-name "\n")))
95 (while (re-search-forward
96 (concat "\n\n" (make-string 27 ?-) "-*\n*")
97 nil t)
98 (replace-match "\n\n\^_\^L\n0, unseen,,\n*** EOOH ***\n")
99 (save-restriction
100 (if (looking-at "End ")
101 (insert "To: " digest-name "\n\n")
102 (narrow-to-region (point)
103 (progn (search-forward "\n\n"
104 nil 'move)
105 (point))))
369718f8
RS
106 (if (mail-fetch-field "To")
107 nil
0d20f9a0 108 (goto-char (point-min))
369718f8
RS
109 (insert "To: " digest-name "\n")))
110 ;; Digestifiers may insert `- ' on lines that start with `-'.
111 ;; Undo that.
112 (save-excursion
113 (goto-char (point-min))
114 (if (re-search-forward
115 "\n\n----------------------------*\n*"
116 nil t)
117 (let ((end (point-marker)))
118 (goto-char (point-min))
119 (while (re-search-forward "^- " end t)
120 (delete-char -2)))))
121 )))
0d20f9a0
JB
122 (setq error nil)
123 (message "Message successfully undigestified")
124 (let ((n rmail-current-message))
125 (rmail-forget-messages)
126 (rmail-show-message n)
4fe9b563
KH
127 (rmail-delete-forward)
128 (if (rmail-summary-exists)
129 (rmail-select-summary
130 (rmail-update-summary)))))
0d20f9a0
JB
131 (cond (error
132 (narrow-to-region (point-min) (1+ (point-max)))
133 (delete-region (point-min) (point-max))
134 (rmail-show-message rmail-current-message))))))
135
372a91d7 136;;;###autoload
9f606031
KH
137(defun unforward-rmail-message ()
138 "Extract a forwarded message from the containing message.
139This puts the forwarded message into a separate rmail message
140following the containing message."
141 (interactive)
d614dd91
RS
142 ;; Don't use save-excursion because we don't want to restore point
143 ;; in the case where we do not switch buffers.
144 (let ((obuf (current-buffer)))
145 (unwind-protect
146 (progn
147 ;; If we are in a summary buffer, switch to the Rmail buffer.
148 (if (local-variable-p 'rmail-buffer)
149 (set-buffer rmail-buffer))
150 (narrow-to-region (rmail-msgbeg rmail-current-message)
151 (rmail-msgend rmail-current-message))
152 (goto-char (point-min))
153 (let (beg end (buffer-read-only nil) msg-string who-forwarded-it)
154 (setq who-forwarded-it (mail-fetch-field "From"))
155 (if (re-search-forward "^----" nil t)
156 nil
157 (error "No forwarded message"))
158 (forward-line 1)
159 (setq beg (point))
160 (if (re-search-forward "^----" nil t)
161 (setq end (match-beginning 0))
162 (error "No terminator for forwarded message"))
163 (widen)
164 (setq msg-string (buffer-substring beg end))
165 (goto-char (rmail-msgend rmail-current-message))
166 (narrow-to-region (point) (point))
167 (insert "\^_\^L\n0, unseen,,\n*** EOOH ***\n")
168 (narrow-to-region (point) (point))
169 (insert "Forwarded-by: " who-forwarded-it "\n")
170 (insert msg-string)
171 (goto-char (point-min))
172 (while (not (eobp))
173 (if (looking-at "- ")
174 (delete-region (point) (+ 2 (point))))
175 (forward-line 1))
176 (let ((n rmail-current-message))
177 (rmail-forget-messages)
178 (rmail-show-message n)
179 (if (rmail-summary-exists)
180 (rmail-select-summary
181 (rmail-update-summary))))))
182 (set-buffer obuf))))
9f606031 183
d501f516 184;;; undigest.el ends here