Import changes from current Gnus.
[bpt/emacs.git] / lisp / gnus / gnus-demon.el
1 ;;; gnus-demon.el --- daemonic Gnus behaviour
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
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 2, 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., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-int)
34 (require 'nnheader)
35 (require 'nntp)
36 (require 'nnmail)
37 (require 'gnus-util)
38 (eval-and-compile
39 (if (string-match "XEmacs" (emacs-version))
40 (require 'itimer)
41 (require 'timer)))
42
43 (defgroup gnus-demon nil
44 "Demonic behaviour."
45 :group 'gnus)
46
47 (defcustom gnus-demon-handlers nil
48 "Alist of daemonic handlers to be run at intervals.
49 Each handler is a list on the form
50
51 \(FUNCTION TIME IDLE)
52
53 FUNCTION is the function to be called.
54 TIME is the number of `gnus-demon-timestep's between each call.
55 If nil, never call. If t, call each `gnus-demon-timestep'.
56 If IDLE is t, only call if Emacs has been idle for a while. If IDLE
57 is a number, only call when Emacs has been idle more than this number
58 of `gnus-demon-timestep's. If IDLE is nil, don't care about
59 idleness. If IDLE is a number and TIME is nil, then call once each
60 time Emacs has been idle for IDLE `gnus-demon-timestep's."
61 :group 'gnus-demon
62 :type '(repeat (list function
63 (choice :tag "Time"
64 (const :tag "never" nil)
65 (const :tag "one" t)
66 (integer :tag "steps" 1))
67 (choice :tag "Idle"
68 (const :tag "don't care" nil)
69 (const :tag "for a while" t)
70 (integer :tag "steps" 1)))))
71
72 (defcustom gnus-demon-timestep 60
73 "*Number of seconds in each demon timestep."
74 :group 'gnus-demon
75 :type 'integer)
76
77 ;;; Internal variables.
78
79 (defvar gnus-demon-timer nil)
80 (defvar gnus-demon-idle-has-been-called nil)
81 (defvar gnus-demon-idle-time 0)
82 (defvar gnus-demon-handler-state nil)
83 (defvar gnus-demon-last-keys nil)
84 (defvar gnus-inhibit-demon nil
85 "*If non-nil, no daemonic function will be run.")
86
87 (eval-and-compile
88 (autoload 'timezone-parse-date "timezone")
89 (autoload 'timezone-make-arpa-date "timezone"))
90
91 ;;; Functions.
92
93 (defun gnus-demon-add-handler (function time idle)
94 "Add the handler FUNCTION to be run at TIME and IDLE."
95 ;; First remove any old handlers that use this function.
96 (gnus-demon-remove-handler function)
97 ;; Then add the new one.
98 (push (list function time idle) gnus-demon-handlers)
99 (gnus-demon-init))
100
101 (defun gnus-demon-remove-handler (function &optional no-init)
102 "Remove the handler FUNCTION from the list of handlers."
103 (gnus-pull function gnus-demon-handlers)
104 (unless no-init
105 (gnus-demon-init)))
106
107 (defun gnus-demon-init ()
108 "Initialize the Gnus daemon."
109 (interactive)
110 (gnus-demon-cancel)
111 (when gnus-demon-handlers
112 ;; Set up the timer.
113 (setq gnus-demon-timer
114 (nnheader-run-at-time
115 gnus-demon-timestep gnus-demon-timestep 'gnus-demon))
116 ;; Reset control variables.
117 (setq gnus-demon-handler-state
118 (mapcar
119 (lambda (handler)
120 (list (car handler) (gnus-demon-time-to-step (nth 1 handler))
121 (nth 2 handler)))
122 gnus-demon-handlers))
123 (setq gnus-demon-idle-time 0)
124 (setq gnus-demon-idle-has-been-called nil)
125 (setq gnus-use-demon t)))
126
127 (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
128
129 (defun gnus-demon-cancel ()
130 "Cancel any Gnus daemons."
131 (interactive)
132 (when gnus-demon-timer
133 (nnheader-cancel-timer gnus-demon-timer))
134 (setq gnus-demon-timer nil
135 gnus-use-demon nil
136 gnus-demon-idle-has-been-called nil)
137 (condition-case ()
138 (nnheader-cancel-function-timers 'gnus-demon)
139 (error t)))
140
141 (defun gnus-demon-is-idle-p ()
142 "Whether Emacs is idle or not."
143 ;; We do this simply by comparing the 100 most recent keystrokes
144 ;; with the ones we had last time. If they are the same, one might
145 ;; guess that Emacs is indeed idle. This only makes sense if one
146 ;; calls this function seldom -- like once a minute, which is what
147 ;; we do here.
148 (let ((keys (recent-keys)))
149 (or (equal keys gnus-demon-last-keys)
150 (progn
151 (setq gnus-demon-last-keys keys)
152 nil))))
153
154 (defun gnus-demon-time-to-step (time)
155 "Find out how many seconds to TIME, which is on the form \"17:43\"."
156 (if (not (stringp time))
157 time
158 (let* ((now (current-time))
159 ;; obtain NOW as discrete components -- make a vector for speed
160 (nowParts (apply 'vector (decode-time now)))
161 ;; obtain THEN as discrete components
162 (thenParts (timezone-parse-time time))
163 (thenHour (string-to-int (elt thenParts 0)))
164 (thenMin (string-to-int (elt thenParts 1)))
165 ;; convert time as elements into number of seconds since EPOCH.
166 (then (encode-time 0
167 thenMin
168 thenHour
169 ;; If THEN is earlier than NOW, make it
170 ;; same time tomorrow. Doc for encode-time
171 ;; says that this is OK.
172 (+ (elt nowParts 3)
173 (if (or (< thenHour (elt nowParts 2))
174 (and (= thenHour (elt nowParts 2))
175 (<= thenMin (elt nowParts 1))))
176 1 0))
177 (elt nowParts 4)
178 (elt nowParts 5)
179 (elt nowParts 6)
180 (elt nowParts 7)
181 (elt nowParts 8)))
182 ;; calculate number of seconds between NOW and THEN
183 (diff (+ (* 65536 (- (car then) (car now)))
184 (- (cadr then) (cadr now)))))
185 ;; return number of timesteps in the number of seconds
186 (round (/ diff gnus-demon-timestep)))))
187
188 (defun gnus-demon ()
189 "The Gnus daemon that takes care of running all Gnus handlers."
190 ;; Increase or reset the time Emacs has been idle.
191 (if (gnus-demon-is-idle-p)
192 (incf gnus-demon-idle-time)
193 (setq gnus-demon-idle-time 0)
194 (setq gnus-demon-idle-has-been-called nil))
195 ;; Disable all daemonic stuff if we're in the minibuffer
196 (when (and (not (window-minibuffer-p (selected-window)))
197 (not gnus-inhibit-demon))
198 ;; Then we go through all the handler and call those that are
199 ;; sufficiently ripe.
200 (let ((handlers gnus-demon-handler-state)
201 (gnus-inhibit-demon t)
202 handler time idle)
203 (while handlers
204 (setq handler (pop handlers))
205 (cond
206 ((numberp (setq time (nth 1 handler)))
207 ;; These handlers use a regular timeout mechanism. We decrease
208 ;; the timer if it hasn't reached zero yet.
209 (unless (zerop time)
210 (setcar (nthcdr 1 handler) (decf time)))
211 (and (zerop time) ; If the timer now is zero...
212 ;; Test for appropriate idleness
213 (progn
214 (setq idle (nth 2 handler))
215 (cond
216 ((null idle) t) ; Don't care about idle.
217 ((numberp idle) ; Numerical idle...
218 (< idle gnus-demon-idle-time)) ; Idle timed out.
219 (t (< 0 gnus-demon-idle-time)))) ; Or just need to be idle.
220 ;; So we call the handler.
221 (progn
222 (ignore-errors (funcall (car handler)))
223 ;; And reset the timer.
224 (setcar (nthcdr 1 handler)
225 (gnus-demon-time-to-step
226 (nth 1 (assq (car handler) gnus-demon-handlers)))))))
227 ;; These are only supposed to be called when Emacs is idle.
228 ((null (setq idle (nth 2 handler)))
229 ;; We do nothing.
230 )
231 ((and (not (numberp idle))
232 (gnus-demon-is-idle-p))
233 ;; We want to call this handler each and every time that
234 ;; Emacs is idle.
235 (ignore-errors (funcall (car handler))))
236 (t
237 ;; We want to call this handler only if Emacs has been idle
238 ;; for a specified number of timesteps.
239 (and (not (memq (car handler) gnus-demon-idle-has-been-called))
240 (< idle gnus-demon-idle-time)
241 (gnus-demon-is-idle-p)
242 (progn
243 (ignore-errors (funcall (car handler)))
244 ;; Make sure the handler won't be called once more in
245 ;; this idle-cycle.
246 (push (car handler) gnus-demon-idle-has-been-called)))))))))
247
248 (defun gnus-demon-add-nocem ()
249 "Add daemonic NoCeM handling to Gnus."
250 (gnus-demon-add-handler 'gnus-demon-scan-nocem 60 30))
251
252 (defun gnus-demon-scan-nocem ()
253 "Scan NoCeM groups for NoCeM messages."
254 (save-window-excursion
255 (gnus-nocem-scan-groups)))
256
257 (defun gnus-demon-add-disconnection ()
258 "Add daemonic server disconnection to Gnus."
259 (gnus-demon-add-handler 'gnus-demon-close-connections nil 30))
260
261 (defun gnus-demon-close-connections ()
262 (save-window-excursion
263 (gnus-close-backends)))
264
265 (defun gnus-demon-add-nntp-close-connection ()
266 "Add daemonic nntp server disconnection to Gnus.
267 If no commands have gone out via nntp during the last five
268 minutes, the connection is closed."
269 (gnus-demon-add-handler 'gnus-demon-close-connections 5 nil))
270
271 (defun gnus-demon-nntp-close-connection ()
272 (save-window-excursion
273 (when (nnmail-time-less '(0 300)
274 (nnmail-time-since nntp-last-command-time))
275 (nntp-close-server))))
276
277 (defun gnus-demon-add-scanmail ()
278 "Add daemonic scanning of mail from the mail backends."
279 (gnus-demon-add-handler 'gnus-demon-scan-mail 120 60))
280
281 (defun gnus-demon-scan-mail ()
282 (save-window-excursion
283 (let ((servers gnus-opened-servers)
284 server)
285 (gnus-clear-inboxes-moved)
286 (while (setq server (car (pop servers)))
287 (and (gnus-check-backend-function 'request-scan (car server))
288 (or (gnus-server-opened server)
289 (gnus-open-server server))
290 (gnus-request-scan nil server))))))
291
292 (defun gnus-demon-add-rescan ()
293 "Add daemonic scanning of new articles from all backends."
294 (gnus-demon-add-handler 'gnus-demon-scan-news 120 60))
295
296 (defun gnus-demon-scan-news ()
297 (let ((win (current-window-configuration)))
298 (unwind-protect
299 (save-window-excursion
300 (save-excursion
301 (when (gnus-alive-p)
302 (save-excursion
303 (set-buffer gnus-group-buffer)
304 (gnus-group-get-new-news)))))
305 (set-window-configuration win))))
306
307 (defun gnus-demon-add-scan-timestamps ()
308 "Add daemonic updating of timestamps in empty newgroups."
309 (gnus-demon-add-handler 'gnus-demon-scan-timestamps nil 30))
310
311 (defun gnus-demon-scan-timestamps ()
312 "Set the timestamp on all newsgroups with no unread and no ticked articles."
313 (when (gnus-alive-p)
314 (let ((cur-time (current-time))
315 (newsrc (cdr gnus-newsrc-alist))
316 info group unread has-ticked)
317 (while (setq info (pop newsrc))
318 (setq group (gnus-info-group info)
319 unread (gnus-group-unread group)
320 has-ticked (cdr (assq 'tick (gnus-info-marks info))))
321 (when (and (numberp unread)
322 (= unread 0)
323 (not has-ticked))
324 (gnus-group-set-parameter group 'timestamp cur-time))))))
325
326 (provide 'gnus-demon)
327
328 ;;; gnus-demon.el ends here