1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
37 (if (featurep 'xemacs)
43 (autoload 'gnus-server-update-server "gnus-srvr")
44 (autoload 'gnus-agent-customize-category "gnus-cus")
47 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
48 "Where the Gnus agent will store its files."
52 (defcustom gnus-agent-plugged-hook nil
53 "Hook run when plugging into the network."
57 (defcustom gnus-agent-unplugged-hook nil
58 "Hook run when unplugging from the network."
62 (defcustom gnus-agent-fetched-hook nil
63 "Hook run when finished fetching articles."
68 (defcustom gnus-agent-handle-level gnus-level-subscribed
69 "Groups on levels higher than this variable will be ignored by the Agent."
73 (defcustom gnus-agent-expire-days 7
74 "Read articles older than this will be expired.
75 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
77 :type '(number :tag "days"))
79 (defcustom gnus-agent-expire-all nil
80 "If non-nil, also expire unread, ticked and dormant articles.
81 If nil, only read articles will be expired."
85 (defcustom gnus-agent-group-mode-hook nil
86 "Hook run in Agent group minor modes."
90 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
91 (when (featurep 'xemacs)
92 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
94 (defcustom gnus-agent-summary-mode-hook nil
95 "Hook run in Agent summary minor modes."
99 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
100 (when (featurep 'xemacs)
101 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
103 (defcustom gnus-agent-server-mode-hook nil
104 "Hook run in Agent summary minor modes."
108 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
109 (when (featurep 'xemacs)
110 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
112 (defcustom gnus-agent-confirmation-function 'y-or-n-p
113 "Function to confirm when error happens."
118 (defcustom gnus-agent-synchronize-flags nil
119 "Indicate if flags are synchronized when you plug in.
120 If this is `ask' the hook will query the user."
121 ;; If the default switches to something else than nil, then the function
122 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
124 :type '(choice (const :tag "Always" t)
125 (const :tag "Never" nil)
126 (const :tag "Ask" ask))
129 (defcustom gnus-agent-go-online 'ask
130 "Indicate if offline servers go online when you plug in.
131 If this is `ask' the hook will query the user."
133 :type '(choice (const :tag "Always" t)
134 (const :tag "Never" nil)
135 (const :tag "Ask" ask))
138 (defcustom gnus-agent-mark-unread-after-downloaded t
139 "Indicate whether to mark articles unread after downloaded."
144 (defcustom gnus-agent-download-marks '(download)
145 "Marks for downloading."
147 :type '(repeat (symbol :tag "Mark"))
150 (defcustom gnus-agent-consider-all-articles nil
151 "When non-nil, the agent will let the agent predicate decide
152 whether articles need to be downloaded or not, for all articles. When
153 nil, the default, the agent will only let the predicate decide
154 whether unread articles are downloaded or not. If you enable this,
155 groups with large active ranges may open slower and you may also want
156 to look into the agent expiry settings to block the expiration of
157 read articles as they would just be downloaded again."
162 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
163 "Chunk size for `gnus-agent-fetch-session'.
164 The function will split its article fetches into chunks smaller than
170 (defcustom gnus-agent-enable-expiration 'ENABLE
171 "The default expiration state for each group.
172 When set to ENABLE, the default, `gnus-agent-expire' will expire old
173 contents from a group's local storage. This value may be overridden
174 to disable expiration in specific categories, topics, and groups. Of
175 course, you could change gnus-agent-enable-expiration to DISABLE then
176 enable expiration per categories, topics, and groups."
179 :type '(radio (const :format "Enable " ENABLE)
180 (const :format "Disable " DISABLE)))
182 (defcustom gnus-agent-expire-unagentized-dirs t
183 "*Whether expiration should expire in unagentized directories.
184 Have gnus-agent-expire scan the directories under
185 \(gnus-agent-directory) for groups that are no longer agentized.
186 When found, offer to remove them."
191 (defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
192 "Initially, all servers from these methods are agentized.
193 The user may remove or add servers using the Server buffer.
194 See Info node `(gnus)Server Buffer'."
196 :type '(repeat symbol)
199 (defcustom gnus-agent-queue-mail t
200 "Whether and when outgoing mail should be queued by the agent.
201 When `always', always queue outgoing mail. When nil, never
202 queue. Otherwise, queue if and only if unplugged."
205 :type '(radio (const :format "Always" always)
206 (const :format "Never" nil)
207 (const :format "When unplugged" t)))
209 (defcustom gnus-agent-prompt-send-queue nil
210 "If non-nil, `gnus-group-send-queue' will prompt if called when
216 (defcustom gnus-agent-article-alist-save-format 1
217 "Indicates whether to use compression(2), versus no
218 compression(1), when writing agentview files. The compressed
219 files do save space but load times are 6-7 times higher. A group
220 must be opened then closed for the agentview to be updated using
222 ;; Wouldn't symbols instead numbers be nicer? --rsteib
225 :type '(radio (const :format "Compressed" 2)
226 (const :format "Uncompressed" 1)))
228 ;;; Internal variables
230 (defvar gnus-agent-history-buffers nil)
231 (defvar gnus-agent-buffer-alist nil)
232 (defvar gnus-agent-article-alist nil
233 "An assoc list identifying the articles whose headers have been fetched.
234 If successfully fetched, these headers will be stored in the group's overview
235 file. The key of each assoc pair is the article ID, the value of each assoc
236 pair is a flag indicating whether the identified article has been downloaded
237 \(gnus-agent-fetch-articles sets the value to the day of the download).
239 1) The last element of this list can not be expired as some
240 routines (for example, get-agent-fetch-headers) use the last
241 value to track which articles have had their headers retrieved.
242 2) The function `gnus-agent-regenerate' may destructively modify the value.")
243 (defvar gnus-agent-group-alist nil)
244 (defvar gnus-category-alist nil)
245 (defvar gnus-agent-current-history nil)
246 (defvar gnus-agent-overview-buffer nil)
247 (defvar gnus-category-predicate-cache nil)
248 (defvar gnus-category-group-cache nil)
249 (defvar gnus-agent-spam-hashtb nil)
250 (defvar gnus-agent-file-name nil)
251 (defvar gnus-agent-send-mail-function nil)
252 (defvar gnus-agent-file-coding-system 'raw-text)
253 (defvar gnus-agent-file-loading-cache nil)
254 (defvar gnus-agent-total-fetched-hashtb nil)
255 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
256 (defvar gnus-agent-need-update-total-fetched-for nil)
259 (defvar gnus-headers)
266 (defun gnus-open-agent ()
268 (gnus-agent-read-servers)
270 (gnus-agent-create-buffer)
271 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
272 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
273 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
275 (defun gnus-agent-create-buffer ()
276 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
278 (setq gnus-agent-overview-buffer
279 (gnus-get-buffer-create " *Gnus agent overview*"))
280 (with-current-buffer gnus-agent-overview-buffer
281 (mm-enable-multibyte))
284 (gnus-add-shutdown 'gnus-close-agent 'gnus)
286 (defun gnus-close-agent ()
287 (setq gnus-category-predicate-cache nil
288 gnus-category-group-cache nil
289 gnus-agent-spam-hashtb nil)
290 (gnus-kill-buffer gnus-agent-overview-buffer))
293 ;;; Utility functions
296 (defmacro gnus-agent-with-refreshed-group (group &rest body)
297 "Performs the body then updates the group's line in the group
298 buffer. Automatically blocks multiple updates due to recursion."
299 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
300 (when (and gnus-agent-need-update-total-fetched-for
301 (not gnus-agent-inhibit-update-total-fetched-for))
303 (set-buffer gnus-group-buffer)
304 (setq gnus-agent-need-update-total-fetched-for nil)
305 (gnus-group-update-group ,group t)))))
307 (defun gnus-agent-read-file (file)
308 "Load FILE and do a `read' there."
311 (nnheader-insert-file-contents file)
312 (goto-char (point-min))
313 (read (current-buffer)))))
315 (defsubst gnus-agent-method ()
316 (concat (symbol-name (car gnus-command-method)) "/"
317 (if (equal (cadr gnus-command-method) "")
319 (cadr gnus-command-method))))
321 (defsubst gnus-agent-directory ()
322 "The name of the Gnus agent directory."
323 (nnheader-concat gnus-agent-directory
324 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
326 (defun gnus-agent-lib-file (file)
327 "The full name of the Gnus agent library FILE."
328 (expand-file-name file
329 (file-name-as-directory
330 (expand-file-name "agent.lib" (gnus-agent-directory)))))
332 (defun gnus-agent-cat-set-property (category property value)
334 (setcdr (or (assq property category)
335 (let ((cell (cons property nil)))
336 (setcdr category (cons cell (cdr category)))
338 (let ((category category))
339 (while (cond ((eq property (caadr category))
340 (setcdr category (cddr category))
343 (setq category (cdr category)))))))
347 (defmacro gnus-agent-cat-defaccessor (name prop-name)
348 "Define accessor and setter methods for manipulating a list of the form
349 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
350 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
351 manipulated as follows:
352 (func LIST): Returns VALUE1
353 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
354 `(progn (defmacro ,name (category)
355 (list (quote cdr) (list (quote assq)
356 (quote (quote ,prop-name)) category)))
358 (define-setf-method ,name (category)
359 (let* ((--category--temp-- (make-symbol "--category--"))
360 (--value--temp-- (make-symbol "--value--")))
361 (list (list --category--temp--) ; temporary-variables
362 (list category) ; value-forms
363 (list --value--temp--) ; store-variables
364 (let* ((category --category--temp--) ; store-form
365 (value --value--temp--))
366 (list (quote gnus-agent-cat-set-property)
368 (quote (quote ,prop-name))
370 (list (quote ,name) --category--temp--) ; access-form
374 (defmacro gnus-agent-cat-name (category)
377 (gnus-agent-cat-defaccessor
378 gnus-agent-cat-days-until-old agent-days-until-old)
379 (gnus-agent-cat-defaccessor
380 gnus-agent-cat-enable-expiration agent-enable-expiration)
381 (gnus-agent-cat-defaccessor
382 gnus-agent-cat-groups agent-groups)
383 (gnus-agent-cat-defaccessor
384 gnus-agent-cat-high-score agent-high-score)
385 (gnus-agent-cat-defaccessor
386 gnus-agent-cat-length-when-long agent-long-article)
387 (gnus-agent-cat-defaccessor
388 gnus-agent-cat-length-when-short agent-short-article)
389 (gnus-agent-cat-defaccessor
390 gnus-agent-cat-low-score agent-low-score)
391 (gnus-agent-cat-defaccessor
392 gnus-agent-cat-predicate agent-predicate)
393 (gnus-agent-cat-defaccessor
394 gnus-agent-cat-score-file agent-score)
395 (gnus-agent-cat-defaccessor
396 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
399 ;; This form is equivalent to defsetf except that it calls make-symbol
400 ;; whereas defsetf calls gensym (Using gensym creates a run-time
401 ;; dependency on the CL library).
404 (define-setf-method gnus-agent-cat-groups (category)
405 (let* ((--category--temp-- (make-symbol "--category--"))
406 (--groups--temp-- (make-symbol "--groups--")))
407 (list (list --category--temp--)
409 (list --groups--temp--)
410 (let* ((category --category--temp--)
411 (groups --groups--temp--))
412 (list (quote gnus-agent-set-cat-groups) category groups))
413 (list (quote gnus-agent-cat-groups) --category--temp--))))
416 (defun gnus-agent-set-cat-groups (category groups)
417 (unless (eq groups 'ignore)
419 (old-g (gnus-agent-cat-groups category)))
420 (cond ((eq new-g old-g)
421 ;; gnus-agent-add-group is fiddling with the group
422 ;; list. Still, Im done.
425 ((eq new-g (cdr old-g))
426 ;; gnus-agent-add-group is fiddling with the group list
427 (setcdr (or (assq 'agent-groups category)
428 (let ((cell (cons 'agent-groups nil)))
429 (setcdr category (cons cell (cdr category)))
432 (let ((groups groups))
434 (let* ((group (pop groups))
435 (old-category (gnus-group-category group)))
436 (if (eq category old-category)
438 (setf (gnus-agent-cat-groups old-category)
439 (delete group (gnus-agent-cat-groups
441 ;; Purge cache as preceeding loop invalidated it.
442 (setq gnus-category-group-cache nil))
444 (setcdr (or (assq 'agent-groups category)
445 (let ((cell (cons 'agent-groups nil)))
446 (setcdr category (cons cell (cdr category)))
449 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
450 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
452 ;;; Fetching setup functions.
454 (defun gnus-agent-start-fetch ()
455 "Initialize data structures for efficient fetching."
456 (gnus-agent-create-buffer))
458 (defun gnus-agent-stop-fetch ()
459 "Save all data structures and clean up."
460 (setq gnus-agent-spam-hashtb nil)
462 (set-buffer nntp-server-buffer)
465 (defmacro gnus-agent-with-fetch (&rest forms)
468 (let ((gnus-agent-fetching t))
469 (gnus-agent-start-fetch)
471 (gnus-agent-stop-fetch)))
473 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
474 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
476 (defmacro gnus-agent-append-to-list (tail value)
477 `(setq ,tail (setcdr ,tail (cons ,value nil))))
479 (defmacro gnus-agent-message (level &rest args)
480 `(if (<= ,level gnus-verbose)
487 (defvar gnus-agent-mode-hook nil
488 "Hook run when installing agent mode.")
490 (defvar gnus-agent-mode nil)
491 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
493 (defun gnus-agent-mode ()
494 "Minor mode for providing a agent support in Gnus buffers."
495 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
496 (symbol-name major-mode))
497 (match-string 1 (symbol-name major-mode))))
498 (mode (intern (format "gnus-agent-%s-mode" buffer))))
499 (set (make-local-variable 'gnus-agent-mode) t)
501 (set (make-local-variable mode) t)
503 (when (gnus-visual-p 'agent-menu 'menu)
504 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
505 (unless (assq 'gnus-agent-mode minor-mode-alist)
506 (push gnus-agent-mode-status minor-mode-alist))
507 (unless (assq mode minor-mode-map-alist)
508 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
510 minor-mode-map-alist))
511 (when (eq major-mode 'gnus-group-mode)
512 (let ((init-plugged gnus-plugged)
513 (gnus-agent-go-online nil))
514 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
515 ;; Therefore, make certain that the current value does not
516 ;; match the desired initial value.
517 (setq gnus-plugged :unknown)
518 (gnus-agent-toggle-plugged init-plugged)))
519 (gnus-run-hooks 'gnus-agent-mode-hook
520 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
522 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
523 (gnus-define-keys gnus-agent-group-mode-map
524 "Ju" gnus-agent-fetch-groups
525 "Jc" gnus-enter-category-buffer
526 "Jj" gnus-agent-toggle-plugged
527 "Js" gnus-agent-fetch-session
528 "JY" gnus-agent-synchronize-flags
529 "JS" gnus-group-send-queue
530 "Ja" gnus-agent-add-group
531 "Jr" gnus-agent-remove-group
532 "Jo" gnus-agent-toggle-group-plugged)
534 (defun gnus-agent-group-make-menu-bar ()
535 (unless (boundp 'gnus-agent-group-menu)
537 gnus-agent-group-menu gnus-agent-group-mode-map ""
539 ["Toggle plugged" gnus-agent-toggle-plugged t]
540 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
541 ["List categories" gnus-enter-category-buffer t]
542 ["Add (current) group to category" gnus-agent-add-group t]
543 ["Remove (current) group from category" gnus-agent-remove-group t]
544 ["Send queue" gnus-group-send-queue gnus-plugged]
546 ["All" gnus-agent-fetch-session gnus-plugged]
547 ["Group" gnus-agent-fetch-group gnus-plugged])
548 ["Synchronize flags" gnus-agent-synchronize-flags t]
551 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
552 (gnus-define-keys gnus-agent-summary-mode-map
553 "Jj" gnus-agent-toggle-plugged
554 "Ju" gnus-agent-summary-fetch-group
555 "JS" gnus-agent-fetch-group
556 "Js" gnus-agent-summary-fetch-series
557 "J#" gnus-agent-mark-article
558 "J\M-#" gnus-agent-unmark-article
559 "@" gnus-agent-toggle-mark
560 "Jc" gnus-agent-catchup)
562 (defun gnus-agent-summary-make-menu-bar ()
563 (unless (boundp 'gnus-agent-summary-menu)
565 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
567 ["Toggle plugged" gnus-agent-toggle-plugged t]
568 ["Mark as downloadable" gnus-agent-mark-article t]
569 ["Unmark as downloadable" gnus-agent-unmark-article t]
570 ["Toggle mark" gnus-agent-toggle-mark t]
571 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
572 ["Catchup undownloaded" gnus-agent-catchup t]))))
574 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
575 (gnus-define-keys gnus-agent-server-mode-map
576 "Jj" gnus-agent-toggle-plugged
577 "Ja" gnus-agent-add-server
578 "Jr" gnus-agent-remove-server)
580 (defun gnus-agent-server-make-menu-bar ()
581 (unless (boundp 'gnus-agent-server-menu)
583 gnus-agent-server-menu gnus-agent-server-mode-map ""
585 ["Toggle plugged" gnus-agent-toggle-plugged t]
586 ["Add" gnus-agent-add-server t]
587 ["Remove" gnus-agent-remove-server t]))))
589 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
590 (if (and (fboundp 'propertize)
591 (fboundp 'make-mode-line-mouse-map))
592 (propertize string 'local-map
593 (make-mode-line-mouse-map mouse-button mouse-func)
594 'mouse-face 'mode-line-highlight)
597 (defun gnus-agent-toggle-plugged (set-to)
598 "Toggle whether Gnus is unplugged or not."
599 (interactive (list (not gnus-plugged)))
600 (cond ((eq set-to gnus-plugged)
603 (setq gnus-plugged set-to)
604 (gnus-run-hooks 'gnus-agent-plugged-hook)
605 (setcar (cdr gnus-agent-mode-status)
606 (gnus-agent-make-mode-line-string " Plugged"
608 'gnus-agent-toggle-plugged))
609 (gnus-agent-go-online gnus-agent-go-online)
610 (gnus-agent-possibly-synchronize-flags))
612 (gnus-agent-close-connections)
613 (setq gnus-plugged set-to)
614 (gnus-run-hooks 'gnus-agent-unplugged-hook)
615 (setcar (cdr gnus-agent-mode-status)
616 (gnus-agent-make-mode-line-string " Unplugged"
618 'gnus-agent-toggle-plugged))))
619 (set-buffer-modified-p t))
621 (defmacro gnus-agent-while-plugged (&rest body)
622 `(let ((original-gnus-plugged gnus-plugged))
624 (progn (gnus-agent-toggle-plugged t)
626 (gnus-agent-toggle-plugged original-gnus-plugged))))
628 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
629 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
631 (defun gnus-agent-close-connections ()
632 "Close all methods covered by the Gnus agent."
633 (let ((methods (gnus-agent-covered-methods)))
635 (gnus-close-server (pop methods)))))
638 (defun gnus-unplugged ()
639 "Start Gnus unplugged."
641 (setq gnus-plugged nil)
645 (defun gnus-plugged ()
646 "Start Gnus plugged."
648 (setq gnus-plugged t)
652 (defun gnus-slave-unplugged (&optional arg)
653 "Read news as a slave unplugged."
655 (setq gnus-plugged nil)
656 (gnus arg nil 'slave))
659 (defun gnus-agentize ()
660 "Allow Gnus to be an offline newsreader.
662 The gnus-agentize function is now called internally by gnus when
663 gnus-agent is set. If you wish to avoid calling gnus-agentize,
664 customize gnus-agent to nil.
666 This will modify the `gnus-setup-news-hook', and
667 `message-send-mail-real-function' variables, and install the Gnus agent
668 minor mode in all Gnus buffers."
671 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
672 (unless gnus-agent-send-mail-function
673 (setq gnus-agent-send-mail-function
674 (or message-send-mail-real-function
675 (function (lambda () (funcall message-send-mail-function))))
676 message-send-mail-real-function 'gnus-agent-send-mail))
678 ;; If the servers file doesn't exist, auto-agentize some servers and
679 ;; save the servers file so this auto-agentizing isn't invoked
681 (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
682 (gnus-message 3 "First time agent user, agentizing remote groups...")
684 (lambda (server-or-method)
685 (let ((method (gnus-server-to-method server-or-method)))
686 (when (memq (car method)
687 gnus-agent-auto-agentize-methods)
688 (push (gnus-method-to-server method)
689 gnus-agent-covered-methods)
690 (setq gnus-agent-method-p-cache nil))))
691 (cons gnus-select-method gnus-secondary-select-methods))
692 (gnus-agent-write-servers)))
694 (defun gnus-agent-queue-setup (&optional group-name)
695 "Make sure the queue group exists.
696 Optional arg GROUP-NAME allows to specify another group."
697 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
699 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
700 (let ((gnus-level-default-subscribed 1))
701 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
703 (gnus-group-set-parameter
704 (format "nndraft:%s" (or group-name "queue"))
705 'gnus-dummy '((gnus-draft-mode)))))
707 (defun gnus-agent-send-mail ()
708 (if (or (not gnus-agent-queue-mail)
709 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
710 (funcall gnus-agent-send-mail-function)
711 (goto-char (point-min))
713 (concat "^" (regexp-quote mail-header-separator) "\n"))
715 (gnus-agent-insert-meta-information 'mail)
716 (gnus-request-accept-article "nndraft:queue" nil t t)))
718 (defun gnus-agent-insert-meta-information (type &optional method)
719 "Insert meta-information into the message that says how it's to be posted.
720 TYPE can be either `mail' or `news'. If the latter, then METHOD can
723 (message-remove-header gnus-agent-meta-information-header)
724 (goto-char (point-min))
725 (insert gnus-agent-meta-information-header ": "
726 (symbol-name type) " " (format "%S" method)
729 (while (search-backward "\n" nil t)
730 (replace-match "\\n" t t))))
732 (defun gnus-agent-restore-gcc ()
733 "Restore GCC field from saved header."
735 (goto-char (point-min))
736 (while (re-search-forward
737 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
738 (replace-match "Gcc:" 'fixedcase))))
740 (defun gnus-agent-any-covered-gcc ()
742 (message-narrow-to-headers)
743 (let* ((gcc (mail-fetch-field "gcc" nil t))
745 (mapcar 'gnus-inews-group-method
746 (message-unquote-tokens
747 (message-tokenize-header
750 (while (and (not covered) methods)
751 (setq covered (gnus-agent-method-p (car methods))
752 methods (cdr methods)))
756 (defun gnus-agent-possibly-save-gcc ()
757 "Save GCC if Gnus is unplugged."
758 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
760 (goto-char (point-min))
761 (let ((case-fold-search t))
762 (while (re-search-forward "^gcc:" nil t)
763 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
765 (defun gnus-agent-possibly-do-gcc ()
766 "Do GCC if Gnus is plugged."
767 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
768 (gnus-inews-do-gcc)))
771 ;;; Group mode commands
774 (defun gnus-agent-fetch-groups (n)
775 "Put all new articles in the current groups into the Agent."
778 (error "Groups can't be fetched when Gnus is unplugged"))
779 (gnus-group-iterate n 'gnus-agent-fetch-group))
781 (defun gnus-agent-fetch-group (&optional group)
782 "Put all new articles in GROUP into the Agent."
783 (interactive (list (gnus-group-group-name)))
784 (setq group (or group gnus-newsgroup-name))
786 (error "No group on the current line"))
788 (gnus-agent-while-plugged
789 (let ((gnus-command-method (gnus-find-method-for-group group)))
790 (gnus-agent-with-fetch
791 (gnus-agent-fetch-group-1 group gnus-command-method)
792 (gnus-message 5 "Fetching %s...done" group)))))
794 (defun gnus-agent-add-group (category arg)
795 "Add the current group to an agent category."
801 (mapcar (lambda (cat) (list (symbol-name (car cat))))
805 (let ((cat (assq category gnus-category-alist))
807 (gnus-group-iterate arg
809 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
810 (setf (gnus-agent-cat-groups c)
811 (delete group (gnus-agent-cat-groups c))))
812 (push group groups)))
813 (setf (gnus-agent-cat-groups cat)
814 (nconc (gnus-agent-cat-groups cat) groups))
815 (gnus-category-write)))
817 (defun gnus-agent-remove-group (arg)
818 "Remove the current group from its agent category, if any."
821 (gnus-group-iterate arg
823 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
824 (setf (gnus-agent-cat-groups c)
825 (delete group (gnus-agent-cat-groups c))))))
826 (gnus-category-write)))
828 (defun gnus-agent-synchronize-flags ()
829 "Synchronize unplugged flags with servers."
832 (dolist (gnus-command-method (gnus-agent-covered-methods))
833 (when (file-exists-p (gnus-agent-lib-file "flags"))
834 (gnus-agent-synchronize-flags-server gnus-command-method)))))
836 (defun gnus-agent-possibly-synchronize-flags ()
837 "Synchronize flags according to `gnus-agent-synchronize-flags'."
840 (dolist (gnus-command-method (gnus-agent-covered-methods))
841 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
842 (eq (gnus-server-status gnus-command-method) 'ok))
843 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
845 (defun gnus-agent-synchronize-flags-server (method)
846 "Synchronize flags set when unplugged for server."
847 (let ((gnus-command-method method)
849 (when (file-exists-p (gnus-agent-lib-file "flags"))
850 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
852 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
853 (cond ((null gnus-plugged)
855 1 "You must be plugged to synchronize flags with server %s"
856 (nth 1 gnus-command-method)))
857 ((null (gnus-check-server gnus-command-method))
859 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
864 (eval (read (current-buffer)))
865 (delete-region bgn (point))))
867 (delete-file (gnus-agent-lib-file "flags")))
869 (let ((file (gnus-agent-lib-file "flags")))
870 (write-region (point-min) (point-max)
871 (gnus-agent-lib-file "flags") nil 'silent)
872 (error "Couldn't set flags from file %s due to %s"
873 file (error-message-string err)))))))
876 (defun gnus-agent-possibly-synchronize-flags-server (method)
877 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
878 (when (or (and gnus-agent-synchronize-flags
879 (not (eq gnus-agent-synchronize-flags 'ask)))
880 (and (eq gnus-agent-synchronize-flags 'ask)
881 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
883 (gnus-agent-synchronize-flags-server method)))
886 (defun gnus-agent-rename-group (old-group new-group)
887 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
888 Always updates the agent, even when disabled, as the old agent
889 files would corrupt gnus when the agent was next enabled.
890 Depends upon the caller to determine whether group renaming is
892 (let* ((old-command-method (gnus-find-method-for-group old-group))
893 (old-path (directory-file-name
894 (let (gnus-command-method old-command-method)
895 (gnus-agent-group-pathname old-group))))
896 (new-command-method (gnus-find-method-for-group new-group))
897 (new-path (directory-file-name
898 (let (gnus-command-method new-command-method)
899 (gnus-agent-group-pathname new-group)))))
900 (gnus-rename-file old-path new-path t)
902 (let* ((old-real-group (gnus-group-real-name old-group))
903 (new-real-group (gnus-group-real-name new-group))
904 (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
905 (gnus-agent-save-group-info old-command-method old-real-group nil)
906 (gnus-agent-save-group-info new-command-method new-real-group old-active)
908 (let ((old-local (gnus-agent-get-local old-group
909 old-real-group old-command-method)))
910 (gnus-agent-set-local old-group
912 old-real-group old-command-method)
913 (gnus-agent-set-local new-group
914 (car old-local) (cdr old-local)
915 new-real-group new-command-method)))))
918 (defun gnus-agent-delete-group (group)
919 "Delete fully-qualified GROUP.
920 Always updates the agent, even when disabled, as the old agent
921 files would corrupt gnus when the agent was next enabled.
922 Depends upon the caller to determine whether group deletion is
924 (let* ((command-method (gnus-find-method-for-group group))
925 (path (directory-file-name
926 (let (gnus-command-method command-method)
927 (gnus-agent-group-pathname group)))))
928 (gnus-delete-directory path)
930 (let* ((real-group (gnus-group-real-name group)))
931 (gnus-agent-save-group-info command-method real-group nil)
933 (let ((local (gnus-agent-get-local group
934 real-group command-method)))
935 (gnus-agent-set-local group
937 real-group command-method)))))
940 ;;; Server mode commands
943 (defun gnus-agent-add-server ()
944 "Enroll SERVER in the agent program."
946 (let* ((server (gnus-server-server-name))
947 (named-server (gnus-server-named-server))
949 (gnus-server-get-method nil server))))
951 (error "No server on the current line"))
953 (when (gnus-agent-method-p method)
954 (error "Server already in the agent program"))
956 (push named-server gnus-agent-covered-methods)
958 (setq gnus-agent-method-p-cache nil)
959 (gnus-server-update-server server)
960 (gnus-agent-write-servers)
961 (gnus-message 1 "Entered %s into the Agent" server)))
963 (defun gnus-agent-remove-server ()
964 "Remove SERVER from the agent program."
966 (let* ((server (gnus-server-server-name))
967 (named-server (gnus-server-named-server)))
969 (error "No server on the current line"))
971 (unless (member named-server gnus-agent-covered-methods)
972 (error "Server not in the agent program"))
974 (setq gnus-agent-covered-methods
975 (delete named-server gnus-agent-covered-methods)
976 gnus-agent-method-p-cache nil)
978 (gnus-server-update-server server)
979 (gnus-agent-write-servers)
980 (gnus-message 1 "Removed %s from the agent" server)))
982 (defun gnus-agent-read-servers ()
983 "Read the alist of covered servers."
984 (setq gnus-agent-covered-methods
985 (gnus-agent-read-file
986 (nnheader-concat gnus-agent-directory "lib/servers"))
987 gnus-agent-method-p-cache nil)
989 ;; I am called so early in start-up that I can not validate server
990 ;; names. When that is the case, I skip the validation. That is
991 ;; alright as the gnus startup code calls the validate methods
993 (if gnus-server-alist
994 (gnus-agent-read-servers-validate)))
996 (defun gnus-agent-read-servers-validate ()
997 (mapcar (lambda (server-or-method)
998 (let* ((server (if (stringp server-or-method)
1000 (gnus-method-to-server server-or-method)))
1001 (method (gnus-server-to-method server)))
1003 (unless (member server gnus-agent-covered-methods)
1004 (push server gnus-agent-covered-methods)
1005 (setq gnus-agent-method-p-cache nil))
1006 (gnus-message 1 "Ignoring disappeared server `%s'" server))))
1007 (prog1 gnus-agent-covered-methods
1008 (setq gnus-agent-covered-methods nil))))
1010 (defun gnus-agent-read-servers-validate-native (native-method)
1011 (setq gnus-agent-covered-methods
1012 (mapcar (lambda (method)
1013 (if (or (not method)
1014 (equal method native-method))
1016 method)) gnus-agent-covered-methods)))
1018 (defun gnus-agent-write-servers ()
1019 "Write the alist of covered servers."
1020 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1021 (let ((coding-system-for-write nnheader-file-coding-system)
1022 (file-name-coding-system nnmail-pathname-coding-system))
1023 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1024 (prin1 gnus-agent-covered-methods
1025 (current-buffer)))))
1028 ;;; Summary commands
1031 (defun gnus-agent-mark-article (n &optional unmark)
1032 "Mark the next N articles as downloadable.
1033 If N is negative, mark backward instead. If UNMARK is non-nil, remove
1034 the mark instead. The difference between N and the actual number of
1035 articles marked is returned."
1037 (let ((backward (< n 0))
1042 (gnus-summary-set-agent-mark
1043 (gnus-summary-article-number) unmark)
1044 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
1047 (gnus-message 7 "No more articles"))
1048 (gnus-summary-recenter)
1049 (gnus-summary-position-point)
1052 (defun gnus-agent-unmark-article (n)
1053 "Remove the downloadable mark from the next N articles.
1054 If N is negative, unmark backward instead. The difference between N and
1055 the actual number of articles unmarked is returned."
1057 (gnus-agent-mark-article n t))
1059 (defun gnus-agent-toggle-mark (n)
1060 "Toggle the downloadable mark from the next N articles.
1061 If N is negative, toggle backward instead. The difference between N and
1062 the actual number of articles toggled is returned."
1064 (gnus-agent-mark-article n 'toggle))
1066 (defun gnus-summary-set-agent-mark (article &optional unmark)
1067 "Mark ARTICLE as downloadable. If UNMARK is nil, article is marked.
1068 When UNMARK is t, the article is unmarked. For any other value, the
1069 article's mark is toggled."
1070 (let ((unmark (cond ((eq nil unmark)
1075 (memq article gnus-newsgroup-downloadable)))))
1076 (when (gnus-summary-goto-subject article nil t)
1077 (gnus-summary-update-mark
1080 (setq gnus-newsgroup-downloadable
1081 (delq article gnus-newsgroup-downloadable))
1082 (gnus-article-mark article))
1083 (setq gnus-newsgroup-downloadable
1084 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
1085 gnus-downloadable-mark)
1089 (defun gnus-agent-get-undownloaded-list ()
1090 "Construct list of articles that have not been downloaded."
1091 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1092 (when (set (make-local-variable 'gnus-newsgroup-agentized)
1093 (gnus-agent-method-p gnus-command-method))
1094 (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
1095 (headers (sort (mapcar (lambda (h)
1096 (mail-header-number h))
1097 gnus-newsgroup-headers) '<))
1098 (cached (and gnus-use-cache gnus-newsgroup-cached))
1099 (undownloaded (list nil))
1100 (tail-undownloaded undownloaded)
1101 (unfetched (list nil))
1102 (tail-unfetched unfetched))
1103 (while (and alist headers)
1104 (let ((a (caar alist))
1107 ;; Ignore IDs in the alist that are not being
1108 ;; displayed in the summary.
1109 (setq alist (cdr alist)))
1111 ;; Headers that are not in the alist should be
1112 ;; fictious (see nnagent-retrieve-headers); they
1113 ;; imply that this article isn't in the agent.
1114 (gnus-agent-append-to-list tail-undownloaded h)
1115 (gnus-agent-append-to-list tail-unfetched h)
1116 (setq headers (cdr headers)))
1118 (setq alist (cdr alist))
1119 (setq headers (cdr headers))
1120 nil ; ignore already downloaded
1123 (setq alist (cdr alist))
1124 (setq headers (cdr headers))
1126 ;; This article isn't in the agent. Check to see
1127 ;; if it is in the cache. If it is, it's been
1129 (while (and cached (< (car cached) a))
1130 (setq cached (cdr cached)))
1131 (unless (equal a (car cached))
1132 (gnus-agent-append-to-list tail-undownloaded a))))))
1135 (let ((num (pop headers)))
1136 (gnus-agent-append-to-list tail-undownloaded num)
1137 (gnus-agent-append-to-list tail-unfetched num)))
1139 (setq gnus-newsgroup-undownloaded (cdr undownloaded)
1140 gnus-newsgroup-unfetched (cdr unfetched))))))
1142 (defun gnus-agent-catchup ()
1143 "Mark as read all unhandled articles.
1144 An article is unhandled if it is neither cached, nor downloaded, nor
1148 (let ((articles gnus-newsgroup-undownloaded))
1149 (when (or gnus-newsgroup-downloadable
1150 gnus-newsgroup-cached)
1151 (setq articles (gnus-sorted-ndifference
1152 (gnus-sorted-ndifference
1153 (gnus-copy-sequence articles)
1154 gnus-newsgroup-downloadable)
1155 gnus-newsgroup-cached)))
1158 (gnus-summary-mark-article
1159 (pop articles) gnus-catchup-mark)))
1160 (gnus-summary-position-point)))
1162 (defun gnus-agent-summary-fetch-series ()
1164 (when gnus-newsgroup-processable
1165 (setq gnus-newsgroup-downloadable
1166 (let* ((dl gnus-newsgroup-downloadable)
1167 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
1168 (gnus-newsgroup-downloadable processable))
1169 (gnus-agent-summary-fetch-group)
1171 ;; For each article that I processed that is no longer
1172 ;; undownloaded, remove its processable mark.
1174 (mapc #'gnus-summary-remove-process-mark
1175 (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))
1177 ;; The preceeding call to (gnus-agent-summary-fetch-group)
1178 ;; updated the temporary gnus-newsgroup-downloadable to
1179 ;; remove each article successfully fetched. Now, I
1180 ;; update the real gnus-newsgroup-downloadable to only
1181 ;; include undownloaded articles.
1182 (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1184 (defun gnus-agent-summary-fetch-group (&optional all)
1185 "Fetch the downloadable articles in the group.
1186 Optional arg ALL, if non-nil, means to fetch all articles."
1189 (if all gnus-newsgroup-articles
1190 gnus-newsgroup-downloadable))
1191 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
1193 (gnus-agent-while-plugged
1195 (error "No articles to download"))
1196 (gnus-agent-with-fetch
1197 (setq gnus-newsgroup-undownloaded
1198 (gnus-sorted-ndifference
1199 gnus-newsgroup-undownloaded
1200 (setq fetched-articles
1201 (gnus-agent-fetch-articles
1202 gnus-newsgroup-name articles)))))
1204 (dolist (article articles)
1205 (let ((was-marked-downloadable
1206 (memq article gnus-newsgroup-downloadable)))
1207 (cond (gnus-agent-mark-unread-after-downloaded
1208 (setq gnus-newsgroup-downloadable
1209 (delq article gnus-newsgroup-downloadable))
1211 (gnus-summary-mark-article article gnus-unread-mark))
1212 (was-marked-downloadable
1213 (gnus-summary-set-agent-mark article t)))
1214 (when (gnus-summary-goto-subject article nil t)
1215 (gnus-summary-update-download-mark article))))))
1218 (defun gnus-agent-fetch-selected-article ()
1219 "Fetch the current article as it is selected.
1220 This can be added to `gnus-select-article-hook' or
1221 `gnus-mark-article-hook'."
1222 (let ((gnus-command-method gnus-current-select-method))
1223 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
1224 (when (gnus-agent-fetch-articles
1226 (list gnus-current-article))
1227 (setq gnus-newsgroup-undownloaded
1228 (delq gnus-current-article gnus-newsgroup-undownloaded))
1229 (gnus-summary-update-download-mark gnus-current-article)))))
1232 ;;; Internal functions
1235 (defun gnus-agent-synchronize-group-flags (group actions server)
1236 "Update a plugged group by performing the indicated actions."
1237 (let* ((gnus-command-method (gnus-server-to-method server))
1239 ;; This initializer is required as gnus-request-set-mark
1240 ;; calls gnus-group-real-name to strip off the host name
1241 ;; before calling the backend. Now that the backend is
1242 ;; trying to call gnus-request-set-mark, I have to
1243 ;; reconstruct the original group name.
1244 (or (gnus-get-info group)
1246 (setq group (gnus-group-full-name
1247 group gnus-command-method))))))
1248 (gnus-request-set-mark group actions)
1251 (dolist (action actions)
1252 (let ((range (nth 0 action))
1253 (what (nth 1 action))
1254 (marks (nth 2 action)))
1255 (dolist (mark marks)
1256 (cond ((eq mark 'read)
1259 (funcall (if (eq what 'add)
1261 'gnus-remove-from-range)
1262 (gnus-info-read info)
1264 (gnus-get-unread-articles-in-group
1266 (gnus-active (gnus-info-group info))))
1267 ((memq mark '(tick))
1268 (let ((info-marks (assoc mark (gnus-info-marks info))))
1270 (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
1271 (setcdr info-marks (funcall (if (eq what 'add)
1273 'gnus-remove-from-range)
1277 ;;Marks can be synchronized at any time by simply toggling from
1278 ;;unplugged to plugged. If that is what is happening right now, make
1279 ;;sure that the group buffer is up to date.
1280 (when (gnus-buffer-live-p gnus-group-buffer)
1281 (gnus-group-update-group group t)))
1284 (defun gnus-agent-save-active (method)
1285 (when (gnus-agent-method-p method)
1286 (let* ((gnus-command-method method)
1287 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
1288 (file (gnus-agent-lib-file "active")))
1289 (gnus-active-to-gnus-format nil new)
1290 (gnus-agent-write-active file new)
1292 (nnheader-insert-file-contents file))))
1294 (defun gnus-agent-write-active (file new)
1295 (gnus-make-directory (file-name-directory file))
1296 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
1297 ;; The hashtable contains real names of groups. However, do NOT
1298 ;; add the foreign server prefix as gnus-active-to-gnus-format
1299 ;; will add it while reading the file.
1300 (gnus-write-active-file file new nil)))
1303 (defun gnus-agent-possibly-alter-active (group active &optional info)
1304 "Possibly expand a group's active range to include articles
1305 downloaded into the agent."
1306 (let* ((gnus-command-method (or gnus-command-method
1307 (gnus-find-method-for-group group))))
1308 (when (gnus-agent-method-p gnus-command-method)
1309 (let* ((local (gnus-agent-get-local group))
1310 (active-min (or (car active) 0))
1311 (active-max (or (cdr active) 0))
1312 (agent-min (or (car local) active-min))
1313 (agent-max (or (cdr local) active-max)))
1315 (when (< agent-min active-min)
1316 (setcar active agent-min))
1318 (when (> agent-max active-max)
1319 (setcdr active agent-max))
1321 (when (and info (< agent-max (- active-min 100)))
1322 ;; I'm expanding the active range by such a large amount
1323 ;; that there is a gap of more than 100 articles between the
1324 ;; last article known to the agent and the first article
1325 ;; currently available on the server. This gap contains
1326 ;; articles that have been lost, mark them as read so that
1327 ;; gnus doesn't waste resources trying to fetch them.
1329 ;; NOTE: I don't do this for smaller gaps (< 100) as I don't
1330 ;; want to modify the local file everytime someone restarts
1331 ;; gnus. The small gap will cause a tiny performance hit
1332 ;; when gnus tries, and fails, to retrieve the articles.
1333 ;; Still that should be smaller than opening a buffer,
1334 ;; printing this list to the buffer, and then writing it to a
1337 (let ((read (gnus-info-read info)))
1342 (list (cons (1+ agent-max)
1343 (1- active-min))))))
1345 ;; Lie about the agent's local range for this group to
1346 ;; disable the set read each time this server is opened.
1347 ;; NOTE: Opening this group will restore the valid local
1348 ;; range but it will also expand the local range to
1349 ;; incompass the new active range.
1350 (gnus-agent-set-local group agent-min (1- active-min)))))))
1352 (defun gnus-agent-save-group-info (method group active)
1353 "Update a single group's active range in the agent's copy of the server's active file."
1354 (when (gnus-agent-method-p method)
1355 (let* ((gnus-command-method (or method gnus-command-method))
1356 (coding-system-for-write&nbs