rfc1843.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ;;; rfc1843.el --- HZ (rfc1843) decoding
  2. ;; Copyright (C) 1998-2015 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. (eval-when-compile (require 'cl))
  25. (require 'mm-util)
  26. (defvar gnus-decode-encoded-word-function)
  27. (defvar gnus-decode-header-function)
  28. (defvar gnus-newsgroup-name)
  29. (defvar rfc1843-word-regexp
  30. "~\\({\\([\041-\167][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
  31. (defvar rfc1843-word-regexp-strictly
  32. "~\\({\\([\041-\167][\041-\176]\\)+\\)\\(~}\\|$\\)")
  33. (defvar rfc1843-hzp-word-regexp
  34. "~\\({\\([\041-\167][\041-\176]\\| \\)+\\|\
  35. \[<>]\\([\041-\175][\041-\176]\\| \\)+\\)\\(~}\\|$\\)")
  36. (defvar rfc1843-hzp-word-regexp-strictly
  37. "~\\({\\([\041-\167][\041-\176]\\)+\\|\
  38. \[<>]\\([\041-\175][\041-\176]\\)+\\)\\(~}\\|$\\)")
  39. (defcustom rfc1843-decode-loosely nil
  40. "Loosely check HZ encoding if non-nil.
  41. When it is set non-nil, only buffers or strings with strictly
  42. HZ-encoded are decoded."
  43. :type 'boolean
  44. :group 'mime)
  45. (defcustom rfc1843-decode-hzp t
  46. "HZ+ decoding support if non-nil.
  47. HZ+ specification (also known as HZP) is to provide a standardized
  48. 7-bit representation of mixed Big5, GB, and ASCII text for convenient
  49. e-mail transmission, news posting, etc.
  50. The document of HZ+ 0.78 specification can be found at
  51. ftp://ftp.math.psu.edu/pub/simpson/chinese/hzp/hzp.doc"
  52. :type 'boolean
  53. :group 'mime)
  54. (defcustom rfc1843-newsgroups-regexp "chinese\\|hz"
  55. "Regexp of newsgroups in which might be HZ encoded."
  56. :type 'string
  57. :group 'mime)
  58. (defun rfc1843-decode-region (from to)
  59. "Decode HZ in the region between FROM and TO."
  60. (interactive "r")
  61. (let (str firstc)
  62. (save-excursion
  63. (goto-char from)
  64. (if (or rfc1843-decode-loosely
  65. (re-search-forward (if rfc1843-decode-hzp
  66. rfc1843-hzp-word-regexp-strictly
  67. rfc1843-word-regexp-strictly) to t))
  68. (save-restriction
  69. (narrow-to-region from to)
  70. (goto-char (point-min))
  71. (while (re-search-forward (if rfc1843-decode-hzp
  72. rfc1843-hzp-word-regexp
  73. rfc1843-word-regexp) (point-max) t)
  74. ;;; Text with extents may cause XEmacs crash
  75. (setq str (buffer-substring-no-properties
  76. (match-beginning 1)
  77. (match-end 1)))
  78. (setq firstc (aref str 0))
  79. (insert (mm-decode-coding-string
  80. (rfc1843-decode
  81. (prog1
  82. (substring str 1)
  83. (delete-region (match-beginning 0) (match-end 0)))
  84. firstc)
  85. (if (eq firstc ?{) 'cn-gb-2312 'cn-big5))))
  86. (goto-char (point-min))
  87. (while (search-forward "~" (point-max) t)
  88. (cond ((eq (char-after) ?\n)
  89. (delete-char -1)
  90. (delete-char 1))
  91. ((eq (char-after) ?~)
  92. (delete-char 1)))))))))
  93. (defun rfc1843-decode-string (string)
  94. "Decode HZ STRING and return the results."
  95. (let ((m (mm-multibyte-p)))
  96. (with-temp-buffer
  97. (when m
  98. (mm-enable-multibyte))
  99. (insert string)
  100. (inline
  101. (rfc1843-decode-region (point-min) (point-max)))
  102. (buffer-string))))
  103. (defun rfc1843-decode (word &optional firstc)
  104. "Decode HZ WORD and return it."
  105. (let ((i -1) (s (substring word 0)) v)
  106. (if (or (not firstc) (eq firstc ?{))
  107. (while (< (incf i) (length s))
  108. (if (eq (setq v (aref s i)) ? ) nil
  109. (aset s i (+ 128 v))))
  110. (while (< (incf i) (length s))
  111. (if (eq (setq v (aref s i)) ? ) nil
  112. (setq v (+ (* 94 v) (aref s (1+ i)) -3135))
  113. (aset s i (+ (/ v 157) (if (eq firstc ?<) 201 161)))
  114. (setq v (% v 157))
  115. (aset s (incf i) (+ v (if (< v 63) 64 98))))))
  116. s))
  117. (autoload 'mail-header-parse-content-type "mail-parse")
  118. (autoload 'message-narrow-to-head "message")
  119. (declare-function message-fetch-field "message" (header &optional not-all))
  120. (defun rfc1843-decode-article-body ()
  121. "Decode HZ encoded text in the article body."
  122. (if (string-match (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
  123. (or gnus-newsgroup-name ""))
  124. (save-excursion
  125. (save-restriction
  126. (message-narrow-to-head)
  127. (let* ((inhibit-point-motion-hooks t)
  128. (case-fold-search t)
  129. (ct (message-fetch-field "Content-Type" t))
  130. (ctl (and ct (mail-header-parse-content-type ct))))
  131. (if (and ctl (not (string-match "/" (car ctl))))
  132. (setq ctl nil))
  133. (goto-char (point-max))
  134. (widen)
  135. (forward-line 1)
  136. (narrow-to-region (point) (point-max))
  137. (when (or (not ctl)
  138. (equal (car ctl) "text/plain"))
  139. (rfc1843-decode-region (point) (point-max))))))))
  140. (defvar gnus-decode-header-methods)
  141. (defvar gnus-decode-encoded-word-methods)
  142. (defun rfc1843-gnus-setup ()
  143. "Setup HZ decoding for Gnus."
  144. (require 'gnus-art)
  145. (require 'gnus-sum)
  146. (add-hook 'gnus-article-decode-hook 'rfc1843-decode-article-body t)
  147. (setq gnus-decode-encoded-word-function
  148. 'gnus-multi-decode-encoded-word-string
  149. gnus-decode-header-function
  150. 'gnus-multi-decode-header
  151. gnus-decode-encoded-word-methods
  152. (nconc gnus-decode-encoded-word-methods
  153. (list
  154. (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
  155. 'rfc1843-decode-string)))
  156. gnus-decode-header-methods
  157. (nconc gnus-decode-header-methods
  158. (list
  159. (cons (concat "\\<\\(" rfc1843-newsgroups-regexp "\\)\\>")
  160. 'rfc1843-decode-region)))))
  161. (provide 'rfc1843)
  162. ;;; rfc1843.el ends here