Mercurial > emacs
comparison lisp/mail/rfc2368.el @ 28496:92a9591b21a2
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Mon, 03 Apr 2000 21:21:42 +0000 |
parents | |
children | ec4edee89622 |
comparison
equal
deleted
inserted
replaced
28495:6452df59183b | 28496:92a9591b21a2 |
---|---|
1 ;;; rfc2368.el --- support for rfc2368 | |
2 | |
3 ;;; Copyright 1999 Sen Nagata <sen@eccosys.com> | |
4 | |
5 ;; Author: Sen Nagata <sen_ml@eccosys.com> | |
6 ;; Keywords: mail | |
7 | |
8 ;; Copyright (C) 1998, 2000 Free Software Foundation, Inc. | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 ;; Boston, MA 02111-1307, USA. | |
26 | |
27 ;;; Commentary: | |
28 ;; | |
29 ;; notes: | |
30 ;; | |
31 ;; -repeat after me: "the colon is not part of the header name..." | |
32 ;; -if w3 becomes part of emacs, then it may make sense to have this | |
33 ;; file depend on w3 -- the maintainer of w3 says merging w/ Emacs | |
34 ;; is planned! | |
35 ;; | |
36 ;; historical note: | |
37 ;; | |
38 ;; this is intended as a replacement for mailto.el | |
39 ;; | |
40 ;; acknowledgements: | |
41 ;; | |
42 ;; the functions that deal w/ unhexifying in this file were basically | |
43 ;; taken from w3 -- i hope to replace them w/ something else soon OR | |
44 ;; perhaps if w3 becomes a part of emacs soon, use the functions from w3. | |
45 | |
46 ;;; History: | |
47 ;; | |
48 ;; 0.3: | |
49 ;; | |
50 ;; added the constant rfc2368-version | |
51 ;; implemented first potential fix for a bug in rfc2368-mailto-regexp | |
52 ;; implemented first potential fix for a bug in rfc2368-parse-mailto | |
53 ;; (both bugs reported by Kenichi OKADA) | |
54 ;; | |
55 ;; 0.2: | |
56 ;; | |
57 ;; started to use checkdoc | |
58 ;; | |
59 ;; 0.1: | |
60 ;; | |
61 ;; initial implementation | |
62 | |
63 ;;; Code: | |
64 | |
65 ;; only an approximation? | |
66 ;; see rfc 1738 | |
67 (defconst rfc2368-mailto-regexp | |
68 "^\\(mailto:\\)\\([^?]+\\)*\\(\\?\\(.*\\)\\)*" | |
69 "Regular expression to match and aid in parsing a mailto url.") | |
70 | |
71 ;; describes 'mailto:' | |
72 (defconst rfc2368-mailto-scheme-index 1 | |
73 "Describes the 'mailto:' portion of the url.") | |
74 ;; i'm going to call this part the 'prequery' | |
75 (defconst rfc2368-mailto-prequery-index 2 | |
76 "Describes the portion of the url between 'mailto:' and '?'.") | |
77 ;; i'm going to call this part the 'query' | |
78 (defconst rfc2368-mailto-query-index 4 | |
79 "Describes the portion of the url after '?'.") | |
80 | |
81 ;; for dealing w/ unhexifying strings, my preferred approach is to use | |
82 ;; a 'string-replace-match-using-function' which can perform a | |
83 ;; string-replace-match and compute the replacement text based on a | |
84 ;; passed function -- however, emacs doesn't seem to have such a | |
85 ;; function yet :-( | |
86 | |
87 ;; for the moment a rip-off of url-unhex (w3/url.el) | |
88 (defun rfc2368-unhexify-char (char) | |
89 "Unhexify CHAR -- e.g. %20 -> <SPC>." | |
90 (if (> char ?9) | |
91 (if (>= char ?a) | |
92 (+ 10 (- char ?a)) | |
93 (+ 10 (- char ?A))) | |
94 (- char ?0))) | |
95 | |
96 ;; for the moment a rip-off of url-unhex-string (w3/url.el) (slightly modified) | |
97 (defun rfc2368-unhexify-string (string) | |
98 "Unhexify STRING -- e.g. 'hello%20there' -> 'hello there'." | |
99 (let ((case-fold-search t) | |
100 (result "")) | |
101 (while (string-match "%[0-9a-f][0-9a-f]" string) | |
102 (let* ((start (match-beginning 0)) | |
103 (hex-code (+ (* 16 | |
104 (rfc2368-unhexify-char (elt string (+ start 1)))) | |
105 (rfc2368-unhexify-char (elt string (+ start 2)))))) | |
106 (setq result (concat | |
107 result (substring string 0 start) | |
108 (char-to-string hex-code)) | |
109 string (substring string (match-end 0))))) | |
110 ;; it seems clearer to do things this way than to just return: | |
111 ;; (concat result string) | |
112 (setq result (concat result string)) | |
113 result)) | |
114 | |
115 (defun rfc2368-parse-mailto-url (mailto-url) | |
116 "Parse MAILTO-URL, and return an alist of header-name, header-value pairs. | |
117 MAILTO-URL should be a RFC 2368 (mailto) compliant url. A cons cell w/ a | |
118 key of 'Body' is a special case and is considered a header for this purpose. | |
119 The returned alist is intended for use w/ the `compose-mail' interface. | |
120 Note: make sure MAILTO-URL has been 'unhtmlized' (e.g. & -> &), before | |
121 calling this function." | |
122 (let ((case-fold-search t) | |
123 prequery query headers-alist) | |
124 | |
125 (if (string-match rfc2368-mailto-regexp mailto-url) | |
126 (progn | |
127 | |
128 (setq prequery | |
129 (match-string rfc2368-mailto-prequery-index mailto-url)) | |
130 | |
131 (setq query | |
132 (match-string rfc2368-mailto-query-index mailto-url)) | |
133 | |
134 ;; build alist of header name-value pairs | |
135 (if (not (null query)) | |
136 (setq headers-alist | |
137 (mapcar | |
138 (lambda (x) | |
139 (let* ((temp-list (split-string x "=")) | |
140 (header-name (car temp-list)) | |
141 (header-value (cadr temp-list))) | |
142 ;; return ("Header-Name" . "header-value") | |
143 (cons | |
144 (capitalize (rfc2368-unhexify-string header-name)) | |
145 (rfc2368-unhexify-string header-value)))) | |
146 (split-string query "&")))) | |
147 | |
148 ;; deal w/ multiple 'To' recipients | |
149 (if prequery | |
150 (progn | |
151 (if (assoc "To" headers-alist) | |
152 (let* ((our-cons-cell | |
153 (assoc "To" headers-alist)) | |
154 (our-cdr | |
155 (cdr our-cons-cell))) | |
156 (setcdr our-cons-cell (concat our-cdr ", " prequery))) | |
157 (setq headers-alist | |
158 (cons (cons "To" prequery) headers-alist))))) | |
159 | |
160 headers-alist) | |
161 | |
162 (error "Failed to match a mailto: url")) | |
163 )) | |
164 | |
165 (provide 'rfc2368) | |
166 | |
167 ;;; rfc2368.el ends here |