(url-dav-file-attributes): Fix declaration.
[bpt/emacs.git] / lisp / url / url-mailto.el
CommitLineData
8c8b8430 1;;; url-mail.el --- Mail Uniform Resource Locator retrieval code
b81c0abb 2
71ddfde5 3;; Copyright (C) 1996, 1997, 1998, 1999, 2004,
ae940284 4;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
b81c0abb 5
8c8b8430
SM
6;; Keywords: comm, data, processes
7
b81c0abb
SM
8;; This file is part of GNU Emacs.
9;;
4936186e 10;; GNU Emacs is free software: you can redistribute it and/or modify
b81c0abb 11;; it under the terms of the GNU General Public License as published by
4936186e
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
14
b81c0abb
SM
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
4936186e 19
b81c0abb 20;; You should have received a copy of the GNU General Public License
4936186e 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b81c0abb
SM
22
23;;; Commentary:
24
25;;; Code:
8c8b8430 26
8c8b8430
SM
27(require 'url-vars)
28(require 'url-parse)
29(require 'url-util)
30
31;;;###autoload
32(defun url-mail (&rest args)
33 (interactive "P")
34 (if (fboundp 'message-mail)
35 (apply 'message-mail args)
36 (or (apply 'mail args)
37 (error "Mail aborted"))))
38
39(defun url-mail-goto-field (field)
40 (if (not field)
41 (goto-char (point-max))
42 (let ((dest nil)
43 (lim nil)
44 (case-fold-search t))
45 (save-excursion
46 (goto-char (point-min))
47 (if (re-search-forward (regexp-quote mail-header-separator) nil t)
48 (setq lim (match-beginning 0)))
49 (goto-char (point-min))
50 (if (re-search-forward (concat "^" (regexp-quote field) ":") lim t)
51 (setq dest (match-beginning 0))))
52 (if dest
53 (progn
54 (goto-char dest)
55 (end-of-line))
56 (goto-char lim)
57 (insert (capitalize field) ": ")
58 (save-excursion
59 (insert "\n"))))))
60
40234eaf 61(declare-function mail-send-and-exit "sendmail")
153ef845 62
8c8b8430
SM
63;;;###autoload
64(defun url-mailto (url)
65 "Handle the mailto: URL syntax."
66 (if (url-user url)
de0234ea 67 ;; malformed mailto URL (mailto://wmperry@gnu.org) instead of
8c8b8430 68 ;; mailto:wmperry@gnu.org
d18ec89f 69 (setf (url-filename url) (concat (url-user url) "@" (url-filename url))))
8c8b8430
SM
70 (setq url (url-filename url))
71 (let (to args source-url subject func headers-start)
72 (if (string-match (regexp-quote "?") url)
73 (setq headers-start (match-end 0)
74 to (url-unhex-string (substring url 0 (match-beginning 0)))
75 args (url-parse-query-string
6fd388f3 76 (substring url headers-start nil) t t))
8c8b8430
SM
77 (setq to (url-unhex-string url)))
78 (setq source-url (url-view-url t))
79 (if (and url-request-data (not (assoc "subject" args)))
80 (setq args (cons (list "subject"
81 (concat "Automatic submission from "
82 url-package-name "/"
83 url-package-version)) args)))
84 (if (and source-url (not (assoc "x-url-from" args)))
85 (setq args (cons (list "x-url-from" source-url) args)))
86
6fd388f3
CY
87 (let ((tolist (assoc "to" args)))
88 (if tolist
89 (if (not (string= to ""))
90 (setcdr tolist
14bd660e 91 (list (concat to ", " (cadr tolist)))))
6fd388f3
CY
92 (setq args (cons (list "to" to) args))))
93
8c8b8430 94 (setq subject (cdr-safe (assoc "subject" args)))
49b545fe
RS
95 (if (eq url-mail-command 'compose-mail)
96 (compose-mail nil nil nil 'new)
97 (if (eq url-mail-command 'mail)
98 (mail 'new)
99 (funcall url-mail-command)))
8c8b8430
SM
100 (while args
101 (if (string= (caar args) "body")
102 (progn
40165674
CY
103 (goto-char (point-min))
104 (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
105 (goto-char (point-max)))
6fd388f3
CY
106 (insert (mapconcat
107 #'(lambda (string)
108 (replace-regexp-in-string "\r\n" "\n" string))
109 (cdar args) "\n")))
8c8b8430
SM
110 (url-mail-goto-field (caar args))
111 (setq func (intern-soft (concat "mail-" (caar args))))
112 (insert (mapconcat 'identity (cdar args) ", ")))
113 (setq args (cdr args)))
114 ;; (url-mail-goto-field "User-Agent")
115;; (insert url-package-name "/" url-package-version " URL/" url-version)
116 (if (not url-request-data)
117 (progn
118 (set-buffer-modified-p nil)
119 (if subject
120 (url-mail-goto-field nil)
121 (url-mail-goto-field "subject")))
122 (if url-request-extra-headers
123 (mapconcat
124 (lambda (x)
125 (url-mail-goto-field (car x))
126 (insert (cdr x)))
127 url-request-extra-headers ""))
128 (goto-char (point-max))
129 (insert url-request-data)
130 ;; It seems Microsoft-ish to send without warning.
131 ;; Fixme: presumably this should depend on a privacy setting.
132 (if (y-or-n-p "Send this auto-generated mail? ")
b0742512
CY
133 (let ((buffer (current-buffer)))
134 (cond ((eq url-mail-command 'compose-mail)
135 (funcall (get mail-user-agent 'sendfunc) nil))
136 ;; otherwise, we can't be sure
137 ((fboundp 'message-send-and-exit)
138 (message-send-and-exit))
139 (t (mail-send-and-exit nil)))
140 (kill-buffer buffer))))
8c8b8430
SM
141 nil))
142
143(provide 'url-mailto)
e5566bd5 144
b81c0abb
SM
145;; arch-tag: 7b7ad52e-8760-497b-9444-75fae14e34c5
146;;; url-mailto.el ends here