1 ;;; rfc2368.el --- support for rfc 2368
3 ;; Copyright (C) 1999 Sen Nagata <sen@eccosys.com>
5 ;; Author: Sen Nagata <sen@eccosys.com>
7 ;; Keywords: rfc 2368, mailto, mail
10 ;; This file is not a part of GNU Emacs.
16 ;; -repeat after me: "the colon is not part of the header name..."
17 ;; -if w3 becomes part of Emacs, then it may make sense to have this
18 ;; file depend on w3 -- the maintainer of w3 says merging w/ emacs
23 ;; this is intended as a replacement for mailto.el
27 ;; the functions that deal w/ unhexifying in this file were basically
28 ;; taken from w3 -- i hope to replace them w/ something else soon OR
29 ;; perhaps if w3 becomes a part of Emacs soon, use the functions from w3.
35 ;; added the constant rfc2368-version
36 ;; implemented first potential fix for a bug in rfc2368-mailto-regexp
37 ;; implemented first potential fix for a bug in rfc2368-parse-mailto
38 ;; (both bugs reported by Kenichi OKADA)
42 ;; started to use checkdoc
46 ;; initial implementation
49 (defconst rfc2368-version "rfc2368.el 0.3")
51 ;; only an approximation?
53 (defconst rfc2368-mailto-regexp
54 "^\\(mailto:\\)\\([^?]+\\)*\\(\\?\\(.*\\)\\)*"
55 "Regular expression to match and aid in parsing a mailto url.")
57 ;; describes 'mailto:'
58 (defconst rfc2368-mailto-scheme-index 1
59 "Describes the 'mailto:' portion of the url.")
60 ;; i'm going to call this part the 'prequery'
61 (defconst rfc2368-mailto-prequery-index 2
62 "Describes the portion of the url between 'mailto:' and '?'.")
63 ;; i'm going to call this part the 'query'
64 (defconst rfc2368-mailto-query-index 4
65 "Describes the portion of the url after '?'.")
67 ;; for dealing w/ unhexifying strings, my preferred approach is to use
68 ;; a 'string-replace-match-using-function' which can perform a
69 ;; string-replace-match and compute the replacement text based on a
70 ;; passed function -- however, emacs doesn't seem to have such a
73 ;; for the moment a rip-off of url-unhex (w3/url.el)
74 (defun rfc2368-unhexify-char (char)
75 "Unhexify CHAR -- e.g. %20 -> <SPC>."
82 ;; for the moment a rip-off of url-unhex-string (w3/url.el) (slightly modified)
83 (defun rfc2368-unhexify-string (string)
84 "Unhexify STRING -- e.g. 'hello%20there' -> 'hello there'."
85 (let ((case-fold-search t)
87 (while (string-match "%[0-9a-f][0-9a-f]" string)
88 (let* ((start (match-beginning 0))
90 (rfc2368-unhexify-char (elt string (+ start 1))))
91 (rfc2368-unhexify-char (elt string (+ start 2))))))
93 result (substring string 0 start)
94 (char-to-string hex-code))
95 string (substring string (match-end 0)))))
96 ;; it seems clearer to do things this way than to just return:
97 ;; (concat result string)
98 (setq result (concat result string))
101 (defun rfc2368-parse-mailto-url (mailto-url)
102 "Parse MAILTO-URL, and return an alist of header-name, header-value pairs.
103 MAILTO-URL should be a RFC 2368 (mailto) compliant url. A cons cell w/ a
104 key of 'Body' is a special case and is considered a header for this purpose.
105 The returned alist is intended for use w/ the `compose-mail' interface.
106 Note: make sure MAILTO-URL has been 'unhtmlized' (e.g. & -> &), before
107 calling this function."
108 (let ((case-fold-search t)
109 prequery query headers-alist)
111 (if (string-match rfc2368-mailto-regexp mailto-url)
115 (match-string rfc2368-mailto-prequery-index mailto-url))
118 (match-string rfc2368-mailto-query-index mailto-url))
120 ;; build alist of header name-value pairs
121 (if (not (null query))
125 (let* ((temp-list (split-string x "="))
126 (header-name (car temp-list))
127 (header-value (cadr temp-list)))
128 ;; return ("Header-Name" . "header-value")
130 (capitalize (rfc2368-unhexify-string header-name))
131 (rfc2368-unhexify-string header-value))))
132 (split-string query "&"))))
134 ;; deal w/ multiple 'To' recipients
137 (if (assoc "To" headers-alist)
138 (let* ((our-cons-cell
139 (assoc "To" headers-alist))
141 (cdr our-cons-cell)))
142 (setcdr our-cons-cell (concat our-cdr ", " prequery)))
144 (cons (cons "To" prequery) headers-alist)))))
148 (error "Failed to match a mailto: url"))
153 ;;; rfc2368.el ends here