Don't require url-auto.
[bpt/emacs.git] / lisp / url / url-mailto.el
1 ;;; url-mail.el --- Mail Uniform Resource Locator retrieval code
2 ;; Author: $Author: fx $
3 ;; Created: $Date: 2001/10/05 17:04:06 $
4 ;; Version: $Revision: 1.4 $
5 ;; Keywords: comm, data, processes
6
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (c) 1993 - 1996 by William M. Perry <wmperry@cs.indiana.edu>
9 ;;; Copyright (c) 1996 - 1999 Free Software Foundation, Inc.
10 ;;;
11 ;;; This file is part of GNU Emacs.
12 ;;;
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;;; it under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 2, or (at your option)
16 ;;; any later version.
17 ;;;
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;;; Boston, MA 02111-1307, USA.
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28
29 (eval-when-compile (require 'cl))
30 (require 'url-vars)
31 (require 'url-parse)
32 (require 'url-util)
33
34 ;;;###autoload
35 (defun url-mail (&rest args)
36 (interactive "P")
37 (if (fboundp 'message-mail)
38 (apply 'message-mail args)
39 (or (apply 'mail args)
40 (error "Mail aborted"))))
41
42 (defun url-mail-goto-field (field)
43 (if (not field)
44 (goto-char (point-max))
45 (let ((dest nil)
46 (lim nil)
47 (case-fold-search t))
48 (save-excursion
49 (goto-char (point-min))
50 (if (re-search-forward (regexp-quote mail-header-separator) nil t)
51 (setq lim (match-beginning 0)))
52 (goto-char (point-min))
53 (if (re-search-forward (concat "^" (regexp-quote field) ":") lim t)
54 (setq dest (match-beginning 0))))
55 (if dest
56 (progn
57 (goto-char dest)
58 (end-of-line))
59 (goto-char lim)
60 (insert (capitalize field) ": ")
61 (save-excursion
62 (insert "\n"))))))
63
64 ;;;###autoload
65 (defun url-mailto (url)
66 "Handle the mailto: URL syntax."
67 (if (url-user url)
68 ;; malformed mailto URL (mailto://wmperry@gnu.org instead of
69 ;; mailto:wmperry@gnu.org
70 (url-set-filename url (concat (url-user url) "@" (url-filename url))))
71 (setq url (url-filename url))
72 (let (to args source-url subject func headers-start)
73 (if (string-match (regexp-quote "?") url)
74 (setq headers-start (match-end 0)
75 to (url-unhex-string (substring url 0 (match-beginning 0)))
76 args (url-parse-query-string
77 (substring url headers-start nil) t))
78 (setq to (url-unhex-string url)))
79 (setq source-url (url-view-url t))
80 (if (and url-request-data (not (assoc "subject" args)))
81 (setq args (cons (list "subject"
82 (concat "Automatic submission from "
83 url-package-name "/"
84 url-package-version)) args)))
85 (if (and source-url (not (assoc "x-url-from" args)))
86 (setq args (cons (list "x-url-from" source-url) args)))
87
88 (if (assoc "to" args)
89 (push to (cdr (assoc "to" args)))
90 (setq args (cons (list "to" to) args)))
91 (setq subject (cdr-safe (assoc "subject" args)))
92 (if (fboundp url-mail-command) (funcall url-mail-command) (mail))
93 (while args
94 (if (string= (caar args) "body")
95 (progn
96 (goto-char (point-max))
97 (insert (mapconcat 'identity (cdar args) "\n")))
98 (url-mail-goto-field (caar args))
99 (setq func (intern-soft (concat "mail-" (caar args))))
100 (insert (mapconcat 'identity (cdar args) ", ")))
101 (setq args (cdr args)))
102 ;; (url-mail-goto-field "User-Agent")
103 ;; (insert url-package-name "/" url-package-version " URL/" url-version)
104 (if (not url-request-data)
105 (progn
106 (set-buffer-modified-p nil)
107 (if subject
108 (url-mail-goto-field nil)
109 (url-mail-goto-field "subject")))
110 (if url-request-extra-headers
111 (mapconcat
112 (lambda (x)
113 (url-mail-goto-field (car x))
114 (insert (cdr x)))
115 url-request-extra-headers ""))
116 (goto-char (point-max))
117 (insert url-request-data)
118 ;; It seems Microsoft-ish to send without warning.
119 ;; Fixme: presumably this should depend on a privacy setting.
120 (if (y-or-n-p "Send this auto-generated mail? ")
121 (cond ((eq url-mail-command 'compose-mail)
122 (funcall (get mail-user-agent 'sendfunc) nil))
123 ;; otherwise, we can't be sure
124 ((fboundp 'message-mail)
125 (message-send-and-exit))
126 (t (mail-send-and-exit nil)))))
127 nil))
128
129 (provide 'url-mailto)