1 ;;; rfc1843.el --- HZ (rfc1843) decoding
2 ;; Copyright (c) 1998,1999 by Shenghuo Zhu <zsh@cs.rochester.edu>
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is a part of GNU Emacs, but the same permissions apply.
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.
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.
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.
28 ;; (rfc1843-gnus-setup)
31 ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}")
37 (defvar rfc1843-word-regexp
38 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\(~}\\|$\\)")
40 (defvar rfc1843-word-regexp-strictly
41 "~\\({\\([\041-\167][\041-\176]\\)+\\(~}\\|$\\)")
43 (defvar rfc1843-hzp-word-regexp
44 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
45 [<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
47 (defvar rfc1843-hzp-word-regexp-strictly
48 "~\\({\\([\041-\167][\041-\176]\\)+\\|\
49 [<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
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."
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"
68 (defcustom rfc1843-newsgroups-regexp "chinese\\|hz"
69 "Regexp of newsgroups in which might be HZ encoded."
73 (defun rfc1843-decode-region (from to)
74 "Decode HZ in the region between FROM and TO."
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))
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
95 (delete-region (match-beginning 0) (match-end 0)))
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)
103 ((eq (char-after) ?~)
104 (delete-char 1)))))))))
106 (defun rfc1843-decode-string (string)
107 "Decode HZ STRING and return the results."
108 (let ((m (mm-multibyte-p)))
111 (mm-enable-multibyte))
114 (rfc1843-decode-region (point-min) (point-max)))
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)))
129 (aset s (incf i) (+ v (if (< v 63) 64 98))))))
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 ""))
138 (message-narrow-to-head)
139 (let* ((inhibit-point-motion-hooks 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))))
146 (goto-char (point-max))
149 (narrow-to-region (point) (point-max))
151 (equal (car ctl) "text/plain"))
152 (rfc1843-decode-region (point) (point-max))))))))
154 (defvar rfc1843-old-gnus-decode-header-function nil)
155 (defvar gnus-decode-header-methods)
156 (defvar gnus-decode-encoded-word-methods)
157 (defvar gnus-decode-encoded-word-function)
159 (defun rfc1843-gnus-setup ()
160 "Setup HZ decoding for Gnus."
163 (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t)
164 (setq gnus-decode-encoded-word-function
165 'gnus-multi-decode-encoded-word-string
166 gnus-decode-header-function
167 'gnus-multi-decode-header
168 gnus-decode-encoded-word-methods
169 (nconc gnus-decode-encoded-word-methods
171 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
172 'rfc1843-decode-string)))
173 gnus-decode-header-methods
174 (nconc gnus-decode-header-methods
176 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
177 'rfc1843-decode-region)))))
181 ;;; rfc1843.el ends here