(gnus-delay-article): Allow "01:23" time spec,
[gnus] / lisp / gnus-delay.el
1 ;;; gnus-delay.el --- Delayed posting of articles -*- coding: latin-1; -*-
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 ;;; Code:
28
29 (require 'nndraft)
30 (require 'gnus-draft)
31
32 (defvar gnus-delay-group "delayed"
33   "Group name for storing delayed articles.")
34
35 (defvar gnus-delay-header "X-Gnus-Delayed"
36   "Header name for storing info about delayed articles.")
37
38 (defvar gnus-delay-default-delay "3d"
39   "*Default length of delay.")
40
41 (defvar gnus-delay-default-hour 8
42   "*If deadline is given as date, then assume this time of day.")
43
44 (defun gnus-delay-article (delay)
45   "Delay this article by some time.
46 DELAY is a string, giving the length of the time.  Possible values are:
47
48 * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
49   weeks (`w'), months (`M'), or years (`Y');
50
51 * YYYY-MM-DD for a specific date.  The time of day is given by the
52   variable `gnus-delay-default-hour', minute and second are zero.
53
54 * hh:mm for a specific time.  Use 24h format.  If it is later than this
55   time, then the deadline is tomorrow, else today."
56   (interactive
57    (list (read-string
58           "Target date (YYYY-MM-DD) or length of delay (units in [mhdwMY]): "
59           gnus-delay-default-delay)))
60   (let (num unit days year month day hour minute deadline)
61     (cond ((string-match
62             "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
63             delay)
64            (setq year  (string-to-number (match-string 1 delay))
65                  month (string-to-number (match-string 2 delay))
66                  day   (string-to-number (match-string 3 delay)))
67            (setq deadline
68                  (message-make-date
69                   (encode-time 0 0      ; second and minute
70                                gnus-delay-default-hour
71                                day month year))))
72           ((string-match "\\([0-9]+\\):\\([0-9]+\\)" delay)
73            (setq hour   (string-to-number (match-string 1 delay))
74                  minute (string-to-number (match-string 2 delay)))
75            ;; Use current time, except...
76            (setq deadline (apply 'vector (decode-time (current-time))))
77            ;; ... for minute and hour.
78            (aset deadline 1 minute)
79            (aset deadline 2 hour)
80            ;; Convert to seconds.
81            (setq deadline (time-to-seconds (apply 'encode-time
82                                                   (append deadline nil))))
83            ;; If this time has passed already, add a day.
84            (when (< deadline (time-to-seconds (current-time)))
85              (setq deadline (+ 3600 deadline))) ;3600 secs/day
86            ;; Convert seconds to date header.
87            (setq deadline (message-make-date
88                            (seconds-to-time deadline))))
89           ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
90            (setq num (match-string 1 delay))
91            (setq unit (match-string 2 delay))
92            ;; Start from seconds, then multiply into needed units.
93            (setq num (string-to-number num))
94            (cond ((string= unit "Y")
95                   (setq delay (* num 60 60 24 365)))
96                  ((string= unit "M")
97                   (setq delay (* num 60 60 24 30)))
98                  ((string= unit "w")
99                   (setq delay (* num 60 60 24 7)))
100                  ((string= unit "d")
101                   (setq delay (* num 60 60 24)))
102                  ((string= unit "h")
103                   (setq delay (* num 60 60)))
104                  (t
105                   (setq delay (* num 60))))
106            (setq deadline (message-make-date
107                            (seconds-to-time (+ (time-to-seconds (current-time))
108                                                delay)))))
109           (t (error "Malformed delay `%s'" delay)))
110     (message-add-header (format "%s: %s" gnus-delay-header deadline)))
111   (set-buffer-modified-p t)
112   (nndraft-request-create-group gnus-delay-group)
113   (message-disassociate-draft)
114   (nndraft-request-associate-buffer gnus-delay-group)
115   (save-buffer 0)
116   (kill-buffer (current-buffer))
117   (message-do-actions message-postpone-actions))
118
119 (defun gnus-delay-send-drafts ()
120   "Send all the delayed messages that are due now."
121   (interactive)
122   (save-excursion
123     (let* ((group (format "nndraft:%s" gnus-delay-group))
124            articles
125            article deadline)
126       (gnus-activate-group group)
127       (setq articles (nndraft-articles))
128       (while (setq article (pop articles))
129         (gnus-request-head article group)
130         (set-buffer nntp-server-buffer)
131         (goto-char (point-min))
132         (if (re-search-forward
133              (concat "^" (regexp-quote gnus-delay-header) ":\\s-+")
134              nil t)
135             (progn
136               (setq deadline (nnheader-header-value))
137               (setq deadline (apply 'encode-time (parse-time-string deadline)))
138               (setq deadline (time-since deadline))
139               (when (and (>= (nth 0 deadline) 0)
140                          (>= (nth 1 deadline) 0))
141                 (message "Sending delayed article %d" article)
142                 (gnus-draft-send article group)
143                 (message "Sending delayed article %d...done" article)))
144           (message "Delay header missing for article %d" article))))))
145
146 ;;;###autoload
147 (defun gnus-delay-initialize (&optional no-keymap no-check)
148   "Initialize the gnus-delay package.
149 This sets up a key binding in `message-mode' to delay a message.
150 This tells Gnus to look for delayed messages after getting new news.
151
152 Key binding is skipped if optional arg NO-KEYMAP is non-nil.
153 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
154   (unless no-keymap
155     (require 'message)
156     (define-key message-mode-map "\C-c\n" 'gnus-delay-article))
157   (unless no-check
158     (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-drafts)))
159
160 (provide 'gnus-delay)
161 ;;; gnus-delay.el ends here