58b39529ea878e5b33ef8a92a775c95d0eb5caca
[gnus] / lisp / gnus-delay.el
1 ;;; gnus-delay.el --- Delayed posting of articles -*- coding: latin-1; -*-
2
3 ;; Copyright (C) 2001  Free Software Foundation, Inc.
4
5 ;; Author: Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
6 ;; Keywords: mail, news, extensions
7
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)
11 ;; any later version.
12
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.
17
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.
22
23 ;;; Commentary:
24
25 ;; Provide delayed posting of articles.
26
27 ;;; Code:
28
29 (require 'nndraft)
30 (require 'gnus-draft)
31
32 (defvar gnus-delay-group "delayed"
33   "Group name for storing delayed articles.")
34
35 (defvar gnus-delay-header "X-Gnus-Delayed"
36   "Header name for storing info about delayed articles.")
37
38 (defvar gnus-delay-default-delay "3d"
39   "*Default length of delay.")
40
41 (defun gnus-delay-article (delay)
42   "Delay this article by some time.
43 DELAY is a string, giving the length of the time.  Possible values are
44 like 3d (meaning 3 days) or 2w (meaning two weeks)."
45   (interactive
46    (list (read-string (format "Length of delay (default `%s'): "
47                               gnus-delay-default-delay)
48                       nil nil gnus-delay-default-delay nil)))
49   (let (num unit days deadline)
50     (unless (string-match "\\([0-9]+\\)\\s-*\\([dw]\\)" delay)
51       (error "Malformed delay `%s'" delay))
52     (setq num (match-string 1 delay))
53     (setq unit (match-string 2 delay))
54     (if (string= unit "w")
55         (setq delay (* 7 (string-to-number num)))
56       (setq delay (string-to-number num)))
57     (setq deadline (message-make-date
58                     (seconds-to-time (+ (time-to-seconds (current-time))
59                                         (* delay 24 60 60)))))
60     (message-add-header (format "%s: %s" gnus-delay-header deadline)))
61   (set-buffer-modified-p t)
62   (nndraft-request-create-group gnus-delay-group)
63   (message-disassociate-draft)
64   (nndraft-request-associate-buffer gnus-delay-group)
65   (save-buffer 0)
66   (kill-buffer (current-buffer))
67   (message-do-actions message-postpone-actions))
68
69 (defun gnus-delay-send-drafts ()
70   "Send all the delayed messages that are due now."
71   (interactive)
72   (save-excursion
73     (let* ((group (format "nndraft:%s" gnus-delay-group))
74            (articles (nndraft-articles))
75            article deadline)
76       (gnus-activate-group group)
77       (while (setq article (pop articles))
78         (gnus-request-head article group)
79         (set-buffer nntp-server-buffer)
80         (unless (re-search-forward
81                  (concat "^" (regexp-quote gnus-delay-header) ":\\s-+"))
82           (error "Couldn't find delay for article %d" article))
83         (setq deadline (nnheader-header-value))
84         (setq deadline (apply 'encode-time (parse-time-string deadline)))
85         (setq deadline (time-since deadline))
86         (when (and (>= (nth 0 deadline) 0)
87                    (>= (nth 1 deadline) 0))
88           (message "Sending article %d" article)
89           (gnus-draft-send article group)
90           (message "Sending article %d...done" article))))))
91
92 ;;;###autoload
93 (defun gnus-delay-initialize (&optional no-keymap no-check)
94   "Initialize the gnus-delay package.
95 This sets up a key binding in `message-mode' to delay a message.
96 This tells Gnus to look for delayed messages after getting new news.
97
98 Key binding is skipped if optional arg NO-KEYMAP is non-nil.
99 Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
100   (unless no-keymap
101     (require 'message)
102     (define-key message-mode-map (kbd "C-c C-j") 'gnus-delay-article))
103   (unless no-check
104     (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-drafts)))
105
106 (provide 'gnus-delay)
107 ;;; gnus-delay.el ends here