12aa1e525c8801553ef4243310be40392b70748b
[gnus] / lisp / gnus-delay.el
1 ;;; gnus-delay.el --- Delayed posting of articles
2
3 ;; Copyright (C) 2001-2011
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
7 ;; Keywords: mail, news, extensions
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Provide delayed posting of articles.
27
28 ;;; Todo:
29
30 ;; * `gnus-delay-send-queue' barfs when group does not exist.
31 ;; * Integrate gnus-delay.el into the rest of Gnus automatically.  How
32 ;;   should this be done?  Basically, we need to do what
33 ;;   `gnus-delay-initialize' does.  But in which files?
34
35 ;;; Code:
36
37 (require 'nndraft)
38 (require 'gnus-draft)
39 (autoload 'parse-time-string "parse-time" nil nil)
40
41 (defgroup gnus-delay nil
42   "Arrange for sending postings later."
43   :version "22.1"
44   :group 'gnus)
45
46 (defcustom gnus-delay-group "delayed"
47   "Group name for storing delayed articles."
48   :type 'string
49   :group 'gnus-delay)
50
51 (defcustom gnus-delay-header "X-Gnus-Delayed"
52   "Header name for storing info about delayed articles."
53   :type 'string
54   :group 'gnus-delay)
55
56 (defcustom gnus-delay-default-delay "3d"
57   "*Default length of delay."
58   :type 'string
59   :group 'gnus-delay)
60
61 (defcustom gnus-delay-default-hour 8
62   "*If deadline is given as date, then assume this time of day."
63   :version "22.1"
64   :type 'integer
65   :group 'gnus-delay)
66
67 ;;;###autoload
68 (defun gnus-delay-article (delay)
69   "Delay this article by some time.
70 DELAY is a string, giving the length of the time.  Possible values are:
71
72 * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
73   weeks (`w'), months (`M'), or years (`Y');
74
75 * YYYY-MM-DD for a specific date.  The time of day is given by the
76   variable `gnus-delay-default-hour', minute and second are zero.
77
78 * hh:mm for a specific time.  Use 24h format.  If it is later than this
79   time, then the deadline is tomorrow, else today."
80   (interactive
81    (list (read-string
82           "Target date (YYYY-MM-DD) or length of delay (units in [mhdwMY]): "
83           gnus-delay-default-delay)))
84   (let (num unit days year month day hour minute deadline)
85     (cond ((string-match
86             "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
87             delay)
88            (setq year  (string-to-number (match-string 1 delay))
89                  month (string-to-number (match-string 2 delay))
90                  day   (string-to-number (match-string 3 delay)))
91            (setq deadline
92                  (message-make-date
93                   (encode-time 0 0      ; second and minute
94                                gnus-delay-default-hour
95                                day month year))))
96           ((string-match "\\([0-9]+\\):\\([0-9]+\\)" delay)
97            (setq hour   (string-to-number (match-string 1 delay))
98                  minute (string-to-number (match-string 2 delay)))
99            ;; Use current time, except...
100            (setq deadline (apply 'vector (decode-time (current-time))))
101            ;; ... for minute and hour.
102            (aset deadline 1 minute)
103            (aset deadline 2 hour)
104            ;; Convert to seconds.
105            (setq deadline (gnus-float-time (apply 'encode-time
106                                                   (append deadline nil))))
107            ;; If this time has passed already, add a day.
108            (when (< deadline (gnus-float-time))
109              (setq deadline (+ 3600 deadline))) ;3600 secs/day
110            ;; Convert seconds to date header.
111            (setq deadline (message-make-date
112                            (seconds-to-time deadline))))
113           ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
114            (setq num (match-string 1 delay))
115            (setq unit (match-string 2 delay))
116            ;; Start from seconds, then multiply into needed units.
117            (setq num (string-to-number num))
118            (cond ((string= unit "Y")
119                   (setq delay (* num 60 60 24 365)))
120                  ((string= unit "M")
121                   (setq delay (* num 60 60 24 30)))
122                  ((string= unit "w")
123                   (setq delay (* num 60 60 24 7)))
124                  ((string= unit "d")
125                   (setq delay (* num 60 60 24)))
126                  ((string= unit "h")
127                   (setq delay (* num 60 60)))
128                  (t
129                   (setq delay (* num 60))))
130            (setq deadline (message-make-date
131                            (seconds-to-time (+ (gnus-float-time) delay)))))
132           (t (error "Malformed delay `%s'" delay)))
133     (message-add-header (format "%s: %s" gnus-delay-header deadline)))
134   (set-buffer-modified-p t)
135   ;; If group does not exist, create it.
136   (gnus-agent-queue-setup gnus-delay-group)
137   (message-disassociate-draft)
138   (nndraft-request-associate-buffer gnus-delay-group)
139   (save-buffer 0)
140   (kill-buffer (current-buffer))
141   (message-do-actions message-postpone-actions))
142
143 ;;;###autoload
144 (defun gnus-delay-send-queue ()
145   "Send all the delayed messages that are due now."
146   (interactive)
147   (save-excursion
148     (let* ((group (format "nndraft:%s" gnus-delay-group))
149            (message-send-hook (copy-sequence message-send-hook))
150            articles
151            article deadline)
152       (when (gnus-group-entry group)
153         (gnus-activate-group group)
154         (add-hook 'message-send-hook
155                   '(lambda ()
156                      (message-remove-header gnus-delay-header)))
157         (setq articles (nndraft-articles))
158         (while (setq article (pop articles))
159           (gnus-request-head article group)
160           (set-buffer nntp-server-buffer)
161           (goto-char (point-min))
162           (if (re-search-forward
163                (concat "^" (regexp-quote gnus-delay-header) ":\\s-+")
164                nil t)
165               (progn
166                 (setq deadline (nnheader-header-value))
167                 (setq deadline (apply 'encode-time
168                                       (parse-time-string deadline)))
169                 (setq deadline (time-since deadline))
170                 (when (and (>= (nth 0 deadline) 0)
171                            (>= (nth 1 deadline) 0))
172                   (message "Sending delayed article %d" article)
173                   (gnus-draft-send article group)
174                   (message "Sending delayed article %d...done" article)))
175             (message "Delay header missing for article %d" article)))))))
176
177 ;;;###autoload
178 (defun gnus-delay-initialize (&optional no-keymap no-check)
179   "Initialize the gnus-delay package.
180 This sets up a key binding in `message-mode' to delay a message.
181 This tells Gnus to look for delayed messages after getting new news.
182
183 The optional arg NO-KEYMAP is ignored.
184 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
185   (unless no-check
186     (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-queue)))
187
188 (provide 'gnus-delay)
189
190 ;; Local Variables:
191 ;; coding: iso-8859-1
192 ;; End:
193
194 ;;; gnus-delay.el ends here