mm-extern.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ;;; mm-extern.el --- showing message/external-body
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
  4. ;; Keywords: message external-body
  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. ;;; Code:
  18. ;; For Emacs <22.2 and XEmacs.
  19. (eval-and-compile
  20. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  21. (eval-when-compile (require 'cl))
  22. (require 'mm-util)
  23. (require 'mm-decode)
  24. (require 'mm-url)
  25. (defvar gnus-article-mime-handles)
  26. (defvar mm-extern-function-alist
  27. '((local-file . mm-extern-local-file)
  28. (url . mm-extern-url)
  29. (anon-ftp . mm-extern-anon-ftp)
  30. (ftp . mm-extern-ftp)
  31. ;;; (tftp . mm-extern-tftp)
  32. (mail-server . mm-extern-mail-server)
  33. ;;; (afs . mm-extern-afs))
  34. ))
  35. (defvar mm-extern-anonymous "anonymous")
  36. (defun mm-extern-local-file (handle)
  37. (erase-buffer)
  38. (let ((name (cdr (assq 'name (cdr (mm-handle-type handle)))))
  39. (coding-system-for-read mm-binary-coding-system))
  40. (unless name
  41. (error "The filename is not specified"))
  42. (mm-disable-multibyte)
  43. (if (file-exists-p name)
  44. (mm-insert-file-contents name nil nil nil nil t)
  45. (error "File %s is gone" name))))
  46. (defun mm-extern-url (handle)
  47. (erase-buffer)
  48. (let ((url (cdr (assq 'url (cdr (mm-handle-type handle)))))
  49. (name buffer-file-name)
  50. (coding-system-for-read mm-binary-coding-system))
  51. (unless url
  52. (error "URL is not specified"))
  53. (mm-disable-multibyte)
  54. (mm-url-insert-file-contents url)
  55. (setq buffer-file-name name)))
  56. (defun mm-extern-anon-ftp (handle)
  57. (erase-buffer)
  58. (let* ((params (cdr (mm-handle-type handle)))
  59. (name (cdr (assq 'name params)))
  60. (site (cdr (assq 'site params)))
  61. (directory (cdr (assq 'directory params)))
  62. (mode (cdr (assq 'mode params)))
  63. (path (concat "/" (or mm-extern-anonymous
  64. (read-string (format "ID for %s: " site)))
  65. "@" site ":" directory "/" name))
  66. (coding-system-for-read mm-binary-coding-system))
  67. (unless name
  68. (error "The filename is not specified"))
  69. (mm-disable-multibyte)
  70. (mm-insert-file-contents path nil nil nil nil t)))
  71. (defun mm-extern-ftp (handle)
  72. (let (mm-extern-anonymous)
  73. (mm-extern-anon-ftp handle)))
  74. (declare-function message-goto-body "message" ())
  75. (defun mm-extern-mail-server (handle)
  76. (require 'message)
  77. (let* ((params (cdr (mm-handle-type handle)))
  78. (server (cdr (assq 'server params)))
  79. (subject (or (cdr (assq 'subject params)) "none"))
  80. (buf (current-buffer))
  81. info)
  82. (if (y-or-n-p (format "Send a request message to %s? " server))
  83. (save-window-excursion
  84. (message-mail server subject)
  85. (message-goto-body)
  86. (delete-region (point) (point-max))
  87. (insert-buffer-substring buf)
  88. (message "Requesting external body...")
  89. (message-send-and-exit)
  90. (setq info "Request is sent.")
  91. (message info))
  92. (setq info "Request is not sent."))
  93. (goto-char (point-min))
  94. (insert "[" info "]\n\n")))
  95. ;;;###autoload
  96. (defun mm-extern-cache-contents (handle)
  97. "Put the external-body part of HANDLE into its cache."
  98. (let* ((access-type (cdr (assq 'access-type
  99. (cdr (mm-handle-type handle)))))
  100. (func (cdr (assq (intern
  101. (downcase
  102. (or access-type
  103. (error "Couldn't find access type"))))
  104. mm-extern-function-alist)))
  105. handles)
  106. (unless func
  107. (error "Access type (%s) is not supported" access-type))
  108. (mm-with-part handle
  109. (goto-char (point-max))
  110. (insert "\n\n")
  111. ;; It should be just a single MIME handle.
  112. (setq handles (mm-dissect-buffer t)))
  113. (unless (bufferp (car handles))
  114. (mm-destroy-parts handles)
  115. (error "Multipart external body is not supported"))
  116. (with-current-buffer (mm-handle-buffer handles)
  117. (let (good)
  118. (unwind-protect
  119. (progn
  120. (funcall func handle)
  121. (setq good t))
  122. (unless good
  123. (mm-destroy-parts handles))))
  124. (mm-handle-set-cache handle handles))
  125. (setq gnus-article-mime-handles
  126. (mm-merge-handles gnus-article-mime-handles handles))))
  127. ;;;###autoload
  128. (defun mm-inline-external-body (handle &optional no-display)
  129. "Show the external-body part of HANDLE.
  130. This function replaces the buffer of HANDLE with a buffer contains
  131. the entire message.
  132. If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
  133. (unless (mm-handle-cache handle)
  134. (mm-extern-cache-contents handle))
  135. (unless no-display
  136. (save-excursion
  137. (save-restriction
  138. (narrow-to-region (point) (point))
  139. (mm-display-part (mm-handle-cache handle))))
  140. ;; Move undisplayer added to the cached handle to the parent.
  141. (mm-handle-set-undisplayer
  142. handle (mm-handle-undisplayer (mm-handle-cache handle)))
  143. (mm-handle-set-undisplayer (mm-handle-cache handle) nil)))
  144. (provide 'mm-extern)
  145. ;;; mm-extern.el ends here