*** empty log message ***
[gnus] / lisp / gnus-demon.el
1 ;;; gnus-demon.el --- daemonic Gnus behaviour
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 (require 'gnus-load)
29 (require 'gnus-int)
30 (require 'nnheader)
31 (require 'gnus)
32 (require 'timer)
33
34 (defvar gnus-demon-handlers nil
35   "Alist of daemonic handlers to be run at intervals.
36 Each handler is a list on the form
37
38 \(FUNCTION TIME IDLE)
39
40 FUNCTION is the function to be called. 
41 TIME is the number of `gnus-demon-timestep's between each call.  
42 If nil, never call.  If t, call each `gnus-demon-timestep'.
43 If IDLE is t, only call if Emacs has been idle for a while.  If IDLE
44 is a number, only call when Emacs has been idle more than this number
45 of `gnus-demon-timestep's.  If IDLE is nil, don't care about
46 idleness.  If IDLE is a number and TIME is nil, then call once each
47 time Emacs has been idle for IDLE `gnus-demon-timestep's.")
48
49 (defvar gnus-demon-timestep 60
50   "*Number of seconds in each demon timestep.")
51
52 ;;; Internal variables.
53
54 (defvar gnus-demon-timer nil)
55 (defvar gnus-demon-idle-has-been-called nil)
56 (defvar gnus-demon-idle-time 0)
57 (defvar gnus-demon-handler-state nil)
58 (defvar gnus-demon-is-idle nil)
59 (defvar gnus-demon-last-keys nil) 
60
61 (eval-and-compile
62   (autoload 'timezone-parse-date "timezone")
63   (autoload 'timezone-make-arpa-date "timezone"))
64
65 ;;; Functions.
66
67 (defun gnus-demon-add-handler (function time idle)
68   "Add the handler FUNCTION to be run at TIME and IDLE."
69   ;; First remove any old handlers that use this function.
70   (gnus-demon-remove-handler function)
71   ;; Then add the new one.
72   (push (list function time idle) gnus-demon-handlers)
73   (gnus-demon-init))
74
75 (defun gnus-demon-remove-handler (function &optional no-init)
76   "Remove the handler FUNCTION from the list of handlers."
77   (setq gnus-demon-handlers 
78         (delq (assq function gnus-demon-handlers)
79               gnus-demon-handlers))
80   (or no-init (gnus-demon-init)))
81
82 (defun gnus-demon-init ()
83   "Initialize the Gnus daemon."
84   (interactive)
85   (gnus-demon-cancel)
86   (if (null gnus-demon-handlers)
87       () ; Nothing to do.
88     ;; Set up timer.
89     (setq gnus-demon-timer 
90           (nnheader-run-at-time 
91            gnus-demon-timestep gnus-demon-timestep 'gnus-demon))
92     ;; Reset control variables.
93     (setq gnus-demon-handler-state
94           (mapcar 
95            (lambda (handler)
96              (list (car handler) (gnus-demon-time-to-step (nth 1 handler))
97                    (nth 2 handler)))
98            gnus-demon-handlers))
99     (setq gnus-demon-idle-time 0)
100     (setq gnus-demon-idle-has-been-called nil)
101     (setq gnus-use-demon t)))
102
103 (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
104
105 (defun gnus-demon-cancel ()
106   "Cancel any Gnus daemons."
107   (interactive)
108   (and gnus-demon-timer
109        (nnheader-cancel-timer gnus-demon-timer))
110   (setq gnus-demon-timer nil
111         gnus-use-demon nil)
112   (nnheader-cancel-function-timers 'gnus-demon))
113
114 (defun gnus-demon-is-idle-p ()
115   "Whether Emacs is idle or not."
116   ;; We do this simply by comparing the 100 most recent keystrokes
117   ;; with the ones we had last time.  If they are the same, one might
118   ;; guess that Emacs is indeed idle.  This only makes sense if one
119   ;; calls this function seldom -- like once a minute, which is what
120   ;; we do here.
121   (let ((keys (recent-keys)))
122     (or (equal keys gnus-demon-last-keys)
123         (progn
124           (setq gnus-demon-last-keys keys)
125           nil))))
126
127 (defun gnus-demon-time-to-step (time)
128   "Find out how many seconds to TIME, which is on the form \"17:43\"."
129   (if (not (stringp time))
130       time
131     (let* ((date (current-time-string))
132            (dv (timezone-parse-date date))
133            (tdate (timezone-make-arpa-date 
134                    (string-to-number (aref dv 0))
135                    (string-to-number (aref dv 1))
136                    (string-to-number (aref dv 2)) time
137                    (or (aref dv 4) "UT")))
138            (nseconds (gnus-time-minus
139                       (gnus-encode-date tdate) (gnus-encode-date date))))
140       (round
141        (/ (if (< nseconds 0)
142               (+ nseconds (* 60 60 24))
143             nseconds) gnus-demon-timestep)))))
144
145 (defun gnus-demon ()
146   "The Gnus daemon that takes care of running all Gnus handlers."
147   ;; Increase or reset the time Emacs has been idle.
148   (if (gnus-demon-is-idle-p)
149       (incf gnus-demon-idle-time)
150     (setq gnus-demon-idle-time 0)
151     (setq gnus-demon-idle-has-been-called nil))
152   ;; Then we go through all the handler and call those that are
153   ;; sufficiently ripe.
154   (let ((handlers gnus-demon-handler-state)
155         handler time idle)
156     (while handlers
157       (setq handler (pop handlers))
158       (cond 
159        ((numberp (setq time (nth 1 handler)))
160         ;; These handlers use a regular timeout mechanism.  We decrease
161         ;; the timer if it hasn't reached zero yet.
162         (or (zerop time)
163             (setcar (nthcdr 1 handler) (decf time)))
164         (and (zerop time)               ; If the timer now is zero...
165              (or (not (setq idle (nth 2 handler))) ; Don't care about idle.
166                  (and (numberp idle)    ; Numerical idle...
167                       (< idle gnus-demon-idle-time)) ; Idle timed out.
168                  gnus-demon-is-idle)    ; Or just need to be idle.
169              ;; So we call the handler.
170              (progn
171                (funcall (car handler))
172                ;; And reset the timer.
173                (setcar (nthcdr 1 handler)
174                        (gnus-demon-time-to-step
175                         (nth 1 (assq (car handler) gnus-demon-handlers)))))))
176        ;; These are only supposed to be called when Emacs is idle. 
177        ((null (setq idle (nth 2 handler)))
178         ;; We do nothing.
179         )
180        ((not (numberp idle))
181         ;; We want to call this handler each and every time that
182         ;; Emacs is idle. 
183         (funcall (car handler)))
184        (t
185         ;; We want to call this handler only if Emacs has been idle
186         ;; for a specified number of timesteps.
187         (and (not (memq (car handler) gnus-demon-idle-has-been-called))
188              (< idle gnus-demon-idle-time)
189              (progn
190                (funcall (car handler))
191                ;; Make sure the handler won't be called once more in
192                ;; this idle-cycle.
193                (push (car handler) gnus-demon-idle-has-been-called))))))))
194
195 (defun gnus-demon-add-nocem ()
196   "Add daemonic NoCeM handling to Gnus."
197   (gnus-demon-add-handler 'gnus-demon-scan-nocem 60 t))
198
199 (defun gnus-demon-scan-nocem ()
200   "Scan NoCeM groups for NoCeM messages."
201   (gnus-nocem-scan-groups))
202
203 (defun gnus-demon-add-disconnection ()
204   "Add daemonic server disconnection to Gnus."
205   (gnus-demon-add-handler 'gnus-demon-close-connections nil 30))
206
207 (defun gnus-demon-close-connections ()
208   (gnus-close-backends))
209
210 (defun gnus-demon-add-scanmail ()
211   "Add daemonic scanning of mail from the mail backends."
212   (gnus-demon-add-handler 'gnus-demon-scan-mail 120 60))
213
214 (defun gnus-demon-scan-mail ()
215   (let ((servers gnus-opened-servers)
216         server)
217     (while (setq server (car (pop servers)))
218       (and (gnus-check-backend-function 'request-scan (car server))
219            (or (gnus-server-opened server)
220                (gnus-open-server server))
221            (gnus-request-scan nil server)))))
222
223 (defun gnus-demon-add-rescan ()
224   "Add daemonic scanning of new articles from all backends."
225   (gnus-demon-add-handler 'gnus-demon-scan-news 120 60))
226
227 (defun gnus-demon-scan-news ()
228   (when (gnus-alive-p)
229     (save-excursion
230       (set-buffer gnus-group-buffer)
231       (gnus-group-get-new-news))))
232
233 (provide 'gnus-demon)
234
235 ;;; gnus-demon.el ends here