mail-utils.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;; Utility functions used both by rmail and rnews
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (provide 'mail-utils)
  18. ;; should be in loaddefs
  19. (defvar mail-use-rfc822 nil
  20. "*If non-nil, use a full, hairy RFC822 parser on mail addresses.
  21. Otherwise, (the default) use a smaller, somewhat faster and
  22. often-correct parser.")
  23. (defun mail-string-delete (string start end)
  24. "Returns a string containing all of STRING except the part
  25. from START (inclusive) to END (exclusive)."
  26. (if (null end) (substring string 0 start)
  27. (concat (substring string 0 start)
  28. (substring string end nil))))
  29. (defun mail-strip-quoted-names (address)
  30. "Delete comments and quoted strings in an address list ADDRESS.
  31. Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
  32. Return a modified address list."
  33. (if mail-use-rfc822
  34. (progn (require 'rfc822)
  35. (mapconcat 'identity (rfc822-addresses address) ", "))
  36. (let (pos)
  37. (string-match "\\`[ \t\n]*" address)
  38. ;; strip surrounding whitespace
  39. (setq address (substring address
  40. (match-end 0)
  41. (string-match "[ \t\n]*\\'" address
  42. (match-end 0))))
  43. ;; strip rfc822 comments
  44. (while (setq pos (string-match
  45. ;; This doesn't hack rfc822 nested comments
  46. ;; `(xyzzy (foo) whinge)' properly. Big deal.
  47. "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*)"
  48. address))
  49. (setq address
  50. (mail-string-delete address
  51. pos (match-end 0))))
  52. ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
  53. (setq pos 0)
  54. (while (setq pos (string-match
  55. "[ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*"
  56. address pos))
  57. ;; If the next thing is "@", we have "foo bar"@host. Leave it.
  58. (if (and (> (length address) (match-end 0))
  59. (= (aref address (match-end 0)) ?@))
  60. (setq pos (match-end 0))
  61. (setq address
  62. (mail-string-delete address
  63. pos (match-end 0)))))
  64. ;; Retain only part of address in <> delims, if there is such a thing.
  65. (while (setq pos (string-match "\\(,\\|\\`\\)[^,]*<\\([^>,]*>\\)"
  66. address))
  67. (let ((junk-beg (match-end 1))
  68. (junk-end (match-beginning 2))
  69. (close (match-end 0)))
  70. (setq address (mail-string-delete address (1- close) close))
  71. (setq address (mail-string-delete address junk-beg junk-end))))
  72. address)))
  73. ; rmail-dont-reply-to-names is defined in loaddefs
  74. (defun rmail-dont-reply-to (userids)
  75. "Returns string of mail addresses USERIDS sans any recipients
  76. that start with matches for rmail-dont-reply-to-names.
  77. Usenet paths ending in an element that matches are removed also."
  78. (if (null rmail-dont-reply-to-names)
  79. (setq rmail-dont-reply-to-names
  80. (concat "info-\\|"
  81. (regexp-quote (or (getenv "USER")
  82. (getenv "LOGNAME")))
  83. "\\>")))
  84. (let ((match (concat "\\(^\\|,\\)[ \t\n]*\\([^,\n]*!\\|\\)\\("
  85. rmail-dont-reply-to-names
  86. "\\)"))
  87. (case-fold-search t)
  88. pos epos)
  89. (while (setq pos (string-match match userids))
  90. (if (> pos 0) (setq pos (1+ pos)))
  91. (setq epos
  92. (if (string-match "[ \t\n,]+" userids (match-end 0))
  93. (match-end 0)
  94. (length userids)))
  95. (setq userids
  96. (mail-string-delete
  97. userids pos epos)))
  98. ;; get rid of any trailing commas
  99. (if (setq pos (string-match "[ ,\t\n]*\\'" userids))
  100. (setq userids (substring userids 0 pos)))
  101. ;; remove leading spaces. they bother me.
  102. (if (string-match "\\s *" userids)
  103. (substring userids (match-end 0))
  104. userids)))
  105. (defun mail-fetch-field (field-name &optional last all)
  106. "Return the value of the header field FIELD.
  107. The buffer is expected to be narrowed to just the headers of the message.
  108. If 2nd arg LAST is non-nil, use the last such field if there are several.
  109. If 3rd arg ALL is non-nil, concatenate all such fields, with commas between."
  110. (save-excursion
  111. (goto-char (point-min))
  112. (let ((case-fold-search t)
  113. (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
  114. (goto-char (point-min))
  115. (if all
  116. (let ((value ""))
  117. (while (re-search-forward name nil t)
  118. (let ((opoint (point)))
  119. (while (progn (forward-line 1)
  120. (looking-at "[ \t]")))
  121. (setq value (concat value
  122. (if (string= value "") "" ", ")
  123. (buffer-substring opoint (1- (point)))))))
  124. (and (not (string= value "")) value))
  125. (if (re-search-forward name nil t)
  126. (progn
  127. (if last (while (re-search-forward name nil t)))
  128. (let ((opoint (point)))
  129. (while (progn (forward-line 1)
  130. (looking-at "[ \t]")))
  131. (buffer-substring opoint (1- (point))))))))))
  132. ;; Parse a list of tokens separated by commas.
  133. ;; It runs from point to the end of the visible part of the buffer.
  134. ;; Whitespace before or after tokens is ignored,
  135. ;; but whitespace within tokens is kept.
  136. (defun mail-parse-comma-list ()
  137. (let (accumulated
  138. beg)
  139. (skip-chars-forward " ")
  140. (while (not (eobp))
  141. (setq beg (point))
  142. (skip-chars-forward "^,")
  143. (skip-chars-backward " ")
  144. (setq accumulated
  145. (cons (buffer-substring beg (point))
  146. accumulated))
  147. (skip-chars-forward "^,")
  148. (skip-chars-forward ", "))
  149. accumulated))
  150. (defun mail-comma-list-regexp (labels)
  151. (let (pos)
  152. (setq pos (or (string-match "[^ \t]" labels) 0))
  153. ;; Remove leading and trailing whitespace.
  154. (setq labels (substring labels pos (string-match "[ \t]*$" labels pos)))
  155. ;; Change each comma to \|, and flush surrounding whitespace.
  156. (while (setq pos (string-match "[ \t]*,[ \t]*" labels))
  157. (setq labels
  158. (concat (substring labels 0 pos)
  159. "\\|"
  160. (substring labels (match-end 0))))))
  161. labels)