4
|
1 ;;; std11.el --- STD 11 functions for GNU Emacs
|
|
2
|
70
|
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
|
4
|
4
|
|
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
|
|
6 ;; Keywords: mail, news, RFC 822, STD 11
|
70
|
7 ;; Version: $Id: std11.el,v 1.1.1.1 1996/12/18 22:43:39 steve Exp $
|
4
|
8
|
|
9 ;; This file is part of MU (Message Utilities).
|
|
10
|
|
11 ;; This program is free software; you can redistribute it and/or
|
|
12 ;; modify it under the terms of the GNU General Public License as
|
|
13 ;; published by the Free Software Foundation; either version 2, or (at
|
|
14 ;; your option) any later version.
|
|
15
|
|
16 ;; This program is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; 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., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Code:
|
|
27
|
|
28 (autoload 'buffer-substring-no-properties "emu")
|
|
29 (autoload 'member "emu")
|
|
30
|
|
31
|
|
32 ;;; @ field
|
|
33 ;;;
|
|
34
|
|
35 (defconst std11-field-name-regexp "[!-9;-~]+")
|
|
36 (defconst std11-field-head-regexp
|
|
37 (concat "^" std11-field-name-regexp ":"))
|
|
38 (defconst std11-next-field-head-regexp
|
|
39 (concat "\n" std11-field-name-regexp ":"))
|
|
40
|
|
41 (defun std11-field-end ()
|
|
42 "Move to end of field and return this point. [std11.el]"
|
|
43 (if (re-search-forward std11-next-field-head-regexp nil t)
|
|
44 (goto-char (match-beginning 0))
|
|
45 (if (re-search-forward "^$" nil t)
|
|
46 (goto-char (1- (match-beginning 0)))
|
|
47 (end-of-line)
|
|
48 ))
|
|
49 (point)
|
|
50 )
|
|
51
|
|
52 (defun std11-field-body (name &optional boundary)
|
|
53 "Return body of field NAME.
|
|
54 If BOUNDARY is not nil, it is used as message header separator.
|
|
55 \[std11.el]"
|
|
56 (save-excursion
|
|
57 (save-restriction
|
|
58 (std11-narrow-to-header boundary)
|
|
59 (goto-char (point-min))
|
|
60 (let ((case-fold-search t))
|
|
61 (if (re-search-forward (concat "^" name ":[ \t]*") nil t)
|
|
62 (buffer-substring-no-properties (match-end 0) (std11-field-end))
|
|
63 )))))
|
|
64
|
|
65 (defun std11-find-field-body (field-names &optional boundary)
|
|
66 "Return the first found field-body specified by FIELD-NAMES
|
|
67 of the message header in current buffer. If BOUNDARY is not nil, it is
|
|
68 used as message header separator. [std11.el]"
|
|
69 (save-excursion
|
|
70 (save-restriction
|
|
71 (std11-narrow-to-header boundary)
|
|
72 (let ((case-fold-search t)
|
|
73 field-name)
|
|
74 (catch 'tag
|
|
75 (while (setq field-name (car field-names))
|
|
76 (goto-char (point-min))
|
|
77 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
|
|
78 (throw 'tag
|
|
79 (buffer-substring-no-properties
|
|
80 (match-end 0) (std11-field-end)))
|
|
81 )
|
|
82 (setq field-names (cdr field-names))
|
|
83 ))))))
|
|
84
|
|
85 (defun std11-field-bodies (field-names &optional default-value boundary)
|
|
86 "Return list of each field-bodies of FIELD-NAMES of the message header
|
|
87 in current buffer. If BOUNDARY is not nil, it is used as message
|
|
88 header separator. [std11.el]"
|
|
89 (save-excursion
|
|
90 (save-restriction
|
|
91 (std11-narrow-to-header boundary)
|
|
92 (let* ((case-fold-search t)
|
|
93 (dest (make-list (length field-names) default-value))
|
|
94 (s-rest field-names)
|
|
95 (d-rest dest)
|
|
96 field-name)
|
|
97 (while (setq field-name (car s-rest))
|
|
98 (goto-char (point-min))
|
|
99 (if (re-search-forward (concat "^" field-name ":[ \t]*") nil t)
|
|
100 (setcar d-rest
|
|
101 (buffer-substring-no-properties
|
|
102 (match-end 0) (std11-field-end)))
|
|
103 )
|
|
104 (setq s-rest (cdr s-rest)
|
|
105 d-rest (cdr d-rest))
|
|
106 )
|
|
107 dest))))
|
|
108
|
|
109
|
|
110 ;;; @ unfolding
|
|
111 ;;;
|
|
112
|
|
113 (defun std11-unfold-string (string)
|
|
114 "Unfold STRING as message header field. [std11.el]"
|
|
115 (let ((dest ""))
|
70
|
116 (while (string-match "\n\\s +" string)
|
|
117 (setq dest (concat dest (substring string 0 (match-beginning 0)) " "))
|
4
|
118 (setq string (substring string (match-end 0)))
|
|
119 )
|
|
120 (concat dest string)
|
|
121 ))
|
|
122
|
|
123
|
|
124 ;;; @ header
|
|
125 ;;;
|
|
126
|
|
127 (defun std11-narrow-to-header (&optional boundary)
|
|
128 "Narrow to the message header.
|
|
129 If BOUNDARY is not nil, it is used as message header separator.
|
|
130 \[std11.el]"
|
|
131 (narrow-to-region
|
|
132 (goto-char (point-min))
|
|
133 (if (re-search-forward
|
|
134 (concat "^\\(" (regexp-quote (or boundary "")) "\\)?$")
|
|
135 nil t)
|
|
136 (match-beginning 0)
|
|
137 (point-max)
|
|
138 )))
|
|
139
|
|
140 (defun std11-header-string (regexp &optional boundary)
|
|
141 "Return string of message header fields matched by REGEXP.
|
|
142 If BOUNDARY is not nil, it is used as message header separator.
|
|
143 \[std11.el]"
|
|
144 (let ((case-fold-search t))
|
|
145 (save-excursion
|
|
146 (save-restriction
|
|
147 (std11-narrow-to-header boundary)
|
|
148 (goto-char (point-min))
|
|
149 (let (field header)
|
|
150 (while (re-search-forward std11-field-head-regexp nil t)
|
|
151 (setq field
|
|
152 (buffer-substring (match-beginning 0) (std11-field-end)))
|
|
153 (if (string-match regexp field)
|
|
154 (setq header (concat header field "\n"))
|
|
155 ))
|
|
156 header)
|
|
157 ))))
|
|
158
|
|
159 (defun std11-header-string-except (regexp &optional boundary)
|
|
160 "Return string of message header fields not matched by REGEXP.
|
|
161 If BOUNDARY is not nil, it is used as message header separator.
|
|
162 \[std11.el]"
|
|
163 (let ((case-fold-search t))
|
|
164 (save-excursion
|
|
165 (save-restriction
|
|
166 (std11-narrow-to-header boundary)
|
|
167 (goto-char (point-min))
|
|
168 (let (field header)
|
|
169 (while (re-search-forward std11-field-head-regexp nil t)
|
|
170 (setq field
|
|
171 (buffer-substring (match-beginning 0) (std11-field-end)))
|
|
172 (if (not (string-match regexp field))
|
|
173 (setq header (concat header field "\n"))
|
|
174 ))
|
|
175 header)
|
|
176 ))))
|
|
177
|
|
178 (defun std11-collect-field-names (&optional boundary)
|
|
179 "Return list of all field-names of the message header in current buffer.
|
|
180 If BOUNDARY is not nil, it is used as message header separator.
|
|
181 \[std11.el]"
|
|
182 (save-excursion
|
|
183 (save-restriction
|
|
184 (std11-narrow-to-header boundary)
|
|
185 (goto-char (point-min))
|
|
186 (let (dest name)
|
|
187 (while (re-search-forward std11-field-head-regexp nil t)
|
|
188 (setq name (buffer-substring-no-properties
|
|
189 (match-beginning 0)(1- (match-end 0))))
|
|
190 (or (member name dest)
|
|
191 (setq dest (cons name dest))
|
|
192 )
|
|
193 )
|
|
194 dest))))
|
|
195
|
|
196
|
|
197 ;;; @ quoted-string
|
|
198 ;;;
|
|
199
|
|
200 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
|
|
201
|
|
202 (defun std11-wrap-as-quoted-string (string)
|
|
203 "Wrap STRING as RFC 822 quoted-string. [std11.el]"
|
|
204 (concat "\""
|
70
|
205 (mapconcat (function
|
|
206 (lambda (chr)
|
|
207 (if (memq chr std11-non-qtext-char-list)
|
|
208 (concat "\\" (char-to-string chr))
|
|
209 (char-to-string chr)
|
|
210 )
|
|
211 )) string "")
|
4
|
212 "\""))
|
|
213
|
70
|
214 (defun std11-strip-quoted-pair (str)
|
|
215 (let ((dest "")
|
4
|
216 (i 0)
|
70
|
217 (len (length str))
|
|
218 chr flag)
|
4
|
219 (while (< i len)
|
70
|
220 (setq chr (aref str i))
|
|
221 (if (or flag (not (eq chr ?\\)))
|
|
222 (progn
|
|
223 (setq dest (concat dest (char-to-string chr)))
|
|
224 (setq flag nil)
|
|
225 )
|
|
226 (setq flag t)
|
|
227 )
|
|
228 (setq i (+ i 1))
|
|
229 )
|
|
230 dest))
|
4
|
231
|
|
232 (defun std11-strip-quoted-string (string)
|
|
233 "Strip quoted-string STRING. [std11.el]"
|
|
234 (let ((len (length string)))
|
|
235 (or (and (>= len 2)
|
|
236 (let ((max (1- len)))
|
|
237 (and (eq (aref string 0) ?\")
|
|
238 (eq (aref string max) ?\")
|
|
239 (std11-strip-quoted-pair (substring string 1 max))
|
|
240 )))
|
|
241 string)))
|
|
242
|
|
243
|
|
244 ;;; @ composer
|
|
245 ;;;
|
|
246
|
|
247 (defun std11-addr-to-string (seq)
|
|
248 "Return string from lexical analyzed list SEQ
|
|
249 represents addr-spec of RFC 822. [std11.el]"
|
|
250 (mapconcat (function
|
|
251 (lambda (token)
|
70
|
252 (if (let ((name (car token)))
|
|
253 (or (eq name 'spaces)
|
|
254 (eq name 'comment)
|
|
255 ))
|
|
256 ""
|
|
257 (cdr token)
|
|
258 )))
|
4
|
259 seq "")
|
|
260 )
|
|
261
|
|
262 (defun std11-address-string (address)
|
|
263 "Return string of address part from parsed ADDRESS of RFC 822.
|
|
264 \[std11.el]"
|
|
265 (cond ((eq (car address) 'group)
|
|
266 (mapconcat (function std11-address-string)
|
|
267 (car (cdr address))
|
|
268 ", ")
|
|
269 )
|
|
270 ((eq (car address) 'mailbox)
|
|
271 (let ((addr (nth 1 address)))
|
|
272 (std11-addr-to-string
|
|
273 (if (eq (car addr) 'phrase-route-addr)
|
|
274 (nth 2 addr)
|
|
275 (cdr addr)
|
|
276 )
|
|
277 )))))
|
|
278
|
|
279 (defun std11-full-name-string (address)
|
|
280 "Return string of full-name part from parsed ADDRESS of RFC 822.
|
|
281 \[std11.el]"
|
|
282 (cond ((eq (car address) 'group)
|
|
283 (mapconcat (function
|
|
284 (lambda (token)
|
|
285 (cdr token)
|
|
286 ))
|
|
287 (nth 1 address) "")
|
|
288 )
|
|
289 ((eq (car address) 'mailbox)
|
|
290 (let ((addr (nth 1 address))
|
|
291 (comment (nth 2 address))
|
|
292 phrase)
|
|
293 (if (eq (car addr) 'phrase-route-addr)
|
70
|
294 (setq phrase (mapconcat (function
|
|
295 (lambda (token)
|
|
296 (cdr token)
|
|
297 ))
|
|
298 (nth 1 addr) ""))
|
4
|
299 )
|
70
|
300 (or phrase comment)
|
4
|
301 ))))
|
|
302
|
|
303
|
|
304 ;;; @ parser
|
|
305 ;;;
|
|
306
|
|
307 (defun std11-parse-address-string (string)
|
|
308 "Parse STRING as mail address. [std11.el]"
|
|
309 (std11-parse-address (std11-lexical-analyze string))
|
|
310 )
|
|
311
|
|
312 (defun std11-parse-addresses-string (string)
|
|
313 "Parse STRING as mail address list. [std11.el]"
|
|
314 (std11-parse-addresses (std11-lexical-analyze string))
|
|
315 )
|
|
316
|
|
317 (defun std11-extract-address-components (string)
|
|
318 "Extract full name and canonical address from STRING.
|
|
319 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
|
|
320 If no name can be extracted, FULL-NAME will be nil. [std11.el]"
|
|
321 (let* ((structure (car (std11-parse-address-string
|
|
322 (std11-unfold-string string))))
|
|
323 (phrase (std11-full-name-string structure))
|
|
324 (address (std11-address-string structure))
|
|
325 )
|
|
326 (list phrase address)
|
|
327 ))
|
|
328
|
|
329 (provide 'std11)
|
|
330
|
|
331 (mapcar (function
|
|
332 (lambda (func)
|
|
333 (autoload func "std11-parse")
|
|
334 ))
|
|
335 '(std11-lexical-analyze
|
|
336 std11-parse-address std11-parse-addresses
|
|
337 std11-parse-address-string))
|
|
338
|
|
339
|
|
340 ;;; @ end
|
|
341 ;;;
|
|
342
|
|
343 ;;; std11.el ends here
|