Regenerate ldefs-boot.el
[bpt/emacs.git] / lisp / gnus / gnus-logic.el
CommitLineData
eec82323 1;;; gnus-logic.el --- advanced scoring code for Gnus
e84b4b86 2
ba318903 3;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
eec82323 4
6748645f 5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
eec82323
LMI
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
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
22
23;;; Commentary:
24
25;;; Code:
26
5ab7173c
RS
27(eval-when-compile (require 'cl))
28
eec82323
LMI
29(require 'gnus)
30(require 'gnus-score)
31(require 'gnus-util)
32
33;;; Internal variables.
34
35(defvar gnus-advanced-headers nil)
36
37;; To avoid having 8-bit characters in the source file.
38(defvar gnus-advanced-not (intern (format "%c" 172)))
39
40(defconst gnus-advanced-index
41 ;; Name to index alist.
42 '(("number" 0 gnus-advanced-integer)
43 ("subject" 1 gnus-advanced-string)
44 ("from" 2 gnus-advanced-string)
45 ("date" 3 gnus-advanced-date)
46 ("message-id" 4 gnus-advanced-string)
47 ("references" 5 gnus-advanced-string)
48 ("chars" 6 gnus-advanced-integer)
49 ("lines" 7 gnus-advanced-integer)
50 ("xref" 8 gnus-advanced-string)
51 ("head" nil gnus-advanced-body)
52 ("body" nil gnus-advanced-body)
53 ("all" nil gnus-advanced-body)))
54
8abf1b22 55(autoload 'parse-time-string "parse-time")
eec82323
LMI
56
57(defun gnus-score-advanced (rule &optional trace)
58 "Apply advanced scoring RULE to all the articles in the current group."
23f87bed
MB
59 (let (new-score score multiple)
60 (dolist (gnus-advanced-headers gnus-newsgroup-headers)
61 (when (setq multiple (gnus-advanced-score-rule (car rule)))
62 (setq new-score (or (nth 1 rule)
63 gnus-score-interactive-default-score))
64 (when (numberp multiple)
65 (setq new-score (* multiple new-score)))
66 ;; This rule was successful, so we add the score to this
67 ;; article.
eec82323
LMI
68 (if (setq score (assq (mail-header-number gnus-advanced-headers)
69 gnus-newsgroup-scored))
70 (setcdr score
23f87bed 71 (+ (cdr score) new-score))
eec82323 72 (push (cons (mail-header-number gnus-advanced-headers)
23f87bed 73 new-score)
eec82323
LMI
74 gnus-newsgroup-scored)
75 (when trace
76 (push (cons "A file" rule)
23f87bed 77 ;; Must be synced with `gnus-score-edit-file-at-point'.
eec82323
LMI
78 gnus-score-trace)))))))
79
80(defun gnus-advanced-score-rule (rule)
81 "Apply RULE to `gnus-advanced-headers'."
82 (let ((type (car rule)))
83 (cond
84 ;; "And" rule.
85 ((or (eq type '&) (eq type 'and))
86 (pop rule)
87 (if (not rule)
88 t ; Empty rule is true.
89 (while (and rule
90 (gnus-advanced-score-rule (car rule)))
91 (pop rule))
92 ;; If all the rules were true, then `rule' should be nil.
93 (not rule)))
94 ;; "Or" rule.
95 ((or (eq type '|) (eq type 'or))
96 (pop rule)
97 (if (not rule)
98 nil
99 (while (and rule
100 (not (gnus-advanced-score-rule (car rule))))
101 (pop rule))
102 ;; If one of the rules returned true, then `rule' should be non-nil.
103 rule))
104 ;; "Not" rule.
105 ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not))
106 (not (gnus-advanced-score-rule (nth 1 rule))))
107 ;; This is a `1-'-type redirection rule.
108 ((and (symbolp type)
109 (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type)))
110 (let ((gnus-advanced-headers
111 (gnus-parent-headers
112 gnus-advanced-headers
113 (if (string-match "^\\([0-9]+\\)-$" (symbol-name type))
114 ;; 1- type redirection.
115 (string-to-number
116 (substring (symbol-name type)
23f87bed 117 (match-beginning 1) (match-end 1)))
eec82323
LMI
118 ;; ^^^ type redirection.
119 (length (symbol-name type))))))
120 (when gnus-advanced-headers
121 (gnus-advanced-score-rule (nth 1 rule)))))
122 ;; Plain scoring rule.
123 ((stringp type)
124 (gnus-advanced-score-article rule))
125 ;; Bug-out time!
126 (t
127 (error "Unknown advanced score type: %s" rule)))))
128
129(defun gnus-advanced-score-article (rule)
23f87bed
MB
130 ;; `rule' is a semi-normal score rule, so we find out what function
131 ;; that's supposed to do the actual processing.
eec82323
LMI
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))))
8b84c4d3 145 (header (or (aref gnus-advanced-headers index) "")))
eec82323
LMI
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)
23f87bed 162 (funcall type (or (aref gnus-advanced-headers index) 0) match)))
eec82323
LMI
163
164(defun gnus-advanced-date (index match type)
6748645f
LMI
165 (let ((date (apply 'encode-time (parse-time-string
166 (aref gnus-advanced-headers index))))
167 (match (apply 'encode-time (parse-time-string match))))
eec82323
LMI
168 (cond
169 ((eq type 'at)
170 (equal date match))
171 ((eq type 'before)
16409b0b 172 (time-less-p match date))
eec82323 173 ((eq type 'after)
16409b0b 174 (time-less-p date match))
eec82323
LMI
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"))
20a673b2 181 (with-current-buffer nntp-server-buffer
eec82323 182 (let* ((request-func (cond ((string= "head" header)
350a1888 183 'gnus-request-head)
350a1888 184 ((string= "body" header)
a2cfe8a3 185 'gnus-request-body)
350a1888
G
186 (t 'gnus-request-article)))
187 ofunc article handles)
23f87bed
MB
188 ;; Not all backends support partial fetching. In that case, we
189 ;; just fetch the entire article.
a2cfe8a3
JT
190 ;; When scoring by body, we need to peek at the headers to detect the
191 ;; content encoding
192 (unless (or (gnus-check-backend-function
193 (intern (concat "request-" header))
194 gnus-newsgroup-name)
195 (string= "body" header))
350a1888
G
196 (setq ofunc request-func)
197 (setq request-func 'gnus-request-article))
eec82323
LMI
198 (setq article (mail-header-number gnus-advanced-headers))
199 (gnus-message 7 "Scoring article %s..." article)
200 (when (funcall request-func article gnus-newsgroup-name)
350a1888
G
201 (when (string= "body" header)
202 (setq handles (gnus-score-decode-text-parts)))
203 (goto-char (point-min))
204 ;; If just parts of the article is to be searched and the
205 ;; backend didn't support partial fetching, we just narrow to
206 ;; the relevant parts.
207 (when ofunc
208 (if (eq ofunc 'gnus-request-head)
209 (narrow-to-region
210 (point)
211 (or (search-forward "\n\n" nil t) (point-max)))
212 (narrow-to-region
213 (or (search-forward "\n\n" nil t) (point))
214 (point-max))))
215 (let* ((case-fold-search (not (eq (downcase (symbol-name type))
216 (symbol-name type))))
217 (search-func
218 (cond ((memq type '(r R regexp Regexp))
219 're-search-forward)
220 ((memq type '(s S string String))
221 'search-forward)
222 (t
223 (error "Invalid match type: %s" type)))))
224 (goto-char (point-min))
225 (prog1
226 (funcall search-func match nil t)
227 (widen)))
228 (when handles (mm-destroy-parts handles))))))
eec82323
LMI
229
230(provide 'gnus-logic)
231
715a2ca2 232;;; gnus-logic.el ends here