root/trunk/lisp/gnus/gnus-agent.el

Revision 4220, 152.1 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; gnus-agent.el --- unplugged support for Gnus
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 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 3, 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 t
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 unplugged" 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
255 ;; Dynamic variables
256 (defvar gnus-headers)
257 (defvar gnus-score)
258
259 ;;;
260 ;;; Setup
261 ;;;
262
263 (defun gnus-open-agent ()
264   (setq gnus-agent t)
265   (gnus-agent-read-servers)
266   (gnus-category-read)
267   (gnus-agent-create-buffer)
268   (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
269   (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
270   (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
271
272 (defun gnus-agent-create-buffer ()
273   (if (gnus-buffer-live-p gnus-agent-overview-buffer)
274       t
275     (setq gnus-agent-overview-buffer
276           (gnus-get-buffer-create " *Gnus agent overview*"))
277     (with-current-buffer gnus-agent-overview-buffer
278       (mm-enable-multibyte))
279     nil))
280
281 (gnus-add-shutdown 'gnus-close-agent 'gnus)
282
283 (defun gnus-close-agent ()
284   (setq gnus-category-predicate-cache nil
285         gnus-category-group-cache nil
286         gnus-agent-spam-hashtb nil)
287   (gnus-kill-buffer gnus-agent-overview-buffer))
288
289 ;;;
290 ;;; Utility functions
291 ;;;
292
293 (defun gnus-agent-read-file (file)
294   "Load FILE and do a `read' there."
295   (with-temp-buffer
296     (ignore-errors
297       (nnheader-insert-file-contents file)
298       (goto-char (point-min))
299       (read (current-buffer)))))
300
301 (defsubst gnus-agent-method ()
302   (concat (symbol-name (car gnus-command-method)) "/"
303           (if (equal (cadr gnus-command-method) "")
304               "unnamed"
305             (cadr gnus-command-method))))
306
307 (defsubst gnus-agent-directory ()
308   "The name of the Gnus agent directory."
309   (nnheader-concat gnus-agent-directory
310                    (nnheader-translate-file-chars (gnus-agent-method)) "/"))
311
312 (defun gnus-agent-lib-file (file)
313   "The full name of the Gnus agent library FILE."
314   (expand-file-name file
315                     (file-name-as-directory
316                      (expand-file-name "agent.lib" (gnus-agent-directory)))))
317
318 (defun gnus-agent-cat-set-property (category property value)
319   (if value
320       (setcdr (or (assq property category)
321               (let ((cell (cons property nil)))
322                     (setcdr category (cons cell (cdr category)))
323                     cell)) value)
324     (let ((category category))
325       (while (cond ((eq property (caadr category))
326                     (setcdr category (cddr category))
327                     nil)
328                    (t
329                     (setq category (cdr category)))))))
330   category)
331
332 (eval-when-compile
333   (defmacro gnus-agent-cat-defaccessor (name prop-name)
334     "Define accessor and setter methods for manipulating a list of the form
335 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
336 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
337 manipulated as follows:
338   (func LIST): Returns VALUE1
339   (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
340     `(progn (defmacro ,name (category)
341               (list (quote cdr) (list (quote assq)
342                                       (quote (quote ,prop-name)) category)))
343
344             (define-setf-method ,name (category)
345               (let* ((--category--temp-- (make-symbol "--category--"))
346                      (--value--temp-- (make-symbol "--value--")))
347                 (list (list --category--temp--) ; temporary-variables
348                       (list category)   ; value-forms
349                       (list --value--temp--) ; store-variables
350                       (let* ((category --category--temp--) ; store-form
351                              (value --value--temp--))
352                         (list (quote gnus-agent-cat-set-property)
353                               category
354                               (quote (quote ,prop-name))
355                               value))
356                       (list (quote ,name) --category--temp--) ; access-form
357                       )))))
358   )
359
360 (defmacro gnus-agent-cat-name (category)
361   `(car ,category))
362
363 (gnus-agent-cat-defaccessor
364  gnus-agent-cat-days-until-old             agent-days-until-old)
365 (gnus-agent-cat-defaccessor
366  gnus-agent-cat-enable-expiration          agent-enable-expiration)
367 (gnus-agent-cat-defaccessor
368  gnus-agent-cat-groups                     agent-groups)
369 (gnus-agent-cat-defaccessor
370  gnus-agent-cat-high-score                 agent-high-score)
371 (gnus-agent-cat-defaccessor
372  gnus-agent-cat-length-when-long           agent-long-article)
373 (gnus-agent-cat-defaccessor
374  gnus-agent-cat-length-when-short          agent-short-article)
375 (gnus-agent-cat-defaccessor
376  gnus-agent-cat-low-score                  agent-low-score)
377 (gnus-agent-cat-defaccessor
378  gnus-agent-cat-predicate                  agent-predicate)
379 (gnus-agent-cat-defaccessor
380  gnus-agent-cat-score-file                 agent-score)
381 (gnus-agent-cat-defaccessor
382  gnus-agent-cat-enable-undownloaded-faces  agent-enable-undownloaded-faces)
383
384
385 ;; This form is equivalent to defsetf except that it calls make-symbol
386 ;; whereas defsetf calls gensym (Using gensym creates a run-time
387 ;; dependency on the CL library).
388
389 (eval-and-compile
390   (define-setf-method gnus-agent-cat-groups (category)
391     (let* ((--category--temp-- (make-symbol "--category--"))
392            (--groups--temp-- (make-symbol "--groups--")))
393       (list (list --category--temp--)
394             (list category)
395             (list --groups--temp--)
396             (let* ((category --category--temp--)
397                    (groups --groups--temp--))
398               (list (quote gnus-agent-set-cat-groups) category groups))
399             (list (quote gnus-agent-cat-groups) --category--temp--))))
400   )
401
402 (defun gnus-agent-set-cat-groups (category groups)
403   (unless (eq groups 'ignore)
404     (let ((new-g groups)
405           (old-g (gnus-agent-cat-groups category)))
406       (cond ((eq new-g old-g)
407              ;; gnus-agent-add-group is fiddling with the group
408              ;; list. Still, Im done.
409              nil
410              )
411             ((eq new-g (cdr old-g))
412              ;; gnus-agent-add-group is fiddling with the group list
413              (setcdr (or (assq 'agent-groups category)
414                          (let ((cell (cons 'agent-groups nil)))
415                            (setcdr category (cons cell (cdr category)))
416                            cell)) new-g))
417             (t
418              (let ((groups groups))
419                (while groups
420                  (let* ((group        (pop groups))
421                         (old-category (gnus-group-category group)))
422                    (if (eq category old-category)
423                        nil
424                      (setf (gnus-agent-cat-groups old-category)
425                            (delete group (gnus-agent-cat-groups
426                                           old-category))))))
427                ;; Purge cache as preceeding loop invalidated it.
428                (setq gnus-category-group-cache nil))
429
430              (setcdr (or (assq 'agent-groups category)
431                          (let ((cell (cons 'agent-groups nil)))
432                            (setcdr category (cons cell (cdr category)))
433                            cell)) groups))))))
434
435 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
436   (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
437
438 ;;; Fetching setup functions.
439
440 (defun gnus-agent-start-fetch ()
441   "Initialize data structures for efficient fetching."
442   (gnus-agent-create-buffer))
443
444 (defun gnus-agent-stop-fetch ()
445   "Save all data structures and clean up."
446   (setq gnus-agent-spam-hashtb nil)
447   (save-excursion
448     (set-buffer nntp-server-buffer)
449     (widen)))
450
451 (defmacro gnus-agent-with-fetch (&rest forms)
452   "Do FORMS safely."
453   `(unwind-protect
454        (let ((gnus-agent-fetching t))
455          (gnus-agent-start-fetch)
456          ,@forms)
457      (gnus-agent-stop-fetch)))
458
459 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
460 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
461
462 (defmacro gnus-agent-append-to-list (tail value)
463   `(setq ,tail (setcdr ,tail (cons ,value nil))))
464
465 (defmacro gnus-agent-message (level &rest args)
466   `(if (<= ,level gnus-verbose)
467        (message ,@args)))
468
469 ;;;
470 ;;; Mode infestation
471 ;;;
472
473 (defvar gnus-agent-mode-hook nil
474   "Hook run when installing agent mode.")
475
476 (defvar gnus-agent-mode nil)
477 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
478
479 (defun gnus-agent-mode ()
480   "Minor mode for providing a agent support in Gnus buffers."
481   (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
482                                       (symbol-name major-mode))
483                         (match-string 1 (symbol-name major-mode))))
484          (mode (intern (format "gnus-agent-%s-mode" buffer))))
485     (set (make-local-variable 'gnus-agent-mode) t)
486     (set mode nil)
487     (set (make-local-variable mode) t)
488     ;; Set up the menu.
489     (when (gnus-visual-p 'agent-menu 'menu)
490       (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
491     (unless (assq 'gnus-agent-mode minor-mode-alist)
492       (push gnus-agent-mode-status minor-mode-alist))
493     (unless (assq mode minor-mode-map-alist)
494       (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
495                                                      buffer))))
496             minor-mode-map-alist))
497     (when (eq major-mode 'gnus-group-mode)
498       (let ((init-plugged gnus-plugged)
499             (gnus-agent-go-online nil))
500         ;; g-a-t-p does nothing when gnus-plugged isn't changed.
501         ;; Therefore, make certain that the current value does not
502         ;; match the desired initial value.
503         (setq gnus-plugged :unknown)
504         (gnus-agent-toggle-plugged init-plugged)))
505     (gnus-run-hooks 'gnus-agent-mode-hook
506                     (intern (format "gnus-agent-%s-mode-hook" buffer)))))
507
508 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
509 (gnus-define-keys gnus-agent-group-mode-map
510   "Ju" gnus-agent-fetch-groups
511   "Jc" gnus-enter-category-buffer
512   "Jj" gnus-agent-toggle-plugged
513   "Js" gnus-agent-fetch-session
514   "JY" gnus-agent-synchronize-flags
515   "JS" gnus-group-send-queue
516   "Ja" gnus-agent-add-group
517   "Jr" gnus-agent-remove-group
518   "Jo" gnus-agent-toggle-group-plugged)
519
520 (defun gnus-agent-group-make-menu-bar ()
521   (unless (boundp 'gnus-agent-group-menu)
522     (easy-menu-define
523      gnus-agent-group-menu gnus-agent-group-mode-map ""
524      '("Agent"
525        ["Toggle plugged" gnus-agent-toggle-plugged t]
526        ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
527        ["List categories" gnus-enter-category-buffer t]
528        ["Add (current) group to category" gnus-agent-add-group t]
529        ["Remove (current) group from category" gnus-agent-remove-group t]
530        ["Send queue" gnus-group-send-queue gnus-plugged]
531        ("Fetch"
532         ["All" gnus-agent-fetch-session gnus-plugged]
533         ["Group" gnus-agent-fetch-group gnus-plugged])
534        ["Synchronize flags" gnus-agent-synchronize-flags t]
535        ))))
536
537 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
538 (gnus-define-keys gnus-agent-summary-mode-map
539   "Jj" gnus-agent-toggle-plugged
540   "Ju" gnus-agent-summary-fetch-group
541   "JS" gnus-agent-fetch-group
542   "Js" gnus-agent-summary-fetch-series
543   "J#" gnus-agent-mark-article
544   "J\M-#" gnus-agent-unmark-article
545   "@" gnus-agent-toggle-mark
546   "Jc" gnus-agent-catchup)
547
548 (defun gnus-agent-summary-make-menu-bar ()
549   (unless (boundp 'gnus-agent-summary-menu)
550     (easy-menu-define
551      gnus-agent-summary-menu gnus-agent-summary-mode-map ""
552      '("Agent"
553        ["Toggle plugged" gnus-agent-toggle-plugged t]
554        ["Mark as downloadable" gnus-agent-mark-article t]
555        ["Unmark as downloadable" gnus-agent-unmark-article t]
556        ["Toggle mark" gnus-agent-toggle-mark t]
557        ["Fetch downloadable" gnus-agent-summary-fetch-group t]
558        ["Catchup undownloaded" gnus-agent-catchup t]))))
559
560 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
561 (gnus-define-keys gnus-agent-server-mode-map
562   "Jj" gnus-agent-toggle-plugged
563   "Ja" gnus-agent-add-server
564   "Jr" gnus-agent-remove-server)
565
566 (defun gnus-agent-server-make-menu-bar ()
567   (unless (boundp 'gnus-agent-server-menu)
568     (easy-menu-define
569      gnus-agent-server-menu gnus-agent-server-mode-map ""
570      '("Agent"
571        ["Toggle plugged" gnus-agent-toggle-plugged t]
572        ["Add" gnus-agent-add-server t]
573        ["Remove" gnus-agent-remove-server t]))))
574
575 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
576   (if (and (fboundp 'propertize)
577            (fboundp 'make-mode-line-mouse-map))
578       (propertize string 'local-map
579                   (make-mode-line-mouse-map mouse-button mouse-func)
580                   'mouse-face
581                   (cond ((and (featurep 'xemacs)
582                               ;; XEmacs' `facep' only checks for a face
583                               ;; object, not for a face name, so it's useless
584                               ;; to check with `facep'.
585                               (find-face 'modeline))
586                          'modeline)
587                         ((facep 'mode-line-highlight) ;; Emacs 22
588                          'mode-line-highlight)
589                         ((facep 'mode-line) ;; Emacs 21
590                          'mode-line)) )
591     string))
592
593 (defun gnus-agent-toggle-plugged (set-to)
594   "Toggle whether Gnus is unplugged or not."
595   (interactive (list (not gnus-plugged)))
596   (cond ((eq set-to gnus-plugged)
597          nil)
598         (set-to
599          (setq gnus-plugged set-to)
600          (gnus-run-hooks 'gnus-agent-plugged-hook)
601          (setcar (cdr gnus-agent-mode-status)
602                  (gnus-agent-make-mode-line-string " Plugged"
603                                                    'mouse-2
604                                                    'gnus-agent-toggle-plugged))
605          (gnus-agent-go-online gnus-agent-go-online)
606          (gnus-agent-possibly-synchronize-flags))
607         (t
608          (gnus-agent-close-connections)
609          (setq gnus-plugged set-to)
610          (gnus-run-hooks 'gnus-agent-unplugged-hook)
611          (setcar (cdr gnus-agent-mode-status)
612                  (gnus-agent-make-mode-line-string " Unplugged"
613                                                    'mouse-2
614                                                    'gnus-agent-toggle-plugged))))
615   (set-buffer-modified-p t))
616
617 (defmacro gnus-agent-while-plugged (&rest body)
618   `(let ((original-gnus-plugged gnus-plugged))
619     (unwind-protect
620         (progn (gnus-agent-toggle-plugged t)
621                ,@body)
622       (gnus-agent-toggle-plugged original-gnus-plugged))))
623
624 (put 'gnus-agent-while-plugged 'lisp-indent-function 0)
625 (put 'gnus-agent-while-plugged 'edebug-form-spec '(body))
626
627 (defun gnus-agent-close-connections ()
628   "Close all methods covered by the Gnus agent."
629   (let ((methods (gnus-agent-covered-methods)))
630     (while methods
631       (gnus-close-server (pop methods)))))
632
633 ;;;###autoload
634 (defun gnus-unplugged ()
635   "Start Gnus unplugged."
636   (interactive)
637   (setq gnus-plugged nil)
638   (gnus))
639
640 ;;;###autoload
641 (defun gnus-plugged ()
642   "Start Gnus plugged."
643   (interactive)
644   (setq gnus-plugged t)
645   (gnus))
646
647 ;;;###autoload
648 (defun gnus-slave-unplugged (&optional arg)
649   "Read news as a slave unplugged."
650   (interactive "P")
651   (setq gnus-plugged nil)
652   (gnus arg nil 'slave))
653
654 ;;;###autoload
655 (defun gnus-agentize ()
656   "Allow Gnus to be an offline newsreader.
657
658 The gnus-agentize function is now called internally by gnus when
659 gnus-agent is set.  If you wish to avoid calling gnus-agentize,
660 customize gnus-agent to nil.
661
662 This will modify the `gnus-setup-news-hook', and
663 `message-send-mail-real-function' variables, and install the Gnus agent
664 minor mode in all Gnus buffers."
665   (interactive)
666   (gnus-open-agent)
667   (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
668   (unless gnus-agent-send-mail-function
669     (setq gnus-agent-send-mail-function
670           (or message-send-mail-real-function
671               (function (lambda () (funcall message-send-mail-function))))
672           message-send-mail-real-function 'gnus-agent-send-mail))
673
674   ;; If the servers file doesn't exist, auto-agentize some servers and
675   ;; save the servers file so this auto-agentizing isn't invoked
676   ;; again.
677   (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
678     (gnus-message 3 "First time agent user, agentizing remote groups...")
679     (mapc
680      (lambda (server-or-method)
681        (let ((method (gnus-server-to-method server-or-method)))
682          (when (memq (car method)
683                      gnus-agent-auto-agentize-methods)
684            (push (gnus-method-to-server method)
685                  gnus-agent-covered-methods)
686            (setq gnus-agent-method-p-cache nil))))
687      (cons gnus-select-method gnus-secondary-select-methods))
688     (gnus-agent-write-servers)))
689
690 (defun gnus-agent-queue-setup (&optional group-name)
691   "Make sure the queue group exists.
692 Optional arg GROUP-NAME allows to specify another group."
693   (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
694                         gnus-newsrc-hashtb)
695     (gnus-request-create-group (or group-name "queue") '(nndraft ""))
696     (let ((gnus-level-default-subscribed 1))
697       (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
698                             nil '(nndraft "")))
699     (gnus-group-set-parameter
700      (format "nndraft:%s" (or group-name "queue"))
701      'gnus-dummy '((gnus-draft-mode)))))
702
703 (defun gnus-agent-send-mail ()
704   (if (or (not gnus-agent-queue-mail)
705           (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
706       (funcall gnus-agent-send-mail-function)
707     (goto-char (point-min))
708     (re-search-forward
709      (concat "^" (regexp-quote mail-header-separator) "\n"))
710     (replace-match "\n")
711     (gnus-agent-insert-meta-information 'mail)
712     (gnus-request-accept-article "nndraft:queue" nil t t)))
713
714 (defun gnus-agent-insert-meta-information (type &optional method)
715   "Insert meta-information into the message that says how it's to be posted.
716 TYPE can be either `mail' or `news'.  If the latter, then METHOD can
717 be a select method."
718   (save-excursion
719     (message-remove-header gnus-agent-meta-information-header)
720     (goto-char (point-min))
721     (insert gnus-agent-meta-information-header ": "
722             (symbol-name type) " " (format "%S" method)
723             "\n")
724     (forward-char -1)
725     (while (search-backward "\n" nil t)
726       (replace-match "\\n" t t))))
727
728 (defun gnus-agent-restore-gcc ()
729   "Restore GCC field from saved header."
730   (save-excursion
731     (goto-char (point-min))
732     (while (re-search-forward
733             (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
734       (replace-match "Gcc:" 'fixedcase))))
735
736 (defun gnus-agent-any-covered-gcc ()
737   (save-restriction
738     (message-narrow-to-headers)
739     (let* ((gcc (mail-fetch-field "gcc" nil t))
740            (methods (and gcc
741                          (mapcar 'gnus-inews-group-method
742                                  (message-unquote-tokens
743                                   (message-tokenize-header
744                                    gcc " ,")))))
745            covered)
746       (while (and (not covered) methods)
747         (setq covered (gnus-agent-method-p (car methods))
748               methods (cdr methods)))
749       covered)))
750
751 ;;;###autoload
752 (defun gnus-agent-possibly-save-gcc ()
753   "Save GCC if Gnus is unplugged."
754   (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
755     (save-excursion
756       (goto-char (point-min))
757       (let ((case-fold-search t))
758         (while (re-search-forward "^gcc:" nil t)
759           (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
760
761 (defun gnus-agent-possibly-do-gcc ()
762   "Do GCC if Gnus is plugged."
763   (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
764     (gnus-inews-do-gcc)))
765
766 ;;;
767 ;;; Group mode commands
768 ;;;
769
770 (defun gnus-agent-fetch-groups (n)
771   "Put all new articles in the current groups into the Agent."
772   (interactive "P")
773   (unless gnus-plugged
774     (error "Groups can't be fetched when Gnus is unplugged"))
775   (gnus-group-iterate n 'gnus-agent-fetch-group))
776
777 (defun gnus-agent-fetch-group (&optional group)
778   "Put all new articles in GROUP into the Agent."
779   (interactive (list (gnus-group-group-name)))
780   (setq group (or group gnus-newsgroup-name))
781   (unless group
782     (error "No group on the current line"))
783
784   (gnus-agent-while-plugged
785     (let ((gnus-command-method (gnus-find-method-for-group group)))
786       (gnus-agent-with-fetch
787         (gnus-agent-fetch-group-1 group gnus-command-method)
788         (gnus-message 5 "Fetching %s...done" group)))))
789
790 (defun gnus-agent-add-group (category arg)
791   "Add the current group to an agent category."
792   (interactive
793    (list
794     (intern
795      (completing-read
796       "Add to category: "
797       (mapcar (lambda (cat) (list (symbol-name (car cat))))
798               gnus-category-alist)
799       nil t))
800     current-prefix-arg))
801   (let ((cat (assq category gnus-category-alist))
802         c groups)
803     (gnus-group-iterate arg
804       (lambda (group)
805         (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
806           (setf (gnus-agent-cat-groups c)
807                 (delete group (gnus-agent-cat-groups c))))
808         (push group groups)))
809     (setf (gnus-agent-cat-groups cat)
810           (nconc (gnus-agent-cat-groups cat) groups))
811     (gnus-category-write)))
812
813 (defun gnus-agent-remove-group (arg)
814   "Remove the current group from its agent category, if any."
815   (interactive "P")
816   (let (c)
817     (gnus-group-iterate arg
818       (lambda (group)
819         (when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
820           (setf (gnus-agent-cat-groups c)
821                 (delete group (gnus-agent-cat-groups c))))))
822     (gnus-category-write)))
823
824 (defun gnus-agent-synchronize-flags ()
825   "Synchronize unplugged flags with servers."
826   (interactive)
827   (save-excursion
828     (dolist (gnus-command-method (gnus-agent-covered-methods))
829       (when (file-exists-p (gnus-agent-lib-file "flags"))
830         (gnus-agent-synchronize-flags-server gnus-command-method)))))
831
832 (defun gnus-agent-possibly-synchronize-flags ()
833   "Synchronize flags according to `gnus-agent-synchronize-flags'."
834   (interactive)
835   (save-excursion
836     (dolist (gnus-command-method (gnus-agent-covered-methods))
837       (when (and (file-exists-p (gnus-agent-lib-file "flags"))
838                  (eq (gnus-server-status gnus-command-method) 'ok))
839         (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
840
841 (defun gnus-agent-synchronize-flags-server (method)
842   "Synchronize flags set when unplugged for server."
843   (let ((gnus-command-method method)
844         (gnus-agent nil))
845     (when (file-exists-p (gnus-agent-lib-file "flags"))
846       (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
847       (erase-buffer)
848       (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
849       (cond ((null gnus-plugged)
850              (gnus-message
851               1 "You must be plugged to synchronize flags with server %s"
852               (nth 1 gnus-command-method)))
853             ((null (gnus-check-server gnus-command-method))
854              (gnus-message
855               1 "Couldn't open server %s" (nth 1 gnus-command-method)))
856             (t
857              (condition-case err
858                  (while t
859                    (let ((bgn (point)))
860                      (eval (read (current-buffer)))
861                      (delete-region bgn (point))))
862                (end-of-file
863                 (delete-file (gnus-agent-lib-file "flags")))
864                (error
865                 (let ((file (gnus-agent-lib-file "flags")))
866                   (write-region (point-min) (point-max)
867                                 (gnus-agent-lib-file "flags") nil 'silent)
868