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