Commit | Line | Data |
---|---|---|
c113de23 | 1 | ;;; rfc1843.el --- HZ (rfc1843) decoding |
e84b4b86 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1998-2014 Free Software Foundation, Inc. |
c113de23 GM |
4 | |
5 | ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu> | |
6 | ;; Keywords: news HZ HZ+ mail i18n | |
7 | ||
715a2ca2 | 8 | ;; This file is part of GNU Emacs. |
c113de23 | 9 | |
5e809f55 GM |
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. | |
c113de23 | 14 | |
5e809f55 GM |
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. | |
c113de23 GM |
19 | |
20 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c113de23 GM |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; Usage: | |
26 | ;; (require 'rfc1843) | |
27 | ;; (rfc1843-gnus-setup) | |
28 | ;; | |
29 | ;; Test: | |
30 | ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}") | |
31 | ||
32 | ;;; Code: | |
33 | ||
1391c567 | 34 | (eval-when-compile (require 'cl)) |
c113de23 GM |
35 | (require 'mm-util) |
36 | ||
2df35c02 JB |
37 | (defvar gnus-decode-encoded-word-function) |
38 | (defvar gnus-decode-header-function) | |
39 | (defvar gnus-newsgroup-name) | |
40 | ||
c113de23 GM |
41 | (defvar rfc1843-word-regexp |
42 | "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") | |
43 | ||
44 | (defvar rfc1843-word-regexp-strictly | |
45 | "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)") | |
46 | ||
47 | (defvar rfc1843-hzp-word-regexp | |
48 | "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\ | |
23f87bed | 49 | \[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)") |
c113de23 GM |
50 | |
51 | (defvar rfc1843-hzp-word-regexp-strictly | |
52 | "~\\({\\([\041-\167][\041-\176]\\)+\\|\ | |
23f87bed | 53 | \[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)") |
c113de23 GM |
54 | |
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." | |
59 | :type 'boolean | |
23f87bed | 60 | :group 'mime) |
c113de23 GM |
61 | |
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" | |
69 | :type 'boolean | |
23f87bed | 70 | :group 'mime) |
c113de23 GM |
71 | |
72 | (defcustom rfc1843-newsgroups-regexp "chinese\\|hz" | |
73 | "Regexp of newsgroups in which might be HZ encoded." | |
74 | :type 'string | |
23f87bed | 75 | :group 'mime) |
c113de23 GM |
76 | |
77 | (defun rfc1843-decode-region (from to) | |
78 | "Decode HZ in the region between FROM and TO." | |
79 | (interactive "r") | |
80 | (let (str firstc) | |
81 | (save-excursion | |
82 | (goto-char from) | |
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)) | |
87 | (save-restriction | |
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 | |
a1506d29 | 94 | (setq str (buffer-substring-no-properties |
c113de23 GM |
95 | (match-beginning 1) |
96 | (match-end 1))) | |
97 | (setq firstc (aref str 0)) | |
98 | (insert (mm-decode-coding-string | |
99 | (rfc1843-decode | |
100 | (prog1 | |
101 | (substring str 1) | |
102 | (delete-region (match-beginning 0) (match-end 0))) | |
103 | firstc) | |
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) | |
108 | (delete-char -1) | |
109 | (delete-char 1)) | |
110 | ((eq (char-after) ?~) | |
111 | (delete-char 1))))))))) | |
112 | ||
113 | (defun rfc1843-decode-string (string) | |
114 | "Decode HZ STRING and return the results." | |
115 | (let ((m (mm-multibyte-p))) | |
116 | (with-temp-buffer | |
117 | (when m | |
118 | (mm-enable-multibyte)) | |
119 | (insert string) | |
120 | (inline | |
121 | (rfc1843-decode-region (point-min) (point-max))) | |
122 | (buffer-string)))) | |
123 | ||
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))) | |
135 | (setq v (% v 157)) | |
136 | (aset s (incf i) (+ v (if (< v 63) 64 98)))))) | |
137 | s)) | |
138 | ||
444f6b28 GM |
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)) | |
142 | ||
c113de23 GM |
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 "")) | |
147 | (save-excursion | |
148 | (save-restriction | |
149 | (message-narrow-to-head) | |
150 | (let* ((inhibit-point-motion-hooks t) | |
151 | (case-fold-search t) | |
152 | (ct (message-fetch-field "Content-Type" t)) | |
c96ec15a | 153 | (ctl (and ct (mail-header-parse-content-type ct)))) |
a1506d29 | 154 | (if (and ctl (not (string-match "/" (car ctl)))) |
c113de23 GM |
155 | (setq ctl nil)) |
156 | (goto-char (point-max)) | |
157 | (widen) | |
158 | (forward-line 1) | |
159 | (narrow-to-region (point) (point-max)) | |
160 | (when (or (not ctl) | |
161 | (equal (car ctl) "text/plain")) | |
162 | (rfc1843-decode-region (point) (point-max)))))))) | |
163 | ||
c113de23 GM |
164 | (defvar gnus-decode-header-methods) |
165 | (defvar gnus-decode-encoded-word-methods) | |
166 | ||
167 | (defun rfc1843-gnus-setup () | |
168 | "Setup HZ decoding for Gnus." | |
169 | (require 'gnus-art) | |
170 | (require 'gnus-sum) | |
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 | |
178 | (list | |
179 | (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
180 | 'rfc1843-decode-string))) | |
181 | gnus-decode-header-methods | |
182 | (nconc gnus-decode-header-methods | |
183 | (list | |
184 | (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>") | |
185 | 'rfc1843-decode-region))))) | |
186 | ||
187 | (provide 'rfc1843) | |
188 | ||
189 | ;;; rfc1843.el ends here |