2 ;; Auto check for new incoming news and mails
3 ;; Autocheck is binded to "vs" to start it and to "ve" to end it in Group mode
5 ;; Copyright (c) Olivier Sirven
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation version 2.
13 ;; This program is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
18 ;; For a copy of the GNU General Public License, search the Internet,
19 ;; or write to the Free Software Foundation, Inc., 59 Temple Place,
20 ;; Suite 330, Boston, MA 02111-1307 USA
23 (defvar gnus-autocheck-version-number "0.2" "Gnus autocheck version number")
27 (defun gnus-autocheck-hourp(str)
28 "Returns non-nil value if STR is a valid time using format HH:MM"
34 (string-match "^\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)$" str))
35 (setq hour (string-to-number (match-string 1 str)))
36 (setq minute (string-to-number (match-string 2 str)))
45 (defgroup gnus-autocheck nil
46 "gnus-autocheck.el is an emacs package for interfacing with Gnus.
47 It makes then possible to define a time frame for gnus to automatically
48 check for new news and mails"
49 :version gnus-autocheck-version-number
52 (defcustom gnus-autocheck-interval
54 "Interval in seconds between two checks"
56 :group 'gnus-autocheck)
58 (defcustom gnus-autocheck-hook
60 "Use this hook to customize what needs to be done before actually checking
63 :group 'gnus-autocheck)
65 (defcustom gnus-autocheck-active-days
67 "List of days (from Sun to Sat) when the autocheck mode will be active.
68 For example you can definie it to be active on Saturday and Sunday.
70 Leave blank if you don't need it."
71 :type '(repeat (choice (cons :tag "Sunday" 'Sun)
72 (cons :tag "Monday" 'Mon)
73 (cons :tag "Tuesday" 'Tue)
74 (cons :tag "Wednesday" 'Wed)
75 (cons :tag "Thursday" 'Thu)
76 (cons :tag "Friday" 'Fri)
77 (cons :tag "Saturday" 'Sat)))
78 :group 'gnus-autocheck)
80 (defcustom gnus-autocheck-active-periods
82 "List of hours periods when the autocheck mode will be active.
83 For example you can define it to be active during 00:00 to 08:00
84 and during 19:00 to 23:59. Time format is HH:MM and symbol is expected
87 Leave blank if you don't need it."
88 :type '(repeat (list (restricted-sexp :tag "Start hour" :match-alternatives (gnus-autocheck-hourp))
89 (restricted-sexp :tag "Stop hour" :match-alternatives (gnus-autocheck-hourp))))
90 :group 'gnus-autocheck)
92 (defvar gnus-autocheck-timer
94 "Store the gnus autocheck timer refrence")
96 (defun gnus-autocheck-get-new-mails-news()
97 "Get all new mails both by calling offlineimap and gnus-group-get-new-news
98 You can use gnus-autocheck-hook hook to customize what needs to be done before
100 (message "autocheck: checking news ...")
101 (run-hooks 'gnus-autocheck-hook)
102 ;(gnus-group-get-new-news)
106 (defun gnus-auto-checkmails()
107 "Callback used auto check for new incoming emails during night hours"
108 (let ((current-day (substring (current-time-string) 0 3))
109 (current-hour (string-to-number (substring (current-time-string) 11 13)))
110 (current-minute (string-to-number (substring (current-time-string) 14 16))))
111 (if (and (or (not gnus-autocheck-active-days)
112 (loop for day-to-check in gnus-autocheck-active-days
113 thereis (string= day-to-check current-day)))
114 (or (not gnus-autocheck-active-periods)
115 (loop for period-to-check in gnus-autocheck-active-periods
116 thereis (let ((period-time-start-hour
117 (string-to-number (substring (nth 0 period-to-check) 0 2)))
118 (period-time-start-minute
119 (string-to-number (substring (nth 0 period-to-check) 3)))
120 (period-time-stop-hour
121 (string-to-number (substring (nth 1 period-to-check) 0 2)))
122 (period-time-stop-minute
123 (string-to-number (substring (nth 1 period-to-check) 3))))
124 (and (>= current-hour period-time-start-hour)
125 (or (= current-hour period-time-start-hour)
126 (>= current-minute period-time-start-minute))
127 (<= current-hour period-time-stop-hour)
128 (<= current-minute period-time-stop-minute))))))
129 (gnus-autocheck-get-new-mails-news))))
131 (defun gnus-autocheck-start(&optional start-at-time)
132 "Start auto check for news and mails"
135 (if (eq start-at-time nil)
136 (setq start-at-time gnus-autocheck-interval))
138 (if gnus-autocheck-timer
139 (message "autocheck is already enabled")
140 (setq gnus-autocheck-timer (run-at-time start-at-time gnus-autocheck-interval 'gnus-auto-checkmails))
141 (message "start autocheck")
142 (add-to-list 'global-mode-string "--[GnusAuto]" t 'equal)
146 (defun gnus-autocheck-stop()
147 "Stop auto check for news and mails"
150 (if (not gnus-autocheck-timer)
151 (message "autocheck is not enabled")
152 (cancel-timer gnus-autocheck-timer)
153 (setq gnus-autocheck-timer nil)
154 (message "stop autocheck")
155 (setq global-mode-string (loop for elt in global-mode-string
156 if (not (equal elt "--[GnusAuto]")) collect elt))
160 ;; bind autocheck to "vs" to start it and to "ve" to end it
161 (define-key gnus-group-mode-map (kbd "vs") '(lambda()
163 (gnus-autocheck-start 0)))
164 (define-key gnus-group-mode-map (kbd "ve") 'gnus-autocheck-stop)
166 (add-hook 'gnus-startup-hook 'gnus-autocheck-start)
167 (add-hook 'gnus-exit-gnus-hook 'gnus-autocheck-stop)
169 (provide 'gnus-autocheck)