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.
45 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'location)
46 gnus-advanced-integer)
48 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'subject)
51 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'from)
54 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'date)
57 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'id)
60 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'references)
63 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'chars)
64 gnus-advanced-integer)
66 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'lines)
67 gnus-advanced-integer)
69 ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'xref)
71 ("head" nil gnus-advanced-body)
72 ("body" nil gnus-advanced-body)
73 ("all" nil gnus-advanced-body)))
76 (autoload 'parse-time-string "parse-time"))
78 (defun gnus-score-advanced (rule &optional trace)
79 "Apply advanced scoring RULE to all the articles in the current group."
80 (let (new-score score multiple)
81 (dolist (gnus-advanced-headers gnus-newsgroup-headers)
82 (when (setq multiple (gnus-advanced-score-rule (car rule)))
83 (setq new-score (or (nth 1 rule)
84 gnus-score-interactive-default-score))
85 (when (numberp multiple)
86 (setq new-score (* multiple new-score)))
87 ;; This rule was successful, so we add the score to this
89 (if (setq score (assq (mail-header-number gnus-advanced-headers)
90 gnus-newsgroup-scored))
92 (+ (cdr score) new-score))
93 (push (cons (mail-header-number gnus-advanced-headers)
95 gnus-newsgroup-scored)
97 (push (cons "A file" rule)
98 ;; Must be synced with `gnus-score-edit-file-at-point'.
99 gnus-score-trace)))))))
101 (defun gnus-advanced-score-rule (rule)
102 "Apply RULE to `gnus-advanced-headers'."
103 (let ((type (car rule)))
106 ((or (eq type '&) (eq type 'and))
109 t ; Empty rule is true.
111 (gnus-advanced-score-rule (car rule)))
113 ;; If all the rules were true, then `rule' should be nil.
116 ((or (eq type '|) (eq type 'or))
121 (not (gnus-advanced-score-rule (car rule))))
123 ;; If one of the rules returned true, then `rule' should be non-nil.
126 ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
127 (not (gnus-advanced-score-rule (nth 1 rule))))
128 ;; This is a `1-'-type redirection rule.
130 (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
131 (let ((gnus-advanced-headers
133 gnus-advanced-headers
134 (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
135 ;; 1- type redirection.
137 (substring (symbol-name type)
138 (match-beginning 1) (match-end 1)))
139 ;; ^^^ type redirection.
140 (length (symbol-name type))))))
141 (when gnus-advanced-headers
142 (gnus-advanced-score-rule (nth 1 rule)))))
143 ;; Plain scoring rule.
145 (gnus-advanced-score-article rule))
148 (error "Unknown advanced score type: %s" rule)))))
150 (defun gnus-advanced-score-article (rule)
151 ;; `rule' is a semi-normal score rule, so we find out what function
152 ;; that's supposed to do the actual processing.
153 (let* ((header (car rule))
154 (func (assoc (downcase header) gnus-advanced-index)))
156 (error "No such header: %s" rule)
157 ;; Call the score function.
158 (funcall (caddr func) (or (cadr func) header)
159 (cadr rule) (caddr rule)))))
161 (defun gnus-advanced-string (index match type)
162 "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
163 (let* ((type (or type 's))
164 (case-fold-search (not (eq (downcase (symbol-name type))
165 (symbol-name type))))
166 (header (or (aref gnus-advanced-headers index) "")))
168 ((memq type '(r R regexp Regexp))
169 (string-match match header))
170 ((memq type '(s S string String))
171 (string-match (regexp-quote match) header))
172 ((memq type '(e E exact Exact))
173 (string= match header))
174 ((memq type '(f F fuzzy Fuzzy))
175 (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
178 (error "No such string match type: %s" type)))))
180 (defun gnus-advanced-integer (index match type)
181 (if (not (memq type '(< > <= >= =)))
182 (error "No such integer score type: %s" type)
183 (funcall type (or (aref gnus-advanced-headers index) 0) match)))
185 (defun gnus-advanced-date (index match type)
186 (let ((date (apply 'encode-time (parse-time-string
187 (aref gnus-advanced-headers index))))
188 (match (apply 'encode-time (parse-time-string match))))
193 (time-less-p match date))
195 (time-less-p date match))
197 (error "No such date score type: %s" type)))))
199 (defun gnus-advanced-body (header match type)
200 (when (string= header "all")
201 (setq header "article"))
203 (set-buffer nntp-server-buffer)
204 (let* ((request-func (cond ((string= "head" header)
206 ((string= "body" header)
208 (t 'gnus-request-article)))
210 ;; Not all backends support partial fetching. In that case, we
211 ;; just fetch the entire article.
212 (unless (gnus-check-backend-function
213 (intern (concat "request-" header))
215 (setq ofunc request-func)
216 (setq request-func 'gnus-request-article))
217 (setq article (mail-header-number gnus-advanced-headers))
218 (gnus-message 7 "Scoring article %s..." article)
219 (when (funcall request-func article gnus-newsgroup-name)
220 (goto-char (point-min))
221 ;; If just parts of the article is to be searched and the
222 ;; backend didn't support partial fetching, we just narrow to
223 ;; the relevant parts.
225 (if (eq ofunc 'gnus-request-head)
228 (or (search-forward "\n\n" nil t) (point-max)))
230 (or (search-forward "\n\n" nil t) (point))
232 (let* ((case-fold-search (not (eq (downcase (symbol-name type))
233 (symbol-name type))))
235 (cond ((memq type '(r R regexp Regexp))
237 ((memq type '(s S string String))
240 (error "Invalid match type: %s" type)))))
241 (goto-char (point-min))
243 (funcall search-func match nil t)
246 (provide 'gnus-logic)
248 ;;; gnus-logic.el ends here