1 ;;; gnus-logic.el --- advanced scoring code for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
35 ;;; Internal variables.
37 (defvar gnus-advanced-headers nil)
39 ;; To avoid having 8-bit characters in the source file.
40 (defvar gnus-advanced-not (intern (format "%c" 172)))
42 (defconst gnus-advanced-index
43 ;; Name to index alist.
45 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
47 gnus-advanced-integer)
49 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
53 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
57 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
61 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
65 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
69 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
71 gnus-advanced-integer)
73 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
75 gnus-advanced-integer)
77 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
80 ("head" nil gnus-advanced-body)
81 ("body" nil gnus-advanced-body)
82 ("all" nil gnus-advanced-body)))
85 (autoload 'parse-time-string "parse-time"))
87 (defun gnus-score-advanced (rule &optional trace)
88 "Apply advanced scoring RULE to all the articles in the current group."
89 (let ((headers gnus-newsgroup-headers)
90 gnus-advanced-headers score)
91 (while (setq gnus-advanced-headers (pop headers))
92 (when (gnus-advanced-score-rule (car rule))
93 ;; This rule was successful, so we add the score to
95 (if (setq score (assq (mail-header-number gnus-advanced-headers)
96 gnus-newsgroup-scored))
100 gnus-score-interactive-default-score)))
101 (push (cons (mail-header-number gnus-advanced-headers)
103 gnus-score-interactive-default-score))
104 gnus-newsgroup-scored)
106 (push (cons "A file" rule)
107 gnus-score-trace)))))))
109 (defun gnus-advanced-score-rule (rule)
110 "Apply RULE to `gnus-advanced-headers'."
111 (let ((type (car rule)))
114 ((or (eq type '&) (eq type 'and))
117 t ; Empty rule is true.
119 (gnus-advanced-score-rule (car rule)))
121 ;; If all the rules were true, then `rule' should be nil.
124 ((or (eq type '|) (eq type 'or))
129 (not (gnus-advanced-score-rule (car rule))))
131 ;; If one of the rules returned true, then `rule' should be non-nil.
134 ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
135 (not (gnus-advanced-score-rule (nth 1 rule))))
136 ;; This is a `1-'-type redirection rule.
138 (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
139 (let ((gnus-advanced-headers
141 gnus-advanced-headers
142 (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
143 ;; 1- type redirection.
145 (substring (symbol-name type)
146 (match-beginning 0) (match-end 0)))
147 ;; ^^^ type redirection.
148 (length (symbol-name type))))))
149 (when gnus-advanced-headers
150 (gnus-advanced-score-rule (nth 1 rule)))))
151 ;; Plain scoring rule.
153 (gnus-advanced-score-article rule))
156 (error "Unknown advanced score type: %s" rule)))))
158 (defun gnus-advanced-score-article (rule)
159 ;; `rule' is a semi-normal score rule, so we find out
160 ;; what function that's supposed to do the actual
162 (let* ((header (car rule))
163 (func (assoc (downcase header) gnus-advanced-index)))
165 (error "No such header: %s" rule)
166 ;; Call the score function.
167 (funcall (caddr func) (or (cadr func) header)
168 (cadr rule) (caddr rule)))))
170 (defun gnus-advanced-string (index match type)
171 "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
172 (let* ((type (or type 's))
173 (case-fold-search (not (eq (downcase (symbol-name type))
174 (symbol-name type))))
175 (header (aref gnus-advanced-headers index)))
177 ((memq type '(r R regexp Regexp))
178 (string-match match header))
179 ((memq type '(s S string String))
180 (string-match (regexp-quote match) header))
181 ((memq type '(e E exact Exact))
182 (string= match header))
183 ((memq type '(f F fuzzy Fuzzy))
184 (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
187 (error "No such string match type: %s" type)))))
189 (defun gnus-advanced-integer (index match type)
190 (if (not (memq type '(< > <= >= =)))
191 (error "No such integer score type: %s" type)
192 (funcall type match (or (aref gnus-advanced-headers index) 0))))
194 (defun gnus-advanced-date (index match type)
195 (let ((date (apply 'encode-time (parse-time-string
196 (aref gnus-advanced-headers index))))
197 (match (apply 'encode-time (parse-time-string match))))
202 (time-less-p match date))
204 (time-less-p date match))
206 (error "No such date score type: %s" type)))))
208 (defun gnus-advanced-body (header match type)
209 (when (string= header "all")
210 (setq header "article"))
212 (set-buffer nntp-server-buffer)
213 (let* ((request-func (cond ((string= "head" header)
215 ((string= "body" header)
217 (t 'gnus-request-article)))
219 ;; Not all backends support partial fetching. In that case,
220 ;; we just fetch the entire article.
221 (unless (gnus-check-backend-function
222 (intern (concat "request-" header))
224 (setq ofunc request-func)
225 (setq request-func 'gnus-request-article))
226 (setq article (mail-header-number gnus-advanced-headers))
227 (gnus-message 7 "Scoring article %s..." article)
228 (when (funcall request-func article gnus-newsgroup-name)
229 (goto-char (point-min))
230 ;; If just parts of the article is to be searched and the
231 ;; backend didn't support partial fetching, we just narrow
232 ;; to the relevant parts.
234 (if (eq ofunc 'gnus-request-head)
237 (or (search-forward "\n\n" nil t) (point-max)))
239 (or (search-forward "\n\n" nil t) (point))
241 (let* ((case-fold-search (not (eq (downcase (symbol-name type))
242 (symbol-name type))))
244 (cond ((memq type '(r R regexp Regexp))
246 ((memq type '(s S string String))
249 (error "Invalid match type: %s" type)))))
250 (goto-char (point-min))
252 (funcall search-func match nil t)
255 (provide 'gnus-logic)
257 ;;; gnus-logic.el ends here.