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