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