1 ;;; gnus-delay.el --- Delayed posting of articles
3 ;; Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
5 ;; Author: Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
6 ;; Keywords: mail, news, extensions
8 ;; This file is part of GNU Emacs.
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
12 ;; by the Free Software Foundation; either version 2, or (at your
13 ;; option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Provide delayed posting of articles.
31 ;; * `gnus-delay-send-queue' barfs when group does not exist.
32 ;; * Integrate gnus-delay.el into the rest of Gnus automatically. How
33 ;; should this be done? Basically, we need to do what
34 ;; `gnus-delay-initialize' does. But in which files?
40 (autoload 'parse-time-string
"parse-time" nil nil
)
43 (defgroup gnus-delay nil
44 "Arrange for sending postings later."
48 (defcustom gnus-delay-group
"delayed"
49 "Group name for storing delayed articles."
53 (defcustom gnus-delay-header
"X-Gnus-Delayed"
54 "Header name for storing info about delayed articles."
58 (defcustom gnus-delay-default-delay
"3d"
59 "*Default length of delay."
63 (defcustom gnus-delay-default-hour
8
64 "*If deadline is given as date, then assume this time of day."
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:
74 * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
75 weeks (`w'), months (`M'), or years (`Y');
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.
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."
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
)
88 "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
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
)))
95 (encode-time 0 0 ; second and minute
96 gnus-delay-default-hour
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)))
123 (setq delay
(* num
60 60 24 30)))
125 (setq delay
(* num
60 60 24 7)))
127 (setq delay
(* num
60 60 24)))
129 (setq delay
(* num
60 60)))
131 (setq delay
(* num
60))))
132 (setq deadline
(message-make-date
133 (seconds-to-time (+ (time-to-seconds (current-time))
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
)
144 (kill-buffer (current-buffer))
145 (message-do-actions message-postpone-actions
))
148 (defun gnus-delay-send-queue ()
149 "Send all the delayed messages that are due now."
152 (let* ((group (format "nndraft:%s" gnus-delay-group
))
153 (message-send-hook (copy-sequence message-send-hook
))
156 (when (gnus-gethash group gnus-newsrc-hashtb
)
157 (gnus-activate-group group
)
158 (add-hook 'message-send-hook
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-+")
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
)))))))
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.
187 The optional arg NO-KEYMAP is ignored.
188 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
190 (add-hook 'gnus-get-new-news-hook
'gnus-delay-send-queue
)))
192 (provide 'gnus-delay
)
195 ;; coding: iso-8859-1
198 ;;; arch-tag: fb2ad634-a897-4142-a503-f5991ec2349d
199 ;;; gnus-delay.el ends here