mm-uu: enhance diff extraction support
[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)