Initial Commit
[packages] / xemacs-packages / w3 / lisp / url-misc.el
1 ;;; url-misc.el --- Misc Uniform Resource Locator retrieval code
2
3 ;; Copyright (C) 1996-1999, 2002, 2004-2012 Free Software Foundation, Inc.
4
5 ;; Keywords: comm, data, processes
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 by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU 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.  If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Code:
23
24 (eval-when-compile (require 'cl))
25 (require 'url-vars)
26 (require 'url-parse)
27 (autoload 'Info-goto-node "info" "" t)
28 (autoload 'man "man" nil t)
29
30 ;;;###autoload
31 (defun url-man (url)
32   "Fetch a Unix manual page URL."
33   (man (url-filename url))
34   nil)
35
36 ;;;###autoload
37 (defun url-info (url)
38   "Fetch a GNU Info URL."
39   ;; Fetch an info node
40   (let* ((fname (url-filename url))
41          (node (url-unhex-string (or (url-target url) "Top"))))
42     (if (and fname node)
43         (Info-goto-node (concat "(" fname ")" node))
44       (error "Malformed url: %s" (url-recreate-url url)))
45     nil))
46
47 (defun url-do-terminal-emulator (type server port user)
48   (terminal-emulator
49    (generate-new-buffer (format "%s%s" (if user (concat user "@") "") server))
50    (case type
51      (rlogin "rlogin")
52      (telnet "telnet")
53      (tn3270 "tn3270")
54      (otherwise
55       (error "Unknown terminal emulator required: %s" type)))
56    (case type
57      (rlogin
58       (if user
59           (list server "-l" user)
60         (list server)))
61      (telnet
62       (if user (message "Please log in as user: %s" user))
63       (if port
64           (list server port)
65         (list server)))
66      (tn3270
67       (if user (message "Please log in as user: %s" user))
68       (list server)))))
69
70 ;;;###autoload
71 (defun url-generic-emulator-loader (url)
72   (let* ((type (intern (downcase (url-type url))))
73          (server (url-host url))
74          (name (url-user url))
75          (port (number-to-string (url-port url))))
76     (url-do-terminal-emulator type server port name))
77   nil)
78
79 ;;;###autoload
80 (defalias 'url-rlogin 'url-generic-emulator-loader)
81 ;;;###autoload
82 (defalias 'url-telnet 'url-generic-emulator-loader)
83 ;;;###autoload
84 (defalias 'url-tn3270 'url-generic-emulator-loader)
85
86 ;; RFC 2397
87 ;;;###autoload
88 (defun url-data (url)
89   "Fetch a data URL (RFC 2397)."
90   (let ((mediatype nil)
91         ;; The mediatype may need to be hex-encoded too -- see the RFC.
92         (desc (url-unhex-string (url-filename url)))
93         (encoding "8bit")
94         (data nil))
95     (save-excursion
96       (if (not (string-match "\\([^,]*\\)?," desc))
97           (error "Malformed data URL: %s" desc)
98         (setq mediatype (match-string 1 desc))
99         (if (and mediatype (string-match ";base64\\'" mediatype))
100             (setq mediatype (substring mediatype 0 (match-beginning 0))
101                   encoding "base64"))
102         (if (or (null mediatype)
103                 (eq ?\; (aref mediatype 0)))
104           (setq mediatype (concat "text/plain" mediatype)))
105         (setq data (url-unhex-string (substring desc (match-end 0)))))
106       (set-buffer (generate-new-buffer " *url-data*"))
107       (mm-disable-multibyte)
108       (insert (format "Content-Length: %d\n" (length data))
109               "Content-Type: " mediatype "\n"
110               "Content-Encoding: " encoding "\n"
111               "\n")
112       (if data (insert data))
113       (current-buffer))))
114
115 (provide 'url-misc)
116
117 ;;; url-misc.el ends here