annotate lisp/gnus/mailheader.el @ 136:b980b6286996 r20-2b2

Import from CVS: tag r20-2b2
author cvs
date Mon, 13 Aug 2007 09:31:12 +0200
parents 360340f9fd5f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; mail-header.el --- Mail header parsing, merging, formatting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1996 by Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Erik Naggum <erik@arcana.naggum.no>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Keywords: tools, mail, news
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; This package provides an abstraction to RFC822-style messages, used in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; mail news, and some other systems. The simple syntactic rules for such
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; headers, such as quoting and line folding, are routinely reimplemented
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; in many individual packages. This package removes the need for this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; redundancy by representing message headers as association lists,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; offering functions to extract the set of headers from a message, to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;; parse individual headers, to merge sets of headers, and to format a set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;; of headers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; The car of each element in the message-header alist is a symbol whose
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;; print name is the name of the header, in all lower-case. The cdr of an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; element depends on the operation. After extracting headers from a
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
39 ;; message, it is a string, the value of the header. An extracted set of
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; headers may be parsed further, which may turn it into a list, whose car
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; is the original value and whose subsequent elements depend on the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;; header. For formatting, it is evaluated to obtain the strings to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;; inserted. For merging, one set of headers consists of strings, while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;; the other set will be evaluated with the symbols in the first set of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; headers bound to their respective values.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (require 'cl)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; Make the byte-compiler shut up.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (defvar headers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (defun mail-header-extract ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 "Extract headers from current buffer after point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 Returns a header alist, where each element is a cons cell (name . value),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 where NAME is a symbol, and VALUE is the string value of the header having
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 that name."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (let ((message-headers ()) (top (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (while (and (setq start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (> (skip-chars-forward "^\0- :") 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (= (following-char) ?:)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (setq end (point))
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 98
diff changeset
65 (progn (forward-char)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (> (skip-chars-forward " \t") 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (let ((header (intern (downcase (buffer-substring start end))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (value (list (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (point) (progn (end-of-line) (point))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (while (progn (forward-char) (> (skip-chars-forward " \t") 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (push (buffer-substring (point) (progn (end-of-line) (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (push (if (cdr value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (cons header (mapconcat #'identity (nreverse value) " "))
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
75 (cons header (car value)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 message-headers)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (goto-char top)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (nreverse message-headers)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (defun mail-header-extract-no-properties ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 "Extract headers from current buffer after point, without properties.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 Returns a header alist, where each element is a cons cell (name . value),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 where NAME is a symbol, and VALUE is the string value of the header having
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 that name."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (lambda (elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (set-text-properties 0 (length (cdr elt)) nil (cdr elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (mail-header-extract)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (defun mail-header-parse (parsing-rules headers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 "Apply PARSING-RULES to HEADERS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 PARSING-RULES is an alist whose keys are header names (symbols) and whose
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 value is a parsing function. The function takes one argument, a string,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 and return a list of values, which will destructively replace the value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 associated with the key in HEADERS, after being prepended with the original
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 value."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (dolist (rule parsing-rules)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (let ((header (assq (car rule) headers)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (when header
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (if (consp (cdr header))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (setf (cddr header) (funcall (cdr rule) (cadr header)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (setf (cdr header)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (cons (cdr header) (funcall (cdr rule) (cdr header))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 headers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (defsubst mail-header (header &optional header-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 "Return the value associated with header HEADER in HEADER-ALIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 If the value is a string, it is the original value of the header. If the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 value is a list, its first element is the original value of the header,
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
111 with any subsequent elements being the result of parsing the value.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 If HEADER-ALIST is nil, the dynamically bound variable `headers' is used."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (cdr (assq header (or header-alist headers))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (defun mail-header-set (header value &optional header-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 "Set the value associated with header HEADER to VALUE in HEADER-ALIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 HEADER-ALIST defaults to the dynamically bound variable `headers' if nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 See `mail-header' for the semantics of VALUE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (let* ((alist (or header-alist headers))
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
120 (entry (assq header alist)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (if entry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (setf (cdr entry) value)
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
123 (nconc alist (list (cons header value)))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (defsetf mail-header (header &optional header-alist) (value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 `(mail-header-set ,header ,value ,header-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (defun mail-header-merge (merge-rules headers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 "Return a new header alist with MERGE-RULES applied to HEADERS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 MERGE-RULES is an alist whose keys are header names (symbols) and whose
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 values are forms to evaluate, the results of which are the new headers. It
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 should be a string or a list of string. The first element may be nil to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 denote that the formatting functions must use the remaining elements, or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 skip the header altogether if there are no other elements.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 The macro `mail-header' can be used to access headers in HEADERS."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (lambda (rule)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (cons (car rule) (eval (cdr rule))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 merge-rules))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (defvar mail-header-format-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (lambda (header value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 "Function to format headers without a specified formatting function."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (insert (capitalize (symbol-name header))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 ": "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (if (consp value) (car value) value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 "\n")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (defun mail-header-format (format-rules headers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 "Use FORMAT-RULES to format HEADERS and insert into current buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 FORMAT-RULES is an alist whose keys are header names (symbols), and whose
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 values are functions that format the header, the results of which are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 inserted, unless it is nil. The function takes two arguments, the header
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 symbol, and the value of that header. If the function itself is nil, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 default action is to insert the value of the header, unless it is nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 The headers are inserted in the order of the FORMAT-RULES.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 A key of t represents any otherwise unmentioned headers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 A key of nil has as its value a list of defaulted headers to ignore."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (let ((ignore (append (cdr (assq nil format-rules))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (mapcar #'car format-rules))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (dolist (rule format-rules)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (let* ((header (car rule))
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
164 (value (mail-header header)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (cond ((null header) 'ignore)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 ((eq header t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (dolist (defaulted headers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (unless (memq (car defaulted) ignore)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (let* ((header (car defaulted))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (value (cdr defaulted)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (if (cdr rule)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (funcall (cdr rule) header value)
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
173 (funcall mail-header-format-function header value))))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (if (cdr rule)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (funcall (cdr rule) header value)
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
177 (funcall mail-header-format-function header value))))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (insert "\n")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (provide 'mailheader)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ;;; mail-header.el ends here