*** empty log message ***
[gnus] / lisp / gnus-logic.el
1 ;;; gnus-logic.el --- advanced scoring code for Gnus
2 ;; Copyright (C) 1996 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-load)
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 charaters 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 (pop rule))))
90         ;; If all the rules were true, then `rule' should be nil.
91         (not rule)))
92      ;; "Or" rule.
93      ((or (eq type '|) (eq type 'or))
94       (pop rule)
95       (if (not rule)
96           nil
97         (while (and rule
98                     (not (gnus-advanced-score-rule (car rule))))
99           (pop rule))
100         ;; If one of the rules returned true, then `rule' should be non-nil.
101         rule))
102      ;; "Not" rule.
103      ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
104       (not (gnus-advanced-score-rule (nth 1 rule))))
105      ;; This is a `1-'-type redirection rule.
106      ((and (symbolp type)
107            (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
108       (let ((gnus-advanced-headers 
109              (gnus-parent-headers
110               gnus-advanced-headers
111               (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
112                   ;; 1- type redirection.
113                   (string-to-number
114                    (substring (symbol-name type)
115                               (match-beginning 0) (match-end 0)))
116                 ;; ^^^ type redirection.
117                 (length (symbol-name type))))))
118         (when gnus-advanced-headers
119           (gnus-advanced-score-rule (nth 1 rule)))))
120      ;; Plain scoring rule.
121      ((stringp type)
122       (gnus-advanced-score-article rule))
123      ;; Bug-out time!
124      (t
125       (error "Unknown advanced score type: %s" rule)))))
126
127 (defun gnus-advanced-score-article (rule)
128   ;; `rule' is a semi-normal score rule, so we find out
129   ;; what function that's supposed to do the actual
130   ;; processing.
131   (let* ((header (car rule))
132          (func (assoc (downcase header) gnus-advanced-index)))
133     (if (not func)
134         (error "No such header: %s" rule)
135       ;; Call the score function.
136       (funcall (caddr func) (or (cadr func) header)
137                (cadr rule) (caddr rule)))))
138
139 (defun gnus-advanced-string (index match type)
140   "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
141   (let* ((type (or type 's))
142          (case-fold-search (not (eq (downcase (symbol-name type))
143                                     (symbol-name type))))
144          (header (aref gnus-advanced-headers index)))
145     (cond
146      ((memq type '(r R regexp Regexp))
147       (string-match match header))
148      ((memq type '(s S string String))
149       (string-match (regexp-quote match) header))
150      ((memq type '(e E exact Exact))
151       (string= match header))
152      ((memq type '(f F fuzzy Fuzzy))
153       (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
154                     header))
155      (t
156       (error "No such string match type: %s" type)))))
157
158 (defun gnus-advanced-integer (index match type)
159   (if (not (memq type '(< > <= >= =)))
160       (error "No such integer score type: %s" type)
161     (funcall type match (or (aref gnus-advanced-headers index) 0))))
162
163 (defun gnus-advanced-date (index match type)
164   (let ((date (encode-time (parse-time-string
165                             (aref gnus-advanced-headers index))))
166         (match (encode-time (parse-time-string match))))
167     (cond 
168      ((eq type 'at)
169       (equal date match))
170      ((eq type 'before)
171       (gnus-time-less match date))
172      ((eq type 'after)
173       (gnus-time-less date match))
174      (t
175       (error "No such date score type: %s" type)))))
176
177 (defun gnus-advanced-body (header match type)
178   (when (string= header "all")
179     (setq header "article"))
180   (save-excursion
181     (set-buffer nntp-server-buffer)
182     (let* ((request-func (cond ((string= "head" header)
183                                 'gnus-request-head)
184                                ((string= "body" header)
185                                 'gnus-request-body)
186                                (t 'gnus-request-article)))
187            ofunc article)
188       ;; Not all backends support partial fetching.  In that case,
189       ;; we just fetch the entire article.
190       (unless (gnus-check-backend-function 
191                (intern (concat "request-" header))
192                gnus-newsgroup-name)
193         (setq ofunc request-func)
194         (setq request-func 'gnus-request-article))
195       (setq article (mail-header-number gnus-advanced-headers))
196       (gnus-message 7 "Scoring article %s..." article)
197       (when (funcall request-func article gnus-newsgroup-name)
198         (goto-char (point-min))
199         ;; If just parts of the article is to be searched and the
200         ;; backend didn't support partial fetching, we just narrow
201         ;; to the relevant parts.
202         (if ofunc
203             (if (eq ofunc 'gnus-request-head)
204                 (narrow-to-region
205                  (point)
206                  (or (search-forward "\n\n" nil t) (point-max)))
207               (narrow-to-region
208                (or (search-forward "\n\n" nil t) (point))
209                (point-max))))
210         (let* ((case-fold-search (not (eq (downcase (symbol-name type))
211                                           (symbol-name type))))
212                (search-func 
213                 (cond ((memq type '(r R regexp Regexp))
214                        're-search-forward)
215                       ((memq type '(s S string String))
216                        'search-forward)
217                       (t
218                        (error "Illegal match type: %s" type)))))
219           (goto-char (point-min))
220           (prog1
221               (funcall search-func match nil t)
222             (widen)))))))
223
224 (provide 'gnus-logic)
225
226 ;;; gnus-logic.el ends here.