* nnmail.el (nnmail-split-it): Revoke the change of 1999-08-19.
[elisp/gnus.git-] / lisp / gnus-logic.el
1 ;;; gnus-logic.el --- advanced scoring code for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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) 'location)
46      gnus-advanced-integer)
47     ("subject"
48      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'subject)
49      gnus-advanced-string)
50     ("from"
51      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'from)
52      gnus-advanced-string)
53     ("date"
54      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'date)
55      gnus-advanced-date)
56     ("message-id"
57      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'id)
58      gnus-advanced-string)
59     ("references"
60      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'references)
61      gnus-advanced-string)
62     ("chars"
63      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'chars)
64      gnus-advanced-integer)
65     ("lines"
66      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'lines)
67      gnus-advanced-integer)
68     ("xref"
69      ,(luna-class-slot-index (luna-find-class 'mime-gnus-entity) 'xref)
70      gnus-advanced-string)
71     ("head" nil gnus-advanced-body)
72     ("body" nil gnus-advanced-body)
73     ("all" nil gnus-advanced-body)))
74
75 (eval-and-compile
76   (autoload 'parse-time-string "parse-time"))
77
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
88         ;; article.
89         (if (setq score (assq (mail-header-number gnus-advanced-headers)
90                               gnus-newsgroup-scored))
91             (setcdr score
92                     (+ (cdr score) new-score))
93           (push (cons (mail-header-number gnus-advanced-headers)
94                       new-score)
95                 gnus-newsgroup-scored)
96           (when trace
97             (push (cons "A file" rule)
98                   gnus-score-trace)))))))
99
100 (defun gnus-advanced-score-rule (rule)
101   "Apply RULE to `gnus-advanced-headers'."
102   (let ((type (car rule)))
103     (cond
104      ;; "And" rule.
105      ((or (eq type '&) (eq type 'and))
106       (pop rule)
107       (if (not rule)
108           t                             ; Empty rule is true.
109         (while (and rule
110                     (gnus-advanced-score-rule (car rule)))
111           (pop rule))
112         ;; If all the rules were true, then `rule' should be nil.
113         (not rule)))
114      ;; "Or" rule.
115      ((or (eq type '|) (eq type 'or))
116       (pop rule)
117       (if (not rule)
118           nil
119         (while (and rule
120                     (not (gnus-advanced-score-rule (car rule))))
121           (pop rule))
122         ;; If one of the rules returned true, then `rule' should be non-nil.
123         rule))
124      ;; "Not" rule.
125      ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
126       (not (gnus-advanced-score-rule (nth 1 rule))))
127      ;; This is a `1-'-type redirection rule.
128      ((and (symbolp type)
129            (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
130       (let ((gnus-advanced-headers
131              (gnus-parent-headers
132               gnus-advanced-headers
133               (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
134                   ;; 1- type redirection.
135                   (string-to-number
136                    (substring (symbol-name type)
137                               (match-beginning 1) (match-end 1)))
138                 ;; ^^^ type redirection.
139                 (length (symbol-name type))))))
140         (when gnus-advanced-headers
141           (gnus-advanced-score-rule (nth 1 rule)))))
142      ;; Plain scoring rule.
143      ((stringp type)
144       (gnus-advanced-score-article rule))
145      ;; Bug-out time!
146      (t
147       (error "Unknown advanced score type: %s" rule)))))
148
149 (defun gnus-advanced-score-article (rule)
150   ;; `rule' is a semi-normal score rule, so we find out what function
151   ;; that's supposed to do the actual processing.
152   (let* ((header (car rule))
153          (func (assoc (downcase header) gnus-advanced-index)))
154     (if (not func)
155         (error "No such header: %s" rule)
156       ;; Call the score function.
157       (funcall (caddr func) (or (cadr func) header)
158                (cadr rule) (caddr rule)))))
159
160 (defun gnus-advanced-string (index match type)
161   "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
162   (let* ((type (or type 's))
163          (case-fold-search (not (eq (downcase (symbol-name type))
164                                     (symbol-name type))))
165          (header (or (aref gnus-advanced-headers index) "")))
166     (cond
167      ((memq type '(r R regexp Regexp))
168       (string-match match header))
169      ((memq type '(s S string String))
170       (string-match (regexp-quote match) header))
171      ((memq type '(e E exact Exact))
172       (string= match header))
173      ((memq type '(f F fuzzy Fuzzy))
174       (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
175                     header))
176      (t
177       (error "No such string match type: %s" type)))))
178
179 (defun gnus-advanced-integer (index match type)
180   (if (not (memq type '(< > <= >= =)))
181       (error "No such integer score type: %s" type)
182     (funcall type (or (aref gnus-advanced-headers index) 0) match)))
183
184 (defun gnus-advanced-date (index match type)
185   (let ((date (apply 'encode-time (parse-time-string
186                                    (aref gnus-advanced-headers index))))
187         (match (apply 'encode-time (parse-time-string match))))
188     (cond
189      ((eq type 'at)
190       (equal date match))
191      ((eq type 'before)
192       (time-less-p match date))
193      ((eq type 'after)
194       (time-less-p date match))
195      (t
196       (error "No such date score type: %s" type)))))
197
198 (defun gnus-advanced-body (header match type)
199   (when (string= header "all")
200     (setq header "article"))
201   (save-excursion
202     (set-buffer nntp-server-buffer)
203     (let* ((request-func (cond ((string= "head" header)
204                                 'gnus-request-head)
205                                ((string= "body" header)
206                                 'gnus-request-body)
207                                (t 'gnus-request-article)))
208            ofunc article)
209       ;; Not all backends support partial fetching.  In that case, we
210       ;; just fetch the entire article.
211       (unless (gnus-check-backend-function
212                (intern (concat "request-" header))
213                gnus-newsgroup-name)
214         (setq ofunc request-func)
215         (setq request-func 'gnus-request-article))
216       (setq article (mail-header-number gnus-advanced-headers))
217       (gnus-message 7 "Scoring article %s..." article)
218       (when (funcall request-func article gnus-newsgroup-name)
219         (goto-char (point-min))
220         ;; If just parts of the article is to be searched and the
221         ;; backend didn't support partial fetching, we just narrow to
222         ;; the relevant parts.
223         (when ofunc
224           (if (eq ofunc 'gnus-request-head)
225               (narrow-to-region
226                (point)
227                (or (search-forward "\n\n" nil t) (point-max)))
228             (narrow-to-region
229              (or (search-forward "\n\n" nil t) (point))
230              (point-max))))
231         (let* ((case-fold-search (not (eq (downcase (symbol-name type))
232                                           (symbol-name type))))
233                (search-func
234                 (cond ((memq type '(r R regexp Regexp))
235                        're-search-forward)
236                       ((memq type '(s S string String))
237                        'search-forward)
238                       (t
239                        (error "Invalid match type: %s" type)))))
240           (goto-char (point-min))
241           (prog1
242               (funcall search-func match nil t)
243             (widen)))))))
244
245 (provide 'gnus-logic)
246
247 ;;; gnus-logic.el ends here