1 ;;; rfc1843.el --- HZ (rfc1843) decoding
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: news HZ HZ+ mail i18n
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; (rfc1843-gnus-setup)
30 ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}")
34 (eval-when-compile (require 'cl
))
37 (defvar gnus-decode-encoded-word-function
)
38 (defvar gnus-decode-header-function
)
39 (defvar gnus-newsgroup-name
)
41 (defvar rfc1843-word-regexp
42 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
44 (defvar rfc1843-word-regexp-strictly
45 "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)")
47 (defvar rfc1843-hzp-word-regexp
48 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
49 [<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
51 (defvar rfc1843-hzp-word-regexp-strictly
52 "~\\({\\([\041-\167][\041-\176]\\)+\\|\
53 [<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
55 (defcustom rfc1843-decode-loosely nil
56 "Loosely check HZ encoding if non-nil.
57 When it is set non-nil, only buffers or strings with strictly
58 HZ-encoded are decoded."
62 (defcustom rfc1843-decode-hzp t
63 "HZ+ decoding support if non-nil.
64 HZ+ specification (also known as HZP) is to provide a standardized
65 7-bit representation of mixed Big5, GB, and ASCII text for convenient
66 e-mail transmission, news posting, etc.
67 The document of HZ+ 0.78 specification can be found at
68 ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc"
72 (defcustom rfc1843-newsgroups-regexp
"chinese\\|hz"
73 "Regexp of newsgroups in which might be HZ encoded."
77 (defun rfc1843-decode-region (from to
)
78 "Decode HZ in the region between FROM and TO."
83 (if (or rfc1843-decode-loosely
84 (re-search-forward (if rfc1843-decode-hzp
85 rfc1843-hzp-word-regexp-strictly
86 rfc1843-word-regexp-strictly
) to t
))
88 (narrow-to-region from to
)
89 (goto-char (point-min))
90 (while (re-search-forward (if rfc1843-decode-hzp
91 rfc1843-hzp-word-regexp
92 rfc1843-word-regexp
) (point-max) t
)
93 ;;; Text with extents may cause XEmacs crash
94 (setq str
(buffer-substring-no-properties
97 (setq firstc
(aref str
0))
98 (insert (mm-decode-coding-string
102 (delete-region (match-beginning 0) (match-end 0)))
104 (if (eq firstc ?
{) 'cn-gb-2312
'cn-big5
))))
105 (goto-char (point-min))
106 (while (search-forward "~" (point-max) t
)
107 (cond ((eq (char-after) ?
\n)
110 ((eq (char-after) ?~
)
111 (delete-char 1)))))))))
113 (defun rfc1843-decode-string (string)
114 "Decode HZ STRING and return the results."
115 (let ((m (mm-multibyte-p)))
118 (mm-enable-multibyte))
121 (rfc1843-decode-region (point-min) (point-max)))
124 (defun rfc1843-decode (word &optional firstc
)
125 "Decode HZ WORD and return it."
126 (let ((i -
1) (s (substring word
0)) v
)
127 (if (or (not firstc
) (eq firstc ?
{))
128 (while (< (incf i
) (length s
))
129 (if (eq (setq v
(aref s i
)) ?
) nil
130 (aset s i
(+ 128 v
))))
131 (while (< (incf i
) (length s
))
132 (if (eq (setq v
(aref s i
)) ?
) nil
133 (setq v
(+ (* 94 v
) (aref s
(1+ i
)) -
3135))
134 (aset s i
(+ (/ v
157) (if (eq firstc ?
<) 201 161)))
136 (aset s
(incf i
) (+ v
(if (< v
63) 64 98))))))
139 (autoload 'mail-header-parse-content-type
"mail-parse")
140 (autoload 'message-narrow-to-head
"message")
141 (declare-function message-fetch-field
"message" (header &optional not-all
))
143 (defun rfc1843-decode-article-body ()
144 "Decode HZ encoded text in the article body."
145 (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp
"\\)\\>")
146 (or gnus-newsgroup-name
""))
149 (message-narrow-to-head)
150 (let* ((inhibit-point-motion-hooks t
)
152 (ct (message-fetch-field "Content-Type" t
))
153 (ctl (and ct
(mail-header-parse-content-type ct
))))
154 (if (and ctl
(not (string-match "/" (car ctl
))))
156 (goto-char (point-max))
159 (narrow-to-region (point) (point-max))
161 (equal (car ctl
) "text/plain"))
162 (rfc1843-decode-region (point) (point-max))))))))
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 ;;; rfc1843.el ends here