Sync up with Pterodactyl Gnus 0.17.
[elisp/gnus.git-] / lisp / gnus-agent.el
index c1a5ea6..a9fe734 100644 (file)
@@ -27,7 +27,8 @@
 (require 'gnus-cache)
 (require 'nnvirtual)
 (require 'gnus-sum)
-(eval-when-compile (require 'cl))
+(eval-when-compile (require 'cl)
+                  (require 'gnus-score))
 
 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
   "Where the Gnus agent will store its files."
@@ -77,8 +78,6 @@ If nil, only read articles will be expired."
 
 ;;; Internal variables
 
-(defvar gnus-agent-meta-information-header "X-Gnus-Agent-Meta-Information")
-
 (defvar gnus-agent-history-buffers nil)
 (defvar gnus-agent-buffer-alist nil)
 (defvar gnus-agent-article-alist nil)
@@ -94,6 +93,13 @@ If nil, only read articles will be expired."
 (defvar gnus-agent-send-mail-function nil)
 (defvar gnus-agent-article-file-coding-system 'no-conversion)
 
+(defconst gnus-agent-scoreable-headers
+  (list
+   "subject" "from" "date" "message-id" 
+   "references" "chars" "lines" "xref")
+  "Headers that are considered when scoring articles
+for download via the Agent.")
+
 ;; Dynamic variables
 (defvar gnus-headers)
 (defvar gnus-score)
@@ -750,17 +756,13 @@ the actual number of articles toggled is returned."
       (pop gnus-agent-group-alist))))
 
 (defun gnus-agent-fetch-headers (group articles &optional force)
-  (gnus-agent-load-alist group)
-  ;; Find out what headers we need to retrieve.
-  (when articles
-    (while (and articles
-               (assq (car articles) gnus-agent-article-alist))
-      (pop articles))
-    (let ((arts articles))
-      (while (cdr arts)
-       (if (assq (cadr arts) gnus-agent-article-alist)
-           (setcdr arts (cddr arts))
-         (setq arts (cdr arts)))))
+  (let ((articles (if (gnus-agent-load-alist group)
+                     (gnus-sorted-intersection
+                      (gnus-list-of-unread-articles group)
+                      (gnus-uncompress-range
+                       (cons (1+ (caar (last gnus-agent-article-alist)))
+                             (cdr (gnus-active group)))))
+                   (gnus-list-of-unread-articles group))))
     ;; Fetch them.
     (when articles
       (gnus-message 7 "Fetching headers for %s..." group)
@@ -789,10 +791,11 @@ the actual number of articles toggled is returned."
                                (file-name-directory file)))
          (write-region (point-min) (point-max) file nil 'silent)
          (gnus-agent-save-alist group articles nil)
-         (gnus-agent-enter-history "last-header-fetched-for-session"
-                                   (list (cons group (nth (- (length  articles) 1) articles)))
-                                   (time-to-day (current-time)))
-       t)))))
+         (gnus-agent-enter-history
+          "last-header-fetched-for-session"
+          (list (cons group (nth (- (length  articles) 1) articles)))
+          (time-to-day (current-time)))
+         articles)))))
 
 (defsubst gnus-agent-copy-nov-line (article)
   (let (b e)
@@ -910,27 +913,63 @@ the actual number of articles toggled is returned."
       ;; Parse them and see which articles we want to fetch.
       (setq gnus-newsgroup-dependencies
            (make-vector (length articles) 0))
-      (setq gnus-newsgroup-headers
-           (gnus-get-newsgroup-headers-xover articles nil nil group))
+      ;; No need to call `gnus-get-newsgroup-headers-xover' with 
+      ;; the entire .overview for group as we still have the just
+      ;; downloaded headers in `gnus-agent-overview-buffer'.
+      (let ((nntp-server-buffer gnus-agent-overview-buffer))
+       (setq gnus-newsgroup-headers
+             (gnus-get-newsgroup-headers-xover articles nil nil group)))
       (setq category (gnus-group-category group))
       (setq predicate
            (gnus-get-predicate 
             (or (gnus-group-get-parameter group 'agent-predicate)
                 (cadr category))))
-      (setq score-param
-           (or (gnus-group-get-parameter group 'agent-score)
-               (caddr category)))
-      (when score-param
-       (gnus-score-headers (list (list score-param))))
-      (setq arts nil)
-      (while (setq gnus-headers (pop gnus-newsgroup-headers))
-       (setq gnus-score
-             (or (cdr (assq (mail-header-number gnus-headers)
-                            gnus-newsgroup-scored))
-                 gnus-summary-default-score))
-       (when (funcall predicate)
-         (push (mail-header-number gnus-headers)
-               arts)))
+      ;; Do we want to download everything, or nothing?
+      (if (or (eq (caaddr predicate) 'gnus-agent-true)
+             (eq (caaddr predicate) 'gnus-agent-false))
+         ;; Yes.
+         (setq arts (symbol-value 
+                     (cadr (assoc (caaddr predicate) 
+                                  '((gnus-agent-true articles)
+                                    (gnus-agent-false nil))))))
+       ;; No, we need to decide what we want.
+       (setq score-param
+             (let ((score-method (or 
+                                  (gnus-group-get-parameter group 'agent-score t)
+                                  (caddr category))))
+               (when score-method
+                 (require 'gnus-score)
+                 (if (eq score-method 'file)
+                     (let ((entries
+                            (gnus-score-load-files
+                             (gnus-all-score-files group)))
+                           list score-file)
+                       (while (setq list (car entries))
+                         (push (car list) score-file)
+                         (setq list (cdr list))
+                         (while list
+                           (when (member (caar list)
+                                         gnus-agent-scoreable-headers)
+                             (push (car list) score-file))
+                           (setq list (cdr list)))
+                         (setq score-param 
+                               (append score-param (list (nreverse score-file)))
+                               score-file nil entries (cdr entries)))
+                       (list score-param))
+                   (if (stringp (car score-method))
+                       score-method
+                     (list (list score-method)))))))
+       (when score-param
+         (gnus-score-headers score-param))
+       (setq arts nil)
+       (while (setq gnus-headers (pop gnus-newsgroup-headers))
+         (setq gnus-score
+               (or (cdr (assq (mail-header-number gnus-headers)
+                              gnus-newsgroup-scored))
+                   gnus-summary-default-score))
+         (when (funcall predicate)
+           (push (mail-header-number gnus-headers)
+                 arts))))
       ;; Fetch the articles.
       (when arts
        (gnus-agent-fetch-articles group arts)))
@@ -1362,7 +1401,8 @@ The following commands are available:
                   (sort gnus-agent-article-alist 'car-less-than-car))
             (let* ((alist gnus-agent-article-alist)
                    (prev (cons nil alist))
-                   (first prev))
+                   (first prev)
+                   expired)
               (while (and alist
                           (<= (caar alist) article))
                 (if (or (not (cdar alist))
@@ -1371,22 +1411,33 @@ The following commands are available:
                                (number-to-string
                                 (caar alist))
                                group))))
-                    (setcdr prev (setq alist (cdr alist)))
+                    (progn
+                      (push (caar alist) expired)
+                      (setcdr prev (setq alist (cdr alist))))
                   (setq prev alist
                         alist (cdr alist))))
               (setq gnus-agent-article-alist (cdr first))
-              ;;; Mark all articles up to the first article
-              ;;; in `gnus-article-alist' as read.
+              (gnus-agent-save-alist group)
+               ;; Mark all articles up to the first article
+              ;; in `gnus-article-alist' as read.
               (when (caar gnus-agent-article-alist)
                 (setcar (nthcdr 2 info)
                         (gnus-range-add
                          (nth 2 info)
                          (cons 1 (- (caar gnus-agent-article-alist) 1)))))
+              ;; Maybe everything has been expired from `gnus-article-alist'
+              ;; and so the above marking as read could not be conducted,
+              ;; or there are expired article within the range of the alist.
+              (when (or (not (caar gnus-agent-article-alist))
+                        (> (car expired) (caar gnus-agent-article-alist)))  
+              (setcar (nthcdr 2 info)
+                      (gnus-add-to-range
+                       (nth 2 info)
+                       (nreverse expired))))
               (gnus-dribble-enter
                (concat "(gnus-group-set-info '"
                        (gnus-prin1-to-string info)
-                       ")"))
-              (gnus-agent-save-alist group)))
+                       ")"))))
           expiry-hashtb)
          (set-buffer history)
          (setq histories (nreverse (sort histories '<)))