gnus-mlspl.el 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ;;; gnus-mlspl.el --- a group params-based mail splitting mechanism
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Alexandre Oliva <oliva@lsd.ic.unicamp.br>
  4. ;; Keywords: news, mail
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'gnus)
  20. (require 'gnus-sum)
  21. (require 'gnus-group)
  22. (require 'nnmail)
  23. (defvar gnus-group-split-updated-hook nil
  24. "Hook called just after `nnmail-split-fancy' is updated by
  25. `gnus-group-split-update'.")
  26. (defvar gnus-group-split-default-catch-all-group "mail.misc"
  27. "Group name (or arbitrary fancy split) with default splitting rules.
  28. Used by `gnus-group-split' and `gnus-group-split-update' as a fallback
  29. split, in case none of the group-based splits matches.")
  30. ;;;###autoload
  31. (defun gnus-group-split-setup (&optional auto-update catch-all)
  32. "Set up the split for `nnmail-split-fancy'.
  33. Sets things up so that nnmail-split-fancy is used for mail
  34. splitting, and defines the variable nnmail-split-fancy according with
  35. group parameters.
  36. If AUTO-UPDATE is non-nil (prefix argument accepted, if called
  37. interactively), it makes sure nnmail-split-fancy is re-computed before
  38. getting new mail, by adding `gnus-group-split-update' to
  39. `nnmail-pre-get-new-mail-hook'.
  40. A non-nil CATCH-ALL replaces the current value of
  41. `gnus-group-split-default-catch-all-group'. This variable is only used
  42. by gnus-group-split-update, and only when its CATCH-ALL argument is
  43. nil. This argument may contain any fancy split, that will be added as
  44. the last split in a `|' split produced by `gnus-group-split-fancy',
  45. unless overridden by any group marked as a catch-all group. Typical
  46. uses are as simple as the name of a default mail group, but more
  47. elaborate fancy splits may also be useful to split mail that doesn't
  48. match any of the group-specified splitting rules. See
  49. `gnus-group-split-fancy' for details."
  50. (interactive "P")
  51. (setq nnmail-split-methods 'nnmail-split-fancy)
  52. (when catch-all
  53. (setq gnus-group-split-default-catch-all-group catch-all))
  54. (gnus-group-split-update)
  55. (when auto-update
  56. (add-hook 'nnmail-pre-get-new-mail-hook 'gnus-group-split-update)))
  57. ;;;###autoload
  58. (defun gnus-group-split-update (&optional catch-all)
  59. "Computes nnmail-split-fancy from group params and CATCH-ALL.
  60. It does this by calling by calling (gnus-group-split-fancy nil
  61. nil CATCH-ALL).
  62. If CATCH-ALL is nil, `gnus-group-split-default-catch-all-group' is used
  63. instead. This variable is set by `gnus-group-split-setup'."
  64. (interactive)
  65. (setq nnmail-split-fancy
  66. (gnus-group-split-fancy
  67. nil (null nnmail-crosspost)
  68. (or catch-all gnus-group-split-default-catch-all-group)))
  69. (run-hooks 'gnus-group-split-updated-hook))
  70. ;;;###autoload
  71. (defun gnus-group-split ()
  72. "Use information from group parameters in order to split mail.
  73. See `gnus-group-split-fancy' for more information.
  74. `gnus-group-split' is a valid value for `nnmail-split-methods'."
  75. (let (nnmail-split-fancy)
  76. (gnus-group-split-update)
  77. (nnmail-split-fancy)))
  78. ;;;###autoload
  79. (defun gnus-group-split-fancy
  80. (&optional groups no-crosspost catch-all)
  81. "Uses information from group parameters in order to split mail.
  82. It can be embedded into `nnmail-split-fancy' lists with the SPLIT
  83. \(: gnus-group-split-fancy GROUPS NO-CROSSPOST CATCH-ALL\)
  84. GROUPS may be a regular expression or a list of group names, that will
  85. be used to select candidate groups. If it is omitted or nil, all
  86. existing groups are considered.
  87. if NO-CROSSPOST is omitted or nil, a & split will be returned,
  88. otherwise, a | split, that does not allow crossposting, will be
  89. returned.
  90. For each selected group, a SPLIT is composed like this: if SPLIT-SPEC
  91. is specified, this split is returned as-is (unless it is nil: in this
  92. case, the group is ignored). Otherwise, if TO-ADDRESS, TO-LIST and/or
  93. EXTRA-ALIASES are specified, a regexp that matches any of them is
  94. constructed (extra-aliases may be a list). Additionally, if
  95. SPLIT-REGEXP is specified, the regexp will be extended so that it
  96. matches this regexp too, and if SPLIT-EXCLUDE is specified, RESTRICT
  97. clauses will be generated.
  98. If CATCH-ALL is nil, no catch-all handling is performed, regardless of
  99. catch-all marks in group parameters. Otherwise, if there is no
  100. selected group whose SPLIT-REGEXP matches the empty string, nor is
  101. there a selected group whose SPLIT-SPEC is 'catch-all, this fancy
  102. split (say, a group name) will be appended to the returned SPLIT list,
  103. as the last element of a '| SPLIT.
  104. For example, given the following group parameters:
  105. nnml:mail.bar:
  106. \((to-address . \"bar@femail.com\")
  107. (split-regexp . \".*@femail\\\\.com\"))
  108. nnml:mail.foo:
  109. \((to-list . \"foo@nowhere.gov\")
  110. (extra-aliases \"foo@localhost\" \"foo-redist@home\")
  111. (split-exclude \"bugs-foo\" \"rambling-foo\")
  112. (admin-address . \"foo-request@nowhere.gov\"))
  113. nnml:mail.others:
  114. \((split-spec . catch-all))
  115. Calling (gnus-group-split-fancy nil nil \"mail.others\") returns:
  116. \(| (& (any \"\\\\(bar@femail\\\\.com\\\\|.*@femail\\\\.com\\\\)\"
  117. \"mail.bar\")
  118. (any \"\\\\(foo@nowhere\\\\.gov\\\\|foo@localhost\\\\|foo-redist@home\\\\)\"
  119. - \"bugs-foo\" - \"rambling-foo\" \"mail.foo\"))
  120. \"mail.others\")"
  121. (let* ((newsrc (cdr gnus-newsrc-alist))
  122. split)
  123. (dolist (info newsrc)
  124. (let ((group (gnus-info-group info))
  125. (params (gnus-info-params info)))
  126. ;; For all GROUPs that match the specified GROUPS
  127. (when (or (not groups)
  128. (and (listp groups)
  129. (memq group groups))
  130. (and (stringp groups)
  131. (string-match groups group)))
  132. (let ((split-spec (assoc 'split-spec params)) group-clean)
  133. ;; Remove backend from group name
  134. (setq group-clean (string-match ":" group))
  135. (setq group-clean
  136. (if group-clean
  137. (substring group (1+ group-clean))
  138. group))
  139. (if split-spec
  140. (when (setq split-spec (cdr split-spec))
  141. (if (eq split-spec 'catch-all)
  142. ;; Emit catch-all only when requested
  143. (when catch-all
  144. (setq catch-all group-clean))
  145. ;; Append split-spec to the main split
  146. (push split-spec split)))
  147. ;; Let's deduce split-spec from other params
  148. (let ((to-address (cdr (assoc 'to-address params)))
  149. (to-list (cdr (assoc 'to-list params)))
  150. (extra-aliases (cdr (assoc 'extra-aliases params)))
  151. (split-regexp (cdr (assoc 'split-regexp params)))
  152. (split-exclude (cdr (assoc 'split-exclude params))))
  153. (when (or to-address to-list extra-aliases split-regexp)
  154. ;; regexp-quote to-address, to-list and extra-aliases
  155. ;; and add them all to split-regexp
  156. (setq split-regexp
  157. (concat
  158. "\\("
  159. (mapconcat
  160. 'identity
  161. (append
  162. (and to-address (list (regexp-quote to-address)))
  163. (and to-list (list (regexp-quote to-list)))
  164. (and extra-aliases
  165. (if (listp extra-aliases)
  166. (mapcar 'regexp-quote extra-aliases)
  167. (list extra-aliases)))
  168. (and split-regexp (list split-regexp)))
  169. "\\|")
  170. "\\)"))
  171. ;; Now create the new SPLIT
  172. (push (append
  173. (list 'any split-regexp)
  174. ;; Generate RESTRICTs for SPLIT-EXCLUDEs.
  175. (if (listp split-exclude)
  176. (apply #'append
  177. (mapcar (lambda (arg) (list '- arg))
  178. split-exclude))
  179. (list '- split-exclude))
  180. (list group-clean))
  181. split)
  182. ;; If it matches the empty string, it is a catch-all
  183. (when (string-match split-regexp "")
  184. (setq catch-all nil)))))))))
  185. ;; Add catch-all if not crossposting
  186. (if (and catch-all no-crosspost)
  187. (push catch-all split))
  188. ;; Move it to the tail, while arranging that SPLITs appear in the
  189. ;; same order as groups.
  190. (setq split (reverse split))
  191. ;; Decide whether to accept cross-postings or not.
  192. (push (if no-crosspost '| '&) split)
  193. ;; Even if we can cross-post, catch-all should not get
  194. ;; cross-posts.
  195. (if (and catch-all (not no-crosspost))
  196. (setq split (list '| split catch-all)))
  197. split))
  198. (provide 'gnus-mlspl)
  199. ;;; gnus-mlspl.el ends here