1 ;;; rfc1843.el --- HZ (rfc1843) decoding
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: news HZ HZ+ mail i18n
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; (rfc1843-gnus-setup)
33 ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}")
37 (eval-when-compile (require 'cl
))
40 (defvar gnus-decode-encoded-word-function
)
41 (defvar gnus-decode-header-function
)
42 (defvar gnus-newsgroup-name
)
44 (defvar rfc1843-word-regexp
45 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
47 (defvar rfc1843-word-regexp-strictly
48 "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)")
50 (defvar rfc1843-hzp-word-regexp
51 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
52 \[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
54 (defvar rfc1843-hzp-word-regexp-strictly
55 "~\\({\\([\041-\167][\041-\176]\\)+\\|\
56 \[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
58 (defcustom rfc1843-decode-loosely nil
59 "Loosely check HZ encoding if non-nil.
60 When it is set non-nil, only buffers or strings with strictly
61 HZ-encoded are decoded."
65 (defcustom rfc1843-decode-hzp t
66 "HZ+ decoding support if non-nil.
67 HZ+ specification (also known as HZP) is to provide a standardized
68 7-bit representation of mixed Big5, GB, and ASCII text for convenient
69 e-mail transmission, news posting, etc.
70 The document of HZ+ 0.78 specification can be found at
71 ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc"
75 (defcustom rfc1843-newsgroups-regexp
"chinese\\|hz"
76 "Regexp of newsgroups in which might be HZ encoded."
80 (defun rfc1843-decode-region (from to
)
81 "Decode HZ in the region between FROM and TO."
86 (if (or rfc1843-decode-loosely
87 (re-search-forward (if rfc1843-decode-hzp
88 rfc1843-hzp-word-regexp-strictly
89 rfc1843-word-regexp-strictly
) to t
))
91 (narrow-to-region from to
)
92 (goto-char (point-min))
93 (while (re-search-forward (if rfc1843-decode-hzp
94 rfc1843-hzp-word-regexp
95 rfc1843-word-regexp
) (point-max) t
)
96 ;;; Text with extents may cause XEmacs crash
97 (setq str
(buffer-substring-no-properties
100 (setq firstc
(aref str
0))
101 (insert (mm-decode-coding-string
105 (delete-region (match-beginning 0) (match-end 0)))
107 (if (eq firstc ?
{) 'cn-gb-2312
'cn-big5
))))
108 (goto-char (point-min))
109 (while (search-forward "~" (point-max) t
)
110 (cond ((eq (char-after) ?
\n)
113 ((eq (char-after) ?~
)
114 (delete-char 1)))))))))
116 (defun rfc1843-decode-string (string)
117 "Decode HZ STRING and return the results."
118 (let ((m (mm-multibyte-p)))
121 (mm-enable-multibyte))
124 (rfc1843-decode-region (point-min) (point-max)))
127 (defun rfc1843-decode (word &optional firstc
)
128 "Decode HZ WORD and return it."
129 (let ((i -
1) (s (substring word
0)) v
)
130 (if (or (not firstc
) (eq firstc ?
{))
131 (while (< (incf i
) (length s
))
132 (if (eq (setq v
(aref s i
)) ?
) nil
133 (aset s i
(+ 128 v
))))
134 (while (< (incf i
) (length s
))
135 (if (eq (setq v
(aref s i
)) ?
) nil
136 (setq v
(+ (* 94 v
) (aref s
(1+ i
)) -
3135))
137 (aset s i
(+ (/ v
157) (if (eq firstc ?
<) 201 161)))
139 (aset s
(incf i
) (+ v
(if (< v
63) 64 98))))))
142 (defun rfc1843-decode-article-body ()
143 "Decode HZ encoded text in the article body."
144 (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp
"\\)\\>")
145 (or gnus-newsgroup-name
""))
148 (message-narrow-to-head)
149 (let* ((inhibit-point-motion-hooks t
)
151 (ct (message-fetch-field "Content-Type" t
))
152 (ctl (and ct
(mail-header-parse-content-type ct
))))
153 (if (and ctl
(not (string-match "/" (car ctl
))))
155 (goto-char (point-max))
158 (narrow-to-region (point) (point-max))
160 (equal (car ctl
) "text/plain"))
161 (rfc1843-decode-region (point) (point-max))))))))
163 (defvar rfc1843-old-gnus-decode-header-function nil
)
164 (defvar gnus-decode-header-methods
)
165 (defvar gnus-decode-encoded-word-methods
)
167 (defun rfc1843-gnus-setup ()
168 "Setup HZ decoding for Gnus."
171 (add-hook 'gnus-article-decode-hook
'rfc1843-decode-article-body t
)
172 (setq gnus-decode-encoded-word-function
173 'gnus-multi-decode-encoded-word-string
174 gnus-decode-header-function
175 'gnus-multi-decode-header
176 gnus-decode-encoded-word-methods
177 (nconc gnus-decode-encoded-word-methods
179 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp
"\\)\\>")
180 'rfc1843-decode-string
)))
181 gnus-decode-header-methods
182 (nconc gnus-decode-header-methods
184 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp
"\\)\\>")
185 'rfc1843-decode-region
)))))
189 ;;; arch-tag: 5149c301-a6ca-4731-9c9d-ba616e2cb687
190 ;;; rfc1843.el ends here