1 ;;; gnus-logic.el --- advanced scoring code for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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.
44 '(("number" 0 gnus-advanced-integer)
45 ("subject" 1 gnus-advanced-string)
46 ("from" 2 gnus-advanced-string)
47 ("date" 3 gnus-advanced-date)
48 ("message-id" 4 gnus-advanced-string)
49 ("references" 5 gnus-advanced-string)
50 ("chars" 6 gnus-advanced-integer)
51 ("lines" 7 gnus-advanced-integer)
52 ("xref" 8 gnus-advanced-string)
53 ("head" nil gnus-advanced-body)
54 ("body" nil gnus-advanced-body)
55 ("all" nil gnus-advanced-body)))
58 (autoload 'parse-time-string "parse-time"))
60 (defun gnus-score-advanced (rule &optional trace)
61 "Apply advanced scoring RULE to all the articles in the current group."
62 (let (new-score score multiple)
63 (dolist (gnus-advanced-headers gnus-newsgroup-headers)
64 (when (setq multiple (gnus-advanced-score-rule (car rule)))
65 (setq new-score (or (nth 1 rule)
66 gnus-score-interactive-default-score))
67 (when (numberp multiple)
68 (setq new-score (* multiple new-score)))
69 ;; This rule was successful, so we add the score to this
71 (if (setq score (assq (mail-header-number gnus-advanced-headers)
72 gnus-newsgroup-scored))
74 (+ (cdr score) new-score))
75 (push (cons (mail-header-number gnus-advanced-headers)
77 gnus-newsgroup-scored)
79 (push (cons "A file" rule)
80 gnus-score-trace)))))))
82 (defun gnus-advanced-score-rule (rule)
83 "Apply RULE to `gnus-advanced-headers'."
84 (let ((type (car rule)))
87 ((or (eq type '&) (eq type 'and))
90 t ; Empty rule is true.
92 (gnus-advanced-score-rule (car rule)))
94 ;; If all the rules were true, then `rule' should be nil.
97 ((or (eq type '|) (eq type 'or))
102 (not (gnus-advanced-score-rule (car rule))))
104 ;; If one of the rules returned true, then `rule' should be non-nil.
107 ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
108 (not (gnus-advanced-score-rule (nth 1 rule))))
109 ;; This is a `1-'-type redirection rule.
111 (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
112 (let ((gnus-advanced-headers
114 gnus-advanced-headers
115 (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
116 ;; 1- type redirection.
118 (substring (symbol-name type)
119 (match-beginning 1) (match-end 1)))
120 ;; ^^^ type redirection.
121 (length (symbol-name type))))))
122 (when gnus-advanced-headers
123 (gnus-advanced-score-rule (nth 1 rule)))))
124 ;; Plain scoring rule.
126 (gnus-advanced-score-article rule))
129 (error "Unknown advanced score type: %s" rule)))))
131 (defun gnus-advanced-score-article (rule)
132 ;; `rule' is a semi-normal score rule, so we find out what function
133 ;; that's supposed to do the actual processing.
134 (let* ((header (car rule))
135 (func (assoc (downcase header) gnus-advanced-index)))
137 (error "No such header: %s" rule)
138 ;; Call the score function.
139 (funcall (caddr func) (or (cadr func) header)
140 (cadr rule) (caddr rule)))))
142 (defun gnus-advanced-string (index match type)
143 "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
144 (let* ((type (or type 's))
145 (case-fold-search (not (eq (downcase (symbol-name type))
146 (symbol-name type))))
147 (header (or (aref gnus-advanced-headers index) "")))
149 ((memq type '(r R regexp Regexp))
150 (string-match match header))
151 ((memq type '(s S string String))
152 (string-match (regexp-quote match) header))
153 ((memq type '(e E exact Exact))
154 (string= match header))
155 ((memq type '(f F fuzzy Fuzzy))
156 (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
159 (error "No such string match type: %s" type)))))
161 (defun gnus-advanced-integer (index match type)
162 (if (not (memq type '(< > <= >= =)))
163 (error "No such integer score type: %s" type)
164 (funcall type (or (aref gnus-advanced-headers index) 0) match)))
166 (defun gnus-advanced-date (index match type)
167 (let ((date (apply 'encode-time (parse-time-string
168 (aref gnus-advanced-headers index))))
169 (match (apply 'encode-time (parse-time-string match))))
174 (time-less-p match date))
176 (time-less-p date match))
178 (error "No such date score type: %s" type)))))
180 (defun gnus-advanced-body (header match type)
181 (when (string= header "all")
182 (setq header "article"))
184 (set-buffer nntp-server-buffer)
185 (let* ((request-func (cond ((string= "head" header)
187 ((string= "body" header)
189 (t 'gnus-request-article)))
191 ;; Not all backends support partial fetching. In that case, we
192 ;; just fetch the entire article.
193 (unless (gnus-check-backend-function
194 (intern (concat "request-" header))
196 (setq ofunc request-func)
197 (setq request-func 'gnus-request-article))
198 (setq article (mail-header-number gnus-advanced-headers))
199 (gnus-message 7 "Scoring article %s..." article)
200 (when (funcall request-func article gnus-newsgroup-name)
201 (goto-char (point-min))
202 ;; If just parts of the article is to be searched and the
203 ;; backend didn't support partial fetching, we just narrow to
204 ;; the relevant parts.
206 (if (eq ofunc 'gnus-request-head)
209 (or (search-forward "\n\n" nil t) (point-max)))
211 (or (search-forward "\n\n" nil t) (point))
213 (let* ((case-fold-search (not (eq (downcase (symbol-name type))
214 (symbol-name type))))
216 (cond ((memq type '(r R regexp Regexp))
218 ((memq type '(s S string String))
221 (error "Invalid match type: %s" type)))))
222 (goto-char (point-min))
224 (funcall search-func match nil t)
227 (provide 'gnus-logic)
229 ;;; gnus-logic.el ends here