* gnus-sum.el: Add gnus-article-outlook-deuglify-article.
[gnus] / 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 ;; Put this in your .gnus:
154 ;;
155 ;; (require 'gnus-outlook-deuglify)
156 ;;
157 ;; and you're enabled to press `W k' in the Summary Buffer.
158 ;;
159 ;; Non recommended usage :-)
160 ;; ---------------------
161 ;;
162 ;; To automatically invoke deuglification on every article you read,
163 ;; put something like that in your .gnus:
164 ;;
165 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-unwrap-lines)
166 ;;
167 ;; or _one_ of the following lines:
168 ;;
169 ;; ;; repair broken attribution lines
170 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-repair-attribution)
171 ;;
172 ;; ;; repair broken attribution lines and citations
173 ;; (add-hook 'gnus-article-decode-hook 'gnus-outlook-rearrange-citation)
174 ;;
175 ;; Note that there always may be some false positives, so I suggest
176 ;; using the manual invocation.  After deuglification you may want to
177 ;; refill the whole article using `W w'.
178 ;;
179 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
180 ;;
181 ;; Limitations
182 ;; -----------
183 ;;
184 ;; As I said before there may (or will) be a few false positives on
185 ;; unwrapping cited lines with `gnus-outlook-unwrap-lines'.
186 ;;
187 ;; `gnus-outlook-repair-attribution' will only fix the first
188 ;; attribution line found in the article.  Furthermore it fixed to
189 ;; certain kinds of attributions.  And there may be horribly many
190 ;; false positives, vanishing lines and so on -- so don't trust your
191 ;; eyes.  Again I recommend manual invocation.
192 ;;
193 ;; `gnus-outlook-rearrange-citation' carries all the limitations of
194 ;; `gnus-outlook-repair-attribution'.
195 ;;
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 ;;
198 ;; $Log: deuglify.el,v $
199 ;; Revision 6.1  2002/02/22 21:14:46  zsh
200 ;;      * deuglify.el: New file. The original file name is
201 ;;      gnus-outlook-deuglify.el from Raymond Scholz <rscholz@zonix.de>.
202 ;;
203 ;; Revision 1.5  2002/01/27 14:39:17  rscholz
204 ;; * New variable `gnus-outlook-deuglify-no-wrap-chars' to inhibit
205 ;;   unwrapping if one these chars is first in the possibly wrapped line.
206 ;; * Improved rearranging of the article.
207 ;; * New function `gnus-outlook-repair-attribution-block' for repairing
208 ;;   those big "Original Message (following some headers)" attributions.
209 ;;
210 ;; Revision 1.4  2002/01/03 14:05:00  rscholz
211 ;; Renamed `gnus-outlook-deuglify-article' to
212 ;; `gnus-article-outlook-deuglify-article'.
213 ;; Made it easier to deuglify the article while being in Gnus' Article
214 ;; Edit Mode. (suggested by Phil Nitschke)
215 ;;
216 ;;
217 ;; Revision 1.3  2002/01/02 23:35:54  rscholz
218 ;; Fix a bug that caused succeeding long attribution lines to be
219 ;; unwrapped.  Minor doc fixes and regular expression tuning.
220 ;;
221 ;; Revision 1.2  2001/12/30 20:14:34  rscholz
222 ;; Clean up source.
223 ;;
224 ;; Revision 1.1  2001/12/30 20:13:32  rscholz
225 ;; Initial revision
226 ;;
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228
229 ;;; Code:
230
231 (require 'gnus-art)
232 (require 'gnus-sum)
233
234 (defconst gnus-outlook-deuglify-version "1.5"
235   "Version of gnus-outlook-deuglify.")
236
237 ;;; User Customizable Variables:
238
239 (defgroup gnus-outlook-deuglify nil
240   "Deuglify articles generated by broken user agents like MS 
241 Outlook (Express).")
242
243 ;;;###autoload
244 (defcustom gnus-outlook-deuglify-unwrap-min 45
245   "Minimum length of the cited line above the (possibly) wrapped line."
246   :type 'number
247   :group 'gnus-outlook-deuglify)
248
249 ;;;###autoload
250 (defcustom gnus-outlook-deuglify-unwrap-max 95
251   "Maximum length of the cited line after unwrapping."
252   :type 'number
253   :group 'gnus-outlook-deuglify)
254
255 (defcustom gnus-outlook-deuglify-cite-marks ">|#%"
256   "Characters that indicate cited lines."
257   :type 'string
258   :group 'gnus-outlook-deuglify)
259
260 (defcustom gnus-outlook-deuglify-unwrap-stop-chars nil ;; ".?!" or nil
261   "Characters that inhibit unwrapping if they are the last one on the
262 cited line above the possible wrapped line."
263   :type 'string
264   :group 'gnus-outlook-deuglify)
265
266 (defcustom gnus-outlook-deuglify-no-wrap-chars "`"
267   "Characters that inhibit unwrapping if they are the first one in the
268 possibly wrapped line."
269   :type 'string
270   :group 'gnus-outlook-deuglify)
271
272 (defcustom  gnus-outlook-deuglify-attrib-cut-regexp
273   "\\(On \\|Am \\)?\\(Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\),[^,]+, "
274   "Regular expression matching the beginning of an attribution line
275 that should be cut off."
276   :type 'string
277   :group 'gnus-outlook-deuglify)
278
279 (defcustom gnus-outlook-deuglify-attrib-verb-regexp
280   "wrote\\|writes\\|says\\|schrieb\\|schreibt\\|meinte\\|skrev\\|a écrit\\|schreef"
281   "Regular expression matching the verb used in an attribution line."
282   :type 'string
283   :group 'gnus-outlook-deuglify)
284
285 (defcustom  gnus-outlook-deuglify-attrib-end-regexp
286   ": *\\|\\.\\.\\."
287   "Regular expression matching the end of an attribution line."
288   :type 'string
289   :group 'gnus-outlook-deuglify)
290
291
292 ;; Functions
293
294 ;; TODO: don't kill MIME parts
295 ;;;###autoload
296 (defun gnus-outlook-unwrap-lines ()
297   "Unwrap lines that appear to be wrapped citation lines.  You can
298 control what lines will be unwrapped by frobbing
299 `gnus-outlook-deuglify-unwrap-min' and
300 `gnus-outlook-deuglify-unwrap-max', indicating the miminum and maximum
301 length of an unwrapped citation line."
302   (interactive)
303   (save-excursion
304     (let ((case-fold-search nil)
305           (inhibit-read-only t)
306           (cite-marks gnus-outlook-deuglify-cite-marks)
307           (no-wrap gnus-outlook-deuglify-no-wrap-chars)
308           (stop-chars gnus-outlook-deuglify-unwrap-stop-chars))
309       (gnus-with-article-buffer
310         (article-goto-body)
311         (while (re-search-forward
312                 (concat
313                  "^\\([ \t" cite-marks "]*\\)"
314                  "\\([" cite-marks "].*[^\n " stop-chars "]\\)[ \t]?\n"
315                  "\\1\\([^\n " cite-marks no-wrap "]+.*\\)$")
316               nil t)
317           (let ((len12 (- (match-end 2) (match-beginning 1)))
318               (len3 (- (match-end 3) (match-beginning 3))))
319             (if (and (> len12 gnus-outlook-deuglify-unwrap-min)
320                      (< (+ len12 len3) gnus-outlook-deuglify-unwrap-max))
321                 (progn 
322                   (replace-match "\\1\\2 \\3")
323                   (goto-char (match-beginning 0))))))))))
324
325 ;; TODO: respect signatures, don't kill MIME parts
326 (defun gnus-outlook-rearrange-article (from-where)
327   "Put the text from `from-where' to the end of buffer at the top of
328 the article buffer."
329   (save-excursion
330     (let ((inhibit-read-only t)
331           (cite-marks gnus-outlook-deuglify-cite-marks))
332       (gnus-with-article-buffer
333         (unless (search-forward-regexp
334                    (concat "^[ \t]*[^" cite-marks "\n]") nil t)
335           (kill-region from-where (point-max))
336           (article-goto-body)
337           (yank)
338           (insert "\n"))))))
339
340 ;; John Doe <john.doe@some.domain> wrote in message
341 ;; news:a87usw8$dklsssa$2@some.news.server...
342
343 (defun gnus-outlook-repair-attribution-outlook ()
344   "Repair a broken attribution line (Outlook)."
345   (save-excursion
346     (let ((case-fold-search nil)
347           (inhibit-read-only t)
348           (cite-marks gnus-outlook-deuglify-cite-marks))
349       (gnus-with-article-buffer
350         (article-goto-body)
351         (if (re-search-forward
352              (concat "^\\([^" cite-marks "].+\\)"
353                      "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\)"
354                      "\\(.*\n?[^\n" cite-marks "].*\\)?"
355                      "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
356              nil t)
357             (progn
358               (replace-match "\\1\\2\\4")
359               (match-beginning 0)))))))
360
361
362 ;; ----- Original Message -----
363 ;; From: "John Doe" <john.doe@some.domain>
364 ;; To: "Doe Foundation" <info@doefnd.org>
365 ;; Sent: Monday, November 19, 2001 12:13 PM
366 ;; Subject: More Doenuts
367
368 (defun gnus-outlook-repair-attribution-block ()
369   "Repair a big broken attribution block."
370   (save-excursion
371     (let ((case-fold-search nil)
372           (inhibit-read-only t)
373           (cite-marks gnus-outlook-deuglify-cite-marks))
374       (gnus-with-article-buffer
375         (article-goto-body)
376         (if (re-search-forward
377              (concat "^----* ?[^-]+ ?----*\n"
378                      "[^\n]+: \\([^\n]+\\)\n"
379                      "[^\n]+: [^\n]+\n"
380                      "[^\n]+: [^\n]+\n"
381                      "[^\n]+: [^\n]+$")
382              nil t)
383             (progn
384               (replace-match "\\1 wrote:")
385               (match-beginning 0)))))))
386
387 ;; On Wed, 16 Jan 2002 23:23:30 +0100, John Doe <john.doe@some.domain> wrote:
388
389 (defun gnus-outlook-repair-attribution-other ()
390   "Repair a broken attribution line (other user agents than Outlook)."
391   (save-excursion
392     (let ((case-fold-search nil)
393           (inhibit-read-only t)
394           (cite-marks gnus-outlook-deuglify-cite-marks))
395       (gnus-with-article-buffer
396         (article-goto-body)
397         (if (re-search-forward
398              (concat "^\\("gnus-outlook-deuglify-attrib-cut-regexp"\\)?"
399                      "\\([^" cite-marks "].+\\)\n\\([^\n" cite-marks "].*\\)?"
400                      "\\(" gnus-outlook-deuglify-attrib-verb-regexp "\\).*"
401                      "\\(" gnus-outlook-deuglify-attrib-end-regexp "\\)$")
402              nil t)
403             (progn
404               (replace-match "\\4 \\5\\6\\7")
405               (match-beginning 0)))))))
406
407 ;;;###autoload
408 (defun gnus-outlook-repair-attribution ()
409   "Repair a broken attribution line."
410   (interactive)
411   (or
412    (gnus-outlook-repair-attribution-other)
413    (gnus-outlook-repair-attribution-block)
414    (gnus-outlook-repair-attribution-outlook)))
415
416 (defun gnus-outlook-rearrange-citation ()
417   "Repair broken citations."
418   (let ((attrib-start (gnus-outlook-repair-attribution)))
419     ;; rearrange citations if an attribution line has been recognized
420     (if attrib-start
421         (gnus-outlook-rearrange-article attrib-start))))
422
423 ;;;###autoload
424 (defun gnus-outlook-deuglify-article ()
425   "Deuglify broken Outlook (Express) articles."
426   (interactive)
427   ;; apply treatment of dumb quotes
428   (gnus-article-treat-dumbquotes)
429   ;; repair wrapped cited lines
430   (gnus-outlook-unwrap-lines)
431   ;; repair attribution line
432   (gnus-outlook-rearrange-citation))
433
434 ;;;###autoload
435 (defun gnus-article-outlook-deuglify-article ()
436   "Deuglify broken Outlook (Express) articles and redisplay."
437   (interactive)
438   (gnus-outlook-deuglify-article)
439   (with-current-buffer (or gnus-article-buffer (current-buffer))
440     (gnus-article-prepare-display)))
441
442 (provide 'deuglify)
443
444 ;; Local Variables:
445 ;; coding: iso-8859-1
446 ;; End:
447
448 ;;; deuglify.el ends here