1 ;;; rfc1843.el --- HZ (rfc1843) decoding
2 ;; Copyright (c) 1998, 1999, 2000, 2003 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: news HZ HZ+ mail i18n
7 ;; This file is part of GNU Emacs.
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!#~}")
35 (eval-when-compile (require 'cl
))
38 (defvar rfc1843-word-regexp
39 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
41 (defvar rfc1843-word-regexp-strictly
42 "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)")
44 (defvar rfc1843-hzp-word-regexp
45 "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
46 \[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
48 (defvar rfc1843-hzp-word-regexp-strictly
49 "~\\({\\([\041-\167][\041-\176]\\)+\\|\
50 \[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
52 (defcustom rfc1843-decode-loosely nil
53 "Loosely check HZ encoding if non-nil.
54 When it is set non-nil, only buffers or strings with strictly
55 HZ-encoded are decoded."
59 (defcustom rfc1843-decode-hzp t
60 "HZ+ decoding support if non-nil.
61 HZ+ specification (also known as HZP) is to provide a standardized
62 7-bit representation of mixed Big5, GB, and ASCII text for convenient
63 e-mail transmission, news posting, etc.
64 The document of HZ+ 0.78 specification can be found at
65 ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc"
69 (defcustom rfc1843-newsgroups-regexp
"chinese\\|hz"
70 "Regexp of newsgroups in which might be HZ encoded."
74 (defun rfc1843-decode-region (from to
)
75 "Decode HZ in the region between FROM and TO."
80 (if (or rfc1843-decode-loosely
81 (re-search-forward (if rfc1843-decode-hzp
82 rfc1843-hzp-word-regexp-strictly
83 rfc1843-word-regexp-strictly
) to t
))
85 (narrow-to-region from to
)
86 (goto-char (point-min))
87 (while (re-search-forward (if rfc1843-decode-hzp
88 rfc1843-hzp-word-regexp
89 rfc1843-word-regexp
) (point-max) t
)
90 ;;; Text with extents may cause XEmacs crash
91 (setq str
(buffer-substring-no-properties
94 (setq firstc
(aref str
0))
95 (insert (mm-decode-coding-string
99 (delete-region (match-beginning 0) (match-end 0)))
101 (if (eq firstc ?
{) 'cn-gb-2312
'cn-big5
))))
102 (goto-char (point-min))
103 (while (search-forward "~" (point-max) t
)
104 (cond ((eq (char-after) ?
\n)
107 ((eq (char-after) ?~
)
108 (delete-char 1)))))))))
110 (defun rfc1843-decode-string (string)
111 "Decode HZ STRING and return the results."
112 (let ((m (mm-multibyte-p)))
115 (mm-enable-multibyte))
118 (rfc1843-decode-region (point-min) (point-max)))
121 (defun rfc1843-decode (word &optional firstc
)
122 "Decode HZ WORD and return it."
123 (let ((i -
1) (s (substring word
0)) v
)
124 (if (or (not firstc
) (eq firstc ?
{))
125 (while (< (incf i
) (length s
))
126 (if (eq (setq v
(aref s i
)) ?
) nil
127 (aset s i
(+ 128 v
))))
128 (while (< (incf i
) (length s
))
129 (if (eq (setq v
(aref s i
)) ?
) nil
130 (setq v
(+ (* 94 v
) (aref s
(1+ i
)) -
3135))
131 (aset s i
(+ (/ v
157) (if (eq firstc ?
<) 201 161)))
133 (aset s
(incf i
) (+ v
(if (< v
63) 64 98))))))
136 (defun rfc1843-decode-article-body ()
137 "Decode HZ encoded text in the article body."
138 (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp
"\\)\\>")
139 (or gnus-newsgroup-name
""))
142 (message-narrow-to-head)
143 (let* ((inhibit-point-motion-hooks t
)
145 (ct (message-fetch-field "Content-Type" t
))
146 (ctl (and ct
(ignore-errors
147 (mail-header-parse-content-type ct
)))))
148 (if (and ctl
(not (string-match "/" (car ctl
))))
150 (goto-char (point-max))
153 (narrow-to-region (point) (point-max))
155 (equal (car ctl
) "text/plain"))
156 (rfc1843-decode-region (point) (point-max))))))))
158 (defvar rfc1843-old-gnus-decode-header-function nil
)
159 (defvar gnus-decode-header-methods
)
160 (defvar gnus-decode-encoded-word-methods
)
162 (defun rfc1843-gnus-setup ()
163 "Setup HZ decoding for Gnus."
166 (add-hook 'gnus-article-decode-hook
'rfc1843-decode-article-body t
)
167 (setq gnus-decode-encoded-word-function
168 'gnus-multi-decode-encoded-word-string
169 gnus-decode-header-function
170 'gnus-multi-decode-header
171 gnus-decode-encoded-word-methods
172 (nconc gnus-decode-encoded-word-methods
174 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp
"\\)\\>")
175 'rfc1843-decode-string
)))
176 gnus-decode-header-methods
177 (nconc gnus-decode-header-methods
179 (cons (concat "\\<\\(" rfc1843-newsgroups-regexp
"\\)\\>")
180 'rfc1843-decode-region
)))))
184 ;;; arch-tag: 5149c301-a6ca-4731-9c9d-ba616e2cb687
185 ;;; rfc1843.el ends here