1 ;;; gnus-delay.el --- Delayed posting of articles -*- coding: latin-1; -*-
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;; Author: Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
6 ;; Keywords: mail, news, extensions
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)
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.
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.
25 ;; Provide delayed posting of articles.
29 ;; * `gnus-delay-send-drafts' 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?
40 (defgroup gnus-delay nil
41 "Arrange for sending postings later."
44 (defcustom gnus-delay-group "delayed"
45 "Group name for storing delayed articles."
49 (defcustom gnus-delay-header "X-Gnus-Delayed"
50 "Header name for storing info about delayed articles."
54 (defcustom gnus-delay-default-delay "3d"
55 "*Default length of delay."
59 (defcustom gnus-delay-default-hour 8
60 "*If deadline is given as date, then assume this time of day."
65 (defun gnus-delay-article (delay)
66 "Delay this article by some time.
67 DELAY is a string, giving the length of the time. Possible values are:
69 * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
70 weeks (`w'), months (`M'), or years (`Y');
72 * YYYY-MM-DD for a specific date. The time of day is given by the
73 variable `gnus-delay-default-hour', minute and second are zero.
75 * hh:mm for a specific time. Use 24h format. If it is later than this
76 time, then the deadline is tomorrow, else today."
79 "Target date (YYYY-MM-DD) or length of delay (units in [mhdwMY]): "
80 gnus-delay-default-delay)))
81 (let (num unit days year month day hour minute deadline)
83 "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
85 (setq year (string-to-number (match-string 1 delay))
86 month (string-to-number (match-string 2 delay))
87 day (string-to-number (match-string 3 delay)))
90 (encode-time 0 0 ; second and minute
91 gnus-delay-default-hour
93 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" delay)
94 (setq hour (string-to-number (match-string 1 delay))
95 minute (string-to-number (match-string 2 delay)))
96 ;; Use current time, except...
97 (setq deadline (apply 'vector (decode-time (current-time))))
98 ;; ... for minute and hour.
99 (aset deadline 1 minute)
100 (aset deadline 2 hour)
101 ;; Convert to seconds.
102 (setq deadline (time-to-seconds (apply 'encode-time
103 (append deadline nil))))
104 ;; If this time has passed already, add a day.
105 (when (< deadline (time-to-seconds (current-time)))
106 (setq deadline (+ 3600 deadline))) ;3600 secs/day
107 ;; Convert seconds to date header.
108 (setq deadline (message-make-date
109 (seconds-to-time deadline))))
110 ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
111 (setq num (match-string 1 delay))
112 (setq unit (match-string 2 delay))
113 ;; Start from seconds, then multiply into needed units.
114 (setq num (string-to-number num))
115 (cond ((string= unit "Y")
116 (setq delay (* num 60 60 24 365)))
118 (setq delay (* num 60 60 24 30)))
120 (setq delay (* num 60 60 24 7)))
122 (setq delay (* num 60 60 24)))
124 (setq delay (* num 60 60)))
126 (setq delay (* num 60))))
127 (setq deadline (message-make-date
128 (seconds-to-time (+ (time-to-seconds (current-time))
130 (t (error "Malformed delay `%s'" delay)))
131 (message-add-header (format "%s: %s" gnus-delay-header deadline)))
132 (set-buffer-modified-p t)
133 (nndraft-request-create-group gnus-delay-group)
134 (message-disassociate-draft)
135 (nndraft-request-associate-buffer gnus-delay-group)
137 (kill-buffer (current-buffer))
138 (message-do-actions message-postpone-actions))
141 (defun gnus-delay-send-drafts ()
142 "Send all the delayed messages that are due now."
145 (let* ((group (format "nndraft:%s" gnus-delay-group))
148 (when (gnus-gethash group gnus-newsrc-hashtb)
149 (gnus-activate-group group)
150 (setq articles (nndraft-articles))
151 (while (setq article (pop articles))
152 (gnus-request-head article group)
153 (set-buffer nntp-server-buffer)
154 (goto-char (point-min))
155 (if (re-search-forward
156 (concat "^" (regexp-quote gnus-delay-header) ":\\s-+")
159 (setq deadline (nnheader-header-value))
160 (setq deadline (apply 'encode-time
161 (parse-time-string deadline)))
162 (setq deadline (time-since deadline))
163 (when (and (>= (nth 0 deadline) 0)
164 (>= (nth 1 deadline) 0))
165 (message "Sending delayed article %d" article)
166 (gnus-draft-send article group)
167 (message "Sending delayed article %d...done" article)))
168 (message "Delay header missing for article %d" article)))))))
171 (defun gnus-delay-initialize (&optional no-keymap no-check)
172 "Initialize the gnus-delay package.
173 This sets up a key binding in `message-mode' to delay a message.
174 This tells Gnus to look for delayed messages after getting new news.
176 The optional arg NO-KEYMAP is ignored.
177 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
179 (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-drafts)))
181 (provide 'gnus-delay)
182 ;;; gnus-delay.el ends here