17493
|
1 ;;; gnus-logic.el --- advanced scoring code for Gnus
|
|
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
|
|
5 ;; Keywords: news
|
|
6
|
|
7 ;; This file is part of GNU Emacs.
|
|
8
|
|
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)
|
|
12 ;; any later version.
|
|
13
|
|
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.
|
|
18
|
|
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.
|
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;;; Code:
|
|
27
|
|
28 (require 'gnus)
|
|
29 (require 'gnus-score)
|
|
30 (require 'gnus-util)
|
|
31
|
|
32 ;;; Internal variables.
|
|
33
|
|
34 (defvar gnus-advanced-headers nil)
|
|
35
|
|
36 ;; To avoid having 8-bit characters in the source file.
|
|
37 (defvar gnus-advanced-not (intern (format "%c" 172)))
|
|
38
|
|
39 (defconst gnus-advanced-index
|
|
40 ;; Name to index alist.
|
|
41 '(("number" 0 gnus-advanced-integer)
|
|
42 ("subject" 1 gnus-advanced-string)
|
|
43 ("from" 2 gnus-advanced-string)
|
|
44 ("date" 3 gnus-advanced-date)
|
|
45 ("message-id" 4 gnus-advanced-string)
|
|
46 ("references" 5 gnus-advanced-string)
|
|
47 ("chars" 6 gnus-advanced-integer)
|
|
48 ("lines" 7 gnus-advanced-integer)
|
|
49 ("xref" 8 gnus-advanced-string)
|
|
50 ("head" nil gnus-advanced-body)
|
|
51 ("body" nil gnus-advanced-body)
|
|
52 ("all" nil gnus-advanced-body)))
|
|
53
|
|
54 (eval-and-compile
|
|
55 (autoload 'parse-time-string "parse-time"))
|
|
56
|
|
57 (defun gnus-score-advanced (rule &optional trace)
|
|
58 "Apply advanced scoring RULE to all the articles in the current group."
|
|
59 (let ((headers gnus-newsgroup-headers)
|
|
60 gnus-advanced-headers score)
|
|
61 (while (setq gnus-advanced-headers (pop headers))
|
|
62 (when (gnus-advanced-score-rule (car rule))
|
|
63 ;; This rule was successful, so we add the score to
|
|
64 ;; this article.
|
|
65 (if (setq score (assq (mail-header-number gnus-advanced-headers)
|
|
66 gnus-newsgroup-scored))
|
|
67 (setcdr score
|
|
68 (+ (cdr score)
|
|
69 (or (nth 1 rule)
|
|
70 gnus-score-interactive-default-score)))
|
|
71 (push (cons (mail-header-number gnus-advanced-headers)
|
|
72 (or (nth 1 rule)
|
|
73 gnus-score-interactive-default-score))
|
|
74 gnus-newsgroup-scored)
|
|
75 (when trace
|
|
76 (push (cons "A file" rule)
|
|
77 gnus-score-trace)))))))
|
|
78
|
|
79 (defun gnus-advanced-score-rule (rule)
|
|
80 "Apply RULE to `gnus-advanced-headers'."
|
|
81 (let ((type (car rule)))
|
|
82 (cond
|
|
83 ;; "And" rule.
|
|
84 ((or (eq type '&) (eq type 'and))
|
|
85 (pop rule)
|
|
86 (if (not rule)
|
|
87 t ; Empty rule is true.
|
|
88 (while (and rule
|
|
89 (gnus-advanced-score-rule (car rule)))
|
|
90 (pop rule))
|
|
91 ;; If all the rules were true, then `rule' should be nil.
|
|
92 (not rule)))
|
|
93 ;; "Or" rule.
|
|
94 ((or (eq type '|) (eq type 'or))
|
|
95 (pop rule)
|
|
96 (if (not rule)
|
|
97 nil
|
|
98 (while (and rule
|
|
99 (not (gnus-advanced-score-rule (car rule))))
|
|
100 (pop rule))
|
|
101 ;; If one of the rules returned true, then `rule' should be non-nil.
|
|
102 rule))
|
|
103 ;; "Not" rule.
|
|
104 ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
|
|
105 (not (gnus-advanced-score-rule (nth 1 rule))))
|
|
106 ;; This is a `1-'-type redirection rule.
|
|
107 ((and (symbolp type)
|
|
108 (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
|
|
109 (let ((gnus-advanced-headers
|
|
110 (gnus-parent-headers
|
|
111 gnus-advanced-headers
|
|
112 (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
|
|
113 ;; 1- type redirection.
|
|
114 (string-to-number
|
|
115 (substring (symbol-name type)
|
|
116 (match-beginning 0) (match-end 0)))
|
|
117 ;; ^^^ type redirection.
|
|
118 (length (symbol-name type))))))
|
|
119 (when gnus-advanced-headers
|
|
120 (gnus-advanced-score-rule (nth 1 rule)))))
|
|
121 ;; Plain scoring rule.
|
|
122 ((stringp type)
|
|
123 (gnus-advanced-score-article rule))
|
|
124 ;; Bug-out time!
|
|
125 (t
|
|
126 (error "Unknown advanced score type: %s" rule)))))
|
|
127
|
|
128 (defun gnus-advanced-score-article (rule)
|
|
129 ;; `rule' is a semi-normal score rule, so we find out
|
|
130 ;; what function that's supposed to do the actual
|
|
131 ;; processing.
|
|
132 (let* ((header (car rule))
|
|
133 (func (assoc (downcase header) gnus-advanced-index)))
|
|
134 (if (not func)
|
|
135 (error "No such header: %s" rule)
|
|
136 ;; Call the score function.
|
|
137 (funcall (caddr func) (or (cadr func) header)
|
|
138 (cadr rule) (caddr rule)))))
|
|
139
|
|
140 (defun gnus-advanced-string (index match type)
|
|
141 "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
|
|
142 (let* ((type (or type 's))
|
|
143 (case-fold-search (not (eq (downcase (symbol-name type))
|
|
144 (symbol-name type))))
|
|
145 (header (aref gnus-advanced-headers index)))
|
|
146 (cond
|
|
147 ((memq type '(r R regexp Regexp))
|
|
148 (string-match match header))
|
|
149 ((memq type '(s S string String))
|
|
150 (string-match (regexp-quote match) header))
|
|
151 ((memq type '(e E exact Exact))
|
|
152 (string= match header))
|
|
153 ((memq type '(f F fuzzy Fuzzy))
|
|
154 (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
|
|
155 header))
|
|
156 (t
|
|
157 (error "No such string match type: %s" type)))))
|
|
158
|
|
159 (defun gnus-advanced-integer (index match type)
|
|
160 (if (not (memq type '(< > <= >= =)))
|
|
161 (error "No such integer score type: %s" type)
|
|
162 (funcall type match (or (aref gnus-advanced-headers index) 0))))
|
|
163
|
|
164 (defun gnus-advanced-date (index match type)
|
|
165 (let ((date (encode-time (parse-time-string
|
|
166 (aref gnus-advanced-headers index))))
|
|
167 (match (encode-time (parse-time-string match))))
|
|
168 (cond
|
|
169 ((eq type 'at)
|
|
170 (equal date match))
|
|
171 ((eq type 'before)
|
|
172 (gnus-time-less match date))
|
|
173 ((eq type 'after)
|
|
174 (gnus-time-less date match))
|
|
175 (t
|
|
176 (error "No such date score type: %s" type)))))
|
|
177
|
|
178 (defun gnus-advanced-body (header match type)
|
|
179 (when (string= header "all")
|
|
180 (setq header "article"))
|
|
181 (save-excursion
|
|
182 (set-buffer nntp-server-buffer)
|
|
183 (let* ((request-func (cond ((string= "head" header)
|
|
184 'gnus-request-head)
|
|
185 ((string= "body" header)
|
|
186 'gnus-request-body)
|
|
187 (t 'gnus-request-article)))
|
|
188 ofunc article)
|
|
189 ;; Not all backends support partial fetching. In that case,
|
|
190 ;; we just fetch the entire article.
|
|
191 (unless (gnus-check-backend-function
|
|
192 (intern (concat "request-" header))
|
|
193 gnus-newsgroup-name)
|
|
194 (setq ofunc request-func)
|
|
195 (setq request-func 'gnus-request-article))
|
|
196 (setq article (mail-header-number gnus-advanced-headers))
|
|
197 (gnus-message 7 "Scoring article %s..." article)
|
|
198 (when (funcall request-func article gnus-newsgroup-name)
|
|
199 (goto-char (point-min))
|
|
200 ;; If just parts of the article is to be searched and the
|
|
201 ;; backend didn't support partial fetching, we just narrow
|
|
202 ;; to the relevant parts.
|
|
203 (when ofunc
|
|
204 (if (eq ofunc 'gnus-request-head)
|
|
205 (narrow-to-region
|
|
206 (point)
|
|
207 (or (search-forward "\n\n" nil t) (point-max)))
|
|
208 (narrow-to-region
|
|
209 (or (search-forward "\n\n" nil t) (point))
|
|
210 (point-max))))
|
|
211 (let* ((case-fold-search (not (eq (downcase (symbol-name type))
|
|
212 (symbol-name type))))
|
|
213 (search-func
|
|
214 (cond ((memq type '(r R regexp Regexp))
|
|
215 're-search-forward)
|
|
216 ((memq type '(s S string String))
|
|
217 'search-forward)
|
|
218 (t
|
|
219 (error "Illegal match type: %s" type)))))
|
|
220 (goto-char (point-min))
|
|
221 (prog1
|
|
222 (funcall search-func match nil t)
|
|
223 (widen)))))))
|
|
224
|
|
225 (provide 'gnus-logic)
|
|
226
|
|
227 ;;; gnus-logic.el ends here.
|