1 ;;; rfc2368.el --- support for rfc 2368
3 ;;; Copyright 1999 Sen Nagata <sen@eccosys.com>
5 ;; Keywords: rfc 2368, mailto, mail
9 ;; This file is not a part of GNU Emacs.
15 ;; -repeat after me: "the colon is not part of the header name..."
16 ;; -if w3 becomes part of emacs, then it may make sense to have this
17 ;; file depend on w3 -- the maintainer of w3 says merging w/ emacs
22 ;; this is intended as a replacement for mailto.el
26 ;; the functions that deal w/ unhexifying in this file were basically
27 ;; taken from w3 -- i hope to replace them w/ something else soon OR
28 ;; perhaps if w3 becomes a part of emacs soon, use the functions from w3.
34 ;; added the constant rfc2368-version
35 ;; implemented first potential fix for a bug in rfc2368-mailto-regexp
36 ;; implemented first potential fix for a bug in rfc2368-parse-mailto
37 ;; (both bugs reported by Kenichi OKADA)
41 ;; started to use checkdoc
45 ;; initial implementation
48 (defconst rfc2368-version "rfc2368.el 0.3")
50 ;; only an approximation?
52 (defconst rfc2368-mailto-regexp
53 "^\\(mailto:\\)\\([^?]+\\)*\\(\\?\\(.*\\)\\)*"
54 "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 '?'.")
66 ;; for dealing w/ unhexifying strings, my preferred approach is to use
67 ;; a 'string-replace-match-using-function' which can perform a
68 ;; string-replace-match and compute the replacement text based on a
69 ;; passed function -- however, emacs doesn't seem to have such a
72 ;; for the moment a rip-off of url-unhex (w3/url.el)
73 (defun rfc2368-unhexify-char (char)
74 "Unhexify CHAR -- e.g. %20 -> <SPC>."
81 ;; for the moment a rip-off of url-unhex-string (w3/url.el) (slightly modified)
82 (defun rfc2368-unhexify-string (string)
83 "Unhexify STRING -- e.g. 'hello%20there' -> 'hello there'."
84 (let ((case-fold-search t)
86 (while (string-match "%[0-9a-f][0-9a-f]" string)
87 (let* ((start (match-beginning 0))
89 (rfc2368-unhexify-char (elt string (+ start 1))))
90 (rfc2368-unhexify-char (elt string (+ start 2))))))
92 result (substring string 0 start)
93 (char-to-string hex-code))
94 string (substring string (match-end 0)))))
95 ;; it seems clearer to do things this way than to just return:
96 ;; (concat result string)
97 (setq result (concat result string))
100 (defun rfc2368-parse-mailto-url (mailto-url)
101 "Parse MAILTO-URL, and return an alist of header-name, header-value pairs.
102 MAILTO-URL should be a RFC 2368 (mailto) compliant url. A cons cell w/ a
103 key of 'Body' is a special case and is considered a header for this purpose.
104 The returned alist is intended for use w/ the `compose-mail' interface.
105 Note: make sure MAILTO-URL has been 'unhtmlized' (e.g. & -> &), before
106 calling this function."
107 (let ((case-fold-search t)
108 prequery query headers-alist)
110 (if (string-match rfc2368-mailto-regexp mailto-url)
114 (match-string rfc2368-mailto-prequery-index mailto-url))
117 (match-string rfc2368-mailto-query-index mailto-url))
119 ;; build alist of header name-value pairs
120 (if (not (null query))
124 (let* ((temp-list (split-string x "="))
125 (header-name (car temp-list))
126 (header-value (cadr temp-list)))
127 ;; return ("Header-Name" . "header-value")
129 (capitalize (rfc2368-unhexify-string header-name))
130 (rfc2368-unhexify-string header-value))))
131 (split-string query "&"))))
133 ;; deal w/ multiple 'To' recipients
136 (if (assoc "To" headers-alist)
137 (let* ((our-cons-cell
138 (assoc "To" headers-alist))
140 (cdr our-cons-cell)))
141 (setcdr our-cons-cell (concat our-cdr ", " prequery)))
143 (cons (cons "To" prequery) headers-alist)))))
147 (error "Failed to match a mailto: url"))
152 ;;; rfc2368.el ends here