access-type=mail-server
[gnus] / lisp / mm-extern.el
1 ;;; mm-extern.el --- showing message/external-body
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: message external-body
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile 
29   (require 'cl))
30
31 (require 'mm-util)
32 (require 'mm-decode)
33
34 (defvar mm-extern-function-alist
35   '((local-file . mm-extern-local-file)
36     (url . mm-extern-url)
37     (anon-ftp . mm-extern-anon-ftp)
38     (ftp . mm-extern-ftp)
39 ;;;     (tftp . mm-extern-tftp)
40     (mail-server . mm-extern-mail-server)
41 ;;;     (afs . mm-extern-afs))
42     ))
43
44 (defvar mm-extern-anonymous "anonymous")
45
46 (defun mm-extern-local-file (handle)
47   (erase-buffer)
48   (let ((name (cdr (assq 'name (cdr (mm-handle-type handle)))))
49         (coding-system-for-read mm-binary-coding-system))
50     (unless name
51       (error "The filename is not specified."))
52     (mm-disable-multibyte-mule4)
53     (mm-insert-file-contents name nil nil nil nil t)))
54
55 (defun mm-extern-url (handle)
56   (erase-buffer)
57   (require 'url)
58   (let ((url (cdr (assq 'url (cdr (mm-handle-type handle)))))
59         (name buffer-file-name)
60         (coding-system-for-read mm-binary-coding-system))
61     (unless url
62       (error "URL is not specified."))
63     (mm-with-unibyte-current-buffer-mule4
64       (url-insert-file-contents url))
65     (mm-disable-multibyte-mule4)
66     (setq buffer-file-name name)))
67
68 (defun mm-extern-anon-ftp (handle)
69   (erase-buffer)
70   (let* ((params (cdr (mm-handle-type handle)))
71          (name (cdr (assq 'name params)))
72          (site (cdr (assq 'site params)))
73          (directory (cdr (assq 'directory params)))
74          (mode (cdr (assq 'mode params)))
75          (path (concat "/" (or mm-extern-anonymous
76                                (read-string (format "ID for %s: " site)))
77                        "@" site ":" directory "/" name))
78          (coding-system-for-read mm-binary-coding-system))
79     (unless name
80       (error "The filename is not specified."))
81     (mm-disable-multibyte-mule4)
82     (mm-insert-file-contents path nil nil nil nil t)))
83
84 (defun mm-extern-ftp (handle)
85   (let (mm-extern-anonymous)
86     (mm-extern-anon-ftp handle)))
87
88 (defun mm-extern-mail-server (handle)
89   (require 'message)
90   (let* ((params (cdr (mm-handle-type handle)))
91          (server (cdr (assq 'server params)))
92          (subject (or (cdr (assq 'subject params)) "none"))
93          (buf (current-buffer))
94          info)
95     (if (y-or-n-p (format "Send a request message to %s?" server))
96         (save-window-excursion
97           (message-mail server subject)
98           (message-goto-body)
99           (delete-region (point) (point-max))
100           (insert-buffer buf)
101           (message "Requesting external body...")
102           (message-send-and-exit)
103           (setq info "Request is sent.")
104           (message info))
105       (setq info "Request is not sent."))
106     (goto-char (point-min))
107     (insert "[" info "]\n\n")))
108
109 ;;;###autoload
110 (defun mm-inline-external-body (handle &optional no-display)
111   "Show the external-body part of HANDLE.
112 This function replaces the buffer of HANDLE with a buffer contains 
113 the entire message.
114 If NO-DISPLAY is nil, display it. Otherwise, do nothing after replacing."
115   (let* ((access-type (cdr (assq 'access-type 
116                                  (cdr (mm-handle-type handle)))))
117          (func (cdr (assq (intern (downcase access-type))
118                           mm-extern-function-alist)))
119          gnus-displaying-mime buf
120          handles)
121     (unless (mm-handle-cache handle)
122       (unless func
123         (error (format "Access type (%s) is not supported." access-type)))
124       (with-temp-buffer
125         (mm-insert-part handle)
126         (goto-char (point-max))
127         (insert "\n\n")
128         (setq handles (mm-dissect-buffer t)))
129       (unless (bufferp (car handles))
130         (mm-destroy-parts handles)
131         (error "Multipart external body is not supported."))
132       (save-excursion ;; single part
133         (set-buffer (setq buf (mm-handle-buffer handles)))
134         (condition-case err
135             (funcall func handle)
136           (error 
137            (mm-destroy-parts handles)
138            (error err)))
139         (mm-handle-set-cache handle handles))
140       (push handles gnus-article-mime-handles))
141     (unless no-display
142       (save-excursion
143         (save-restriction
144           (narrow-to-region (point) (point))
145           (gnus-display-mime (mm-handle-cache handle))
146           (mm-handle-set-undisplayer
147            handle
148            `(lambda ()
149               (let (buffer-read-only)
150                 (condition-case nil
151                     ;; This is only valid on XEmacs.
152                     (mapcar (lambda (prop)
153                             (remove-specifier
154                              (face-property 'default prop) (current-buffer)))
155                             '(background background-pixmap foreground))
156                   (error nil))
157                 (delete-region ,(point-min-marker) ,(point-max-marker))))))))))
158
159 ;; mm-extern.el ends here