1;;; rfc2368.el --- support for rfc2368
2
3;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4;;   2005, 2006, 2007 Free Software Foundation, Inc.
5
6;; Author: Sen Nagata <sen@eccosys.com>
7;; Keywords: mail
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING.  If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Commentary:
27;;
28;; notes:
29;;
30;;   -repeat after me: "the colon is not part of the header name..."
31;;   -if w3 becomes part of emacs, then it may make sense to have this
32;;    file depend on w3 -- the maintainer of w3 says merging w/ Emacs
33;;    is planned!
34;;
35;; historical note:
36;;
37;;   this is intended as a replacement for mailto.el
38;;
39;; acknowledgements:
40;;
41;;   the functions that deal w/ unhexifying in this file were basically
42;; taken from w3 -- i hope to replace them w/ something else soon OR
43;; perhaps if w3 becomes a part of emacs soon, use the functions from w3.
44
45;;; History:
46;;
47;; 0.3:
48;;
49;;  added the constant rfc2368-version
50;;  implemented first potential fix for a bug in rfc2368-mailto-regexp
51;;  implemented first potential fix for a bug in rfc2368-parse-mailto
52;;  (both bugs reported by Kenichi OKADA)
53;;
54;; 0.2:
55;;
56;;  started to use checkdoc
57;;
58;; 0.1:
59;;
60;;  initial implementation
61
62;;; Code:
63
64;; only an approximation?
65;; see rfc 1738
66(defconst rfc2368-mailto-regexp
67  "^\\(mailto:\\)\\([^?]+\\)*\\(\\?\\(.*\\)\\)*"
68  "Regular expression to match and aid in parsing a mailto url.")
69
70;; describes 'mailto:'
71(defconst rfc2368-mailto-scheme-index 1
72  "Describes the 'mailto:' portion of the url.")
73;; i'm going to call this part the 'prequery'
74(defconst rfc2368-mailto-prequery-index 2
75  "Describes the portion of the url between 'mailto:' and '?'.")
76;; i'm going to call this part the 'query'
77(defconst rfc2368-mailto-query-index 4
78  "Describes the portion of the url after '?'.")
79
80(defun rfc2368-unhexify-string (string)
81  "Unhexify STRING -- e.g. 'hello%20there' -> 'hello there'."
82  (replace-regexp-in-string "%[[:xdigit:]]\\{2\\}"
83			    (lambda (match)
84			      (string (string-to-number (substring match 1)
85							16)))
86			    string t t))
87
88(defun rfc2368-parse-mailto-url (mailto-url)
89  "Parse MAILTO-URL, and return an alist of header-name, header-value pairs.
90MAILTO-URL should be a RFC 2368 (mailto) compliant url.  A cons cell w/ a
91key of 'Body' is a special case and is considered a header for this purpose.
92The returned alist is intended for use w/ the `compose-mail' interface.
93Note: make sure MAILTO-URL has been 'unhtmlized' (e.g. &amp; -> &), before
94calling this function."
95  (let ((case-fold-search t)
96	prequery query headers-alist)
97
98    (if (string-match rfc2368-mailto-regexp mailto-url)
99	(progn
100
101	  (setq prequery
102		(match-string rfc2368-mailto-prequery-index mailto-url))
103
104	  (setq query
105		(match-string rfc2368-mailto-query-index mailto-url))
106
107	  ;; build alist of header name-value pairs
108	  (if (not (null query))
109	      (setq headers-alist
110		    (mapcar
111		     (lambda (x)
112		       (let* ((temp-list (split-string x "="))
113			      (header-name (car temp-list))
114			      (header-value (cadr temp-list)))
115			 ;; return ("Header-Name" . "header-value")
116			 (cons
117			  (capitalize (rfc2368-unhexify-string header-name))
118			  (rfc2368-unhexify-string header-value))))
119		     (split-string query "&"))))
120
121	  ;; deal w/ multiple 'To' recipients
122	  (if prequery
123	      (progn
124		(setq prequery (rfc2368-unhexify-string prequery))
125		(if (assoc "To" headers-alist)
126		    (let* ((our-cons-cell
127			    (assoc "To" headers-alist))
128			   (our-cdr
129			    (cdr our-cons-cell)))
130		      (setcdr our-cons-cell (concat prequery ", " our-cdr)))
131		  (setq headers-alist
132			(cons (cons "To" prequery) headers-alist)))))
133
134	  headers-alist)
135
136      (error "Failed to match a mailto: url"))
137    ))
138
139(provide 'rfc2368)
140
141;;; arch-tag: ea804934-ad96-4f69-957b-857a76e4fd95
142;;; rfc2368.el ends here
143