1 ;;; gnus-logic.el --- advanced scoring code for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
33 ;;; Internal variables.
35 (defvar gnus-advanced-headers nil)
37 ;; To avoid having 8-bit characters in the source file.
38 (defvar gnus-advanced-not (intern (format "%c" 172)))
40 (defconst gnus-advanced-index
41 ;; Name to index alist.
42 '(("number" 0 gnus-advanced-integer)
43 ("subject" 1 gnus-advanced-string)
44 ("from" 2 gnus-advanced-string)
45 ("date" 3 gnus-advanced-date)
46 ("message-id" 4 gnus-advanced-string)
47 ("references" 5 gnus-advanced-string)
48 ("chars" 6 gnus-advanced-integer)
49 ("lines" 7 gnus-advanced-integer)
50 ("xref" 8 gnus-advanced-string)
51 ("head" nil gnus-advanced-body)
52 ("body" nil gnus-advanced-body)
53 ("all" nil gnus-advanced-body)))
56 (autoload 'parse-time-string "parse-time"))
58 (defun gnus-score-advanced (rule &optional trace)
59 "Apply advanced scoring RULE to all the articles in the current group."
60 (let ((headers gnus-newsgroup-headers)
61 gnus-advanced-headers score)
62 (while (setq gnus-advanced-headers (pop headers))
63 (when (gnus-advanced-score-rule (car rule))
64 ;; This rule was successful, so we add the score to
66 (if (setq score (assq (mail-header-number gnus-advanced-headers)
67 gnus-newsgroup-scored))
71 gnus-score-interactive-default-score)))
72 (push (cons (mail-header-number gnus-advanced-headers)
74 gnus-score-interactive-default-score))
75 gnus-newsgroup-scored)
77 (push (cons "A file" rule)
78 gnus-score-trace)))))))
80 (defun gnus-advanced-score-rule (rule)
81 "Apply RULE to `gnus-advanced-headers'."
82 (let ((type (car rule)))
85 ((or (eq type '&) (eq type 'and))
88 t ; Empty rule is true.
90 (gnus-advanced-score-rule (car rule)))
92 ;; If all the rules were true, then `rule' should be nil.
95 ((or (eq type '|) (eq type 'or))
100 (not (gnus-advanced-score-rule (car rule))))
102 ;; If one of the rules returned true, then `rule' should be non-nil.
105 ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
106 (not (gnus-advanced-score-rule (nth 1 rule))))
107 ;; This is a `1-'-type redirection rule.
109 (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
110 (let ((gnus-advanced-headers
112 gnus-advanced-headers
113 (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
114 ;; 1- type redirection.
116 (substring (symbol-name type)
117 (match-beginning 0) (match-end 0)))
118 ;; ^^^ type redirection.
119 (length (symbol-name type))))))
120 (when gnus-advanced-headers
121 (gnus-advanced-score-rule (nth 1 rule)))))
122 ;; Plain scoring rule.
124 (gnus-advanced-score-article rule))
127 (error "Unknown advanced score type: %s" rule)))))
129 (defun gnus-advanced-score-article (rule)
130 ;; `rule' is a semi-normal score rule, so we find out
131 ;; what function that's supposed to do the actual
133 (let* ((header (car rule))
134 (func (assoc (downcase header) gnus-advanced-index)))
136 (error "No such header: %s" rule)
137 ;; Call the score function.
138 (funcall (caddr func) (or (cadr func) header)
139 (cadr rule) (caddr rule)))))
141 (defun gnus-advanced-string (index match type)
142 "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
143 (let* ((type (or type 's))
144 (case-fold-search (not (eq (downcase (symbol-name type))
145 (symbol-name type))))
146 (header (aref gnus-advanced-headers index)))
148 ((memq type '(r R regexp Regexp))
149 (string-match match header))
150 ((memq type '(s S string String))
151 (string-match (regexp-quote match) header))
152 ((memq type '(e E exact Exact))
153 (string= match header))
154 ((memq type '(f F fuzzy Fuzzy))
155 (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
158 (error "No such string match type: %s" type)))))
160 (defun gnus-advanced-integer (index match type)
161 (if (not (memq type '(< > <= >= =)))
162 (error "No such integer score type: %s" type)
163 (funcall type match (or (aref gnus-advanced-headers index) 0))))
165 (defun gnus-advanced-date (index match type)
166 (let ((date (apply 'encode-time (parse-time-string
167 (aref gnus-advanced-headers index))))
168 (match (apply 'encode-time (parse-time-string match))))
173 (time-less-p match date))
175 (time-less-p date match))
177 (error "No such date score type: %s" type)))))
179 (defun gnus-advanced-body (header match type)
180 (when (string= header "all")
181 (setq header "article"))
183 (set-buffer nntp-server-buffer)
184 (let* ((request-func (cond ((string= "head" header)
186 ((string= "body" header)
188 (t 'gnus-request-article)))
190 ;; Not all backends support partial fetching. In that case,
191 ;; we just fetch the entire article.
192 (unless (gnus-check-backend-function
193 (intern (concat "request-" header))
195 (setq ofunc request-func)
196 (setq request-func 'gnus-request-article))
197 (setq article (mail-header-number gnus-advanced-headers))
198 (gnus-message 7 "Scoring article %s..." article)
199 (when (funcall request-func article gnus-newsgroup-name)
200 (goto-char (point-min))
201 ;; If just parts of the article is to be searched and the
202 ;; backend didn't support partial fetching, we just narrow
203 ;; to the relevant parts.
205 (if (eq ofunc 'gnus-request-head)
208 (or (search-forward "\n\n" nil t) (point-max)))
210 (or (search-forward "\n\n" nil t) (point))
212 (let* ((case-fold-search (not (eq (downcase (symbol-name type))
213 (symbol-name type))))
215 (cond ((memq type '(r R regexp Regexp))
217 ((memq type '(s S string String))
220 (error "Invalid match type: %s" type)))))
221 (goto-char (point-min))
223 (funcall search-func match nil t)
226 (provide 'gnus-logic)
228 ;;; gnus-logic.el ends here.