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?
42 (defgroup gnus-delay nil
43 "Arrange for sending postings later."
46 (defcustom gnus-delay-group "delayed"
47 "Group name for storing delayed articles."
51 (defcustom gnus-delay-header "X-Gnus-Delayed"
52 "Header name for storing info about delayed articles."
56 (defcustom gnus-delay-default-delay "3d"
57 "*Default length of delay."
61 (defcustom gnus-delay-default-hour 8
62 "*If deadline is given as date, then assume this time of day."
68 (defun gnus-delay-article (delay)
69 "Delay this article by some time.
70 DELAY is a string, giving the length of the time. Possible values are:
72 * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
73 weeks (`w'), months (`M'), or years (`Y');
75 * YYYY-MM-DD for a specific date. The time of day is given by the
76 variable `gnus-delay-default-hour', minute and second are zero.
78 * hh:mm for a specific time. Use 24h format. If it is later than this
79 time, then the deadline is tomorrow, else today."
82 "Target date (YYYY-MM-DD) or length of delay (units in [mhdwMY]): "
83 gnus-delay-default-delay)))
84 (let (num unit days year month day hour minute deadline)
86 "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
88 (setq year (string-to-number (match-string 1 delay))
89 month (string-to-number (match-string 2 delay))
90 day (string-to-number (match-string 3 delay)))
93 (encode-time 0 0 ; second and minute
94 gnus-delay-default-hour
96 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" delay)
97 (setq hour (string-to-number (match-string 1 delay))
98 minute (string-to-number (match-string 2 delay)))
99 ;; Use current time, except...
100 (setq deadline (apply 'vector (decode-time (current-time))))
101 ;; ... for minute and hour.
102 (aset deadline 1 minute)
103 (aset deadline 2 hour)
104 ;; Convert to seconds.
105 (setq deadline (time-to-seconds (apply 'encode-time
106 (append deadline nil))))
107 ;; If this time has passed already, add a day.
108 (when (< deadline (time-to-seconds (current-time)))
109 (setq deadline (+ 3600 deadline))) ;3600 secs/day
110 ;; Convert seconds to date header.
111 (setq deadline (message-make-date
112 (seconds-to-time deadline))))
113 ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
114 (setq num (match-string 1 delay))
115 (setq unit (match-string 2 delay))
116 ;; Start from seconds, then multiply into needed units.
117 (setq num (string-to-number num))
118 (cond ((string= unit "Y")
119 (setq delay (* num 60 60 24 365)))
121 (setq delay (* num 60 60 24 30)))
123 (setq delay (* num 60 60 24 7)))
125 (setq delay (* num 60 60 24)))
127 (setq delay (* num 60 60)))
129 (setq delay (* num 60))))
130 (setq deadline (message-make-date
131 (seconds-to-time (+ (time-to-seconds (current-time))
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 (let ((group (format "nndraft:%s" gnus-delay-group)))
138 (gnus-agent-queue-setup gnus-delay-group))
139 (message-disassociate-draft)
140 (nndraft-request-associate-buffer gnus-delay-group)
142 (kill-buffer (current-buffer))
143 (message-do-actions message-postpone-actions))
146 (defun gnus-delay-send-queue ()
147 "Send all the delayed messages that are due now."
150 (let* ((group (format "nndraft:%s" gnus-delay-group))
151 (message-send-hook (copy-sequence message-send-hook))
154 (when (gnus-group-entry group)
155 (gnus-activate-group group)
156 (add-hook 'message-send-hook
158 (message-remove-header gnus-delay-header)))
159 (setq articles (nndraft-articles))
160 (while (setq article (pop articles))
161 (gnus-request-head article group)
162 (set-buffer nntp-server-buffer)
163 (goto-char (point-min))
164 (if (re-search-forward
165 (concat "^" (regexp-quote gnus-delay-header) ":\\s-+")
168 (setq deadline (nnheader-header-value))
169 (setq deadline (apply 'encode-time
170 (parse-time-string deadline)))
171 (setq deadline (time-since deadline))
172 (when (and (>= (nth 0 deadline) 0)
173 (>= (nth 1 deadline) 0))
174 (message "Sending delayed article %d" article)
175 (gnus-draft-send article group)
176 (message "Sending delayed article %d...done" article)))
177 (message "Delay header missing for article %d" article)))))))
180 (defun gnus-delay-initialize (&optional no-keymap no-check)
181 "Initialize the gnus-delay package.
182 This sets up a key binding in `message-mode' to delay a message.
183 This tells Gnus to look for delayed messages after getting new news.
185 The optional arg NO-KEYMAP is ignored.
186 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
188 (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-queue)))
190 (provide 'gnus-delay)
193 ;; coding: iso-8859-1
196 ;;; gnus-delay.el ends here