(gnus-vers): Update to 7.1.0.31.
[elisp/gnus.git-] / lisp / gnus-logic.el
1 ;;; gnus-logic.el --- advanced scoring code for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-score)
33 (require 'gnus-util)
34
35 ;;; Internal variables.
36
37 (defvar gnus-advanced-headers nil)
38
39 ;; To avoid having 8-bit characters in the source file.
40 (defvar gnus-advanced-not (intern (format "%c" 172)))
41
42 (defconst gnus-advanced-index
43   ;; Name to index alist.
44   `(("number"
45      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
46                              'location)
47      gnus-advanced-integer)
48     ("subject"
49      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
50                              'subject)
51      gnus-advanced-string)
52     ("from" 
53      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
54                              'from)
55      gnus-advanced-string)
56     ("date"
57      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
58                              'date)
59      gnus-advanced-date)
60     ("message-id"
61      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
62                              'id)
63      gnus-advanced-string)
64     ("references"
65      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
66                              'references)
67      gnus-advanced-string)
68     ("chars"
69      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
70                              'chars)
71      gnus-advanced-integer)
72     ("lines" 
73      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
74                              'lines)
75      gnus-advanced-integer)
76     ("xref"
77      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity)
78                              'xref)
79      gnus-advanced-string)
80     ("head" nil gnus-advanced-body)
81     ("body" nil gnus-advanced-body)
82     ("all" nil gnus-advanced-body)))
83
84 (eval-and-compile
85   (autoload 'parse-time-string "parse-time"))
86
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
94         ;; this article.
95         (if (setq score (assq (mail-header-number gnus-advanced-headers)
96                               gnus-newsgroup-scored))
97             (setcdr score
98                     (+ (cdr score)
99                        (or (nth 1 rule)
100                            gnus-score-interactive-default-score)))
101           (push (cons (mail-header-number gnus-advanced-headers)
102                       (or (nth 1 rule)
103                           gnus-score-interactive-default-score))
104                 gnus-newsgroup-scored)
105           (when trace
106             (push (cons "A file" rule)
107                   gnus-score-trace)))))))
108
109 (defun gnus-advanced-score-rule (rule)
110   "Apply RULE to `gnus-advanced-headers'."
111   (let ((type (car rule)))
112     (cond
113      ;; "And" rule.
114      ((or (eq type '&) (eq type 'and))
115       (pop rule)
116       (if (not rule)
117           t                             ; Empty rule is true.
118         (while (and rule
119                     (gnus-advanced-score-rule (car rule)))
120           (pop rule))
121         ;; If all the rules were true, then `rule' should be nil.
122         (not rule)))
123      ;; "Or" rule.
124      ((or (eq type '|) (eq type 'or))
125       (pop rule)
126       (if (not rule)
127           nil
128         (while (and rule
129                     (not (gnus-advanced-score-rule (car rule))))
130           (pop rule))
131         ;; If one of the rules returned true, then `rule' should be non-nil.
132         rule))
133      ;; "Not" rule.
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.
137      ((and (symbolp type)
138            (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
139       (let ((gnus-advanced-headers
140              (gnus-parent-headers
141               gnus-advanced-headers
142               (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
143                   ;; 1- type redirection.
144                   (string-to-number
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.
152      ((stringp type)
153       (gnus-advanced-score-article rule))
154      ;; Bug-out time!
155      (t
156       (error "Unknown advanced score type: %s" rule)))))
157
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
161   ;; processing.
162   (let* ((header (car rule))
163          (func (assoc (downcase header) gnus-advanced-index)))
164     (if (not func)
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)))))
169
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)))
176     (cond
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))
185                     header))
186      (t
187       (error "No such string match type: %s" type)))))
188
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))))
193
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))))
198     (cond
199      ((eq type 'at)
200       (equal date match))
201      ((eq type 'before)
202       (time-less-p match date))
203      ((eq type 'after)
204       (time-less-p date match))
205      (t
206       (error "No such date score type: %s" type)))))
207
208 (defun gnus-advanced-body (header match type)
209   (when (string= header "all")
210     (setq header "article"))
211   (save-excursion
212     (set-buffer nntp-server-buffer)
213     (let* ((request-func (cond ((string= "head" header)
214                                 'gnus-request-head)
215                                ((string= "body" header)
216                                 'gnus-request-body)
217                                (t 'gnus-request-article)))
218            ofunc 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))
223                gnus-newsgroup-name)
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.
233         (when ofunc
234           (if (eq ofunc 'gnus-request-head)
235               (narrow-to-region
236                (point)
237                (or (search-forward "\n\n" nil t) (point-max)))
238             (narrow-to-region
239              (or (search-forward "\n\n" nil t) (point))
240              (point-max))))
241         (let* ((case-fold-search (not (eq (downcase (symbol-name type))
242                                           (symbol-name type))))
243                (search-func
244                 (cond ((memq type '(r R regexp Regexp))
245                        're-search-forward)
246                       ((memq type '(s S string String))
247                        'search-forward)
248                       (t
249                        (error "Invalid match type: %s" type)))))
250           (goto-char (point-min))
251           (prog1
252               (funcall search-func match nil t)
253             (widen)))))))
254
255 (provide 'gnus-logic)
256
257 ;;; gnus-logic.el ends here.