1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997-2011 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 3 of the License, or
11 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
34 (if (featurep 'xemacs)
39 (autoload 'gnus-server-update-server "gnus-srvr")
40 (autoload 'gnus-agent-customize-category "gnus-cus")
42 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
43 "Where the Gnus agent will store its files."
47 (defcustom gnus-agent-plugged-hook nil
48 "Hook run when plugging into the network."
52 (defcustom gnus-agent-unplugged-hook nil
53 "Hook run when unplugging from the network."
57 (defcustom gnus-agent-fetched-hook nil
58 "Hook run when finished fetching articles."
63 (defcustom gnus-agent-handle-level gnus-level-subscribed
64 "Groups on levels higher than this variable will be ignored by the Agent."
68 (defcustom gnus-agent-expire-days 7
69 "Read articles older than this will be expired.
70 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
72 :type '(number :tag "days"))
74 (defcustom gnus-agent-expire-all nil
75 "If non-nil, also expire unread, ticked and dormant articles.
76 If nil, only read articles will be expired."
80 (defcustom gnus-agent-group-mode-hook nil
81 "Hook run in Agent group minor modes."
85 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
86 (when (featurep 'xemacs)
87 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
89 (defcustom gnus-agent-summary-mode-hook nil
90 "Hook run in Agent summary minor modes."
94 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
95 (when (featurep 'xemacs)
96 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
98 (defcustom gnus-agent-server-mode-hook nil
99 "Hook run in Agent summary minor modes."
103 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
104 (when (featurep 'xemacs)
105 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
107 (defcustom gnus-agent-confirmation-function 'y-or-n-p
108 "Function to confirm when error happens."
113 (defcustom gnus-agent-synchronize-flags nil
114 "Indicate if flags are synchronized when you plug in.
115 If this is `ask' the hook will query the user."
116 ;; If the default switches to something else than nil, then the function
117 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
119 :type '(choice (const :tag "Always" t)
120 (const :tag "Never" nil)
121 (const :tag "Ask" ask))
124 (defcustom gnus-agent-go-online 'ask
125 "Indicate if offline servers go online when you plug in.
126 If this is `ask' the hook will query the user."
128 :type '(choice (const :tag "Always" t)
129 (const :tag "Never" nil)
130 (const :tag "Ask" ask))
133 (defcustom gnus-agent-mark-unread-after-downloaded t
134 "Indicate whether to mark articles unread after downloaded."
139 (defcustom gnus-agent-download-marks '(download)
140 "Marks for downloading."
142 :type '(repeat (symbol :tag "Mark"))
145 (defcustom gnus-agent-consider-all-articles nil
146 "When non-nil, the agent will let the agent predicate decide
147 whether articles need to be downloaded or not, for all articles. When
148 nil, the default, the agent will only let the predicate decide
149 whether unread articles are downloaded or not. If you enable this,
150 groups with large active ranges may open slower and you may also want
151 to look into the agent expiry settings to block the expiration of
152 read articles as they would just be downloaded again."
157 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
158 "Chunk size for `gnus-agent-fetch-session'.
159 The function will split its article fetches into chunks smaller than
165 (defcustom gnus-agent-enable-expiration 'ENABLE
166 "The default expiration state for each group.
167 When set to ENABLE, the default, `gnus-agent-expire' will expire old
168 contents from a group's local storage. This value may be overridden
169 to disable expiration in specific categories, topics, and groups. Of
170 course, you could change gnus-agent-enable-expiration to DISABLE then
171 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 nil
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."
200 :type '(radio (const :format "Always" always)
201 (const :format "Never" nil)
202 (const :format "When unplugged" t)))
204 (defcustom gnus-agent-prompt-send-queue nil
205 "If non-nil, `gnus-group-send-queue' will prompt if called when unplugged."
210 (defcustom gnus-agent-article-alist-save-format 1
211 "Indicates whether to use compression(2), versus no
212 compression(1), when writing agentview files. The compressed
213 files do save space but load times are 6-7 times higher. A group
214 must be opened then closed for the agentview to be updated using
216 ;; Wouldn't symbols instead numbers be nicer? --rsteib
219 :type '(radio (const :format "Compressed" 2)
220 (const :format "Uncompressed" 1)))
222 ;;; Internal variables
224 (defvar gnus-agent-history-buffers nil)
225 (defvar gnus-agent-buffer-alist nil)
226 (defvar gnus-agent-article-alist nil
227 "An assoc list identifying the articles whose headers have been fetched.
228 If successfully fetched, these headers will be stored in the group's overview
229 file. The key of each assoc pair is the article ID, the value of each assoc
230 pair is a flag indicating whether the identified article has been downloaded
231 \(gnus-agent-fetch-articles sets the value to the day of the download).
233 1) The last element of this list can not be expired as some
234 routines (for example, get-agent-fetch-headers) use the last
235 value to track which articles have had their headers retrieved.
236 2) The function `gnus-agent-regenerate' may destructively modify the value.")
237 (defvar gnus-agent-group-alist nil)
238 (defvar gnus-category-alist nil)
239 (defvar gnus-agent-current-history nil)
240 (defvar gnus-agent-overview-buffer nil)
241 (defvar gnus-category-predicate-cache nil)
242 (defvar gnus-category-group-cache nil)
243 (defvar gnus-agent-spam-hashtb nil)
244 (defvar gnus-agent-file-name nil)
245 (defvar gnus-agent-send-mail-function nil)
246 (defvar gnus-agent-file-coding-system 'raw-text)
247 (defvar gnus-agent-file-loading-cache nil)
248 (defvar gnus-agent-total-fetched-hashtb nil)
249 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
250 (defvar gnus-agent-need-update-total-fetched-for nil)
253 (defvar gnus-headers)
256 ;; Added to support XEmacs
258 (unless (fboundp 'directory-files-and-attributes)
259 (defun directory-files-and-attributes (directory
260 &optional full match nosort)
262 (dolist (file (directory-files directory full match nosort))
263 (push (cons file (file-attributes file)) result))
264 (nreverse result)))))
270 (defun gnus-open-agent ()
272 (gnus-agent-read-servers)
274 (gnus-agent-create-buffer)
275 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
276 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
277 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
279 (defun gnus-agent-create-buffer ()
280 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
282 (setq gnus-agent-overview-buffer
283 (gnus-get-buffer-create " *Gnus agent overview*"))
284 (with-current-buffer gnus-agent-overview-buffer
285 (mm-enable-multibyte))
288 (gnus-add-shutdown 'gnus-close-agent 'gnus)
290 (defun gnus-close-agent ()
291 (setq gnus-category-predicate-cache nil
292 gnus-category-group-cache nil
293 gnus-agent-spam-hashtb nil)
294 (gnus-kill-buffer gnus-agent-overview-buffer))
297 ;;; Utility functions
300 (defmacro gnus-agent-with-refreshed-group (group &rest body)
301 "Performs the body then updates the group's line in the group
302 buffer. Automatically blocks multiple updates due to recursion."
303 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
304 (when (and gnus-agent-need-update-total-fetched-for
305 (not gnus-agent-inhibit-update-total-fetched-for))
306 (with-current-buffer gnus-group-buffer
307 (setq gnus-agent-need-update-total-fetched-for nil)
308 (gnus-group-update-group ,group t)))))
310 (defun gnus-agent-read-file (file)
311 "Load FILE and do a `read' there."
314 (nnheader-insert-file-contents file)
315 (goto-char (point-min))
316 (read (current-buffer)))))
318 (defsubst gnus-agent-method ()
319 (concat (symbol-name (car gnus-command-method)) "/"
320 (if (equal (cadr gnus-command-method) "")
322 (cadr gnus-command-method))))
324 (defsubst gnus-agent-directory ()
325 "The name of the Gnus agent directory."
326 (nnheader-concat gnus-agent-directory
327 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
329 (defun gnus-agent-lib-file (file)
330 "The full name of the Gnus agent library FILE."
331 (expand-file-name file
332 (file-name-as-directory
333 (expand-file-name "agent.lib" (gnus-agent-directory)))))
335 (defun gnus-agent-cat-set-property (category property value)
337 (setcdr (or (assq property category)
338 (let ((cell (cons property nil)))
339 (setcdr category (cons cell (cdr category)))
341 (let ((category category))
342 (while (cond ((eq property (caadr category))
343 (setcdr category (cddr category))
346 (setq category (cdr category)))))))
350 (defmacro gnus-agent-cat-defaccessor (name prop-name)
351 "Define accessor and setter methods for manipulating a list of the form
352 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
353 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
354 manipulated as follows:
355 (func LIST): Returns VALUE1
356 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
357 `(progn (defmacro ,name (category)
358 (list (quote cdr) (list (quote assq)
359 (quote (quote ,prop-name)) category)))
361 (define-setf-method ,name (category)
362 (let* ((--category--temp-- (make-symbol "--category--"))
363 (--value--temp-- (make-symbol "--value--")))
364 (list (list --category--temp--) ; temporary-variables
365 (list category) ; value-forms
366 (list --value--temp--) ; store-variables
367 (let* ((category --category--temp--) ; store-form
368 (value --value--temp--))
369 (list (quote gnus-agent-cat-set-property)
371 (quote (quote ,prop-name))
373 (list (quote ,name) --category--temp--) ; access-form
377 (defmacro gnus-agent-cat-name (category)
380 (gnus-agent-cat-defaccessor
381 gnus-agent-cat-days-until-old agent-days-until-old)
382 (gnus-agent-cat-defaccessor
383 gnus-agent-cat-enable-expiration agent-enable-expiration)
384 (gnus-agent-cat-defaccessor
385 gnus-agent-cat-groups agent-groups)
386 (gnus-agent-cat-defaccessor
387 gnus-agent-cat-high-score agent-high-score)
388 (gnus-agent-cat-defaccessor
389 gnus-agent-cat-length-when-long agent-long-article)
390 (gnus-agent-cat-defaccessor
391 gnus-agent-cat-length-when-short agent-short-article)
392 (gnus-agent-cat-defaccessor
393 gnus-agent-cat-low-score agent-low-score)
394 (gnus-agent-cat-defaccessor
395 gnus-agent-cat-predicate agent-predicate)
396 (gnus-agent-cat-defaccessor
397 gnus-agent-cat-score-file agent-score)
398 (gnus-agent-cat-defaccessor
399 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
402 ;; This form is equivalent to defsetf except that it calls make-symbol
403 ;; whereas defsetf calls gensym (Using gensym creates a run-time
404 ;; dependency on the CL library).
407 (define-setf-method gnus-agent-cat-groups (category)
408 (let* ((--category--temp-- (make-symbol "--category--"))
409 (--groups--temp-- (make-symbol "--groups--")))
410 (list (list --category--temp--)
412 (list --groups--temp--)
413 (let* ((category --category--temp--)
414 (groups --groups--temp--))
415 (list (quote gnus-agent-set-cat-groups) category groups))
416 (list (quote gnus-agent-cat-groups) --category--temp--))))
419 (defun gnus-agent-set-cat-groups (category groups)
420 (unless (eq groups 'ignore)
422 (old-g (gnus-agent-cat-groups category)))
423 (cond ((eq new-g old-g)
424 ;; gnus-agent-add-group is fiddling with the group
425 ;; list. Still, Im done.
428 ((eq new-g (cdr old-g))
429 ;; gnus-agent-add-group is fiddling with the group list
430 (setcdr (or (assq 'agent-groups category)
431 (let ((cell (cons 'agent-groups nil)))
432 (setcdr category (cons cell (cdr category)))
435 (let ((groups groups))
437 (let* ((group (pop groups))
438 (old-category (gnus-group-category group)))
439 (if (eq category old-category)
441 (setf (gnus-agent-cat-groups old-category)
442 (delete group (gnus-agent-cat-groups
444 ;; Purge cache as preceding loop invalidated it.
445 (setq gnus-category-group-cache nil))
447 (setcdr (or (assq 'agent-groups category)
448 (let ((cell (cons 'agent-groups nil)))
449 (setcdr category (cons cell (cdr category)))
452 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
453 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
455 (defun gnus-agent-read-group ()
456 "Read a group name in the minibuffer, with completion."
457 (let ((def (or (gnus-group-group-name) gnus-newsgroup-name)))
459 (setq def (gnus-group-decoded-name def)))
460 (gnus-group-completing-read nil nil t nil nil def)))
462 ;;; Fetching setup functions.
464 (defun gnus-agent-start-fetch ()
465 "Initialize data structures for efficient fetching."
466 (gnus-agent-create-buffer))
468 (defun gnus-agent-stop-fetch ()
469 "Save all data structures and clean up."
470 (setq gnus-agent-spam-hashtb nil)
471 (with-current-buffer nntp-server-buffer
474 (defmacro gnus-agent-with-fetch (&rest forms)
477 (let ((gnus-agent-fetching t))
478 (gnus-agent-start-fetch)
480 (gnus-agent-stop-fetch)))
482 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
483 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
485 (defmacro gnus-agent-append-to-list (tail value)
486 `(setq ,tail (setcdr ,tail (cons ,value nil))))
488 (defmacro gnus-agent-message (level &rest args)
489 `(if (<= ,level gnus-verbose)
496 (defvar gnus-agent-mode-hook nil
497 "Hook run when installing agent mode.")
499 (defvar gnus-agent-mode nil)
500 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
502 (defun gnus-agent-mode ()
503 "Minor mode for providing a agent support in Gnus buffers."
504 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
505 (symbol-name major-mode))
506 (match-string 1 (symbol-name major-mode))))
507 (mode (intern (format "gnus-agent-%s-mode" buffer))))
508 (set (make-local-variable 'gnus-agent-mode) t)
510 (set (make-local-variable mode) t)
512 (when (gnus-visual-p 'agent-menu 'menu)
513 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
514 (unless (assq mode minor-mode-alist)
515 (push (cons mode (cdr gnus-agent-mode-status)) minor-mode-alist))
516 (unless (assq mode minor-mode-map-alist)
517 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
519 minor-mode-map-alist))
520 (when (eq major-mode 'gnus-group-mode)
521 (let ((init-plugged gnus-plugged)
522 (gnus-agent-go-online nil))
523 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
524 ;; Therefore, make certain that the current value does not
525 ;; match the desired initial value.
526 (setq gnus-plugged :unknown)
527 (gnus-agent-toggle-plugged init-plugged)))
528 (gnus-run-hooks 'gnus-agent-mode-hook
529 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
531 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
532 (gnus-define-keys gnus-agent-group-mode-map
533 "Ju" gnus-agent-fetch-groups
534 "Jc" gnus-enter-category-buffer
535 "Jj" gnus-agent-toggle-plugged
536 "Js" gnus-agent-fetch-session
537 "JY" gnus-agent-synchronize-flags
538 "JS" gnus-group-send-queue
539 "Ja" gnus-agent-add-group
540 "Jr" gnus-agent-remove-group
541 "Jo" gnus-agent-toggle-group-plugged)
543 (defun gnus-agent-group-make-menu-bar ()
544 (unless (boundp 'gnus-agent-group-menu)
546 gnus-agent-group-menu gnus-agent-group-mode-map ""
548 ["Toggle plugged" gnus-agent-toggle-plugged t]
549 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
550 ["List categories" gnus-enter-category-buffer t]
551 ["Add (current) group to category" gnus-agent-add-group t]
552 ["Remove (current) group from category" gnus-agent-remove-group t]
553 ["Send queue" gnus-group-send-queue gnus-plugged]
555 ["All" gnus-agent-fetch-session gnus-plugged]
556 ["Group" gnus-agent-fetch-group gnus-plugged])
557 ["Synchronize flags" gnus-agent-synchronize-flags t]
560 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
561 (gnus-define-keys gnus-agent-summary-mode-map
562 "Jj" gnus-agent-toggle-plugged
563 "Ju" gnus-agent-summary-fetch-group
564 "JS" gnus-agent-fetch-group
565 "Js" gnus-agent-summary-fetch-series
566 "J#" gnus-agent-mark-article
567 "J\M-#" gnus-agent-unmark-article
568 "@" gnus-agent-toggle-mark
569 "Jc" gnus-agent-catchup)
571 (defun gnus-agent-summary-make-menu-bar ()
572 (unless (boundp 'gnus-agent-summary-menu)
574 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
576 ["Toggle plugged" gnus-agent-toggle-plugged t]
577 ["Mark as downloadable" gnus-agent-mark-article t]
578 ["Unmark as downloadable" gnus-agent-unmark-article t]
579 ["Toggle mark" gnus-agent-toggle-mark t]
580 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
581 ["Catchup undownloaded" gnus-agent-catchup t]))))
583 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
584 (gnus-define-keys gnus-agent-server-mode-map
585 "Jj" gnus-agent-toggle-plugged
586 "Ja" gnus-agent-add-server
587 "Jr" gnus-agent-remove-server)
589 (defun gnus-agent-server-make-menu-bar ()
590 (unless (boundp 'gnus-agent-server-menu)
592 gnus-agent-server-menu gnus-agent-server-mode-map ""
594 ["Toggle plugged" gnus-agent-toggle-plugged t]
595 ["Add" gnus-agent-add-server t]
596 ["Remove" gnus-agent-remove-server t]))))
598 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
599 (if (and (fboundp 'propertize)
600 (fboundp 'make-mode-line-mouse-map))
601 (propertize string 'local-map
602 (make-mode-line-mouse-map mouse-button mouse-func)
604 (if (and (featurep 'xemacs)
605 ;; XEmacs' `facep' only checks for a face
606 ;; object, not for a face name, so it's useless
607 ;; to check with `facep'.
608 (find-face 'modeline))
610 'mode-line-highlight))
613 (defun gnus-agent-toggle-plugged (set-to)
614 "Toggle whether Gnus is unplugged or not."
615 (interactive (list (not gnus-plugged)))
616 (cond ((eq set-to gnus-plugged)
619 (setq gnus-plugged set-to)
620 (gnus-run-hooks 'gnus-agent-plugged-hook)
621 (setcar (cdr gnus-agent-mode-status)
622 (gnus-agent-make-mode-line-string " Plugged"
624 'gnus-agent-toggle-plugged))
625 (gnus-agent-go-online gnus-agent-go-online))
627 (gnus-agent-close-connections)
628 (setq gnus-plugged set-to)
629 (gnus-run-hooks 'gnus-agent-unplugged-hook)
630 (setcar (cdr gnus-agent-mode-status)
631 (gnus-agent-make-mode-line-string " Unplugged"
633 'gnus-agent-toggle-plugged))))
634 (set-buffer-modified-p t))
636 (defmacro gnus-agent-while-plugged (&rest body)
637 `(let ((original-gnus-plugged gnus-plugged))
639 (progn (gnus-agent-toggle-plugged t)
641 (gnus-agent-toggle-plugged original-gnus-plugged))))
643 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
644 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
646 (defun gnus-agent-close-connections ()
647 "Close all methods covered by the Gnus agent."
648 (let ((methods (gnus-agent-covered-methods)))
650 (gnus-close-server (pop methods)))))
653 (defun gnus-unplugged ()
654 "Start Gnus unplugged."
656 (setq gnus-plugged nil)
660 (defun gnus-plugged ()
661 "Start Gnus plugged."
663 (setq gnus-plugged t)
667 (defun gnus-slave-unplugged (&optional arg)
668 "Read news as a slave unplugged."
670 (setq gnus-plugged nil)
671 (gnus arg nil 'slave))
674 (defun gnus-agentize ()
675 "Allow Gnus to be an offline newsreader.
677 The gnus-agentize function is now called internally by gnus when
678 gnus-agent is set. If you wish to avoid calling gnus-agentize,
679 customize gnus-agent to nil.
681 This will modify the `gnus-setup-news-hook', and
682 `message-send-mail-real-function' variables, and install the Gnus agent
683 minor mode in all Gnus buffers."
686 (unless gnus-agent-send-mail-function
687 (setq gnus-agent-send-mail-function
688 (or message-send-mail-real-function
689 (function (lambda () (funcall message-send-mail-function))))
690 message-send-mail-real-function 'gnus-agent-send-mail))
692 ;; If the servers file doesn't exist, auto-agentize some servers and
693 ;; save the servers file so this auto-agentizing isn't invoked
695 (when (and (not (file-exists-p (nnheader-concat
696 gnus-agent-directory "lib/servers")))
697 gnus-agent-auto-agentize-methods)
698 (gnus-message 3 "First time agent user, agentizing remote groups...")
700 (lambda (server-or-method)
701 (let ((method (gnus-server-to-method server-or-method)))
702 (when (memq (car method)
703 gnus-agent-auto-agentize-methods)
704 (push (gnus-method-to-server method)
705 gnus-agent-covered-methods)
706 (setq gnus-agent-method-p-cache nil))))
707 (cons gnus-select-method gnus-secondary-select-methods))
708 (gnus-agent-write-servers)))
710 (defun gnus-agent-queue-setup (&optional group-name)
711 "Make sure the queue group exists.
712 Optional arg GROUP-NAME allows to specify another group."
713 (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
715 (gnus-request-create-group (or group-name "queue") '(nndraft ""))
716 (let ((gnus-level-default-subscribed 1))
717 (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
719 (gnus-group-set-parameter
720 (format "nndraft:%s" (or group-name "queue"))
721 'gnus-dummy '((gnus-draft-mode)))))
723 (defun gnus-agent-send-mail ()
724 (if (or (not gnus-agent-queue-mail)
725 (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
726 (funcall gnus-agent-send-mail-function)
727 (goto-char (point-min))
729 (concat "^" (regexp-quote mail-header-separator) "\n"))
731 (gnus-agent-insert-meta-information 'mail)
732 (gnus-request-accept-article "nndraft:queue" nil t t)
733 (gnus-group-refresh-group "nndraft:queue")))
735 (defun gnus-agent-insert-meta-information (type &optional method)
736 "Insert meta-information into the message that says how it's to be posted.
737 TYPE can be either `mail' or `news'. If the latter, then METHOD can
740 (message-remove-header gnus-agent-meta-information-header)
741 (goto-char (point-min))
742 (insert gnus-agent-meta-information-header ": "
743 (symbol-name type) " " (format "%S" method)
746 (while (search-backward "\n" nil t)
747 (replace-match "\\n" t t))))
749 (defun gnus-agent-restore-gcc ()
750 "Restore GCC field from saved header."
752 (goto-char (point-min))
753 (while (re-search-forward
754 (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
755 (replace-match "Gcc:" 'fixedcase))))
757 (defun gnus-agent-any-covered-gcc ()
759 (message-narrow-to-headers)
760 (let* ((gcc (mail-fetch-field "gcc" nil t))
762 (mapcar 'gnus-inews-group-method
763 (message-unquote-tokens
764 (message-tokenize-header
767 (while (and (not covered) methods)
768 (setq covered (gnus-agent-method-p (car methods))
769 methods (cdr methods)))
773 (defun gnus-agent-possibly-save-gcc ()
774 "Save GCC if Gnus is unplugged."
775 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
777 (goto-char (point-min))
778 (let ((case-fold-search t))
779 (while (re-search-forward "^gcc:" nil t)
780 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
782 (defun gnus-agent-possibly-do-gcc ()
783 "Do GCC if Gnus is plugged."
784 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
785 (gnus-inews-do-gcc)))
788 ;;; Group mode commands
791 (defun gnus-agent-fetch-groups (n)
792 "Put all new articles in the current groups into the Agent."
795 (error "Groups can't be fetched when Gnus is unplugged"))
796 (gnus-group-iterate n 'gnus-agent-fetch-group))
798 (defun gnus-agent-fetch-group (&optional group)
799 "Put all new articles in GROUP into the Agent."
800 (interactive (list (gnus-group-group-name)))
801 (setq group (or group gnus-newsgroup-name))
803 (error "No group on the current line"))
804 (if (not (gnus-agent-group-covered-p group))
805 (message "%s isn't covered by the agent" group)
806 (gnus-agent-while-plugged
807 (let ((gnus-command-method (gnus-find-method-for-group group)))
808 (gnus-agent-with-fetch
809 (gnus-agent-fetch-group-1 group gnus-command-method)
810 (gnus-message 5 "Fetching %s...done" group))))))
812 (defun gnus-agent-add-group (category arg)
813 "Add the current group to an agent category."
817 (gnus-completing-read
819 (mapcar (lambda (cat) (symbol-name (car cat)))
823 (let ((cat (assq category gnus-category-alist))
825 (gnus-group-iterate arg
827 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
828 (setf (gnus-agent-cat-groups c)
829 (delete group (gnus-agent-cat-groups c))))
830 (push group groups)))
831 (setf (gnus-agent-cat-groups cat)
832 (nconc (gnus-agent-cat-groups cat) groups))
833 (gnus-category-write)))
835 (defun gnus-agent-remove-group (arg)
836 "Remove the current group from its agent category, if any."
839 (gnus-group-iterate arg
841 (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
842 (setf (gnus-agent-cat-groups c)
843 (delete group (gnus-agent-cat-groups c))))))
844 (gnus-category-write)))
846 (defun gnus-agent-synchronize-flags ()
847 "Synchronize unplugged flags with servers."
850 (dolist (gnus-command-method (gnus-agent-covered-methods))
851 (when (file-exists-p (gnus-agent-lib-file "flags"))
852 (gnus-agent-synchronize-flags-server gnus-command-method)))))
854 (defun gnus-agent-possibly-synchronize-flags ()
855 "Synchronize flags according to `gnus-agent-synchronize-flags'."
858 (dolist (gnus-command-method (gnus-agent-covered-methods))
859 (when (eq (gnus-server-status gnus-command-method) 'ok)
860 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
862 (defun gnus-agent-synchronize-flags-server (method)
863 "Synchronize flags set when unplugged for server."
864 (let ((gnus-command-method method)
866 (when (file-exists-p (gnus-agent-lib-file "flags"))
867 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
869 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
870 (cond ((null gnus-plugged)
872 1 "You must be plugged to synchronize flags with server %s"
873 (nth 1 gnus-command-method)))
874 ((null (gnus-check-server gnus-command-method))
876 1 "Couldn't open server %s" (nth 1 gnus-command-method)))
881 (eval (read (current-buffer)))
882 (delete-region bgn (point))))
884 (delete-file (gnus-agent-lib-file "flags")))
886 (let ((file (gnus-agent-lib-file "flags")))
887 (write-region (point-min) (point-max)
888 (gnus-agent-lib-file "flags") nil 'silent)
889 (error "Couldn't set flags from file %s due to %s"
890 file (error-message-string err)))))))
893 (defun gnus-agent-possibly-synchronize-flags-server (method)
894 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
895 (when (and (file-exists-p (gnus-agent-lib-file "flags"))
896 (or (and gnus-agent-synchronize-flags
897 (not (eq gnus-agent-synchronize-flags 'ask)))
898 (and (eq gnus-agent-synchronize-flags 'ask)
900 (format "Synchronize flags on server `%s'? "
902 (gnus-agent-synchronize-flags-server method)))
905 (defun gnus-agent-rename-group (old-group new-group)
906 "Rename fully-qualified OLD-GROUP as NEW-GROUP.
907 Always updates the agent, even when disabled, as the old agent
908 files would corrupt gnus when the agent was next enabled.
909 Depends upon the caller to determine whether group renaming is
911 (let* ((old-command-method (gnus-find-method-for-group old-group))
912 (old-path (directory-file-name
913 (let (gnus-command-method old-command-method)
914 (gnus-agent-group-pathname old-group))))
915 (new-command-method (gnus-find-method-for-group new-group))
916 (new-path (directory-file-name