(gnus-delay-article): Fix `read-string' for
[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 (defun gnus-delay-article (delay)
42   "Delay this article by some time.
43 DELAY is a string, giving the length of the time.  Possible values are
44 <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
45 weeks (`w'), months (`M'), or years (`Y')."
46   (interactive
47    (list (read-string "Length of delay (units in [mhdwMY]): "
48                       gnus-delay-default-delay)))
49   (let (num unit days deadline)
50     (unless (string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
51       (error "Malformed delay `%s'" delay))
52     (setq num (match-string 1 delay))
53     (setq unit (match-string 2 delay))
54     ;; Start from seconds, then multiply into needed units.
55     (setq num (string-to-number num))
56     (cond ((string= unit "Y")
57            (setq delay (* num 60 60 24 365)))
58           ((string= unit "M")
59            (setq delay (* num 60 60 24 30)))
60           ((string= unit "w")
61            (setq delay (* num 60 60 24 7)))
62           ((string= unit "d")
63            (setq delay (* num 60 60 24)))
64           ((string= unit "h")
65            (setq delay (* num 60 60)))
66           (t
67            (setq delay (* num 60))))
68     (setq deadline (message-make-date
69                     (seconds-to-time (+ (time-to-seconds (current-time))
70                                         delay))))
71     (message-add-header (format "%s: %s" gnus-delay-header deadline)))
72   (set-buffer-modified-p t)
73   (nndraft-request-create-group gnus-delay-group)
74   (message-disassociate-draft)
75   (nndraft-request-associate-buffer gnus-delay-group)
76   (save-buffer 0)
77   (kill-buffer (current-buffer))
78   (message-do-actions message-postpone-actions))
79
80 (defun gnus-delay-send-drafts ()
81   "Send all the delayed messages that are due now."
82   (interactive)
83   (save-excursion
84     (let* ((group (format "nndraft:%s" gnus-delay-group))
85            (articles (nndraft-articles))
86            article deadline)
87       (gnus-activate-group group)
88       (while (setq article (pop articles))
89         (gnus-request-head article group)
90         (set-buffer nntp-server-buffer)
91         (unless (re-search-forward
92                  (concat "^" (regexp-quote gnus-delay-header) ":\\s-+"))
93           (error "Couldn't find delay for article %d" article))
94         (setq deadline (nnheader-header-value))
95         (setq deadline (apply 'encode-time (parse-time-string deadline)))
96         (setq deadline (time-since deadline))
97         (when (and (>= (nth 0 deadline) 0)
98                    (>= (nth 1 deadline) 0))
99           (message "Sending article %d" article)
100           (gnus-draft-send article group)
101           (message "Sending article %d...done" article))))))
102
103 ;;;###autoload
104 (defun gnus-delay-initialize (&optional no-keymap no-check)
105   "Initialize the gnus-delay package.
106 This sets up a key binding in `message-mode' to delay a message.
107 This tells Gnus to look for delayed messages after getting new news.
108
109 Key binding is skipped if optional arg NO-KEYMAP is non-nil.
110 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
111   (unless no-keymap
112     (require 'message)
113     (define-key message-mode-map (kbd "C-c C-j") 'gnus-delay-article))
114   (unless no-check
115     (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-drafts)))
116
117 (provide 'gnus-delay)
118 ;;; gnus-delay.el ends here