gnus-demon.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ;;; gnus-demon.el --- daemonic Gnus behavior
  2. ;; Copyright (C) 1995-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'gnus)
  20. (require 'gnus-int)
  21. (require 'nnheader)
  22. (require 'nntp)
  23. (require 'nnmail)
  24. (defgroup gnus-demon nil
  25. "Demonic behavior."
  26. :group 'gnus)
  27. (defcustom gnus-demon-handlers nil
  28. "Alist of daemonic handlers to be run at intervals.
  29. Each handler is a list on the form
  30. \(FUNCTION TIME IDLE)
  31. FUNCTION is the function to be called. TIME is the number of
  32. `gnus-demon-timestep's between each call.
  33. If nil, never call. If t, call each `gnus-demon-timestep'.
  34. If IDLE is t, only call each time Emacs has been idle for TIME.
  35. If IDLE is a number, only call when Emacs has been idle more than
  36. this number of `gnus-demon-timestep's.
  37. If IDLE is nil, don't care about idleness.
  38. If IDLE is a number and TIME is nil, then call once each time
  39. Emacs has been idle for IDLE `gnus-demon-timestep's."
  40. :group 'gnus-demon
  41. :type '(repeat (list function
  42. (choice :tag "Time"
  43. (const :tag "never" nil)
  44. (const :tag "one" t)
  45. (integer :tag "steps" 1))
  46. (choice :tag "Idle"
  47. (const :tag "don't care" nil)
  48. (const :tag "for a while" t)
  49. (integer :tag "steps" 1)))))
  50. (defcustom gnus-demon-timestep 60
  51. "Number of seconds in each demon timestep."
  52. :group 'gnus-demon
  53. :type 'integer)
  54. ;;; Internal variables.
  55. (defvar gnus-demon-timers nil
  56. "List of idle timers which are running.")
  57. (defvar gnus-inhibit-demon nil
  58. "If non-nil, no daemonic function will be run.")
  59. ;;; Functions.
  60. (defun gnus-demon-add-handler (function time idle)
  61. "Add the handler FUNCTION to be run at TIME and IDLE."
  62. ;; First remove any old handlers that use this function.
  63. (gnus-demon-remove-handler function)
  64. ;; Then add the new one.
  65. (push (list function time idle) gnus-demon-handlers)
  66. (gnus-demon-init))
  67. (defun gnus-demon-remove-handler (function &optional no-init)
  68. "Remove the handler FUNCTION from the list of handlers."
  69. (gnus-alist-pull function gnus-demon-handlers)
  70. (unless no-init
  71. (gnus-demon-init)))
  72. (defun gnus-demon-idle-since ()
  73. "Return the number of seconds since when Emacs is idle."
  74. (if (featurep 'xemacs)
  75. (itimer-time-difference (current-time) last-command-event-time)
  76. (float-time (or (current-idle-time)
  77. '(0 0 0)))))
  78. (defun gnus-demon-run-callback (func &optional idle)
  79. "Run FUNC if Emacs has been idle for longer than IDLE seconds."
  80. (unless gnus-inhibit-demon
  81. (when (or (not idle)
  82. (and (eq idle t) (> (gnus-demon-idle-since) 0))
  83. (<= idle (gnus-demon-idle-since)))
  84. (with-local-quit
  85. (ignore-errors
  86. (funcall func))))))
  87. (defun gnus-demon-init ()
  88. "Initialize the Gnus daemon."
  89. (interactive)
  90. (gnus-demon-cancel)
  91. (dolist (handler gnus-demon-handlers)
  92. ;; Set up the timer.
  93. (let* ((func (nth 0 handler))
  94. (time (nth 1 handler))
  95. (time-type (type-of time))
  96. (idle (nth 2 handler))
  97. ;; Compute time according with timestep.
  98. ;; If t, replace by 1
  99. (time (cond ((eq time t)
  100. gnus-demon-timestep)
  101. ((null time)
  102. nil)
  103. ((stringp time)
  104. (* (gnus-demon-time-to-step time) gnus-demon-timestep))
  105. (t
  106. (* time gnus-demon-timestep))))
  107. (idle (if (numberp idle)
  108. (* idle gnus-demon-timestep)
  109. idle))
  110. (timer
  111. (cond
  112. ;; (func nil number)
  113. ;; Only call when Emacs has been idle for `idle'
  114. ((and (null time) (numberp idle))
  115. (run-with-idle-timer idle t 'gnus-demon-run-callback func))
  116. ;; (func number any)
  117. ;; Call every `time'
  118. ((eq time-type 'integer)
  119. (run-with-timer time time 'gnus-demon-run-callback func idle))
  120. ;; (func string any)
  121. ((eq time-type 'string)
  122. (run-with-timer time (* 24 60 60) 'gnus-demon-run-callback func idle)))))
  123. (when timer
  124. (add-to-list 'gnus-demon-timers timer)))))
  125. (defun gnus-demon-time-to-step (time)
  126. "Find out how many steps to TIME, which is on the form \"17:43\"."
  127. (let* ((now (current-time))
  128. ;; obtain NOW as discrete components -- make a vector for speed
  129. (nowParts (decode-time now))
  130. ;; obtain THEN as discrete components
  131. (thenParts (parse-time-string time))
  132. (thenHour (elt thenParts 2))
  133. (thenMin (elt thenParts 1))
  134. ;; convert time as elements into number of seconds since EPOCH.
  135. (then (encode-time 0
  136. thenMin
  137. thenHour
  138. ;; If THEN is earlier than NOW, make it
  139. ;; same time tomorrow. Doc for encode-time
  140. ;; says that this is OK.
  141. (+ (elt nowParts 3)
  142. (if (or (< thenHour (elt nowParts 2))
  143. (and (= thenHour (elt nowParts 2))
  144. (<= thenMin (elt nowParts 1))))
  145. 1 0))
  146. (elt nowParts 4)
  147. (elt nowParts 5)
  148. (elt nowParts 6)
  149. (elt nowParts 7)
  150. (elt nowParts 8)))
  151. ;; calculate number of seconds between NOW and THEN
  152. (diff (+ (* 65536 (- (car then) (car now)))
  153. (- (cadr then) (cadr now)))))
  154. ;; return number of timesteps in the number of seconds
  155. (round (/ diff gnus-demon-timestep))))
  156. (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
  157. (defun gnus-demon-cancel ()
  158. "Cancel any Gnus daemons."
  159. (interactive)
  160. (dolist (timer gnus-demon-timers)
  161. (nnheader-cancel-timer timer))
  162. (setq gnus-demon-timers nil))
  163. (defun gnus-demon-add-disconnection ()
  164. "Add daemonic server disconnection to Gnus."
  165. (gnus-demon-add-handler 'gnus-demon-close-connections nil 30))
  166. (defun gnus-demon-close-connections ()
  167. (save-window-excursion
  168. (gnus-close-backends)))
  169. (defun gnus-demon-add-nntp-close-connection ()
  170. "Add daemonic nntp server disconnection to Gnus.
  171. If no commands have gone out via nntp during the last five
  172. minutes, the connection is closed."
  173. (gnus-demon-add-handler 'gnus-demon-nntp-close-connection 5 nil))
  174. (defun gnus-demon-nntp-close-connection ()
  175. (save-window-excursion
  176. (when (time-less-p '(0 300) (time-since nntp-last-command-time))
  177. (nntp-close-server))))
  178. (defun gnus-demon-add-scanmail ()
  179. "Add daemonic scanning of mail from the mail backends."
  180. (gnus-demon-add-handler 'gnus-demon-scan-mail 120 60))
  181. (defun gnus-demon-scan-mail ()
  182. (save-window-excursion
  183. (let ((servers gnus-opened-servers)
  184. server
  185. (nnmail-fetched-sources (list t)))
  186. (while (setq server (car (pop servers)))
  187. (and (gnus-check-backend-function 'request-scan (car server))
  188. (or (gnus-server-opened server)
  189. (gnus-open-server server))
  190. (gnus-request-scan nil server))))))
  191. (defun gnus-demon-add-rescan ()
  192. "Add daemonic scanning of new articles from all backends."
  193. (gnus-demon-add-handler 'gnus-demon-scan-news 120 60))
  194. (defun gnus-demon-scan-news ()
  195. (let ((win (current-window-configuration)))
  196. (unwind-protect
  197. (save-window-excursion
  198. (when (gnus-alive-p)
  199. (with-current-buffer gnus-group-buffer
  200. (gnus-group-get-new-news))))
  201. (set-window-configuration win))))
  202. (defun gnus-demon-add-scan-timestamps ()
  203. "Add daemonic updating of timestamps in empty newgroups."
  204. (gnus-demon-add-handler 'gnus-demon-scan-timestamps nil 30))
  205. (defun gnus-demon-scan-timestamps ()
  206. "Set the timestamp on all newsgroups with no unread and no ticked articles."
  207. (when (gnus-alive-p)
  208. (let ((cur-time (current-time))
  209. (newsrc (cdr gnus-newsrc-alist))
  210. info group unread has-ticked)
  211. (while (setq info (pop newsrc))
  212. (setq group (gnus-info-group info)
  213. unread (gnus-group-unread group)
  214. has-ticked (cdr (assq 'tick (gnus-info-marks info))))
  215. (when (and (numberp unread)
  216. (= unread 0)
  217. (not has-ticked))
  218. (gnus-group-set-parameter group 'timestamp cur-time))))))
  219. (provide 'gnus-demon)
  220. ;;; gnus-demon.el ends here