mm-extern.el 5.2 KB

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