rfc1843.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ;;; rfc1843.el --- HZ (rfc1843) decoding
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
  4. ;; Keywords: news HZ HZ+ mail i18n
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Usage:
  18. ;; (require 'rfc1843)
  19. ;; (rfc1843-gnus-setup)
  20. ;;
  21. ;; Test:
  22. ;; (rfc1843-decode-string "~{<:Ky2;S{#,NpJ)l6HK!#~}")
  23. ;;; Code:
  24. ;; For Emacs <22.2 and XEmacs.
  25. (eval-and-compile
  26. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  27. (eval-when-compile (require 'cl))
  28. (require 'mm-util)
  29. (defvar gnus-decode-encoded-word-function)
  30. (defvar gnus-decode-header-function)
  31. (defvar gnus-newsgroup-name)
  32. (defvar rfc1843-word-regexp
  33. "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
  34. (defvar rfc1843-word-regexp-strictly
  35. "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)")
  36. (defvar rfc1843-hzp-word-regexp
  37. "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
  38. \[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
  39. (defvar rfc1843-hzp-word-regexp-strictly
  40. "~\\({\\([\041-\167][\041-\176]\\)+\\|\
  41. \[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
  42. (defcustom rfc1843-decode-loosely nil
  43. "Loosely check HZ encoding if non-nil.
  44. When it is set non-nil, only buffers or strings with strictly
  45. HZ-encoded are decoded."
  46. :type 'boolean
  47. :group 'mime)
  48. (defcustom rfc1843-decode-hzp t
  49. "HZ+ decoding support if non-nil.
  50. HZ+ specification (also known as HZP) is to provide a standardized
  51. 7-bit representation of mixed Big5, GB, and ASCII text for convenient
  52. e-mail transmission, news posting, etc.
  53. The document of HZ+ 0.78 specification can be found at
  54. ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc"
  55. :type 'boolean
  56. :group 'mime)
  57. (defcustom rfc1843-newsgroups-regexp "chinese\\|hz"
  58. "Regexp of newsgroups in which might be HZ encoded."
  59. :type 'string
  60. :group 'mime)
  61. (defun rfc1843-decode-region (from to)
  62. "Decode HZ in the region between FROM and TO."
  63. (interactive "r")
  64. (let (str firstc)
  65. (save-excursion
  66. (goto-char from)
  67. (if (or rfc1843-decode-loosely
  68. (re-search-forward (if rfc1843-decode-hzp
  69. rfc1843-hzp-word-regexp-strictly
  70. rfc1843-word-regexp-strictly) to t))
  71. (save-restriction
  72. (narrow-to-region from to)
  73. (goto-char (point-min))
  74. (while (re-search-forward (if rfc1843-decode-hzp
  75. rfc1843-hzp-word-regexp
  76. rfc1843-word-regexp) (point-max) t)
  77. ;;; Text with extents may cause XEmacs crash
  78. (setq str (buffer-substring-no-properties
  79. (match-beginning 1)
  80. (match-end 1)))
  81. (setq firstc (aref str 0))
  82. (insert (mm-decode-coding-string
  83. (rfc1843-decode
  84. (prog1
  85. (substring str 1)
  86. (delete-region (match-beginning 0) (match-end 0)))
  87. firstc)
  88. (if (eq firstc ?{) 'cn-gb-2312 'cn-big5))))
  89. (goto-char (point-min))
  90. (while (search-forward "~" (point-max) t)
  91. (cond ((eq (char-after) ?\n)
  92. (delete-char -1)
  93. (delete-char 1))
  94. ((eq (char-after) ?~)
  95. (delete-char 1)))))))))
  96. (defun rfc1843-decode-string (string)
  97. "Decode HZ STRING and return the results."
  98. (let ((m (mm-multibyte-p)))
  99. (with-temp-buffer
  100. (when m
  101. (mm-enable-multibyte))
  102. (insert string)
  103. (inline
  104. (rfc1843-decode-region (point-min) (point-max)))
  105. (buffer-string))))
  106. (defun rfc1843-decode (word &optional firstc)
  107. "Decode HZ WORD and return it."
  108. (let ((i -1) (s (substring word 0)) v)
  109. (if (or (not firstc) (eq firstc ?{))
  110. (while (< (incf i) (length s))
  111. (if (eq (setq v (aref s i)) ? ) nil
  112. (aset s i (+ 128 v))))
  113. (while (< (incf i) (length s))
  114. (if (eq (setq v (aref s i)) ? ) nil
  115. (setq v (+ (* 94 v) (aref s (1+ i)) -3135))
  116. (aset s i (+ (/ v 157) (if (eq firstc ?<) 201 161)))
  117. (setq v (% v 157))
  118. (aset s (incf i) (+ v (if (< v 63) 64 98))))))
  119. s))
  120. (autoload 'mail-header-parse-content-type "mail-parse")
  121. (autoload 'message-narrow-to-head "message")
  122. (declare-function message-fetch-field "message" (header &optional not-all))
  123. (defun rfc1843-decode-article-body ()
  124. "Decode HZ encoded text in the article body."
  125. (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
  126. (or gnus-newsgroup-name ""))
  127. (save-excursion
  128. (save-restriction
  129. (message-narrow-to-head)
  130. (let* ((inhibit-point-motion-hooks t)
  131. (case-fold-search t)
  132. (ct (message-fetch-field "Content-Type" t))
  133. (ctl (and ct (mail-header-parse-content-type ct))))
  134. (if (and ctl (not (string-match "/" (car ctl))))
  135. (setq ctl nil))
  136. (goto-char (point-max))
  137. (widen)
  138. (forward-line 1)
  139. (narrow-to-region (point) (point-max))
  140. (when (or (not ctl)
  141. (equal (car ctl) "text/plain"))
  142. (rfc1843-decode-region (point) (point-max))))))))
  143. (defvar gnus-decode-header-methods)
  144. (defvar gnus-decode-encoded-word-methods)
  145. (defun rfc1843-gnus-setup ()
  146. "Setup HZ decoding for Gnus."
  147. (require 'gnus-art)
  148. (require 'gnus-sum)
  149. (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t)
  150. (setq gnus-decode-encoded-word-function
  151. 'gnus-multi-decode-encoded-word-string
  152. gnus-decode-header-function
  153. 'gnus-multi-decode-header
  154. gnus-decode-encoded-word-methods
  155. (nconc gnus-decode-encoded-word-methods
  156. (list
  157. (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
  158. 'rfc1843-decode-string)))
  159. gnus-decode-header-methods
  160. (nconc gnus-decode-header-methods
  161. (list
  162. (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
  163. 'rfc1843-decode-region)))))
  164. (provide 'rfc1843)
  165. ;;; rfc1843.el ends here