Apply Dave Love's patches.
[gnus] / lisp / rfc1843.el
1 ;;; rfc1843.el --- HZ (rfc1843) decoding
2 ;; Copyright (c) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: news HZ HZ+
6
7 ;; This file is a part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; Usage:
27 ;; (require 'rfc1843)
28 ;; (rfc1843-gnus-setup)
29 ;;
30 ;; Test:
31 ;; (rfc1843-decode-string  "~{<:Ky2;S{#,NpJ)l6HK!#~}")
32
33 ;;; Code:
34
35 (require 'mm-util)
36
37 (defvar rfc1843-word-regexp
38   "~\\({\\([\041-\167][\041-\176]\\| \\)+\\(~}\\|$\\)")
39
40 (defvar rfc1843-word-regexp-strictly
41   "~\\({\\([\041-\167][\041-\176]\\)+\\(~}\\|$\\)")
42
43 (defvar rfc1843-hzp-word-regexp
44   "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
45 [<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
46
47 (defvar rfc1843-hzp-word-regexp-strictly
48   "~\\({\\([\041-\167][\041-\176]\\)+\\|\
49 [<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
50
51 (defcustom rfc1843-decode-loosely nil
52   "Loosely check HZ encoding if non-nil.
53 When it is set non-nil, only buffers or strings with strictly
54 HZ-encoded are decoded."
55   :type 'boolean
56   :group 'gnus)
57
58 (defcustom rfc1843-decode-hzp t
59   "HZ+ decoding support if non-nil.
60 HZ+ specification (also known as HZP) is to provide a standardized
61 7-bit representation of mixed Big5, GB, and ASCII text for convenient
62 e-mail transmission, news posting, etc.
63 The document of HZ+ 0.78 specification can be found at
64 ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc"
65   :type 'boolean
66   :group 'gnus)
67
68 (defcustom rfc1843-newsgroups-regexp "chinese\\|hz"
69   "Regexp of newsgroups in which might be HZ encoded."
70   :type 'string
71   :group 'gnus)
72
73 (defun rfc1843-decode-region (from to)
74   "Decode HZ in the region between FROM and TO."
75   (interactive "r")
76   (let (str firstc)
77     (save-excursion
78       (goto-char from)
79       (if (or rfc1843-decode-loosely
80               (re-search-forward (if rfc1843-decode-hzp
81                                      rfc1843-hzp-word-regexp-strictly
82                                    rfc1843-word-regexp-strictly) to t))
83           (save-restriction
84             (narrow-to-region from to)
85             (goto-char (point-min))
86             (while (re-search-forward (if rfc1843-decode-hzp
87                                           rfc1843-hzp-word-regexp
88                                         rfc1843-word-regexp) (point-max) t)
89               (setq str (match-string 1))
90               (setq firstc (aref str 0))
91               (insert (mm-decode-coding-string
92                        (rfc1843-decode
93                         (prog1
94                             (substring str 1)
95                           (delete-region (match-beginning 0) (match-end 0)))
96                         firstc)
97                        (if (eq firstc ?{) 'cn-gb-2312 'cn-big5))))
98             (goto-char (point-min))
99             (while (search-forward "~" (point-max) t)
100               (cond ((eq (char-after) ?\n)
101                      (delete-char -1)
102                      (delete-char 1))
103                     ((eq (char-after) ?~)
104                      (delete-char 1)))))))))
105
106 (defun rfc1843-decode-string (string)
107   "Decode HZ STRING and return the results."
108   (let ((m (mm-multibyte-p)))
109     (with-temp-buffer
110       (when m
111         (mm-enable-multibyte))
112       (insert string)
113       (inline
114         (rfc1843-decode-region (point-min) (point-max)))
115       (buffer-string))))
116
117 (defun rfc1843-decode (word &optional firstc)
118   "Decode HZ WORD and return it."
119   (let ((i -1) (s (substring word 0)) v)
120     (if (or (not firstc) (eq firstc ?{))
121         (while (< (incf i) (length s))
122           (if (eq (setq v (aref s i)) ? ) nil
123             (aset s i (+ 128 v))))
124       (while (< (incf i) (length s))
125         (if (eq (setq v (aref s i)) ? ) nil
126           (setq v (+ (* 94 v) (aref s (1+ i)) -3135))
127           (aset s i (+ (/ v 157) (if (eq firstc ?<) 201 161)))
128           (setq v (% v 157))
129           (aset s (incf i) (+ v (if (< v 63) 64 98))))))
130     s))
131
132 (defun rfc1843-decode-article-body ()
133   "Decode HZ encoded text in the article body."
134   (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
135                     (or gnus-newsgroup-name ""))
136       (save-excursion
137         (save-restriction
138           (message-narrow-to-head)
139           (let* ((inhibit-point-motion-hooks t)
140                  (case-fold-search t)
141                  (ct (message-fetch-field "Content-Type" t))
142                  (ctl (and ct (ignore-errors
143                                 (mail-header-parse-content-type ct)))))
144             (if (and ctl (not (string-match "/" (car ctl)))) 
145                 (setq ctl nil))
146             (goto-char (point-max))
147             (widen)
148             (forward-line 1)
149             (narrow-to-region (point) (point-max))
150             (when (or (not ctl)
151                       (equal (car ctl) "text/plain"))
152               (rfc1843-decode-region (point) (point-max))))))))
153
154 (defvar rfc1843-old-gnus-decode-header-function  nil)
155 (defvar gnus-decode-header-methods)
156 (defvar gnus-decode-encoded-word-methods)
157
158 (defun rfc1843-gnus-setup ()
159   "Setup HZ decoding for Gnus."
160   (require 'gnus-art)
161   (require 'gnus-sum)
162   (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t)
163   (setq gnus-decode-encoded-word-function
164         'gnus-multi-decode-encoded-word-string
165         gnus-decode-header-function
166         'gnus-multi-decode-header
167         gnus-decode-encoded-word-methods
168         (nconc gnus-decode-encoded-word-methods
169                (list
170                 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
171                       'rfc1843-decode-string)))
172         gnus-decode-header-methods
173         (nconc gnus-decode-header-methods
174                (list
175                 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
176                       'rfc1843-decode-region)))))
177
178 (provide 'rfc1843)
179
180 ;;; rfc1843.el ends here