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