Merge branch 'master' of https://git.gnus.org/gnus
[gnus] / lisp / gnus-agent.el
1 ;;; gnus-agent.el --- unplugged support for Gnus
2
3 ;; Copyright (C) 1997-2011  Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
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.
12
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.
17
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/>.
20
21 ;;; Commentary:
22
23 ;;; Code:
24
25 (require 'gnus)
26 (require 'gnus-cache)
27 (require 'nnmail)
28 (require 'nnvirtual)
29 (require 'gnus-sum)
30 (require 'gnus-score)
31 (require 'gnus-srvr)
32 (require 'gnus-util)
33 (eval-when-compile
34   (if (featurep 'xemacs)
35       (require 'itimer)
36     (require 'timer))
37   (require 'cl))
38
39 (autoload 'gnus-server-update-server "gnus-srvr")
40 (autoload 'gnus-agent-customize-category "gnus-cus")
41
42 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
43   "Where the Gnus agent will store its files."
44   :group 'gnus-agent
45   :type 'directory)
46
47 (defcustom gnus-agent-plugged-hook nil
48   "Hook run when plugging into the network."
49   :group 'gnus-agent
50   :type 'hook)
51
52 (defcustom gnus-agent-unplugged-hook nil
53   "Hook run when unplugging from the network."
54   :group 'gnus-agent
55   :type 'hook)
56
57 (defcustom gnus-agent-fetched-hook nil
58   "Hook run when finished fetching articles."
59   :version "22.1"
60   :group 'gnus-agent
61   :type 'hook)
62
63 (defcustom gnus-agent-handle-level gnus-level-subscribed
64   "Groups on levels higher than this variable will be ignored by the Agent."
65   :group 'gnus-agent
66   :type 'integer)
67
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'."
71   :group 'gnus-agent
72   :type '(number :tag "days"))
73
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."
77   :group 'gnus-agent
78   :type 'boolean)
79
80 (defcustom gnus-agent-group-mode-hook nil
81   "Hook run in Agent group minor modes."
82   :group 'gnus-agent
83   :type 'hook)
84
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))
88
89 (defcustom gnus-agent-summary-mode-hook nil
90   "Hook run in Agent summary minor modes."
91   :group 'gnus-agent
92   :type 'hook)
93
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))
97
98 (defcustom gnus-agent-server-mode-hook nil
99   "Hook run in Agent summary minor modes."
100   :group 'gnus-agent
101   :type 'hook)
102
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))
106
107 (defcustom gnus-agent-confirmation-function 'y-or-n-p
108   "Function to confirm when error happens."
109   :version "21.1"
110   :group 'gnus-agent
111   :type 'function)
112
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.
118   :version "21.1"
119   :type '(choice (const :tag "Always" t)
120                  (const :tag "Never" nil)
121                  (const :tag "Ask" ask))
122   :group 'gnus-agent)
123
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."
127   :version "21.3"
128   :type '(choice (const :tag "Always" t)
129                  (const :tag "Never" nil)
130                  (const :tag "Ask" ask))
131   :group 'gnus-agent)
132
133 (defcustom gnus-agent-mark-unread-after-downloaded t
134   "Indicate whether to mark articles unread after downloaded."
135   :version "21.1"
136   :type 'boolean
137   :group 'gnus-agent)
138
139 (defcustom gnus-agent-download-marks '(download)
140   "Marks for downloading."
141   :version "21.1"
142   :type '(repeat (symbol :tag "Mark"))
143   :group 'gnus-agent)
144
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."
153   :version "22.1"
154   :type 'boolean
155   :group 'gnus-agent)
156
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
160 this limit."
161   :version "22.1"
162   :group 'gnus-agent
163   :type 'integer)
164
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."
172   :version "22.1"
173   :group 'gnus-agent
174   :type '(radio (const :format "Enable " ENABLE)
175                 (const :format "Disable " DISABLE)))
176
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."
182   :version "22.1"
183   :type 'boolean
184   :group 'gnus-agent)
185
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'."
190   :version "22.1"
191   :type '(repeat symbol)
192   :group 'gnus-agent)
193
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."
198   :version "22.1"
199   :group 'gnus-agent
200   :type '(radio (const :format "Always" always)
201                 (const :format "Never" nil)
202                 (const :format "When unplugged" t)))
203
204 (defcustom gnus-agent-prompt-send-queue nil
205   "If non-nil, `gnus-group-send-queue' will prompt if called when unplugged."
206   :version "22.1"
207   :group 'gnus-agent
208   :type 'boolean)
209
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
215 the new format."
216   ;; Wouldn't symbols instead numbers be nicer?  --rsteib
217   :version "22.1"
218   :group 'gnus-agent
219   :type '(radio (const :format "Compressed" 2)
220                 (const :format "Uncompressed" 1)))
221
222 ;;; Internal variables
223
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).
232 NOTES:
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)
251
252 ;; Dynamic variables
253 (defvar gnus-headers)
254 (defvar gnus-score)
255
256 ;; Added to support XEmacs
257 (eval-and-compile
258   (unless (fboundp 'directory-files-and-attributes)
259     (defun directory-files-and-attributes (directory
260                                            &optional full match nosort)
261       (let (result)
262         (dolist (file (directory-files directory full match nosort))
263           (push (cons file (file-attributes file)) result))
264         (nreverse result)))))
265
266 ;;;
267 ;;; Setup
268 ;;;
269
270 (defun gnus-open-agent ()
271   (setq gnus-agent t)
272   (gnus-agent-read-servers)
273   (gnus-category-read)
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))
278
279 (defun gnus-agent-create-buffer ()
280   (if (gnus-buffer-live-p gnus-agent-overview-buffer)
281       t
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))
286     nil))
287
288 (gnus-add-shutdown 'gnus-close-agent 'gnus)
289
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))
295
296 ;;;
297 ;;; Utility functions
298 ;;;
299
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)))))
309
310 (defun gnus-agent-read-file (file)
311   "Load FILE and do a `read' there."
312   (with-temp-buffer
313     (ignore-errors
314       (nnheader-insert-file-contents file)
315       (goto-char (point-min))
316       (read (current-buffer)))))
317
318 (defsubst gnus-agent-method ()
319   (concat (symbol-name (car gnus-command-method)) "/"
320           (if (equal (cadr gnus-command-method) "")
321               "unnamed"
322             (cadr gnus-command-method))))
323
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)) "/"))
328
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)))))
334
335 (defun gnus-agent-cat-set-property (category property value)
336   (if value
337       (setcdr (or (assq property category)
338               (let ((cell (cons property nil)))
339                     (setcdr category (cons cell (cdr category)))
340                     cell)) value)
341     (let ((category category))
342       (while (cond ((eq property (caadr category))
343                     (setcdr category (cddr category))
344                     nil)
345                    (t
346                     (setq category (cdr category)))))))
347   category)
348
349 (eval-when-compile
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)))
360
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)
370                               category
371                               (quote (quote ,prop-name))
372                               value))
373                       (list (quote ,name) --category--temp--) ; access-form
374                       )))))
375   )
376
377 (defmacro gnus-agent-cat-name (category)
378   `(car ,category))
379
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)
400
401
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).
405
406 (eval-and-compile
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--)
411             (list category)
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--))))
417   )
418
419 (defun gnus-agent-set-cat-groups (category groups)
420   (unless (eq groups 'ignore)
421     (let ((new-g groups)
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.
426              nil
427              )
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)))
433                            cell)) new-g))
434             (t
435              (let ((groups groups))
436                (while groups
437                  (let* ((group        (pop groups))
438                         (old-category (gnus-group-category group)))
439                    (if (eq category old-category)
440                        nil
441                      (setf (gnus-agent-cat-groups old-category)
442                            (delete group (gnus-agent-cat-groups
443                                           old-category))))))
444                ;; Purge cache as preceding loop invalidated it.
445                (setq gnus-category-group-cache nil))
446
447              (setcdr (or (assq 'agent-groups category)
448                          (let ((cell (cons 'agent-groups nil)))
449                            (setcdr category (cons cell (cdr category)))
450                            cell)) groups))))))
451
452 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
453   (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
454
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)))
458     (when def
459       (setq def (gnus-group-decoded-name def)))
460     (gnus-group-completing-read nil nil t nil nil def)))
461
462 ;;; Fetching setup functions.
463
464 (defun gnus-agent-start-fetch ()
465   "Initialize data structures for efficient fetching."
466   (gnus-agent-create-buffer))
467
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
472     (widen)))
473
474 (defmacro gnus-agent-with-fetch (&rest forms)
475   "Do FORMS safely."
476   `(unwind-protect
477        (let ((gnus-agent-fetching t))
478          (gnus-agent-start-fetch)
479          ,@forms)
480      (gnus-agent-stop-fetch)))
481
482 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
483 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
484
485 (defmacro gnus-agent-append-to-list (tail value)
486   `(setq ,tail (setcdr ,tail (cons ,value nil))))
487
488 (defmacro gnus-agent-message (level &rest args)
489   `(if (<= ,level gnus-verbose)
490        (message ,@args)))
491
492 ;;;
493 ;;; Mode infestation
494 ;;;
495
496 (defvar gnus-agent-mode-hook nil
497   "Hook run when installing agent mode.")
498
499 (defvar gnus-agent-mode nil)
500 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
501
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)
509     (set mode nil)
510     (set (make-local-variable mode) t)
511     ;; Set up the menu.
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"
518                                                      buffer))))
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)))))
530
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)
542
543 (defun gnus-agent-group-make-menu-bar ()
544   (unless (boundp 'gnus-agent-group-menu)
545     (easy-menu-define
546      gnus-agent-group-menu gnus-agent-group-mode-map ""
547      '("Agent"
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]
554        ("Fetch"
555         ["All" gnus-agent-fetch-session gnus-plugged]
556         ["Group" gnus-agent-fetch-group gnus-plugged])
557        ["Synchronize flags" gnus-agent-synchronize-flags t]
558        ))))
559
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)
570
571 (defun gnus-agent-summary-make-menu-bar ()
572   (unless (boundp 'gnus-agent-summary-menu)
573     (easy-menu-define
574      gnus-agent-summary-menu gnus-agent-summary-mode-map ""
575      '("Agent"
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]))))
582
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)
588
589 (defun gnus-agent-server-make-menu-bar ()
590   (unless (boundp 'gnus-agent-server-menu)
591     (easy-menu-define
592      gnus-agent-server-menu gnus-agent-server-mode-map ""
593      '("Agent"
594        ["Toggle plugged" gnus-agent-toggle-plugged t]
595        ["Add" gnus-agent-add-server t]
596        ["Remove" gnus-agent-remove-server t]))))
597
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)
603                   'mouse-face
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))
609                       'modeline
610                     'mode-line-highlight))
611     string))
612
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)
617          nil)
618         (set-to
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"
623                                                    'mouse-2
624                                                    'gnus-agent-toggle-plugged))
625          (gnus-agent-go-online gnus-agent-go-online))
626         (t
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"
632                                                    'mouse-2
633                                                    'gnus-agent-toggle-plugged))))
634   (set-buffer-modified-p t))
635
636 (defmacro gnus-agent-while-plugged (&rest body)
637   `(let ((original-gnus-plugged gnus-plugged))
638     (unwind-protect
639         (progn (gnus-agent-toggle-plugged t)
640                ,@body)
641       (gnus-agent-toggle-plugged original-gnus-plugged))))
642
643 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
644 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
645
646 (defun gnus-agent-close-connections ()
647   "Close all methods covered by the Gnus agent."
648   (let ((methods (gnus-agent-covered-methods)))
649     (while methods
650       (gnus-close-server (pop methods)))))
651
652 ;;;###autoload
653 (defun gnus-unplugged ()
654   "Start Gnus unplugged."
655   (interactive)
656   (setq gnus-plugged nil)
657   (gnus))
658
659 ;;;###autoload
660 (defun gnus-plugged ()
661   "Start Gnus plugged."
662   (interactive)
663   (setq gnus-plugged t)
664   (gnus))
665
666 ;;;###autoload
667 (defun gnus-slave-unplugged (&optional arg)
668   "Read news as a slave unplugged."
669   (interactive "P")
670   (setq gnus-plugged nil)
671   (gnus arg nil 'slave))
672
673 ;;;###autoload
674 (defun gnus-agentize ()
675   "Allow Gnus to be an offline newsreader.
676
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.
680
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."
684   (interactive)
685   (gnus-open-agent)
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))
691
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
694   ;; again.
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...")
699     (mapc
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)))
709
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"))
714                         gnus-newsrc-hashtb)
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"))
718                             nil '(nndraft "")))
719     (gnus-group-set-parameter
720      (format "nndraft:%s" (or group-name "queue"))
721      'gnus-dummy '((gnus-draft-mode)))))
722
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))
728     (re-search-forward
729      (concat "^" (regexp-quote mail-header-separator) "\n"))
730     (replace-match "\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")))
734
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
738 be a select method."
739   (save-excursion
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)
744             "\n")
745     (forward-char -1)
746     (while (search-backward "\n" nil t)
747       (replace-match "\\n" t t))))
748
749 (defun gnus-agent-restore-gcc ()
750   "Restore GCC field from saved header."
751   (save-excursion
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))))
756
757 (defun gnus-agent-any-covered-gcc ()
758   (save-restriction
759     (message-narrow-to-headers)
760     (let* ((gcc (mail-fetch-field "gcc" nil t))
761            (methods (and gcc
762                          (mapcar 'gnus-inews-group-method
763                                  (message-unquote-tokens
764                                   (message-tokenize-header
765                                    gcc " ,")))))
766            covered)
767       (while (and (not covered) methods)
768         (setq covered (gnus-agent-method-p (car methods))
769               methods (cdr methods)))
770       covered)))
771
772 ;;;###autoload
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))
776     (save-excursion
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))))))
781
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)))
786
787 ;;;
788 ;;; Group mode commands
789 ;;;
790
791 (defun gnus-agent-fetch-groups (n)
792   "Put all new articles in the current groups into the Agent."
793   (interactive "P")
794   (unless gnus-plugged
795     (error "Groups can't be fetched when Gnus is unplugged"))
796   (gnus-group-iterate n 'gnus-agent-fetch-group))
797
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))
802   (unless group
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))))))
811
812 (defun gnus-agent-add-group (category arg)
813   "Add the current group to an agent category."
814   (interactive
815    (list
816     (intern
817      (gnus-completing-read
818       "Add to category"
819       (mapcar (lambda (cat) (symbol-name (car cat)))
820               gnus-category-alist)
821       t))
822     current-prefix-arg))
823   (let ((cat (assq category gnus-category-alist))
824         c groups)
825     (gnus-group-iterate arg
826       (lambda (group)
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)))
834
835 (defun gnus-agent-remove-group (arg)
836   "Remove the current group from its agent category, if any."
837   (interactive "P")
838   (let (c)
839     (gnus-group-iterate arg
840       (lambda (group)
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)))
845
846 (defun gnus-agent-synchronize-flags ()
847   "Synchronize unplugged flags with servers."
848   (interactive)
849   (save-excursion
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)))))
853
854 (defun gnus-agent-possibly-synchronize-flags ()
855   "Synchronize flags according to `gnus-agent-synchronize-flags'."
856   (interactive)
857   (save-excursion
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)))))
861
862 (defun gnus-agent-synchronize-flags-server (method)
863   "Synchronize flags set when unplugged for server."
864   (let ((gnus-command-method method)
865         (gnus-agent nil))
866     (when (file-exists-p (gnus-agent-lib-file "flags"))
867       (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
868       (erase-buffer)
869       (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
870       (cond ((null gnus-plugged)
871              (gnus-message
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))
875              (gnus-message
876               1 "Couldn't open server %s" (nth 1 gnus-command-method)))
877             (t
878              (condition-case err
879                  (while t
880                    (let ((bgn (point)))
881                      (eval (read (current-buffer)))
882                      (delete-region bgn (point))))
883                (end-of-file
884                 (delete-file (gnus-agent-lib-file "flags")))
885                (error
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)))))))
891       (kill-buffer nil))))
892
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)
899                       (gnus-y-or-n-p
900                        (format "Synchronize flags on server `%s'? "
901                                (cadr method))))))
902     (gnus-agent-synchronize-flags-server method)))
903
904 ;;;###autoload
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
910 supported."
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