1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
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)
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.
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., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
37 (if (featurep 'xemacs)
43 (autoload 'gnus-server-update-server "gnus-srvr")
44 (autoload 'gnus-agent-customize-category "gnus-cus")
47 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
48 "Where the Gnus agent will store its files."
52 (defcustom gnus-agent-plugged-hook nil
53 "Hook run when plugging into the network."
57 (defcustom gnus-agent-unplugged-hook nil
58 "Hook run when unplugging from the network."
62 (defcustom gnus-agent-fetched-hook nil
63 "Hook run when finished fetching articles."
68 (defcustom gnus-agent-handle-level gnus-level-subscribed
69 "Groups on levels higher than this variable will be ignored by the Agent."
73 (defcustom gnus-agent-expire-days 7
74 "Read articles older than this will be expired.
75 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
77 :type '(number :tag "days"))
79 (defcustom gnus-agent-expire-all nil
80 "If non-nil, also expire unread, ticked and dormant articles.
81 If nil, only read articles will be expired."
85 (defcustom gnus-agent-group-mode-hook nil
86 "Hook run in Agent group minor modes."
90 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
91 (when (featurep 'xemacs)
92 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
94 (defcustom gnus-agent-summary-mode-hook nil
95 "Hook run in Agent summary minor modes."
99 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
100 (when (featurep 'xemacs)
101 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
103 (defcustom gnus-agent-server-mode-hook nil
104 "Hook run in Agent summary minor modes."
108 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
109 (when (featurep 'xemacs)
110 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
112 (defcustom gnus-agent-confirmation-function 'y-or-n-p
113 "Function to confirm when error happens."
118 (defcustom gnus-agent-synchronize-flags t
119 "Indicate if flags are synchronized when you plug in.
120 If this is `ask' the hook will query the user."
121 ;; If the default switches to something else than nil, then the function
122 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
124 :type '(choice (const :tag "Always" t)
125 (const :tag "Never" nil)
126 (const :tag "Ask" ask))
129 (defcustom gnus-agent-go-online 'ask
130 "Indicate if offline servers go online when you plug in.
131 If this is `ask' the hook will query the user."
133 :type '(choice (const :tag "Always" t)
134 (const :tag "Never" nil)
135 (const :tag "Ask" ask))
138 (defcustom gnus-agent-mark-unread-after-downloaded t
139 "Indicate whether to mark articles unread after downloaded."
144 (defcustom gnus-agent-download-marks '(download)
145 "Marks for downloading."
147 :type '(repeat (symbol :tag "Mark"))
150 (defcustom gnus-agent-consider-all-articles nil
151 "When non-nil, the agent will let the agent predicate decide
152 whether articles need to be downloaded or not, for all articles. When
153 nil, the default, the agent will only let the predicate decide
154 whether unread articles are downloaded or not. If you enable this,
155 groups with large active ranges may open slower and you may also want
156 to look into the agent expiry settings to block the expiration of
157 read articles as they would just be downloaded again."
162 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
163 "Chunk size for `gnus-agent-fetch-session'.
164 The function will split its article fetches into chunks smaller than
170 (defcustom gnus-agent-enable-expiration 'ENABLE
171 "The default expiration state for each group.
172 When set to ENABLE, the default, `gnus-agent-expire' will expire old
173 contents from a group's local storage. This value may be overridden
174 to disable expiration in specific categories, topics, and groups. Of
175 course, you could change gnus-agent-enable-expiration to DISABLE then
176 enable expiration per categories, topics, and groups."
179 :type '(radio (const :format "Enable " ENABLE)
180 (const :format "Disable " DISABLE)))
182 (defcustom gnus-agent-expire-unagentized-dirs t
183 "*Whether expiration should expire in unagentized directories.
184 Have gnus-agent-expire scan the directories under
185 \(gnus-agent-directory) for groups that are no longer agentized.
186 When found, offer to remove them."
191 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
192 "Initially, all servers from these methods are agentized.
193 The user may remove or add servers using the Server buffer.
194 See Info node `(gnus)Server Buffer'."
196 :type '(repeat symbol)
199 (defcustom gnus-agent-queue-mail t
200 "Whether and when outgoing mail should be queued by the agent.
201 When `always', always queue outgoing mail. When nil, never
202 queue. Otherwise, queue if and only if unplugged."
205 :type '(radio (const :format "Always" always)
206 (const :format "Never" nil)
207 (const :format "When unplugged" t)))
209 (defcustom gnus-agent-prompt-send-queue nil
210 "If non-nil, `gnus-group-send-queue' will prompt if called when
216 (defcustom gnus-agent-article-alist-save-format 1
217 "Indicates whether to use compression(2), versus no
218 compression(1), when writing agentview files. The compressed
219 files do save space but load times are 6-7 times higher. A group
220 must be opened then closed for the agentview to be updated using
222 ;; Wouldn't symbols instead numbers be nicer? --rsteib
225 :type '(radio (const :format "Compressed" 2)
226 (const :format "Uncompressed" 1)))
228 ;;; Internal variables
230 (defvar gnus-agent-history-buffers nil)
231 (defvar gnus-agent-buffer-alist nil)
232 (defvar gnus-agent-article-alist nil
233 "An assoc list identifying the articles whose headers have been fetched.
234 If successfully fetched, these headers will be stored in the group's overview
235 file. The key of each assoc pair is the article ID, the value of each assoc
236 pair is a flag indicating whether the identified article has been downloaded
237 \(gnus-agent-fetch-articles sets the value to the day of the download).
239 1) The last element of this list can not be expired as some
240 routines (for example, get-agent-fetch-headers) use the last
241 value to track which articles have had their headers retrieved.
242 2) The function `gnus-agent-regenerate' may destructively modify the value.")
243 (defvar gnus-agent-group-alist nil)
244 (defvar gnus-category-alist nil)
245 (defvar gnus-agent-current-history nil)
246 (defvar gnus-agent-overview-buffer nil)
247 (defvar gnus-category-predicate-cache nil)
248 (defvar gnus-category-group-cache nil)
249 (defvar gnus-agent-spam-hashtb nil)
250 (defvar gnus-agent-file-name nil)
251 (defvar gnus-agent-send-mail-function nil)
252 (defvar gnus-agent-file-coding-system 'raw-text)
253 (defvar gnus-agent-file-loading-cache nil)
256 (defvar gnus-headers)
263 (defun gnus-open-agent ()
265 (gnus-agent-read-servers)
267 (gnus-agent-create-buffer)
268 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
269 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
270 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
272 (defun gnus-agent-create-buffer ()
273 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
275 (setq gnus-agent-overview-buffer
276 (gnus-get-buffer-create " *Gnus agent overview*"))
277 (with-current-buffer gnus-agent-overview-buffer
278 (mm-enable-multibyte))
281 (gnus-add-shutdown 'gnus-close-agent 'gnus)
283 (defun gnus-close-agent ()
284 (setq gnus-category-predicate-cache nil
285 gnus-category-group-cache nil
286 gnus-agent-spam-hashtb nil)
287 (gnus-kill-buffer gnus-agent-overview-buffer))
290 ;;; Utility functions
293 (defun gnus-agent-read-file (file)
294 "Load FILE and do a `read' there."
297 (nnheader-insert-file-contents file)
298 (goto-char (point-min))
299 (read (current-buffer)))))
301 (defsubst gnus-agent-method ()
302 (concat (symbol-name (car gnus-command-method)) "/"
303 (if (equal (cadr gnus-command-method) "")
305 (cadr gnus-command-method))))
307 (defsubst gnus-agent-directory ()
308 "The name of the Gnus agent directory."
309 (nnheader-concat gnus-agent-directory
310 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
312 (defun gnus-agent-lib-file (file)
313 "The full name of the Gnus agent library FILE."
314 (expand-file-name file
315 (file-name-as-directory
316 (expand-file-name "agent.lib" (gnus-agent-directory)))))
318 (defun gnus-agent-cat-set-property (category property value)
320 (setcdr (or (assq property category)
321 (let ((cell (cons property nil)))
322 (setcdr category (cons cell (cdr category)))
324 (let ((category category))
325 (while (cond ((eq property (caadr category))
326 (setcdr category (cddr category))
329 (setq category (cdr category)))))))
333 (defmacro gnus-agent-cat-defaccessor (name prop-name)
334 "Define accessor and setter methods for manipulating a list of the form
335 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
336 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
337 manipulated as follows:
338 (func LIST): Returns VALUE1
339 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
340 `(progn (defmacro ,name (category)
341 (list (quote cdr) (list (quote assq)
342 (quote (quote ,prop-name)) category)))
344 (define-setf-method ,name (category)
345 (let* ((--category--temp-- (make-symbol "--category--"))
346 (--value--temp-- (make-symbol "--value--")))
347 (list (list --category--temp--) ; temporary-variables
348 (list category) ; value-forms
349 (list --value--temp--) ; store-variables
350 (let* ((category --category--temp--) ; store-form
351 (value --value--temp--))
352 (list (quote gnus-agent-cat-set-property)
354 (quote (quote ,prop-name))
356 (list (quote ,name) --category--temp--) ; access-form
360 (defmacro gnus-agent-cat-name (category)
363 (gnus-agent-cat-defaccessor
364 gnus-agent-cat-days-until-old agent-days-until-old)
365 (gnus-agent-cat-defaccessor
366 gnus-agent-cat-enable-expiration agent-enable-expiration)
367 (gnus-agent-cat-defaccessor
368 gnus-agent-cat-groups agent-groups)
369 (gnus-agent-cat-defaccessor
370 gnus-agent-cat-high-score agent-high-score)
371 (gnus-agent-cat-defaccessor
372 gnus-agent-cat-length-when-long agent-long-article)
373 (gnus-agent-cat-defaccessor
374 gnus-agent-cat-length-when-short agent-short-article)
375 (gnus-agent-cat-defaccessor
376 gnus-agent-cat-low-score agent-low-score)
377 (gnus-agent-cat-defaccessor
378 gnus-agent-cat-predicate agent-predicate)
379 (gnus-agent-cat-defaccessor
380 gnus-agent-cat-score-file agent-score)
381 (gnus-agent-cat-defaccessor
382 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
385 ;; This form is equivalent to defsetf except that it calls make-symbol
386 ;; whereas defsetf calls gensym (Using gensym creates a run-time
387 ;; dependency on the CL library).
390 (define-setf-method gnus-agent-cat-groups (category)
391 (let* ((--category--temp-- (make-symbol "--category--"))
392 (--groups--temp-- (make-symbol "--groups--")))
393 (list (list --category--temp--)
395 (list --groups--temp--)
396 (let* ((category --category--temp--)
397 (groups --groups--temp--))
398 (list (quote gnus-agent-set-cat-groups) category groups))
399 (list (quote gnus-agent-cat-groups) --category--temp--))))
402 (defun gnus-agent-set-cat-groups (category groups)
403 (unless (eq groups 'ignore)
405 (old-g (gnus-agent-cat-groups category)))
406 (cond ((eq new-g old-g)
407 ;; gnus-agent-add-group is fiddling with the group
408 ;; list. Still, Im done.
411 ((eq new-g (cdr old-g))
412 ;; gnus-agent-add-group is fiddling with the group list
413 (setcdr (or (assq 'agent-groups category)
414 (let ((cell (cons 'agent-groups nil)))
415 (setcdr category (cons cell (cdr category)))
418 (let ((groups groups))
420 (let* ((group (pop groups))
421 (old-category (gnus-group-category group)))
422 (if (eq category old-category)
424 (setf (gnus-agent-cat-groups old-category)
425 (delete group (gnus-agent-cat-groups
427 ;; Purge cache as preceeding loop invalidated it.
428 (setq gnus-category-group-cache nil))
430 (setcdr (or (assq 'agent-groups category)
431 (let ((cell (cons 'agent-groups nil)))
432 (setcdr category (cons cell (cdr category)))
435 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
436 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
438 ;;; Fetching setup functions.
440 (defun gnus-agent-start-fetch ()
441 "Initialize data structures for efficient fetching."
442 (gnus-agent-create-buffer))
444 (defun gnus-agent-stop-fetch ()
445 "Save all data structures and clean up."
446 (setq gnus-agent-spam-hashtb nil)
448 (set-buffer nntp-server-buffer)
451 (defmacro gnus-agent-with-fetch (&rest forms)
454 (let ((gnus-agent-fetching t))
455 (gnus-agent-start-fetch)
457 (gnus-agent-stop-fetch)))
459 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
460 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
462 (defmacro gnus-agent-append-to-list (tail value)
463 `(setq ,tail (setcdr ,tail (cons ,value nil))))
465 (defmacro gnus-agent-message (level &rest args)
466 `(if (<= ,level gnus-verbose)
473 (defvar gnus-agent-mode-hook nil
474 "Hook run when installing agent mode.")
476 (defvar gnus-agent-mode nil)
477 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
479 (defun gnus-agent-mode ()
480 "Minor mode for providing a agent support in Gnus buffers."
481 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
482 (symbol-name major-mode))
483 (match-string 1 (symbol-name major-mode))))
484 (mode (intern (format "gnus-agent-%s-mode" buffer))))
485 (set (make-local-variable 'gnus-agent-mode) t)
487 (set (make-local-variable mode) t)
489 (when (gnus-visual-p 'agent-menu 'menu)
490 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
491 (unless (assq 'gnus-agent-mode minor-mode-alist)
492 (push gnus-agent-mode-status minor-mode-alist))
493 (unless (assq mode minor-mode-map-alist)
494 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
496 minor-mode-map-alist))
497 (when (eq major-mode 'gnus-group-mode)
498 (let ((init-plugged gnus-plugged)
499 (gnus-agent-go-online nil))
500 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
501 ;; Therefore, make certain that the current value does not
502 ;; match the desired initial value.
503 (setq gnus-plugged :unknown)
504 (gnus-agent-toggle-plugged init-plugged)))
505 (gnus-run-hooks 'gnus-agent-mode-hook
506 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
508 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
509 (gnus-define-keys gnus-agent-group-mode-map
510 "Ju" gnus-agent-fetch-groups
511 "Jc" gnus-enter-category-buffer
512 "Jj" gnus-agent-toggle-plugged
513 "Js" gnus-agent-fetch-session
514 "JY" gnus-agent-synchronize-flags
515 "JS" gnus-group-send-queue
516 "Ja" gnus-agent-add-group
517 "Jr" gnus-agent-remove-group
518 "Jo" gnus-agent-toggle-group-plugged)
520 (defun gnus-agent-group-make-menu-bar ()
521 (unless (boundp 'gnus-agent-group-menu)
523 gnus-agent-group-menu gnus-agent-group-mode-map ""
525 ["Toggle plugged" gnus-agent-toggle-plugged t]
526 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
527 ["List categories" gnus-enter-category-buffer t]
528 ["Add (current) group to category" gnus-agent-add-group t]
529 ["Remove (current) group from category" gnus-agent-remove-group t]
530 ["Send queue" gnus-group-send-queue gnus-plugged]
532 ["All" gnus-agent-fetch-session gnus-plugged]
533 ["Group" gnus-agent-fetch-group gnus-plugged])
534 ["Synchronize flags" gnus-agent-synchronize-flags t]
537 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
538 (gnus-define-keys gnus-agent-summary-mode-map
539 "Jj" gnus-agent-toggle-plugged
540 "Ju" gnus-agent-summary-fetch-group
541 "JS" gnus-agent-fetch-group
542 "Js" gnus-agent-summary-fetch-series
543 "J#" gnus-agent-mark-article
544 "J\M-#" gnus-agent-unmark-article
545 "@" gnus-agent-toggle-mark
546 "Jc" gnus-agent-catchup)
548 (defun gnus-agent-summary-make-menu-bar ()
549 (unless (boundp 'gnus-agent-summary-menu)
551 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
553 ["Toggle plugged" gnus-agent-toggle-plugged t]
554 ["Mark as downloadable" gnus-agent-mark-article t]
555 ["Unmark as downloadable" gnus-agent-unmark-article t]
556 ["Toggle mark" gnus-agent-toggle-mark t]
557 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
558 ["Catchup undownloaded" gnus-agent-catchup t]))))
560 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
561 (gnus-define-keys gnus-agent-server-mode-map
562 "Jj" gnus-agent-toggle-plugged
563 "Ja" gnus-agent-add-server
564 "Jr" gnus-agent-remove-server)
566 (defun gnus-agent-server-make-menu-bar ()
567 (unless (boundp 'gnus-agent-server-menu)
569 gnus-agent-server-menu gnus-agent-server-mode-map ""
571 ["Toggle plugged" gnus-agent-toggle-plugged t]
572 ["Add" gnus-agent-add-server t]
573 ["Remove" gnus-agent-remove-server t]))))
575 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
576 (if (and (fboundp 'propertize)
577 (fboundp 'make-mode-line-mouse-map))
578 (propertize string 'local-map
579 (make-mode-line-mouse-map mouse-button mouse-func)
580 'mouse-face 'mode-line-highlight)
583 (defun gnus-agent-toggle-plugged (set-to)
584 "Toggle whether Gnus is unplugged or not."
585 (interactive (list (not gnus-plugged)))
586 (cond ((eq set-to gnus-plugged)
589 (setq gnus-plugged set-to)
590 (gnus-run-hooks 'gnus-agent-plugged-hook)
591 (setcar (cdr gnus-agent-mode-status)
592 (gnus-agent-make-mode-line-string " Plugged"
594 'gnus-agent-toggle-plugged))
595 (gnus-agent-go-online gnus-agent-go-online)
596 (gnus-agent-possibly-synchronize-flags))
598 (gnus-agent-close-connections)
599 (setq gnus-plugged set-to)
600 (gnus-run-hooks 'gnus-agent-unplugged-hook)
601 (setcar (cdr gnus-agent-mode-status)
602 (gnus-agent-make-mode-line-string " Unplugged"
604 'gnus-agent-toggle-plugged))))
605 (set-buffer-modified-p t))
607 (defmacro gnus-agent-while-plugged (&rest body)
608 `(let ((original-gnus-plugged gnus-plugged))
610 (progn (gnus-agent-toggle-plugged t)
612 (gnus-agent-toggle-plugged original-gnus-plugged))))
614 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
615 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
617 (defun gnus-agent-close-connections ()
618 "Close all methods covered by the Gnus agent."
619 (let ((methods (gnus-agent-covered-methods)))
621 (gnus-close-server (pop methods)))))
624 (defun gnus-unplugged ()
625 "Start Gnus unplugged."
627 (setq gnus-plugged nil)
631 (defun gnus-plugged ()
632 "Start Gnus plugged."
634 (setq gnus-plugged t)
638 (defun gnus-slave-unplugged (&optional arg)
639 "Read news as a slave unplugged."
641 (setq gnus-plugged nil)
642 (gnus arg nil 'slave))
645 (defun gnus-agentize ()
646 "Allow Gnus to be an offline newsreader.
648 The gnus-agentize function is now called internally by gnus when
649 gnus-agent is set. If you wish to avoid calling gnus-agentize,
650 customize gnus-agent to nil.
652 This will modify the `gnus-setup-news-hook', and
653 `message-send-mail-real-function' variables, and install the Gnus agent
654 minor mode in all Gnus buffers."
657 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
658 (unless gnus-agent-send-mail-function
659 (setq gnus-agent-send-mail-function
660 (or message-send-mail-real-function
661 (function (lambda () (funcall message-send-mail-function))))
662 message-send-mail-real-function 'gnus-agent-send-mail))
664 ;; If the servers file doesn't exist, auto-agentize some servers and
665 ;; save the servers file so this auto-agentizing isn't invoked
667 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
668 (gnus-message 3 "First time agent user, agentizing remote groups...")
670 (lambda (server-or-method)
671 (let ((method (gnus-server-to-method server-or-method)))
672 (when (memq (car method)
673 gnus-agent-auto-agentize-methods)
674 (push (gnus-method-to-server method)
675 gnus-agent-covered-methods)
676 (setq gnus-agent-method-p-cache nil))))
677 (cons gnus-select-method gnus-secondary-select-methods))
678 (gnus-agent-write-servers)))
680 (defun gnus-agent-queue-setup (&optional group-name)
681 "Make sure the queue group exists.
682 Optional arg GROUP-NAME allows to specify another group."
683 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
685 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
686 (let ((gnus-level-default-subscribed 1))
687 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
689 (gnus-group-set-parameter
690 (format "nndraft:%s" (or group-name "queue"))
691 'gnus-dummy '((gnus-draft-mode)))))
693 (defun gnus-agent-send-mail ()
694 (if (or (not gnus-agent-queue-mail)
695 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
696 (funcall gnus-agent-send-mail-function)
697 (goto-char (point-min))
699 (concat "^" (regexp-quote mail-header-separator) "\n"))
701 (gnus-agent-insert-meta-information 'mail)
702 (gnus-request-accept-article "nndraft:queue" nil t t)))
704 (defun gnus-agent-insert-meta-information (type &optional method)
705 "Insert meta-information into the message that says how it's to be posted.
706 TYPE can be either `mail' or `news'. If the latter, then METHOD can
709 (message-remove-header gnus-agent-meta-information-header)
710 (goto-char (point-min))
711 (insert gnus-agent-meta-information-header ": "
712 (symbol-name type) " " (format "%S" method)
715 (while (search-backward "\n" nil t)
716 (replace-match "\\n" t t))))
718 (defun gnus-agent-restore-gcc ()
719 "Restore GCC field from saved header."
721 (goto-char (point-min))
722 (while (re-search-forward
723 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
724 (replace-match "Gcc:" 'fixedcase))))
726 (defun gnus-agent-any-covered-gcc ()
728 (message-narrow-to-headers)
729 (let* ((gcc (mail-fetch-field "gcc" nil t))
731 (mapcar 'gnus-inews-group-method
732 (message-unquote-tokens
733 (message-tokenize-header
736 (while (and (not covered) methods)
737 (setq covered (gnus-agent-method-p (car methods))
738 methods (cdr methods)))
742 (defun gnus-agent-possibly-save-gcc ()
743 "Save GCC if Gnus is unplugged."
744 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
746 (goto-char (point-min))
747 (let ((case-fold-search t))
748 (while (re-search-forward "^gcc:" nil t)
749 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
751 (defun gnus-agent-possibly-do-gcc ()
752 "Do GCC if Gnus is plugged."
753 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
754 (gnus-inews-do-gcc)))
757 ;;; Group mode commands
760 (defun gnus-agent-fetch-groups (n)
761 "Put all new articles in the current groups into the Agent."
764 (error "Groups can't be fetched when Gnus is unplugged"))
765 (gnus-group-iterate n 'gnus-agent-fetch-group))
767 (defun gnus-agent-fetch-group (&optional group)
768 "Put all new articles in GROUP into the Agent."
769 (interactive (list (gnus-group-group-name)))
770 (setq group (or group gnus-newsgroup-name))
772 (error "No group on the current line"))
774 (gnus-agent-while-plugged
775 (let ((gnus-command-method (gnus-find-method-for-group group)))
776 (gnus-agent-with-fetch
777 (gnus-agent-fetch-group-1 group gnus-command-method)
778 (gnus-message 5 "Fetching %s...done" group)))))
780 (defun gnus-agent-add-group (category arg)
781 "Add the current group to an agent category."
787 (mapcar (lambda (cat) (list (symbol-name (car cat))))
791 (let ((cat (assq category gnus-category-alist))
793 (gnus-group-iterate arg
795 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
796 (setf (gnus-agent-cat-groups c)
797 (delete group (gnus-agent-cat-groups c))))
798 (push group groups)))
799 (setf (gnus-agent-cat-groups cat)
800 (nconc (gnus-agent-cat-groups cat) groups))
801 (gnus-category-write)))
803 (defun gnus-agent-remove-group (arg)
804 "Remove the current group from its agent category, if any."
807 (gnus-group-iterate arg
809 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
810 (setf (gnus-agent-cat-groups c)
811 (delete group (gnus-agent-cat-groups c))))))
812 (gnus-category-write)))
814 (defun gnus-agent-synchronize-flags ()
815 "Synchronize unplugged flags with servers."
818 (dolist (gnus-command-method (gnus-agent-covered-methods))
819 (when (file-exists-p (gnus-agent-lib-file "flags"))
820 (gnus-agent-synchronize-flags-server gnus-command-method)))))
822 (defun gnus-agent-possibly-synchronize-flags ()
823 "Synchronize flags according to `gnus-agent-synchronize-flags'."
826 (dolist (gnus-command-method (gnus-agent-covered-methods))
827 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
828 (eq (gnus-server-status gnus-command-method) 'ok))
829 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
831 (defun gnus-agent-synchronize-flags-server (method)
832 "Synchronize flags set when unplugged for server."
833 (let ((gnus-command-method method)
835 (when (file-exists-p (gnus-agent-lib-file "flags"))
836 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
838 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
839 (cond ((null gnus-plugged)
841 1 "You must be plugged to synchronize flags with server %s"
842 (nth 1 gnus-command-method)))
843 ((null (gnus-check-server gnus-command-method))
845 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
850 (eval (read (current-buffer)))
851 (delete-region bgn (point))))
853 (delete-file (gnus-agent-lib-file "flags")))
855 (let ((file (gnus-agent-lib-file "flags")))
856 (write-region (point-min) (point-max)
857 (gnus-agent-lib-file "flags") nil 'silent)
858 (error "Couldn't set flags from file %s due to %s"
859 file (error-message-string err)))))))
862 (defun gnus-agent-possibly-synchronize-flags-server (method)
863 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
864 (when (or (and gnus-agent-synchronize-flags
865 (not (eq gnus-agent-synchronize-flags 'ask)))
866 (and (eq gnus-agent-synchronize-flags 'ask)
867 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
869 (gnus-agent-synchronize-flags-server method)))
872 (defun gnus-agent-rename-group (old-group new-group)
873 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
874 Always updates the agent, even when disabled, as the old agent
875 files would corrupt gnus when the agent was next enabled.
876 Depends upon the caller to determine whether group renaming is
878 (let* ((old-command-method (gnus-find-method-for-group old-group))
879 (old-path (directory-file-name
880 (let (gnus-command-method old-command-method)
881 (gnus-agent-group-pathname old-group))))
882 (new-command-method (gnus-find-method-for-group new-group))
883 (new-path (directory-file-name
884 (let (gnus-command-method new-command-method)
885 (gnus-agent-group-pathname new-group)))))
886 (gnus-rename-file old-path new-path t)
888 (let* ((old-real-group (gnus-group-real-name old-group))
889 (new-real-group (gnus-group-real-name new-group))
890 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
891 (gnus-agent-save-group-info old-command-method old-real-group nil)
892 (gnus-agent-save-group-info new-command-method new-real-group old-active)
894 (let ((old-local (gnus-agent-get-local old-group
895 old-real-group old-command-method)))
896 (gnus-agent-set-local old-group
898 old-real-group old-command-method)
899 (gnus-agent-set-local new-group
900 (car old-local) (cdr old-local)
901 new-real-group new-command-method)))))
904 (defun gnus-agent-delete-group (group)
905 "Delete fully-qualified GROUP.
906 Always updates the agent, even when disabled, as the old agent
907 files would corrupt gnus when the agent was next enabled.
908 Depends upon the caller to determine whether group deletion is
910 (let* ((command-method (gnus-find-method-for-group group))
911 (path (directory-file-name
912 (let (gnus-command-method command-method)
913 (gnus-agent-group-pathname group)))))
914 (gnus-delete-directory path)
916 (let* ((real-group (gnus-group-real-name group)))
917 (gnus-agent-save-group-info command-method real-group nil)
919 (let ((local (gnus-agent-get-local group
920 real-group command-method)))
921 (gnus-agent-set-local group
923 real-group command-method)))))
926 ;;; Server mode commands
929 (defun gnus-agent-add-server ()
930 "Enroll SERVER in the agent program."
932 (let* ((server (gnus-server-server-name))
933 (named-server (gnus-server-named-server))
935 (gnus-server-get-method nil server))))
937 (error "No server on the current line"))
939 (when (gnus-agent-method-p method)
940 (error "Server already in the agent program"))
942 (push named-server gnus-agent-covered-methods)
944 (setq gnus-agent-method-p-cache nil)
945 (gnus-server-update-server server)
946 (gnus-agent-write-servers)
947 (gnus-message 1 "Entered %s into the Agent" server)))
949 (defun gnus-agent-remove-server ()
950 "Remove SERVER from the agent program."
952 (let* ((server (gnus-server-server-name))
953 (named-server (gnus-server-named-server)))
955 (error "No server on the current line"))
957 (unless (member named-server gnus-agent-covered-methods)
958 (error "Server not in the agent program"))
960 (setq gnus-agent-covered-methods
961 (delete named-server gnus-agent-covered-methods)
962 gnus-agent-method-p-cache nil)
964 (gnus-server-update-server server)
965 (gnus-agent-write-servers)
966 (gnus-message 1 "Removed %s from the agent" server)))
968 (defun gnus-agent-read-servers ()
969 "Read the alist of covered servers."
970 (setq gnus-agent-covered-methods
971 (gnus-agent-read-file
972 (nnheader-concat gnus-agent-directory "lib/servers"))
973 gnus-agent-method-p-cache nil)
975 ;; I am called so early in start-up that I can not validate server
976 ;; names. When that is the case, I skip the validation. That is
977 ;; alright as the gnus startup code calls the validate methods
979 (if gnus-server-alist
980 (gnus-agent-read-servers-validate)))
982 (defun gnus-agent-read-servers-validate ()
983 (mapcar (lambda (server-or-method)
984 (let* ((server (if (stringp server-or-method)
986 (gnus-method-to-server server-or-method)))
987 (method (gnus-server-to-method server)))
989 (unless (member server gnus-agent-covered-methods)
990 (push server gnus-agent-covered-methods)
991 (setq gnus-agent-method-p-cache nil))
992 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
993 (prog1 gnus-agent-covered-methods
994 (setq gnus-agent-covered-methods nil))))
996 (defun gnus-agent-read-servers-validate-native (native-method)
997 (setq gnus-agent-covered-methods
998 (mapcar (lambda (method)
1000 (equal method native-method))
1002 method)) gnus-agent-covered-methods)))
1004 (defun gnus-agent-write-servers ()
1005 "Write the alist of covered servers."
1006 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1007 (let ((coding-system-for-write nnheader-file-coding-system)
1008 (file-name-coding-system nnmail-pathname-coding-system))
1009 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1010 (prin1 gnus-agent-covered-methods
1011 (current-buffer)))))
1014 ;;; Summary commands
1017 (defun gnus-agent-mark-article (n &optional unmark)
1018 "Mark the next N articles as downloadable.
1019 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1020 the mark instead. The difference between N and the actual number of
1021 articles marked is returned."
1023 (let ((backward (< n 0))
1028 (gnus-summary-set-agent-mark
1029 (gnus-summary-article-number) unmark)
1030 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1033 (gnus-message 7 "No more articles"))
1034 (gnus-summary-recenter)
1035 (gnus-summary-position-point)
1038 (defun gnus-agent-unmark-article (n)
1039 "Remove the downloadable mark from the next N articles.
1040 If N is negative, unmark backward instead. The difference between N and
1041 the actual number of articles unmarked is returned."
1043 (gnus-agent-mark-article n t))
1045 (defun gnus-agent-toggle-mark (n)
1046 "Toggle the downloadable mark from the next N articles.
1047 If N is negative, toggle backward instead. The difference between N and
1048 the actual number of articles toggled is returned."
1050 (gnus-agent-mark-article n 'toggle))
1052 (defun gnus-summary-set-agent-mark (article &optional unmark)
1053 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1054 When UNMARK is t, the article is unmarked. For any other value, the
1055 article's mark is toggled."
1056 (let ((unmark (cond ((eq nil unmark)
1061 (memq article gnus-newsgroup-downloadable)))))
1062 (when (gnus-summary-goto-subject article nil t)
1063 (gnus-summary-update-mark
1066 (setq gnus-newsgroup-downloadable
1067 (delq article gnus-newsgroup-downloadable))
1068 (gnus-article-mark article))
1069 (setq gnus-newsgroup-downloadable
1070 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1071 gnus-downloadable-mark)
1075 (defun gnus-agent-get-undownloaded-list ()
1076 "Construct list of articles that have not been downloaded."
1077 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1078 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1079 (gnus-agent-method-p gnus-command-method))
1080 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1081 (headers (sort (mapcar (lambda (h)
1082 (mail-header-number h))
1083 gnus-newsgroup-headers) '<))
1084 (cached (and gnus-use-cache gnus-newsgroup-cached))
1085 (undownloaded (list nil))
1086 (tail-undownloaded undownloaded)
1087 (unfetched (list nil))
1088 (tail-unfetched unfetched))
1089 (while (and alist headers)
1090 (let ((a (caar alist))
1093 ;; Ignore IDs in the alist that are not being
1094 ;; displayed in the summary.
1095 (setq alist (cdr alist)))
1097 ;; Headers that are not in the alist should be
1098 ;; fictious (see nnagent-retrieve-headers); they
1099 ;; imply that this article isn't in the agent.
1100 (gnus-agent-append-to-list tail-undownloaded h)
1101 (gnus-agent-append-to-list tail-unfetched h)
1102 (setq headers (cdr headers)))
1104 (setq alist (cdr alist))
1105 (setq headers (cdr headers))
1106 nil ; ignore already downloaded
1109 (setq alist (cdr alist))
1110 (setq headers (cdr headers))
1112 ;; This article isn't in the agent. Check to see
1113 ;; if it is in the cache. If it is, it's been
1115 (while (and cached (< (car cached) a))
1116 (setq cached (cdr cached)))
1117 (unless (equal a (car cached))
1118 (gnus-agent-append-to-list tail-undownloaded a))))))
1121 (let ((num (pop headers)))
1122 (gnus-agent-append-to-list tail-undownloaded num)
1123 (gnus-agent-append-to-list tail-unfetched num)))
1125 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1126 gnus-newsgroup-unfetched (cdr unfetched))))))
1128 (defun gnus-agent-catchup ()
1129 "Mark as read all unhandled articles.
1130 An article is unhandled if it is neither cached, nor downloaded, nor
1134 (let ((articles gnus-newsgroup-undownloaded))
1135 (when (or gnus-newsgroup-downloadable
1136 gnus-newsgroup-cached)
1137 (setq articles (gnus-sorted-ndifference
1138 (gnus-sorted-ndifference
1139 (gnus-copy-sequence articles)
1140 gnus-newsgroup-downloadable)
1141 gnus-newsgroup-cached)))
1144 (gnus-summary-mark-article
1145 (pop articles) gnus-catchup-mark)))
1146 (gnus-summary-position-point)))
1148 (defun gnus-agent-summary-fetch-series ()
1150 (when gnus-newsgroup-processable
1151 (setq gnus-newsgroup-downloadable
1152 (let* ((dl gnus-newsgroup-downloadable)
1153 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1154 (gnus-newsgroup-downloadable processable))
1155 (gnus-agent-summary-fetch-group)
1157 ;; For each article that I processed that is no longer
1158 ;; undownloaded, remove its processable mark.
1160 (mapc #'gnus-summary-remove-process-mark
1161 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1163 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1164 ;; updated the temporary gnus-newsgroup-downloadable to
1165 ;; remove each article successfully fetched. Now, I
1166 ;; update the real gnus-newsgroup-downloadable to only
1167 ;; include undownloaded articles.
1168 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1170 (defun gnus-agent-summary-fetch-group (&optional all)
1171 "Fetch the downloadable articles in the group.
1172 Optional arg ALL, if non-nil, means to fetch all articles."
1175 (if all gnus-newsgroup-articles
1176 gnus-newsgroup-downloadable))
1177 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1179 (gnus-agent-while-plugged
1181 (error "No articles to download"))
1182 (gnus-agent-with-fetch
1183 (setq gnus-newsgroup-undownloaded
1184 (gnus-sorted-ndifference
1185 gnus-newsgroup-undownloaded
1186 (setq fetched-articles
1187 (gnus-agent-fetch-articles
1188 gnus-newsgroup-name articles)))))
1190 (dolist (article articles)
1191 (let ((was-marked-downloadable
1192 (memq article gnus-newsgroup-downloadable)))
1193 (cond (gnus-agent-mark-unread-after-downloaded
1194 (setq gnus-newsgroup-downloadable
1195 (delq article gnus-newsgroup-downloadable))
1197 (gnus-summary-mark-article article gnus-unread-mark))
1198 (was-marked-downloadable
1199 (gnus-summary-set-agent-mark article t)))
1200 (when (gnus-summary-goto-subject article nil t)
1201 (gnus-summary-update-download-mark article))))))
1204 (defun gnus-agent-fetch-selected-article ()
1205 "Fetch the current article as it is selected.
1206 This can be added to `gnus-select-article-hook' or
1207 `gnus-mark-article-hook'."
1208 (let ((gnus-command-method gnus-current-select-method))
1209 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1210 (when (gnus-agent-fetch-articles
1212 (list gnus-current-article))
1213 (setq gnus-newsgroup-undownloaded
1214 (delq gnus-current-article gnus-newsgroup-undownloaded))
1215 (gnus-summary-update-download-mark gnus-current-article)))))
1218 ;;; Internal functions
1221 (defun gnus-agent-synchronize-group-flags (group actions server)
1222 "Update a plugged group by performing the indicated actions."
1223 (let* ((gnus-command-method (gnus-server-to-method server))
1225 ;; This initializer is required as gnus-request-set-mark
1226 ;; calls gnus-group-real-name to strip off the host name
1227 ;; before calling the backend. Now that the backend is
1228 ;; trying to call gnus-request-set-mark, I have to
1229 ;; reconstruct the original group name.
1230 (or (gnus-get-info group)
1232 (setq group (gnus-group-full-name
1233 group gnus-command-method))))))
1234 (gnus-request-set-mark group actions)
1237 (dolist (action actions)
1238 (let ((range (nth 0 action))
1239 (what (nth 1 action))
1240 (marks (nth 2 action)))
1241 (dolist (mark marks)
1242 (cond ((eq mark 'read)
1245 (funcall (if (eq what 'add)
1247 'gnus-remove-from-range)
1248 (gnus-info-read info)
1250 (gnus-get-unread-articles-in-group
1252 (gnus-active (gnus-info-group info))))
1253 ((memq mark '(tick))
1254 (let ((info-marks (assoc mark (gnus-info-marks info))))
1256 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1257 (setcdr info-marks (funcall (if (eq what 'add)
1259 'gnus-remove-from-range)
1263 ;;Marks can be synchronized at any time by simply toggling from
1264 ;;unplugged to plugged. If that is what is happening right now, make
1265 ;;sure that the group buffer is up to date.
1266 (when (gnus-buffer-live-p gnus-group-buffer)
1267 (gnus-group-update-group group t)))
1270 (defun gnus-agent-save-active (method)
1271 (when (gnus-agent-method-p method)
1272 (let* ((gnus-command-method method)
1273 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1274 (file (gnus-agent-lib-file "active")))
1275 (gnus-active-to-gnus-format nil new)
1276 (gnus-agent-write-active file new)
1278 (nnheader-insert-file-contents file))))
1280 (defun gnus-agent-write-active (file new)
1281 (gnus-make-directory (file-name-directory file))
1282 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1283 ;; The hashtable contains real names of groups. However, do NOT
1284 ;; add the foreign server prefix as gnus-active-to-gnus-format
1285 ;; will add it while reading the file.
1286 (gnus-write-active-file file new nil)))
1289 (defun gnus-agent-possibly-alter-active (group active &optional info)
1290 "Possibly expand a group's active range to include articles
1291 downloaded into the agent."
1292 (let* ((gnus-command-method (or gnus-command-method
1293 (gnus-find-method-for-group group))))
1294 (when (gnus-agent-method-p gnus-command-method)
1295 (let* ((local (gnus-agent-get-local group))
1296 (active-min (or (car active) 0))
1297 (active-max (or (cdr active) 0))
1298 (agent-min (or (car local) active-min))
1299 (agent-max (or (cdr local) active-max)))
1301 (when (< agent-min active-min)
1302 (setcar active agent-min))
1304 (when (> agent-max active-max)
1305 (setcdr active agent-max))
1307 (when (and info (< agent-max (- active-min 100)))
1308 ;; I'm expanding the active range by such a large amount
1309 ;; that there is a gap of more than 100 articles between the
1310 ;; last article known to the agent and the first article
1311 ;; currently available on the server. This gap contains
1312 ;; articles that have been lost, mark them as read so that
1313 ;; gnus doesn't waste resources trying to fetch them.
1315 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1316 ;; want to modify the local file everytime someone restarts
1317 ;; gnus. The small gap will cause a tiny performance hit
1318 ;; when gnus tries, and fails, to retrieve the articles.
1319 ;; Still that should be smaller than opening a buffer,
1320 ;; printing this list to the buffer, and then writing it to a
1323 (let ((read (gnus-info-read info)))
1328 (list (cons (1+ agent-max)
1329 (1- active-min))))))
1331 ;; Lie about the agent's local range for this group to
1332 ;; disable the set read each time this server is opened.
1333 ;; NOTE: Opening this group will restore the valid local
1334 ;; range but it will also expand the local range to
1335 ;; incompass the new active range.
1336 (gnus-agent-set-local group agent-min (1- active-min)))))))
1338 (defun gnus-agent-save-group-info (method group active)
1339 "Update a single group's active range in the agent's copy of the server's active file."
1340 (when (gnus-agent-method-p method)
1341 (let* ((gnus-command-method (or method gnus-command-method))
1342 (coding-system-for-write nnheader-file-coding-system)
1343 (file-name-coding-system nnmail-pathname-coding-system)
1344 (file (gnus-agent-lib-file "active"))
1345 oactive-min oactive-max)
1346 (gnus-make-directory (file-name-directory file))
1347 (with-temp-file file
1348 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1349 (mm-disable-multibyte)
1350 (when (file-exists-p file)
1351 (nnheader-insert-file-contents file)
1353 (goto-char (point-min))
1354 (when (re-search-forward
1355 (concat "^" (regexp-quote group) " ") nil t)
1357 (setq oactive-max (read (current-buffer)) ;; max
1358 oactive-min (read (current-buffer)))) ;; min
1359 (gnus-delete-line)))
1361 (insert (format "%S %d %d y\n" (intern group)
1362 (max (or oactive-max (cdr active)) (cdr active))
1363 (min (or oactive-min (car active)) (car active))))
1364 (goto-char (point-max))
1365 (while (search-backward "\\." nil t)
1366 (delete-char 1)))))))
1368 (defun gnus-agent-get-group-info (method group)
1369 "Get a single group's active range in the agent's copy of the server's active file."
1370 (when (gnus-agent-method-p method)
1371 (let* ((gnus-command-method (or method gnus-command-method))
1372 (coding-system-for-write nnheader-file-coding-system)
1373 (file-name-coding-system nnmail-pathname-coding-system)
1374 (file (gnus-agent-lib-file "active"))
1375 oactive-min oactive-max)
1376 (gnus-make-directory (file-name-directory file))
1378 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1379 (mm-disable-multibyte)
1380 (when (file-exists-p file)
1381 (nnheader-insert-file-contents file)
1383 (goto-char (point-min))
1384 (when (re-search-forward
1385 (concat "^" (regexp-quote group) " ") nil t)
1387 (setq oactive-max (read (current-buffer)) ;; max
1388 oactive-min (read (current-buffer))) ;; min
1389 (cons oactive-min oactive-max))))))))
1391 (defun gnus-agent-group-path (group)
1392 "Translate GROUP into a file name."
1394 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1395 ;; The two methods must be kept synchronized, which is why
1396 ;; gnus-agent-group-pathname was added.
1399 (nnheader-translate-file-chars
1400 (nnheader-replace-duplicate-chars-in-string
1401 (nnheader-replace-chars-in-string
1402 (gnus-group-real-name (gnus-group-decoded-name group))
1405 (if (or nnmail-use-long-file-names
1406 (file-directory-p (expand-file-name group (gnus-agent-directory))))
1408 (mm-encode-coding-string
1409 (nnheader-replace-chars-in-string group ?. ?/)
1410 nnmail-pathname-coding-system)))
1412 (defun gnus-agent-group-pathname (group)
1413 "Translate GROUP into a file name."
1414 ;; nnagent uses nnmail-group-pathname to read articles while
1415 ;; unplugged. The agent must, therefore, use the same directory
1417 (let ((gnus-command-method (or gnus-command-method
1418 (gnus-find-method-for-group group))))
1419 (nnmail-group-pathname (gnus-group-real-name
1420 (gnus-group-decoded-name group))
1421 (gnus-agent-directory))))
1423 (defun gnus-agent-get-function (method)
1424 (if (gnus-online method)
1429 (defun gnus-agent-covered-methods ()
1430 "Return the subset of methods that are covered by the agent."
1431 (delq nil (mapcar #'gnus-server-to-method gnus-agent-covered-methods)))
1433 ;;; History functions
1435 (defun gnus-agent-history-buffer ()
1436 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
1438 (defun gnus-agent-open-history ()
1440 (push (cons (gnus-agent-method)
1441 (set-buffer (gnus-get-buffer-create
1442 (format " *Gnus agent %s history*"
1443 (gnus-agent-method)))))
1444 gnus-agent-history-buffers)
1445 (mm-disable-multibyte) ;; everything is binary
1448 (let ((file (gnus-agent-lib-file "history")))
1449 (when (file-exists-p file)
1450 (nnheader-insert-file-contents file))
1451 (set (make-local-variable 'gnus-agent-file-name) file))))
1453 (defun gnus-agent-close-history ()
1454 (when (gnus-buffer-live-p gnus-agent-current-history)
1455 (kill-buffer gnus-agent-current-history)
1456 (setq gnus-agent-history-buffers
1457 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
1458 gnus-agent-history-buffers))))
1464 (defun gnus-agent-fetch-articles (group articles)
1465 "Fetch ARTICLES from GROUP and put them into the Agent."
1467 (gnus-agent-load-alist group)
1468 (let* ((alist gnus-agent-article-alist)
1469 (headers (if (< (length articles) 2) nil gnus-newsgroup-headers))
1470 (selected-sets (list nil))
1471 (current-set-size 0)
1474 ;; Check each article
1475 (while (setq article (pop articles))
1476 ;; Skip alist entries preceeding this article
1477 (while (> article (or (caar alist) (1+ article)))
1478 (setq alist (cdr alist)))
1480 ;; Prune off articles that we have already fetched.
1481 (unless (and (eq article (caar alist))
1483 ;; Skip headers preceeding this article
1486 (let* ((header (car headers)))
1488 (mail-header-number header)
1490 (setq headers (cdr headers)))
1492 ;; Add this article to the current set
1493 (setcar selected-sets (cons article (car selected-sets)))
1495 ;; Update the set size, when the set is too large start a
1496 ;; new one. I do this after adding the article as I want at
1497 ;; least one article in each set.
1498 (when (< gnus-agent-max-fetch-size
1499 (setq current-set-size
1501 (if (= header-number article)
1502 (let ((char-size (mail-header-chars
1504 (if (<= char-size 0)
1505 ;; The char size was missing/invalid,
1506 ;; assume a worst-case situation of
1507 ;; 65 char/line. If the line count
1508 ;; is missing, arbitrarily assume a
1509 ;; size of 1000 characters.
1510 (max (* 65 (mail-header-lines
1515 (setcar selected-sets (nreverse (car selected-sets)))
1516 (setq selected-sets (cons nil selected-sets)
1517 current-set-size 0))))
1519 (when (or (cdr selected-sets) (car selected-sets))
1520 (let* ((fetched-articles (list nil))
1521 (tail-fetched-articles fetched-articles)
1522 (dir (gnus-agent-group-pathname group))
1523 (date (time-to-days (current-time)))
1524 (case-fold-search t)
1527 (setcar selected-sets (nreverse (car selected-sets)))
1528 (setq selected-sets (nreverse selected-sets))
1530 (gnus-make-directory dir)
1531 (gnus-message 7 "Fetching articles for %s..." group)
1534 (while (setq articles (pop selected-sets))
1535 ;; Fetch the articles from the backend.
1536 (if (gnus-check-backend-function 'retrieve-articles group)
1537 (setq pos (gnus-retrieve-articles articles group))
1540 (while (setq article (pop articles))
1541 (gnus-message 10 "Fetching article %s for %s..."
1544 (gnus-backlog-request-article group article
1546 (gnus-request-article article group))
1547 (goto-char (point-max))
1548 (push (cons article (point)) pos)
1549 (insert-buffer-substring nntp-server-buffer)))
1551 nntp-server-buffer (point-min) (point-max))
1552 (setq pos (nreverse pos)))))
1553 ;; Then save these articles into the Agent.
1555 (set-buffer nntp-server-buffer)
1557 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1558 (goto-char (point-min))
1559 (unless (eobp) ;; Don't save empty articles.
1560 (when (search-forward "\n\n" nil t)
1561 (when (search-backward "\nXrefs: " nil t)
1562 ;; Handle cross posting.
1563 (goto-char (match-end 0)) ; move to end of header name
1564 (skip-chars-forward "^ ") ; skip server name
1565 (skip-chars-forward " ")
1567 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1568 (push (cons (buffer-substring (match-beginning 1)
1571 (buffer-substring (match-beginning 2)
1574 (goto-char (match-end 0)))
1575 (gnus-agent-crosspost crosses (caar pos) date)))
1576 (goto-char (point-min))
1577 (if (not (re-search-forward
1578 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1579 (setq id "No-Message-ID-in-article")
1580 (setq id (buffer-substring
1581 (match-beginning 1) (match-end 1))))
1582 (let ((coding-system-for-write
1583 gnus-agent-file-coding-system))
1584 (write-region (point-min) (point-max)
1585 (concat dir (number-to-string (caar pos)))
1588 (gnus-agent-append-to-list
1589 tail-fetched-articles (caar pos)))
1591 (setq pos (cdr pos)))))
1593 (gnus-agent-save-alist group (cdr fetched-articles) date)
1594 (gnus-message 7 ""))
1595 (cdr fetched-articles))))))
1597 (defun gnus-agent-unfetch-articles (group articles)
1598 "Delete ARTICLES that were fetched from GROUP into the agent."
1600 (gnus-agent-load-alist group)
1601 (let* ((alist (cons nil gnus-agent-article-alist))
1602 (articles (sort articles #'<))
1603 (next-possibility alist)
1604 (delete-this (pop articles)))
1605 (while (and (cdr next-possibility) delete-this)
1606 (let ((have-this (caar (cdr next-possibility))))
1607 (cond ((< delete-this have-this)
1608 (setq delete-this (pop articles)))
1609 ((= delete-this have-this)
1610 (let ((timestamp (cdar (cdr next-possibility))))
1612 (let* ((file-name (concat (gnus-agent-group-pathname group)
1613 (number-to-string have-this))))
1614 (delete-file file-name))))
1616 (setcdr next-possibility (cddr next-possibility)))
1618 (setq next-possibility (cdr next-possibility))))))
1619 (setq gnus-agent-article-alist (cdr alist))
1620 (gnus-agent-save-alist group))))
1622 (defun gnus-agent-crosspost (crosses article &optional date)
1623 (setq date (or date t))
1625 (let (gnus-agent-article-alist group alist beg end)
1627 (set-buffer gnus-agent-overview-buffer)
1628 (when (nnheader-find-nov-line article)
1631 (setq end (progn (forward-line 1) (point)))))
1633 (setq group (caar crosses))
1634 (unless (setq alist (assoc group gnus-agent-group-alist))
1635 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1636 gnus-agent-group-alist))
1637 (setcdr alist (cons (cons (cdar crosses) date) (cdr alist)))
1639 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1641 (when (= (point-max) (point-min))
1642 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1644 (nnheader-insert-file-contents
1645 (gnus-agent-article-name ".overview" group))))
1646 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1647 (insert (string-to-number (cdar crosses)))
1648 (insert-buffer-substring gnus-agent-overview-buffer beg end)
1649 (gnus-agent-check-overview-buffer))
1650 (setq crosses (cdr crosses)))))
1652 (defun gnus-agent-backup-overview-buffer ()
1653 (when gnus-newsgroup-name
1654 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name))
1657 (while (file-exists-p
1658 (setq name (concat root "~"
1659 (int-to-string (setq cnt (1+ cnt))) "~"))))
1660 (write-region (point-min) (point-max) name nil 'no-msg)
1661 (gnus-message 1 "Created backup copy of overview in %s." name)))
1664 (defun gnus-agent-check-overview-buffer (&optional buffer)
1665 "Check the overview file given for sanity.
1666 In particular, checks that the file is sorted by article number
1667 and that there are no duplicates."
1672 (set-buffer buffer))
1675 (goto-char (point-min))
1677 (while (< (point) (point-max))
1679 (cur (condition-case nil
1680 (read (current-buffer))
1683 ((or (not (integerp cur))
1684 (not (eq (char-after) ?\t)))
1686 (setq backed-up (gnus-agent-backup-overview-buffer)))
1688 "Overview buffer contains garbage '%s'."
1690 p (gnus-point-at-eol))))
1693 (setq backed-up (gnus-agent-backup-overview-buffer)))
1695 "Duplicate overview line for %d" cur)
1696 (delete-region p (progn (forward-line 1) (point))))
1699 (setq backed-up (gnus-agent-backup-overview-buffer)))
1700 (gnus-message 1 "Overview buffer not sorted!")
1701 (sort-numeric-fields 1 (point-min) (point-max))
1702 (goto-char (point-min))
1705 (setq prev-num cur)))
1706 (forward-line 1)))))))
1708 (defun gnus-agent-flush-cache ()
1710 (while gnus-agent-buffer-alist
1711 (set-buffer (cdar gnus-agent-buffer-alist))
1712 (let ((coding-system-for-write
1713 gnus-agent-file-coding-system))
1714 (write-region (point-min) (point-max)
1715 (gnus-agent-article-name ".overview"
1716 (caar gnus-agent-buffer-alist))
1718 (setq gnus-agent-buffer-alist (cdr gnus-agent-buffer-alist)))
1719 (while gnus-agent-group-alist
1720 (with-temp-file (gnus-agent-article-name
1721 ".agentview" (caar gnus-agent-group-alist))
1722 (princ (cdar gnus-agent-group-alist))
1724 (princ 1 (current-buffer))
1726 (setq gnus-agent-group-alist (cdr gnus-agent-group-alist)))))
1729 (defun gnus-agent-find-parameter (group symbol)
1730 "Search for GROUPs SYMBOL in the group's parameters, the group's
1731 topic parameters, the group's category, or the customizable
1732 variables. Returns the first non-nil value found."
1733 (or (gnus-group-find-parameter group symbol t)
1734 (gnus-group-parameter-value (cdr (gnus-group-category group)) symbol t)
1738 '((agent-short-article . gnus-agent-short-article)
1739 (agent-long-article . gnus-agent-long-article)
1740 (agent-low-score . gnus-agent-low-score)
1741 (agent-high-score . gnus-agent-high-score)
1742 (agent-days-until-old . gnus-agent-expire-days)
1743 (agent-enable-expiration
1744 . gnus-agent-enable-expiration)
1745 (agent-predicate . gnus-agent-predicate)))))))
1747 (defun gnus-agent-fetch-headers (group &optional force)
1748 "Fetch interesting headers into the agent. The group's overview
1749 file will be updated to include the headers while a list of available
1750 article numbers will be returned."
1751 (let* ((fetch-all (and gnus-agent-consider-all-articles
1752 ;; Do not fetch all headers if the predicate
1753 ;; implies that we only consider unread articles.
1754 (not (gnus-predicate-implies-unread
1755 (gnus-agent-find-parameter group
1756 'agent-predicate)))))
1757 (articles (if fetch-all
1758 (gnus-uncompress-range (gnus-active group))
1759 (gnus-list-of-unread-articles group)))
1760 (gnus-decode-encoded-word-function 'identity)
1761 (file (gnus-agent-article-name ".overview" group)))
1764 ;; Add articles with marks to the list of article headers we want to
1765 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1766 ;; mark, but do fetch recent or seen articles if they have other, more
1767 ;; interesting marks. (We have to fetch articles with boring marks
1768 ;; because otherwise the agent will remove their marks.)
1769 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1770 (unless (memq (car arts) '(seen recent killed cache))
1771 (setq articles (gnus-range-add articles (cdr arts)))))
1772 (setq articles (sort (gnus-uncompress-sequence articles) '<)))
1774 ;; At this point, I have the list of articles to consider for
1775 ;; fetching. This is the list that I'll return to my caller. Some
1776 ;; of these articles may have already been fetched. That's OK as
1777 ;; the fetch article code will filter those out. Internally, I'll
1778 ;; filter this list to just those articles whose headers need to
1780 (let ((articles articles))
1781 ;; Remove known articles.
1782 (when (and (or gnus-agent-cache
1784 (gnus-agent-load-alist group))
1785 ;; Remove articles marked as downloaded.
1787 ;; I want to fetch all headers in the active range.
1788 ;; Therefore, exclude only those headers that are in the
1790 ;; NOTE: This is probably NOT what I want to do after
1791 ;; agent expiration in this group.
1792 (setq articles (gnus-agent-uncached-articles articles group))
1794 ;; I want to only fetch those headers that have never been
1795 ;; fetched. Therefore, exclude all headers that are, or
1796 ;; WERE, in the article alist.
1797 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1798 (high (cdr (gnus-active group))))
1799 ;; Low can be greater than High when the same group is
1800 ;; fetched twice in the same session {The first fetch will
1801 ;; fill the article alist such that (last
1802 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1803 ;; group))}. The addition of one(the 1+ above) then
1804 ;; forces Low to be greater than High. When this happens,
1805 ;; gnus-list-range-intersection returns nil which
1806 ;; indicates that no headers need to be fetched. -- Kevin
1807 (setq articles (gnus-list-range-intersection
1808 articles (list (cons low high)))))))
1811 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1812 (gnus-compress-sequence articles t))
1815 (set-buffer nntp-server-buffer)
1819 (gnus-message 7 "Fetching headers for %s..." group)
1822 (gnus-make-directory (nnheader-translate-file-chars
1823 (file-name-directory file) t))
1825 (unless (eq 'nov (gnus-retrieve-headers articles group))
1826 (nnvirtual-convert-headers))
1827 (gnus-agent-check-overview-buffer)
1828 ;; Move these headers to the overview buffer so that
1829 ;; gnus-agent-braid-nov can merge them with the contents
1832 gnus-agent-overview-buffer (point-min) (point-max))
1833 ;; NOTE: Call g-a-brand-nov even when the file does not
1834 ;; exist. As a minimum, it will validate the article
1835 ;; numbers already in the buffer.
1836 (gnus-agent-braid-nov group articles file)
1837 (let ((coding-system-for-write
1838 gnus-agent-file-coding-system))
1839 (gnus-agent-check-overview-buffer)
1840 (write-region (point-min) (point-max) file nil 'silent))
1841 (gnus-agent-save-alist group articles nil)
1845 (nnheader-insert-file-contents file)))))
1848 (defsubst gnus-agent-read-article-number ()
1849 "Reads the article number at point. Returns nil when a valid article number can not be read."
1851 ;; It is unfortunate but the read function quietly overflows
1852 ;; integer. As a result, I have to use string operations to test
1853 ;; for overflow BEFORE calling read.
1854 (when (looking-at "[0-9]+\t")
1855 (let ((len (- (match-end 0) (match-beginning 0))))
1857 (read (current-buffer)))
1859 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1860 ;; Back convert from int to string to ensure that this is one of them.
1861 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1862 (num (read (current-buffer)))
1863 (str2 (int-to-string num)))
1864 (when (equal str1 str2)
1867 (defsubst gnus-agent-copy-nov-line (article)
1868 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1870 (set-buffer gnus-agent-overview-buffer)
1871 (while (and (not (eobp))
1872 (or (not (setq art (gnus-agent-read-article-number)))
1877 (not (eq article art)))
1878 (set-buffer nntp-server-buffer)
1880 (setq e (progn (forward-line 1) (point)))
1881 (set-buffer nntp-server-buffer)
1882 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1884 (defun gnus-agent-braid-nov (group articles file)
1885 "Merge agent overview data with given file.
1886 Takes unvalidated headers for ARTICLES from
1887 `gnus-agent-overview-buffer' and validated headers from the given
1888 FILE and places the combined valid headers into
1889 `nntp-server-buffer'. This function can be used, when file
1890 doesn't exist, to valid the overview buffer."
1892 (set-buffer gnus-agent-overview-buffer)
1893 (goto-char (point-min))
1894 (set-buffer nntp-server-buffer)
1896 (when (file-exists-p file)
1897 (nnheader-insert-file-contents file))
1898 (goto-char (point-max))
1901 (unless (or (= (point-min) (point-max))
1902 (< (setq last (read (current-buffer))) (car articles)))
1903 ;; Old and new overlap -- We do it the hard way.
1904 (when (nnheader-find-nov-line (car articles))
1905 ;; Replacing existing NOV entry
1906 (delete-region (point) (progn (forward-line 1) (point))))
1907 (gnus-agent-copy-nov-line (pop articles))
1911 (while (let ((art (read (current-buffer))))
1912 (cond ((< art (car articles))
1915 ((= art (car articles))
1918 (point) (progn (forward-line 1) (point)))
1924 (gnus-agent-copy-nov-line (pop articles)))))
1926 (goto-char (point-max))
1928 ;; Append the remaining lines
1931 (set-buffer gnus-agent-overview-buffer)
1932 (setq start (point))
1933 (set-buffer nntp-server-buffer))
1936 (insert-buffer-substring gnus-agent-overview-buffer start)
1939 (setq last (or last -134217728))
1940 (while (catch 'problems
1943 (setq art (gnus-agent-read-article-number))
1945 ;; Bad art num - delete this line
1947 (delete-region (point) (progn (forward-line 1) (point))))
1949 ;; Art num out of order - enable sort
1953 ;; Bad repeat of art number - delete this line
1955 (delete-region (point) (progn (forward-line 1) (point))))
1961 ;; something is seriously wrong as we simply shouldn't see out-of-order data.
1962 ;; First, we'll fix the sort.
1963 (sort-numeric-fields 1 (point-min) (point-max))
1965 ;; but now we have to consider that we may have duplicate rows...
1966 ;; so reset to beginning of file
1967 (goto-char (point-min))
1968 (setq last -134217728)
1970 ;; and throw a code that restarts this scan
1971 (throw 'problems t))
1974 ;; Keeps the compiler from warning about the free variable in
1975 ;; gnus-agent-read-agentview.
1977 (defvar gnus-agent-read-agentview))
1979 (defun gnus-agent-load-alist (group)
1980 "Load the article-state alist for GROUP."
1981 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1982 (let ((gnus-agent-read-agentview group))
1983 (setq gnus-agent-article-alist
1984 (gnus-cache-file-contents
1985 (gnus-agent-article-name ".agentview" group)
1986 'gnus-agent-file-loading-cache
1987 'gnus-agent-read-agentview))))
1989 (defun gnus-agent-read-agentview (file)
1990 "Load FILE and do a `read' there."
1994 (nnheader-insert-file-contents file)
1995 (goto-char (point-min))
1996 (let ((alist (read (current-buffer)))
1997 (version (condition-case nil (read (current-buffer))
2003 (let ((inhibit-quit t)
2005 (gnus-agent-open-history)
2006 (set-buffer (gnus-agent-history-buffer))
2007 (goto-char (point-min))
2009 (if (and (looking-at
2010 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
2011 (string= (match-string 2)
2012 gnus-agent-read-agentview)
2013 (setq entry (assoc (string-to-number (match-string 3)) alist)))
2014 (setcdr entry (string-to-number (match-string 1))))
2016 (gnus-agent-close-history)
2017 (setq changed-version t)))
2019 (setq changed-version (not (= 1 gnus-agent-article-alist-save-format))))
2024 (let ((state (car comp-list))
2026 (gnus-uncompress-range
2028 (mapcar (lambda (article-id)
2029 (setq uncomp (cons (cons article-id state) uncomp)))
2032 (setq alist (sort uncomp 'car-less-than-car)))
2033 (setq changed-version (not (= 2 gnus-agent-article-alist-save-format)))))
2034 (when changed-version
2035 (let ((gnus-agent-article-alist alist))
2036 (gnus-agent-save-alist gnus-agent-read-agentview)))
2040 (defun gnus-agent-save-alist (group &optional articles state)
2041 "Save the article-state alist for GROUP."
2042 (let* ((file-name-coding-system nnmail-pathname-coding-system)
2043 (prev (cons nil gnus-agent-article-alist))
2045 print-level print-length item article)
2046 (while (setq article (pop articles))
2047 (while (and (cdr prev)
2048 (< (caadr prev) article))
2049 (setq prev (cdr prev)))
2052 (setcdr prev (list (cons article state))))
2053 ((> (caadr prev) article)
2054 (setcdr prev (cons (cons article state) (cdr prev))))
2055 ((= (caadr prev) article)
2056 (setcdr (cadr prev) state)))
2057 (setq prev (cdr prev)))
2058 (setq gnus-agent-article-alist (cdr all))
2060 (gnus-agent-set-local group
2061 (caar gnus-agent-article-alist)
2062 (caar (last gnus-agent-article-alist)))
2064 (gnus-make-directory (gnus-agent-article-name "" group))
2065 (with-temp-file (gnus-agent-article-name ".agentview" group)
2066 (cond ((eq gnus-agent-article-alist-save-format 1)
2067 (princ gnus-agent-article-alist (current-buffer)))
2068 ((eq gnus-agent-article-alist-save-format 2)
2069 (let ((compressed nil))
2070 (mapcar (lambda (pair)
2071 (let* ((article-id (car pair))
2072 (day-of-download (cdr pair))
2073 (comp-list (assq day-of-download compressed)))
2076 (cons article-id (cdr comp-list)))
2078 (cons (list day-of-download article-id)
2080 nil)) gnus-agent-article-alist)
2081 (mapcar (lambda (comp-list)
2083 (gnus-compress-sequence
2084 (nreverse (cdr comp-list)))))
2086 (princ compressed (current-buffer)))))
2088 (princ gnus-agent-article-alist-save-format (current-buffer))
2091 (defvar gnus-agent-article-local nil)
2092 (defvar gnus-agent-file-loading-local nil)
2094 (defun gnus-agent-load-local (&optional method)
2095 "Load the METHOD'S local file. The local file contains min/max
2096 article counts for each of the method's subscribed groups."
2097 (let ((gnus-command-method (or method gnus-command-method)))
2098 (setq gnus-agent-article-local
2099 (gnus-cache-file-contents
2100 (gnus-agent-lib-file "local")
2101 'gnus-agent-file-loading-local
2102 'gnus-agent-read-and-cache-local))))
2104 (defun gnus-agent-read-and-cache-local (file)
2105 "Load and read FILE then bind its contents to
2106 gnus-agent-article-local. If that variable had `dirty' (also known as
2107 modified) original contents, they are first saved to their own file."
2109 (if (and gnus-agent-article-local
2110 (symbol-value (intern "+dirty" gnus-agent-article-local)))
2111 (gnus-agent-save-local))
2112 (gnus-agent-read-local file))
2114 (defun gnus-agent-read-local (file)
2115 "Load FILE and do a `read' there."
2116 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2121 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
2122 (nnheader-insert-file-contents file))
2125 (goto-char (point-min))
2126 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2127 (while (= (following-char) ?\;)
2129 (setq line (1+ line)))
2136 (cur (current-buffer))
2137 (obarray my-obarray))
2138 (setq group (read cur)
2142 (when (stringp group)
2143 (setq group (intern group my-obarray)))
2145 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2146 (set group (cons (+ 0 min) (+ 0 max))))
2148 (gnus-message 3 "Warning - invalid agent local: %s on line %d: %s"
2149 file line (error-message-string err))))
2151 (setq line (1+ line))))
2153 (set (intern "+dirty" my-obarray) nil)
2154 (set (intern "+method" my-obarray) gnus-command-method)
2157 (defun gnus-agent-save-local (&optional force)
2158 "Save gnus-agent-article-local under it method's agent.lib directory."
2159 (let ((my-obarray gnus-agent-article-local))
2160 (when (and my-obarray
2161 (or force (symbol-value (intern "+dirty" my-obarray))))
2162 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2163 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2164 (dest (gnus-agent-lib-file "local")))
2165 (gnus-make-directory (gnus-agent-lib-file ""))
2167 (let ((buffer-file-coding-system gnus-agent-file-coding-system))
2168 (with-temp-file dest
2169 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray)))
2170 (file-name-coding-system nnmail-pathname-coding-system)
2171 print-level print-length item article
2172 (standard-output (current-buffer)))
2173 (mapatoms (lambda (symbol)
2174 (cond ((not (boundp symbol))
2176 ((member (symbol-name symbol) '("+dirty" "+method"))
2179 (let ((range (symbol-value symbol)))
2189 (defun gnus-agent-get-local (group &optional gmane method)
2190 (let* ((gmane (or gmane (gnus-group-real-name group)))
2191 (gnus-command-method (or method (gnus-find-method-for-group group)))
2192 (local (gnus-agent-load-local))
2193 (symb (intern gmane local))
2194 (minmax (and (boundp symb) (symbol-value symb))))
2196 ;; Bind these so that gnus-agent-load-alist doesn't change the
2197 ;; current alist (i.e. gnus-agent-article-alist)
2198 (let* ((gnus-agent-article-alist gnus-agent-article-alist)
2199 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache)
2200 (alist (gnus-agent-load-alist group)))
2204 (caar (last alist))))
2205 (gnus-agent-set-local group (car minmax) (cdr minmax)
2206 gmane gnus-command-method local))))
2209 (defun gnus-agent-set-local (group min max &optional gmane method local)
2210 (let* ((gmane (or gmane (gnus-group-real-name group)))
2211 (gnus-command-method (or method (gnus-find-method-for-group group)))
2212 (local (or local (gnus-agent-load-local)))
2213 (symb (intern gmane local))
2214 (minmax (and (boundp symb) (symbol-value symb))))
2216 (if (cond ((and minmax
2217 (or (not (eq min (car minmax)))
2218 (not (eq max (cdr minmax))))
2227 (set symb (cons min max))
2230 (unintern symb local)))
2231 (set (intern "+dirty" local) t))))
2233 (defun gnus-agent-article-name (article group)
2234 (expand-file-name article
2235 (file-name-as-directory
2236 (gnus-agent-group-pathname group))))
2238 (defun gnus-agent-batch-confirmation (msg)
2239 "Show error message and return t."
2240 (gnus-message 1 msg)
2244 (defun gnus-agent-batch-fetch ()
2245 "Start Gnus and fetch session."
2248 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2249 (gnus-agent-fetch-session))
2252 (defun gnus-agent-fetch-session ()
2253 "Fetch all articles and headers that are eligible for fetching."
2255 (unless gnus-agent-covered-methods
2256 (error "No servers are covered by the Gnus agent"))
2257 (unless gnus-plugged
2258 (error "Can't fetch articles while Gnus is unplugged"))
2259 (let ((methods (gnus-agent-covered-methods))
2260 groups group gnus-command-method)
2263 (setq gnus-command-method (car methods))
2264 (when (and (or (gnus-server-opened gnus-command-method)
2265 (gnus-open-server gnus-command-method))
2266 (gnus-online gnus-command-method))
2267 (setq groups (gnus-groups-from-server (car methods)))
2268 (gnus-agent-with-fetch
2269 (while (setq group (pop groups))
2270 (when (<= (gnus-group-level group)
2271 gnus-agent-handle-level)
2272 (if (or debug-on-error debug-on-quit)
2273 (gnus-agent-fetch-group-1
2274 group gnus-command-method)
2276 (gnus-agent-fetch-group-1
2277 group gnus-command-method)
2279 (unless (funcall gnus-agent-confirmation-function
2280 (format "Error %s while fetching session. Should gnus continue? "
2281 (error-message-string err)))
2282 (error "Cannot fetch articles into the Gnus agent")))
2284 (gnus-agent-regenerate-group group)
2285 (unless (funcall gnus-agent-confirmation-function
2287 "%s while fetching session. Should gnus continue? "
2288 (error-message-string err)))
2290 "Cannot fetch articles into the Gnus agent")))))))))
2291 (setq methods (cdr methods)))
2292 (gnus-run-hooks 'gnus-agent-fetched-hook)
2293 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2295 (defun gnus-agent-fetch-group-1 (group method)
2297 (let ((gnus-command-method method)
2298 (gnus-newsgroup-name group)
2299 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies)
2300 (gnus-newsgroup-headers gnus-newsgroup-headers)
2301 (gnus-newsgroup-scored gnus-newsgroup-scored)
2302 (gnus-use-cache gnus-use-cache)
2303 (gnus-summary-expunge-below gnus-summary-expunge-below)
2304 (gnus-summary-mark-below gnus-summary-mark-below)
2305 (gnus-orphan-score gnus-orphan-score)
2306 ;; Maybe some other gnus-summary local variables should also
2312 category predicate info marks score-param
2314 (unless (gnus-check-group group)
2315 (error "Can't open server for %s" group))
2318 (when (or gnus-newsgroup-active
2320 (gnus-activate-group group))
2321 (let ((marked-articles gnus-newsgroup-downloadable))
2322 ;; Identify the articles marked for download
2323 (unless gnus-newsgroup-active
2324 ;; The variable gnus-newsgroup-active was selected as I need
2325 ;; a gnus-summary local variable that is NOT bound to any
2326 ;; value (its global value should default to nil).
2327 (dolist (mark gnus-agent-download-marks)
2328 (let ((arts (cdr (assq mark (gnus-info-marks
2329 (setq info (gnus-get-info group)))))))
2331 (setq marked-articles (nconc (gnus-uncompress-range arts)
2334 (setq marked-articles (sort marked-articles '<))
2336 ;; Fetch any new articles from the server
2337 (setq articles (gnus-agent-fetch-headers group))
2339 ;; Merge new articles with marked
2340 (setq articles (sort (append marked-articles articles) '<))
2343 ;; Parse them and see which articles we want to fetch.
2344 (setq gnus-newsgroup-dependencies
2345 (or gnus-newsgroup-dependencies
2346 (make-vector (length articles) 0)))
2347 (setq gnus-newsgroup-headers
2348 (or gnus-newsgroup-headers
2349 (gnus-get-newsgroup-headers-xover articles nil nil
2351 ;; `gnus-agent-overview-buffer' may be killed for
2352 ;; timeout reason. If so, recreate it.
2353 (gnus-agent-create-buffer)
2355 ;; Figure out how to select articles in this group
2356 (setq category (gnus-group-category group))
2360 (gnus-agent-find-parameter group 'agent-predicate)))
2362 ;; If the selection predicate requires scoring, score each header
2363 (unless (memq predicate '(gnus-agent-true gnus-agent-false))
2365 (gnus-agent-find-parameter group 'agent-score-file)))
2366 ;; Translate score-param into real one
2369 ((eq score-param 'file)
2370 (setq score-param (gnus-all-score-files group)))
2371 ((stringp (car score-param)))
2373 (setq score-param (list (list score-param)))))
2375 (gnus-score-headers score-param))))
2377 (unless (and (eq predicate 'gnus-agent-false)
2378 (not marked-articles))
2379 (let ((arts (list nil)))
2380 (let ((arts-tail arts)
2381 (alist (gnus-agent-load-alist group))
2382 (marked-articles marked-articles)
2383 (gnus-newsgroup-headers gnus-newsgroup-headers))
2384 (while (setq gnus-headers (pop gnus-newsgroup-headers))
2385 (let ((num (mail-header-number gnus-headers)))
2386 ;; Determine if this article is already in the cache
2388 (> num (caar alist)))
2389 (setq alist (cdr alist)))
2391 (unless (and (eq num (caar alist))
2394 ;; Determine if this article was marked for download.
2395 (while (and marked-articles
2396 (> num (car marked-articles)))
2397 (setq marked-articles
2398 (cdr marked-articles)))
2400 ;; When this article is marked, or selected by the
2401 ;; predicate, add it to the download list
2402 (when (or (eq num (car marked-articles))
2405 (assq num gnus-newsgroup-scored))
2406 gnus-summary-default-score))
2407 (gnus-agent-long-article
2408 (gnus-agent-find-parameter
2409 group 'agent-long-article))
2410 (gnus-agent-short-article
2411 (gnus-agent-find-parameter
2412 group 'agent-short-article))
2413 (gnus-agent-low-score
2414 (gnus-agent-find-parameter
2415 group 'agent-low-score))
2416 (gnus-agent-high-score
2417 (gnus-agent-find-parameter
2418 group 'agent-high-score))
2419 (gnus-agent-expire-days
2420 (gnus-agent-find-parameter
2421 group 'agent-days-until-old)))
2422 (funcall predicate)))
2423 (gnus-agent-append-to-list arts-tail num))))))
2425 (let (fetched-articles)
2426 ;; Fetch all selected articles
2427 (setq gnus-newsgroup-undownloaded
2428 (gnus-sorted-ndifference
2429 gnus-newsgroup-undownloaded
2430 (setq fetched-articles
2432 (gnus-agent-fetch-articles group (cdr arts))
2435 (let ((unfetched-articles
2436 (gnus-sorted-ndifference (cdr arts) fetched-articles)))
2437 (if gnus-newsgroup-active
2438 ;; Update the summary buffer
2440 (dolist (article marked-articles)
2441 (gnus-summary-set-agent-mark article t))
2442 (dolist (article fetched-articles)
2443 (when gnus-agent-mark-unread-after-downloaded
2444 (setq gnus-newsgroup-downloadable
2445 (delq article gnus-newsgroup-downloadable))
2446 (gnus-summary-mark-article
2447 article gnus-unread-mark))
2448 (when (gnus-summary-goto-subject article nil t)
2449 (gnus-summary-update-download-mark article)))
2450 (dolist (article unfetched-articles)
2451 (gnus-summary-mark-article
2452 article gnus-canceled-mark)))
2454 ;; Update the group buffer.
2456 ;; When some, or all, of the marked articles came
2457 ;; from the download mark. Remove that mark. I
2458 ;; didn't do this earlier as I only want to remove
2459 ;; the marks after the fetch is completed.
2461 (dolist (mark gnus-agent-download-marks)
2462 (when (eq mark 'download)
2464 (assq mark (gnus-info-marks
2465 (setq info (gnus-get-info group))))))
2466 (when (cdr marked-arts)
2468 (delq marked-arts (gnus-info-marks info)))
2469 (gnus-info-set-marks info marks)))))
2470 (let ((read (gnus-info-read
2471 (or info (setq info (gnus-get-info group))))))
2473 info (gnus-add-to-range read unfetched-articles)))
2475 (gnus-group-update-group group t)
2479 (concat "(gnus-group-set-info '"
2480 (gnus-prin1-to-string info)
2484 ;;; Agent Category Mode
2487 (defvar gnus-category-mode-hook nil
2488 "Hook run in `gnus-category-mode' buffers.")
2490 (defvar gnus-category-line-format " %(%20c%): %g\n"
2491 "Format of category lines.
2493 Valid specifiers include:
2494 %c Topic name (string)
2495 %g The number of groups in the topic (integer)
2497 General format specifiers can also be used. See Info node
2498 `(gnus)Formatting Variables'.")
2500 (defvar gnus-category-mode-line-format "Gnus: %%b"
2501 "The format specification for the category mode line.")
2503 (defvar gnus-agent-predicate 'false
2504 "The selection predicate used when no other source is available.")
2506 (defvar gnus-agent-short-article 100
2507 "Articles that have fewer lines than this are short.")
2509 (defvar gnus-agent-long-article 200
2510 "Articles that have more lines than this are long.")
2512 (defvar gnus-agent-low-score 0
2513 "Articles that have a score lower than this have a low score.")
2515 (defvar gnus-agent-high-score 0
2516 "Articles that have a score higher than this have a high score.")
2519 ;;; Internal variables.
2521 (defvar gnus-category-buffer "*Agent Category*")
2523 (defvar gnus-category-line-format-alist
2524 `((?c gnus-tmp-name ?s)
2525 (?g gnus-tmp-groups ?d)))
2527 (defvar gnus-category-mode-line-format-alist
2528 `((?u user-defined ?s)))
2530 (defvar gnus-category-line-format-spec nil)
2531 (defvar gnus-category-mode-line-format-spec nil)
2533 (defvar gnus-category-mode-map nil)
2534 (put 'gnus-category-mode 'mode-class 'special)
2536 (unless gnus-category-mode-map
2537 (setq gnus-category-mode-map (make-sparse-keymap))
2538 (suppress-keymap gnus-category-mode-map)
2540 (gnus-define-keys gnus-category-mode-map
2541 "q" gnus-category-exit
2542 "k" gnus-category-kill
2543 "c" gnus-category-copy
2544 "a" gnus-category-add
2545 "e" gnus-agent-customize-category
2546 "p" gnus-category-edit-predicate
2547 "g" gnus-category-edit-groups
2548 "s" gnus-category-edit-score
2549 "l" gnus-category-list
2551 "\C-c\C-i" gnus-info-find-node
2552 "\C-c\C-b" gnus-bug))
2554 (defvar gnus-category-menu-hook nil
2555 "*Hook run after the creation of the menu.")
2557 (defun gnus-category-make-menu-bar ()
2558 (gnus-turn-off-edit-menu 'category)
2559 (unless (boundp 'gnus-category-menu)
2561 gnus-category-menu gnus-category-mode-map ""
2563 ["Add" gnus-category-add t]
2564 ["Kill" gnus-category-kill t]
2565 ["Copy" gnus-category-copy t]
2566 ["Edit category" gnus-agent-customize-category t]
2567 ["Edit predicate" gnus-category-edit-predicate t]
2568 ["Edit score" gnus-category-edit-score t]
2569 ["Edit groups" gnus-category-edit-groups t]
2570 ["Exit" gnus-category-exit t]))
2572 (gnus-run-hooks 'gnus-category-menu-hook)))
2574 (defun gnus-category-mode ()
2575 "Major mode for listing and editing agent categories.
2577 All normal editing commands are switched off.
2578 \\<gnus-category-mode-map>
2579 For more in-depth information on this mode, read the manual
2580 \(`\\[gnus-info-find-node]').
2582 The following commands are available:
2584 \\{gnus-category-mode-map}"
2586 (when (gnus-visual-p 'category-menu 'menu)
2587 (gnus-category-make-menu-bar))
2588 (kill-all-local-variables)
2589 (gnus-simplify-mode-line)
2590 (setq major-mode 'gnus-category-mode)
2591 (setq mode-name "Category")
2592 (gnus-set-default-directory)
2593 (setq mode-line-process nil)
2594 (use-local-map gnus-category-mode-map)
2595 (buffer-disable-undo)
2596 (setq truncate-lines t)
2597 (setq buffer-read-only t)
2598 (gnus-run-mode-hooks 'gnus-category-mode-hook))
2600 (defalias 'gnus-category-position-point 'gnus-goto-colon)
2602 (defun gnus-category-insert-line (category)
2603 (let* ((gnus-tmp-name (format "%s" (car category)))
2604 (gnus-tmp-groups (length (gnus-agent-cat-groups category))))
2606 (gnus-add-text-properties
2610 (eval gnus-category-line-format-spec))
2611 (list 'gnus-category gnus-tmp-name))))
2613 (defun gnus-enter-category-buffer ()
2614 "Go to the Category buffer."
2616 (gnus-category-setup-buffer)
2617 (gnus-configure-windows 'category)
2618 (gnus-category-prepare))
2620 (defun gnus-category-setup-buffer ()
2621 (unless (get-buffer gnus-category-buffer)
2623 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
2624 (gnus-category-mode))))
2626 (defun gnus-category-prepare ()
2627 (gnus-set-format 'category-mode)
2628 (gnus-set-format 'category t)
2629 (let ((alist gnus-category-alist)
2630 (buffer-read-only nil))
2633 (gnus-category-insert-line (pop alist)))
2634 (goto-char (point-min))
2635 (gnus-category-position-point)))
2637 (defun gnus-category-name ()
2638 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
2639 (error "No category on the current line")))
2641 (defun gnus-category-read ()
2642 "Read the category alist."
2643 (setq gnus-category-alist
2647 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory "lib/categories"))
2648 (goto-char (point-min))
2649 ;; This code isn't temp, it will be needed so long as
2650 ;; anyone may be migrating from an older version.
2652 ;; Once we're certain that people will not revert to an
2653 ;; earlier version, we can take out the old-list code in
2654 ;; gnus-category-write.
2655 (let* ((old-list (read (current-buffer)))
2656 (new-list (ignore-errors (read (current-buffer)))))
2659 ;; Convert from a positional list to an alist.
2669 '(agent-predicate agent-score-file agent-groups))))
2672 (list (gnus-agent-cat-make 'default 'short)))))
2674 (defun gnus-category-write ()
2675 "Write the category alist."
2676 (setq gnus-category-predicate-cache nil
2677 gnus-category-group-cache nil)
2678 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
2679 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
2680 ;; This prin1 is temporary. It exists so that people can revert
2681 ;; to an earlier version of gnus-agent.
2682 (prin1 (mapcar (lambda (c)
2684 (cdr (assoc 'agent-predicate c))
2685 (cdr (assoc 'agent-score-file c))
2686 (cdr (assoc 'agent-groups c))))
2687 gnus-category-alist)
2690 (prin1 gnus-category-alist (current-buffer))))
2692 (defun gnus-category-edit-predicate (category)
2693 "Edit the predicate for CATEGORY."
2694 (interactive (list (gnus-category-name)))
2695 (let ((info (assq category gnus-category-alist)))
2697 (gnus-agent-cat-predicate info)
2698 (format "Editing the select predicate for category %s" category)
2699 `(lambda (predicate)
2700 ;; Avoid run-time execution of setf form
2701 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2703 ;; use its expansion instead:
2704 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2705 'agent-predicate predicate)
2707 (gnus-category-write)
2708 (gnus-category-list)))))
2710 (defun gnus-category-edit-score (category)
2711 "Edit the score expression for CATEGORY."
2712 (interactive (list (gnus-category-name)))
2713 (let ((info (assq category gnus-category-alist)))
2715 (gnus-agent-cat-score-file info)
2716 (format "Editing the score expression for category %s" category)
2717 `(lambda (score-file)
2718 ;; Avoid run-time execution of setf form
2719 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2721 ;; use its expansion instead:
2722 (gnus-agent-cat-set-property (assq ',category gnus-category-alist)
2723 'agent-score-file score-file)
2725 (gnus-category-write)
2726 (gnus-category-list)))))
2728 (defun gnus-category-edit-groups (category)
2729 "Edit the group list for CATEGORY."
2730 (interactive (list (gnus-category-name)))
2731 (let ((info (assq category gnus-category-alist)))
2733 (gnus-agent-cat-groups info)
2734 (format "Editing the group list for category %s" category)
2736 ;; Avoid run-time execution of setf form
2737 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2739 ;; use its expansion instead:
2740 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist)
2743 (gnus-category-write)
2744 (gnus-category-list)))))
2746 (defun gnus-category-kill (category)
2747 "Kill the current category."
2748 (interactive (list (gnus-category-name)))
2749 (let ((info (assq category gnus-category-alist))
2750 (buffer-read-only nil))
2752 (setq gnus-category-alist (delq info gnus-category-alist))
2753 (gnus-category-write)))
2755 (defun gnus-category-copy (category to)
2756 "Copy the current category."
2757 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2758 (let ((info (assq category gnus-category-alist)))
2759 (push (let ((newcat (gnus-copy-sequence info)))
2760 (setf (gnus-agent-cat-name newcat) to)
2761 (setf (gnus-agent-cat-groups newcat) nil)
2763 gnus-category-alist)
2764 (gnus-category-write)
2765 (gnus-category-list)))
2767 (defun gnus-category-add (category)
2768 "Create a new category."
2769 (interactive "SCategory name: ")
2770 (when (assq category gnus-category-alist)
2771 (error "Category %s already exists" category))
2772 (push (gnus-agent-cat-make category)
2773 gnus-category-alist)
2774 (gnus-category-write)
2775 (gnus-category-list))
2777 (defun gnus-category-list ()
2778 "List all categories."
2780 (gnus-category-prepare))
2782 (defun gnus-category-exit ()
2783 "Return to the group buffer."
2785 (kill-buffer (current-buffer))
2786 (gnus-configure-windows 'group t))
2788 ;; To avoid having 8-bit characters in the source file.
2789 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
2791 (defvar gnus-category-predicate-alist
2792 '((spam . gnus-agent-spam-p)
2793 (short . gnus-agent-short-p)
2794 (long . gnus-agent-long-p)
2795 (low . gnus-agent-low-scored-p)
2796 (high . gnus-agent-high-scored-p)
2797 (read . gnus-agent-read-p)
2798 (true . gnus-agent-true)
2799 (false . gnus-agent-false))
2800 "Mapping from short score predicate symbols to predicate functions.")
2802 (defun gnus-agent-spam-p ()
2803 "Say whether an article is spam or not."
2804 (unless gnus-agent-spam-hashtb
2805 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
2806 (if (not (equal (mail-header-references gnus-headers) ""))
2808 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
2810 (gnus-gethash string gnus-agent-spam-hashtb)
2811 (gnus-sethash string t gnus-agent-spam-hashtb)))))
2813 (defun gnus-agent-short-p ()
2814 "Say whether an article is short or not."
2815 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
2817 (defun gnus-agent-long-p ()
2818 "Say whether an article is long or not."
2819 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
2821 (defun gnus-agent-low-scored-p ()
2822 "Say whether an article has a low score or not."
2823 (< gnus-score gnus-agent-low-score))
2825 (defun gnus-agent-high-scored-p ()
2826 "Say whether an article has a high score or not."
2827 (> gnus-score gnus-agent-high-score))
2829 (defun gnus-agent-read-p ()
2830 "Say whether an article is read or not."
2831 (gnus-member-of-range (mail-header-number gnus-headers)
2832 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
2834 (defun gnus-category-make-function (predicate)
2835 "Make a function from PREDICATE."
2836 (let ((func (gnus-category-make-function-1 predicate)))
2837 (if (and (= (length func) 1)
2838 (symbolp (car func)))
2840 (gnus-byte-compile `(lambda () ,func)))))
2842 (defun gnus-agent-true ()
2846 (defun gnus-agent-false ()
2850 (defun gnus-category-make-function-1 (predicate)
2851 "Make a function from PREDICATE."
2853 ;; Functions are just returned as is.
2854 ((or (symbolp predicate)
2855 (functionp predicate))
2856 `(,(or (cdr (assq predicate gnus-category-predicate-alist))
2858 ;; More complex predicate.
2861 ((memq (car predicate) '(& and))
2863 ((memq (car predicate) '(| or))
2865 ((memq (car predicate) gnus-category-not)
2867 ,@(mapcar 'gnus-category-make-function-1 (cdr predicate))))
2869 (error "Unknown predicate type: %s" predicate))))
2871 (defun gnus-get-predicate (predicate)
2872 "Return the function implementing PREDICATE."
2873 (or (cdr (assoc predicate gnus-category-predicate-cache))
2874 (let ((func (gnus-category-make-function predicate)))
2875 (setq gnus-category-predicate-cache
2876 (nconc gnus-category-predicate-cache
2877 (list (cons predicate func))))
2880 (defun gnus-predicate-implies-unread (predicate)
2881 "Say whether PREDICATE implies unread articles only.
2882 It is okay to miss some cases, but there must be no false positives.
2883 That is, if this predicate returns true, then indeed the predicate must
2884 return only unread articles."
2885 (eq t (gnus-function-implies-unread-1
2886 (gnus-category-make-function-1 predicate))))
2888 (defun gnus-function-implies-unread-1 (function)
2889 "Recursively evaluate a predicate function to determine whether it can select
2890 any read articles. Returns t if the function is known to never
2891 return read articles, nil when it is known to always return read
2892 articles, and t_nil when the function may return both read and unread
2894 (let ((func (car function))
2895 (args (mapcar 'gnus-function-implies-unread-1 (cdr function))))
2896 (cond ((eq func 'and)
2897 (cond ((memq t args) ; if any argument returns only unread articles
2898 ;; then that argument constrains the result to only unread articles.
2900 ((memq 't_nil args) ; if any argument is indeterminate
2901 ;; then the result is indeterminate
2904 (cond ((memq nil args) ; if any argument returns read articles
2905 ;; then that argument ensures that the results includes read articles.
2907 ((memq 't_nil args) ; if any argument is indeterminate
2908 ;; then that argument ensures that the results are indeterminate
2910 (t ; if all arguments return only unread articles
2911 ;; then the result returns only unread articles
2914 (cond ((eq (car args) 't_nil) ; if the argument is indeterminate
2915 ; then the result is indeterminate
2918 ; toggle the result to be the opposite of the argument
2920 ((eq func 'gnus-agent-read-p)
2921 nil) ; The read predicate NEVER returns unread articles
2922 ((eq func 'gnus-agent-false)
2923 t) ; The false predicate returns t as the empty set excludes all read articles
2924 ((eq func 'gnus-agent-true)
2925 nil) ; The true predicate ALWAYS returns read articles
2926 ((catch 'found-match
2927 (let ((alist gnus-category-predicate-alist))
2929 (if (eq func (cdar alist))
2930 (throw 'found-match t)
2931 (setq alist (cdr alist))))))
2932 't_nil) ; All other predicates return read and unread articles
2934 (error "Unknown predicate function: %s" function)))))
2936 (defun gnus-group-category (group)
2937 "Return the category GROUP belongs to."
2938 (unless gnus-category-group-cache
2939 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
2940 (let ((cs gnus-category-alist)
2942 (while (setq cat (pop cs))
2943 (setq groups (gnus-agent-cat-groups cat))
2945 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
2946 (or (gnus-gethash group gnus-category-group-cache)
2947 (assq 'default gnus-category-alist)))
2949 (defun gnus-agent-expire-group (group &optional articles force)
2950 "Expire all old articles in GROUP.
2951 If you want to force expiring of certain articles, this function can
2952 take ARTICLES, and FORCE parameters as well.
2954 The articles on which the expiration process runs are selected as follows:
2955 if ARTICLES is null, all read and unmarked articles.
2956 if ARTICLES is t, all articles.
2957 if ARTICLES is a list, just those articles.
2958 FORCE is equivalent to setting the expiration predicates to true."
2960 (list (let ((def (or (gnus-group-group-name)
2961 gnus-newsgroup-name)))
2962 (let ((select (read-string (if def
2963 (concat "Group Name ("
2966 (if (and (equal "" select)
2972 (gnus-agent-expire articles group force)
2973 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2974 ;; expiration statistics of this single group
2975 (gnus-agent-expire-stats (list 0 0 0.0)))
2976 (if (or (not (eq articles t))
2978 (concat "Are you sure that you want to "
2979 "expire all articles in " group "? ")))
2980 (let ((gnus-command-method (gnus-find-method-for-group group))
2981 (overview (gnus-get-buffer-create " *expire overview*"))
2984 (let ((active-file (gnus-agent-lib-file "active")))
2985 (when (file-exists-p active-file)
2987 (nnheader-insert-file-contents active-file)
2988 (gnus-active-to-gnus-format
2990 (setq orig (gnus-make-hashtable
2991 (count-lines (point-min) (point-max))))))
2993 (gnus-agent-expire-group-1
2994 group overview (gnus-gethash-safe group orig)
2996 (kill-buffer overview))))
2997 (gnus-message 4 (gnus-agent-expire-done-message)))))
2999 (defun gnus-agent-expire-group-1 (group overview active articles force)
3000 ;; Internal function - requires caller to have set
3001 ;; gnus-command-method, initialized overview buffer, and to have
3002 ;; provided a non-nil active
3004 (let ((dir (gnus-agent-group-pathname group)))
3005 (when (boundp 'gnus-agent-expire-current-dirs)
3006 (set 'gnus-agent-expire-current-dirs
3008 (symbol-value 'gnus-agent-expire-current-dirs))))
3010 (if (and (not force)
3011 (eq 'DISABLE (gnus-agent-find-parameter group
3012 'agent-enable-expiration)))
3013 (gnus-message 5 "Expiry skipping over %s" group)
3014 (gnus-message 5 "Expiring articles in %s" group)
3015 (gnus-agent-load-alist group)
3016 (let* ((bytes-freed 0)
3018 (nov-entries-deleted 0)
3019 (info (gnus-get-info group))
3020 (alist gnus-agent-article-alist)
3021 (day (- (time-to-days (current-time))
3022 (gnus-agent-find-parameter group 'agent-days-until-old)))
3023 (specials (if (and alist
3025 ;; This could be a bit of a problem. I need to
3026 ;; keep the last article to avoid refetching
3027 ;; headers when using nntp in the backend. At
3028 ;; the same time, if someone uses a backend
3029 ;; that supports article moving then I may have
3030 ;; to remove the last article to complete the
3031 ;; move. Right now, I'm going to assume that
3032 ;; FORCE overrides specials.
3033 (list (caar (last alist)))))
3034 (unreads ;; Articles that are excluded from the
3035 ;; expiration process
3036 (cond (gnus-agent-expire-all
3037 ;; All articles are marked read by global decree
3040 ;; All articles are marked read by function
3044 ;; Unread articles are marked protected from
3045 ;; expiration Don't call
3046 ;; gnus-list-of-unread-articles as it returns
3047 ;; articles that have not been fetched into the
3050 (gnus-agent-unread-articles group)))
3052 ;; All articles EXCEPT those named by the caller
3053 ;; are protected from expiration
3054 (gnus-sorted-difference
3055 (gnus-uncompress-range
3057 (caar (last alist))))
3058 (sort articles '<)))))
3059 (marked ;; More articles that are excluded from the
3060 ;; expiration process
3061 (cond (gnus-agent-expire-all
3062 ;; All articles are unmarked by global decree
3065 ;; All articles are unmarked by function
3069 ;; All articles may as well be unmarked as the
3070 ;; unreads list already names the articles we are
3074 ;; Ticked and/or dormant articles are excluded
3077 (gnus-uncompress-range
3078 (cdr (assq 'tick (gnus-info-marks info))))
3079 (gnus-uncompress-range
3081 (gnus-info-marks info))))))))
3082 (nov-file (concat dir ".overview"))
3088 ;; The normal article alist contains elements that look like
3089 ;; (article# . fetch_date) I need to combine other
3090 ;; information with this list. For example, a flag indicating
3091 ;; that a particular article MUST BE KEPT. To do this, I'm
3092 ;; going to transform the elements to look like (article#
3093 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
3094 ;; the process to generate the expired article alist.
3096 ;; Convert the alist elements to (article# fetch_date nil
3098 (setq dlist (mapcar (lambda (e)
3099 (list (car e) (cdr e) nil nil)) alist))
3101 ;; Convert the keep lists to elements that look like (article#
3102 ;; nil keep_flag nil) then append it to the expanded dlist
3103 ;; These statements are sorted by ascending precidence of the
3105 (setq dlist (nconc dlist
3107 (list e nil 'unread nil))
3109 (setq dlist (nconc dlist
3111 (list e nil 'marked nil))
3113 (setq dlist (nconc dlist
3115 (list e nil 'special nil))
3118 (set-buffer overview)
3120 (buffer-disable-undo)
3121 (when (file-exists-p nov-file)
3122 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3123 (nnheader-insert-file-contents nov-file)
3124 (goto-char (point-min))
3127 (while (< (setq p (point)) (point-max))
3129 ;; If I successfully read an integer (the plus zero
3130 ;; ensures a numeric type), prepend a marker entry
3132 (push (list (+ 0 (read (current-buffer))) nil nil
3133 (set-marker (make-marker) p))
3136 (gnus-message 1 "gnus-agent-expire: read error \
3137 occurred when reading expression at %s in %s. Skipping to next \
3138 line." (point) nov-file)))
3139 ;; Whether I succeeded, or failed, it doesn't matter.
3140 ;; Move to the next line then try again.
3144 7 "gnus-agent-expire: Loading overview... Done"))
3145 (set-buffer-modified-p nil)
3147 ;; At this point, all of the information is in dlist. The
3148 ;; only problem is that much of it is spread across multiple
3149 ;; entries. Sort then MERGE!!
3150 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3151 ;; If two entries have the same article-number then sort by
3152 ;; ascending keep_flag.
3159 (cond ((< (nth 0 a) (nth 0 b))
3161 ((> (nth 0 a) (nth 0 b))
3164 (let ((a (or (symbol-value (nth 2 a))
3166 (b (or (symbol-value (nth 2 b))
3169 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3170 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3171 (let ((dlist dlist))
3172 (while (cdr dlist) ; I'm not at the end-of-list
3173 (if (eq (caar dlist) (caadr dlist))
3174 (let ((first (cdr (car dlist)))
3175 (secnd (cdr (cadr dlist))))
3176 (setcar first (or (car first)
3177 (car secnd))) ; fetch_date
3178 (setq first (cdr first)
3180 (setcar first (or (car first)
3181 (car secnd))) ; Keep_flag
3182 (setq first (cdr first)
3184 (setcar first (or (car first)
3185 (car secnd))) ; NOV_entry_marker
3187 (setcdr dlist (cddr dlist)))
3188 (setq dlist (cdr dlist)))))
3189 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3191 (let* ((len (float (length dlist)))
3195 (let ((new-completed (truncate (* 100.0
3196 (/ (setq cnt (1+ cnt))
3199 (when (> new-completed completed)
3200 (setq completed new-completed)
3201 (gnus-message 7 "%3d%% completed..." completed)))
3202 (let* ((entry (car dlist))
3203 (article-number (nth 0 entry))
3204 (fetch-date (nth 1 entry))
3205 (keep (nth 2 entry))
3206 (marker (nth 3 entry)))
3209 ;; Kept articles are unread, marked, or special.
3211 (gnus-agent-message 10
3212 "gnus-agent-expire: %s:%d: Kept %s article%s."
3213 group article-number keep (if fetch-date " and file" ""))
3215 (unless (file-exists-p
3216 (concat dir (number-to-string
3218 (setf (nth 1 entry) nil)
3219 (gnus-agent-message 3 "gnus-agent-expire cleared \
3220 download flag on %s:%d as the cached article file is missing."
3221 group (caar dlist)))
3223 (gnus-message 1 "gnus-agent-expire detected a \
3224 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3225 (gnus-agent-append-to-list
3227 (cons article-number fetch-date)))
3229 ;; The following articles are READ, UNMARKED, and
3230 ;; ORDINARY. See if they can be EXPIRED!!!
3233 ((not (integerp fetch-date))
3234 'read) ;; never fetched article (may expire
3236 ((not (file-exists-p
3237 (concat dir (number-to-string
3239 (setf (nth 1 entry) nil)
3240 'externally-expired) ;; Can't find the cached
3241 ;; article. Handle case
3242 ;; as though this article
3243 ;; was never fetched.
3245 ;; We now have the arrival day, so we see
3246 ;; whether it's old enough to be expired.
3252 ;; I found some reason to expire this entry.
3254 (let ((actions nil))
3255 (when (memq type '(forced expired))
3256 (ignore-errors ; Just being paranoid.
3257 (let* ((file-name (nnheader-concat dir (number-to-string
3259 (size (float (nth 7 (file-attributes file-name)))))
3260 (incf bytes-freed size)
3261 (incf files-deleted)
3262 (delete-file file-name))
3263 (push "expired cached article" actions))
3264 (setf (nth 1 entry) nil)
3268 (push "NOV entry removed" actions)
3271 (incf nov-entries-deleted)
3273 (let ((from (gnus-point-at-bol))
3274 (to (progn (forward-line 1) (point))))
3275 (incf bytes-freed (- to from))
3276 (delete-region from to)))
3278 ;; If considering all articles is set, I can only
3279 ;; expire article IDs that are no longer in the
3280 ;; active range (That is, articles that preceed the
3281 ;; first article in the new alist).
3282 (if (and gnus-agent-consider-all-articles
3283 (>= article-number (car active)))
3284 ;; I have to keep this ID in the alist
3285 (gnus-agent-append-to-list
3286 tail-alist (cons article-number fetch-date))
3287 (push (format "Removed %s article number from \
3288 article alist" type) actions))
3291 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3292 group article-number
3293 (mapconcat 'identity actions ", ")))))
3296 10 "gnus-agent-expire: %s:%d: Article kept as \
3297 expiration tests failed." group article-number)
3298 (gnus-agent-append-to-list
3299 tail-alist (cons article-number fetch-date)))
3302 ;; Clean up markers as I want to recycle this buffer
3303 ;; over several groups.
3305 (set-marker marker nil))
3307 (setq dlist (cdr dlist))))
3309 (setq alist (cdr alist))
3311 (let ((inhibit-quit t))
3312 (unless (equal alist gnus-agent-article-alist)
3313 (setq gnus-agent-article-alist alist)
3314 (gnus-agent-save-alist group))
3316 (when (buffer-modified-p)
3317 (let ((coding-system-for-write
3318 gnus-agent-file-coding-system))
3319 (gnus-make-directory dir)
3320 (write-region (point-min) (point-max) nov-file nil
3322 ;; clear the modified flag as that I'm not confused by
3323 ;; its status on the next pass through this routine.
3324 (set-buffer-modified-p nil)))
3326 (when (eq articles t)
3327 (gnus-summary-update-info))))
3329 (when (boundp 'gnus-agent-expire-stats)
3330 (let ((stats (symbol-value 'gnus-agent-expire-stats)))
3331 (incf (nth 2 stats) bytes-freed)
3332 (incf (nth 1 stats) files-deleted)
3333 (incf (nth 0 stats) nov-entries-deleted)))
3336 (defun gnus-agent-expire (&optional articles group force)
3337 "Expire all old articles.
3338 If you want to force expiring of certain articles, this function can
3339 take ARTICLES, GROUP and FORCE parameters as well.
3341 The articles on which the expiration process runs are selected as follows:
3342 if ARTICLES is null, all read and unmarked articles.
3343 if ARTICLES is t, all articles.
3344 if ARTICLES is a list, just those articles.
3345 Setting GROUP will limit expiration to that group.
3346 FORCE is equivalent to setting the expiration predicates to true."
3350 (gnus-agent-expire-group group articles force)
3351 (if (or (not (eq articles t))
3352 (yes-or-no-p "Are you sure that you want to expire all \
3353 articles in every agentized group? "))
3354 (let ((methods (gnus-agent-covered-methods))
3355 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3356 ;; of agent directories.
3357 (gnus-agent-expire-current-dirs nil)
3358 ;; Bind gnus-agent-expire-stats to enable tracking of
3359 ;; expiration statistics across all groups
3360 (gnus-agent-expire-stats (list 0 0 0.0))
3361 gnus-command-method overview orig)
3362 (setq overview (gnus-get-buffer-create " *expire overview*"))
3364 (while (setq gnus-command-method (pop methods))
3365 (let ((active-file (gnus-agent-lib-file "active")))
3366 (when (file-exists-p active-file)
3368 (nnheader-insert-file-contents active-file)
3369 (gnus-active-to-gnus-format
3371 (setq orig (gnus-make-hashtable
3372 (count-lines (point-min) (point-max))))))
3373 (dolist (expiring-group (gnus-groups-from-server
3374 gnus-command-method))
3376 (gnus-gethash-safe expiring-group orig)))
3380 (gnus-agent-expire-group-1
3381 expiring-group overview active articles force))))))))
3382 (kill-buffer overview))
3383 (gnus-agent-expire-unagentized-dirs)
3384 (gnus-message 4 (gnus-agent-expire-done-message))))))
3386 (defun gnus-agent-expire-done-message ()
3387 (if (and (> gnus-verbose 4)
3388 (boundp 'gnus-agent-expire-stats))
3389 (let* ((stats (symbol-value 'gnus-agent-expire-stats))
3390 (size (nth 2 stats))
3391 (units '(B KB MB GB)))
3392 (while (and (> size 1024.0)
3394 (setq size (/ size 1024.0)
3397 (format "Expiry recovered %d NOV entries, deleted %d files,\
3404 (defun gnus-agent-expire-unagentized-dirs ()
3405 (when (and gnus-agent-expire-unagentized-dirs
3406 (boundp 'gnus-agent-expire-current-dirs))
3407 (let* ((keep (gnus-make-hashtable))
3408 ;; Formally bind gnus-agent-expire-current-dirs so that the
3409 ;; compiler will not complain about free references.
3410 (gnus-agent-expire-current-dirs
3411 (symbol-value 'gnus-agent-expire-current-dirs))
3414 (gnus-sethash gnus-agent-directory t keep)
3415 (while gnus-agent-expire-current-dirs
3416 (setq dir (pop gnus-agent-expire-current-dirs))
3417 (when (and (stringp dir)
3418 (file-directory-p dir))
3419 (while (not (gnus-gethash dir keep))
3420 (gnus-sethash dir t keep)
3421 (setq dir (file-name-directory (directory-file-name dir))))))
3428 "Given a directory, check it and its subdirectories for
3429 membership in the keep hash. If it isn't found, add
3431 (let ((files (directory-files d))
3433 (while (setq file (pop files))
3434 (cond ((equal file ".") ; Ignore self
3436 ((equal file "..") ; Ignore parent
3438 ((equal file ".overview")
3439 ;; Directory must contain .overview to be
3440 ;; agent's cache of a group.
3441 (let ((d (file-name-as-directory d))
3443 ;; Search ancestor's for last directory NOT
3444 ;; found in keep hash.
3445 (while (not (gnus-gethash
3446 (setq d (file-name-directory d)) keep))
3448 d (directory-file-name d)))
3449 ;; if ANY ancestor was NOT in keep hash and
3450 ;; it it's already in to-remove, add it to
3453 (not (member r to-remove)))
3454 (push r to-remove))))
3455 ((file-directory-p (setq file (nnheader-concat d file)))
3456 (funcall checker file)))))))))
3457 (funcall checker (expand-file-name gnus-agent-directory))
3459 (when (and to-remove
3460 (or gnus-expert-user
3462 "gnus-agent-expire has identified local directories that are\
3463 not currently required by any agentized group. Do you wish to consider\
3466 (let ((dir (pop to-remove)))
3467 (if (gnus-y-or-n-p (format "Delete %s? " dir))
3468 (let* (delete-recursive
3473 (if (file-directory-p f-or-d)
3475 (delete-directory f-or-d)
3478 (or (member f '("." ".."))
3479 (funcall delete-recursive
3482 (directory-files f-or-d))
3483 (delete-directory f-or-d)))
3484 (delete-file f-or-d)))))))
3485 (funcall delete-recursive dir))))))))))
3488 (defun gnus-agent-batch ()
3489 "Start Gnus, send queue and fetch session."
3491 (let ((init-file-user "")
3492 (gnus-always-read-dribble-file t))
3494 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
3495 (gnus-group-send-queue)
3496 (gnus-agent-fetch-session)))
3498 (defun gnus-agent-unread-articles (group)
3499 (let* ((read (gnus-info-read (gnus-get-info group)))
3500 (known (gnus-agent-load-alist group))
3502 (tail-unread unread))
3503 (while (and known read)
3504 (let ((candidate (car (pop known))))
3505 (while (let* ((range (car read))
3506 (min (if (numberp range) range (car range)))
3507 (max (if (numberp range) range (cdr range))))
3508 (cond ((or (not min)
3510 (gnus-agent-append-to-list tail-unread candidate)
3513 (setq read (cdr read))
3514 ;; return t so that I always loop one more
3515 ;; time. If I just iterated off the end of
3516 ;; read, min will become nil and the current
3517 ;; candidate will be added to the unread list.
3520 (gnus-agent-append-to-list tail-unread (car (pop known))))
3523 (defun gnus-agent-uncached-articles (articles group &optional cached-header)
3524 "Restrict ARTICLES to numbers already fetched.
3525 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3526 that have already been fetched.
3527 If CACHED-HEADER is nil, articles are only excluded if the article itself
3530 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3531 ;; 'car gnus-agent-article-alist))
3533 ;; Functionally, I don't need to construct a temp list using mapcar.
3535 (if (and (or gnus-agent-cache (not gnus-plugged))
3536 (gnus-agent-load-alist group))
3537 (let* ((ref gnus-agent-article-alist)
3539 (uncached (list nil))
3540 (tail-uncached uncached))
3541 (while (and ref arts)
3542 (let ((v1 (car arts))
3544 (cond ((< v1 v2) ; v1 does not appear in the reference list
3545 (gnus-agent-append-to-list tail-uncached v1)
3546 (setq arts (cdr arts)))
3548 (unless (or cached-header (cdar ref)) ; v1 is already cached
3549 (gnus-agent-append-to-list tail-uncached v1))
3550 (setq arts (cdr arts))
3551 (setq ref (cdr ref)))
3552 (t ; reference article (v2) preceeds the list being filtered
3553 (setq ref (cdr ref))))))
3555 (gnus-agent-append-to-list tail-uncached (pop arts)))
3557 ;; if gnus-agent-load-alist fails, no articles are cached.
3560 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
3562 (gnus-agent-create-buffer)
3563 (let ((gnus-decode-encoded-word-function 'identity)
3564 (file (gnus-agent-article-name ".overview" group))
3565 cached-articles uncached-articles)
3566 (gnus-make-directory (nnheader-translate-file-chars
3567 (file-name-directory file) t))
3569 ;; Populate temp buffer with known headers
3570 (when (file-exists-p file)
3571 (with-current-buffer gnus-agent-overview-buffer
3573 (let ((nnheader-file-coding-system
3574 gnus-agent-file-coding-system))
3575 (nnheader-insert-nov-file file (car articles)))))
3577 (if (setq uncached-articles (gnus-agent-uncached-articles articles group
3580 ;; Populate nntp-server-buffer with uncached headers
3581 (set-buffer nntp-server-buffer)
3583 (cond ((not (eq 'nov (let (gnus-agent) ; Turn off agent
3584 (gnus-retrieve-headers
3585 uncached-articles group fetch-old))))
3586 (nnvirtual-convert-headers))
3587 ((eq 'nntp (car gnus-current-select-method))
3588 ;; The author of gnus-get-newsgroup-headers-xover
3589 ;; reports that the XOVER command is commonly
3590 ;; unreliable. The problem is that recently
3591 ;; posted articles may not be entered into the
3592 ;; NOV database in time to respond to my XOVER
3595 ;; I'm going to use his assumption that the NOV
3596 ;; database is updated in order of ascending
3597 ;; article ID. Therefore, a response containing
3598 ;; article ID N implies that all articles from 1
3599 ;; to N-1 are up-to-date. Therefore, missing
3600 ;; articles in that range have expired.
3602 (set-buffer nntp-server-buffer)
3603 (let* ((fetched-articles (list nil))
3604 (tail-fetched-articles fetched-articles)
3605 (min (cond ((numberp fetch-old)
3606 (max 1 (- (car articles) fetch-old)))
3611 (max (car (last articles))))
3613 ;; Get the list of articles that were fetched
3614 (goto-char (point-min))
3615 (let ((pm (point-max))
3617 (while (< (point) pm)
3618 (when (setq art (gnus-agent-read-article-number))
3619 (gnus-agent-append-to-list tail-fetched-articles art))
3622 ;; Clip this list to the headers that will
3623 ;; actually be returned
3624 (setq fetched-articles (gnus-list-range-intersection
3625 (cdr fetched-articles)
3628 ;; Clip the uncached articles list to exclude
3629 ;; IDs after the last FETCHED header. The
3630 ;; excluded IDs may be fetchable using HEAD.
3631 (if (car tail-fetched-articles)
3632 (setq uncached-articles
3633 (gnus-list-range-intersection
3635 (cons (car uncached-articles)
3636 (car tail-fetched-articles)))))
3638 ;; Create the list of articles that were
3639 ;; "successfully" fetched. Success, in this
3640 ;; case, means that the ID should not be
3641 ;; fetched again. In the case of an expired
3642 ;; article, the header will not be fetched.
3643 (setq uncached-articles
3644 (gnus-sorted-nunion fetched-articles
3648 ;; Erase the temp buffer
3649 (set-buffer gnus-agent-overview-buffer)
3652 ;; Copy the nntp-server-buffer to the temp buffer
3653 (set-buffer nntp-server-buffer)
3654 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
3656 ;; Merge the temp buffer with the known headers (found on
3657 ;; disk in FILE) into the nntp-server-buffer
3658 (when uncached-articles
3659 (gnus-agent-braid-nov group uncached-articles file))
3661 ;; Save the new set of known headers to FILE
3662 (set-buffer nntp-server-buffer)
3663 (let ((coding-system-for-write
3664 gnus-agent-file-coding-system))
3665 (gnus-agent-check-overview-buffer)
3666 (write-region (point-min) (point-max) file nil 'silent))
3668 ;; Update the group's article alist to include the newly
3669 ;; fetched articles.
3670 (gnus-agent-load-alist group)
3671 (gnus-agent-save-alist group uncached-articles nil)
3674 ;; Copy the temp buffer to the nntp-server-buffer
3675 (set-buffer nntp-server-buffer)
3677 (insert-buffer-substring gnus-agent-overview-buffer)))
3680 (not (numberp fetch-old)))
3681 t ; Don't remove anything.
3682 (nnheader-nov-delete-outside-range
3683 (if fetch-old (max 1 (- (car articles) fetch-old))
3685 (car (last articles)))
3690 (defun gnus-agent-request-article (article group)
3691 "Retrieve ARTICLE in GROUP from the agent cache."
3692 (when (and gnus-agent
3693 (or gnus-agent-cache
3696 (let* ((gnus-command-method (gnus-find-method-for-group group))
3697 (file (gnus-agent-article-name (number-to-string article) group))
3698 (buffer-read-only nil))
3699 (when (and (file-exists-p file)
3700 (> (nth 7 (file-attributes file)) 0))
3702 (gnus-kill-all-overlays)
3703 (let ((coding-system-for-read gnus-cache-coding-system))
3704 (insert-file-contents file))
3707 (defun gnus-agent-regenerate-group (group &optional reread)
3709 If REREAD is t, all articles in the .overview are marked as unread.
3710 If REREAD is a list, the specified articles will be marked as unread.
3711 In addition, their NOV entries in .overview will be refreshed using
3712 the articles' current headers.
3713 If REREAD is not nil, downloaded articles are marked as unread."
3715 (list (let ((def (or (gnus-group-group-name)
3716 gnus-newsgroup-name)))
3717 (let ((select (read-string (if def
3718 (concat "Group Name ("
3721 (if (and (equal "" select)
3727 (cursor-in-echo-area t)
3728 (echo-keystrokes 0))
3729 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3730 (setq c (read-char-exclusive))
3732 (cond ((or (eq c ?\r) (eq c ?n) (eq c ?N))
3734 ((or (eq c ?a) (eq c ?A))
3736 ((or (eq c ?d) (eq c ?D))
3737 (throw 'mark 'some)))
3738 (gnus-message 3 "Ignoring unexpected input")
3742 (gnus-message 5 "Regenerating in %s" group)
3743 (let* ((gnus-command-method (or gnus-command-method
3744 (gnus-find-method-for-group group)))
3745 (file (gnus-agent-article-name ".overview" group))
3746 (dir (file-name-directory file))
3748 (downloaded (if (file-exists-p dir)
3749 (sort (delq nil (mapcar (lambda (name)
3750 (and (not (file-directory-p (nnheader-concat dir name)))
3751 (string-to-number name)))
3752 (directory-files dir nil "^[0-9]+$" t)))
3754 (progn (gnus-make-directory dir) nil)))
3759 (mm-with-unibyte-buffer
3760 (if (file-exists-p file)
3761 (let ((nnheader-file-coding-system
3762 gnus-agent-file-coding-system))
3763 (nnheader-insert-file-contents file)))
3764 (set-buffer-modified-p nil)
3766 ;; Load the article IDs found in the overview file. As a
3767 ;; side-effect, validate the file contents.
3771 (goto-char (point-min))
3772 (while (< (point) (point-max))
3773 (cond ((and (looking-at "[0-9]+\t")
3774 (<= (- (match-end 0) (match-beginning 0)) 9))
3775 (push (read (current-buffer)) nov-arts)
3777 (let ((l1 (car nov-arts))
3778 (l2 (cadr nov-arts)))
3779 (cond ((and (listp reread) (memq l1 reread))
3781 (setq nov-arts (cdr nov-arts))
3782 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3783 entry of article %s deleted." l1))
3787 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3788 entries are NOT in ascending order.")
3789 ;; Don't sort now as I haven't verified
3790 ;; that every line begins with a number
3794 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3795 entries contained duplicate of article %s. Duplicate deleted." l1)
3797 (setq nov-arts (cdr nov-arts))))))
3799 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3800 entries contained line that did not begin with an article number. Deleted\
3802 (gnus-delete-line))))
3804 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3805 entries into ascending order.")
3806 (sort-numeric-fields 1 (point-min) (point-max))
3807 (setq nov-arts nil))))
3808 (gnus-agent-check-overview-buffer)
3810 ;; Construct a new article alist whose nodes match every header
3811 ;; in the .overview file. As a side-effect, missing headers are
3812 ;; reconstructed from the downloaded article file.
3813 (while (or downloaded nov-arts)
3814 (cond ((and downloaded
3816 (> (car downloaded) (car nov-arts))))
3817 ;; This entry is missing from the overview file
3818 (gnus-message 3 "Regenerating NOV %s %d..." group
3820 (let ((file (concat dir (number-to-string (car downloaded)))))
3821 (mm-with-unibyte-buffer
3822 (nnheader-insert-file-contents file)
3823 (nnheader-remove-body)
3824 (setq header (nnheader-parse-naked-head)))
3825 (mail-header-set-number header (car downloaded))
3827 (let ((key (concat "^" (int-to-string (car nov-arts))
3829 (or (re-search-backward key nil t)
3830 (re-search-forward key))
3832 (goto-char (point-min)))
3833 (nnheader-insert-nov header))
3834 (setq nov-arts (cons (car downloaded) nov-arts)))
3835 ((eq (car downloaded) (car nov-arts))
3836 ;; This entry in the overview has been downloaded
3837 (push (cons (car downloaded)
3839 (nth 5 (file-attributes
3840 (concat dir (number-to-string
3841 (car downloaded))))))) alist)
3842 (setq downloaded (cdr downloaded))
3843 (setq nov-arts (cdr nov-arts)))
3845 ;; This entry in the overview has not been downloaded
3846 (push (cons (car nov-arts) nil) alist)
3847 (setq nov-arts (cdr nov-arts)))))
3849 ;; When gnus-agent-consider-all-articles is set,
3850 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3851 ;; the alist. Those IDs serve as markers to indicate that an
3852 ;; attempt has been made to fetch that article's header.
3854 ;; When gnus-agent-consider-all-articles is NOT set,
3855 ;; gnus-agent-regenerate-group can remove the article ID of every
3856 ;; article (with the exception of the last ID in the list - it's
3857 ;; special) that no longer appears in the overview. In this
3858 ;; situtation, the last article ID in the list implies that it,
3859 ;; and every article ID preceeding it, have been fetched from the
3862 (if gnus-agent-consider-all-articles
3863 ;; Restore all article IDs that were not found in the overview file.
3864 (let* ((n (cons nil alist))
3866 (o (gnus-agent-load-alist group)))
3868 (let ((nID (caadr n))
3871 (setq n (setcdr n (list (list oID))))
3874 (setcdr n (cons (list oID) (cdr n)))
3880 (setq n (cdr n))))))
3881 (setq alist (cdr merged)))
3882 ;; Restore the last article ID if it is not already in the new alist
3883 (let ((n (last alist))
3884 (o (last (gnus-agent-load-alist group))))
3888 (push (cons (caar o) nil) alist))
3889 ((< (caar n) (caar o))
3890 (setcdr n (list (car o)))))))
3892 (let ((inhibit-quit t))
3893 (if (setq regenerated (buffer-modified-p))
3894 (let ((coding-system-for-write gnus-agent-file-coding-system))
3895 (write-region (point-min) (point-max) file nil 'silent)))
3897 (setq regenerated (or regenerated
3898 (and reread gnus-agent-article-alist)
3899 (not (equal alist gnus-agent-article-alist))))
3901 (setq gnus-agent-article-alist alist)
3904 (gnus-agent-save-alist group)
3906 ;; I have to alter the group's active range NOW as
3907 ;; gnus-make-ascending-articles-unread will use it to
3908 ;; recalculate the number of unread articles in the group
3910 (let ((group (gnus-group-real-name group))
3911 (group-active (or (gnus-active group)
3912 (gnus-activate-group group))))
3913 (gnus-agent-possibly-alter-active group group-active)))))
3915 (when (and reread gnus-agent-article-alist)
3916 (gnus-agent-synchronize-group-flags
3921 (delq nil (mapcar (function (lambda (c)
3922 (cond ((eq reread t)
3926 gnus-agent-article-alist)))
3928 gnus-command-method)
3930 (when (gnus-buffer-live-p gnus-group-buffer)
3931 (gnus-group-update-group group t)))
3937 (defun gnus-agent-regenerate (&optional clean reread)
3938 "Regenerate all agent covered files.
3939 If CLEAN, obsolete (ignore)."
3942 (gnus-message 4 "Regenerating Gnus agent files...")
3943 (dolist (gnus-command-method (gnus-agent-covered-methods))
3944 (dolist (group (gnus-groups-from-server gnus-command-method))
3945 (setq regenerated (or (gnus-agent-regenerate-group group reread)
3947 (gnus-message 4 "Regenerating Gnus agent files...done")
3951 (defun gnus-agent-go-online (&optional force)
3952 "Switch servers into online status."
3953 (interactive (list t))
3954 (dolist (server gnus-opened-servers)
3955 (when (eq (nth 1 server) 'offline)
3956 (if (if (eq force 'ask)
3958 (format "Switch %s:%s into online status? "
3959 (caar server) (cadar server)))
3961 (setcar (nthcdr 1 server) 'close)))))
3963 (defun gnus-agent-toggle-group-plugged (group)
3964 "Toggle the status of the server of the current group."
3965 (interactive (list (gnus-group-group-name)))
3966 (let* ((method (gnus-find-method-for-group group))
3967 (status (cadr (assoc method gnus-opened-servers))))
3968 (if (eq status 'offline)
3969 (gnus-server-set-status method 'closed)
3970 (gnus-close-server method)
3971 (gnus-server-set-status method 'offline))
3972 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
3973 (if (eq status 'offline) 'offline 'online)
3974 (if (eq status 'offline) 'online 'offline))))
3976 (defun gnus-agent-group-covered-p (group)
3977 (gnus-agent-method-p (gnus-group-method group)))
3979 (provide 'gnus-agent)
3981 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
3982 ;;; gnus-agent.el ends here