1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
35 (if (featurep 'xemacs)
40 (autoload 'gnus-server-update-server "gnus-srvr")
41 (autoload 'gnus-agent-customize-category "gnus-cus")
43 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
44 "Where the Gnus agent will store its files."
48 (defcustom gnus-agent-plugged-hook nil
49 "Hook run when plugging into the network."
53 (defcustom gnus-agent-unplugged-hook nil
54 "Hook run when unplugging from the network."
58 (defcustom gnus-agent-fetched-hook nil
59 "Hook run when finished fetching articles."
64 (defcustom gnus-agent-handle-level gnus-level-subscribed
65 "Groups on levels higher than this variable will be ignored by the Agent."
69 (defcustom gnus-agent-expire-days 7
70 "Read articles older than this will be expired.
71 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
73 :type '(number :tag "days"))
75 (defcustom gnus-agent-expire-all nil
76 "If non-nil, also expire unread, ticked and dormant articles.
77 If nil, only read articles will be expired."
81 (defcustom gnus-agent-group-mode-hook nil
82 "Hook run in Agent group minor modes."
86 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
87 (when (featurep 'xemacs)
88 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
90 (defcustom gnus-agent-summary-mode-hook nil
91 "Hook run in Agent summary minor modes."
95 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
96 (when (featurep 'xemacs)
97 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
99 (defcustom gnus-agent-server-mode-hook nil
100 "Hook run in Agent summary minor modes."
104 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
105 (when (featurep 'xemacs)
106 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
108 (defcustom gnus-agent-confirmation-function 'y-or-n-p
109 "Function to confirm when error happens."
114 (defcustom gnus-agent-synchronize-flags nil
115 "Indicate if flags are synchronized when you plug in.
116 If this is `ask' the hook will query the user."
117 ;; If the default switches to something else than nil, then the function
118 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
120 :type '(choice (const :tag "Always" t)
121 (const :tag "Never" nil)
122 (const :tag "Ask" ask))
125 (defcustom gnus-agent-go-online 'ask
126 "Indicate if offline servers go online when you plug in.
127 If this is `ask' the hook will query the user."
129 :type '(choice (const :tag "Always" t)
130 (const :tag "Never" nil)
131 (const :tag "Ask" ask))
134 (defcustom gnus-agent-mark-unread-after-downloaded t
135 "Indicate whether to mark articles unread after downloaded."
140 (defcustom gnus-agent-download-marks '(download)
141 "Marks for downloading."
143 :type '(repeat (symbol :tag "Mark"))
146 (defcustom gnus-agent-consider-all-articles nil
147 "When non-nil, the agent will let the agent predicate decide
148 whether articles need to be downloaded or not, for all articles. When
149 nil, the default, the agent will only let the predicate decide
150 whether unread articles are downloaded or not. If you enable this,
151 groups with large active ranges may open slower and you may also want
152 to look into the agent expiry settings to block the expiration of
153 read articles as they would just be downloaded again."
158 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
159 "Chunk size for `gnus-agent-fetch-session'.
160 The function will split its article fetches into chunks smaller than
166 (defcustom gnus-agent-enable-expiration 'ENABLE
167 "The default expiration state for each group.
168 When set to ENABLE, the default, `gnus-agent-expire' will expire old
169 contents from a group's local storage. This value may be overridden
170 to disable expiration in specific categories, topics, and groups. Of
171 course, you could change gnus-agent-enable-expiration to DISABLE then
172 enable expiration per categories, topics, and groups."
175 :type '(radio (const :format "Enable " ENABLE)
176 (const :format "Disable " DISABLE)))
178 (defcustom gnus-agent-expire-unagentized-dirs t
179 "*Whether expiration should expire in unagentized directories.
180 Have gnus-agent-expire scan the directories under
181 \(gnus-agent-directory) for groups that are no longer agentized.
182 When found, offer to remove them."
187 (defcustom gnus-agent-auto-agentize-methods nil
188 "Initially, all servers from these methods are agentized.
189 The user may remove or add servers using the Server buffer.
190 See Info node `(gnus)Server Buffer'."
192 :type '(repeat symbol)
195 (defcustom gnus-agent-queue-mail t
196 "Whether and when outgoing mail should be queued by the agent.
197 When `always', always queue outgoing mail. When nil, never
198 queue. Otherwise, queue if and only if unplugged."
201 :type '(radio (const :format "Always" always)
202 (const :format "Never" nil)
203 (const :format "When unplugged" t)))
205 (defcustom gnus-agent-prompt-send-queue nil
206 "If non-nil, `gnus-group-send-queue' will prompt if called when
212 (defcustom gnus-agent-article-alist-save-format 1
213 "Indicates whether to use compression(2), versus no
214 compression(1), when writing agentview files. The compressed
215 files do save space but load times are 6-7 times higher. A group
216 must be opened then closed for the agentview to be updated using
218 ;; Wouldn't symbols instead numbers be nicer? --rsteib
221 :type '(radio (const :format "Compressed" 2)
222 (const :format "Uncompressed" 1)))
224 ;;; Internal variables
226 (defvar gnus-agent-history-buffers nil)
227 (defvar gnus-agent-buffer-alist nil)
228 (defvar gnus-agent-article-alist nil
229 "An assoc list identifying the articles whose headers have been fetched.
230 If successfully fetched, these headers will be stored in the group's overview
231 file. The key of each assoc pair is the article ID, the value of each assoc
232 pair is a flag indicating whether the identified article has been downloaded
233 \(gnus-agent-fetch-articles sets the value to the day of the download).
235 1) The last element of this list can not be expired as some
236 routines (for example, get-agent-fetch-headers) use the last
237 value to track which articles have had their headers retrieved.
238 2) The function `gnus-agent-regenerate' may destructively modify the value.")
239 (defvar gnus-agent-group-alist nil)
240 (defvar gnus-category-alist nil)
241 (defvar gnus-agent-current-history nil)
242 (defvar gnus-agent-overview-buffer nil)
243 (defvar gnus-category-predicate-cache nil)
244 (defvar gnus-category-group-cache nil)
245 (defvar gnus-agent-spam-hashtb nil)
246 (defvar gnus-agent-file-name nil)
247 (defvar gnus-agent-send-mail-function nil)
248 (defvar gnus-agent-file-coding-system 'raw-text)
249 (defvar gnus-agent-file-loading-cache nil)
250 (defvar gnus-agent-total-fetched-hashtb nil)
251 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
252 (defvar gnus-agent-need-update-total-fetched-for nil)
255 (defvar gnus-headers)
258 ;; Added to support XEmacs
260 (unless (fboundp 'directory-files-and-attributes)
261 (defun directory-files-and-attributes (directory
262 &optional full match nosort)
264 (dolist (file (directory-files directory full match nosort))
265 (push (cons file (file-attributes file)) result))
266 (nreverse result)))))
272 (defun gnus-open-agent ()
274 (gnus-agent-read-servers)
276 (gnus-agent-create-buffer)
277 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
278 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
279 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
281 (defun gnus-agent-create-buffer ()
282 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
284 (setq gnus-agent-overview-buffer
285 (gnus-get-buffer-create " *Gnus agent overview*"))
286 (with-current-buffer gnus-agent-overview-buffer
287 (mm-enable-multibyte))
290 (gnus-add-shutdown 'gnus-close-agent 'gnus)
292 (defun gnus-close-agent ()
293 (setq gnus-category-predicate-cache nil
294 gnus-category-group-cache nil
295 gnus-agent-spam-hashtb nil)
296 (gnus-kill-buffer gnus-agent-overview-buffer))
299 ;;; Utility functions
302 (defmacro gnus-agent-with-refreshed-group (group &rest body)
303 "Performs the body then updates the group's line in the group
304 buffer. Automatically blocks multiple updates due to recursion."
305 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
306 (when (and gnus-agent-need-update-total-fetched-for
307 (not gnus-agent-inhibit-update-total-fetched-for))
308 (with-current-buffer gnus-group-buffer
309 (setq gnus-agent-need-update-total-fetched-for nil)
310 (gnus-group-update-group ,group t)))))
312 (defun gnus-agent-read-file (file)
313 "Load FILE and do a `read' there."
316 (nnheader-insert-file-contents file)
317 (goto-char (point-min))
318 (read (current-buffer)))))
320 (defsubst gnus-agent-method ()
321 (concat (symbol-name (car gnus-command-method)) "/"
322 (if (equal (cadr gnus-command-method) "")
324 (cadr gnus-command-method))))
326 (defsubst gnus-agent-directory ()
327 "The name of the Gnus agent directory."
328 (nnheader-concat gnus-agent-directory
329 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
331 (defun gnus-agent-lib-file (file)
332 "The full name of the Gnus agent library FILE."
333 (expand-file-name file
334 (file-name-as-directory
335 (expand-file-name "agent.lib" (gnus-agent-directory)))))
337 (defun gnus-agent-cat-set-property (category property value)
339 (setcdr (or (assq property category)
340 (let ((cell (cons property nil)))
341 (setcdr category (cons cell (cdr category)))
343 (let ((category category))
344 (while (cond ((eq property (caadr category))
345 (setcdr category (cddr category))
348 (setq category (cdr category)))))))
352 (defmacro gnus-agent-cat-defaccessor (name prop-name)
353 "Define accessor and setter methods for manipulating a list of the form
354 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
355 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
356 manipulated as follows:
357 (func LIST): Returns VALUE1
358 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
359 `(progn (defmacro ,name (category)
360 (list (quote cdr) (list (quote assq)
361 (quote (quote ,prop-name)) category)))
363 (define-setf-method ,name (category)
364 (let* ((--category--temp-- (make-symbol "--category--"))
365 (--value--temp-- (make-symbol "--value--")))
366 (list (list --category--temp--) ; temporary-variables
367 (list category) ; value-forms
368 (list --value--temp--) ; store-variables
369 (let* ((category --category--temp--) ; store-form
370 (value --value--temp--))
371 (list (quote gnus-agent-cat-set-property)
373 (quote (quote ,prop-name))
375 (list (quote ,name) --category--temp--) ; access-form
379 (defmacro gnus-agent-cat-name (category)
382 (gnus-agent-cat-defaccessor
383 gnus-agent-cat-days-until-old agent-days-until-old)
384 (gnus-agent-cat-defaccessor
385 gnus-agent-cat-enable-expiration agent-enable-expiration)
386 (gnus-agent-cat-defaccessor
387 gnus-agent-cat-groups agent-groups)
388 (gnus-agent-cat-defaccessor
389 gnus-agent-cat-high-score agent-high-score)
390 (gnus-agent-cat-defaccessor
391 gnus-agent-cat-length-when-long agent-long-article)
392 (gnus-agent-cat-defaccessor
393 gnus-agent-cat-length-when-short agent-short-article)
394 (gnus-agent-cat-defaccessor
395 gnus-agent-cat-low-score agent-low-score)
396 (gnus-agent-cat-defaccessor
397 gnus-agent-cat-predicate agent-predicate)
398 (gnus-agent-cat-defaccessor
399 gnus-agent-cat-score-file agent-score)
400 (gnus-agent-cat-defaccessor
401 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
404 ;; This form is equivalent to defsetf except that it calls make-symbol
405 ;; whereas defsetf calls gensym (Using gensym creates a run-time
406 ;; dependency on the CL library).
409 (define-setf-method gnus-agent-cat-groups (category)
410 (let* ((--category--temp-- (make-symbol "--category--"))
411 (--groups--temp-- (make-symbol "--groups--")))
412 (list (list --category--temp--)
414 (list --groups--temp--)
415 (let* ((category --category--temp--)
416 (groups --groups--temp--))
417 (list (quote gnus-agent-set-cat-groups) category groups))
418 (list (quote gnus-agent-cat-groups) --category--temp--))))
421 (defun gnus-agent-set-cat-groups (category groups)
422 (unless (eq groups 'ignore)
424 (old-g (gnus-agent-cat-groups category)))
425 (cond ((eq new-g old-g)
426 ;; gnus-agent-add-group is fiddling with the group
427 ;; list. Still, Im done.
430 ((eq new-g (cdr old-g))
431 ;; gnus-agent-add-group is fiddling with the group list
432 (setcdr (or (assq 'agent-groups category)
433 (let ((cell (cons 'agent-groups nil)))
434 (setcdr category (cons cell (cdr category)))
437 (let ((groups groups))
439 (let* ((group (pop groups))
440 (old-category (gnus-group-category group)))
441 (if (eq category old-category)
443 (setf (gnus-agent-cat-groups old-category)
444 (delete group (gnus-agent-cat-groups
446 ;; Purge cache as preceeding loop invalidated it.
447 (setq gnus-category-group-cache nil))
449 (setcdr (or (assq 'agent-groups category)
450 (let ((cell (cons 'agent-groups nil)))
451 (setcdr category (cons cell (cdr category)))
454 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
455 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
457 (defun gnus-agent-read-group ()
458 "Read a group name in the minibuffer, with completion."
459 (let ((def (or (gnus-group-group-name) gnus-newsgroup-name)))
461 (setq def (gnus-group-decoded-name def)))
462 (gnus-group-completing-read nil nil t nil nil def)))
464 ;;; Fetching setup functions.
466 (defun gnus-agent-start-fetch ()
467 "Initialize data structures for efficient fetching."
468 (gnus-agent-create-buffer))
470 (defun gnus-agent-stop-fetch ()
471 "Save all data structures and clean up."
472 (setq gnus-agent-spam-hashtb nil)
473 (with-current-buffer nntp-server-buffer
476 (defmacro gnus-agent-with-fetch (&rest forms)
479 (let ((gnus-agent-fetching t))
480 (gnus-agent-start-fetch)
482 (gnus-agent-stop-fetch)))
484 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
485 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
487 (defmacro gnus-agent-append-to-list (tail value)
488 `(setq ,tail (setcdr ,tail (cons ,value nil))))
490 (defmacro gnus-agent-message (level &rest args)
491 `(if (<= ,level gnus-verbose)
498 (defvar gnus-agent-mode-hook nil
499 "Hook run when installing agent mode.")
501 (defvar gnus-agent-mode nil)
502 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
504 (defun gnus-agent-mode ()
505 "Minor mode for providing a agent support in Gnus buffers."
506 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
507 (symbol-name major-mode))
508 (match-string 1 (symbol-name major-mode))))
509 (mode (intern (format "gnus-agent-%s-mode" buffer))))
510 (set (make-local-variable 'gnus-agent-mode) t)
512 (set (make-local-variable mode) t)
514 (when (gnus-visual-p 'agent-menu 'menu)
515 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
516 (unless (assq mode minor-mode-alist)
517 (push (cons mode (cdr gnus-agent-mode-status)) minor-mode-alist))
518 (unless (assq mode minor-mode-map-alist)
519 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
521 minor-mode-map-alist))
522 (when (eq major-mode 'gnus-group-mode)
523 (let ((init-plugged gnus-plugged)
524 (gnus-agent-go-online nil))
525 ;; g-a-t-p does nothing when gnus-plugged isn't changed.
526 ;; Therefore, make certain that the current value does not
527 ;; match the desired initial value.
528 (setq gnus-plugged :unknown)
529 (gnus-agent-toggle-plugged init-plugged)))
530 (gnus-run-hooks 'gnus-agent-mode-hook
531 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
533 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
534 (gnus-define-keys gnus-agent-group-mode-map
535 "Ju" gnus-agent-fetch-groups
536 "Jc" gnus-enter-category-buffer
537 "Jj" gnus-agent-toggle-plugged
538 "Js" gnus-agent-fetch-session