9e80ba8acac38b3d213cce56de9f8ec04046f695
[gnus] / lisp / gnus-agent.el
1 ;;; gnus-agent.el --- unplugged support for Gnus
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-cache)
30 (require 'nnmail)
31 (require 'nnvirtual)
32 (require 'gnus-sum)
33 (require 'gnus-score)
34 (require 'gnus-srvr)
35 (require 'gnus-util)
36 (eval-when-compile
37   (if (featurep 'xemacs)
38       (require 'itimer)
39     (require 'timer))
40   (require 'cl))
41
42 (eval-and-compile
43   (autoload 'gnus-server-update-server "gnus-srvr")
44   (autoload 'gnus-agent-customize-category "gnus-cus")
45 )
46
47 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
48   "Where the Gnus agent will store its files."
49   :group 'gnus-agent
50   :type 'directory)
51
52 (defcustom gnus-agent-plugged-hook nil
53   "Hook run when plugging into the network."
54   :group 'gnus-agent
55   :type 'hook)
56
57 (defcustom gnus-agent-unplugged-hook nil
58   "Hook run when unplugging from the network."
59   :group 'gnus-agent
60   :type 'hook)
61
62 (defcustom gnus-agent-fetched-hook nil
63   "Hook run when finished fetching articles."
64   :version "22.1"
65   :group 'gnus-agent
66   :type 'hook)
67
68 (defcustom gnus-agent-handle-level gnus-level-subscribed
69   "Groups on levels higher than this variable will be ignored by the Agent."
70   :group 'gnus-agent
71   :type 'integer)
72
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'."
76   :group 'gnus-agent
77   :type '(number :tag "days"))
78
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."
82   :group 'gnus-agent
83   :type 'boolean)
84
85 (defcustom gnus-agent-group-mode-hook nil
86   "Hook run in Agent group minor modes."
87   :group 'gnus-agent
88   :type 'hook)
89
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))
93
94 (defcustom gnus-agent-summary-mode-hook nil
95   "Hook run in Agent summary minor modes."
96   :group 'gnus-agent
97   :type 'hook)
98
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))
102
103 (defcustom gnus-agent-server-mode-hook nil
104   "Hook run in Agent summary minor modes."
105   :group 'gnus-agent
106   :type 'hook)
107
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))
111
112 (defcustom gnus-agent-confirmation-function 'y-or-n-p
113   "Function to confirm when error happens."
114   :version "21.1"
115   :group 'gnus-agent
116   :type 'function)
117
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.
123   :version "21.1"
124   :type '(choice (const :tag "Always" t)
125                  (const :tag "Never" nil)
126                  (const :tag "Ask" ask))
127   :group 'gnus-agent)
128
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."
132   :version "21.3"
133   :type '(choice (const :tag "Always" t)
134                  (const :tag "Never" nil)
135                  (const :tag "Ask" ask))
136   :group 'gnus-agent)
137
138 (defcustom gnus-agent-mark-unread-after-downloaded t
139   "Indicate whether to mark articles unread after downloaded."
140   :version "21.1"
141   :type 'boolean
142   :group 'gnus-agent)
143
144 (defcustom gnus-agent-download-marks '(download)
145   "Marks for downloading."
146   :version "21.1"
147   :type '(repeat (symbol :tag "Mark"))
148   :group 'gnus-agent)
149
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."
158   :version "22.1"
159   :type 'boolean
160   :group 'gnus-agent)
161
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
165 this limit."
166   :version "22.1"
167   :group 'gnus-agent
168   :type 'integer)
169
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."
177   :version "22.1"
178   :group 'gnus-agent
179   :type '(radio (const :format "Enable " ENABLE)
180                 (const :format "Disable " DISABLE)))
181
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."
187   :version "22.1"
188   :type 'boolean
189   :group 'gnus-agent)
190
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'."
195   :version "22.1"
196   :type '(repeat symbol)
197   :group 'gnus-agent)
198
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."
203   :version "22.1"
204   :group 'gnus-agent
205   :type '(radio (const :format "Always" always)
206                 (const :format "Never" nil)
207                 (const :format "When plugged" t)))
208
209 (defcustom gnus-agent-prompt-send-queue nil
210   "If non-nil, `gnus-group-send-queue' will prompt if called when
211 unplugged."
212   :version "22.1"
213   :group 'gnus-agent
214   :type 'boolean)
215
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
221 the new format."
222   ;; Wouldn't symbols instead numbers be nicer?  --rsteib
223   :version "22.1"
224   :group 'gnus-agent
225   :type '(radio (const :format "Compressed" 2)
226                 (const :format "Uncompressed" 1)))
227
228 ;;; Internal variables
229
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).
238 NOTES:
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)
257
258 ;; Dynamic variables
259 (defvar gnus-headers)
260 (defvar gnus-score)
261
262 ;;;
263 ;;; Setup
264 ;;;
265
266 (defun gnus-open-agent ()
267   (setq gnus-agent t)
268   (gnus-agent-read-servers)
269   (gnus-category-read)
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))
274
275 (defun gnus-agent-create-buffer ()
276   (if (gnus-buffer-live-p gnus-agent-overview-buffer)
277       t
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))
282     nil))
283
284 (gnus-add-shutdown 'gnus-close-agent 'gnus)
285
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))
291
292 ;;;
293 ;;; Utility functions
294 ;;;
295
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))
302         (save-excursion
303           (set-buffer gnus-group-buffer)
304           (setq gnus-agent-need-update-total-fetched-for nil)
305           (gnus-group-update-group ,group t)))))
306
307 (defun gnus-agent-read-file (file)
308   "Load FILE and do a `read' there."
309   (with-temp-buffer
310     (ignore-errors
311       (nnheader-insert-file-contents file)
312       (goto-char (point-min))
313       (read (current-buffer)))))
314
315 (defsubst gnus-agent-method ()
316   (concat (symbol-name (car gnus-command-method)) "/"
317           (if (equal (cadr gnus-command-method) "")
318               "unnamed"
319             (cadr gnus-command-method))))
320
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)) "/"))
325
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)))))
331
332 (defun gnus-agent-cat-set-property (category property value)
333   (if value
334       (setcdr (or (assq property category)
335               (let ((cell (cons property nil)))
336                     (setcdr category (cons cell (cdr category)))
337                     cell)) value)
338     (let ((category category))
339       (while (cond ((eq property (caadr category))
340                     (setcdr category (cddr category))
341                     nil)
342                    (t
343                     (setq category (cdr category)))))))
344   category)
345
346 (eval-when-compile
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)))
357
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)
367                               category
368                               (quote (quote ,prop-name))
369                               value))
370                       (list (quote ,name) --category--temp--) ; access-form
371                       )))))
372   )
373
374 (defmacro gnus-agent-cat-name (category)
375   `(car ,category))
376
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)
397
398
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).
402
403 (eval-and-compile
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--)
408             (list category)
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--))))
414   )
415
416 (defun gnus-agent-set-cat-groups (category groups)
417   (unless (eq groups 'ignore)
418     (let ((new-g groups)
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.
423              nil
424              )
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)))
430                            cell)) new-g))
431             (t
432              (let ((groups groups))
433                (while groups
434                  (let* ((group        (pop groups))
435                         (old-category (gnus-group-category group)))
436                    (if (eq category old-category)
437                        nil
438                      (setf (gnus-agent-cat-groups old-category)
439                            (delete group (gnus-agent-cat-groups
440                                           old-category))))))
441                ;; Purge cache as preceeding loop invalidated it.
442                (setq gnus-category-group-cache nil))
443
444              (setcdr (or (assq 'agent-groups category)
445                          (let ((cell (cons 'agent-groups nil)))
446                            (setcdr category (cons cell (cdr category)))
447                            cell)) groups))))))
448
449 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
450   (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
451
452 ;;; Fetching setup functions.
453
454 (defun gnus-agent-start-fetch ()
455   "Initialize data structures for efficient fetching."
456   (gnus-agent-create-buffer))
457
458 (defun gnus-agent-stop-fetch ()
459   "Save all data structures and clean up."
460   (setq gnus-agent-spam-hashtb nil)
461   (save-excursion
462     (set-buffer nntp-server-buffer)
463     (widen)))
464
465 (defmacro gnus-agent-with-fetch (&rest forms)
466   "Do FORMS safely."
467   `(unwind-protect
468        (let ((gnus-agent-fetching t))
469          (gnus-agent-start-fetch)
470          ,@forms)
471      (gnus-agent-stop-fetch)))
472
473 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
474 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
475
476 (defmacro gnus-agent-append-to-list (tail value)
477   `(setq ,tail (setcdr ,tail (cons ,value nil))))
478
479 (defmacro gnus-agent-message (level &rest args)
480   `(if (<= ,level gnus-verbose)
481        (message ,@args)))
482
483 ;;;
484 ;;; Mode infestation
485 ;;;
486
487 (defvar gnus-agent-mode-hook nil
488   "Hook run when installing agent mode.")
489
490 (defvar gnus-agent-mode nil)
491 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
492
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)
500     (set mode nil)
501     (set (make-local-variable mode) t)
502     ;; Set up the menu.
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"
509                                                      buffer))))
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)))))
521
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)
533
534 (defun gnus-agent-group-make-menu-bar ()
535   (unless (boundp 'gnus-agent-group-menu)
536     (easy-menu-define
537      gnus-agent-group-menu gnus-agent-group-mode-map ""
538      '("Agent"
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]
545        ("Fetch"
546         ["All" gnus-agent-fetch-session gnus-plugged]
547         ["Group" gnus-agent-fetch-group gnus-plugged])
548        ["Synchronize flags" gnus-agent-synchronize-flags t]
549        ))))
550
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)
561
562 (defun gnus-agent-summary-make-menu-bar ()
563   (unless (boundp 'gnus-agent-summary-menu)
564     (easy-menu-define
565      gnus-agent-summary-menu gnus-agent-summary-mode-map ""
566      '("Agent"
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]))))
573
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)
579
580 (defun gnus-agent-server-make-menu-bar ()
581   (unless (boundp 'gnus-agent-server-menu)
582     (easy-menu-define
583      gnus-agent-server-menu gnus-agent-server-mode-map ""
584      '("Agent"
585        ["Toggle plugged" gnus-agent-toggle-plugged t]
586        ["Add" gnus-agent-add-server t]
587        ["Remove" gnus-agent-remove-server t]))))
588
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)
595     string))
596
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)
601          nil)
602         (set-to
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"
607                                                    'mouse-2
608                                                    'gnus-agent-toggle-plugged))
609          (gnus-agent-go-online gnus-agent-go-online)
610          (gnus-agent-possibly-synchronize-flags))
611         (t
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"
617                                                    'mouse-2
618                                                    'gnus-agent-toggle-plugged))))
619   (set-buffer-modified-p t))
620
621 (defmacro gnus-agent-while-plugged (&rest body)
622   `(let ((original-gnus-plugged gnus-plugged))
623     (unwind-protect
624         (progn (gnus-agent-toggle-plugged t)
625                ,@body)
626       (gnus-agent-toggle-plugged original-gnus-plugged))))
627
628 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
629 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
630
631 (defun gnus-agent-close-connections ()
632   "Close all methods covered by the Gnus agent."
633   (let ((methods (gnus-agent-covered-methods)))
634     (while methods
635       (gnus-close-server (pop methods)))))
636
637 ;;;###autoload
638 (defun gnus-unplugged ()
639   "Start Gnus unplugged."
640   (interactive)
641   (setq gnus-plugged nil)
642   (gnus))
643
644 ;;;###autoload
645 (defun gnus-plugged ()
646   "Start Gnus plugged."
647   (interactive)
648   (setq gnus-plugged t)
649   (gnus))
650
651 ;;;###autoload
652 (defun gnus-slave-unplugged (&optional arg)
653   "Read news as a slave unplugged."
654   (interactive "P")
655   (setq gnus-plugged nil)
656   (gnus arg nil 'slave))
657
658 ;;;###autoload
659 (defun gnus-agentize ()
660   "Allow Gnus to be an offline newsreader.
661
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.
665
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."
669   (interactive)
670   (gnus-open-agent)
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))
677
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
680   ;; again.
681   (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
682     (gnus-message 3 "First time agent user, agentizing remote groups...")
683     (mapc
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)))
693
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"))
698                         gnus-newsrc-hashtb)
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"))
702                             nil '(nndraft "")))
703     (gnus-group-set-parameter
704      (format "nndraft:%s" (or group-name "queue"))
705      'gnus-dummy '((gnus-draft-mode)))))
706
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))
712     (re-search-forward
713      (concat "^" (regexp-quote mail-header-separator) "\n"))
714     (replace-match "\n")
715     (gnus-agent-insert-meta-information 'mail)
716     (gnus-request-accept-article "nndraft:queue" nil t t)))
717
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
721 be a select method."
722   (save-excursion
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)
727             "\n")
728     (forward-char -1)
729     (while (search-backward "\n" nil t)
730       (replace-match "\\n" t t))))
731
732 (defun gnus-agent-restore-gcc ()
733   "Restore GCC field from saved header."
734   (save-excursion
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))))
739
740 (defun gnus-agent-any-covered-gcc ()
741   (save-restriction
742     (message-narrow-to-headers)
743     (let* ((gcc (mail-fetch-field "gcc" nil t))
744            (methods (and gcc
745                          (mapcar 'gnus-inews-group-method
746                                  (message-unquote-tokens
747                                   (message-tokenize-header
748                                    gcc " ,")))))
749            covered)
750       (while (and (not covered) methods)
751         (setq covered (gnus-agent-method-p (car methods))
752               methods (cdr methods)))
753       covered)))
754
755 ;;;###autoload
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))
759     (save-excursion
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))))))
764
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)))
769
770 ;;;
771 ;;; Group mode commands
772 ;;;
773
774 (defun gnus-agent-fetch-groups (n)
775   "Put all new articles in the current groups into the Agent."
776   (interactive "P")
777   (unless gnus-plugged
778     (error "Groups can't be fetched when Gnus is unplugged"))
779   (gnus-group-iterate n 'gnus-agent-fetch-group))
780
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))
785   (unless group
786     (error "No group on the current line"))
787
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)))))
793
794 (defun gnus-agent-add-group (category arg)
795   "Add the current group to an agent category."
796   (interactive
797    (list
798     (intern
799      (completing-read
800       "Add to category: "
801       (mapcar (lambda (cat) (list (symbol-name (car cat))))
802               gnus-category-alist)
803       nil t))
804     current-prefix-arg))
805   (let ((cat (assq category gnus-category-alist))
806         c groups)
807     (gnus-group-iterate arg
808       (lambda (group)
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)))
816
817 (defun gnus-agent-remove-group (arg)
818   "Remove the current group from its agent category, if any."
819   (interactive "P")
820   (let (c)
821     (gnus-group-iterate arg
822       (lambda (group)
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)))
827
828 (defun gnus-agent-synchronize-flags ()
829   "Synchronize unplugged flags with servers."
830   (interactive)
831   (save-excursion
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)))))
835
836 (defun gnus-agent-possibly-synchronize-flags ()
837   "Synchronize flags according to `gnus-agent-synchronize-flags'."
838   (interactive)
839   (save-excursion
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)))))
844
845 (defun gnus-agent-synchronize-flags-server (method)
846   "Synchronize flags set when unplugged for server."
847   (let ((gnus-command-method method)
848         (gnus-agent nil))
849     (when (file-exists-p (gnus-agent-lib-file "flags"))
850       (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
851       (erase-buffer)
852       (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
853       (cond ((null gnus-plugged)
854              (gnus-message
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))
858              (gnus-message
859               1 "Couldn't open server %s" (nth 1 gnus-command-method)))
860             (t
861              (condition-case err
862                  (while t
863                    (let ((bgn (point)))
864                      (eval (read (current-buffer)))
865                      (delete-region bgn (point))))
866                (end-of-file
867                 (delete-file (gnus-agent-lib-file "flags")))
868                (error
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)))))))
874       (kill-buffer nil))))
875
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'? "
882                                         (cadr method)))))
883     (gnus-agent-synchronize-flags-server method)))
884
885 ;;;###autoload
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
891 supported."
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)
901
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)
907
908       (let ((old-local (gnus-agent-get-local old-group
909                                              old-real-group old-command-method)))
910         (gnus-agent-set-local old-group
911                               nil nil
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)))))
916
917 ;;;###autoload
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
923 supported."
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)
929
930     (let* ((real-group (gnus-group-real-name group)))
931       (gnus-agent-save-group-info command-method real-group nil)
932
933       (let ((local (gnus-agent-get-local group
934                                          real-group command-method)))
935         (gnus-agent-set-local group
936                               nil nil
937                               real-group command-method)))))
938
939 ;;;
940 ;;; Server mode commands
941 ;;;
942
943 (defun gnus-agent-add-server ()
944   "Enroll SERVER in the agent program."
945   (interactive)
946   (let* ((server       (gnus-server-server-name))
947          (named-server (gnus-server-named-server))
948          (method       (and server
949                             (gnus-server-get-method nil server))))
950     (unless server
951       (error "No server on the current line"))
952
953     (when (gnus-agent-method-p method)
954       (error "Server already in the agent program"))
955
956     (push named-server gnus-agent-covered-methods)
957
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)))
962
963 (defun gnus-agent-remove-server ()
964   "Remove SERVER from the agent program."
965   (interactive)
966   (let* ((server       (gnus-server-server-name))
967          (named-server (gnus-server-named-server)))
968     (unless server
969       (error "No server on the current line"))
970
971     (unless (member named-server gnus-agent-covered-methods)
972       (error "Server not in the agent program"))
973
974     (setq gnus-agent-covered-methods
975           (delete named-server gnus-agent-covered-methods)
976           gnus-agent-method-p-cache nil)
977
978     (gnus-server-update-server server)
979     (gnus-agent-write-servers)
980     (gnus-message 1 "Removed %s from the agent" server)))
981
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)
988
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
992   ;; directly.
993   (if gnus-server-alist
994       (gnus-agent-read-servers-validate)))
995
996 (defun gnus-agent-read-servers-validate ()
997   (mapcar (lambda (server-or-method)
998             (let* ((server (if (stringp server-or-method)
999                                server-or-method
1000                              (gnus-method-to-server server-or-method)))
1001                    (method (gnus-server-to-method server)))
1002               (if method
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))))