gnus-delay.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; gnus-delay.el --- Delayed posting of articles
  2. ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>
  4. ;; Keywords: mail, news, extensions
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Provide delayed posting of articles.
  18. ;;; Todo:
  19. ;; * `gnus-delay-send-queue' barfs when group does not exist.
  20. ;; * Integrate gnus-delay.el into the rest of Gnus automatically. How
  21. ;; should this be done? Basically, we need to do what
  22. ;; `gnus-delay-initialize' does. But in which files?
  23. ;;; Code:
  24. (require 'nndraft)
  25. (require 'gnus-draft)
  26. (autoload 'parse-time-string "parse-time" nil nil)
  27. (defgroup gnus-delay nil
  28. "Arrange for sending postings later."
  29. :version "22.1"
  30. :group 'gnus)
  31. (defcustom gnus-delay-group "delayed"
  32. "Group name for storing delayed articles."
  33. :type 'string
  34. :group 'gnus-delay)
  35. (defcustom gnus-delay-header "X-Gnus-Delayed"
  36. "Header name for storing info about delayed articles."
  37. :type 'string
  38. :group 'gnus-delay)
  39. (defcustom gnus-delay-default-delay "3d"
  40. "*Default length of delay."
  41. :type 'string
  42. :group 'gnus-delay)
  43. (defcustom gnus-delay-default-hour 8
  44. "*If deadline is given as date, then assume this time of day."
  45. :version "22.1"
  46. :type 'integer
  47. :group 'gnus-delay)
  48. ;;;###autoload
  49. (defun gnus-delay-article (delay)
  50. "Delay this article by some time.
  51. DELAY is a string, giving the length of the time. Possible values are:
  52. * <digits><units> for <units> in minutes (`m'), hours (`h'), days (`d'),
  53. weeks (`w'), months (`M'), or years (`Y');
  54. * YYYY-MM-DD for a specific date. The time of day is given by the
  55. variable `gnus-delay-default-hour', minute and second are zero.
  56. * hh:mm for a specific time. Use 24h format. If it is later than this
  57. time, then the deadline is tomorrow, else today."
  58. (interactive
  59. (list (read-string
  60. "Target date (YYYY-MM-DD), time (hh:mm), or length of delay (units in [mhdwMY]): "
  61. gnus-delay-default-delay)))
  62. (let (num unit days year month day hour minute deadline)
  63. (cond ((string-match
  64. "\\([0-9][0-9][0-9]?[0-9]?\\)-\\([0-9]+\\)-\\([0-9]+\\)"
  65. delay)
  66. (setq year (string-to-number (match-string 1 delay))
  67. month (string-to-number (match-string 2 delay))
  68. day (string-to-number (match-string 3 delay)))
  69. (setq deadline
  70. (message-make-date
  71. (encode-time 0 0 ; second and minute
  72. gnus-delay-default-hour
  73. day month year))))
  74. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" delay)
  75. (setq hour (string-to-number (match-string 1 delay))
  76. minute (string-to-number (match-string 2 delay)))
  77. ;; Use current time, except...
  78. (setq deadline (apply 'vector (decode-time (current-time))))
  79. ;; ... for minute and hour.
  80. (aset deadline 1 minute)
  81. (aset deadline 2 hour)
  82. ;; Convert to seconds.
  83. (setq deadline (gnus-float-time (apply 'encode-time
  84. (append deadline nil))))
  85. ;; If this time has passed already, add a day.
  86. (when (< deadline (gnus-float-time))
  87. (setq deadline (+ 86400 deadline))) ; 86400 secs/day
  88. ;; Convert seconds to date header.
  89. (setq deadline (message-make-date
  90. (seconds-to-time deadline))))
  91. ((string-match "\\([0-9]+\\)\\s-*\\([mhdwMY]\\)" delay)
  92. (setq num (match-string 1 delay))
  93. (setq unit (match-string 2 delay))
  94. ;; Start from seconds, then multiply into needed units.
  95. (setq num (string-to-number num))
  96. (cond ((string= unit "Y")
  97. (setq delay (* num 60 60 24 365)))
  98. ((string= unit "M")
  99. (setq delay (* num 60 60 24 30)))
  100. ((string= unit "w")
  101. (setq delay (* num 60 60 24 7)))
  102. ((string= unit "d")
  103. (setq delay (* num 60 60 24)))
  104. ((string= unit "h")
  105. (setq delay (* num 60 60)))
  106. (t
  107. (setq delay (* num 60))))
  108. (setq deadline (message-make-date
  109. (seconds-to-time (+ (gnus-float-time) delay)))))
  110. (t (error "Malformed delay `%s'" delay)))
  111. (message-add-header (format "%s: %s" gnus-delay-header deadline)))
  112. (set-buffer-modified-p t)
  113. ;; If group does not exist, create it.
  114. (gnus-agent-queue-setup gnus-delay-group)
  115. (message-disassociate-draft)
  116. (nndraft-request-associate-buffer gnus-delay-group)
  117. (save-buffer 0)
  118. (kill-buffer (current-buffer))
  119. (message-do-actions message-postpone-actions))
  120. ;;;###autoload
  121. (defun gnus-delay-send-queue ()
  122. "Send all the delayed messages that are due now."
  123. (interactive)
  124. (save-excursion
  125. (let* ((group (format "nndraft:%s" gnus-delay-group))
  126. (message-send-hook (copy-sequence message-send-hook))
  127. articles
  128. article deadline)
  129. (when (gnus-group-entry group)
  130. (gnus-activate-group group)
  131. (add-hook 'message-send-hook
  132. (lambda () (message-remove-header gnus-delay-header)))
  133. (setq articles (nndraft-articles))
  134. (while (setq article (pop articles))
  135. (gnus-request-head article group)
  136. (set-buffer nntp-server-buffer)
  137. (goto-char (point-min))
  138. (if (re-search-forward
  139. (concat "^" (regexp-quote gnus-delay-header) ":\\s-+")
  140. nil t)
  141. (progn
  142. (setq deadline (nnheader-header-value))
  143. (setq deadline (apply 'encode-time
  144. (parse-time-string deadline)))
  145. (setq deadline (time-since deadline))
  146. (when (and (>= (nth 0 deadline) 0)
  147. (>= (nth 1 deadline) 0))
  148. (message "Sending delayed article %d" article)
  149. (gnus-draft-send article group)
  150. (message "Sending delayed article %d...done" article)))
  151. (message "Delay header missing for article %d" article)))))))
  152. ;;;###autoload
  153. (defun gnus-delay-initialize (&optional no-keymap no-check)
  154. "Initialize the gnus-delay package.
  155. This sets up a key binding in `message-mode' to delay a message.
  156. This tells Gnus to look for delayed messages after getting new news.
  157. The optional arg NO-KEYMAP is ignored.
  158. Checking delayed messages is skipped if optional arg NO-CHECK is non-nil."
  159. (unless no-check
  160. (add-hook 'gnus-get-new-news-hook 'gnus-delay-send-queue)))
  161. (provide 'gnus-delay)
  162. ;; Local Variables:
  163. ;; coding: iso-8859-1
  164. ;; End:
  165. ;;; gnus-delay.el ends here