rfc2368.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ;;; rfc2368.el --- support for rfc2368
  2. ;; Copyright (C) 1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Sen Nagata <sen@eccosys.com>
  4. ;; Keywords: mail
  5. ;; This file is part of GNU Emacs.
  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. ;;
  18. ;; notes:
  19. ;;
  20. ;; -repeat after me: "the colon is not part of the header name..."
  21. ;; -if w3 becomes part of emacs, then it may make sense to have this
  22. ;; file depend on w3 -- the maintainer of w3 says merging w/ Emacs
  23. ;; is planned!
  24. ;;
  25. ;; historical note:
  26. ;;
  27. ;; this is intended as a replacement for mailto.el
  28. ;;
  29. ;; acknowledgements:
  30. ;;
  31. ;; the functions that deal w/ unhexifying in this file were basically
  32. ;; taken from w3 -- i hope to replace them w/ something else soon OR
  33. ;; perhaps if w3 becomes a part of emacs soon, use the functions from w3.
  34. ;;; History:
  35. ;;
  36. ;; 0.3:
  37. ;;
  38. ;; added the constant rfc2368-version
  39. ;; implemented first potential fix for a bug in rfc2368-mailto-regexp
  40. ;; implemented first potential fix for a bug in rfc2368-parse-mailto
  41. ;; (both bugs reported by Kenichi OKADA)
  42. ;;
  43. ;; 0.2:
  44. ;;
  45. ;; started to use checkdoc
  46. ;;
  47. ;; 0.1:
  48. ;;
  49. ;; initial implementation
  50. ;;; Code:
  51. ;; only an approximation?
  52. ;; see rfc 1738
  53. (defconst rfc2368-mailto-regexp
  54. "^\\(mailto:\\)\\([^?]+\\)*\\(\\?\\(.*\\)\\)*"
  55. "Regular expression to match and aid in parsing a mailto url.")
  56. ;; describes 'mailto:'
  57. (defconst rfc2368-mailto-scheme-index 1
  58. "Describes the 'mailto:' portion of the url.")
  59. ;; i'm going to call this part the 'prequery'
  60. (defconst rfc2368-mailto-prequery-index 2
  61. "Describes the portion of the url between 'mailto:' and '?'.")
  62. ;; i'm going to call this part the 'query'
  63. (defconst rfc2368-mailto-query-index 4
  64. "Describes the portion of the url after '?'.")
  65. (defun rfc2368-unhexify-string (string)
  66. "Unhexify STRING -- e.g. 'hello%20there' -> 'hello there'."
  67. (replace-regexp-in-string "%[[:xdigit:]]\\{2\\}"
  68. (lambda (match)
  69. (string (string-to-number (substring match 1)
  70. 16)))
  71. string t t))
  72. (defun rfc2368-parse-mailto-url (mailto-url)
  73. "Parse MAILTO-URL, and return an alist of header-name, header-value pairs.
  74. MAILTO-URL should be a RFC 2368 (mailto) compliant url. A cons cell w/ a
  75. key of 'Body' is a special case and is considered a header for this purpose.
  76. The returned alist is intended for use w/ the `compose-mail' interface.
  77. Note: make sure MAILTO-URL has been 'unhtmlized' (e.g. &amp; -> &), before
  78. calling this function."
  79. (let ((case-fold-search t)
  80. prequery query headers-alist)
  81. (setq mailto-url (replace-regexp-in-string "\n" " " mailto-url))
  82. (if (string-match rfc2368-mailto-regexp mailto-url)
  83. (progn
  84. (setq prequery
  85. (match-string rfc2368-mailto-prequery-index mailto-url))
  86. (setq query
  87. (match-string rfc2368-mailto-query-index mailto-url))
  88. ;; build alist of header name-value pairs
  89. (if (not (null query))
  90. (setq headers-alist
  91. (mapcar
  92. (lambda (x)
  93. (let* ((temp-list (split-string x "="))
  94. (header-name (car temp-list))
  95. (header-value (cadr temp-list)))
  96. ;; return ("Header-Name" . "header-value")
  97. (cons
  98. (capitalize (rfc2368-unhexify-string header-name))
  99. (rfc2368-unhexify-string header-value))))
  100. (split-string query "&"))))
  101. ;; deal w/ multiple 'To' recipients
  102. (if prequery
  103. (progn
  104. (setq prequery (rfc2368-unhexify-string prequery))
  105. (if (assoc "To" headers-alist)
  106. (let* ((our-cons-cell
  107. (assoc "To" headers-alist))
  108. (our-cdr
  109. (cdr our-cons-cell)))
  110. (setcdr our-cons-cell (concat prequery ", " our-cdr)))
  111. (setq headers-alist
  112. (cons (cons "To" prequery) headers-alist)))))
  113. headers-alist)
  114. (error "Failed to match a mailto: url"))))
  115. (provide 'rfc2368)
  116. ;;; rfc2368.el ends here