PageRenderTime 136ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/gnus/gnus-mlspl.el

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