Import Oort Gnus v0.09.
[elisp/gnus.git-] / lisp / deuglify.el
1 ;;; deuglify.el --- deuglify broken Outlook (Express) articles
2
3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
4 ;; Copyright (C) 2001,2002 Raymond Scholz
5
6 ;; Author: Raymond Scholz <rscholz@zonix.de>
7 ;;         Thomas Steffen (unwrapping algorithm,
8 ;;                         based on an idea of Stefan Monnier)
9 ;; Keywords: mail, news
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; This file enables Gnus to repair broken citations produced by
31 ;; common user agents like MS Outlook (Express).  It may repair
32 ;; articles of other user agents too.
33 ;;
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35
36 ;;
37 ;; Outlook sometimes wraps cited lines before sending a message as
38 ;; seen in this example:
39 ;;
40 ;; Example #1
41 ;; ----------
42 ;;
43 ;; John Doe wrote:
44 ;;
45 ;; > This sentence no verb.  This sentence no verb.  This sentence
46 ;; no
47 ;; > verb.  This sentence no verb.  This sentence no verb.  This
48 ;; > sentence no verb.
49 ;;
50 ;; The function `gnus-outlook-unwrap-lines' tries to recognize those
51 ;; erroneously wrapped lines and will unwrap them.  I.e. putting the
52 ;; wrapped parts ("no" in this example) back where they belong (at the
53 ;; end of the cited line above).
54 ;;
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;;
57 ;; Note that some people not only use broken user agents but also
58 ;; practice a bad citation style by omitting blank lines between the
59 ;; cited text and their own text.
60 ;:
61 ;; Example #2
62 ;; ----------
63 ;;
64 ;; John Doe wrote:
65 ;;
66 ;; > This sentence no verb.  This sentence no verb.  This sentence no
67 ;; You forgot in all your sentences.
68 ;; > verb.  This sentence no verb.  This sentence no verb.  This
69 ;; > sentence no verb.
70 ;;
71 ;; Unwrapping "You forgot in all your sentences." would be illegal as
72 ;; this part wasn't intended to be cited text.
73 ;; `gnus-outlook-unwrap-lines' will only unwrap lines if the resulting
74 ;; citation line will be of a certain maximum length.  You can control
75 ;; this by adjusting `gnus-outlook-deuglify-unwrap-max'.  Also
76 ;; unwrapping will only be done if the line above the (possibly)
77 ;; wrapped line has a minimum length of `gnus-outlook-deuglify-unwrap-min'.
78 ;;
79 ;; Furthermore no unwrapping will be undertaken if the last character
80 ;; is one of the chars specified in
81 ;; `gnus-outlook-deuglify-unwrap-stop-chars'.  Setting this to ".?!"
82 ;; inhibits unwrapping if the cited line ends with a full stop,
83 ;; question mark or exclamation mark.  Note that this variable
84 ;; defaults to `nil', triggering a few false positives but generally
85 ;; giving you better results.
86 ;;
87 ;; Unwrapping works on every level of citation.  Thus you will be able
88 ;; repair broken citations of broken user agents citing broken
89 ;; citations of broken user agents citing broken citations...
90 ;;
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 ;;
93 ;; Citations are commonly introduced with an attribution line
94 ;; indicating who wrote the cited text.  Outlook adds superfluous
95 ;; information that can be found in the header of the message to this
96 ;; line and often wraps it.
97 ;;
98 ;; If that weren't enough, lots of people write their own text above
99 ;; the cited text and cite the complete original article below.
100 ;;
101 ;; Example #3
102 ;; ----------
103 ;;
104 ;; Hey, John.  There's no in all your sentences!
105 ;;
106 ;; John Doe <john.doe@some.domain> wrote in message
107 ;; news:a87usw8$dklsssa$2@some.news.server...
108 ;; > This sentence no verb.  This sentence no verb.  This sentence
109 ;; no
110 ;; > verb.  This sentence no verb.  This sentence no verb.  This
111 ;; > sentence no verb.
112 ;; >
113 ;; > Bye, John
114 ;;
115 ;; Repairing the attribution line will be done by function
116 ;; `gnus-outlook-repair-attribution' which calls other function that
117 ;; try to recognize and repair broken attribution lines.  See variable
118 ;; `gnus-outlook-deuglify-attrib-cut-regexp' for stuff that should be
119 ;; cut off from the beginning of an attribution line and variable
120 ;; `gnus-outlook-deuglify-attrib-verb-regexp' for the verbs that are
121 ;; required to be found in an attribution line.  These function return
122 ;; the point where the repaired attribution line starts.
123 ;;
124 ;; Rearranging the article so that the cited text appears above the
125 ;; new text will be done by function
126 ;; `gnus-outlook-rearrange-citation'.  This function calls
127 ;; `gnus-outlook-repair-attribution' to find and repair an attribution
128 ;; line.
129 ;;
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;;
132 ;; Well, and that's what the message will look like after applying
133 ;; deuglification:
134 ;;
135 ;; Example #3 (deuglified)
136 ;; -----------------------
137 ;;
138 ;; John Doe <john.doe@some.domain> wrote:
139 ;;
140 ;; > This sentence no verb.  This sentence no verb.  This sentence no
141 ;; > verb.  This sentence no verb.  This sentence no verb.  This
142 ;; > sentence no verb.
143 ;; >
144 ;; > Bye, John
145 ;;
146 ;; Hey, John.  There's no in all your sentences!
147 ;;
148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
149 ;; 
150 ;; Usage
151 ;; -----
152 ;;
153 ;; Press `W k' in the Summary Buffer.
154 ;;
155 ;; Non recommended usage :-)
156 ;; ---------------------
157 ;;
158 ;; To automatically invoke deuglification on every article you read,
159 ;; put something like that in your .gnus:
160 ;;
161 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-unwrap-lines)
162 ;;
163 ;; or _one_ of the following lines:
164 ;;
165 ;; ;; repair broken attribution lines
166 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-repair-attribution)
167 ;;
168 ;; ;; repair broken attribution lines and citations
169 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-rearrange-citation)
170 ;;
171 ;; Note that there always may be some false positives, so I suggest
172 ;; using the manual invocation.  After deuglification you may want to
173 ;; refill the whole article using `W w'.
174 ;;
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;;
177 ;; Limitations
178 ;; -----------
179 ;;
180 ;; As I said before there may (or will) be a few false positives on
181 ;; unwrapping cited lines with `gnus-outlook-unwrap-lines'.
182 ;;
183 ;; `gnus-outlook-repair-attribution' will only fix the first
184 ;; attribution line found in the article.  Furthermore it fixed to
185 ;; certain kinds of attributions.  And there may be horribly many
186 ;; false positives, vanishing lines and so on -- so don't trust your
187 ;; eyes.  Again I recommend manual invocation.
188 ;;
189 ;; `gnus-outlook-rearrange-citation' carries all the limitations of
190 ;; `gnus-outlook-repair-attribution'.
191 ;;
192 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 ;;
194 ;; See ChangeLog for other changes.
195 ;;
196 ;; Revision 1.5  2002/01/27 14:39:17  rscholz
197 ;; * New variable `gnus-outlook-deuglify-no-wrap-chars' to inhibit
198 ;;   unwrapping if one these chars is first in the possibly wrapped line.
199 ;; * Improved rearranging of the article.
200 ;; * New function `gnus-outlook-repair-attribution-block' for repairing
201 ;;   those big "Original Message (following some headers)" attributions.
202 ;;
203 ;; Revision 1.4  2002/01/03 14:05:00  rscholz
204 ;; Renamed `gnus-outlook-deuglify-article' to
205 ;; `gnus-article-outlook-deuglify-article'.
206 ;; Made it easier to deuglify the article while being in Gnus' Article
207 ;; Edit Mode. (suggested by Phil Nitschke)
208 ;;
209 ;;
210 ;; Revision 1.3  2002/01/02 23:35:54  rscholz
211 ;; Fix a bug that caused succeeding long attribution lines to be
212 ;; unwrapped.  Minor doc fixes and regular expression tuning.
213 ;;
214 ;; Revision 1.2  2001/12/30 20:14:34  rscholz
215 ;; Clean up source.
216 ;;
217 ;; Revision 1.1  2001/12/30 20:13:32  rscholz
218 ;; Initial revision
219 ;;
220 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221
222 ;;; Code:
223
224 (require 'gnus-art)
225 (require 'gnus-sum)
226
227 (defconst gnus-outlook-deuglify-version "1.5 Gnus version"
228   "Version of gnus-outlook-deuglify.")
229
230 ;;; User Customizable Variables:
231
232 (defgroup gnus-outlook-deuglify nil
233   "Deuglify articles generated by broken user agents like MS Outlook (Express).")
234
235 ;;;###autoload
236 (defcustom gnus-outlook-deuglify-unwrap-min 45
237   "Minimum length of the cited line above the (possibly) wrapped line."
238   :type 'number
239   :group 'gnus-outlook-deuglify)
240
241 ;;;###autoload
242 (defcustom gnus-outlook-deuglify-unwrap-max 95
243   "Maximum length of the cited line after unwrapping."
244   :type 'number
245   :group 'gnus-outlook-deuglify)
246
247 (defcustom gnus-outlook-deuglify-cite-marks ">|#%"
248   "Characters that indicate cited lines."
249   :type 'string
250   :group 'gnus-outlook-deuglify)
251
252 (defcustom gnus-outlook-deuglify-unwrap-stop-chars nil ;; ".?!" or nil
253   "Characters that inhibit unwrapping if they are the last one on the cited line above the possible wrapped line."
254   :type 'string
255   :group 'gnus-outlook-deuglify)
256
257 (defcustom gnus-outlook-deuglify-no-wrap-chars "`"
258   "Characters that inhibit unwrapping if they are the first one in the possibly wrapped line."
259   :type 'string
260   :group 'gnus-outlook-deuglify)
261
262 (defcustom  gnus-outlook-deuglify-attrib-cut-regexp
263   "\\(On \\|Am \\)?\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),[^,]+, "
264   "Regular expression matching the beginning of an attribution line that should be cut off."
265   :type 'string
266   :group 'gnus-outlook-deuglify)
267
268 (defcustom gnus-outlook-deuglify-attrib-verb-regexp
269   "wrote\\|writes\\|says\\|schrieb\\|schreibt\\|meinte\\|skrev\\|a écrit\\|schreef"
270   "Regular expression matching the verb used in an attribution line."
271   :type 'string
272   :group 'gnus-outlook-deuglify)
273
274 (defcustom  gnus-outlook-deuglify-attrib-end-regexp
275   ": *\\|\\.\\.\\."
276   "Regular expression matching the end of an attribution line."
277   :type 'string
278   :group 'gnus-outlook-deuglify)
279
280
281 ;; Functions
282
283 ;;;###autoload
284 (defun gnus-outlook-unwrap-lines ()
285   "Unwrap lines that appear to be wrapped citation lines.
286 You can control what lines will be unwrapped by frobbing
287 `gnus-outlook-deuglify-unwrap-min' and
288 `gnus-outlook-deuglify-unwrap-max', indicating the miminum and maximum
289 length of an unwrapped citation line."
290   (interactive)
291   (save-excursion
292     (let ((case-fold-search nil)
293           (inhibit-read-only t)
294           (cite-marks gnus-outlook-deuglify-cite-marks)
295           (no-wrap gnus-outlook-deuglify-no-wrap-chars)
296           (stop-chars gnus-outlook-deuglify-unwrap-stop-chars))
297       (gnus-with-article-buffer
298         (article-goto-body)
299         (while (re-search-forward
300                 (concat
301                  "^\\([ \t" cite-marks "]*\\)"
302                  "\\([" cite-marks "].*[^\n " stop-chars "]\\)[ \t]?\n"
303                  "\\1\\([^\n " cite-marks no-wrap "]+.*\\)$")
304               nil t)
305           (let ((len12 (- (match-end 2) (match-beginning 1)))
306               (len3 (- (match-end 3) (match-beginning 3))))
307             (if (and (> len12 gnus-outlook-deuglify-unwrap-min)
308                      (< (+ len12 len3) gnus-outlook-deuglify-unwrap-max))
309                 (progn 
310                   (replace-match "\\1\\2 \\3")
311                   (goto-char (match-beginning 0))))))))))
312
313 (defun gnus-outlook-rearrange-article (attr-start)
314   "Put the text from `attr-start' to the end of buffer at the top of the article buffer."
315   (save-excursion
316     (let ((inhibit-read-only t)
317           (cite-marks gnus-outlook-deuglify-cite-marks))
318       (gnus-with-article-buffer
319         (article-goto-body)
320         ;; article does not start with attribution
321         (unless (= (point) attr-start)
322           (gnus-kill-all-overlays)
323           (let ((cur (point))
324                 ;; before signature or end of buffer
325                 (to (if (gnus-article-search-signature)
326                         (point)
327                       (point-max))))
328             ;; handle the case where the full quote is below the
329             ;; signature
330             (if (< to attr-start)
331                 (setq to (point-max)))
332             (transpose-regions cur attr-start attr-start to)))))))
333
334 ;; John Doe <john.doe@some.domain> wrote in message
335 ;; news:a87usw8$dklsssa$2@some.news.server...
336
337 (defun gnus-outlook-repair-attribution-outlook ()
338   "Repair a broken attribution line (Outlook)."
339   (save-excursion
340     (let ((case-fold-search nil)
341           (inhibit-read-only t)
342           (cite-marks gnus-outlook-deuglify-cite-marks))
343       (gnus-with-article-buffer
344         (article-goto-body)
345         (if (re-search-forward
346              (concat "^\\([^" cite-marks "].+\\)"
347                      "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\)"
348                      "\\(.*\n?[^\n" cite-marks "].*\\)?"
349                      "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
350              nil t)
351             (progn
352               (gnus-kill-all-overlays)
353               (replace-match "\\1\\2\\4")
354               (match-beginning 0)))))))
355
356
357 ;; ----- Original Message -----
358 ;; From: "John Doe" <john.doe@some.domain>
359 ;; To: "Doe Foundation" <info@doefnd.org>
360 ;; Sent: Monday, November 19, 2001 12:13 PM
361 ;; Subject: More Doenuts
362
363 (defun gnus-outlook-repair-attribution-block ()
364   "Repair a big broken attribution block."
365   (save-excursion
366     (let ((case-fold-search nil)
367           (inhibit-read-only t)
368           (cite-marks gnus-outlook-deuglify-cite-marks))
369       (gnus-with-article-buffer
370         (article-goto-body)
371         (if (re-search-forward
372              (concat "^[" cite-marks " \t]*----* ?[^-]+ [^-]+ ?----*\n"
373                      "[^\n:]+:[ \t]*\\([^\n]+\\)\n"
374                      "\\([^\n:]+:[ \t]*[^\n]+\n\\)+")
375              nil t)
376             (progn
377               (gnus-kill-all-overlays)
378               (replace-match "\\1 wrote:\n")
379               (match-beginning 0)))))))
380
381 ;; On Wed, 16 Jan 2002 23:23:30 +0100, John Doe <john.doe@some.domain> wrote:
382
383 (defun gnus-outlook-repair-attribution-other ()
384   "Repair a broken attribution line (other user agents than Outlook)."
385   (save-excursion
386     (let ((case-fold-search nil)
387           (inhibit-read-only t)
388           (cite-marks gnus-outlook-deuglify-cite-marks))
389       (gnus-with-article-buffer
390         (article-goto-body)
391         (if (re-search-forward
392              (concat "^\\("gnus-outlook-deuglify-attrib-cut-regexp"\\)?"
393                      "\\([^" cite-marks "].+\\)\n\\([^\n" cite-marks "].*\\)?"
394                      "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\).*"
395                      "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
396              nil t)
397             (progn
398               (gnus-kill-all-overlays)
399               (replace-match "\\4 \\5\\6\\7")
400               (match-beginning 0)))))))
401
402 ;;;###autoload
403 (defun gnus-outlook-repair-attribution ()
404   "Repair a broken attribution line."
405   (interactive)
406   (or
407    (gnus-outlook-repair-attribution-other)
408    (gnus-outlook-repair-attribution-block)
409    (gnus-outlook-repair-attribution-outlook)))
410
411 (defun gnus-outlook-rearrange-citation ()
412   "Repair broken citations."
413   (let ((attrib-start (gnus-outlook-repair-attribution)))
414     ;; rearrange citations if an attribution line has been recognized
415     (if attrib-start
416         (gnus-outlook-rearrange-article attrib-start))))
417
418 ;;;###autoload
419 (defun gnus-outlook-deuglify-article ()
420   "Deuglify broken Outlook (Express) articles."
421   (interactive)
422   ;; apply treatment of dumb quotes
423   (gnus-article-treat-dumbquotes)
424   ;; repair wrapped cited lines
425   (gnus-outlook-unwrap-lines)
426   ;; repair attribution line
427   (gnus-outlook-rearrange-citation))
428
429 ;;;###autoload
430 (defun gnus-article-outlook-deuglify-article ()
431   "Deuglify broken Outlook (Express) articles and redisplay."
432   (interactive)
433   (gnus-outlook-deuglify-article)
434   (with-current-buffer (or gnus-article-buffer (current-buffer))
435     ;; "Emulate" `gnus-article-prepare-display' without calling
436     ;; it. Calling `gnus-article-prepare-display' on an already
437     ;; prepared article removes all MIME parts.  I'm unsure whether
438     ;; this is a bug or not.
439     (gnus-article-highlight t)
440     (gnus-treat-article nil)
441     (gnus-run-hooks 'gnus-article-prepare-hook)))
442
443 (provide 'deuglify)
444
445 ;; Local Variables:
446 ;; coding: iso-8859-1
447 ;; End:
448
449 ;;; deuglify.el ends here