nndoc.el (nndoc-dissect-mime-parts-sub): Fix last change
[gnus] / lisp / gnus-delay.el
1 ;;; gnus-delay.el --- Delayed posting of articles
2
3 ;; Copyright (C) 2001-2013 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 part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Provide delayed posting of articles.
26
27 ;;; Todo:
28
29 ;; * `gnus-delay-send-queue' 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 (autoload 'parse-time-string "parse-time" nil nil)
39
40 (defgroup gnus-delay nil
41   "Arrange for sending postings later."
42   :version "22.1"
43   :group 'gnus)
44
45 (defcustom gnus-delay-group "delayed"
46   "Group name for storing delayed articles."
47   :type 'string
48   :group 'gnus-delay)
49
50 (defcustom gnus-delay-header "X-Gnus-Delayed"
51   "Header name for storing info about delayed articles."
52   :type 'string
53   :group 'gnus-delay)
54
55 (defcustom gnus-delay-default-delay "3d"
56   "*Default length of delay."
57   :type 'string
58   :group 'gnus-delay)
59
60 (defcustom gnus-delay-default-hour 8
61   "*If deadline is given as date, then assume this time of day."
62   :version "22.1"
63   :type 'integer
64   :group 'gnus-delay)
65
66 ;;;###autoload
67 (defun gnus-delay-article (delay)
68   "Delay this article by some time.
69 DELAY is a string, giving the length of the time.  Possible values are:
70
71 * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
72   weeks (`w'), months (`M'), or years (`Y');
73
74 * YYYY-MM-DD for a specific date.  The time of day is given by the
75   variable `gnus-delay-default-hour', minute and second are zero.
76
77 * hh:mm for a specific time.  Use 24h format.  If it is later than this
78   time, then the deadline is tomorrow, else today."
79   (interactive
80    (list (read-string
81           "Target date (YYYY-MM-DD), time (hh:mm), or length of delay (units in [mhdwMY]): "
82           gnus-delay-default-delay)))
83   ;; Allow spell checking etc.
84   (run-hooks 'message-send-hook)
85   (let (num unit days year month day hour minute deadline)
86     (cond ((string-match
87             "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
88             delay)
89            (setq year  (string-to-number (match-string 1 delay))
90                  month (string-to-number (match-string 2 delay))
91                  day   (string-to-number (match-string 3 delay)))
92            (setq deadline
93                  (message-make-date
94                   (encode-time 0 0      ; second and minute
95                                gnus-delay-default-hour
96                                day month year))))
97           ((string-match "\\([0-9]+\\):\\([0-9]+\\)" delay)
98            (setq hour   (string-to-number (match-string 1 delay))
99                  minute (string-to-number (match-string 2 delay)))
100            ;; Use current time, except...
101            (setq deadline (apply 'vector (decode-time (current-time))))
102            ;; ... for minute and hour.
103            (aset deadline 1 minute)
104            (aset deadline 2 hour)
105            ;; Convert to seconds.
106            (setq deadline (gnus-float-time (apply 'encode-time
107                                                   (append deadline nil))))
108            ;; If this time has passed already, add a day.
109            (when (< deadline (gnus-float-time))
110              (setq deadline (+ 86400 deadline))) ; 86400 secs/day
111            ;; Convert seconds to date header.
112            (setq deadline (message-make-date
113                            (seconds-to-time deadline))))
114           ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
115            (setq num (match-string 1 delay))
116            (setq unit (match-string 2 delay))
117            ;; Start from seconds, then multiply into needed units.
118            (setq num (string-to-number num))
119            (cond ((string= unit "Y")
120                   (setq delay (* num 60 60 24 365)))
121                  ((string= unit "M")
122                   (setq delay (* num 60 60 24 30)))
123                  ((string= unit "w")
124                   (setq delay (* num 60 60 24 7)))
125                  ((string= unit "d")
126                   (setq delay (* num 60 60 24)))
127                  ((string= unit "h")
128                   (setq delay (* num 60 60)))
129                  (t
130                   (setq delay (* num 60))))
131            (setq deadline (message-make-date
132                            (seconds-to-time (+ (gnus-float-time) delay)))))
133           (t (error "Malformed delay `%s'" delay)))
134     (message-add-header (format "%s: %s" gnus-delay-header deadline)))
135   (set-buffer-modified-p t)
136   ;; If group does not exist, create it.
137   (gnus-agent-queue-setup gnus-delay-group)
138   (message-disassociate-draft)
139   (nndraft-request-associate-buffer gnus-delay-group)
140   (save-buffer 0)
141   (kill-buffer (current-buffer))
142   (message-do-actions message-postpone-actions))
143
144 ;;;###autoload
145 (defun gnus-delay-send-queue ()
146   "Send all the delayed messages that are due now."
147   (interactive)
148   (save-excursion
149     (let* ((group (format "nndraft:%s" gnus-delay-group))
150            (message-send-hook (copy-sequence message-send-hook))
151            articles
152            article deadline)
153       (when (gnus-group-entry group)
154         (gnus-activate-group group)
155         (add-hook 'message-send-hook
156                   (lambda () (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: utf-8
192 ;; End:
193
194 ;;; gnus-delay.el ends here