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