Fix variable name clobbering from previous patch.
[gnus] / lisp / gnus-draft.el
1 ;;; gnus-draft.el --- draft message support for Gnus
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-sum)
30 (require 'message)
31 (require 'gnus-msg)
32 (require 'nndraft)
33 (require 'gnus-agent)
34 (eval-when-compile (require 'cl))
35 (eval-when-compile
36   (when (featurep 'xemacs)
37     (require 'easy-mmode))) ; for `define-minor-mode'
38
39 ;;; Draft minor mode
40
41 (defvar gnus-draft-mode-map
42   (let ((map (make-sparse-keymap)))
43     (gnus-define-keys map
44      "Dt" gnus-draft-toggle-sending
45      "e"  gnus-draft-edit-message ;; Use `B w' for `gnus-summary-edit-article'
46      "De" gnus-draft-edit-message
47      "Ds" gnus-draft-send-message
48      "DS" gnus-draft-send-all-messages)
49     map))
50
51 (defun gnus-draft-make-menu-bar ()
52   (unless (boundp 'gnus-draft-menu)
53     (easy-menu-define
54      gnus-draft-menu gnus-draft-mode-map ""
55      '("Drafts"
56        ["Toggle whether to send" gnus-draft-toggle-sending t]
57        ["Edit" gnus-draft-edit-message t]
58        ["Send selected message(s)" gnus-draft-send-message t]
59        ["Send all messages" gnus-draft-send-all-messages t]
60        ["Delete draft" gnus-summary-delete-article t]))))
61
62 (define-minor-mode gnus-draft-mode
63   "Minor mode for providing a draft summary buffers.
64
65 \\{gnus-draft-mode-map}"
66   :lighter " Draft" :keymap gnus-draft-mode-map
67   (cond
68    ((not (derived-mode-p 'gnus-summary-mode)) (setq gnus-draft-mode nil))
69    (gnus-draft-mode
70     ;; Set up the menu.
71     (when (gnus-visual-p 'draft-menu 'menu)
72       (gnus-draft-make-menu-bar)))))
73
74 ;;; Commands
75
76 (defun gnus-draft-toggle-sending (article)
77   "Toggle whether to send an article or not."
78   (interactive (list (gnus-summary-article-number)))
79   (if (gnus-draft-article-sendable-p article)
80       (progn
81         (push article gnus-newsgroup-unsendable)
82         (gnus-summary-mark-article article gnus-unsendable-mark))
83     (setq gnus-newsgroup-unsendable
84           (delq article gnus-newsgroup-unsendable))
85     (gnus-summary-mark-article article gnus-unread-mark))
86   (gnus-summary-position-point))
87
88 (defun gnus-draft-edit-message ()
89   "Enter a mail/post buffer to edit and send the draft."
90   (interactive)
91   (let ((article (gnus-summary-article-number))
92         (group gnus-newsgroup-name))
93     (gnus-draft-check-draft-articles (list article))
94     (gnus-summary-mark-as-read article gnus-canceled-mark)
95     (gnus-draft-setup article group t)
96     (set-buffer-modified-p t)
97     (save-excursion
98       (save-restriction
99         (message-narrow-to-headers)
100         (message-remove-header "date")))
101     (let ((message-draft-headers
102            (delq 'Date (copy-sequence message-draft-headers))))
103       (save-buffer))
104     (let ((gnus-verbose-backends nil))
105       (gnus-request-expire-articles (list article) group t))
106     (push
107      `((lambda ()
108          (when (gnus-buffer-exists-p ,gnus-summary-buffer)
109            (save-excursion
110              (set-buffer ,gnus-summary-buffer)
111              (gnus-cache-possibly-remove-article ,article nil nil nil t)))))
112      message-send-actions)))
113
114 (defun gnus-draft-send-message (&optional n)
115   "Send the current draft(s).
116 Obeys the standard process/prefix convention."
117   (interactive "P")
118   (let* ((articles (gnus-summary-work-articles n))
119          (total (length articles))
120          article)
121     (gnus-draft-check-draft-articles articles)
122     (while (setq article (pop articles))
123       (gnus-summary-remove-process-mark article)
124       (unless (memq article gnus-newsgroup-unsendable)
125         (let ((message-sending-message
126                (format "Sending message %d of %d..."
127                        (- total (length articles)) total)))
128           (gnus-draft-send article gnus-newsgroup-name t))
129         (gnus-summary-mark-article article gnus-canceled-mark)))))
130
131 (defun gnus-draft-send (article &optional group interactive)
132   "Send message ARTICLE."
133   (let* ((is-queue (or (not group)
134                        (equal group "nndraft:queue")))
135          (message-syntax-checks (if interactive message-syntax-checks
136                                   'dont-check-for-anything-just-trust-me))
137          (message-hidden-headers nil)
138          (message-inhibit-body-encoding (or is-queue
139                                             message-inhibit-body-encoding))
140          (message-send-hook (and (not is-queue)
141                                  message-send-hook))
142          (message-setup-hook (and (not is-queue)
143                                   message-setup-hook))
144          (gnus-message-setup-hook (and (not is-queue)
145                                        gnus-message-setup-hook))
146          (message-signature (and (not is-queue)
147                                  message-signature))
148          (gnus-agent-queue-mail (and (not is-queue)
149                                      gnus-agent-queue-mail))
150          (rfc2047-encode-encoded-words nil)
151          type method move-to)
152     (gnus-draft-setup article (or group "nndraft:queue"))
153     ;; We read the meta-information that says how and where
154     ;; this message is to be sent.
155     (save-restriction
156       (message-narrow-to-headers)
157       (when (re-search-forward
158              (concat "^" (regexp-quote gnus-agent-target-move-group-header)
159                      ":") nil t)
160         (skip-syntax-forward "-")
161         (setq move-to (buffer-substring (point) (point-at-eol)))
162         (message-remove-header gnus-agent-target-move-group-header))
163       (goto-char (point-min))
164       (when (re-search-forward
165              (concat "^" (regexp-quote gnus-agent-meta-information-header) ":")
166              nil t)
167         (setq type (ignore-errors (read (current-buffer)))
168               method (ignore-errors (read (current-buffer))))
169         (message-remove-header gnus-agent-meta-information-header)))
170     ;; Let Agent restore any GCC lines and have message.el perform them.
171     (gnus-agent-restore-gcc)
172     ;; Then we send it.  If we have no meta-information, we just send
173     ;; it and let Message figure out how.
174     (when (and (or (null method)
175                    (gnus-server-opened method)
176                    (gnus-open-server method))
177                (if type
178                    (let ((message-this-is-news (eq type 'news))
179                          (message-this-is-mail (eq type 'mail))
180                          (gnus-post-method method)
181                          (message-post-method method))
182                      (if move-to
183                          (gnus-inews-do-gcc move-to)
184                        (message-send-and-exit)))
185                  (if move-to
186                      (gnus-inews-do-gcc move-to)
187                    (message-send-and-exit))))
188       (let ((gnus-verbose-backends nil))
189         (gnus-request-expire-articles
190          (list article) (or group "nndraft:queue") t)))))
191
192 (defun gnus-draft-send-all-messages ()
193   "Send all the sendable drafts."
194   (interactive)
195   (when (or
196          gnus-expert-user
197          (gnus-y-or-n-p
198           "Send all drafts? "))
199     (gnus-uu-mark-buffer)
200     (gnus-draft-send-message)))
201
202 (defun gnus-group-send-queue ()
203   "Send all sendable articles from the queue group."
204   (interactive)
205   (when (or gnus-plugged
206             (not gnus-agent-prompt-send-queue)
207             (gnus-y-or-n-p "Gnus is unplugged; really send queue? "))
208     (gnus-activate-group "nndraft:queue")
209     (save-excursion
210       (let* ((articles (nndraft-articles))
211              (unsendable (gnus-uncompress-range
212                           (cdr (assq 'unsend
213                                      (gnus-info-marks
214                                       (gnus-get-info "nndraft:queue"))))))
215              (gnus-posting-styles nil)
216              message-send-mail-partially-limit
217              (total (length articles))
218              article)
219         (while (setq article (pop articles))
220           (unless (memq article unsendable)
221             (let ((message-sending-message
222                    (format "Sending message %d of %d..."
223                            (- total (length articles)) total)))
224               (gnus-draft-send article))))))))
225
226 ;;;###autoload
227 (defun gnus-draft-reminder ()
228   "Reminder user if there are unsent drafts."
229   (interactive)
230   (if (gnus-alive-p)
231       (let (active)
232         (catch 'continue
233           (dolist (group '("nndraft:drafts" "nndraft:queue"))
234             (setq active (gnus-activate-group group))
235             (if (and active (>= (cdr active) (car active)))
236                 (if (y-or-n-p "There are unsent drafts.  Confirm to exit? ")
237                     (throw 'continue t)
238                   (error "Stop!"))))))))
239
240 (defcustom gnus-draft-setup-hook nil
241   "Hook run after setting up a draft buffer."
242   :group 'gnus-message
243   :version "23.1" ;; No Gnus
244   :type 'hook)
245
246 ;;; Utility functions
247
248 ;;;!!!If this is byte-compiled, it fails miserably.
249 ;;;!!!This is because `gnus-setup-message' uses uninterned symbols.
250 ;;;!!!This has been fixed in recent versions of Emacs and XEmacs,
251 ;;;!!!but for the time being, we'll just run this tiny function uncompiled.
252
253 (progn
254   (defun gnus-draft-setup (narticle group &optional restore)
255     (let (ga)
256       (gnus-setup-message 'forward
257         (let ((article narticle))
258           (message-mail)
259           (erase-buffer)
260           (if (not (gnus-request-restore-buffer article group))
261               (error "Couldn't restore the article")
262             (when (and restore
263                        (equal group "nndraft:queue"))
264               (mime-to-mml))
265             ;; Insert the separator.
266             (goto-char (point-min))
267             (search-forward "\n\n")
268             (forward-char -1)
269             (save-restriction
270               (narrow-to-region (point-min) (point))
271               (setq ga
272                     (message-fetch-field gnus-draft-meta-information-header)))
273             (insert mail-header-separator)
274             (forward-line 1)
275             (message-set-auto-save-file-name))))
276       (gnus-backlog-remove-article group narticle)
277       (when (and ga
278                  (ignore-errors (setq ga (car (read-from-string ga)))))
279         (setq gnus-newsgroup-name
280               (if (equal (car ga) "") nil (car ga)))
281         (gnus-configure-posting-styles)
282         (setq gnus-message-group-art (cons gnus-newsgroup-name (cadr ga)))
283         (setq message-post-method
284               `(lambda (arg)
285                  (gnus-post-method arg ,(car ga))))
286         (unless (equal (cadr ga) "")
287           (dolist (article (cdr ga))
288             (message-add-action
289              `(progn
290                 (gnus-add-mark ,(car ga) 'replied ,article)
291                 (gnus-request-set-mark ,(car ga) (list (list (list ,article)
292                                                              'add '(reply)))))
293              'send))))
294       (run-hooks 'gnus-draft-setup-hook))))
295
296 (defun gnus-draft-article-sendable-p (article)
297   "Say whether ARTICLE is sendable."
298   (not (memq article gnus-newsgroup-unsendable)))
299
300 (defun gnus-draft-check-draft-articles (articles)
301   "Check whether the draft articles ARTICLES are under edit."
302   (when (equal gnus-newsgroup-name "nndraft:drafts")
303     (let ((buffers (buffer-list))
304           file buffs buff)
305       (save-current-buffer
306         (while (and articles
307                     (not buff))
308           (setq file (nndraft-article-filename (pop articles))
309                 buffs buffers)
310           (while buffs
311             (set-buffer (setq buff (pop buffs)))
312             (if (and buffer-file-name
313                      (string-equal (file-truename buffer-file-name)
314                                    (file-truename file))
315                      (buffer-modified-p))
316                 (setq buffs nil)
317               (setq buff nil)))))
318       (when buff
319         (let* ((window (get-buffer-window buff t))
320                (frame (and window (window-frame window))))
321           (if frame
322               (gnus-select-frame-set-input-focus frame)
323             (pop-to-buffer buff t)))
324         (error "The draft %s is under edit" file)))))
325
326 (provide 'gnus-draft)
327
328 ;;; gnus-draft.el ends here