mailheader.el 7.8 KB

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