Cleaning up leading and trailing spaces.
[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   (interactive
54    (list (read-string
55           "Target date (YYYY-MM-DD) or length of delay (units in [mhdwMY]): "
56           gnus-delay-default-delay)))
57   (let (num unit days year month day deadline)
58     (cond ((string-match
59             "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
60             delay)
61            (setq year  (string-to-number (match-string 1 delay))
62                  month (string-to-number (match-string 2 delay))
63                  day   (string-to-number (match-string 3 delay)))
64            (setq deadline
65                  (message-make-date
66                   (encode-time 0 0      ; second and minute
67                                gnus-delay-default-hour
68                                day month year))))
69           ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
70            (setq num (match-string 1 delay))
71            (setq unit (match-string 2 delay))
72            ;; Start from seconds, then multiply into needed units.
73            (setq num (string-to-number num))
74            (cond ((string= unit "Y")
75                   (setq delay (* num 60 60 24 365)))
76                  ((string= unit "M")
77                   (setq delay (* num 60 60 24 30)))
78                  ((string= unit "w")
79                   (setq delay (* num 60 60 24 7)))
80                  ((string= unit "d")
81                   (setq delay (* num 60 60 24)))
82                  ((string= unit "h")
83                   (setq delay (* num 60 60)))
84                  (t
85                   (setq delay (* num 60))))
86            (setq deadline (message-make-date
87                            (seconds-to-time (+ (time-to-seconds (current-time))
88                                                delay)))))
89           (t (error "Malformed delay `%s'" delay)))
90     (message-add-header (format "%s: %s" gnus-delay-header deadline)))
91   (set-buffer-modified-p t)
92   (nndraft-request-create-group gnus-delay-group)
93   (message-disassociate-draft)
94   (nndraft-request-associate-buffer gnus-delay-group)
95   (save-buffer 0)
96   (kill-buffer (current-buffer))
97   (message-do-actions message-postpone-actions))
98
99 (defun gnus-delay-send-drafts ()
100   "Send all the delayed messages that are due now."
101   (interactive)
102   (save-excursion
103     (let* ((group (format "nndraft:%s" gnus-delay-group))
104            (articles (nndraft-articles))
105            article deadline)
106       (gnus-activate-group group)
107       (while (setq article (pop articles))
108         (gnus-request-head article group)
109         (set-buffer nntp-server-buffer)
110         (unless (re-search-forward
111                  (concat "^" (regexp-quote gnus-delay-header) ":\\s-+"))
112           (error "Couldn't find delay for article %d" article))
113         (setq deadline (nnheader-header-value))
114         (setq deadline (apply 'encode-time (parse-time-string deadline)))
115         (setq deadline (time-since deadline))
116         (when (and (>= (nth 0 deadline) 0)
117                    (>= (nth 1 deadline) 0))
118           (message "Sending article %d" article)
119           (gnus-draft-send article group)
120           (message "Sending article %d...done" article))))))
121
122 ;;;###autoload
123 (defun gnus-delay-initialize (&optional no-keymap no-check)
124   "Initialize the gnus-delay package.
125 This sets up a key binding in `message-mode' to delay a message.
126 This tells Gnus to look for delayed messages after getting new news.
127
128 Key binding is skipped if optional arg NO-KEYMAP is non-nil.
129 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
130   (unless no-keymap
131     (require 'message)
132     (define-key message-mode-map (kbd "C-c C-j") 'gnus-delay-article))
133   (unless no-check
134     (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-drafts)))
135
136 (provide 'gnus-delay)
137 ;;; gnus-delay.el ends here