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