1 ;;; rfc1843.el --- HZ (rfc1843) decoding
2 ;; Copyright (c) 1998 by Shenghuo Zhu <zsh@cs.rochester.edu>
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; $Revision: 1.1.2.5 $
7 ;; Time-stamp: <Tue Oct 6 23:48:49 EDT 1998 zsh>
9 ;; This file is not part of GNU Emacs, but the same permissions
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.
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.
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.
31 ;; (rfc1843-gnus-setup)
34 ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}")
40 (defvar rfc1843-word-regexp
41 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\(~}\\|$\\)")
43 (defvar rfc1843-word-regexp-strictly
44 "~\\({\\([\041-\167][\041-\176]\\)+\\(~}\\|$\\)")
46 (defvar rfc1843-hzp-word-regexp
47 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
48 [<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
50 (defvar rfc1843-hzp-word-regexp-strictly
51 "~\\({\\([\041-\167][\041-\176]\\)+\\|\
52 [<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
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."
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"
71 (defcustom rfc1843-newsgroups-regexp "chinese\\|hz"
72 "Regexp of newsgroups in which might be HZ encoded."
76 (defun rfc1843-decode-region (from to)
77 "Decode HZ in the region between FROM and TO."
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))
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
98 (delete-region (match-beginning 0) (match-end 0)))
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)
106 ((eq (char-after) ?~)
107 (delete-char 1)))))))))
109 (defun rfc1843-decode-string (string)
110 "Decode HZ STRING and return the results."
111 (let ((m (mm-multibyte-p)))
114 (mm-enable-multibyte))
117 (rfc1843-decode-region (point-min) (point-max)))
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)))
132 (aset s (incf i) (+ v (if (< v 63) 64 98))))))
135 (defun rfc1843-decode-article-body ()
136 "Decode HZ encoded text in the article body."
137 (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
141 (message-narrow-to-head)
142 (goto-char (point-max))
144 (rfc1843-decode-region (point) (point-max))))))
146 (defvar rfc1843-old-gnus-decode-header-function nil)
147 (defvar gnus-decode-header-methods)
148 (defvar gnus-decode-encoded-word-methods)
149 (defvar gnus-decode-encoded-word-function)
151 (defun rfc1843-gnus-setup ()
152 "Setup HZ decoding for Gnus."
155 (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t)
156 (setq gnus-decode-encoded-word-function
157 'gnus-multi-decode-encoded-word-string
158 gnus-decode-header-function
159 'gnus-multi-decode-header
160 gnus-decode-encoded-word-methods
161 (nconc gnus-decode-encoded-word-methods
163 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
164 'rfc1843-decode-string)))
165 gnus-decode-header-methods
166 (nconc gnus-decode-header-methods
168 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
169 'rfc1843-decode-region)))))
173 ;;; rfc1843.el ends here