Filewatcher File Search
FTP Search
  
Directory (beta)
  
Content Search (beta)
   
pkg://xemacs-packages-el-20050118-6.noarch.rpm:9780884/usr/share/xemacs/xemacs-packages/lisp/gnus/gnus-agent.el  info  downloads

;;; gnus-agent.el --- unplugged support for Gnus
;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
;;        Free Software Foundation, Inc.

;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;;; Code:

(require 'gnus)
(require 'gnus-cache)
(require 'nnmail)
(require 'nnvirtual)
(require 'gnus-sum)
(require 'gnus-score)
(require 'gnus-srvr)
(require 'gnus-util)
(eval-when-compile
  (if (featurep 'xemacs)
      (require 'itimer)
    (require 'timer))
  (require 'cl))

(eval-and-compile
  (autoload 'gnus-server-update-server "gnus-srvr")
  (autoload 'gnus-agent-customize-category "gnus-cus")
)

(defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
  "Where the Gnus agent will store its files."
  :group 'gnus-agent
  :type 'directory)

(defcustom gnus-agent-plugged-hook nil
  "Hook run when plugging into the network."
  :group 'gnus-agent
  :type 'hook)

(defcustom gnus-agent-unplugged-hook nil
  "Hook run when unplugging from the network."
  :group 'gnus-agent
  :type 'hook)

(defcustom gnus-agent-fetched-hook nil
  "Hook run when finished fetching articles."
  :version "21.4"
  :group 'gnus-agent
  :type 'hook)

(defcustom gnus-agent-handle-level gnus-level-subscribed
  "Groups on levels higher than this variable will be ignored by the Agent."
  :group 'gnus-agent
  :type 'integer)

(defcustom gnus-agent-expire-days 7
  "Read articles older than this will be expired.
If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
  :group 'gnus-agent
  :type '(number :tag "days"))

(defcustom gnus-agent-expire-all nil
  "If non-nil, also expire unread, ticked and dormant articles.
If nil, only read articles will be expired."
  :group 'gnus-agent
  :type 'boolean)

(defcustom gnus-agent-group-mode-hook nil
  "Hook run in Agent group minor modes."
  :group 'gnus-agent
  :type 'hook)

;; Extracted from gnus-xmas-redefine in order to preserve user settings
(when (featurep 'xemacs)
  (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))

(defcustom gnus-agent-summary-mode-hook nil
  "Hook run in Agent summary minor modes."
  :group 'gnus-agent
  :type 'hook)

;; Extracted from gnus-xmas-redefine in order to preserve user settings
(when (featurep 'xemacs)
  (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))

(defcustom gnus-agent-server-mode-hook nil
  "Hook run in Agent summary minor modes."
  :group 'gnus-agent
  :type 'hook)

;; Extracted from gnus-xmas-redefine in order to preserve user settings
(when (featurep 'xemacs)
  (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))

(defcustom gnus-agent-confirmation-function 'y-or-n-p
  "Function to confirm when error happens."
  :version "21.1"
  :group 'gnus-agent
  :type 'function)

(defcustom gnus-agent-synchronize-flags 'ask
  "Indicate if flags are synchronized when you plug in.
If this is `ask' the hook will query the user."
  :version "21.1"
  :type '(choice (const :tag "Always" t)
		 (const :tag "Never" nil)
		 (const :tag "Ask" ask))
  :group 'gnus-agent)

(defcustom gnus-agent-go-online 'ask
  "Indicate if offline servers go online when you plug in.
If this is `ask' the hook will query the user."
  :version "21.3"
  :type '(choice (const :tag "Always" t)
		 (const :tag "Never" nil)
		 (const :tag "Ask" ask))
  :group 'gnus-agent)

(defcustom gnus-agent-mark-unread-after-downloaded t
  "Indicate whether to mark articles unread after downloaded."
  :version "21.1"
  :type 'boolean
  :group 'gnus-agent)

(defcustom gnus-agent-download-marks '(download)
  "Marks for downloading."
  :version "21.1"
  :type '(repeat (symbol :tag "Mark"))
  :group 'gnus-agent)

(defcustom gnus-agent-consider-all-articles nil
  "When non-nil, the agent will let the agent predicate decide
whether articles need to be downloaded or not, for all articles.  When
nil, the default, the agent will only let the predicate decide
whether unread articles are downloaded or not.  If you enable this,
groups with large active ranges may open slower and you may also want
to look into the agent expiry settings to block the expiration of
read articles as they would just be downloaded again."
  :version "21.4"
  :type 'boolean
  :group 'gnus-agent)

(defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
  "Chunk size for `gnus-agent-fetch-session'.
The function will split its article fetches into chunks smaller than
this limit."
  :group 'gnus-agent
  :type 'integer)

(defcustom gnus-agent-enable-expiration 'ENABLE
  "The default expiration state for each group.
When set to ENABLE, the default, `gnus-agent-expire' will expire old
contents from a group's local storage.  This value may be overridden
to disable expiration in specific categories, topics, and groups.  Of
course, you could change gnus-agent-enable-expiration to DISABLE then
enable expiration per categories, topics, and groups."
  :group 'gnus-agent
  :type '(radio (const :format "Enable " ENABLE)
                (const :format "Disable " DISABLE)))

(defcustom gnus-agent-expire-unagentized-dirs t
  "*Whether expiration should expire in unagentized directories.
Have gnus-agent-expire scan the directories under
\(gnus-agent-directory) for groups that are no longer agentized.
When found, offer to remove them."
  :version "21.4"
  :type 'boolean
  :group 'gnus-agent)

(defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
  "Initially, all servers from these methods are agentized.
The user may remove or add servers using the Server buffer.
See Info node `(gnus)Server Buffer'."
  :version "21.4"
  :type '(repeat symbol)
  :group 'gnus-agent)

(defcustom gnus-agent-queue-mail t
  "Whether and when outgoing mail should be queued by the agent.
When `always', always queue outgoing mail.  When nil, never
queue.  Otherwise, queue if and only if unplugged."
  :group 'gnus-agent
  :type '(radio (const :format "Always" always)
		(const :format "Never" nil)
		(const :format "When plugged" t)))

(defcustom gnus-agent-prompt-send-queue nil
  "If non-nil, `gnus-group-send-queue' will prompt if called when
unplugged."
  :group 'gnus-agent
  :type 'boolean)

;;; Internal variables

(defvar gnus-agent-history-buffers nil)
(defvar gnus-agent-buffer-alist nil)
(defvar gnus-agent-article-alist nil
  "An assoc list identifying the articles whose headers have been fetched.  
If successfully fetched, these headers will be stored in the group's overview
file.  The key of each assoc pair is the article ID, the value of each assoc
pair is a flag indicating whether the identified article has been downloaded
\(gnus-agent-fetch-articles sets the value to the day of the download).
NOTES:
1) The last element of this list can not be expired as some 
   routines (for example, get-agent-fetch-headers) use the last
   value to track which articles have had their headers retrieved.
2) The function `gnus-agent-regenerate' may destructively modify the value.")
(defvar gnus-agent-group-alist nil)
(defvar gnus-category-alist nil)
(defvar gnus-agent-current-history nil)
(defvar gnus-agent-overview-buffer nil)
(defvar gnus-category-predicate-cache nil)
(defvar gnus-category-group-cache nil)
(defvar gnus-agent-spam-hashtb nil)
(defvar gnus-agent-file-name nil)
(defvar gnus-agent-send-mail-function nil)
(defvar gnus-agent-file-coding-system 'raw-text)
(defvar gnus-agent-file-loading-cache nil)

;; Dynamic variables
(defvar gnus-headers)
(defvar gnus-score)

;;;
;;; Setup
;;;

(defun gnus-open-agent ()
  (setq gnus-agent t)
  (gnus-agent-read-servers)
  (gnus-category-read)
  (gnus-agent-create-buffer)
  (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
  (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
  (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))

(defun gnus-agent-create-buffer ()
  (if (gnus-buffer-live-p gnus-agent-overview-buffer)
      t
    (setq gnus-agent-overview-buffer
	  (gnus-get-buffer-create " *Gnus agent overview*"))
    (with-current-buffer gnus-agent-overview-buffer
      (mm-enable-multibyte))
    nil))

(gnus-add-shutdown 'gnus-close-agent 'gnus)

(defun gnus-close-agent ()
  (setq gnus-category-predicate-cache nil
	gnus-category-group-cache nil
	gnus-agent-spam-hashtb nil)
  (gnus-kill-buffer gnus-agent-overview-buffer))

;;;
;;; Utility functions
;;;

(defun gnus-agent-read-file (file)
  "Load FILE and do a `read' there."
  (with-temp-buffer
    (ignore-errors
      (nnheader-insert-file-contents file)
      (goto-char (point-min))
      (read (current-buffer)))))

(defsubst gnus-agent-method ()
  (concat (symbol-name (car gnus-command-method)) "/"
	  (if (equal (cadr gnus-command-method) "")
	      "unnamed"
	    (cadr gnus-command-method))))

(defsubst gnus-agent-directory ()
  "The name of the Gnus agent directory."
  (nnheader-concat gnus-agent-directory
		   (nnheader-translate-file-chars (gnus-agent-method)) "/"))

(defun gnus-agent-lib-file (file)
  "The full name of the Gnus agent library FILE."
  (expand-file-name file
		    (file-name-as-directory
		     (expand-file-name "agent.lib" (gnus-agent-directory)))))

(defun gnus-agent-cat-set-property (category property value)
  (if value
      (setcdr (or (assq property category)
              (let ((cell (cons property nil)))
                    (setcdr category (cons cell (cdr category)))
                    cell)) value)
    (let ((category category))
      (while (cond ((eq property (caadr category))
                    (setcdr category (cddr category))
                    nil)
                   (t
                    (setq category (cdr category)))))))
  category)

(eval-when-compile
  (defmacro gnus-agent-cat-defaccessor (name prop-name)
    "Define accessor and setter methods for manipulating a list of the form
\(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
manipulated as follows:
  (func LIST): Returns VALUE1
  (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
    `(progn (defmacro ,name (category)
              (list (quote cdr) (list (quote assq)
                                      (quote (quote ,prop-name)) category)))

            (define-setf-method ,name (category)
              (let* ((--category--temp-- (make-symbol "--category--"))
                     (--value--temp-- (make-symbol "--value--")))
                (list (list --category--temp--) ; temporary-variables
                      (list category)   ; value-forms
                      (list --value--temp--) ; store-variables
                      (let* ((category --category--temp--) ; store-form
                             (value --value--temp--))
                        (list (quote gnus-agent-cat-set-property)
                              category
                              (quote (quote ,prop-name))
                              value))
                      (list (quote ,name) --category--temp--) ; access-form
                      )))))
  )

(defmacro gnus-agent-cat-name (category)
  `(car ,category))

(gnus-agent-cat-defaccessor
 gnus-agent-cat-days-until-old             agent-days-until-old)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-enable-expiration          agent-enable-expiration)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-groups                     agent-groups)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-high-score                 agent-high-score)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-length-when-long           agent-length-when-long)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-length-when-short          agent-length-when-short)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-low-score                  agent-low-score)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-predicate                  agent-predicate)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-score-file                 agent-score-file)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)

(eval-and-compile
  (defsetf gnus-agent-cat-groups (category) (groups)
    (list 'gnus-agent-set-cat-groups category groups)))

(defun gnus-agent-set-cat-groups (category groups)
  (unless (eq groups 'ignore)
    (let ((new-g groups)
          (old-g (gnus-agent-cat-groups category)))
      (cond ((eq new-g old-g)
             ;; gnus-agent-add-group is fiddling with the group
             ;; list. Still, Im done.
             nil
             )
            ((eq new-g (cdr old-g))
             ;; gnus-agent-add-group is fiddling with the group list
             (setcdr (or (assq 'agent-groups category)
                         (let ((cell (cons 'agent-groups nil)))
                           (setcdr category (cons cell (cdr category)))
                           cell)) new-g))
            (t
             (let ((groups groups))
               (while groups
                 (let* ((group        (pop groups))
                        (old-category (gnus-group-category group)))
                   (if (eq category old-category)
                       nil
                     (setf (gnus-agent-cat-groups old-category)
                           (delete group (gnus-agent-cat-groups
                                          old-category))))))
               ;; Purge cache as preceeding loop invalidated it.
               (setq gnus-category-group-cache nil))

             (setcdr (or (assq 'agent-groups category)
                         (let ((cell (cons 'agent-groups nil)))
                           (setcdr category (cons cell (cdr category)))
                           cell)) groups))))))

(defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
  (list name `(agent-predicate . ,(or default-agent-predicate 'false))))

;;; Fetching setup functions.

(defun gnus-agent-start-fetch ()
  "Initialize data structures for efficient fetching."
  (gnus-agent-create-buffer))

(defun gnus-agent-stop-fetch ()
  "Save all data structures and clean up."
  (setq gnus-agent-spam-hashtb nil)
  (save-excursion
    (set-buffer nntp-server-buffer)
    (widen)))

(defmacro gnus-agent-with-fetch (&rest forms)
  "Do FORMS safely."
  `(unwind-protect
       (let ((gnus-agent-fetching t))
	 (gnus-agent-start-fetch)
	 ,@forms)
     (gnus-agent-stop-fetch)))

(put 'gnus-agent-with-fetch 'lisp-indent-function 0)
(put 'gnus-agent-with-fetch 'edebug-form-spec '(body))

(defmacro gnus-agent-append-to-list (tail value)
  `(setq ,tail (setcdr ,tail (cons ,value nil))))

(defmacro gnus-agent-message (level &rest args)
  `(if (<= ,level gnus-verbose)
       (message ,@args)))

;;;
;;; Mode infestation
;;;

(defvar gnus-agent-mode-hook nil
  "Hook run when installing agent mode.")

(defvar gnus-agent-mode nil)
(defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))

(defun gnus-agent-mode ()
  "Minor mode for providing a agent support in Gnus buffers."
  (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
				      (symbol-name major-mode))
			(match-string 1 (symbol-name major-mode))))
	 (mode (intern (format "gnus-agent-%s-mode" buffer))))
    (set (make-local-variable 'gnus-agent-mode) t)
    (set mode nil)
    (set (make-local-variable mode) t)
    ;; Set up the menu.
    (when (gnus-visual-p 'agent-menu 'menu)
      (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
    (unless (assq 'gnus-agent-mode minor-mode-alist)
      (push gnus-agent-mode-status minor-mode-alist))
    (unless (assq mode minor-mode-map-alist)
      (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
						     buffer))))
	    minor-mode-map-alist))
    (when (eq major-mode 'gnus-group-mode)
      (let ((init-plugged gnus-plugged)
            (gnus-agent-go-online nil))
        ;; g-a-t-p does nothing when gnus-plugged isn't changed.
        ;; Therefore, make certain that the current value does not
        ;; match the desired initial value.
        (setq gnus-plugged :unknown)
        (gnus-agent-toggle-plugged init-plugged)))
    (gnus-run-hooks 'gnus-agent-mode-hook
		    (intern (format "gnus-agent-%s-mode-hook" buffer)))))

(defvar gnus-agent-group-mode-map (make-sparse-keymap))
(gnus-define-keys gnus-agent-group-mode-map
  "Ju" gnus-agent-fetch-groups
  "Jc" gnus-enter-category-buffer
  "Jj" gnus-agent-toggle-plugged
  "Js" gnus-agent-fetch-session
  "JY" gnus-agent-synchronize-flags
  "JS" gnus-group-send-queue
  "Ja" gnus-agent-add-group
  "Jr" gnus-agent-remove-group
  "Jo" gnus-agent-toggle-group-plugged)

(defun gnus-agent-group-make-menu-bar ()
  (unless (boundp 'gnus-agent-group-menu)
    (easy-menu-define
     gnus-agent-group-menu gnus-agent-group-mode-map ""
     '("Agent"
       ["Toggle plugged" gnus-agent-toggle-plugged t]
       ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
       ["List categories" gnus-enter-category-buffer t]
       ["Add (current) group to category" gnus-agent-add-group t]
       ["Remove (current) group from category" gnus-agent-remove-group t]
       ["Send queue" gnus-group-send-queue gnus-plugged]
       ("Fetch"
	["All" gnus-agent-fetch-session gnus-plugged]
	["Group" gnus-agent-fetch-group gnus-plugged])
       ["Synchronize flags" gnus-agent-synchronize-flags t]
       ))))

(defvar gnus-agent-summary-mode-map (make-sparse-keymap))
(gnus-define-keys gnus-agent-summary-mode-map
  "Jj" gnus-agent-toggle-plugged
  "Ju" gnus-agent-summary-fetch-group
  "JS" gnus-agent-fetch-group
  "Js" gnus-agent-summary-fetch-series
  "J#" gnus-agent-mark-article
  "J\M-#" gnus-agent-unmark-article
  "@" gnus-agent-toggle-mark
  "Jc" gnus-agent-catchup)

(defun gnus-agent-summary-make-menu-bar ()
  (unless (boundp 'gnus-agent-summary-menu)
    (easy-menu-define
     gnus-agent-summary-menu gnus-agent-summary-mode-map ""
     '("Agent"
       ["Toggle plugged" gnus-agent-toggle-plugged t]
       ["Mark as downloadable" gnus-agent-mark-article t]
       ["Unmark as downloadable" gnus-agent-unmark-article t]
       ["Toggle mark" gnus-agent-toggle-mark t]
       ["Fetch downloadable" gnus-agent-summary-fetch-group t]
       ["Catchup undownloaded" gnus-agent-catchup t]))))

(defvar gnus-agent-server-mode-map (make-sparse-keymap))
(gnus-define-keys gnus-agent-server-mode-map
  "Jj" gnus-agent-toggle-plugged
  "Ja" gnus-agent-add-server
  "Jr" gnus-agent-remove-server)

(defun gnus-agent-server-make-menu-bar ()
  (unless (boundp 'gnus-agent-server-menu)
    (easy-menu-define
     gnus-agent-server-menu gnus-agent-server-mode-map ""
     '("Agent"
       ["Toggle plugged" gnus-agent-toggle-plugged t]
       ["Add" gnus-agent-add-server t]
       ["Remove" gnus-agent-remove-server t]))))

(defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
  (if (and (fboundp 'propertize)
	   (fboundp 'make-mode-line-mouse-map))
      (propertize string 'local-map
		  (make-mode-line-mouse-map mouse-button mouse-func))
    string))

(defun gnus-agent-toggle-plugged (set-to)
  "Toggle whether Gnus is unplugged or not."
  (interactive (list (not gnus-plugged)))
  (cond ((eq set-to gnus-plugged)
         nil)
        (set-to
         (setq gnus-plugged set-to)
         (gnus-run-hooks 'gnus-agent-plugged-hook)
         (setcar (cdr gnus-agent-mode-status)
                 (gnus-agent-make-mode-line-string " Plugged"
                                                   'mouse-2
                                                   'gnus-agent-toggle-plugged))
         (gnus-agent-go-online gnus-agent-go-online)
         (gnus-agent-possibly-synchronize-flags))
        (t
         (gnus-agent-close-connections)
         (setq gnus-plugged set-to)
         (gnus-run-hooks 'gnus-agent-unplugged-hook)
         (setcar (cdr gnus-agent-mode-status)
                 (gnus-agent-make-mode-line-string " Unplugged"
                                                   'mouse-2
                                                   'gnus-agent-toggle-plugged))))
  (set-buffer-modified-p t))

(defmacro gnus-agent-while-plugged (&rest body)
  `(let ((original-gnus-plugged gnus-plugged))
    (unwind-protect
        (progn (gnus-agent-toggle-plugged t)
               ,@body)
      (gnus-agent-toggle-plugged original-gnus-plugged))))

(put 'gnus-agent-while-plugged 'lisp-indent-function 0)
(put 'gnus-agent-while-plugged 'edebug-form-spec '(body))

(defun gnus-agent-close-connections ()
  "Close all methods covered by the Gnus agent."
  (let ((methods (gnus-agent-covered-methods)))
    (while methods
      (gnus-close-server (pop methods)))))

;;;###autoload
(defun gnus-unplugged ()
  "Start Gnus unplugged."
  (interactive)
  (setq gnus-plugged nil)
  (gnus))

;;;###autoload
(defun gnus-plugged ()
  "Start Gnus plugged."
  (interactive)
  (setq gnus-plugged t)
  (gnus))

;;;###autoload
(defun gnus-slave-unplugged (&optional arg)
  "Read news as a slave unplugged."
  (interactive "P")
  (setq gnus-plugged nil)
  (gnus arg nil 'slave))

;;;###autoload
(defun gnus-agentize ()
  "Allow Gnus to be an offline newsreader.

The gnus-agentize function is now called internally by gnus when
gnus-agent is set.  If you wish to avoid calling gnus-agentize,
customize gnus-agent to nil.

This will modify the `gnus-setup-news-hook', and
`message-send-mail-real-function' variables, and install the Gnus agent
minor mode in all Gnus buffers."
  (interactive)
  (gnus-open-agent)
  (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
  (unless gnus-agent-send-mail-function
    (setq gnus-agent-send-mail-function
	  (or message-send-mail-real-function
	      message-send-mail-function)
	  message-send-mail-real-function 'gnus-agent-send-mail))

  ;; If the servers file doesn't exist, auto-agentize some servers and
  ;; save the servers file so this auto-agentizing isn't invoked
  ;; again.
  (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
    (gnus-message 3 "First time agent user, agentizing remote groups...")
    (mapc
     (lambda (server-or-method)
       (let ((method (gnus-server-to-method server-or-method)))
	 (when (memq (car method)
		     gnus-agent-auto-agentize-methods)
	   (push (gnus-method-to-server method)
		 gnus-agent-covered-methods)
	   (setq gnus-agent-method-p-cache nil))))
     (cons gnus-select-method gnus-secondary-select-methods))
    (gnus-agent-write-servers)))

(defun gnus-agent-queue-setup (&optional group-name)
  "Make sure the queue group exists.
Optional arg GROUP-NAME allows to specify another group."
  (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
			gnus-newsrc-hashtb)
    (gnus-request-create-group (or group-name "queue") '(nndraft ""))
    (let ((gnus-level-default-subscribed 1))
      (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
			    nil '(nndraft "")))
    (gnus-group-set-parameter
     (format "nndraft:%s" (or group-name "queue"))
     'gnus-dummy '((gnus-draft-mode)))))

(defun gnus-agent-send-mail ()
  (if (or (not gnus-agent-queue-mail)
	  (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
      (funcall gnus-agent-send-mail-function)
    (goto-char (point-min))
    (re-search-forward
     (concat "^" (regexp-quote mail-header-separator) "\n"))
    (replace-match "\n")
    (gnus-agent-insert-meta-information 'mail)
    (gnus-request-accept-article "nndraft:queue" nil t t)))

(defun gnus-agent-insert-meta-information (type &optional method)
  "Insert meta-information into the message that says how it's to be posted.
TYPE can be either `mail' or `news'.  If the latter, then METHOD can
be a select method."
  (save-excursion
    (message-remove-header gnus-agent-meta-information-header)
    (goto-char (point-min))
    (insert gnus-agent-meta-information-header ": "
	    (symbol-name type) " " (format "%S" method)
	    "\n")
    (forward-char -1)
    (while (search-backward "\n" nil t)
      (replace-match "\\n" t t))))

(defun gnus-agent-restore-gcc ()
  "Restore GCC field from saved header."
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward
	    (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
      (replace-match "Gcc:" 'fixedcase))))

(defun gnus-agent-any-covered-gcc ()
  (save-restriction
    (message-narrow-to-headers)
    (let* ((gcc (mail-fetch-field "gcc" nil t))
	   (methods (and gcc
			 (mapcar 'gnus-inews-group-method
				 (message-unquote-tokens
				  (message-tokenize-header
				   gcc " ,")))))
	   covered)
      (while (and (not covered) methods)
	(setq covered (gnus-agent-method-p (car methods))
	      methods (cdr methods)))
      covered)))

;;;###autoload
(defun gnus-agent-possibly-save-gcc ()
  "Save GCC if Gnus is unplugged."
  (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
    (save-excursion
      (goto-char (point-min))
      (let ((case-fold-search t))
	(while (re-search-forward "^gcc:" nil t)
	  (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))

(defun gnus-agent-possibly-do-gcc ()
  "Do GCC if Gnus is plugged."
  (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
    (gnus-inews-do-gcc)))

;;;
;;; Group mode commands
;;;

(defun gnus-agent-fetch-groups (n)
  "Put all new articles in the current groups into the Agent."
  (interactive "P")
  (unless gnus-plugged
    (error "Groups can't be fetched when Gnus is unplugged"))
  (gnus-group-iterate n 'gnus-agent-fetch-group))

(defun gnus-agent-fetch-group (&optional group)
  "Put all new articles in GROUP into the Agent."
  (interactive (list (gnus-group-group-name)))
  (setq group (or group gnus-newsgroup-name))
  (unless group
    (error "No group on the current line"))

  (gnus-agent-while-plugged
    (let ((gnus-command-method (gnus-find-method-for-group group)))
      (gnus-agent-with-fetch
        (gnus-agent-fetch-group-1 group gnus-command-method)
        (gnus-message 5 "Fetching %s...done" group)))))

(defun gnus-agent-add-group (category arg)
  "Add the current group to an agent category."
  (interactive
   (list
    (intern
     (completing-read
      "Add to category: "
      (mapcar (lambda (cat) (list (symbol-name (car cat))))
	      gnus-category-alist)
      nil t))
    current-prefix-arg))
  (let ((cat (assq category gnus-category-alist))
	c groups)
    (gnus-group-iterate arg
      (lambda (group)
	(when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
	  (setf (gnus-agent-cat-groups c)
                (delete group (gnus-agent-cat-groups c))))
	(push group groups)))
    (setf (gnus-agent-cat-groups cat)
          (nconc (gnus-agent-cat-groups cat) groups))
    (gnus-category-write)))

(defun gnus-agent-remove-group (arg)
  "Remove the current group from its agent category, if any."
  (interactive "P")
  (let (c)
    (gnus-group-iterate arg
      (lambda (group)
	(when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
	  (setf (gnus-agent-cat-groups c)
                (delete group (gnus-agent-cat-groups c))))))
    (gnus-category-write)))

(defun gnus-agent-synchronize-flags ()
  "Synchronize unplugged flags with servers."
  (interactive)
  (save-excursion
    (dolist (gnus-command-method (gnus-agent-covered-methods))
      (when (file-exists-p (gnus-agent-lib-file "flags"))
	(gnus-agent-synchronize-flags-server gnus-command-method)))))

(defun gnus-agent-possibly-synchronize-flags ()
  "Synchronize flags according to `gnus-agent-synchronize-flags'."
  (interactive)
  (save-excursion
    (dolist (gnus-command-method (gnus-agent-covered-methods))
      (when (file-exists-p (gnus-agent-lib-file "flags"))
	(gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))

(defun gnus-agent-synchronize-flags-server (method)
  "Synchronize flags set when unplugged for server."
  (let ((gnus-command-method method))
    (when (file-exists-p (gnus-agent-lib-file "flags"))
      (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
      (erase-buffer)
      (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
      (if (null (gnus-check-server gnus-command-method))
	  (gnus-message 1 "Couldn't open server %s" (nth 1 gnus-command-method))
	(while (not (eobp))
	  (if (null (eval (read (current-buffer))))
	      (gnus-delete-line)
	    (write-file (gnus-agent-lib-file "flags"))
	    (error "Couldn't set flags from file %s"
		   (gnus-agent-lib-file "flags"))))
	(delete-file (gnus-agent-lib-file "flags")))
      (kill-buffer nil))))

(defun gnus-agent-possibly-synchronize-flags-server (method)
  "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
  (when (or (and gnus-agent-synchronize-flags
		 (not (eq gnus-agent-synchronize-flags 'ask)))
	    (and (eq gnus-agent-synchronize-flags 'ask)
		 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
					(cadr method)))))
    (gnus-agent-synchronize-flags-server method)))

;;;
;;; Server mode commands
;;;

(defun gnus-agent-add-server ()
  "Enroll SERVER in the agent program."
  (interactive)
  (let* ((server       (gnus-server-server-name))
         (named-server (gnus-server-named-server))
         (method       (and server
                            (gnus-server-get-method nil server))))
    (unless server
      (error "No server on the current line"))

    (when (gnus-agent-method-p method)
      (error "Server already in the agent program"))

    (push named-server gnus-agent-covered-methods)

    (setq gnus-agent-method-p-cache nil)
    (gnus-server-update-server server)
    (gnus-agent-write-servers)
    (gnus-message 1 "Entered %s into the Agent" server)))

(defun gnus-agent-remove-server ()
  "Remove SERVER from the agent program."
  (interactive)
  (let* ((server       (gnus-server-server-name))
         (named-server (gnus-server-named-server)))
    (unless server
      (error "No server on the current line"))

    (unless (member named-server gnus-agent-covered-methods)
      (error "Server not in the agent program"))

    (setq gnus-agent-covered-methods 
          (delete named-server gnus-agent-covered-methods)
          gnus-agent-method-p-cache nil)

    (gnus-server-update-server server)
    (gnus-agent-write-servers)
    (gnus-message 1 "Removed %s from the agent" server)))

(defun gnus-agent-read-servers ()
  "Read the alist of covered servers."
  (setq gnus-agent-covered-methods 
        (gnus-agent-read-file
         (nnheader-concat gnus-agent-directory "lib/servers"))
        gnus-agent-method-p-cache nil)

  ;; I am called so early in start-up that I can not validate server
  ;; names.  When that is the case, I skip the validation.  That is
  ;; alright as the gnus startup code calls the validate methods
  ;; directly.
  (if gnus-server-alist
      (gnus-agent-read-servers-validate)))

(defun gnus-agent-read-servers-validate ()
  (mapcar (lambda (server-or-method)
            (let* ((server (if (stringp server-or-method)
                               server-or-method
                             (gnus-method-to-server server-or-method)))
                   (method (gnus-server-to-method server)))
              (if method
                  (unless (member server gnus-agent-covered-methods)
                    (push server gnus-agent-covered-methods)
                    (setq gnus-agent-method-p-cache nil))
                (gnus-message 1 "Ignoring disappeared server `%s'" server))))
          (prog1 gnus-agent-covered-methods
            (setq gnus-agent-covered-methods nil))))

(defun gnus-agent-read-servers-validate-native (native-method)
  (setq gnus-agent-covered-methods
        (mapcar (lambda (method)
                  (if (or (not method)
                          (equal method native-method))
                      "native"
                    method)) gnus-agent-covered-methods)))

(defun gnus-agent-write-servers ()
  "Write the alist of covered servers."
  (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
  (let ((coding-system-for-write nnheader-file-coding-system)
	(file-name-coding-system nnmail-pathname-coding-system))
    (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
      (prin1 gnus-agent-covered-methods
	     (current-buffer)))))

;;;
;;; Summary commands
;;;

(defun gnus-agent-mark-article (n &optional unmark)
  "Mark the next N articles as downloadable.
If N is negative, mark backward instead.  If UNMARK is non-nil, remove
the mark instead.  The difference between N and the actual number of
articles marked is returned."
  (interactive "p")
  (let ((backward (< n 0))
	(n (abs n)))
    (while (and
	    (> n 0)
	    (progn
	      (gnus-summary-set-agent-mark
	       (gnus-summary-article-number) unmark)
	      (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
      (setq n (1- n)))
    (when (/= 0 n)
      (gnus-message 7 "No more articles"))
    (gnus-summary-recenter)
    (gnus-summary-position-point)
    n))

(defun gnus-agent-unmark-article (n)
  "Remove the downloadable mark from the next N articles.
If N is negative, unmark backward instead.  The difference between N and
the actual number of articles unmarked is returned."
  (interactive "p")
  (gnus-agent-mark-article n t))

(defun gnus-agent-toggle-mark (n)
  "Toggle the downloadable mark from the next N articles.
If N is negative, toggle backward instead.  The difference between N and
the actual number of articles toggled is returned."
  (interactive "p")
  (gnus-agent-mark-article n 'toggle))

(defun gnus-summary-set-agent-mark (article &optional unmark)
  "Mark ARTICLE as downloadable.  If UNMARK is nil, article is marked.
When UNMARK is t, the article is unmarked.  For any other value, the
article's mark is toggled."
  (let ((unmark (cond ((eq nil unmark)
		       nil)
		      ((eq t unmark)
		       t)
		      (t
		       (memq article gnus-newsgroup-downloadable)))))
    (when (gnus-summary-goto-subject article nil t)
      (gnus-summary-update-mark
       (if unmark
           (progn
             (setq gnus-newsgroup-downloadable
                   (delq article gnus-newsgroup-downloadable))
             (gnus-article-mark article))
	 (setq gnus-newsgroup-downloadable
	       (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
	 gnus-downloadable-mark)
       'unread))))

(defun gnus-agent-get-undownloaded-list ()
  "Construct list of articles that have not been downloaded."
  (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
    (when (set (make-local-variable 'gnus-newsgroup-agentized)
               (gnus-agent-method-p gnus-command-method))
      (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
             (headers (sort (mapcar (lambda (h)
                                      (mail-header-number h))
                                    gnus-newsgroup-headers) '<))
             (cached (and gnus-use-cache gnus-newsgroup-cached))
             (undownloaded (list nil))
             (tail-undownloaded undownloaded)
             (unfetched (list nil))
             (tail-unfetched unfetched))
	(while (and alist headers)
	  (let ((a (caar alist))
		(h (car headers)))
	    (cond ((< a h)
		   ;; Ignore IDs in the alist that are not being
		   ;; displayed in the summary.
		   (setq alist (cdr alist)))
		  ((> a h)
                   ;; Headers that are not in the alist should be
                   ;; fictious (see nnagent-retrieve-headers); they
                   ;; imply that this article isn't in the agent.
		   (gnus-agent-append-to-list tail-undownloaded h)
		   (gnus-agent-append-to-list tail-unfetched    h)
                   (setq headers (cdr headers))) 
		  ((cdar alist)
		   (setq alist (cdr alist))
		   (setq headers (cdr headers))
		   nil                  ; ignore already downloaded
		   )
		  (t
		   (setq alist (cdr alist))
		   (setq headers (cdr headers))
                   
                   ;; This article isn't in the agent.  Check to see
                   ;; if it is in the cache.  If it is, it's been
                   ;; downloaded.
                   (while (and cached (< (car cached) a))
                     (setq cached (cdr cached)))
                   (unless (equal a (car cached))
                     (gnus-agent-append-to-list tail-undownloaded a))))))

	(while headers
          (let ((num (pop headers)))
            (gnus-agent-append-to-list tail-undownloaded num)
            (gnus-agent-append-to-list tail-unfetched    num)))

	(setq gnus-newsgroup-undownloaded (cdr undownloaded)
              gnus-newsgroup-unfetched    (cdr unfetched))))))

(defun gnus-agent-catchup ()
  "Mark as read all unhandled articles.
An article is unhandled if it is neither cached, nor downloaded, nor
downloadable."
  (interactive)
  (save-excursion
    (let ((articles gnus-newsgroup-undownloaded))
      (when (or gnus-newsgroup-downloadable
                gnus-newsgroup-cached)
        (setq articles (gnus-sorted-ndifference
			(gnus-sorted-ndifference
			 (gnus-copy-sequence articles)
			 gnus-newsgroup-downloadable)
			gnus-newsgroup-cached)))

      (while articles
        (gnus-summary-mark-article
         (pop articles) gnus-catchup-mark)))
    (gnus-summary-position-point)))

(defun gnus-agent-summary-fetch-series ()
  (interactive)
  (when gnus-newsgroup-processable
    (setq gnus-newsgroup-downloadable
          (let* ((dl gnus-newsgroup-downloadable)
                 (gnus-newsgroup-downloadable
		  (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
                 (fetched-articles (gnus-agent-summary-fetch-group)))
            ;; The preceeding call to (gnus-agent-summary-fetch-group)
            ;; updated gnus-newsgroup-downloadable to remove each
            ;; article successfully fetched.

            ;; For each article that I processed, remove its
            ;; processable mark IF the article is no longer
            ;; downloadable (i.e. it's already downloaded)
            (dolist (article gnus-newsgroup-processable)
              (unless (memq article gnus-newsgroup-downloadable)
                (gnus-summary-remove-process-mark article)))
            (gnus-sorted-ndifference dl fetched-articles)))))

(defun gnus-agent-summary-fetch-group (&optional all)
  "Fetch the downloadable articles in the group.
Optional arg ALL, if non-nil, means to fetch all articles."
  (interactive "P")
  (let ((articles
	 (if all gnus-newsgroup-articles
	   gnus-newsgroup-downloadable))
	(gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
        fetched-articles)
    (gnus-agent-while-plugged
      (unless articles
        (error "No articles to download"))
      (gnus-agent-with-fetch
        (setq gnus-newsgroup-undownloaded
              (gnus-sorted-ndifference
               gnus-newsgroup-undownloaded
               (setq fetched-articles
                     (gnus-agent-fetch-articles
                      gnus-newsgroup-name articles)))))
      (save-excursion
        (dolist (article articles)
          (let ((was-marked-downloadable 
                 (memq article gnus-newsgroup-downloadable)))
            (cond (gnus-agent-mark-unread-after-downloaded
                   (setq gnus-newsgroup-downloadable
                         (delq article gnus-newsgroup-downloadable))

                   (gnus-summary-mark-article article gnus-unread-mark))
                  (was-marked-downloadable
                   (gnus-summary-set-agent-mark article t)))
            (when (gnus-summary-goto-subject article nil t)
              (gnus-summary-update-download-mark article))))))
    fetched-articles))

(defun gnus-agent-fetch-selected-article ()
  "Fetch the current article as it is selected.
This can be added to `gnus-select-article-hook' or
`gnus-mark-article-hook'."
  (let ((gnus-command-method gnus-current-select-method))
    (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
      (when (gnus-agent-fetch-articles
             gnus-newsgroup-name
	     (list gnus-current-article))
	(setq gnus-newsgroup-undownloaded
	      (delq gnus-current-article gnus-newsgroup-undownloaded))
        (gnus-summary-update-download-mark gnus-current-article)))))

;;;
;;; Internal functions
;;;

(defun gnus-agent-save-active (method)
  (when (gnus-agent-method-p method)
    (let* ((gnus-command-method method)
	   (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
	   (file (gnus-agent-lib-file "active")))
      (gnus-active-to-gnus-format nil new)
      (gnus-agent-write-active file new)
      (erase-buffer)
      (nnheader-insert-file-contents file))))

(defun gnus-agent-write-active (file new)
    (gnus-make-directory (file-name-directory file))
    (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
      ;; The hashtable contains real names of groups.  However, do NOT
      ;; add the foreign server prefix as gnus-active-to-gnus-format
      ;; will add it while reading the file.
      (gnus-write-active-file file new nil)))

(defun gnus-agent-possibly-alter-active (group active &optional info)
  "Possibly expand a group's active range to include articles
downloaded into the agent."
  (let* ((gnus-command-method (or gnus-command-method
                                  (gnus-find-method-for-group group))))
    (when (gnus-agent-method-p gnus-command-method)
      (let* ((local (gnus-agent-get-local group))
             (active-min (or (car active) 0))
             (active-max (or (cdr active) 0))
             (agent-min (or (car local) active-min))
             (agent-max (or (cdr local) active-max)))

        (when (< agent-min active-min)
          (setcar active agent-min))

        (when (> agent-max active-max)
          (setcdr active agent-max))

        (when (and info (< agent-max (- active-min 100)))
          ;; I'm expanding the active range by such a large amount
          ;; that there is a gap of more than 100 articles between the
          ;; last article known to the agent and the first article
          ;; currently available on the server.  This gap contains
          ;; articles that have been lost, mark them as read so that
          ;; gnus doesn't waste resources trying to fetch them.

          ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
          ;; want to modify the local file everytime someone restarts
          ;; gnus.  The small gap will cause a tiny performance hit
          ;; when gnus tries, and fails, to retrieve the articles.
          ;; Still that should be smaller than opening a buffer,
          ;; printing this list to the buffer, and then writing it to a
          ;; file.

          (let ((read (gnus-info-read info)))
            (gnus-info-set-read 
             info 
             (gnus-range-add 
              read 
              (list (cons (1+ agent-max) 
                          (1- active-min))))))

          ;; Lie about the agent's local range for this group to
          ;; disable the set read each time this server is opened.
          ;; NOTE: Opening this group will restore the valid local
          ;; range but it will also expand the local range to
          ;; incompass the new active range.
          (gnus-agent-set-local group agent-min (1- active-min)))))))

(defun gnus-agent-save-group-info (method group active)
  "Update a single group's active range in the agent's copy of the server's active file."
  (when (gnus-agent-method-p method)
    (let* ((gnus-command-method method)
	   (coding-system-for-write nnheader-file-coding-system)
	   (file-name-coding-system nnmail-pathname-coding-system)
	   (file (gnus-agent-lib-file "active"))
	   oactive-min oactive-max)
      (gnus-make-directory (file-name-directory file))
      (with-temp-file file
	;; Emacs got problem to match non-ASCII group in multibyte buffer.
	(mm-disable-multibyte)
	(when (file-exists-p file)
	  (nnheader-insert-file-contents file)

          (goto-char (point-min))
          (when (re-search-forward
                 (concat "^" (regexp-quote group) " ") nil t)
            (save-excursion
              (setq oactive-max (read (current-buffer)) ;; max
                    oactive-min (read (current-buffer)))) ;; min
            (gnus-delete-line)))
	(insert (format "%S %d %d y\n" (intern group)
			(max (or oactive-max (cdr active)) (cdr active))
                        (min (or oactive-min (car active)) (car active))))
	(goto-char (point-max))
	(while (search-backward "\\." nil t)
	  (delete-char 1))))))

(defun gnus-agent-group-path (group)
  "Translate GROUP into a file name."

  ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
  ;; The two methods must be kept synchronized, which is why
  ;; gnus-agent-group-pathname was added.

  (setq group
        (nnheader-translate-file-chars
         (nnheader-replace-duplicate-chars-in-string
          (nnheader-replace-chars-in-string 
           (gnus-group-real-name group)
           ?/ ?_)
          ?. ?_)))
  (if (or nnmail-use-long-file-names
          (file-directory-p (expand-file-name group (gnus-agent-directory))))
      group
    (mm-encode-coding-string
     (nnheader-replace-chars-in-string group ?. ?/)
     nnmail-pathname-coding-system)))

(defun gnus-agent-group-pathname (group)
  "Translate GROUP into a file name."
  ;; nnagent uses nnmail-group-pathname to read articles while
  ;; unplugged.  The agent must, therefore, use the same directory
  ;; while plugged.
  (let ((gnus-command-method (or gnus-command-method
                                 (gnus-find-method-for-group group))))
    (nnmail-group-pathname (gnus-group-real-name group) (gnus-agent-directory))))

(defun gnus-agent-get-function (method)
  (if (gnus-online method)
      (car method)
    (require 'nnagent)
    'nnagent))

(defun gnus-agent-covered-methods ()
  "Return the subset of methods that are covered by the agent."
  (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))

;;; History functions

(defun gnus-agent-history-buffer ()
  (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))

(defun gnus-agent-open-history ()
  (save-excursion
    (push (cons (gnus-agent-method)
		(set-buffer (gnus-get-buffer-create
			     (format " *Gnus agent %s history*"
				     (gnus-agent-method)))))
	  gnus-agent-history-buffers)
    (mm-disable-multibyte) ;; everything is binary
    (erase-buffer)
    (insert "\n")
    (let ((file (gnus-agent-lib-file "history")))
      (when (file-exists-p file)
	(nnheader-insert-file-contents file))
      (set (make-local-variable 'gnus-agent-file-name) file))))

(defun gnus-agent-close-history ()
  (when (gnus-buffer-live-p gnus-agent-current-history)
    (kill-buffer gnus-agent-current-history)
    (setq gnus-agent-history-buffers
	  (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
		gnus-agent-history-buffers))))

;;;
;;; Fetching
;;;

(defun gnus-agent-fetch-articles (group articles)
  "Fetch ARTICLES from GROUP and put them into the Agent."
  (when articles
    (gnus-agent-load-alist group)
    (let* ((alist   gnus-agent-article-alist)
           (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
           (selected-sets (list nil))
           (current-set-size 0)
           article
           header-number)
      ;; Check each article
      (while (setq article (pop articles))
        ;; Skip alist entries preceeding this article
        (while (> article (or (caar alist) (1+ article)))
          (setq alist (cdr alist)))

        ;; Prune off articles that we have already fetched.
        (unless (and (eq article (caar alist))
                     (cdar alist))
          ;; Skip headers preceeding this article
          (while (> article 
                    (setq header-number
                          (let* ((header (car headers)))
                            (if header
                                (mail-header-number header)
                              (1+ article)))))
            (setq headers (cdr headers)))

          ;; Add this article to the current set
          (setcar selected-sets (cons article (car selected-sets)))

          ;; Update the set size, when the set is too large start a
          ;; new one.  I do this after adding the article as I want at
          ;; least one article in each set.
          (when (< gnus-agent-max-fetch-size
                   (setq current-set-size
			 (+ current-set-size
			    (if (= header-number article)
                                (let ((char-size (mail-header-chars
                                                  (car headers))))
                                  (if (<= char-size 0)
                                      ;; The char size was missing/invalid,
                                      ;; assume a worst-case situation of
                                      ;; 65 char/line.  If the line count
                                      ;; is missing, arbitrarily assume a
                                      ;; size of 1000 characters.
                                    (max (* 65 (mail-header-lines
                                                (car headers)))
                                         1000)
                                    char-size))
			      0))))
            (setcar selected-sets (nreverse (car selected-sets)))
            (setq selected-sets (cons nil selected-sets)
                  current-set-size 0))))

      (when (or (cdr selected-se
Results 1 - 1
Help - FTP Sites List - Software Dir.
Searching half a billion files worldwide
© 1997-2008 IT MARUHN