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.
32 (defvar gnus-delay-group "delayed"
33 "Group name for storing delayed articles.")
35 (defvar gnus-delay-header "X-Gnus-Delayed"
36 "Header name for storing info about delayed articles.")
38 (defvar gnus-delay-default-delay "3d"
39 "*Default length of delay.")
41 (defvar gnus-delay-default-hour 8
42 "*If deadline is given as date, then assume this time of day.")
44 (defun gnus-delay-article (delay)
45 "Delay this article by some time.
46 DELAY is a string, giving the length of the time. Possible values are:
48 * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
49 weeks (`w'), months (`M'), or years (`Y');
51 * YYYY-MM-DD for a specific date. The time of day is given by the
52 variable `gnus-delay-default-hour', minute and second are zero."
55 "Target date (YYYY-MM-DD) or length of delay (units in [mhdwMY]): "
56 gnus-delay-default-delay)))
57 (let (num unit days year month day deadline)
59 "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
61 (setq year (string-to-number (match-string 1 delay))
62 month (string-to-number (match-string 2 delay))
63 day (string-to-number (match-string 3 delay)))
66 (encode-time 0 0 ; second and minute
67 gnus-delay-default-hour
69 ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
70 (setq num (match-string 1 delay))
71 (setq unit (match-string 2 delay))
72 ;; Start from seconds, then multiply into needed units.
73 (setq num (string-to-number num))
74 (cond ((string= unit "Y")
75 (setq delay (* num 60 60 24 365)))
77 (setq delay (* num 60 60 24 30)))
79 (setq delay (* num 60 60 24 7)))
81 (setq delay (* num 60 60 24)))
83 (setq delay (* num 60 60)))
85 (setq delay (* num 60))))
86 (setq deadline (message-make-date
87 (seconds-to-time (+ (time-to-seconds (current-time))
89 (t (error "Malformed delay `%s'" delay)))
90 (message-add-header (format "%s: %s" gnus-delay-header deadline)))
91 (set-buffer-modified-p t)
92 (nndraft-request-create-group gnus-delay-group)
93 (message-disassociate-draft)
94 (nndraft-request-associate-buffer gnus-delay-group)
96 (kill-buffer (current-buffer))
97 (message-do-actions message-postpone-actions))
99 (defun gnus-delay-send-drafts ()
100 "Send all the delayed messages that are due now."
103 (let* ((group (format "nndraft:%s" gnus-delay-group))
104 (articles (nndraft-articles))
106 (gnus-activate-group group)
107 (while (setq article (pop articles))
108 (gnus-request-head article group)
109 (set-buffer nntp-server-buffer)
110 (goto-char (point-min))
111 (if (re-search-forward
112 (concat "^" (regexp-quote gnus-delay-header) ":\\s-+")
115 (setq deadline (nnheader-header-value))
116 (setq deadline (apply 'encode-time (parse-time-string deadline)))
117 (setq deadline (time-since deadline))
118 (when (time-less-p (apply 'encode-time
119 (parse-time-string deadline))
121 (message "Sending article %d" article)
122 (gnus-draft-send article group)
123 (message "Sending article %d...done" article)))
124 (message "Delay header missing for article %d" article))))))
127 (defun gnus-delay-initialize (&optional no-keymap no-check)
128 "Initialize the gnus-delay package.
129 This sets up a key binding in `message-mode' to delay a message.
130 This tells Gnus to look for delayed messages after getting new news.
132 Key binding is skipped if optional arg NO-KEYMAP is non-nil.
133 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
136 (define-key message-mode-map "\C-c\n" 'gnus-delay-article))
138 (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-drafts)))
140 (provide 'gnus-delay)
141 ;;; gnus-delay.el ends here