Commit | Line | Data |
---|---|---|
c113de23 | 1 | ;;; rfc1843.el --- HZ (rfc1843) decoding |
e84b4b86 TTN |
2 | |
3 | ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, | |
4 | ;; 2005 Free Software Foundation, Inc. | |
c113de23 GM |
5 | |
6 | ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
7 | ;; Keywords: news HZ HZ+ mail i18n | |
8 | ||
715a2ca2 | 9 | ;; This file is part of GNU Emacs. |
c113de23 GM |
10 | |
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. | |
15 | ||
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. | |
20 | ||
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 | |
3a35cf56 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
c113de23 GM |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; Usage: | |
29 | ;; (require 'rfc1843) | |
30 | ;; (rfc1843-gnus-setup) | |
31 | ;; | |
32 | ;; Test: | |
33 | ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}") | |
34 | ||
35 | ;;; Code: | |
36 | ||
1391c567 | 37 | (eval-when-compile (require 'cl)) |
c113de23 GM |
38 | (require 'mm-util) |
39 | ||
40 | (defvar rfc1843-word-regexp | |
41 | "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") | |
42 | ||
43 | (defvar rfc1843-word-regexp-strictly | |
44 | "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)") | |
45 | ||
46 | (defvar rfc1843-hzp-word-regexp | |
47 | "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\ | |
23f87bed | 48 | \[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") |
c113de23 GM |
49 | |
50 | (defvar rfc1843-hzp-word-regexp-strictly | |
51 | "~\\({\\([\041-\167][\041-\176]\\)+\\|\ | |
23f87bed | 52 | \[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)") |
c113de23 GM |
53 | |
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." | |
58 | :type 'boolean | |
23f87bed | 59 | :group 'mime) |
c113de23 GM |
60 | |
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" | |
68 | :type 'boolean | |
23f87bed | 69 | :group 'mime) |
c113de23 GM |
70 | |
71 | (defcustom rfc1843-newsgroups-regexp "chinese\\|hz" | |
72 | "Regexp of newsgroups in which might be HZ encoded." | |
73 | :type 'string | |
23f87bed | 74 | :group 'mime) |
c113de23 GM |
75 | |
76 | (defun rfc1843-decode-region (from to) | |
77 | "Decode HZ in the region between FROM and TO." | |
78 | (interactive "r") | |
79 | (let (str firstc) | |
80 | (save-excursion | |
81 | (goto-char from) | |
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)) | |
86 | (save-restriction | |
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 | ;;; Text with extents may cause XEmacs crash | |
a1506d29 | 93 | (setq str (buffer-substring-no-properties |
c113de23 GM |
94 | (match-beginning 1) |
95 | (match-end 1))) | |
96 | (setq firstc (aref str 0)) | |
97 | (insert (mm-decode-coding-string | |
98 | (rfc1843-decode | |
99 | (prog1 | |
100 | (substring str 1) | |
101 | (delete-region (match-beginning 0) (match-end 0))) | |
102 | firstc) | |
103 | (if (eq firstc ?{) 'cn-gb-2312 'cn-big5)))) | |
104 | (goto-char (point-min)) | |
105 | (while (search-forward "~" (point-max) t) | |
106 | (cond ((eq (char-after) ?\n) | |
107 | (delete-char -1) | |
108 | (delete-char 1)) | |
109 | ((eq (char-after) ?~) | |
110 | (delete-char 1))))))))) | |
111 | ||
112 | (defun rfc1843-decode-string (string) | |
113 | "Decode HZ STRING and return the results." | |
114 | (let ((m (mm-multibyte-p))) | |
115 | (with-temp-buffer | |
116 | (when m | |
117 | (mm-enable-multibyte)) | |
118 | (insert string) | |
119 | (inline | |
120 | (rfc1843-decode-region (point-min) (point-max))) | |
121 | (buffer-string)))) | |
122 | ||
123 | (defun rfc1843-decode (word &optional firstc) | |
124 | "Decode HZ WORD and return it." | |
125 | (let ((i -1) (s (substring word 0)) v) | |
126 | (if (or (not firstc) (eq firstc ?{)) | |
127 | (while (< (incf i) (length s)) | |
128 | (if (eq (setq v (aref s i)) ? ) nil | |
129 | (aset s i (+ 128 v)))) | |
130 | (while (< (incf i) (length s)) | |
131 | (if (eq (setq v (aref s i)) ? ) nil | |
132 | (setq v (+ (* 94 v) (aref s (1+ i)) -3135)) | |
133 | (aset s i (+ (/ v 157) (if (eq firstc ?<) 201 161))) | |
134 | (setq v (% v 157)) | |
135 | (aset s (incf i) (+ v (if (< v 63) 64 98)))))) | |
136 | s)) | |
137 | ||
138 | (defun rfc1843-decode-article-body () | |
139 | "Decode HZ encoded text in the article body." | |
140 | (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
141 | (or gnus-newsgroup-name "")) | |
142 | (save-excursion | |
143 | (save-restriction | |
144 | (message-narrow-to-head) | |
145 | (let* ((inhibit-point-motion-hooks t) | |
146 | (case-fold-search t) | |
147 | (ct (message-fetch-field "Content-Type" t)) | |
148 | (ctl (and ct (ignore-errors | |
149 | (mail-header-parse-content-type ct))))) | |
a1506d29 | 150 | (if (and ctl (not (string-match "/" (car ctl)))) |
c113de23 GM |
151 | (setq ctl nil)) |
152 | (goto-char (point-max)) | |
153 | (widen) | |
154 | (forward-line 1) | |
155 | (narrow-to-region (point) (point-max)) | |
156 | (when (or (not ctl) | |
157 | (equal (car ctl) "text/plain")) | |
158 | (rfc1843-decode-region (point) (point-max)))))))) | |
159 | ||
160 | (defvar rfc1843-old-gnus-decode-header-function nil) | |
161 | (defvar gnus-decode-header-methods) | |
162 | (defvar gnus-decode-encoded-word-methods) | |
163 | ||
164 | (defun rfc1843-gnus-setup () | |
165 | "Setup HZ decoding for Gnus." | |
166 | (require 'gnus-art) | |
167 | (require 'gnus-sum) | |
168 | (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t) | |
169 | (setq gnus-decode-encoded-word-function | |
170 | 'gnus-multi-decode-encoded-word-string | |
171 | gnus-decode-header-function | |
172 | 'gnus-multi-decode-header | |
173 | gnus-decode-encoded-word-methods | |
174 | (nconc gnus-decode-encoded-word-methods | |
175 | (list | |
176 | (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
177 | 'rfc1843-decode-string))) | |
178 | gnus-decode-header-methods | |
179 | (nconc gnus-decode-header-methods | |
180 | (list | |
181 | (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
182 | 'rfc1843-decode-region))))) | |
183 | ||
184 | (provide 'rfc1843) | |
185 | ||
ab5796a9 | 186 | ;;; arch-tag: 5149c301-a6ca-4731-9c9d-ba616e2cb687 |
c113de23 | 187 | ;;; rfc1843.el ends here |