1 ;;; nndraft.el --- draft article access for Gnus
3 ;; Copyright (C) 1995-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
34 (eval-when-compile (require 'cl
))
36 ;; The nnoo-import at the end, I think.
37 (declare-function nndraft-request-list
"nndraft" (&rest args
) t
)
42 (defvoo nndraft-directory
(nnheader-concat gnus-directory
"drafts/")
43 "Where nndraft will store its files."
46 (defcustom nndraft-required-headers
'(Date)
47 "Headers to be generated when saving a draft message.
48 The headers in this variable and the ones in `message-required-headers'
49 are generated if and only if they are also in `message-draft-headers'."
51 :group
'message-headers
) ; FIXME wrong group
55 (defvoo nndraft-current-group
"" nil nnmh-current-group
)
56 (defvoo nndraft-get-new-mail nil nil nnmh-get-new-mail
)
57 (defvoo nndraft-current-directory nil nil nnmh-current-directory
)
59 (defconst nndraft-version
"nndraft 1.0")
60 (defvoo nndraft-status-string
"" nil nnmh-status-string
)
64 ;;; Interface functions.
66 (nnoo-define-basics nndraft
)
68 (deffoo nndraft-open-server
(server &optional defs
)
69 (nnoo-change-server 'nndraft server defs
)
71 ((not (file-exists-p nndraft-directory
))
72 (nndraft-close-server)
73 (nnheader-report 'nndraft
"No such file or directory: %s"
75 ((not (file-directory-p (file-truename nndraft-directory
)))
76 (nndraft-close-server)
77 (nnheader-report 'nndraft
"Not a directory: %s" nndraft-directory
))
79 (nnheader-report 'nndraft
"Opened server %s using directory %s"
80 server nndraft-directory
)
83 (deffoo nndraft-retrieve-headers
(articles &optional group server fetch-old
)
84 (nndraft-possibly-change-group group
)
85 (with-current-buffer nntp-server-buffer
87 (let (article lines chars
)
88 ;; We don't support fetching by Message-ID.
89 (if (stringp (car articles
))
92 (narrow-to-region (point) (point))
93 (when (nndraft-request-article
94 (setq article
(pop articles
)) group server
(current-buffer))
95 (goto-char (point-min))
96 (if (search-forward "\n\n" nil t
)
98 (goto-char (point-max)))
99 (setq lines
(count-lines (point) (point-max))
100 chars
(- (point-max) (point)))
101 (delete-region (point) (point-max))
102 (goto-char (point-min))
103 (insert (format "221 %d Article retrieved.\n" article
))
104 (insert (format "Lines: %d\nChars: %d\n" lines chars
))
106 (goto-char (point-max))
109 (nnheader-fold-continuation-lines)
112 (deffoo nndraft-request-article
(id &optional group server buffer
)
113 (nndraft-possibly-change-group group
)
115 ;; We get the newest file of the auto-saved file and the
117 (let* ((file (nndraft-article-filename id
))
118 (auto (nndraft-auto-save-file-name file
))
119 (newest (if (file-newer-than-file-p file auto
) file auto
))
120 (nntp-server-buffer (or buffer nntp-server-buffer
)))
121 (when (and (file-exists-p newest
)
122 (let ((nnmail-file-coding-system
123 (if (file-newer-than-file-p file auto
)
124 (if (member group
'("drafts" "delayed"))
125 message-draft-coding-system
126 mm-text-coding-system
)
127 mm-auto-save-coding-system
)))
128 (nnmail-find-file newest
)))
129 (with-current-buffer nntp-server-buffer
130 (goto-char (point-min))
131 ;; If there's a mail header separator in this file,
133 (when (re-search-forward
134 (concat "^" (regexp-quote mail-header-separator
) "$") nil t
)
135 (replace-match "" t t
)))
138 (deffoo nndraft-request-restore-buffer
(article &optional group server
)
139 "Request a new buffer that is restored to the state of ARTICLE."
140 (nndraft-possibly-change-group group
)
141 (when (nndraft-request-article article group server
(current-buffer))
142 (message-remove-header "xref")
143 (message-remove-header "lines")
144 ;; Articles in nndraft:queue are considered as sent messages. The
145 ;; Date field should be the time when they are sent.
146 ;;(message-remove-header "date")
149 (deffoo nndraft-request-update-info
(group info
&optional server
)
150 (nndraft-possibly-change-group group
)
153 (gnus-update-read-articles (gnus-group-prefixed-name group
'(nndraft ""))
154 (nndraft-articles) t
))
155 (let ((marks (nth 3 info
)))
157 ;; Nix out all marks except the `unsend'-able article marks.
158 (setcar (nthcdr 3 info
)
159 (if (assq 'unsend marks
)
160 (list (assq 'unsend marks
))
164 (defun nndraft-generate-headers ()
166 (message-generate-headers
167 (message-headers-to-generate
168 nndraft-required-headers message-draft-headers nil
))))
170 (defun nndraft-update-unread-articles ()
171 "Update groups' unread articles in the group buffer."
172 (nndraft-request-list)
173 (with-current-buffer gnus-group-buffer
174 (let* ((groups (mapcar (lambda (elem)
175 (gnus-group-prefixed-name (car elem
)
177 (nnmail-get-active)))
178 (gnus-group-marked (copy-sequence groups
))
179 ;; Don't send delayed articles.
180 (gnus-get-new-news-hook nil
)
181 (inhibit-read-only t
))
182 (gnus-group-get-new-news-this-group nil t
)
184 (dolist (group groups
)
185 (unless (and gnus-permanently-visible-groups
186 (string-match gnus-permanently-visible-groups
188 (gnus-group-goto-group group
)
189 (when (zerop (gnus-group-group-unread))
190 (gnus-delete-line))))))))
192 (deffoo nndraft-request-associate-buffer
(group)
193 "Associate the current buffer with some article in the draft group."
194 (nndraft-open-server "")
195 (nndraft-request-group group
)
196 (nndraft-possibly-change-group group
)
197 (let ((gnus-verbose-backends nil
)
198 (buf (current-buffer))
201 (insert-buffer-substring buf
)
202 (setq article
(nndraft-request-accept-article
203 group
(nnoo-current-server 'nndraft
) t
'noinsert
)
204 file
(nndraft-article-filename article
)))
205 (setq buffer-file-name
(expand-file-name file
)
206 buffer-auto-save-file-name
(make-auto-save-file-name))
207 (clear-visited-file-modtime)
208 (add-hook 'write-contents-functions
'nndraft-generate-headers nil t
)
209 (add-hook 'after-save-hook
'nndraft-update-unread-articles nil t
)
210 (message-add-action '(nndraft-update-unread-articles)
211 'exit
'postpone
'kill
)
214 (deffoo nndraft-request-group
(group &optional server dont-check info
)
215 (nndraft-possibly-change-group group
)
217 (let* ((pathname (nnmail-group-pathname group nndraft-directory
))
218 (file-name-coding-system nnmail-pathname-coding-system
)
220 (nnheader-re-read-dir pathname
)
221 (setq dir
(mapcar (lambda (name) (string-to-number (substring name
1)))
222 (ignore-errors (directory-files
223 pathname nil
"^#[0-9]+#$" t
))))
225 (unless (file-exists-p
226 (setq file
(expand-file-name (int-to-string n
) pathname
)))
227 (rename-file (nndraft-auto-save-file-name file
) file
)))))
228 (nnoo-parent-function 'nndraft
230 (list group server dont-check
)))
232 (deffoo nndraft-request-move-article
(article group server accept-form
233 &optional last move-is-internal
)
234 (nndraft-possibly-change-group group
)
235 (let ((buf (get-buffer-create " *nndraft move*"))
238 (nndraft-request-article article group server
)
239 (with-current-buffer buf
241 (insert-buffer-substring nntp-server-buffer
)
242 (setq result
(eval accept-form
))
243 (kill-buffer (current-buffer))
245 (null (nndraft-request-expire-articles (list article
) group server
'force
))
248 (deffoo nndraft-request-expire-articles
(articles group
&optional server force
)
249 (nndraft-possibly-change-group group
)
250 (let* ((nnmh-allow-delete-final t
)
251 (nnmail-expiry-target 'delete
)
252 ;; FIXME: If we want to move a draft message to an expiry group,
253 ;; there are things to have to improve:
254 ;; - Remove a header separator.
255 ;; - Encode it, including attachments, into a MIME message.
256 ;;(nnmail-expiry-target
257 ;; (or (gnus-group-find-parameter
258 ;; (gnus-group-prefixed-name group (list 'nndraft server))
260 ;; nnmail-expiry-target))
261 (res (nnoo-parent-function 'nndraft
262 'nnmh-request-expire-articles
263 (list articles group server force
)))
265 ;; Delete all the "state" files of articles that have been expired.
267 (unless (memq (setq article
(pop articles
)) res
)
268 (let ((auto (nndraft-auto-save-file-name
269 (nndraft-article-filename article
))))
270 (when (file-exists-p auto
)
271 (funcall nnmail-delete-file-function auto
)))
273 (let ((kept-new-versions 1)
274 (kept-old-versions 0))
275 (find-backup-file-name
276 (nndraft-article-filename article
))))
277 (when (file-exists-p backup
)
278 (funcall nnmail-delete-file-function backup
)))))
281 (deffoo nndraft-request-accept-article
(group &optional server last noinsert
)
282 (nndraft-possibly-change-group group
)
283 (let ((gnus-verbose-backends nil
))
284 (nnoo-parent-function 'nndraft
'nnmh-request-accept-article
285 (list group server last noinsert
))))
287 (deffoo nndraft-request-replace-article
(article group buffer
)
288 (nndraft-possibly-change-group group
)
289 (let ((nnmail-file-coding-system
290 (if (member group
'("drafts" "delayed"))
291 message-draft-coding-system
292 mm-text-coding-system
)))
293 (nnoo-parent-function 'nndraft
'nnmh-request-replace-article
294 (list article group buffer
))))
296 (deffoo nndraft-request-create-group
(group &optional server args
)
297 (nndraft-possibly-change-group group
)
298 (if (file-exists-p nndraft-current-directory
)
299 (if (file-directory-p nndraft-current-directory
)
304 (gnus-make-directory nndraft-current-directory
)
309 ;;; Low-Level Interface
311 (defun nndraft-possibly-change-group (group)
313 (not (equal group nndraft-current-group
)))
314 (nndraft-open-server "")
315 (setq nndraft-current-group group
)
316 (setq nndraft-current-directory
317 (nnheader-concat nndraft-directory group
))))
319 (defun nndraft-article-filename (article &rest args
)
321 (file-name-as-directory nndraft-current-directory
)
322 (int-to-string article
)
325 (defun nndraft-auto-save-file-name (file)
329 (set-buffer (get-buffer-create " *draft tmp*"))
330 (setq buffer-file-name file
)
331 (make-auto-save-file-name))
332 (kill-buffer (current-buffer)))))
334 (defun nndraft-articles ()
335 "Return the list of messages in the group."
336 (gnus-make-directory nndraft-current-directory
)
338 (mapcar 'string-to-number
339 (directory-files nndraft-current-directory nil
"\\`[0-9]+\\'" t
))
344 nnmh-retrieve-headers
351 ;;; nndraft.el ends here