Initial Commit
[packages] / xemacs-packages / w3 / lisp / url-mailto.el
1 ;;; url-mail.el --- Mail Uniform Resource Locator retrieval code
2
3 ;; Copyright (C) 1996-1999, 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 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'url-vars)
27 (require 'url-parse)
28 (require 'url-util)
29
30 ;;;###autoload
31 (defun url-mail (&rest args)
32   (interactive "P")
33   (if (fboundp 'message-mail)
34       (apply 'message-mail args)
35     (or (apply 'mail args)
36         (error "Mail aborted"))))
37
38 (defun url-mail-goto-field (field)
39   (if (not field)
40       (goto-char (point-max))
41     (let ((dest nil)
42           (lim nil)
43           (case-fold-search t))
44       (save-excursion
45         (goto-char (point-min))
46         (if (re-search-forward (regexp-quote mail-header-separator) nil t)
47             (setq lim (match-beginning 0)))
48         (goto-char (point-min))
49         (if (re-search-forward (concat "^" (regexp-quote field) ":") lim t)
50             (setq dest (match-beginning 0))))
51       (if dest
52           (progn
53             (goto-char dest)
54             (end-of-line))
55         (goto-char lim)
56         (insert (capitalize field) ": ")
57         (save-excursion
58           (insert "\n"))))))
59
60 (declare-function mail-send-and-exit "sendmail")
61
62 ;;;###autoload
63 (defun url-mailto (url)
64   "Handle the mailto: URL syntax."
65   (if (url-user url)
66       ;; malformed mailto URL (mailto://wmperry@gnu.org) instead of
67       ;; mailto:wmperry@gnu.org
68       (setf (url-filename url) (concat (url-user url) "@" (url-filename url))))
69   (setq url (url-filename url))
70   (let (to args source-url subject func headers-start)
71     (if (string-match (regexp-quote "?") url)
72         (setq headers-start (match-end 0)
73               to (url-unhex-string (substring url 0 (match-beginning 0)))
74               args (url-parse-query-string
75                     (substring url headers-start nil) t t))
76       (setq to (url-unhex-string url)))
77     (setq source-url (url-view-url t))
78     (if (and url-request-data (not (assoc "subject" args)))
79         (setq args (cons (list "subject"
80                                (concat "Automatic submission from "
81                                        url-package-name "/"
82                                        url-package-version)) args)))
83     (if (and source-url (not (assoc "x-url-from" args)))
84         (setq args (cons (list "x-url-from" source-url) args)))
85
86     (let ((tolist (assoc "to" args)))
87       (if tolist
88           (if (not (string= to ""))
89               (setcdr tolist
90                       (list (concat to ", " (cadr tolist)))))
91         (setq args (cons (list "to" to) args))))
92
93     (setq subject (cdr-safe (assoc "subject" args)))
94     (if (eq url-mail-command 'compose-mail)
95         (compose-mail nil nil nil 'new)
96       (if (eq url-mail-command 'mail)
97           (mail 'new)
98         (funcall url-mail-command)))
99     (while args
100       (if (string= (caar args) "body")
101           (progn
102             (goto-char (point-min))
103             (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
104                 (goto-char (point-max)))
105             (insert (mapconcat 
106                      #'(lambda (string)
107                          (replace-regexp-in-string "\r\n" "\n" string))
108                      (cdar args) "\n")))
109         (url-mail-goto-field (caar args))
110         (setq func (intern-soft (concat "mail-" (caar args))))
111         (insert (mapconcat 'identity (cdar args) ", ")))
112       (setq args (cdr args)))
113     ;; (url-mail-goto-field "User-Agent")
114 ;;     (insert url-package-name "/" url-package-version " URL/" url-version)
115     (if (not url-request-data)
116         (progn
117           (set-buffer-modified-p nil)
118           (if subject
119               (url-mail-goto-field nil)
120             (url-mail-goto-field "subject")))
121       (if url-request-extra-headers
122           (mapconcat
123            (lambda (x)
124              (url-mail-goto-field (car x))
125              (insert (cdr x)))
126            url-request-extra-headers ""))
127       (goto-char (point-max))
128       (insert url-request-data)
129       ;; It seems Microsoft-ish to send without warning.
130       ;; Fixme: presumably this should depend on a privacy setting.
131       (if (y-or-n-p "Send this auto-generated mail? ")
132           (let ((buffer (current-buffer)))
133             (cond ((eq url-mail-command 'compose-mail)
134                    (funcall (get mail-user-agent 'sendfunc) nil))
135                   ;; otherwise, we can't be sure
136                   ((fboundp 'message-send-and-exit)
137                    (message-send-and-exit))
138                   (t (mail-send-and-exit nil)))
139             (kill-buffer buffer))))
140     nil))
141
142 (provide 'url-mailto)
143
144 ;;; url-mailto.el ends here