1 ;;; gnus-agent.el --- unplugged support for Gnus
3 ;; Copyright (C) 1997-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
34 (if (featurep 'xemacs)
39 (autoload 'gnus-server-update-server "gnus-srvr")
40 (autoload 'gnus-agent-customize-category "gnus-cus")
42 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
43 "Where the Gnus agent will store its files."
47 (defcustom gnus-agent-plugged-hook nil
48 "Hook run when plugging into the network."
52 (defcustom gnus-agent-unplugged-hook nil
53 "Hook run when unplugging from the network."
57 (defcustom gnus-agent-fetched-hook nil
58 "Hook run when finished fetching articles."
63 (defcustom gnus-agent-handle-level gnus-level-subscribed
64 "Groups on levels higher than this variable will be ignored by the Agent."
68 (defcustom gnus-agent-expire-days 7
69 "Read articles older than this will be expired.
70 If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
72 :type '(number :tag "days"))
74 (defcustom gnus-agent-expire-all nil
75 "If non-nil, also expire unread, ticked and dormant articles.
76 If nil, only read articles will be expired."
80 (defcustom gnus-agent-group-mode-hook nil
81 "Hook run in Agent group minor modes."
85 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
86 (when (featurep 'xemacs)
87 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
89 (defcustom gnus-agent-summary-mode-hook nil
90 "Hook run in Agent summary minor modes."
94 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
95 (when (featurep 'xemacs)
96 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
98 (defcustom gnus-agent-server-mode-hook nil
99 "Hook run in Agent summary minor modes."
103 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
104 (when (featurep 'xemacs)
105 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
107 (defcustom gnus-agent-confirmation-function 'y-or-n-p
108 "Function to confirm when error happens."
113 (defcustom gnus-agent-synchronize-flags nil
114 "Indicate if flags are synchronized when you plug in.
115 If this is `ask' the hook will query the user."
116 ;; If the default switches to something else than nil, then the function
117 ;; should be fixed not be exceedingly slow. See 2005-09-20 ChangeLog entry.
119 :type '(choice (const :tag "Always" t)
120 (const :tag "Never" nil)
121 (const :tag "Ask" ask))
124 (defcustom gnus-agent-go-online 'ask
125 "Indicate if offline servers go online when you plug in.
126 If this is `ask' the hook will query the user."
128 :type '(choice (const :tag "Always" t)
129 (const :tag "Never" nil)
130 (const :tag "Ask" ask))
133 (defcustom gnus-agent-mark-unread-after-downloaded t
134 "Indicate whether to mark articles unread after downloaded."
139 (defcustom gnus-agent-download-marks '(download)
140 "Marks for downloading."
142 :type '(repeat (symbol :tag "Mark"))
145 (defcustom gnus-agent-consider-all-articles nil
146 "When non-nil, the agent will let the agent predicate decide
147 whether articles need to be downloaded or not, for all articles. When
148 nil, the default, the agent will only let the predicate decide
149 whether unread articles are downloaded or not. If you enable this,
150 groups with large active ranges may open slower and you may also want
151 to look into the agent expiry settings to block the expiration of
152 read articles as they would just be downloaded again."
157 (defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
158 "Chunk size for `gnus-agent-fetch-session'.
159 The function will split its article fetches into chunks smaller than
165 (defcustom gnus-agent-enable-expiration 'ENABLE
166 "The default expiration state for each group.
167 When set to ENABLE, the default, `gnus-agent-expire' will expire old
168 contents from a group's local storage. This value may be overridden
169 to disable expiration in specific categories, topics, and groups. Of
170 course, you could change gnus-agent-enable-expiration to DISABLE then
171 enable expiration per categories, topics, and groups."
174 :type '(radio (const :format "Enable " ENABLE)
175 (const :format "Disable " DISABLE)))
177 (defcustom gnus-agent-expire-unagentized-dirs t
178 "*Whether expiration should expire in unagentized directories.
179 Have gnus-agent-expire scan the directories under
180 \(gnus-agent-directory) for groups that are no longer agentized.
181 When found, offer to remove them."
186 (defcustom gnus-agent-auto-agentize-methods nil
187 "Initially, all servers from these methods are agentized.
188 The user may remove or add servers using the Server buffer.
189 See Info node `(gnus)Server Buffer'."
191 :type '(repeat symbol)
194 (defcustom gnus-agent-queue-mail t
195 "Whether and when outgoing mail should be queued by the agent.
196 When `always', always queue outgoing mail. When nil, never
197 queue. Otherwise, queue if and only if unplugged."
200 :type '(radio (const :format "Always" always)
201 (const :format "Never" nil)
202 (const :format "When unplugged" t)))
204 (defcustom gnus-agent-prompt-send-queue nil
205 "If non-nil, `gnus-group-send-queue' will prompt if called when unplugged."
210 (defcustom gnus-agent-article-alist-save-format 1
211 "Indicates whether to use compression(2), versus no
212 compression(1), when writing agentview files. The compressed
213 files do save space but load times are 6-7 times higher. A group
214 must be opened then closed for the agentview to be updated using
216 ;; Wouldn't symbols instead numbers be nicer? --rsteib
219 :type '(radio (const :format "Compressed" 2)
220 (const :format "Uncompressed" 1)))
222 ;;; Internal variables
224 (defvar gnus-agent-history-buffers nil)
225 (defvar gnus-agent-buffer-alist nil)
226 (defvar gnus-agent-article-alist nil
227 "An assoc list identifying the articles whose headers have been fetched.
228 If successfully fetched, these headers will be stored in the group's overview
229 file. The key of each assoc pair is the article ID, the value of each assoc
230 pair is a flag indicating whether the identified article has been downloaded
231 \(gnus-agent-fetch-articles sets the value to the day of the download).
233 1) The last element of this list can not be expired as some
234 routines (for example, get-agent-fetch-headers) use the last
235 value to track which articles have had their headers retrieved.
236 2) The function `gnus-agent-regenerate' may destructively modify the value.")
237 (defvar gnus-agent-group-alist nil)
238 (defvar gnus-category-alist nil)
239 (defvar gnus-agent-current-history nil)
240 (defvar gnus-agent-overview-buffer nil)
241 (defvar gnus-category-predicate-cache nil)
242 (defvar gnus-category-group-cache nil)
243 (defvar gnus-agent-spam-hashtb nil)
244 (defvar gnus-agent-file-name nil)
245 (defvar gnus-agent-send-mail-function nil)
246 (defvar gnus-agent-file-coding-system 'raw-text)
247 (defvar gnus-agent-file-loading-cache nil)
248 (defvar gnus-agent-total-fetched-hashtb nil)
249 (defvar gnus-agent-inhibit-update-total-fetched-for nil)
250 (defvar gnus-agent-need-update-total-fetched-for nil)
253 (defvar gnus-headers)
256 ;; Added to support XEmacs
258 (unless (fboundp 'directory-files-and-attributes)
259 (defun directory-files-and-attributes (directory
260 &optional full match nosort)
262 (dolist (file (directory-files directory full match nosort))
263 (push (cons file (file-attributes file)) result))
264 (nreverse result)))))
270 (defun gnus-open-agent ()
272 (gnus-agent-read-servers)
274 (gnus-agent-create-buffer)
275 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
276 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
277 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
279 (defun gnus-agent-create-buffer ()
280 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
282 (setq gnus-agent-overview-buffer
283 (gnus-get-buffer-create " *Gnus agent overview*"))
284 (with-current-buffer gnus-agent-overview-buffer
285 (mm-enable-multibyte))
288 (gnus-add-shutdown 'gnus-close-agent 'gnus)
290 (defun gnus-close-agent ()
291 (setq gnus-category-predicate-cache nil
292 gnus-category-group-cache nil
293 gnus-agent-spam-hashtb nil)
294 (gnus-kill-buffer gnus-agent-overview-buffer))
297 ;;; Utility functions
300 (defmacro gnus-agent-with-refreshed-group (group &rest body)
301 "Performs the body then updates the group's line in the group
302 buffer. Automatically blocks multiple updates due to recursion."
303 `(prog1 (let ((gnus-agent-inhibit-update-total-fetched-for t)) ,@body)
304 (when (and gnus-agent-need-update-total-fetched-for
305 (not gnus-agent-inhibit-update-total-fetched-for))
306 (with-current-buffer gnus-group-buffer
307 (setq gnus-agent-need-update-total-fetched-for nil)
308 (gnus-group-update-group ,group t)))))
310 (defun gnus-agent-read-file (file)
311 "Load FILE and do a `read' there."
314 (nnheader-insert-file-contents file)
315 (goto-char (point-min))
316 (read (current-buffer)))))
318 (defsubst gnus-agent-method ()
319 (concat (symbol-name (car gnus-command-method)) "/"
320 (if (equal (cadr gnus-command-method) "")
322 (cadr gnus-command-method))))
324 (defsubst gnus-agent-directory ()
325 "The name of the Gnus agent directory."
326 (nnheader-concat gnus-agent-directory
327 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
329 (defun gnus-agent-lib-file (file)
330 "The full name of the Gnus agent library FILE."
331 (expand-file-name file
332 (file-name-as-directory
333 (expand-file-name "agent.lib" (gnus-agent-directory)))))
335 (defun gnus-agent-cat-set-property (category property value)
337 (setcdr (or (assq property category)
338 (let ((cell (cons property nil)))
339 (setcdr category (cons cell (cdr category)))
341 (let ((category category))
342 (while (cond ((eq property (caadr category))
343 (setcdr category (cddr category))
346 (setq category (cdr category)))))))
350 (defmacro gnus-agent-cat-defaccessor (name prop-name)
351 "Define accessor and setter methods for manipulating a list of the form
352 \(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
353 Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
354 manipulated as follows:
355 (func LIST): Returns VALUE1
356 (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
357 `(progn (defmacro ,name (category)
358 (list (quote cdr) (list (quote assq)
359 (quote (quote ,prop-name)) category)))
361 (define-setf-method ,name (category)
362 (let* ((--category--temp-- (make-symbol "--category--"))
363 (--value--temp-- (make-symbol "--value--")))
364 (list (list --category--temp--) ; temporary-variables
365 (list category) ; value-forms
366 (list --value--temp--) ; store-variables
367 (let* ((category --category--temp--) ; store-form
368 (value --value--temp--))
369 (list (quote gnus-agent-cat-set-property)
371 (quote (quote ,prop-name))
373 (list (quote ,name) --category--temp--) ; access-form
377 (defmacro gnus-agent-cat-name (category)
380 (gnus-agent-cat-defaccessor
381 gnus-agent-cat-days-until-old agent-days-until-old)
382 (gnus-agent-cat-defaccessor
383 gnus-agent-cat-enable-expiration agent-enable-expiration)
384 (gnus-agent-cat-defaccessor
385 gnus-agent-cat-groups agent-groups)
386 (gnus-agent-cat-defaccessor
387 gnus-agent-cat-high-score agent-high-score)
388 (gnus-agent-cat-defaccessor
389 gnus-agent-cat-length-when-long agent-long-article)
390 (gnus-agent-cat-defaccessor
391 gnus-agent-cat-length-when-short agent-short-article)
392 (gnus-agent-cat-defaccessor
393 gnus-agent-cat-low-score agent-low-score)
394 (gnus-agent-cat-defaccessor
395 gnus-agent-cat-predicate agent-predicate)
396 (gnus-agent-cat-defaccessor
397 gnus-agent-cat-score-file agent-score)
398 (gnus-agent-cat-defaccessor
399 gnus-agent-cat-enable-undownloaded-faces agent-enable-undownloaded-faces)
402 ;; This form is equivalent to defsetf except that it calls make-symbol
403 ;; whereas defsetf calls gensym (Using gensym creates a run-time
404 ;; dependency on the CL library).
407 (define-setf-method gnus-agent-cat-groups (category)
408 (let* ((--category--temp-- (make-symbol "--category--"))
409 (--groups--temp-- (make-symbol "--groups--")))
410 (list (list --category--temp--)
412 (list --groups--temp--)
413 (let* ((category --category--temp--)
414 (groups --groups--temp--))
415 (list (quote gnus-agent-set-cat-groups) category groups))
416 (list (quote gnus-agent-cat-groups) --category--temp--))))
419 (defun gnus-agent-set-cat-groups (category groups)
420 (unless (eq groups 'ignore)
422 (old-g (gnus-agent-cat-groups category)))
423 (cond ((eq new-g old-g)
424 ;; gnus-agent-add-group is fiddling with the group
425 ;; list. Still, Im done.
428 ((eq new-g (cdr old-g))
429 ;; gnus-agent-add-group is fiddling with the group list
430 (setcdr (or (assq 'agent-groups category)
431 (let ((cell (cons 'agent-groups nil)))
432 (setcdr category (cons cell (cdr category)))
435 (let ((groups groups))
437 (let* ((group (pop groups))
438 (old-category (gnus-group-category group)))
439 (if (eq category old-category)
441 (setf (gnus-agent-cat-groups old-category)
442 (delete group (gnus-agent-cat-groups
444 ;; Purge cache as preceding loop invalidated it.
445 (setq gnus-category-group-cache nil))
447 (setcdr (or (assq 'agent-groups category)
448 (let ((cell (cons 'agent-groups nil)))
449 (setcdr category (cons cell (cdr category)))
452 (defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
453 (list name `(agent-predicate . ,(or default-agent-predicate 'false))))
455 (defun gnus-agent-read-group ()
456 "Read a group name in the minibuffer, with completion."
457 (let ((def (or (gnus-group-group-name) gnus-newsgroup-name)))
459 (setq def (gnus-group-decoded-name def)))
460 (gnus-group-completing-read nil nil t nil nil def)))
462 ;;; Fetching setup functions.
464 (defun gnus-agent-start-fetch ()
465 "Initialize data structures for efficient fetching."
466 (gnus-agent-create-buffer))
468 (defun gnus-agent-stop-fetch ()
469 "Save all data structures and clean up."
470 (setq gnus-agent-spam-hashtb nil)
471 (with-current-buffer nntp-server-buffer
474 (defmacro gnus-agent-with-fetch (&rest forms)
477 (let ((gnus-agent-fetching t))
478 (gnus-agent-start-fetch)