Commit | Line | Data |
---|---|---|
8c8b8430 | 1 | ;;; url-mail.el --- Mail Uniform Resource Locator retrieval code |
b81c0abb | 2 | |
ba318903 | 3 | ;; Copyright (C) 1996-1999, 2004-2014 Free Software Foundation, Inc. |
b81c0abb | 4 | |
8c8b8430 SM |
5 | ;; Keywords: comm, data, processes |
6 | ||
b81c0abb SM |
7 | ;; This file is part of GNU Emacs. |
8 | ;; | |
4936186e | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
b81c0abb | 10 | ;; it under the terms of the GNU General Public License as published by |
4936186e GM |
11 | ;; the Free Software Foundation, either version 3 of the License, or |
12 | ;; (at your option) any later version. | |
13 | ||
b81c0abb SM |
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. | |
4936186e | 18 | |
b81c0abb | 19 | ;; You should have received a copy of the GNU General Public License |
4936186e | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b81c0abb SM |
21 | |
22 | ;;; Commentary: | |
23 | ||
24 | ;;; Code: | |
8c8b8430 | 25 | |
8c8b8430 SM |
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 | ||
40234eaf | 60 | (declare-function mail-send-and-exit "sendmail") |
153ef845 | 61 | |
8c8b8430 SM |
62 | ;;;###autoload |
63 | (defun url-mailto (url) | |
64 | "Handle the mailto: URL syntax." | |
65 | (if (url-user url) | |
de0234ea | 66 | ;; malformed mailto URL (mailto://wmperry@gnu.org) instead of |
8c8b8430 | 67 | ;; mailto:wmperry@gnu.org |
d18ec89f | 68 | (setf (url-filename url) (concat (url-user url) "@" (url-filename url)))) |
8c8b8430 SM |
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 | |
6fd388f3 | 75 | (substring url headers-start nil) t t)) |
8c8b8430 SM |
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 | ||
6fd388f3 CY |
86 | (let ((tolist (assoc "to" args))) |
87 | (if tolist | |
88 | (if (not (string= to "")) | |
89 | (setcdr tolist | |
14bd660e | 90 | (list (concat to ", " (cadr tolist))))) |
6fd388f3 CY |
91 | (setq args (cons (list "to" to) args)))) |
92 | ||
8c8b8430 | 93 | (setq subject (cdr-safe (assoc "subject" args))) |
49b545fe RS |
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))) | |
8c8b8430 SM |
99 | (while args |
100 | (if (string= (caar args) "body") | |
101 | (progn | |
40165674 CY |
102 | (goto-char (point-min)) |
103 | (or (search-forward (concat "\n" mail-header-separator "\n") nil t) | |
104 | (goto-char (point-max))) | |
6fd388f3 CY |
105 | (insert (mapconcat |
106 | #'(lambda (string) | |
107 | (replace-regexp-in-string "\r\n" "\n" string)) | |
108 | (cdar args) "\n"))) | |
8c8b8430 SM |
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? ") | |
b0742512 CY |
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)))) | |
8c8b8430 SM |
140 | nil)) |
141 | ||
142 | (provide 'url-mailto) | |
e5566bd5 | 143 | |
b81c0abb | 144 | ;;; url-mailto.el ends here |