url-mailto.el 4.6 KB

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