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