1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
36 (if (featurep 'xemacs
)
42 (autoload 'gnus-server-update-server
"gnus-srvr")
43 (autoload 'gnus-agent-customize-category
"gnus-cus")
46 (defcustom gnus-agent-directory
(nnheader-concat gnus-directory
"agent/")
47 "Where the Gnus agent will store its files."
51 (defcustom gnus-agent-plugged-hook nil
52 "Hook run when plugging into the network."
56 (defcustom gnus-agent-unplugged-hook nil
57 "Hook run when unplugging from the network."
61 (defcustom gnus-agent-fetched-hook nil
62 "Hook run when finished fetching articles."
67 (defcustom gnus-agent-handle-level gnus-level-subscribed
68 "Groups on levels higher than this variable will be ignored by the Agent."
72 (defcustom gnus-agent-expire-days
7
73 "Read articles older than this will be expired.
74 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
76 :type
'(number :tag
"days"))
78 (defcustom gnus-agent-expire-all nil
79 "If non-nil, also expire unread, ticked and dormant articles.
80 If nil, only read articles will be expired."
84 (defcustom gnus-agent-group-mode-hook nil
85 "Hook run in Agent group minor modes."
89 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
90 (when (featurep 'xemacs
)
91 (add-hook 'gnus-agent-group-mode-hook
'gnus-xmas-agent-group-menu-add
))
93 (defcustom gnus-agent-summary-mode-hook nil
94 "Hook run in Agent summary minor modes."
98 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
99 (when (featurep 'xemacs
)
100 (add-hook 'gnus-agent-summary-mode-hook
'gnus-xmas-agent-summary-menu-add
))
102 (defcustom gnus-agent-server-mode-hook nil
103 "Hook run in Agent summary minor modes."
107 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
108 (when (featurep 'xemacs
)
109 (add-hook 'gnus-agent-server-mode-hook
'gnus-xmas-agent-server-menu-add
))
111 (defcustom gnus-agent-confirmation-function
'y-or-n-p
112 "Function to confirm when error happens."
117 (defcustom gnus-agent-synchronize-flags nil
118 "Indicate if flags are synchronized when you plug in.
119 If this is `ask' the hook will query the user."
121 :type
'(choice (const :tag
"Always" t
)
122 (const :tag
"Never" nil
)
123 (const :tag
"Ask" ask
))
126 (defcustom gnus-agent-go-online
'ask
127 "Indicate if offline servers go online when you plug in.
128 If this is `ask' the hook will query the user."
130 :type
'(choice (const :tag
"Always" t
)
131 (const :tag
"Never" nil
)
132 (const :tag
"Ask" ask
))
135 (defcustom gnus-agent-mark-unread-after-downloaded t
136 "Indicate whether to mark articles unread after downloaded."
141 (defcustom gnus-agent-download-marks
'(download)
142 "Marks for downloading."
144 :type
'(repeat (symbol :tag
"Mark"))
147 (defcustom gnus-agent-consider-all-articles nil
148 "When non-nil, the agent will let the agent predicate decide
149 whether articles need to be downloaded or not, for all articles. When
150 nil, the default, the agent will only let the predicate decide
151 whether unread articles are downloaded or not. If you enable this,
152 groups with large active ranges may open slower and you may also want
153 to look into the agent expiry settings to block the expiration of
154 read articles as they would just be downloaded again."
159 (defcustom gnus-agent-max-fetch-size
10000000 ;; 10 Mb
160 "Chunk size for `gnus-agent-fetch-session'.
161 The function will split its article fetches into chunks smaller than
166 (defcustom gnus-agent-enable-expiration
'ENABLE
167 "The default expiration state for each group.
168 When set to ENABLE, the default, `gnus-agent-expire' will expire old
169 contents from a group's local storage. This value may be overridden
170 to disable expiration in specific categories, topics, and groups. Of
171 course, you could change gnus-agent-enable-expiration to DISABLE then
172 enable expiration per categories, topics, and groups."
174 :type
'(radio (const :format
"Enable " ENABLE
)
175 (const :format
"Disable " DISABLE
)))
177 (defcustom gnus-agent-expire-unagentized-dirs t
178 "*Whether expiration should expire in unagentized directories.
179 Have gnus-agent-expire scan the directories under
180 \(gnus-agent-directory) for groups that are no longer agentized.
181 When found, offer to remove them."
186 (defcustom gnus-agent-auto-agentize-methods
'(nntp nnimap
)
187 "Initially, all servers from these methods are agentized.
188 The user may remove or add servers using the Server buffer.
189 See Info node `(gnus)Server Buffer'."
191 :type
'(repeat symbol
)
194 (defcustom gnus-agent-queue-mail t
195 "Whether and when outgoing mail should be queued by the agent.
196 When `always', always queue outgoing mail. When nil, never
197 queue. Otherwise, queue if and only if unplugged."
199 :type
'(radio (const :format
"Always" always
)
200 (const :format
"Never" nil
)
201 (const :format
"When plugged" t
)))
203 (defcustom gnus-agent-prompt-send-queue nil
204 "If non-nil, `gnus-group-send-queue' will prompt if called when
209 ;;; Internal variables
211 (defvar gnus-agent-history-buffers nil
)
212 (defvar gnus-agent-buffer-alist nil
)
213 (defvar gnus-agent-article-alist nil
214 "An assoc list identifying the articles whose headers have been fetched.
215 If successfully fetched, these headers will be stored in the group's overview
216 file. The key of each assoc pair is the article ID, the value of each assoc
217 pair is a flag indicating whether the identified article has been downloaded
218 \(gnus-agent-fetch-articles sets the value to the day of the download).
220 1) The last element of this list can not be expired as some
221 routines (for example, get-agent-fetch-headers) use the last
222 value to track which articles have had their headers retrieved.
223 2) The function `gnus-agent-regenerate' may destructively modify the value.")
224 (defvar gnus-agent-group-alist nil
)
225 (defvar gnus-category-alist nil
)
226 (defvar gnus-agent-current-history nil
)
227 (defvar gnus-agent-overview-buffer nil
)
228 (defvar gnus-category-predicate-cache nil
)
229 (defvar gnus-category-group-cache nil
)
230 (defvar gnus-agent-spam-hashtb nil
)
231 (defvar gnus-agent-file-name nil
)
232 (defvar gnus-agent-send-mail-function nil
)
233 (defvar gnus-agent-file-coding-system
'raw-text
)
234 (defvar gnus-agent-file-loading-cache nil
)
237 (defvar gnus-headers
)
244 (defun gnus-open-agent ()
246 (gnus-agent-read-servers)
248 (gnus-agent-create-buffer)
249 (add-hook 'gnus-group-mode-hook
'gnus-agent-mode
)
250 (add-hook 'gnus-summary-mode-hook
'gnus-agent-mode
)
251 (add-hook 'gnus-server-mode-hook
'gnus-agent-mode
))
253 (defun gnus-agent-create-buffer ()
254 (if (gnus-buffer-live-p gnus-agent-overview-buffer
)
256 (setq gnus-agent-overview-buffer
257 (gnus-get-buffer-create " *Gnus agent overview*"))
258 (with-current-buffer gnus-agent-overview-buffer
259 (mm-enable-multibyte))
262 (gnus-add-shutdown 'gnus-close-agent
'gnus
)
264 (defun gnus-close-agent ()
265 (setq gnus-category-predicate-cache nil
266 gnus-category-group-cache nil
267 gnus-agent-spam-hashtb nil
)
268 (gnus-kill-buffer gnus-agent-overview-buffer
))
271 ;;; Utility functions
274 (defun gnus-agent-read-file (file)
275 "Load FILE and do a `read' there."
278 (nnheader-insert-file-contents file
)
279 (goto-char (point-min))
280 (read (current-buffer)))))
282 (defsubst gnus-agent-method
()
283 (concat (symbol-name (car gnus-command-method
)) "/"
284 (if (equal (cadr gnus-command-method
) "")
286 (cadr gnus-command-method
))))
288 (defsubst gnus-agent-directory
()
289 "The name of the Gnus agent directory."
290 (nnheader-concat gnus-agent-directory
291 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
293 (defun gnus-agent-lib-file (file)
294 "The full name of the Gnus agent library FILE."
295 (expand-file-name file
296 (file-name-as-directory
297 (expand-file-name "agent.lib" (gnus-agent-directory)))))
299 (defun gnus-agent-cat-set-property (category property value
)
301 (setcdr (or (assq property category
)
302 (let ((cell (cons property nil
)))
303 (setcdr category
(cons cell
(cdr category
)))
305 (let ((category category
))
306 (while (cond ((eq property
(caadr category
))
307 (setcdr category
(cddr category
))
310 (setq category
(cdr category
)))))))
314 (defmacro gnus-agent-cat-defaccessor
(name prop-name
)
315 "Define accessor and setter methods for manipulating a list of the form
316 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
317 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
318 manipulated as follows:
319 (func LIST): Returns VALUE1
320 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
321 `(progn (defmacro ,name
(category)
322 (list (quote cdr
) (list (quote assq
)
323 (quote (quote ,prop-name
)) category
)))
325 (define-setf-method ,name
(category)
326 (let* ((--category--temp-- (make-symbol "--category--"))
327 (--value--temp-- (make-symbol "--value--")))
328 (list (list --category--temp--
) ; temporary-variables
329 (list category
) ; value-forms
330 (list --value--temp--
) ; store-variables
331 (let* ((category --category--temp--
) ; store-form
332 (value --value--temp--
))
333 (list (quote gnus-agent-cat-set-property
)
335 (quote (quote ,prop-name
))
337 (list (quote ,name
) --category--temp--
) ; access-form
341 (defmacro gnus-agent-cat-name
(category)
344 (gnus-agent-cat-defaccessor
345 gnus-agent-cat-days-until-old agent-days-until-old
)
346 (gnus-agent-cat-defaccessor
347 gnus-agent-cat-enable-expiration agent-enable-expiration
)
348 (gnus-agent-cat-defaccessor
349 gnus-agent-cat-groups agent-groups
)
350 (gnus-agent-cat-defaccessor
351 gnus-agent-cat-high-score agent-high-score
)
352 (gnus-agent-cat-defaccessor
353 gnus-agent-cat-length-when-long agent-length-when-long
)
354 (gnus-agent-cat-defaccessor
355 gnus-agent-cat-length-when-short agent-length-when-short
)
356 (gnus-agent-cat-defaccessor
357 gnus-agent-cat-low-score agent-low-score
)
358 (gnus-agent-cat-defaccessor
359 gnus-agent-cat-predicate agent-predicate
)
360 (gnus-agent-cat-defaccessor
361 gnus-agent-cat-score-file agent-score-file
)
362 (gnus-agent-cat-defaccessor
363 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces
)
366 ;; This form is equivalent to defsetf except that it calls make-symbol
367 ;; whereas defsetf calls gensym (Using gensym creates a run-time
368 ;; dependency on the CL library).
371 (define-setf-method gnus-agent-cat-groups
(category)
372 (let* ((--category--temp-- (make-symbol "--category--"))
373 (--groups--temp-- (make-symbol "--groups--")))
374 (list (list --category--temp--
)
376 (list --groups--temp--
)
377 (let* ((category --category--temp--
)
378 (groups --groups--temp--
))
379 (list (quote gnus-agent-set-cat-groups
) category groups
))
380 (list (quote gnus-agent-cat-groups
) --category--temp--
))))
383 (defun gnus-agent-set-cat-groups (category groups
)
384 (unless (eq groups
'ignore
)
386 (old-g (gnus-agent-cat-groups category
)))
387 (cond ((eq new-g old-g
)
388 ;; gnus-agent-add-group is fiddling with the group
389 ;; list. Still, Im done.
392 ((eq new-g
(cdr old-g
))
393 ;; gnus-agent-add-group is fiddling with the group list
394 (setcdr (or (assq 'agent-groups category
)
395 (let ((cell (cons 'agent-groups nil
)))
396 (setcdr category
(cons cell
(cdr category
)))
399 (let ((groups groups
))
401 (let* ((group (pop groups
))
402 (old-category (gnus-group-category group
)))
403 (if (eq category old-category
)
405 (setf (gnus-agent-cat-groups old-category
)
406 (delete group
(gnus-agent-cat-groups
408 ;; Purge cache as preceeding loop invalidated it.
409 (setq gnus-category-group-cache nil
))
411 (setcdr (or (assq 'agent-groups category
)
412 (let ((cell (cons 'agent-groups nil
)))
413 (setcdr category
(cons cell
(cdr category
)))
416 (defsubst gnus-agent-cat-make
(name &optional default-agent-predicate
)
417 (list name
`(agent-predicate .
,(or default-agent-predicate
'false
))))
419 ;;; Fetching setup functions.
421 (defun gnus-agent-start-fetch ()
422 "Initialize data structures for efficient fetching."
423 (gnus-agent-create-buffer))
425 (defun gnus-agent-stop-fetch ()
426 "Save all data structures and clean up."
427 (setq gnus-agent-spam-hashtb nil
)
429 (set-buffer nntp-server-buffer
)
432 (defmacro gnus-agent-with-fetch
(&rest forms
)
435 (let ((gnus-agent-fetching t
))
436 (gnus-agent-start-fetch)
438 (gnus-agent-stop-fetch)))
440 (put 'gnus-agent-with-fetch
'lisp-indent-function
0)
441 (put 'gnus-agent-with-fetch
'edebug-form-spec
'(body))
443 (defmacro gnus-agent-append-to-list
(tail value
)
444 `(setq ,tail
(setcdr ,tail
(cons ,value nil
))))
446 (defmacro gnus-agent-message
(level &rest args
)
447 `(if (<= ,level gnus-verbose
)
454 (defvar gnus-agent-mode-hook nil
455 "Hook run when installing agent mode.")
457 (defvar gnus-agent-mode nil
)
458 (defvar gnus-agent-mode-status
'(gnus-agent-mode " Plugged"))
460 (defun gnus-agent-mode ()
461 "Minor mode for providing a agent support in Gnus buffers."
462 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
463 (symbol-name major-mode
))
464 (match-string 1 (symbol-name major-mode
))))
465 (mode (intern (format "gnus-agent-%s-mode" buffer
))))
466 (set (make-local-variable 'gnus-agent-mode
) t
)
468 (set (make-local-variable mode
) t
)
470 (when (gnus-visual-p 'agent-menu
'menu
)
471 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer
))))
472 (unless (assq 'gnus-agent-mode minor-mode-alist
)
473 (push gnus-agent-mode-status minor-mode-alist
))
474 (unless (assq mode minor-mode-map-alist
)
475 (push (cons mode
(symbol-value (intern (format "gnus-agent-%s-mode-map"
477 minor-mode-map-alist
))
478 (when (eq major-mode
'gnus-group-mode
)
479 (let ((init-plugged gnus-plugged
)
480 (gnus-agent-go-online nil
))
481 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
482 ;; Therefore, make certain that the current value does not
483 ;; match the desired initial value.
484 (setq gnus-plugged
:unknown
)
485 (gnus-agent-toggle-plugged init-plugged
)))
486 (gnus-run-hooks 'gnus-agent-mode-hook
487 (intern (format "gnus-agent-%s-mode-hook" buffer
)))))
489 (defvar gnus-agent-group-mode-map
(make-sparse-keymap))
490 (gnus-define-keys gnus-agent-group-mode-map
491 "Ju" gnus-agent-fetch-groups
492 "Jc" gnus-enter-category-buffer
493 "Jj" gnus-agent-toggle-plugged
494 "Js" gnus-agent-fetch-session
495 "JY" gnus-agent-synchronize-flags
496 "JS" gnus-group-send-queue
497 "Ja" gnus-agent-add-group
498 "Jr" gnus-agent-remove-group
499 "Jo" gnus-agent-toggle-group-plugged
)
501 (defun gnus-agent-group-make-menu-bar ()
502 (unless (boundp 'gnus-agent-group-menu
)
504 gnus-agent-group-menu gnus-agent-group-mode-map
""
506 ["Toggle plugged" gnus-agent-toggle-plugged t
]
507 ["Toggle group plugged" gnus-agent-toggle-group-plugged t
]
508 ["List categories" gnus-enter-category-buffer t
]
509 ["Add (current) group to category" gnus-agent-add-group t
]
510 ["Remove (current) group from category" gnus-agent-remove-group t
]
511 ["Send queue" gnus-group-send-queue gnus-plugged
]
513 ["All" gnus-agent-fetch-session gnus-plugged
]
514 ["Group" gnus-agent-fetch-group gnus-plugged
])
515 ["Synchronize flags" gnus-agent-synchronize-flags t
]
518 (defvar gnus-agent-summary-mode-map
(make-sparse-keymap))
519 (gnus-define-keys gnus-agent-summary-mode-map
520 "Jj" gnus-agent-toggle-plugged
521 "Ju" gnus-agent-summary-fetch-group
522 "JS" gnus-agent-fetch-group
523 "Js" gnus-agent-summary-fetch-series
524 "J#" gnus-agent-mark-article
525 "J\M-#" gnus-agent-unmark-article
526 "@" gnus-agent-toggle-mark
527 "Jc" gnus-agent-catchup
)
529 (defun gnus-agent-summary-make-menu-bar ()
530 (unless (boundp 'gnus-agent-summary-menu
)
532 gnus-agent-summary-menu gnus-agent-summary-mode-map
""
534 ["Toggle plugged" gnus-agent-toggle-plugged t
]
535 ["Mark as downloadable" gnus-agent-mark-article t
]
536 ["Unmark as downloadable" gnus-agent-unmark-article t
]
537 ["Toggle mark" gnus-agent-toggle-mark t
]
538 ["Fetch downloadable" gnus-agent-summary-fetch-group t
]
539 ["Catchup undownloaded" gnus-agent-catchup t
]))))
541 (defvar gnus-agent-server-mode-map
(make-sparse-keymap))
542 (gnus-define-keys gnus-agent-server-mode-map
543 "Jj" gnus-agent-toggle-plugged
544 "Ja" gnus-agent-add-server
545 "Jr" gnus-agent-remove-server
)
547 (defun gnus-agent-server-make-menu-bar ()
548 (unless (boundp 'gnus-agent-server-menu
)
550 gnus-agent-server-menu gnus-agent-server-mode-map
""
552 ["Toggle plugged" gnus-agent-toggle-plugged t
]
553 ["Add" gnus-agent-add-server t
]
554 ["Remove" gnus-agent-remove-server t
]))))
556 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func
)
557 (if (and (fboundp 'propertize
)
558 (fboundp 'make-mode-line-mouse-map
))
559 (propertize string
'local-map
560 (make-mode-line-mouse-map mouse-button mouse-func
))
563 (defun gnus-agent-toggle-plugged (set-to)
564 "Toggle whether Gnus is unplugged or not."
565 (interactive (list (not gnus-plugged
)))
566 (cond ((eq set-to gnus-plugged
)
569 (setq gnus-plugged set-to
)
570 (gnus-run-hooks 'gnus-agent-plugged-hook
)
571 (setcar (cdr gnus-agent-mode-status
)
572 (gnus-agent-make-mode-line-string " Plugged"
574 'gnus-agent-toggle-plugged
))
575 (gnus-agent-go-online gnus-agent-go-online
)
576 (gnus-agent-possibly-synchronize-flags))
578 (gnus-agent-close-connections)
579 (setq gnus-plugged set-to
)
580 (gnus-run-hooks 'gnus-agent-unplugged-hook
)
581 (setcar (cdr gnus-agent-mode-status
)
582 (gnus-agent-make-mode-line-string " Unplugged"
584 'gnus-agent-toggle-plugged
))))
585 (set-buffer-modified-p t
))
587 (defmacro gnus-agent-while-plugged
(&rest body
)
588 `(let ((original-gnus-plugged gnus-plugged
))
590 (progn (gnus-agent-toggle-plugged t
)
592 (gnus-agent-toggle-plugged original-gnus-plugged
))))
594 (put 'gnus-agent-while-plugged
'lisp-indent-function
0)
595 (put 'gnus-agent-while-plugged
'edebug-form-spec
'(body))
597 (defun gnus-agent-close-connections ()
598 "Close all methods covered by the Gnus agent."
599 (let ((methods (gnus-agent-covered-methods)))
601 (gnus-close-server (pop methods
)))))
604 (defun gnus-unplugged ()
605 "Start Gnus unplugged."
607 (setq gnus-plugged nil
)
611 (defun gnus-plugged ()
612 "Start Gnus plugged."
614 (setq gnus-plugged t
)
618 (defun gnus-slave-unplugged (&optional arg
)
619 "Read news as a slave unplugged."
621 (setq gnus-plugged nil
)
622 (gnus arg nil
'slave
))
625 (defun gnus-agentize ()
626 "Allow Gnus to be an offline newsreader.
628 The gnus-agentize function is now called internally by gnus when
629 gnus-agent is set. If you wish to avoid calling gnus-agentize,
630 customize gnus-agent to nil.
632 This will modify the `gnus-setup-news-hook', and
633 `message-send-mail-real-function' variables, and install the Gnus agent
634 minor mode in all Gnus buffers."
637 (add-hook 'gnus-setup-news-hook
'gnus-agent-queue-setup
)
638 (unless gnus-agent-send-mail-function
639 (setq gnus-agent-send-mail-function
640 (or message-send-mail-real-function
641 (function (lambda () (funcall message-send-mail-function
))))
642 message-send-mail-real-function
'gnus-agent-send-mail
))
644 ;; If the servers file doesn't exist, auto-agentize some servers and
645 ;; save the servers file so this auto-agentizing isn't invoked
647 (unless (file-exists-p (nnheader-concat gnus-agent-directory
"lib/servers"))
648 (gnus-message 3 "First time agent user, agentizing remote groups...")
650 (lambda (server-or-method)
651 (let ((method (gnus-server-to-method server-or-method
)))
652 (when (memq (car method
)
653 gnus-agent-auto-agentize-methods
)
654 (push (gnus-method-to-server method
)
655 gnus-agent-covered-methods
)
656 (setq gnus-agent-method-p-cache nil
))))
657 (cons gnus-select-method gnus-secondary-select-methods
))
658 (gnus-agent-write-servers)))
660 (defun gnus-agent-queue-setup (&optional group-name
)
661 "Make sure the queue group exists.
662 Optional arg GROUP-NAME allows to specify another group."
663 (unless (gnus-gethash (format "nndraft:%s" (or group-name
"queue"))
665 (gnus-request-create-group (or group-name
"queue") '(nndraft ""))
666 (let ((gnus-level-default-subscribed 1))
667 (gnus-subscribe-group (format "nndraft:%s" (or group-name
"queue"))
669 (gnus-group-set-parameter
670 (format "nndraft:%s" (or group-name
"queue"))
671 'gnus-dummy
'((gnus-draft-mode)))))
673 (defun gnus-agent-send-mail ()
674 (if (or (not gnus-agent-queue-mail
)
675 (and gnus-plugged
(not (eq gnus-agent-queue-mail
'always
))))
676 (funcall gnus-agent-send-mail-function
)
677 (goto-char (point-min))
679 (concat "^" (regexp-quote mail-header-separator
) "\n"))
681 (gnus-agent-insert-meta-information 'mail
)
682 (gnus-request-accept-article "nndraft:queue" nil t t
)))
684 (defun gnus-agent-insert-meta-information (type &optional method
)
685 "Insert meta-information into the message that says how it's to be posted.
686 TYPE can be either `mail' or `news'. If the latter, then METHOD can
689 (message-remove-header gnus-agent-meta-information-header
)
690 (goto-char (point-min))
691 (insert gnus-agent-meta-information-header
": "
692 (symbol-name type
) " " (format "%S" method
)
695 (while (search-backward "\n" nil t
)
696 (replace-match "\\n" t t
))))
698 (defun gnus-agent-restore-gcc ()
699 "Restore GCC field from saved header."
701 (goto-char (point-min))
702 (while (re-search-forward
703 (concat "^" (regexp-quote gnus-agent-gcc-header
) ":") nil t
)
704 (replace-match "Gcc:" 'fixedcase
))))
706 (defun gnus-agent-any-covered-gcc ()
708 (message-narrow-to-headers)
709 (let* ((gcc (mail-fetch-field "gcc" nil t
))
711 (mapcar 'gnus-inews-group-method
712 (message-unquote-tokens
713 (message-tokenize-header
716 (while (and (not covered
) methods
)
717 (setq covered
(gnus-agent-method-p (car methods
))
718 methods
(cdr methods
)))
722 (defun gnus-agent-possibly-save-gcc ()
723 "Save GCC if Gnus is unplugged."
724 (when (and (not gnus-plugged
) (gnus-agent-any-covered-gcc))
726 (goto-char (point-min))
727 (let ((case-fold-search t
))
728 (while (re-search-forward "^gcc:" nil t
)
729 (replace-match (concat gnus-agent-gcc-header
":") 'fixedcase
))))))
731 (defun gnus-agent-possibly-do-gcc ()
732 "Do GCC if Gnus is plugged."
733 (when (or gnus-plugged
(not (gnus-agent-any-covered-gcc)))
734 (gnus-inews-do-gcc)))
737 ;;; Group mode commands
740 (defun gnus-agent-fetch-groups (n)
741 "Put all new articles in the current groups into the Agent."
744 (error "Groups can't be fetched when Gnus is unplugged"))
745 (gnus-group-iterate n
'gnus-agent-fetch-group
))
747 (defun gnus-agent-fetch-group (&optional group
)
748 "Put all new articles in GROUP into the Agent."
749 (interactive (list (gnus-group-group-name)))
750 (setq group
(or group gnus-newsgroup-name
))
752 (error "No group on the current line"))
754 (gnus-agent-while-plugged
755 (let ((gnus-command-method (gnus-find-method-for-group group
)))
756 (gnus-agent-with-fetch
757 (gnus-agent-fetch-group-1 group gnus-command-method
)
758 (gnus-message 5 "Fetching %s...done" group
)))))
760 (defun gnus-agent-add-group (category arg
)
761 "Add the current group to an agent category."
767 (mapcar (lambda (cat) (list (symbol-name (car cat
))))
771 (let ((cat (assq category gnus-category-alist
))
773 (gnus-group-iterate arg
775 (when (gnus-agent-cat-groups (setq c
(gnus-group-category group
)))
776 (setf (gnus-agent-cat-groups c
)
777 (delete group
(gnus-agent-cat-groups c
))))
778 (push group groups
)))
779 (setf (gnus-agent-cat-groups cat
)
780 (nconc (gnus-agent-cat-groups cat
) groups
))
781 (gnus-category-write)))
783 (defun gnus-agent-remove-group (arg)
784 "Remove the current group from its agent category, if any."
787 (gnus-group-iterate arg
789 (when (gnus-agent-cat-groups (setq c
(gnus-group-category group
)))
790 (setf (gnus-agent-cat-groups c
)
791 (delete group
(gnus-agent-cat-groups c
))))))
792 (gnus-category-write)))
794 (defun gnus-agent-synchronize-flags ()
795 "Synchronize unplugged flags with servers."
798 (dolist (gnus-command-method (gnus-agent-covered-methods))
799 (when (file-exists-p (gnus-agent-lib-file "flags"))
800 (gnus-agent-synchronize-flags-server gnus-command-method
)))))
802 (defun gnus-agent-possibly-synchronize-flags ()
803 "Synchronize flags according to `gnus-agent-synchronize-flags'."
806 (dolist (gnus-command-method (gnus-agent-covered-methods))
807 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
808 (not (eq (gnus-server-status gnus-command-method
) 'offline
)))
809 (gnus-agent-possibly-synchronize-flags-server gnus-command-method
)))))
811 (defun gnus-agent-synchronize-flags-server (method)
812 "Synchronize flags set when unplugged for server."
813 (let ((gnus-command-method method
)
815 (when (file-exists-p (gnus-agent-lib-file "flags"))
816 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
818 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
819 (cond ((null gnus-plugged
)
821 1 "You must be plugged to synchronize flags with server %s"
822 (nth 1 gnus-command-method
)))
823 ((null (gnus-check-server gnus-command-method
))
825 1 "Couldn't open server %s" (nth 1 gnus-command-method
)))
830 (eval (read (current-buffer)))
831 (delete-region bgn
(point))))
833 (delete-file (gnus-agent-lib-file "flags")))
835 (let ((file (gnus-agent-lib-file "flags")))
836 (write-region (point-min) (point-max)
837 (gnus-agent-lib-file "flags") nil
'silent
)
838 (error "Couldn't set flags from file %s due to %s"
839 file
(error-message-string err
)))))))
842 (defun gnus-agent-possibly-synchronize-flags-server (method)
843 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
844 (when (or (and gnus-agent-synchronize-flags
845 (not (eq gnus-agent-synchronize-flags
'ask
)))
846 (and (eq gnus-agent-synchronize-flags
'ask
)
847 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
849 (gnus-agent-synchronize-flags-server method
)))
852 (defun gnus-agent-rename-group (old-group new-group
)
853 "Rename fully-qualified OLD-GROUP as NEW-GROUP. Always updates the agent, even when
854 disabled, as the old agent files would corrupt gnus when the agent was
855 next enabled. Depends upon the caller to determine whether group renaming is supported."
856 (let* ((old-command-method (gnus-find-method-for-group old-group
))
857 (old-path (directory-file-name
858 (let (gnus-command-method old-command-method
)
859 (gnus-agent-group-pathname old-group
))))
860 (new-command-method (gnus-find-method-for-group new-group
))
861 (new-path (directory-file-name
862 (let (gnus-command-method new-command-method
)
863 (gnus-agent-group-pathname new-group
)))))
864 (gnus-rename-file old-path new-path t
)
866 (let* ((old-real-group (gnus-group-real-name old-group
))
867 (new-real-group (gnus-group-real-name new-group
))
868 (old-active (gnus-agent-get-group-info old-command-method old-real-group
)))
869 (gnus-agent-save-group-info old-command-method old-real-group nil
)
870 (gnus-agent-save-group-info new-command-method new-real-group old-active
)
872 (let ((old-local (gnus-agent-get-local old-group
873 old-real-group old-command-method
)))
874 (gnus-agent-set-local old-group
876 old-real-group old-command-method
)
877 (gnus-agent-set-local new-group
878 (car old-local
) (cdr old-local
)
879 new-real-group new-command-method
)))))
882 (defun gnus-agent-delete-group (group)
883 "Delete fully-qualified GROUP. Always updates the agent, even when
884 disabled, as the old agent files would corrupt gnus when the agent was
885 next enabled. Depends upon the caller to determine whether group deletion is supported."
886 (let* ((command-method (gnus-find-method-for-group group
))
887 (path (directory-file-name
888 (let (gnus-command-method command-method
)
889 (gnus-agent-group-pathname group
)))))
890 (gnus-delete-file path
)
892 (let* ((real-group (gnus-group-real-name group
)))
893 (gnus-agent-save-group-info command-method real-group nil
)
895 (let ((local (gnus-agent-get-local group
896 real-group command-method
)))
897 (gnus-agent-set-local group
899 real-group command-method
)))))
902 ;;; Server mode commands
905 (defun gnus-agent-add-server ()
906 "Enroll SERVER in the agent program."
908 (let* ((server (gnus-server-server-name))
909 (named-server (gnus-server-named-server))
911 (gnus-server-get-method nil server
))))
913 (error "No server on the current line"))
915 (when (gnus-agent-method-p method
)
916 (error "Server already in the agent program"))
918 (push named-server gnus-agent-covered-methods
)
920 (setq gnus-agent-method-p-cache nil
)
921 (gnus-server-update-server server
)
922 (gnus-agent-write-servers)
923 (gnus-message 1 "Entered %s into the Agent" server
)))
925 (defun gnus-agent-remove-server ()
926 "Remove SERVER from the agent program."
928 (let* ((server (gnus-server-server-name))
929 (named-server (gnus-server-named-server)))
931 (error "No server on the current line"))
933 (unless (member named-server gnus-agent-covered-methods
)
934 (error "Server not in the agent program"))
936 (setq gnus-agent-covered-methods
937 (delete named-server gnus-agent-covered-methods
)
938 gnus-agent-method-p-cache nil
)
940 (gnus-server-update-server server
)
941 (gnus-agent-write-servers)
942 (gnus-message 1 "Removed %s from the agent" server
)))
944 (defun gnus-agent-read-servers ()
945 "Read the alist of covered servers."
946 (setq gnus-agent-covered-methods
947 (gnus-agent-read-file
948 (nnheader-concat gnus-agent-directory
"lib/servers"))
949 gnus-agent-method-p-cache nil
)
951 ;; I am called so early in start-up that I can not validate server
952 ;; names. When that is the case, I skip the validation. That is
953 ;; alright as the gnus startup code calls the validate methods
955 (if gnus-server-alist
956 (gnus-agent-read-servers-validate)))
958 (defun gnus-agent-read-servers-validate ()
959 (mapcar (lambda (server-or-method)
960 (let* ((server (if (stringp server-or-method
)
962 (gnus-method-to-server server-or-method
)))
963 (method (gnus-server-to-method server
)))
965 (unless (member server gnus-agent-covered-methods
)
966 (push server gnus-agent-covered-methods
)
967 (setq gnus-agent-method-p-cache nil
))
968 (gnus-message 1 "Ignoring disappeared server `%s'" server
))))
969 (prog1 gnus-agent-covered-methods
970 (setq gnus-agent-covered-methods nil
))))
972 (defun gnus-agent-read-servers-validate-native (native-method)
973 (setq gnus-agent-covered-methods
974 (mapcar (lambda (method)
976 (equal method native-method
))
978 method
)) gnus-agent-covered-methods
)))
980 (defun gnus-agent-write-servers ()
981 "Write the alist of covered servers."
982 (gnus-make-directory (nnheader-concat gnus-agent-directory
"lib"))
983 (let ((coding-system-for-write nnheader-file-coding-system
)
984 (file-name-coding-system nnmail-pathname-coding-system
))
985 (with-temp-file (nnheader-concat gnus-agent-directory
"lib/servers")
986 (prin1 gnus-agent-covered-methods
993 (defun gnus-agent-mark-article (n &optional unmark
)
994 "Mark the next N articles as downloadable.
995 If N is negative, mark backward instead. If UNMARK is non-nil, remove
996 the mark instead. The difference between N and the actual number of
997 articles marked is returned."
999 (let ((backward (< n
0))
1004 (gnus-summary-set-agent-mark
1005 (gnus-summary-article-number) unmark
)
1006 (zerop (gnus-summary-next-subject (if backward -
1 1) nil t
))))
1009 (gnus-message 7 "No more articles"))
1010 (gnus-summary-recenter)
1011 (gnus-summary-position-point)
1014 (defun gnus-agent-unmark-article (n)
1015 "Remove the downloadable mark from the next N articles.
1016 If N is negative, unmark backward instead. The difference between N and
1017 the actual number of articles unmarked is returned."
1019 (gnus-agent-mark-article n t
))
1021 (defun gnus-agent-toggle-mark (n)
1022 "Toggle the downloadable mark from the next N articles.
1023 If N is negative, toggle backward instead. The difference between N and
1024 the actual number of articles toggled is returned."
1026 (gnus-agent-mark-article n
'toggle
))
1028 (defun gnus-summary-set-agent-mark (article &optional unmark
)
1029 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1030 When UNMARK is t, the article is unmarked. For any other value, the
1031 article's mark is toggled."
1032 (let ((unmark (cond ((eq nil unmark
)
1037 (memq article gnus-newsgroup-downloadable
)))))
1038 (when (gnus-summary-goto-subject article nil t
)
1039 (gnus-summary-update-mark
1042 (setq gnus-newsgroup-downloadable
1043 (delq article gnus-newsgroup-downloadable
))
1044 (gnus-article-mark article
))
1045 (setq gnus-newsgroup-downloadable
1046 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article
))
1047 gnus-downloadable-mark
)
1051 (defun gnus-agent-get-undownloaded-list ()
1052 "Construct list of articles that have not been downloaded."
1053 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name
)))
1054 (when (set (make-local-variable 'gnus-newsgroup-agentized
)
1055 (gnus-agent-method-p gnus-command-method
))
1056 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name
))
1057 (headers (sort (mapcar (lambda (h)
1058 (mail-header-number h
))
1059 gnus-newsgroup-headers
) '<))
1060 (cached (and gnus-use-cache gnus-newsgroup-cached
))
1061 (undownloaded (list nil
))
1062 (tail-undownloaded undownloaded
)
1063 (unfetched (list nil
))
1064 (tail-unfetched unfetched
))
1065 (while (and alist headers
)
1066 (let ((a (caar alist
))
1069 ;; Ignore IDs in the alist that are not being
1070 ;; displayed in the summary.
1071 (setq alist
(cdr alist
)))
1073 ;; Headers that are not in the alist should be
1074 ;; fictious (see nnagent-retrieve-headers); they
1075 ;; imply that this article isn't in the agent.
1076 (gnus-agent-append-to-list tail-undownloaded h
)
1077 (gnus-agent-append-to-list tail-unfetched h
)
1078 (setq headers
(cdr headers
)))
1080 (setq alist
(cdr alist
))
1081 (setq headers
(cdr headers
))
1082 nil
; ignore already downloaded
1085 (setq alist
(cdr alist
))
1086 (setq headers
(cdr headers
))
1088 ;; This article isn't in the agent. Check to see
1089 ;; if it is in the cache. If it is, it's been
1091 (while (and cached
(< (car cached
) a
))
1092 (setq cached
(cdr cached
)))
1093 (unless (equal a
(car cached
))
1094 (gnus-agent-append-to-list tail-undownloaded a
))))))
1097 (let ((num (pop headers
)))
1098 (gnus-agent-append-to-list tail-undownloaded num
)
1099 (gnus-agent-append-to-list tail-unfetched num
)))
1101 (setq gnus-newsgroup-undownloaded
(cdr undownloaded
)
1102 gnus-newsgroup-unfetched
(cdr unfetched
))))))
1104 (defun gnus-agent-catchup ()
1105 "Mark as read all unhandled articles.
1106 An article is unhandled if it is neither cached, nor downloaded, nor
1110 (let ((articles gnus-newsgroup-undownloaded
))
1111 (when (or gnus-newsgroup-downloadable
1112 gnus-newsgroup-cached
)
1113 (setq articles
(gnus-sorted-ndifference
1114 (gnus-sorted-ndifference
1115 (gnus-copy-sequence articles
)
1116 gnus-newsgroup-downloadable
)
1117 gnus-newsgroup-cached
)))
1120 (gnus-summary-mark-article
1121 (pop articles
) gnus-catchup-mark
)))
1122 (gnus-summary-position-point)))
1124 (defun gnus-agent-summary-fetch-series ()
1126 (when gnus-newsgroup-processable
1127 (setq gnus-newsgroup-downloadable
1128 (let* ((dl gnus-newsgroup-downloadable
)
1129 (gnus-newsgroup-downloadable
1130 (sort (gnus-copy-sequence gnus-newsgroup-processable
) '<))
1131 (fetched-articles (gnus-agent-summary-fetch-group)))
1132 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1133 ;; updated gnus-newsgroup-downloadable to remove each
1134 ;; article successfully fetched.
1136 ;; For each article that I processed, remove its
1137 ;; processable mark IF the article is no longer
1138 ;; downloadable (i.e. it's already downloaded)
1139 (dolist (article gnus-newsgroup-processable
)
1140 (unless (memq article gnus-newsgroup-downloadable
)
1141 (gnus-summary-remove-process-mark article
)))
1142 (gnus-sorted-ndifference dl fetched-articles
)))))
1144 (defun gnus-agent-summary-fetch-group (&optional all
)
1145 "Fetch the downloadable articles in the group.
1146 Optional arg ALL, if non-nil, means to fetch all articles."
1149 (if all gnus-newsgroup-articles
1150 gnus-newsgroup-downloadable
))
1151 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name
))
1153 (gnus-agent-while-plugged
1155 (error "No articles to download"))
1156 (gnus-agent-with-fetch
1157 (setq gnus-newsgroup-undownloaded
1158 (gnus-sorted-ndifference
1159 gnus-newsgroup-undownloaded
1160 (setq fetched-articles
1161 (gnus-agent-fetch-articles
1162 gnus-newsgroup-name articles
)))))
1164 (dolist (article articles
)
1165 (let ((was-marked-downloadable
1166 (memq article gnus-newsgroup-downloadable
)))
1167 (cond (gnus-agent-mark-unread-after-downloaded
1168 (setq gnus-newsgroup-downloadable
1169 (delq article gnus-newsgroup-downloadable
))
1171 (gnus-summary-mark-article article gnus-unread-mark
))
1172 (was-marked-downloadable
1173 (gnus-summary-set-agent-mark article t
)))
1174 (when (gnus-summary-goto-subject article nil t
)
1175 (gnus-summary-update-download-mark article
))))))
1178 (defun gnus-agent-fetch-selected-article ()
1179 "Fetch the current article as it is selected.
1180 This can be added to `gnus-select-article-hook' or
1181 `gnus-mark-article-hook'."
1182 (let ((gnus-command-method gnus-current-select-method
))
1183 (when (and gnus-plugged
(gnus-agent-method-p gnus-command-method
))
1184 (when (gnus-agent-fetch-articles
1186 (list gnus-current-article
))
1187 (setq gnus-newsgroup-undownloaded
1188 (delq gnus-current-article gnus-newsgroup-undownloaded
))
1189 (gnus-summary-update-download-mark gnus-current-article
)))))
1192 ;;; Internal functions
1195 (defun gnus-agent-synchronize-group-flags (group actions server
)
1196 "Update a plugged group by performing the indicated actions."
1197 (let* ((gnus-command-method (gnus-server-to-method server
))
1199 ;; This initializer is required as gnus-request-set-mark
1200 ;; calls gnus-group-real-name to strip off the host name
1201 ;; before calling the backend. Now that the backend is
1202 ;; trying to call gnus-request-set-mark, I have to
1203 ;; reconstruct the original group name.
1204 (or (gnus-get-info group
)
1206 (setq group
(gnus-group-full-name
1207 group gnus-command-method
))))))
1208 (gnus-request-set-mark group actions
)
1211 (dolist (action actions
)
1212 (let ((range (nth 0 action
))
1213 (what (nth 1 action
))
1214 (marks (nth 2 action
)))
1215 (dolist (mark marks
)
1216 (cond ((eq mark
'read
)
1219 (funcall (if (eq what
'add
)
1221 'gnus-remove-from-range
)
1222 (gnus-info-read info
)
1224 (gnus-get-unread-articles-in-group
1226 (gnus-active (gnus-info-group info
))))
1227 ((memq mark
'(tick))
1228 (let ((info-marks (assoc mark
(gnus-info-marks info
))))
1230 (gnus-info-set-marks info
(cons (setq info-marks
(list mark
)) (gnus-info-marks info
))))
1231 (setcdr info-marks
(funcall (if (eq what
'add
)
1233 'gnus-remove-from-range
)
1238 (defun gnus-agent-save-active (method)
1239 (when (gnus-agent-method-p method
)
1240 (let* ((gnus-command-method method
)
1241 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1242 (file (gnus-agent-lib-file "active")))
1243 (gnus-active-to-gnus-format nil new
)
1244 (gnus-agent-write-active file new
)
1246 (nnheader-insert-file-contents file
))))
1248 (defun gnus-agent-write-active (file new
)
1249 (gnus-make-directory (file-name-directory file
))
1250 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system
))
1251 ;; The hashtable contains real names of groups. However, do NOT
1252 ;; add the foreign server prefix as gnus-active-to-gnus-format
1253 ;; will add it while reading the file.
1254 (gnus-write-active-file file new nil
)))
1257 (defun gnus-agent-possibly-alter-active (group active
&optional info
)
1258 "Possibly expand a group's active range to include articles
1259 downloaded into the agent."
1260 (let* ((gnus-command-method (or gnus-command-method
1261 (gnus-find-method-for-group group
))))
1262 (when (gnus-agent-method-p gnus-command-method
)
1263 (let* ((local (gnus-agent-get-local group
))
1264 (active-min (or (car active
) 0))
1265 (active-max (or (cdr active
) 0))
1266 (agent-min (or (car local
) active-min
))
1267 (agent-max (or (cdr local
) active-max
)))
1269 (when (< agent-min active-min
)
1270 (setcar active agent-min
))
1272 (when (> agent-max active-max
)
1273 (setcdr active agent-max
))
1275 (when (and info
(< agent-max
(- active-min
100)))
1276 ;; I'm expanding the active range by such a large amount
1277 ;; that there is a gap of more than 100 articles between the
1278 ;; last article known to the agent and the first article
1279 ;; currently available on the server. This gap contains
1280 ;; articles that have been lost, mark them as read so that
1281 ;; gnus doesn't waste resources trying to fetch them.
1283 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1284 ;; want to modify the local file everytime someone restarts
1285 ;; gnus. The small gap will cause a tiny performance hit
1286 ;; when gnus tries, and fails, to retrieve the articles.
1287 ;; Still that should be smaller than opening a buffer,
1288 ;; printing this list to the buffer, and then writing it to a
1291 (let ((read (gnus-info-read info
)))
1296 (list (cons (1+ agent-max
)
1297 (1- active-min
))))))
1299 ;; Lie about the agent's local range for this group to
1300 ;; disable the set read each time this server is opened.
1301 ;; NOTE: Opening this group will restore the valid local
1302 ;; range but it will also expand the local range to
1303 ;; incompass the new active range.
1304 (gnus-agent-set-local group agent-min
(1- active-min
)))))))
1306 (defun gnus-agent-save-group-info (method group active
)
1307 "Update a single group's active range in the agent's copy of the server's active file."
1308 (when (gnus-agent-method-p method
)
1309 (let* ((gnus-command-method (or method gnus-command-method
))
1310 (coding-system-for-write nnheader-file-coding-system
)
1311 (file-name-coding-system nnmail-pathname-coding-system
)
1312 (file (gnus-agent-lib-file "active"))
1313 oactive-min oactive-max
)
1314 (gnus-make-directory (file-name-directory file
))
1315 (with-temp-file file
1316 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1317 (mm-disable-multibyte)
1318 (when (file-exists-p file
)
1319 (nnheader-insert-file-contents file
)
1321 (goto-char (point-min))
1322 (when (re-search-forward
1323 (concat "^" (regexp-quote group
) " ") nil t
)
1325 (setq oactive-max
(read (current-buffer)) ;; max
1326 oactive-min
(read (current-buffer)))) ;; min
1327 (gnus-delete-line)))
1329 (insert (format "%S %d %d y\n" (intern group
)
1330 (max (or oactive-max
(cdr active
)) (cdr active
))
1331 (min (or oactive-min
(car active
)) (car active
))))
1332 (goto-char (point-max))
1333 (while (search-backward "\\." nil t
)
1334 (delete-char 1)))))))
1336 (defun gnus-agent-get-group-info (method group
)
1337 "Get a single group's active range in the agent's copy of the server's active file."
1338 (when (gnus-agent-method-p method
)
1339 (let* ((gnus-command-method (or method gnus-command-method
))
1340 (coding-system-for-write nnheader-file-coding-system
)
1341 (file-name-coding-system nnmail-pathname-coding-system
)
1342 (file (gnus-agent-lib-file "active"))
1343 oactive-min oactive-max
)
1344 (gnus-make-directory (file-name-directory file
))
1346 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
1347 (mm-disable-multibyte)
1348 (when (file-exists-p file
)
1349 (nnheader-insert-file-contents file
)
1351 (goto-char (point-min))
1352 (when (re-search-forward
1353 (concat "^" (regexp-quote group
) " ") nil t
)
1355 (setq oactive-max
(read (current-buffer)) ;; max
1356 oactive-min
(read (current-buffer))) ;; min
1357 (cons oactive-min oactive-max
))))))))
1359 (defun gnus-agent-group-path (group)
1360 "Translate GROUP into a file name."
1362 ;; NOTE: This is what nnmail-group-pathname does as of Apr 2003.
1363 ;; The two methods must be kept synchronized, which is why
1364 ;; gnus-agent-group-pathname was added.
1367 (nnheader-translate-file-chars
1368 (nnheader-replace-duplicate-chars-in-string
1369 (nnheader-replace-chars-in-string
1370 (gnus-group-real-name group
)
1373 (if (or nnmail-use-long-file-names
1374 (file-directory-p (expand-file-name group
(gnus-agent-directory))))
1376 (mm-encode-coding-string
1377 (nnheader-replace-chars-in-string group ?. ?
/)
1378 nnmail-pathname-coding-system
)))
1380 (defun gnus-agent-group-pathname (group)
1381 "Translate GROUP into a file name."
1382 ;; nnagent uses nnmail-group-pathname to read articles while
1383 ;; unplugged. The agent must, therefore, use the same directory
1385 (let ((gnus-command-method (or gnus-command-method
1386 (gnus-find-method-for-group group
))))
1387 (nnmail-group-pathname (gnus-group-real-name group
) (gnus-agent-directory))))
1389 (defun gnus-agent-get-function (method)
1390 (if (gnus-online method
)
1395 (defun gnus-agent-covered-methods ()
1396 "Return the subset of methods that are covered by the agent."
1397 (delq nil
(mapcar #'gnus-server-to-method gnus-agent-covered-methods
)))
1399 ;;; History functions
1401 (defun gnus-agent-history-buffer ()
1402 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers
)))
1404 (defun gnus-agent-open-history ()
1406 (push (cons (gnus-agent-method)
1407 (set-buffer (gnus-get-buffer-create
1408 (format " *Gnus agent %s history*"
1409 (gnus-agent-method)))))
1410 gnus-agent-history-buffers
)
1411 (mm-disable-multibyte) ;; everything is binary
1414 (let ((file (gnus-agent-lib-file "history")))
1415 (when (file-exists-p file
)
1416 (nnheader-insert-file-contents file
))
1417 (set (make-local-variable 'gnus-agent-file-name
) file
))))
1419 (defun gnus-agent-close-history ()
1420 (when (gnus-buffer-live-p gnus-agent-current-history
)
1421 (kill-buffer gnus-agent-current-history
)
1422 (setq gnus-agent-history-buffers
1423 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers
)
1424 gnus-agent-history-buffers
))))
1430 (defun gnus-agent-fetch-articles (group articles
)
1431 "Fetch ARTICLES from GROUP and put them into the Agent."
1433 (gnus-agent-load-alist group
)
1434 (let* ((alist gnus-agent-article-alist
)
1435 (headers (if (< (length articles
) 2) nil gnus-newsgroup-headers
))
1436 (selected-sets (list nil
))
1437 (current-set-size 0)
1440 ;; Check each article
1441 (while (setq article
(pop articles
))
1442 ;; Skip alist entries preceeding this article
1443 (while (> article
(or (caar alist
) (1+ article
)))
1444 (setq alist
(cdr alist
)))
1446 ;; Prune off articles that we have already fetched.
1447 (unless (and (eq article
(caar alist
))
1449 ;; Skip headers preceeding this article
1452 (let* ((header (car headers
)))
1454 (mail-header-number header
)
1456 (setq headers
(cdr headers
)))
1458 ;; Add this article to the current set
1459 (setcar selected-sets
(cons article
(car selected-sets
)))
1461 ;; Update the set size, when the set is too large start a
1462 ;; new one. I do this after adding the article as I want at
1463 ;; least one article in each set.
1464 (when (< gnus-agent-max-fetch-size
1465 (setq current-set-size
1467 (if (= header-number article
)
1468 (let ((char-size (mail-header-chars
1470 (if (<= char-size
0)
1471 ;; The char size was missing/invalid,
1472 ;; assume a worst-case situation of
1473 ;; 65 char/line. If the line count
1474 ;; is missing, arbitrarily assume a
1475 ;; size of 1000 characters.
1476 (max (* 65 (mail-header-lines
1481 (setcar selected-sets
(nreverse (car selected-sets
)))
1482 (setq selected-sets
(cons nil selected-sets
)
1483 current-set-size
0))))
1485 (when (or (cdr selected-sets
) (car selected-sets
))
1486 (let* ((fetched-articles (list nil
))
1487 (tail-fetched-articles fetched-articles
)
1488 (dir (gnus-agent-group-pathname group
))
1489 (date (time-to-days (current-time)))
1490 (case-fold-search t
)
1493 (setcar selected-sets
(nreverse (car selected-sets
)))
1494 (setq selected-sets
(nreverse selected-sets
))
1496 (gnus-make-directory dir
)
1497 (gnus-message 7 "Fetching articles for %s..." group
)
1500 (while (setq articles
(pop selected-sets
))
1501 ;; Fetch the articles from the backend.
1502 (if (gnus-check-backend-function 'retrieve-articles group
)
1503 (setq pos
(gnus-retrieve-articles articles group
))
1506 (while (setq article
(pop articles
))
1507 (gnus-message 10 "Fetching article %s for %s..."
1510 (gnus-backlog-request-article group article
1512 (gnus-request-article article group
))
1513 (goto-char (point-max))
1514 (push (cons article
(point)) pos
)
1515 (insert-buffer-substring nntp-server-buffer
)))
1517 nntp-server-buffer
(point-min) (point-max))
1518 (setq pos
(nreverse pos
)))))
1519 ;; Then save these articles into the Agent.
1521 (set-buffer nntp-server-buffer
)
1523 (narrow-to-region (cdar pos
) (or (cdadr pos
) (point-max)))
1524 (goto-char (point-min))
1525 (unless (eobp) ;; Don't save empty articles.
1526 (when (search-forward "\n\n" nil t
)
1527 (when (search-backward "\nXrefs: " nil t
)
1528 ;; Handle cross posting.
1529 (goto-char (match-end 0)) ; move to end of header name
1530 (skip-chars-forward "^ ") ; skip server name
1531 (skip-chars-forward " ")
1533 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) *")
1534 (push (cons (buffer-substring (match-beginning 1)
1537 (buffer-substring (match-beginning 2)
1540 (goto-char (match-end 0)))
1541 (gnus-agent-crosspost crosses
(caar pos
) date
)))
1542 (goto-char (point-min))
1543 (if (not (re-search-forward
1544 "^Message-ID: *<\\([^>\n]+\\)>" nil t
))
1545 (setq id
"No-Message-ID-in-article")
1546 (setq id
(buffer-substring
1547 (match-beginning 1) (match-end 1))))
1548 (let ((coding-system-for-write
1549 gnus-agent-file-coding-system
))
1550 (write-region (point-min) (point-max)
1551 (concat dir
(number-to-string (caar pos
)))
1554 (gnus-agent-append-to-list
1555 tail-fetched-articles
(caar pos
)))
1557 (setq pos
(cdr pos
)))))
1559 (gnus-agent-save-alist group
(cdr fetched-articles
) date
)
1560 (gnus-message 7 ""))
1561 (cdr fetched-articles
))))))
1563 (defun gnus-agent-unfetch-articles (group articles
)
1564 "Delete ARTICLES that were fetched from GROUP into the agent."
1566 (gnus-agent-load-alist group
)
1567 (let* ((alist (cons nil gnus-agent-article-alist
))
1568 (articles (sort articles
#'<))
1569 (next-possibility alist
)
1570 (delete-this (pop articles
)))
1571 (while (and (cdr next-possibility
) delete-this
)
1572 (let ((have-this (caar (cdr next-possibility
))))
1573 (cond ((< delete-this have-this
)
1574 (setq delete-this
(pop articles
)))
1575 ((= delete-this have-this
)
1576 (let ((timestamp (cdar (cdr next-possibility
))))
1578 (let* ((file-name (concat (gnus-agent-group-pathname group
)
1579 (number-to-string have-this
))))
1580 (delete-file file-name
))))
1582 (setcdr next-possibility
(cddr next-possibility
)))
1584 (setq next-possibility
(cdr next-possibility
))))))
1585 (setq gnus-agent-article-alist
(cdr alist
))
1586 (gnus-agent-save-alist group
))))
1588 (defun gnus-agent-crosspost (crosses article
&optional date
)
1589 (setq date
(or date t
))
1591 (let (gnus-agent-article-alist group alist beg end
)
1593 (set-buffer gnus-agent-overview-buffer
)
1594 (when (nnheader-find-nov-line article
)
1597 (setq end
(progn (forward-line 1) (point)))))
1599 (setq group
(caar crosses
))
1600 (unless (setq alist
(assoc group gnus-agent-group-alist
))
1601 (push (setq alist
(list group
(gnus-agent-load-alist (caar crosses
))))
1602 gnus-agent-group-alist
))
1603 (setcdr alist
(cons (cons (cdar crosses
) date
) (cdr alist
)))
1605 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1607 (when (= (point-max) (point-min))
1608 (push (cons group
(current-buffer)) gnus-agent-buffer-alist
)
1610 (nnheader-insert-file-contents
1611 (gnus-agent-article-name ".overview" group
))))
1612 (nnheader-find-nov-line (string-to-number (cdar crosses
)))
1613 (insert (string-to-number (cdar crosses
)))
1614 (insert-buffer-substring gnus-agent-overview-buffer beg end
)
1615 (gnus-agent-check-overview-buffer))
1616 (setq crosses
(cdr crosses
)))))
1618 (defun gnus-agent-backup-overview-buffer ()
1619 (when gnus-newsgroup-name
1620 (let ((root (gnus-agent-article-name ".overview" gnus-newsgroup-name
))
1623 (while (file-exists-p
1624 (setq name
(concat root
"~"
1625 (int-to-string (setq cnt
(1+ cnt
))) "~"))))
1626 (write-region (point-min) (point-max) name nil
'no-msg
)
1627 (gnus-message 1 "Created backup copy of overview in %s." name
)))
1630 (defun gnus-agent-check-overview-buffer (&optional buffer
)
1631 "Check the overview file given for sanity.
1632 In particular, checks that the file is sorted by article number
1633 and that there are no duplicates."
1638 (set-buffer buffer
))
1641 (goto-char (point-min))
1643 (while (< (point) (point-max))
1645 (cur (condition-case nil
1646 (read (current-buffer))
1649 ((or (not (integerp cur
))
1650 (not (eq (char-after) ?
\t)))
1652 (setq backed-up
(gnus-agent-backup-overview-buffer)))
1654 "Overview buffer contains garbage '%s'."
1656 p
(gnus-point-at-eol))))
1659 (setq backed-up
(gnus-agent-backup-overview-buffer)))
1661 "Duplicate overview line for %d" cur
)
1662 (delete-region p
(progn (forward-line 1) (point))))
1665 (setq backed-up
(gnus-agent-backup-overview-buffer)))
1666 (gnus-message 1 "Overview buffer not sorted!")
1667 (sort-numeric-fields 1 (point-min) (point-max))
1668 (goto-char (point-min))
1671 (setq prev-num cur
)))
1672 (forward-line 1)))))))
1674 (defun gnus-agent-flush-cache ()
1676 (while gnus-agent-buffer-alist
1677 (set-buffer (cdar gnus-agent-buffer-alist
))
1678 (let ((coding-system-for-write
1679 gnus-agent-file-coding-system
))
1680 (write-region (point-min) (point-max)
1681 (gnus-agent-article-name ".overview"
1682 (caar gnus-agent-buffer-alist
))
1684 (setq gnus-agent-buffer-alist
(cdr gnus-agent-buffer-alist
)))
1685 (while gnus-agent-group-alist
1686 (with-temp-file (gnus-agent-article-name
1687 ".agentview" (caar gnus-agent-group-alist
))
1688 (princ (cdar gnus-agent-group-alist
))
1690 (princ 1 (current-buffer))
1692 (setq gnus-agent-group-alist
(cdr gnus-agent-group-alist
)))))
1695 (defun gnus-agent-find-parameter (group symbol
)
1696 "Search for GROUPs SYMBOL in the group's parameters, the group's
1697 topic parameters, the group's category, or the customizable
1698 variables. Returns the first non-nil value found."
1699 (or (gnus-group-find-parameter group symbol t
)
1700 (gnus-group-parameter-value (cdr (gnus-group-category group
)) symbol t
)
1704 '((agent-short-article . gnus-agent-short-article
)
1705 (agent-long-article . gnus-agent-long-article
)
1706 (agent-low-score . gnus-agent-low-score
)
1707 (agent-high-score . gnus-agent-high-score
)
1708 (agent-days-until-old . gnus-agent-expire-days
)
1709 (agent-enable-expiration
1710 . gnus-agent-enable-expiration
)
1711 (agent-predicate . gnus-agent-predicate
)))))))
1713 (defun gnus-agent-fetch-headers (group &optional force
)
1714 "Fetch interesting headers into the agent. The group's overview
1715 file will be updated to include the headers while a list of available
1716 article numbers will be returned."
1717 (let* ((fetch-all (and gnus-agent-consider-all-articles
1718 ;; Do not fetch all headers if the predicate
1719 ;; implies that we only consider unread articles.
1720 (not (gnus-predicate-implies-unread
1721 (gnus-agent-find-parameter group
1722 'agent-predicate
)))))
1723 (articles (if fetch-all
1724 (gnus-uncompress-range (gnus-active group
))
1725 (gnus-list-of-unread-articles group
)))
1726 (gnus-decode-encoded-word-function 'identity
)
1727 (file (gnus-agent-article-name ".overview" group
)))
1730 ;; Add articles with marks to the list of article headers we want to
1731 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1732 ;; mark, but do fetch recent or seen articles if they have other, more
1733 ;; interesting marks. (We have to fetch articles with boring marks
1734 ;; because otherwise the agent will remove their marks.)
1735 (dolist (arts (gnus-info-marks (gnus-get-info group
)))
1736 (unless (memq (car arts
) '(seen recent killed cache
))
1737 (setq articles
(gnus-range-add articles
(cdr arts
)))))
1738 (setq articles
(sort (gnus-uncompress-sequence articles
) '<)))
1740 ;; At this point, I have the list of articles to consider for
1741 ;; fetching. This is the list that I'll return to my caller. Some
1742 ;; of these articles may have already been fetched. That's OK as
1743 ;; the fetch article code will filter those out. Internally, I'll
1744 ;; filter this list to just those articles whose headers need to
1746 (let ((articles articles
))
1747 ;; Remove known articles.
1748 (when (and (or gnus-agent-cache
1750 (gnus-agent-load-alist group
))
1751 ;; Remove articles marked as downloaded.
1753 ;; I want to fetch all headers in the active range.
1754 ;; Therefore, exclude only those headers that are in the
1756 ;; NOTE: This is probably NOT what I want to do after
1757 ;; agent expiration in this group.
1758 (setq articles
(gnus-agent-uncached-articles articles group
))
1760 ;; I want to only fetch those headers that have never been
1761 ;; fetched. Therefore, exclude all headers that are, or
1762 ;; WERE, in the article alist.
1763 (let ((low (1+ (caar (last gnus-agent-article-alist
))))
1764 (high (cdr (gnus-active group
))))
1765 ;; Low can be greater than High when the same group is
1766 ;; fetched twice in the same session {The first fetch will
1767 ;; fill the article alist such that (last
1768 ;; gnus-agent-article-alist) equals (cdr (gnus-active
1769 ;; group))}. The addition of one(the 1+ above) then
1770 ;; forces Low to be greater than High. When this happens,
1771 ;; gnus-list-range-intersection returns nil which
1772 ;; indicates that no headers need to be fetched. -- Kevin
1773 (setq articles
(gnus-list-range-intersection
1774 articles
(list (cons low high
)))))))
1777 10 "gnus-agent-fetch-headers: undownloaded articles are '%s'"
1778 (gnus-compress-sequence articles t
))
1781 (set-buffer nntp-server-buffer
)
1785 (gnus-message 7 "Fetching headers for %s..." group
)
1788 (gnus-make-directory (nnheader-translate-file-chars
1789 (file-name-directory file
) t
))
1791 (unless (eq 'nov
(gnus-retrieve-headers articles group
))
1792 (nnvirtual-convert-headers))
1793 (gnus-agent-check-overview-buffer)
1794 ;; Move these headers to the overview buffer so that
1795 ;; gnus-agent-braid-nov can merge them with the contents
1798 gnus-agent-overview-buffer
(point-min) (point-max))
1799 ;; NOTE: Call g-a-brand-nov even when the file does not
1800 ;; exist. As a minimum, it will validate the article
1801 ;; numbers already in the buffer.
1802 (gnus-agent-braid-nov group articles file
)
1803 (let ((coding-system-for-write
1804 gnus-agent-file-coding-system
))
1805 (gnus-agent-check-overview-buffer)
1806 (write-region (point-min) (point-max) file nil
'silent
))
1807 (gnus-agent-save-alist group articles nil
)
1811 (nnheader-insert-file-contents file
)))))
1814 (defsubst gnus-agent-read-article-number
()
1815 "Reads the article number at point. Returns nil when a valid article number can not be read."
1817 ;; It is unfortunite but the read function quietly overflows
1818 ;; integer. As a result, I have to use string operations to test
1819 ;; for overflow BEFORE calling read.
1820 (when (looking-at "[0-9]+\t")
1821 (let ((len (- (match-end 0) (match-beginning 0))))
1823 (read (current-buffer)))
1825 ;; Many 9 digit base-10 numbers can be represented in a 27-bit int
1826 ;; Back convert from int to string to ensure that this is one of them.
1827 (let* ((str1 (buffer-substring (match-beginning 0) (1- (match-end 0))))
1828 (num (read (current-buffer)))
1829 (str2 (int-to-string num
)))
1830 (when (equal str1 str2
)
1833 (defsubst gnus-agent-copy-nov-line
(article)
1834 "Copy the indicated ARTICLE from the overview buffer to the nntp server buffer."
1836 (set-buffer gnus-agent-overview-buffer
)
1837 (while (and (not (eobp))
1838 (or (not (setq art
(gnus-agent-read-article-number)))
1843 (not (eq article art
)))
1844 (set-buffer nntp-server-buffer
)
1846 (setq e
(progn (forward-line 1) (point)))
1847 (set-buffer nntp-server-buffer
)
1848 (insert-buffer-substring gnus-agent-overview-buffer b e
))))
1850 (defun gnus-agent-braid-nov (group articles file
)
1851 "Merge agent overview data with given file.
1852 Takes unvalidated headers for ARTICLES from
1853 `gnus-agent-overview-buffer' and validated headers from the given
1854 FILE and places the combined valid headers into
1855 `nntp-server-buffer'. This function can be used, when file
1856 doesn't exist, to valid the overview buffer."
1858 (set-buffer gnus-agent-overview-buffer
)
1859 (goto-char (point-min))
1860 (set-buffer nntp-server-buffer
)
1862 (when (file-exists-p file
)
1863 (nnheader-insert-file-contents file
))
1864 (goto-char (point-max))
1867 (unless (or (= (point-min) (point-max))
1868 (< (setq last
(read (current-buffer))) (car articles
)))
1869 ;; Old and new overlap -- We do it the hard way.
1870 (when (nnheader-find-nov-line (car articles
))
1871 ;; Replacing existing NOV entry
1872 (delete-region (point) (progn (forward-line 1) (point))))
1873 (gnus-agent-copy-nov-line (pop articles
))
1877 (while (let ((art (read (current-buffer))))
1878 (cond ((< art
(car articles
))
1881 ((= art
(car articles
))
1884 (point) (progn (forward-line 1) (point)))
1890 (gnus-agent-copy-nov-line (pop articles
)))))
1892 (goto-char (point-max))
1894 ;; Append the remaining lines
1897 (set-buffer gnus-agent-overview-buffer
)
1898 (setq start
(point))
1899 (set-buffer nntp-server-buffer
))
1902 (insert-buffer-substring gnus-agent-overview-buffer start
)
1905 (setq last
(or last -
134217728))
1908 (setq art
(gnus-agent-read-article-number))
1910 ;; Bad art num - delete this line
1912 (delete-region (point) (progn (forward-line 1) (point))))
1914 ;; Art num out of order - enable sort
1922 (sort-numeric-fields 1 (point-min) (point-max)))))))
1924 ;; Keeps the compiler from warning about the free variable in
1925 ;; gnus-agent-read-agentview.
1927 (defvar gnus-agent-read-agentview
))
1929 (defun gnus-agent-load-alist (group)
1930 "Load the article-state alist for GROUP."
1931 ;; Bind free variable that's used in `gnus-agent-read-agentview'.
1932 (let ((gnus-agent-read-agentview group
))
1933 (setq gnus-agent-article-alist
1934 (gnus-cache-file-contents
1935 (gnus-agent-article-name ".agentview" group
)
1936 'gnus-agent-file-loading-cache
1937 'gnus-agent-read-agentview
))))
1939 ;; Save format may be either 1 or 2. Two is the new, compressed
1940 ;; format that is still being tested. Format 1 is uncompressed but
1941 ;; known to be reliable.
1942 (defconst gnus-agent-article-alist-save-format
2)
1944 (defun gnus-agent-read-agentview (file)
1945 "Load FILE and do a `read' there."
1949 (nnheader-insert-file-contents file
)
1950 (goto-char (point-min))
1951 (let ((alist (read (current-buffer)))
1952 (version (condition-case nil
(read (current-buffer))
1958 (error "gnus-agent-read-agentview no longer supports version %d. Stop gnus, manually evaluate gnus-agent-convert-to-compressed-agentview, then restart gnus." version
))
1960 (let ((inhibit-quit t
)
1962 (gnus-agent-open-history)
1963 (set-buffer (gnus-agent-history-buffer))
1964 (goto-char (point-min))
1966 (if (and (looking-at
1967 "[^\t\n]+\t\\([0-9]+\\)\t\\([^ \n]+\\) \\([0-9]+\\)")
1968 (string= (match-string 2)
1969 gnus-agent-read-agentview
)
1970 (setq entry
(assoc (string-to-number (match-string 3)) alist
)))
1971 (setcdr entry
(string-to-number (match-string 1))))
1973 (gnus-agent-close-history)
1974 (setq changed-version t
)))
1976 (setq changed-version
(not (= 1 gnus-agent-article-alist-save-format
))))
1981 (let ((state (car comp-list
))
1983 (gnus-uncompress-range
1985 (mapcar (lambda (article-id)
1986 (setq uncomp
(cons (cons article-id state
) uncomp
)))
1989 (setq alist
(sort uncomp
'car-less-than-car
)))))
1990 (when changed-version
1991 (let ((gnus-agent-article-alist alist
))
1992 (gnus-agent-save-alist gnus-agent-read-agentview
)))
1996 (defun gnus-agent-save-alist (group &optional articles state
)
1997 "Save the article-state alist for GROUP."
1998 (let* ((file-name-coding-system nnmail-pathname-coding-system
)
1999 (prev (cons nil gnus-agent-article-alist
))
2001 print-level print-length item article
)
2002 (while (setq article
(pop articles
))
2003 (while (and (cdr prev
)
2004 (< (caadr prev
) article
))
2005 (setq prev
(cdr prev
)))
2008 (setcdr prev
(list (cons article state
))))
2009 ((> (caadr prev
) article
)
2010 (setcdr prev
(cons (cons article state
) (cdr prev
))))
2011 ((= (caadr prev
) article
)
2012 (setcdr (cadr prev
) state
)))
2013 (setq prev
(cdr prev
)))
2014 (setq gnus-agent-article-alist
(cdr all
))
2016 (gnus-agent-set-local group
2017 (caar gnus-agent-article-alist
)
2018 (caar (last gnus-agent-article-alist
)))
2020 (gnus-make-directory (gnus-agent-article-name "" group
))
2021 (with-temp-file (gnus-agent-article-name ".agentview" group
)
2022 (cond ((eq gnus-agent-article-alist-save-format
1)
2023 (princ gnus-agent-article-alist
(current-buffer)))
2024 ((eq gnus-agent-article-alist-save-format
2)
2025 (let ((compressed nil
))
2026 (mapcar (lambda (pair)
2027 (let* ((article-id (car pair
))
2028 (day-of-download (cdr pair
))
2029 (comp-list (assq day-of-download compressed
)))
2032 (cons article-id
(cdr comp-list
)))
2034 (cons (list day-of-download article-id
)
2036 nil
)) gnus-agent-article-alist
)
2037 (mapcar (lambda (comp-list)
2039 (gnus-compress-sequence
2040 (nreverse (cdr comp-list
)))))
2042 (princ compressed
(current-buffer)))))
2044 (princ gnus-agent-article-alist-save-format
(current-buffer))
2047 (defvar gnus-agent-article-local nil
)
2048 (defvar gnus-agent-file-loading-local nil
)
2050 (defun gnus-agent-load-local (&optional method
)
2051 "Load the METHOD'S local file. The local file contains min/max
2052 article counts for each of the method's subscribed groups."
2053 (let ((gnus-command-method (or method gnus-command-method
)))
2054 (setq gnus-agent-article-local
2055 (gnus-cache-file-contents
2056 (gnus-agent-lib-file "local")
2057 'gnus-agent-file-loading-local
2058 'gnus-agent-read-and-cache-local
))))
2060 (defun gnus-agent-read-and-cache-local (file)
2061 "Load and read FILE then bind its contents to
2062 gnus-agent-article-local. If that variable had `dirty' (also known as
2063 modified) original contents, they are first saved to their own file."
2065 (if (and gnus-agent-article-local
2066 (symbol-value (intern "+dirty" gnus-agent-article-local
)))
2067 (gnus-agent-save-local))
2068 (gnus-agent-read-local file
))
2070 (defun gnus-agent-read-local (file)
2071 "Load FILE and do a `read' there."
2072 (let ((my-obarray (gnus-make-hashtable (count-lines (point-min)
2077 (let ((nnheader-file-coding-system gnus-agent-file-coding-system
))
2078 (nnheader-insert-file-contents file
))
2081 (goto-char (point-min))
2082 ;; Skip any comments at the beginning of the file (the only place where they may appear)
2083 (while (= (following-char) ?\
;)
2085 (setq line
(1+ line
)))
2092 (cur (current-buffer)))
2093 (setq group
(read cur
)
2097 (when (stringp group
)
2098 (setq group
(intern group my-obarray
)))
2100 ;; NOTE: The '+ 0' ensure that min and max are both numerics.
2101 (set group
(cons (+ 0 min
) (+ 0 max
))))
2103 (gnus-message 3 "Warning - invalid agent local: %s on line %d: "
2104 file line
(error-message-string err
))))
2106 (setq line
(1+ line
))))
2108 (set (intern "+dirty" my-obarray
) nil
)
2109 (set (intern "+method" my-obarray
) gnus-command-method
)
2112 (defun gnus-agent-save-local (&optional force
)
2113 "Save gnus-agent-article-local under it method's agent.lib directory."
2114 (let ((my-obarray gnus-agent-article-local
))
2115 (when (and my-obarray
2116 (or force
(symbol-value (intern "+dirty" my-obarray
))))
2117 (let* ((gnus-command-method (symbol-value (intern "+method" my-obarray
)))
2118 ;; NOTE: gnus-command-method is used within gnus-agent-lib-file.
2119 (dest (gnus-agent-lib-file "local")))
2120 (gnus-make-directory (gnus-agent-lib-file ""))
2122 (let ((buffer-file-coding-system gnus-agent-file-coding-system
))
2123 (with-temp-file dest
2124 (let ((gnus-command-method (symbol-value (intern "+method" my-obarray
)))
2125 (file-name-coding-system nnmail-pathname-coding-system
)
2126 print-level print-length item article
2127 (standard-output (current-buffer)))
2128 (mapatoms (lambda (symbol)
2129 (cond ((not (boundp symbol
))
2131 ((member (symbol-name symbol
) '("+dirty" "+method"))
2135 (let ((range (symbol-value symbol
)))
2143 (defun gnus-agent-get-local (group &optional gmane method
)
2144 (let* ((gmane (or gmane
(gnus-group-real-name group
)))
2145 (gnus-command-method (or method
(gnus-find-method-for-group group
)))
2146 (local (gnus-agent-load-local))
2147 (symb (intern gmane local
))
2148 (minmax (and (boundp symb
) (symbol-value symb
))))
2150 ;; Bind these so that gnus-agent-load-alist doesn't change the
2151 ;; current alist (i.e. gnus-agent-article-alist)
2152 (let* ((gnus-agent-article-alist gnus-agent-article-alist
)
2153 (gnus-agent-file-loading-cache gnus-agent-file-loading-cache
)
2154 (alist (gnus-agent-load-alist group
)))
2158 (caar (last alist
))))
2159 (gnus-agent-set-local group
(car minmax
) (cdr minmax
)
2160 gmane gnus-command-method local
))))
2163 (defun gnus-agent-set-local (group min max
&optional gmane method local
)
2164 (let* ((gmane (or gmane
(gnus-group-real-name group
)))
2165 (gnus-command-method (or method
(gnus-find-method-for-group group
)))
2166 (local (or local
(gnus-agent-load-local)))
2167 (symb (intern gmane local
))
2168 (minmax (and (boundp symb
) (symbol-value symb
))))
2170 (if (cond ((and minmax
2171 (or (not (eq min
(car minmax
)))
2172 (not (eq max
(cdr minmax
)))))
2179 (set symb
(cons min max
))
2182 (unintern symb local
)))
2183 (set (intern "+dirty" local
) t
))))
2185 (defun gnus-agent-article-name (article group
)
2186 (expand-file-name article
2187 (file-name-as-directory
2188 (gnus-agent-group-pathname group
))))
2190 (defun gnus-agent-batch-confirmation (msg)
2191 "Show error message and return t."
2192 (gnus-message 1 msg
)
2196 (defun gnus-agent-batch-fetch ()
2197 "Start Gnus and fetch session."
2200 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation
))
2201 (gnus-agent-fetch-session))
2204 (defun gnus-agent-fetch-session ()
2205 "Fetch all articles and headers that are eligible for fetching."
2207 (unless gnus-agent-covered-methods
2208 (error "No servers are covered by the Gnus agent"))
2209 (unless gnus-plugged
2210 (error "Can't fetch articles while Gnus is unplugged"))
2211 (let ((methods (gnus-agent-covered-methods))
2212 groups group gnus-command-method
)
2215 (setq gnus-command-method
(car methods
))
2216 (when (and (or (gnus-server-opened gnus-command-method
)
2217 (gnus-open-server gnus-command-method
))
2218 (gnus-online gnus-command-method
))
2219 (setq groups
(gnus-groups-from-server (car methods
)))
2220 (gnus-agent-with-fetch
2221 (while (setq group
(pop groups
))
2222 (when (<= (gnus-group-level group
)
2223 gnus-agent-handle-level
)
2224 (if (or debug-on-error debug-on-quit
)
2225 (gnus-agent-fetch-group-1
2226 group gnus-command-method
)
2228 (gnus-agent-fetch-group-1
2229 group gnus-command-method
)
2231 (unless (funcall gnus-agent-confirmation-function
2232 (format "Error %s while fetching session. Should gnus continue? "
2233 (error-message-string err
)))
2234 (error "Cannot fetch articles into the Gnus agent")))
2236 (gnus-agent-regenerate-group group
)
2237 (unless (funcall gnus-agent-confirmation-function
2239 "%s while fetching session. Should gnus continue? "
2240 (error-message-string err
)))
2242 "Cannot fetch articles into the Gnus agent")))))))))
2243 (setq methods
(cdr methods
)))
2244 (gnus-run-hooks 'gnus-agent-fetched-hook
)
2245 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
2247 (defun gnus-agent-fetch-group-1 (group method
)
2249 (let ((gnus-command-method method
)
2250 (gnus-newsgroup-name group
)
2251 (gnus-newsgroup-dependencies gnus-newsgroup-dependencies
)
2252 (gnus-newsgroup-headers gnus-newsgroup-headers
)
2253 (gnus-newsgroup-scored gnus-newsgroup-scored
)
2254 (gnus-use-cache gnus-use-cache
)
2255 (gnus-summary-expunge-below gnus-summary-expunge-below
)
2256 (gnus-summary-mark-below gnus-summary-mark-below
)
2257 (gnus-orphan-score gnus-orphan-score
)
2258 ;; Maybe some other gnus-summary local variables should also
2264 category predicate info marks score-param
2266 (unless (gnus-check-group group
)
2267 (error "Can't open server for %s" group
))
2270 (when (or gnus-newsgroup-active
2272 (gnus-activate-group group
))
2273 (let ((marked-articles gnus-newsgroup-downloadable
))
2274 ;; Identify the articles marked for download
2275 (unless gnus-newsgroup-active
2276 ;; The variable gnus-newsgroup-active was selected as I need
2277 ;; a gnus-summary local variable that is NOT bound to any
2278 ;; value (its global value should default to nil).
2279 (dolist (mark gnus-agent-download-marks
)
2280 (let ((arts (cdr (assq mark
(gnus-info-marks
2281 (setq info
(gnus-get-info group
)))))))
2283 (setq marked-articles
(nconc (gnus-uncompress-range arts
)
2286 (setq marked-articles
(sort marked-articles
'<))
2288 ;; Fetch any new articles from the server
2289 (setq articles
(gnus-agent-fetch-headers group
))
2291 ;; Merge new articles with marked
2292 (setq articles
(sort (append marked-articles articles
) '<))
2295 ;; Parse them and see which articles we want to fetch.
2296 (setq gnus-newsgroup-dependencies
2297 (or gnus-newsgroup-dependencies
2298 (make-vector (length articles
) 0)))
2299 (setq gnus-newsgroup-headers
2300 (or gnus-newsgroup-headers
2301 (gnus-get-newsgroup-headers-xover articles nil nil
2303 ;; `gnus-agent-overview-buffer' may be killed for
2304 ;; timeout reason. If so, recreate it.
2305 (gnus-agent-create-buffer)
2307 ;; Figure out how to select articles in this group
2308 (setq category
(gnus-group-category group
))
2312 (gnus-agent-find-parameter group
'agent-predicate
)))
2314 ;; If the selection predicate requires scoring, score each header
2315 (unless (memq predicate
'(gnus-agent-true gnus-agent-false
))
2317 (gnus-agent-find-parameter group
'agent-score-file
)))
2318 ;; Translate score-param into real one
2321 ((eq score-param
'file
)
2322 (setq score-param
(gnus-all-score-files group
)))
2323 ((stringp (car score-param
)))
2325 (setq score-param
(list (list score-param
)))))
2327 (gnus-score-headers score-param
))))
2329 (unless (and (eq predicate
'gnus-agent-false
)
2330 (not marked-articles
))
2331 (let ((arts (list nil
)))
2332 (let ((arts-tail arts
)
2333 (alist (gnus-agent-load-alist group
))
2334 (marked-articles marked-articles
)
2335 (gnus-newsgroup-headers gnus-newsgroup-headers
))
2336 (while (setq gnus-headers
(pop gnus-newsgroup-headers
))
2337 (let ((num (mail-header-number gnus-headers
)))
2338 ;; Determine if this article is already in the cache
2340 (> num
(caar alist
)))
2341 (setq alist
(cdr alist
)))
2343 (unless (and (eq num
(caar alist
))
2346 ;; Determine if this article was marked for download.
2347 (while (and marked-articles
2348 (> num
(car marked-articles
)))
2349 (setq marked-articles
2350 (cdr marked-articles
)))
2352 ;; When this article is marked, or selected by the
2353 ;; predicate, add it to the download list
2354 (when (or (eq num
(car marked-articles
))
2357 (assq num gnus-newsgroup-scored
))
2358 gnus-summary-default-score
))
2359 (gnus-agent-long-article
2360 (gnus-agent-find-parameter
2361 group
'agent-long-article
))
2362 (gnus-agent-short-article
2363 (gnus-agent-find-parameter
2364 group
'agent-short-article
))
2365 (gnus-agent-low-score
2366 (gnus-agent-find-parameter
2367 group
'agent-low-score
))
2368 (gnus-agent-high-score
2369 (gnus-agent-find-parameter
2370 group
'agent-high-score
))
2371 (gnus-agent-expire-days
2372 (gnus-agent-find-parameter
2373 group
'agent-days-until-old
)))
2374 (funcall predicate
)))
2375 (gnus-agent-append-to-list arts-tail num
))))))
2377 (let (fetched-articles)
2378 ;; Fetch all selected articles
2379 (setq gnus-newsgroup-undownloaded
2380 (gnus-sorted-ndifference
2381 gnus-newsgroup-undownloaded
2382 (setq fetched-articles
2384 (gnus-agent-fetch-articles group
(cdr arts
))
2387 (let ((unfetched-articles
2388 (gnus-sorted-ndifference (cdr arts
) fetched-articles
)))
2389 (if gnus-newsgroup-active
2390 ;; Update the summary buffer
2392 (dolist (article marked-articles
)
2393 (gnus-summary-set-agent-mark article t
))
2394 (dolist (article fetched-articles
)
2395 (if gnus-agent-mark-unread-after-downloaded
2396 (gnus-summary-mark-article
2397 article gnus-unread-mark
))
2398 (when (gnus-summary-goto-subject article nil t
)
2399 (gnus-summary-update-download-mark article
)))
2400 (dolist (article unfetched-articles
)
2401 (gnus-summary-mark-article
2402 article gnus-canceled-mark
)))
2404 ;; Update the group buffer.
2406 ;; When some, or all, of the marked articles came
2407 ;; from the download mark. Remove that mark. I
2408 ;; didn't do this earlier as I only want to remove
2409 ;; the marks after the fetch is completed.
2411 (dolist (mark gnus-agent-download-marks
)
2412 (when (eq mark
'download
)
2414 (assq mark
(gnus-info-marks
2415 (setq info
(gnus-get-info group
))))))
2416 (when (cdr marked-arts
)
2418 (delq marked-arts
(gnus-info-marks info
)))
2419 (gnus-info-set-marks info marks
)))))
2420 (let ((read (gnus-info-read
2421 (or info
(setq info
(gnus-get-info group
))))))
2423 info
(gnus-add-to-range read unfetched-articles
)))
2425 (gnus-group-update-group group t
)
2429 (concat "(gnus-group-set-info '"
2430 (gnus-prin1-to-string info
)
2434 ;;; Agent Category Mode
2437 (defvar gnus-category-mode-hook nil
2438 "Hook run in `gnus-category-mode' buffers.")
2440 (defvar gnus-category-line-format
" %(%20c%): %g\n"
2441 "Format of category lines.
2443 Valid specifiers include:
2444 %c Topic name (string)
2445 %g The number of groups in the topic (integer)
2447 General format specifiers can also be used. See Info node
2448 `(gnus)Formatting Variables'.")
2450 (defvar gnus-category-mode-line-format
"Gnus: %%b"
2451 "The format specification for the category mode line.")
2453 (defvar gnus-agent-predicate
'false
2454 "The selection predicate used when no other source is available.")
2456 (defvar gnus-agent-short-article
100
2457 "Articles that have fewer lines than this are short.")
2459 (defvar gnus-agent-long-article
200
2460 "Articles that have more lines than this are long.")
2462 (defvar gnus-agent-low-score
0
2463 "Articles that have a score lower than this have a low score.")
2465 (defvar gnus-agent-high-score
0
2466 "Articles that have a score higher than this have a high score.")
2469 ;;; Internal variables.
2471 (defvar gnus-category-buffer
"*Agent Category*")
2473 (defvar gnus-category-line-format-alist
2474 `((?c gnus-tmp-name ?s
)
2475 (?g gnus-tmp-groups ?d
)))
2477 (defvar gnus-category-mode-line-format-alist
2478 `((?u user-defined ?s
)))
2480 (defvar gnus-category-line-format-spec nil
)
2481 (defvar gnus-category-mode-line-format-spec nil
)
2483 (defvar gnus-category-mode-map nil
)
2484 (put 'gnus-category-mode
'mode-class
'special
)
2486 (unless gnus-category-mode-map
2487 (setq gnus-category-mode-map
(make-sparse-keymap))
2488 (suppress-keymap gnus-category-mode-map
)
2490 (gnus-define-keys gnus-category-mode-map
2491 "q" gnus-category-exit
2492 "k" gnus-category-kill
2493 "c" gnus-category-copy
2494 "a" gnus-category-add
2495 "e" gnus-agent-customize-category
2496 "p" gnus-category-edit-predicate
2497 "g" gnus-category-edit-groups
2498 "s" gnus-category-edit-score
2499 "l" gnus-category-list
2501 "\C-c\C-i" gnus-info-find-node
2502 "\C-c\C-b" gnus-bug
))
2504 (defvar gnus-category-menu-hook nil
2505 "*Hook run after the creation of the menu.")
2507 (defun gnus-category-make-menu-bar ()
2508 (gnus-turn-off-edit-menu 'category
)
2509 (unless (boundp 'gnus-category-menu
)
2511 gnus-category-menu gnus-category-mode-map
""
2513 ["Add" gnus-category-add t
]
2514 ["Kill" gnus-category-kill t
]
2515 ["Copy" gnus-category-copy t
]
2516 ["Edit category" gnus-agent-customize-category t
]
2517 ["Edit predicate" gnus-category-edit-predicate t
]
2518 ["Edit score" gnus-category-edit-score t
]
2519 ["Edit groups" gnus-category-edit-groups t
]
2520 ["Exit" gnus-category-exit t
]))
2522 (gnus-run-hooks 'gnus-category-menu-hook
)))
2524 (defun gnus-category-mode ()
2525 "Major mode for listing and editing agent categories.
2527 All normal editing commands are switched off.
2528 \\<gnus-category-mode-map>
2529 For more in-depth information on this mode, read the manual
2530 \(`\\[gnus-info-find-node]').
2532 The following commands are available:
2534 \\{gnus-category-mode-map}"
2536 (when (gnus-visual-p 'category-menu
'menu
)
2537 (gnus-category-make-menu-bar))
2538 (kill-all-local-variables)
2539 (gnus-simplify-mode-line)
2540 (setq major-mode
'gnus-category-mode
)
2541 (setq mode-name
"Category")
2542 (gnus-set-default-directory)
2543 (setq mode-line-process nil
)
2544 (use-local-map gnus-category-mode-map
)
2545 (buffer-disable-undo)
2546 (setq truncate-lines t
)
2547 (setq buffer-read-only t
)
2548 (gnus-run-hooks 'gnus-category-mode-hook
))
2550 (defalias 'gnus-category-position-point
'gnus-goto-colon
)
2552 (defun gnus-category-insert-line (category)
2553 (let* ((gnus-tmp-name (format "%s" (car category
)))
2554 (gnus-tmp-groups (length (gnus-agent-cat-groups category
))))
2556 (gnus-add-text-properties
2560 (eval gnus-category-line-format-spec
))
2561 (list 'gnus-category gnus-tmp-name
))))
2563 (defun gnus-enter-category-buffer ()
2564 "Go to the Category buffer."
2566 (gnus-category-setup-buffer)
2567 (gnus-configure-windows 'category
)
2568 (gnus-category-prepare))
2570 (defun gnus-category-setup-buffer ()
2571 (unless (get-buffer gnus-category-buffer
)
2573 (set-buffer (gnus-get-buffer-create gnus-category-buffer
))
2574 (gnus-category-mode))))
2576 (defun gnus-category-prepare ()
2577 (gnus-set-format 'category-mode
)
2578 (gnus-set-format 'category t
)
2579 (let ((alist gnus-category-alist
)
2580 (buffer-read-only nil
))
2583 (gnus-category-insert-line (pop alist
)))
2584 (goto-char (point-min))
2585 (gnus-category-position-point)))
2587 (defun gnus-category-name ()
2588 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category
))
2589 (error "No category on the current line")))
2591 (defun gnus-category-read ()
2592 "Read the category alist."
2593 (setq gnus-category-alist
2597 (nnheader-insert-file-contents (nnheader-concat gnus-agent-directory
"lib/categories"))
2598 (goto-char (point-min))
2599 ;; This code isn't temp, it will be needed so long as
2600 ;; anyone may be migrating from an older version.
2602 ;; Once we're certain that people will not revert to an
2603 ;; earlier version, we can take out the old-list code in
2604 ;; gnus-category-write.
2605 (let* ((old-list (read (current-buffer)))
2606 (new-list (ignore-errors (read (current-buffer)))))
2609 ;; Convert from a positional list to an alist.
2619 '(agent-predicate agent-score-file agent-groups
))))
2622 (list (gnus-agent-cat-make 'default
'short
)))))
2624 (defun gnus-category-write ()
2625 "Write the category alist."
2626 (setq gnus-category-predicate-cache nil
2627 gnus-category-group-cache nil
)
2628 (gnus-make-directory (nnheader-concat gnus-agent-directory
"lib"))
2629 (with-temp-file (nnheader-concat gnus-agent-directory
"lib/categories")
2630 ;; This prin1 is temporary. It exists so that people can revert
2631 ;; to an earlier version of gnus-agent.
2632 (prin1 (mapcar (lambda (c)
2634 (cdr (assoc 'agent-predicate c
))
2635 (cdr (assoc 'agent-score-file c
))
2636 (cdr (assoc 'agent-groups c
))))
2637 gnus-category-alist
)
2640 (prin1 gnus-category-alist
(current-buffer))))
2642 (defun gnus-category-edit-predicate (category)
2643 "Edit the predicate for CATEGORY."
2644 (interactive (list (gnus-category-name)))
2645 (let ((info (assq category gnus-category-alist
)))
2647 (gnus-agent-cat-predicate info
)
2648 (format "Editing the select predicate for category %s" category
)
2649 `(lambda (predicate)
2650 ;; Avoid run-time execution of setf form
2651 ;; (setf (gnus-agent-cat-predicate (assq ',category gnus-category-alist))
2653 ;; use its expansion instead:
2654 (gnus-agent-cat-set-property (assq ',category gnus-category-alist
)
2655 'agent-predicate predicate
)
2657 (gnus-category-write)
2658 (gnus-category-list)))))
2660 (defun gnus-category-edit-score (category)
2661 "Edit the score expression for CATEGORY."
2662 (interactive (list (gnus-category-name)))
2663 (let ((info (assq category gnus-category-alist
)))
2665 (gnus-agent-cat-score-file info
)
2666 (format "Editing the score expression for category %s" category
)
2667 `(lambda (score-file)
2668 ;; Avoid run-time execution of setf form
2669 ;; (setf (gnus-agent-cat-score-file (assq ',category gnus-category-alist))
2671 ;; use its expansion instead:
2672 (gnus-agent-cat-set-property (assq ',category gnus-category-alist
)
2673 'agent-score-file score-file
)
2675 (gnus-category-write)
2676 (gnus-category-list)))))
2678 (defun gnus-category-edit-groups (category)
2679 "Edit the group list for CATEGORY."
2680 (interactive (list (gnus-category-name)))
2681 (let ((info (assq category gnus-category-alist
)))
2683 (gnus-agent-cat-groups info
)
2684 (format "Editing the group list for category %s" category
)
2686 ;; Avoid run-time execution of setf form
2687 ;; (setf (gnus-agent-cat-groups (assq ',category gnus-category-alist))
2689 ;; use its expansion instead:
2690 (gnus-agent-set-cat-groups (assq ',category gnus-category-alist
)
2693 (gnus-category-write)
2694 (gnus-category-list)))))
2696 (defun gnus-category-kill (category)
2697 "Kill the current category."
2698 (interactive (list (gnus-category-name)))
2699 (let ((info (assq category gnus-category-alist
))
2700 (buffer-read-only nil
))
2702 (setq gnus-category-alist
(delq info gnus-category-alist
))
2703 (gnus-category-write)))
2705 (defun gnus-category-copy (category to
)
2706 "Copy the current category."
2707 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
2708 (let ((info (assq category gnus-category-alist
)))
2709 (push (let ((newcat (gnus-copy-sequence info
)))
2710 (setf (gnus-agent-cat-name newcat
) to
)
2711 (setf (gnus-agent-cat-groups newcat
) nil
)
2713 gnus-category-alist
)
2714 (gnus-category-write)
2715 (gnus-category-list)))
2717 (defun gnus-category-add (category)
2718 "Create a new category."
2719 (interactive "SCategory name: ")
2720 (when (assq category gnus-category-alist
)
2721 (error "Category %s already exists" category
))
2722 (push (gnus-agent-cat-make category
)
2723 gnus-category-alist
)
2724 (gnus-category-write)
2725 (gnus-category-list))
2727 (defun gnus-category-list ()
2728 "List all categories."
2730 (gnus-category-prepare))
2732 (defun gnus-category-exit ()
2733 "Return to the group buffer."
2735 (kill-buffer (current-buffer))
2736 (gnus-configure-windows 'group t
))
2738 ;; To avoid having 8-bit characters in the source file.
2739 (defvar gnus-category-not
(list '! 'not
(intern (format "%c" 172))))
2741 (defvar gnus-category-predicate-alist
2742 '((spam . gnus-agent-spam-p
)
2743 (short . gnus-agent-short-p
)
2744 (long . gnus-agent-long-p
)
2745 (low . gnus-agent-low-scored-p
)
2746 (high . gnus-agent-high-scored-p
)
2747 (read . gnus-agent-read-p
)
2748 (true . gnus-agent-true
)
2749 (false . gnus-agent-false
))
2750 "Mapping from short score predicate symbols to predicate functions.")
2752 (defun gnus-agent-spam-p ()
2753 "Say whether an article is spam or not."
2754 (unless gnus-agent-spam-hashtb
2755 (setq gnus-agent-spam-hashtb
(gnus-make-hashtable 1000)))
2756 (if (not (equal (mail-header-references gnus-headers
) ""))
2758 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers
))))
2760 (gnus-gethash string gnus-agent-spam-hashtb
)
2761 (gnus-sethash string t gnus-agent-spam-hashtb
)))))
2763 (defun gnus-agent-short-p ()
2764 "Say whether an article is short or not."
2765 (< (mail-header-lines gnus-headers
) gnus-agent-short-article
))
2767 (defun gnus-agent-long-p ()
2768 "Say whether an article is long or not."
2769 (> (mail-header-lines gnus-headers
) gnus-agent-long-article
))
2771 (defun gnus-agent-low-scored-p ()
2772 "Say whether an article has a low score or not."
2773 (< gnus-score gnus-agent-low-score
))
2775 (defun gnus-agent-high-scored-p ()
2776 "Say whether an article has a high score or not."
2777 (> gnus-score gnus-agent-high-score
))
2779 (defun gnus-agent-read-p ()
2780 "Say whether an article is read or not."
2781 (gnus-member-of-range (mail-header-number gnus-headers
)
2782 (gnus-info-read (gnus-get-info gnus-newsgroup-name
))))
2784 (defun gnus-category-make-function (predicate)
2785 "Make a function from PREDICATE."
2786 (let ((func (gnus-category-make-function-1 predicate
)))
2787 (if (and (= (length func
) 1)
2788 (symbolp (car func
)))
2790 (gnus-byte-compile `(lambda () ,func
)))))
2792 (defun gnus-agent-true ()
2796 (defun gnus-agent-false ()
2800 (defun gnus-category-make-function-1 (predicate)
2801 "Make a function from PREDICATE."
2803 ;; Functions are just returned as is.
2804 ((or (symbolp predicate
)
2805 (functionp predicate
))
2806 `(,(or (cdr (assq predicate gnus-category-predicate-alist
))
2808 ;; More complex predicate.
2811 ((memq (car predicate
) '(& and
))
2813 ((memq (car predicate
) '(| or
))
2815 ((memq (car predicate
) gnus-category-not
)
2817 ,@(mapcar 'gnus-category-make-function-1
(cdr predicate
))))
2819 (error "Unknown predicate type: %s" predicate
))))
2821 (defun gnus-get-predicate (predicate)
2822 "Return the function implementing PREDICATE."
2823 (or (cdr (assoc predicate gnus-category-predicate-cache
))
2824 (let ((func (gnus-category-make-function predicate
)))
2825 (setq gnus-category-predicate-cache
2826 (nconc gnus-category-predicate-cache
2827 (list (cons predicate func
))))
2830 (defun gnus-predicate-implies-unread (predicate)
2831 "Say whether PREDICATE implies unread articles only.
2832 It is okay to miss some cases, but there must be no false positives.
2833 That is, if this predicate returns true, then indeed the predicate must
2834 return only unread articles."
2835 (eq t
(gnus-function-implies-unread-1
2836 (gnus-category-make-function-1 predicate
))))
2838 (defun gnus-function-implies-unread-1 (function)
2839 "Recursively evaluate a predicate function to determine whether it can select
2840 any read articles. Returns t if the function is known to never
2841 return read articles, nil when it is known to always return read
2842 articles, and t_nil when the function may return both read and unread
2844 (let ((func (car function
))
2845 (args (mapcar 'gnus-function-implies-unread-1
(cdr function
))))
2846 (cond ((eq func
'and
)
2847 (cond ((memq t args
) ; if any argument returns only unread articles
2848 ;; then that argument constrains the result to only unread articles.
2850 ((memq 't_nil args
) ; if any argument is indeterminate
2851 ;; then the result is indeterminate
2854 (cond ((memq nil args
) ; if any argument returns read articles
2855 ;; then that argument ensures that the results includes read articles.
2857 ((memq 't_nil args
) ; if any argument is indeterminate
2858 ;; then that argument ensures that the results are indeterminate
2860 (t ; if all arguments return only unread articles
2861 ;; then the result returns only unread articles
2864 (cond ((eq (car args
) 't_nil
) ; if the argument is indeterminate
2865 ; then the result is indeterminate
2868 ; toggle the result to be the opposite of the argument
2870 ((eq func
'gnus-agent-read-p
)
2871 nil
) ; The read predicate NEVER returns unread articles
2872 ((eq func
'gnus-agent-false
)
2873 t
) ; The false predicate returns t as the empty set excludes all read articles
2874 ((eq func
'gnus-agent-true
)
2875 nil
) ; The true predicate ALWAYS returns read articles
2876 ((catch 'found-match
2877 (let ((alist gnus-category-predicate-alist
))
2879 (if (eq func
(cdar alist
))
2880 (throw 'found-match t
)
2881 (setq alist
(cdr alist
))))))
2882 't_nil
) ; All other predicates return read and unread articles
2884 (error "Unknown predicate function: %s" function
)))))
2886 (defun gnus-group-category (group)
2887 "Return the category GROUP belongs to."
2888 (unless gnus-category-group-cache
2889 (setq gnus-category-group-cache
(gnus-make-hashtable 1000))
2890 (let ((cs gnus-category-alist
)
2892 (while (setq cat
(pop cs
))
2893 (setq groups
(gnus-agent-cat-groups cat
))
2895 (gnus-sethash (pop groups
) cat gnus-category-group-cache
)))))
2896 (or (gnus-gethash group gnus-category-group-cache
)
2897 (assq 'default gnus-category-alist
)))
2899 (defun gnus-agent-expire-group (group &optional articles force
)
2900 "Expire all old articles in GROUP.
2901 If you want to force expiring of certain articles, this function can
2902 take ARTICLES, and FORCE parameters as well.
2904 The articles on which the expiration process runs are selected as follows:
2905 if ARTICLES is null, all read and unmarked articles.
2906 if ARTICLES is t, all articles.
2907 if ARTICLES is a list, just those articles.
2908 FORCE is equivalent to setting the expiration predicates to true."
2910 (list (let ((def (or (gnus-group-group-name)
2911 gnus-newsgroup-name
)))
2912 (let ((select (read-string (if def
2913 (concat "Group Name ("
2916 (if (and (equal "" select
)
2922 (gnus-agent-expire articles group force
)
2923 (let ( ;; Bind gnus-agent-expire-stats to enable tracking of
2924 ;; expiration statistics of this single group
2925 (gnus-agent-expire-stats (list 0 0 0.0)))
2926 (if (or (not (eq articles t
))
2928 (concat "Are you sure that you want to "
2929 "expire all articles in " group
".")))
2930 (let ((gnus-command-method (gnus-find-method-for-group group
))
2931 (overview (gnus-get-buffer-create " *expire overview*"))
2934 (let ((active-file (gnus-agent-lib-file "active")))
2935 (when (file-exists-p active-file
)
2937 (nnheader-insert-file-contents active-file
)
2938 (gnus-active-to-gnus-format
2940 (setq orig
(gnus-make-hashtable
2941 (count-lines (point-min) (point-max))))))
2943 (gnus-agent-expire-group-1
2944 group overview
(gnus-gethash-safe group orig
)
2946 (kill-buffer overview
))))
2947 (gnus-message 4 (gnus-agent-expire-done-message)))))
2949 (defun gnus-agent-expire-group-1 (group overview active articles force
)
2950 ;; Internal function - requires caller to have set
2951 ;; gnus-command-method, initialized overview buffer, and to have
2952 ;; provided a non-nil active
2954 (let ((dir (gnus-agent-group-pathname group
)))
2955 (when (boundp 'gnus-agent-expire-current-dirs
)
2956 (set 'gnus-agent-expire-current-dirs
2958 (symbol-value 'gnus-agent-expire-current-dirs
))))
2960 (if (and (not force
)
2961 (eq 'DISABLE
(gnus-agent-find-parameter group
2962 'agent-enable-expiration
)))
2963 (gnus-message 5 "Expiry skipping over %s" group
)
2964 (gnus-message 5 "Expiring articles in %s" group
)
2965 (gnus-agent-load-alist group
)
2966 (let* ((bytes-freed 0)
2968 (nov-entries-deleted 0)
2969 (info (gnus-get-info group
))
2970 (alist gnus-agent-article-alist
)
2971 (day (- (time-to-days (current-time))
2972 (gnus-agent-find-parameter group
'agent-days-until-old
)))
2973 (specials (if (and alist
2975 ;; This could be a bit of a problem. I need to
2976 ;; keep the last article to avoid refetching
2977 ;; headers when using nntp in the backend. At
2978 ;; the same time, if someone uses a backend
2979 ;; that supports article moving then I may have
2980 ;; to remove the last article to complete the
2981 ;; move. Right now, I'm going to assume that
2982 ;; FORCE overrides specials.
2983 (list (caar (last alist
)))))
2984 (unreads ;; Articles that are excluded from the
2985 ;; expiration process
2986 (cond (gnus-agent-expire-all
2987 ;; All articles are marked read by global decree
2990 ;; All articles are marked read by function
2994 ;; Unread articles are marked protected from
2995 ;; expiration Don't call
2996 ;; gnus-list-of-unread-articles as it returns
2997 ;; articles that have not been fetched into the
3000 (gnus-agent-unread-articles group
)))
3002 ;; All articles EXCEPT those named by the caller
3003 ;; are protected from expiration
3004 (gnus-sorted-difference
3005 (gnus-uncompress-range
3007 (caar (last alist
))))
3008 (sort articles
'<)))))
3009 (marked ;; More articles that are excluded from the
3010 ;; expiration process
3011 (cond (gnus-agent-expire-all
3012 ;; All articles are unmarked by global decree
3015 ;; All articles are unmarked by function
3019 ;; All articles may as well be unmarked as the
3020 ;; unreads list already names the articles we are
3024 ;; Ticked and/or dormant articles are excluded
3027 (gnus-uncompress-range
3028 (cdr (assq 'tick
(gnus-info-marks info
))))
3029 (gnus-uncompress-range
3031 (gnus-info-marks info
))))))))
3032 (nov-file (concat dir
".overview"))
3038 ;; The normal article alist contains elements that look like
3039 ;; (article# . fetch_date) I need to combine other
3040 ;; information with this list. For example, a flag indicating
3041 ;; that a particular article MUST BE KEPT. To do this, I'm
3042 ;; going to transform the elements to look like (article#
3043 ;; fetch_date keep_flag NOV_entry_marker) Later, I'll reverse
3044 ;; the process to generate the expired article alist.
3046 ;; Convert the alist elements to (article# fetch_date nil
3048 (setq dlist
(mapcar (lambda (e)
3049 (list (car e
) (cdr e
) nil nil
)) alist
))
3051 ;; Convert the keep lists to elements that look like (article#
3052 ;; nil keep_flag nil) then append it to the expanded dlist
3053 ;; These statements are sorted by ascending precidence of the
3055 (setq dlist
(nconc dlist
3057 (list e nil
'unread nil
))
3059 (setq dlist
(nconc dlist
3061 (list e nil
'marked nil
))
3063 (setq dlist
(nconc dlist
3065 (list e nil
'special nil
))
3068 (set-buffer overview
)
3070 (buffer-disable-undo)
3071 (when (file-exists-p nov-file
)
3072 (gnus-message 7 "gnus-agent-expire: Loading overview...")
3073 (nnheader-insert-file-contents nov-file
)
3074 (goto-char (point-min))
3077 (while (< (setq p
(point)) (point-max))
3079 ;; If I successfully read an integer (the plus zero
3080 ;; ensures a numeric type), prepend a marker entry
3082 (push (list (+ 0 (read (current-buffer))) nil nil
3083 (set-marker (make-marker) p
))
3086 (gnus-message 1 "gnus-agent-expire: read error \
3087 occurred when reading expression at %s in %s. Skipping to next \
3088 line." (point) nov-file
)))
3089 ;; Whether I succeeded, or failed, it doesn't matter.
3090 ;; Move to the next line then try again.
3094 7 "gnus-agent-expire: Loading overview... Done"))
3095 (set-buffer-modified-p nil
)
3097 ;; At this point, all of the information is in dlist. The
3098 ;; only problem is that much of it is spread across multiple
3099 ;; entries. Sort then MERGE!!
3100 (gnus-message 7 "gnus-agent-expire: Sorting entries... ")
3101 ;; If two entries have the same article-number then sort by
3102 ;; ascending keep_flag.
3109 (cond ((< (nth 0 a
) (nth 0 b
))
3111 ((> (nth 0 a
) (nth 0 b
))
3114 (let ((a (or (symbol-value (nth 2 a
))
3116 (b (or (symbol-value (nth 2 b
))
3119 (gnus-message 7 "gnus-agent-expire: Sorting entries... Done")
3120 (gnus-message 7 "gnus-agent-expire: Merging entries... ")
3121 (let ((dlist dlist
))
3122 (while (cdr dlist
) ; I'm not at the end-of-list
3123 (if (eq (caar dlist
) (caadr dlist
))
3124 (let ((first (cdr (car dlist
)))
3125 (secnd (cdr (cadr dlist
))))
3126 (setcar first
(or (car first
)
3127 (car secnd
))) ; fetch_date
3128 (setq first
(cdr first
)
3130 (setcar first
(or (car first
)
3131 (car secnd
))) ; Keep_flag
3132 (setq first
(cdr first
)
3134 (setcar first
(or (car first
)
3135 (car secnd
))) ; NOV_entry_marker
3137 (setcdr dlist
(cddr dlist
)))
3138 (setq dlist
(cdr dlist
)))))
3139 (gnus-message 7 "gnus-agent-expire: Merging entries... Done")
3141 (let* ((len (float (length dlist
)))
3145 (let ((new-completed (truncate (* 100.0
3146 (/ (setq cnt
(1+ cnt
))
3149 (when (> new-completed completed
)
3150 (setq completed new-completed
)
3151 (gnus-message 7 "%3d%% completed..." completed
)))
3152 (let* ((entry (car dlist
))
3153 (article-number (nth 0 entry
))
3154 (fetch-date (nth 1 entry
))
3155 (keep (nth 2 entry
))
3156 (marker (nth 3 entry
)))
3159 ;; Kept articles are unread, marked, or special.
3161 (gnus-agent-message 10
3162 "gnus-agent-expire: %s:%d: Kept %s article%s."
3163 group article-number keep
(if fetch-date
" and file" ""))
3165 (unless (file-exists-p
3166 (concat dir
(number-to-string
3168 (setf (nth 1 entry
) nil
)
3169 (gnus-agent-message 3 "gnus-agent-expire cleared \
3170 download flag on %s:%d as the cached article file is missing."
3171 group
(caar dlist
)))
3173 (gnus-message 1 "gnus-agent-expire detected a \
3174 missing NOV entry. Run gnus-agent-regenerate-group to restore it.")))
3175 (gnus-agent-append-to-list
3177 (cons article-number fetch-date
)))
3179 ;; The following articles are READ, UNMARKED, and
3180 ;; ORDINARY. See if they can be EXPIRED!!!
3183 ((not (integerp fetch-date
))
3184 'read
) ;; never fetched article (may expire
3186 ((not (file-exists-p
3187 (concat dir
(number-to-string
3189 (setf (nth 1 entry
) nil
)
3190 'externally-expired
) ;; Can't find the cached
3191 ;; article. Handle case
3192 ;; as though this article
3193 ;; was never fetched.
3195 ;; We now have the arrival day, so we see
3196 ;; whether it's old enough to be expired.
3202 ;; I found some reason to expire this entry.
3204 (let ((actions nil
))
3205 (when (memq type
'(forced expired
))
3206 (ignore-errors ; Just being paranoid.
3207 (let* ((file-name (nnheader-concat dir
(number-to-string
3209 (size (float (nth 7 (file-attributes file-name
)))))
3210 (incf bytes-freed size
)
3211 (incf files-deleted
)
3212 (delete-file file-name
))
3213 (push "expired cached article" actions
))
3214 (setf (nth 1 entry
) nil
)
3218 (push "NOV entry removed" actions
)
3221 (incf nov-entries-deleted
)
3223 (let ((from (gnus-point-at-bol))
3224 (to (progn (forward-line 1) (point))))
3225 (incf bytes-freed
(- to from
))
3226 (delete-region from to
)))
3228 ;; If considering all articles is set, I can only
3229 ;; expire article IDs that are no longer in the
3230 ;; active range (That is, articles that preceed the
3231 ;; first article in the new alist).
3232 (if (and gnus-agent-consider-all-articles
3233 (>= article-number
(car active
)))
3234 ;; I have to keep this ID in the alist
3235 (gnus-agent-append-to-list
3236 tail-alist
(cons article-number fetch-date
))
3237 (push (format "Removed %s article number from \
3238 article alist" type
) actions
))
3241 (gnus-agent-message 8 "gnus-agent-expire: %s:%d: %s"
3242 group article-number
3243 (mapconcat 'identity actions
", ")))))
3246 10 "gnus-agent-expire: %s:%d: Article kept as \
3247 expiration tests failed." group article-number
)
3248 (gnus-agent-append-to-list
3249 tail-alist
(cons article-number fetch-date
)))
3252 ;; Clean up markers as I want to recycle this buffer
3253 ;; over several groups.
3255 (set-marker marker nil
))
3257 (setq dlist
(cdr dlist
))))
3259 (setq alist
(cdr alist
))
3261 (let ((inhibit-quit t
))
3262 (unless (equal alist gnus-agent-article-alist
)
3263 (setq gnus-agent-article-alist alist
)
3264 (gnus-agent-save-alist group
))
3266 (when (buffer-modified-p)
3267 (let ((coding-system-for-write
3268 gnus-agent-file-coding-system
))
3269 (gnus-make-directory dir
)
3270 (write-region (point-min) (point-max) nov-file nil
3272 ;; clear the modified flag as that I'm not confused by
3273 ;; its status on the next pass through this routine.
3274 (set-buffer-modified-p nil
)))
3276 (when (eq articles t
)
3277 (gnus-summary-update-info))))
3279 (when (boundp 'gnus-agent-expire-stats
)
3280 (let ((stats (symbol-value 'gnus-agent-expire-stats
)))
3281 (incf (nth 2 stats
) bytes-freed
)
3282 (incf (nth 1 stats
) files-deleted
)
3283 (incf (nth 0 stats
) nov-entries-deleted
)))
3286 (defun gnus-agent-expire (&optional articles group force
)
3287 "Expire all old articles.
3288 If you want to force expiring of certain articles, this function can
3289 take ARTICLES, GROUP and FORCE parameters as well.
3291 The articles on which the expiration process runs are selected as follows:
3292 if ARTICLES is null, all read and unmarked articles.
3293 if ARTICLES is t, all articles.
3294 if ARTICLES is a list, just those articles.
3295 Setting GROUP will limit expiration to that group.
3296 FORCE is equivalent to setting the expiration predicates to true."
3300 (gnus-agent-expire-group group articles force
)
3301 (if (or (not (eq articles t
))
3302 (yes-or-no-p "Are you sure that you want to expire all \
3303 articles in every agentized group."))
3304 (let ((methods (gnus-agent-covered-methods))
3305 ;; Bind gnus-agent-expire-current-dirs to enable tracking
3306 ;; of agent directories.
3307 (gnus-agent-expire-current-dirs nil
)
3308 ;; Bind gnus-agent-expire-stats to enable tracking of
3309 ;; expiration statistics across all groups
3310 (gnus-agent-expire-stats (list 0 0 0.0))
3311 gnus-command-method overview orig
)
3312 (setq overview
(gnus-get-buffer-create " *expire overview*"))
3314 (while (setq gnus-command-method
(pop methods
))
3315 (let ((active-file (gnus-agent-lib-file "active")))
3316 (when (file-exists-p active-file
)
3318 (nnheader-insert-file-contents active-file
)
3319 (gnus-active-to-gnus-format
3321 (setq orig
(gnus-make-hashtable
3322 (count-lines (point-min) (point-max))))))
3323 (dolist (expiring-group (gnus-groups-from-server
3324 gnus-command-method
))
3326 (gnus-gethash-safe expiring-group orig
)))
3330 (gnus-agent-expire-group-1
3331 expiring-group overview active articles force
))))))))
3332 (kill-buffer overview
))
3333 (gnus-agent-expire-unagentized-dirs)
3334 (gnus-message 4 (gnus-agent-expire-done-message))))))
3336 (defun gnus-agent-expire-done-message ()
3337 (if (and (> gnus-verbose
4)
3338 (boundp 'gnus-agent-expire-stats
))
3339 (let* ((stats (symbol-value 'gnus-agent-expire-stats
))
3340 (size (nth 2 stats
))
3341 (units '(B KB MB GB
)))
3342 (while (and (> size
1024.0)
3344 (setq size
(/ size
1024.0)
3347 (format "Expiry recovered %d NOV entries, deleted %d files,\
3354 (defun gnus-agent-expire-unagentized-dirs ()
3355 (when (and gnus-agent-expire-unagentized-dirs
3356 (boundp 'gnus-agent-expire-current-dirs
))
3357 (let* ((keep (gnus-make-hashtable))
3358 ;; Formally bind gnus-agent-expire-current-dirs so that the
3359 ;; compiler will not complain about free references.
3360 (gnus-agent-expire-current-dirs
3361 (symbol-value 'gnus-agent-expire-current-dirs
))
3364 (gnus-sethash gnus-agent-directory t keep
)
3365 (while gnus-agent-expire-current-dirs
3366 (setq dir
(pop gnus-agent-expire-current-dirs
))
3367 (when (and (stringp dir
)
3368 (file-directory-p dir
))
3369 (while (not (gnus-gethash dir keep
))
3370 (gnus-sethash dir t keep
)
3371 (setq dir
(file-name-directory (directory-file-name dir
))))))
3378 "Given a directory, check it and its subdirectories for
3379 membership in the keep hash. If it isn't found, add
3381 (let ((files (directory-files d
))
3383 (while (setq file
(pop files
))
3384 (cond ((equal file
".") ; Ignore self
3386 ((equal file
"..") ; Ignore parent
3388 ((equal file
".overview")
3389 ;; Directory must contain .overview to be
3390 ;; agent's cache of a group.
3391 (let ((d (file-name-as-directory d
))
3393 ;; Search ancestor's for last directory NOT
3394 ;; found in keep hash.
3395 (while (not (gnus-gethash
3396 (setq d
(file-name-directory d
)) keep
))
3398 d
(directory-file-name d
)))
3399 ;; if ANY ancestor was NOT in keep hash and
3400 ;; it it's already in to-remove, add it to
3403 (not (member r to-remove
)))
3404 (push r to-remove
))))
3405 ((file-directory-p (setq file
(nnheader-concat d file
)))
3406 (funcall checker file
)))))))))
3407 (funcall checker
(expand-file-name gnus-agent-directory
))
3409 (when (and to-remove
3410 (or gnus-expert-user
3412 "gnus-agent-expire has identified local directories that are\
3413 not currently required by any agentized group. Do you wish to consider\
3416 (let ((dir (pop to-remove
)))
3417 (if (gnus-y-or-n-p (format "Delete %s? " dir
))
3418 (let* (delete-recursive
3423 (if (file-directory-p f-or-d
)
3425 (delete-directory f-or-d
)
3428 (or (member f
'("." ".."))
3429 (funcall delete-recursive
3432 (directory-files f-or-d
))
3433 (delete-directory f-or-d
)))
3434 (delete-file f-or-d
)))))))
3435 (funcall delete-recursive dir
))))))))))
3438 (defun gnus-agent-batch ()
3439 "Start Gnus, send queue and fetch session."
3441 (let ((init-file-user "")
3442 (gnus-always-read-dribble-file t
))
3444 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation
))
3445 (gnus-group-send-queue)
3446 (gnus-agent-fetch-session)))
3448 (defun gnus-agent-unread-articles (group)
3449 (let* ((read (gnus-info-read (gnus-get-info group
)))
3450 (known (gnus-agent-load-alist group
))
3452 (tail-unread unread
))
3453 (while (and known read
)
3454 (let ((candidate (car (pop known
))))
3455 (while (let* ((range (car read
))
3456 (min (if (numberp range
) range
(car range
)))
3457 (max (if (numberp range
) range
(cdr range
))))
3458 (cond ((or (not min
)
3460 (gnus-agent-append-to-list tail-unread candidate
)
3463 (setq read
(cdr read
))
3464 ;; return t so that I always loop one more
3465 ;; time. If I just iterated off the end of
3466 ;; read, min will become nil and the current
3467 ;; candidate will be added to the unread list.
3470 (gnus-agent-append-to-list tail-unread
(car (pop known
))))
3473 (defun gnus-agent-uncached-articles (articles group
&optional cached-header
)
3474 "Restrict ARTICLES to numbers already fetched.
3475 Returns a sublist of ARTICLES that excludes those article ids in GROUP
3476 that have already been fetched.
3477 If CACHED-HEADER is nil, articles are only excluded if the article itself
3480 ;; Logically equivalent to: (gnus-sorted-difference articles (mapcar
3481 ;; 'car gnus-agent-article-alist))
3483 ;; Functionally, I don't need to construct a temp list using mapcar.
3485 (if (and (or gnus-agent-cache
(not gnus-plugged
))
3486 (gnus-agent-load-alist group
))
3487 (let* ((ref gnus-agent-article-alist
)
3489 (uncached (list nil
))
3490 (tail-uncached uncached
))
3491 (while (and ref arts
)
3492 (let ((v1 (car arts
))
3494 (cond ((< v1 v2
) ; v1 does not appear in the reference list
3495 (gnus-agent-append-to-list tail-uncached v1
)
3496 (setq arts
(cdr arts
)))
3498 (unless (or cached-header
(cdar ref
)) ; v1 is already cached
3499 (gnus-agent-append-to-list tail-uncached v1
))
3500 (setq arts
(cdr arts
))
3501 (setq ref
(cdr ref
)))
3502 (t ; reference article (v2) preceeds the list being filtered
3503 (setq ref
(cdr ref
))))))
3505 (gnus-agent-append-to-list tail-uncached
(pop arts
)))
3507 ;; if gnus-agent-load-alist fails, no articles are cached.
3510 (defun gnus-agent-retrieve-headers (articles group
&optional fetch-old
)
3512 (gnus-agent-create-buffer)
3513 (let ((gnus-decode-encoded-word-function 'identity
)
3514 (file (gnus-agent-article-name ".overview" group
))
3515 cached-articles uncached-articles
)
3516 (gnus-make-directory (nnheader-translate-file-chars
3517 (file-name-directory file
) t
))
3519 ;; Populate temp buffer with known headers
3520 (when (file-exists-p file
)
3521 (with-current-buffer gnus-agent-overview-buffer
3523 (let ((nnheader-file-coding-system
3524 gnus-agent-file-coding-system
))
3525 (nnheader-insert-nov-file file
(car articles
)))))
3527 (if (setq uncached-articles
(gnus-agent-uncached-articles articles group
3530 ;; Populate nntp-server-buffer with uncached headers
3531 (set-buffer nntp-server-buffer
)
3533 (cond ((not (eq 'nov
(let (gnus-agent) ; Turn off agent
3534 (gnus-retrieve-headers
3535 uncached-articles group fetch-old
))))
3536 (nnvirtual-convert-headers))
3537 ((eq 'nntp
(car gnus-current-select-method
))
3538 ;; The author of gnus-get-newsgroup-headers-xover
3539 ;; reports that the XOVER command is commonly
3540 ;; unreliable. The problem is that recently
3541 ;; posted articles may not be entered into the
3542 ;; NOV database in time to respond to my XOVER
3545 ;; I'm going to use his assumption that the NOV
3546 ;; database is updated in order of ascending
3547 ;; article ID. Therefore, a response containing
3548 ;; article ID N implies that all articles from 1
3549 ;; to N-1 are up-to-date. Therefore, missing
3550 ;; articles in that range have expired.
3552 (set-buffer nntp-server-buffer
)
3553 (let* ((fetched-articles (list nil
))
3554 (tail-fetched-articles fetched-articles
)
3555 (min (cond ((numberp fetch-old
)
3556 (max 1 (- (car articles
) fetch-old
)))
3561 (max (car (last articles
))))
3563 ;; Get the list of articles that were fetched
3564 (goto-char (point-min))
3565 (let ((pm (point-max))
3567 (while (< (point) pm
)
3568 (when (setq art
(gnus-agent-read-article-number))
3569 (gnus-agent-append-to-list tail-fetched-articles art
))
3572 ;; Clip this list to the headers that will
3573 ;; actually be returned
3574 (setq fetched-articles
(gnus-list-range-intersection
3575 (cdr fetched-articles
)
3578 ;; Clip the uncached articles list to exclude
3579 ;; IDs after the last FETCHED header. The
3580 ;; excluded IDs may be fetchable using HEAD.
3581 (if (car tail-fetched-articles
)
3582 (setq uncached-articles
3583 (gnus-list-range-intersection
3585 (cons (car uncached-articles
)
3586 (car tail-fetched-articles
)))))
3588 ;; Create the list of articles that were
3589 ;; "successfully" fetched. Success, in this
3590 ;; case, means that the ID should not be
3591 ;; fetched again. In the case of an expired
3592 ;; article, the header will not be fetched.
3593 (setq uncached-articles
3594 (gnus-sorted-nunion fetched-articles
3598 ;; Erase the temp buffer
3599 (set-buffer gnus-agent-overview-buffer
)
3602 ;; Copy the nntp-server-buffer to the temp buffer
3603 (set-buffer nntp-server-buffer
)
3604 (copy-to-buffer gnus-agent-overview-buffer
(point-min) (point-max))
3606 ;; Merge the temp buffer with the known headers (found on
3607 ;; disk in FILE) into the nntp-server-buffer
3608 (when uncached-articles
3609 (gnus-agent-braid-nov group uncached-articles file
))
3611 ;; Save the new set of known headers to FILE
3612 (set-buffer nntp-server-buffer
)
3613 (let ((coding-system-for-write
3614 gnus-agent-file-coding-system
))
3615 (gnus-agent-check-overview-buffer)
3616 (write-region (point-min) (point-max) file nil
'silent
))
3618 ;; Update the group's article alist to include the newly
3619 ;; fetched articles.
3620 (gnus-agent-load-alist group
)
3621 (gnus-agent-save-alist group uncached-articles nil
)
3624 ;; Copy the temp buffer to the nntp-server-buffer
3625 (set-buffer nntp-server-buffer
)
3627 (insert-buffer-substring gnus-agent-overview-buffer
)))
3630 (not (numberp fetch-old
)))
3631 t
; Don't remove anything.
3632 (nnheader-nov-delete-outside-range
3633 (if fetch-old
(max 1 (- (car articles
) fetch-old
))
3635 (car (last articles
)))
3640 (defun gnus-agent-request-article (article group
)
3641 "Retrieve ARTICLE in GROUP from the agent cache."
3642 (when (and gnus-agent
3643 (or gnus-agent-cache
3646 (let* ((gnus-command-method (gnus-find-method-for-group group
))
3647 (file (gnus-agent-article-name (number-to-string article
) group
))
3648 (buffer-read-only nil
))
3649 (when (and (file-exists-p file
)
3650 (> (nth 7 (file-attributes file
)) 0))
3652 (gnus-kill-all-overlays)
3653 (let ((coding-system-for-read gnus-cache-coding-system
))
3654 (insert-file-contents file
))
3657 (defun gnus-agent-regenerate-group (group &optional reread
)
3659 If REREAD is t, all articles in the .overview are marked as unread.
3660 If REREAD is a list, the specified articles will be marked as unread.
3661 In addition, their NOV entries in .overview will be refreshed using
3662 the articles' current headers.
3663 If REREAD is not nil, downloaded articles are marked as unread."
3665 (list (let ((def (or (gnus-group-group-name)
3666 gnus-newsgroup-name
)))
3667 (let ((select (read-string (if def
3668 (concat "Group Name ("
3671 (if (and (equal "" select
)
3677 (cursor-in-echo-area t
)
3678 (echo-keystrokes 0))
3679 (message "Mark as unread: (n)one / (a)ll / all (d)ownloaded articles? (n) ")
3680 (setq c
(read-char-exclusive))
3682 (cond ((or (eq c ?
\r) (eq c ?n
) (eq c ?N
))
3684 ((or (eq c ?a
) (eq c ?A
))
3686 ((or (eq c ?d
) (eq c ?D
))
3687 (throw 'mark
'some
)))
3688 (gnus-message 3 "Ignoring unexpected input")
3692 (gnus-message 5 "Regenerating in %s" group
)
3693 (let* ((gnus-command-method (or gnus-command-method
3694 (gnus-find-method-for-group group
)))
3695 (file (gnus-agent-article-name ".overview" group
))
3696 (dir (file-name-directory file
))
3698 (downloaded (if (file-exists-p dir
)
3699 (sort (mapcar (lambda (name) (string-to-int name
))
3700 (directory-files dir nil
"^[0-9]+$" t
))
3702 (progn (gnus-make-directory dir
) nil
)))
3707 (mm-with-unibyte-buffer
3708 (if (file-exists-p file
)
3709 (let ((nnheader-file-coding-system
3710 gnus-agent-file-coding-system
))
3711 (nnheader-insert-file-contents file
)))
3712 (set-buffer-modified-p nil
)
3714 ;; Load the article IDs found in the overview file. As a
3715 ;; side-effect, validate the file contents.
3719 (goto-char (point-min))
3720 (while (< (point) (point-max))
3721 (cond ((and (looking-at "[0-9]+\t")
3722 (<= (- (match-end 0) (match-beginning 0)) 9))
3723 (push (read (current-buffer)) nov-arts
)
3725 (let ((l1 (car nov-arts
))
3726 (l2 (cadr nov-arts
)))
3727 (cond ((and (listp reread
) (memq l1 reread
))
3729 (setq nov-arts
(cdr nov-arts
))
3730 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3731 entry of article %s deleted." l1
))
3735 (gnus-message 3 "gnus-agent-regenerate-group: NOV\
3736 entries are NOT in ascending order.")
3737 ;; Don't sort now as I haven't verified
3738 ;; that every line begins with a number
3742 (gnus-message 4 "gnus-agent-regenerate-group: NOV\
3743 entries contained duplicate of article %s. Duplicate deleted." l1
)
3745 (setq nov-arts
(cdr nov-arts
))))))
3747 (gnus-message 1 "gnus-agent-regenerate-group: NOV\
3748 entries contained line that did not begin with an article number. Deleted\
3750 (gnus-delete-line))))
3752 (gnus-message 5 "gnus-agent-regenerate-group: Sorting NOV\
3753 entries into ascending order.")
3754 (sort-numeric-fields 1 (point-min) (point-max))
3755 (setq nov-arts nil
))))
3756 (gnus-agent-check-overview-buffer)
3758 ;; Construct a new article alist whose nodes match every header
3759 ;; in the .overview file. As a side-effect, missing headers are
3760 ;; reconstructed from the downloaded article file.
3761 (while (or downloaded nov-arts
)
3762 (cond ((and downloaded
3764 (> (car downloaded
) (car nov-arts
))))
3765 ;; This entry is missing from the overview file
3766 (gnus-message 3 "Regenerating NOV %s %d..." group
3768 (let ((file (concat dir
(number-to-string (car downloaded
)))))
3769 (mm-with-unibyte-buffer
3770 (nnheader-insert-file-contents file
)
3771 (nnheader-remove-body)
3772 (setq header
(nnheader-parse-naked-head)))
3773 (mail-header-set-number header
(car downloaded
))
3775 (let ((key (concat "^" (int-to-string (car nov-arts
))
3777 (or (re-search-backward key nil t
)
3778 (re-search-forward key
))
3780 (goto-char (point-min)))
3781 (nnheader-insert-nov header
))
3782 (setq nov-arts
(cons (car downloaded
) nov-arts
)))
3783 ((eq (car downloaded
) (car nov-arts
))
3784 ;; This entry in the overview has been downloaded
3785 (push (cons (car downloaded
)
3787 (nth 5 (file-attributes
3788 (concat dir
(number-to-string
3789 (car downloaded
))))))) alist
)
3790 (setq downloaded
(cdr downloaded
))
3791 (setq nov-arts
(cdr nov-arts
)))
3793 ;; This entry in the overview has not been downloaded
3794 (push (cons (car nov-arts
) nil
) alist
)
3795 (setq nov-arts
(cdr nov-arts
)))))
3797 ;; When gnus-agent-consider-all-articles is set,
3798 ;; gnus-agent-regenerate-group should NOT remove article IDs from
3799 ;; the alist. Those IDs serve as markers to indicate that an
3800 ;; attempt has been made to fetch that article's header.
3802 ;; When gnus-agent-consider-all-articles is NOT set,
3803 ;; gnus-agent-regenerate-group can remove the article ID of every
3804 ;; article (with the exception of the last ID in the list - it's
3805 ;; special) that no longer appears in the overview. In this
3806 ;; situtation, the last article ID in the list implies that it,
3807 ;; and every article ID preceeding it, have been fetched from the
3810 (if gnus-agent-consider-all-articles
3811 ;; Restore all article IDs that were not found in the overview file.
3812 (let* ((n (cons nil alist
))
3814 (o (gnus-agent-load-alist group
)))
3816 (let ((nID (caadr n
))
3819 (setq n
(setcdr n
(list (list oID
))))
3822 (setcdr n
(cons (list oID
) (cdr n
)))
3828 (setq n
(cdr n
))))))
3829 (setq alist
(cdr merged
)))
3830 ;; Restore the last article ID if it is not already in the new alist
3831 (let ((n (last alist
))
3832 (o (last (gnus-agent-load-alist group
))))
3836 (push (cons (caar o
) nil
) alist
))
3837 ((< (caar n
) (caar o
))
3838 (setcdr n
(list (car o
)))))))
3840 (let ((inhibit-quit t
))
3841 (if (setq regenerated
(buffer-modified-p))
3842 (let ((coding-system-for-write gnus-agent-file-coding-system
))
3843 (write-region (point-min) (point-max) file nil
'silent
)))
3845 (setq regenerated
(or regenerated
3846 (and reread gnus-agent-article-alist
)
3847 (not (equal alist gnus-agent-article-alist
))))
3849 (setq gnus-agent-article-alist alist
)
3852 (gnus-agent-save-alist group
)
3854 ;; I have to alter the group's active range NOW as
3855 ;; gnus-make-ascending-articles-unread will use it to
3856 ;; recalculate the number of unread articles in the group
3858 (let ((group (gnus-group-real-name group
))
3859 (group-active (or (gnus-active group
)
3860 (gnus-activate-group group
))))
3861 (gnus-agent-possibly-alter-active group group-active
)))))
3863 (when (and reread gnus-agent-article-alist
)
3864 (gnus-make-ascending-articles-unread
3868 (delq nil
(mapcar (function (lambda (c)
3869 (cond ((eq reread t
)
3873 gnus-agent-article-alist
))))
3875 (when (gnus-buffer-live-p gnus-group-buffer
)
3876 (gnus-group-update-group group t
)))
3882 (defun gnus-agent-regenerate (&optional clean reread
)
3883 "Regenerate all agent covered files.
3884 If CLEAN, obsolete (ignore)."
3887 (gnus-message 4 "Regenerating Gnus agent files...")
3888 (dolist (gnus-command-method (gnus-agent-covered-methods))
3889 (dolist (group (gnus-groups-from-server gnus-command-method
))
3890 (setq regenerated
(or (gnus-agent-regenerate-group group reread
)
3892 (gnus-message 4 "Regenerating Gnus agent files...done")
3896 (defun gnus-agent-go-online (&optional force
)
3897 "Switch servers into online status."
3898 (interactive (list t
))
3899 (dolist (server gnus-opened-servers
)
3900 (when (eq (nth 1 server
) 'offline
)
3901 (if (if (eq force
'ask
)
3903 (format "Switch %s:%s into online status? "
3904 (caar server
) (cadar server
)))
3906 (setcar (nthcdr 1 server
) 'close
)))))
3908 (defun gnus-agent-toggle-group-plugged (group)
3909 "Toggle the status of the server of the current group."
3910 (interactive (list (gnus-group-group-name)))
3911 (let* ((method (gnus-find-method-for-group group
))
3912 (status (cadr (assoc method gnus-opened-servers
))))
3913 (if (eq status
'offline
)
3914 (gnus-server-set-status method
'closed
)
3915 (gnus-close-server method
)
3916 (gnus-server-set-status method
'offline
))
3917 (message "Turn %s:%s from %s to %s." (car method
) (cadr method
)
3918 (if (eq status
'offline
) 'offline
'online
)
3919 (if (eq status
'offline
) 'online
'offline
))))
3921 (defun gnus-agent-group-covered-p (group)
3922 (gnus-agent-method-p (gnus-group-method group
)))
3924 (provide 'gnus-agent
)
3926 ;;; arch-tag: b0ba4afc-5229-4cee-ad25-9956daa4e91e
3927 ;;; gnus-agent.el ends here