Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; nndraft.el --- draft article access for Gnus |
23f87bed | 2 | |
ba318903 | 3 | ;; Copyright (C) 1995-2014 Free Software Foundation, Inc. |
eec82323 | 4 | |
6748645f | 5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
eec82323 LMI |
6 | ;; Keywords: news |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
5e809f55 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
eec82323 | 11 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
eec82323 LMI |
14 | |
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. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
eec82323 LMI |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;;; Code: | |
26 | ||
27 | (require 'nnheader) | |
6748645f LMI |
28 | (require 'nnmail) |
29 | (require 'gnus-start) | |
8db5f662 | 30 | (require 'gnus-group) |
eec82323 LMI |
31 | (require 'nnmh) |
32 | (require 'nnoo) | |
16409b0b | 33 | (require 'mm-util) |
23f87bed | 34 | (eval-when-compile (require 'cl)) |
eec82323 | 35 | |
5261b92e GM |
36 | ;; The nnoo-import at the end, I think. |
37 | (declare-function nndraft-request-list "nndraft" (&rest args) t) | |
8db5f662 | 38 | |
6748645f LMI |
39 | (nnoo-declare nndraft |
40 | nnmh) | |
eec82323 | 41 | |
6748645f LMI |
42 | (defvoo nndraft-directory (nnheader-concat gnus-directory "drafts/") |
43 | "Where nndraft will store its files." | |
44 | nnmh-directory) | |
eec82323 | 45 | |
01c52d31 MB |
46 | (defvar 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'.") | |
50 | ||
eec82323 LMI |
51 | \f |
52 | ||
6748645f LMI |
53 | (defvoo nndraft-current-group "" nil nnmh-current-group) |
54 | (defvoo nndraft-get-new-mail nil nil nnmh-get-new-mail) | |
55 | (defvoo nndraft-current-directory nil nil nnmh-current-directory) | |
56 | ||
eec82323 | 57 | (defconst nndraft-version "nndraft 1.0") |
6748645f | 58 | (defvoo nndraft-status-string "" nil nnmh-status-string) |
eec82323 LMI |
59 | |
60 | \f | |
61 | ||
62 | ;;; Interface functions. | |
63 | ||
64 | (nnoo-define-basics nndraft) | |
65 | ||
6748645f LMI |
66 | (deffoo nndraft-open-server (server &optional defs) |
67 | (nnoo-change-server 'nndraft server defs) | |
68 | (cond | |
69 | ((not (file-exists-p nndraft-directory)) | |
70 | (nndraft-close-server) | |
71 | (nnheader-report 'nndraft "No such file or directory: %s" | |
72 | nndraft-directory)) | |
73 | ((not (file-directory-p (file-truename nndraft-directory))) | |
74 | (nndraft-close-server) | |
75 | (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory)) | |
76 | (t | |
77 | (nnheader-report 'nndraft "Opened server %s using directory %s" | |
78 | server nndraft-directory) | |
79 | t))) | |
80 | ||
eec82323 | 81 | (deffoo nndraft-retrieve-headers (articles &optional group server fetch-old) |
6748645f | 82 | (nndraft-possibly-change-group group) |
20a673b2 | 83 | (with-current-buffer nntp-server-buffer |
eec82323 | 84 | (erase-buffer) |
758845a0 | 85 | (let (article lines chars) |
eec82323 LMI |
86 | ;; We don't support fetching by Message-ID. |
87 | (if (stringp (car articles)) | |
88 | 'headers | |
89 | (while articles | |
16409b0b | 90 | (narrow-to-region (point) (point)) |
eec82323 LMI |
91 | (when (nndraft-request-article |
92 | (setq article (pop articles)) group server (current-buffer)) | |
93 | (goto-char (point-min)) | |
94 | (if (search-forward "\n\n" nil t) | |
95 | (forward-line -1) | |
96 | (goto-char (point-max))) | |
758845a0 LMI |
97 | (setq lines (count-lines (point) (point-max)) |
98 | chars (- (point-max) (point))) | |
eec82323 | 99 | (delete-region (point) (point-max)) |
16409b0b | 100 | (goto-char (point-min)) |
eec82323 | 101 | (insert (format "221 %d Article retrieved.\n" article)) |
758845a0 | 102 | (insert (format "Lines: %d\nChars: %d\n" lines chars)) |
16409b0b GM |
103 | (widen) |
104 | (goto-char (point-max)) | |
eec82323 LMI |
105 | (insert ".\n"))) |
106 | ||
107 | (nnheader-fold-continuation-lines) | |
108 | 'headers)))) | |
109 | ||
eec82323 | 110 | (deffoo nndraft-request-article (id &optional group server buffer) |
6748645f | 111 | (nndraft-possibly-change-group group) |
eec82323 LMI |
112 | (when (numberp id) |
113 | ;; We get the newest file of the auto-saved file and the | |
114 | ;; "real" file. | |
115 | (let* ((file (nndraft-article-filename id)) | |
116 | (auto (nndraft-auto-save-file-name file)) | |
117 | (newest (if (file-newer-than-file-p file auto) file auto)) | |
118 | (nntp-server-buffer (or buffer nntp-server-buffer))) | |
119 | (when (and (file-exists-p newest) | |
16409b0b GM |
120 | (let ((nnmail-file-coding-system |
121 | (if (file-newer-than-file-p file auto) | |
23f87bed | 122 | (if (member group '("drafts" "delayed")) |
16409b0b GM |
123 | message-draft-coding-system |
124 | mm-text-coding-system) | |
125 | mm-auto-save-coding-system))) | |
126 | (nnmail-find-file newest))) | |
20a673b2 | 127 | (with-current-buffer nntp-server-buffer |
eec82323 LMI |
128 | (goto-char (point-min)) |
129 | ;; If there's a mail header separator in this file, | |
130 | ;; we remove it. | |
131 | (when (re-search-forward | |
23f87bed | 132 | (concat "^" (regexp-quote mail-header-separator) "$") nil t) |
eec82323 LMI |
133 | (replace-match "" t t))) |
134 | t)))) | |
135 | ||
136 | (deffoo nndraft-request-restore-buffer (article &optional group server) | |
137 | "Request a new buffer that is restored to the state of ARTICLE." | |
6748645f LMI |
138 | (nndraft-possibly-change-group group) |
139 | (when (nndraft-request-article article group server (current-buffer)) | |
140 | (message-remove-header "xref") | |
141 | (message-remove-header "lines") | |
23f87bed MB |
142 | ;; Articles in nndraft:queue are considered as sent messages. The |
143 | ;; Date field should be the time when they are sent. | |
144 | ;;(message-remove-header "date") | |
6748645f | 145 | t)) |
eec82323 LMI |
146 | |
147 | (deffoo nndraft-request-update-info (group info &optional server) | |
6748645f LMI |
148 | (nndraft-possibly-change-group group) |
149 | (gnus-info-set-read | |
150 | info | |
151 | (gnus-update-read-articles (gnus-group-prefixed-name group '(nndraft "")) | |
152 | (nndraft-articles) t)) | |
16409b0b GM |
153 | (let ((marks (nth 3 info))) |
154 | (when marks | |
155 | ;; Nix out all marks except the `unsend'-able article marks. | |
6748645f LMI |
156 | (setcar (nthcdr 3 info) |
157 | (if (assq 'unsend marks) | |
158 | (list (assq 'unsend marks)) | |
159 | nil)))) | |
eec82323 LMI |
160 | t) |
161 | ||
23f87bed MB |
162 | (defun nndraft-generate-headers () |
163 | (save-excursion | |
164 | (message-generate-headers | |
165 | (message-headers-to-generate | |
01c52d31 | 166 | nndraft-required-headers message-draft-headers nil)))) |
23f87bed | 167 | |
30b0f250 KY |
168 | (defun nndraft-update-unread-articles () |
169 | "Update groups' unread articles in the group buffer." | |
170 | (nndraft-request-list) | |
171 | (with-current-buffer gnus-group-buffer | |
bb9e218d KY |
172 | (let* ((groups (mapcar (lambda (elem) |
173 | (gnus-group-prefixed-name (car elem) | |
174 | (list 'nndraft ""))) | |
175 | (nnmail-get-active))) | |
176 | (gnus-group-marked (copy-sequence groups)) | |
39397fad KY |
177 | ;; Don't send delayed articles. |
178 | (gnus-get-new-news-hook nil) | |
bb9e218d KY |
179 | (inhibit-read-only t)) |
180 | (gnus-group-get-new-news-this-group nil t) | |
9937bef4 G |
181 | (save-excursion |
182 | (dolist (group groups) | |
183 | (unless (and gnus-permanently-visible-groups | |
184 | (string-match gnus-permanently-visible-groups | |
185 | group)) | |
186 | (gnus-group-goto-group group) | |
187 | (when (zerop (gnus-group-group-unread)) | |
188 | (gnus-delete-line)))))))) | |
30b0f250 | 189 | |
eec82323 LMI |
190 | (deffoo nndraft-request-associate-buffer (group) |
191 | "Associate the current buffer with some article in the draft group." | |
6748645f LMI |
192 | (nndraft-open-server "") |
193 | (nndraft-request-group group) | |
194 | (nndraft-possibly-change-group group) | |
195 | (let ((gnus-verbose-backends nil) | |
196 | (buf (current-buffer)) | |
16409b0b GM |
197 | article file) |
198 | (with-temp-buffer | |
199 | (insert-buffer-substring buf) | |
6748645f | 200 | (setq article (nndraft-request-accept-article |
16409b0b GM |
201 | group (nnoo-current-server 'nndraft) t 'noinsert) |
202 | file (nndraft-article-filename article))) | |
203 | (setq buffer-file-name (expand-file-name file) | |
204 | buffer-auto-save-file-name (make-auto-save-file-name)) | |
eec82323 | 205 | (clear-visited-file-modtime) |
4a43ee9b MB |
206 | (let ((hook (if (boundp 'write-contents-functions) |
207 | 'write-contents-functions | |
208 | 'write-contents-hooks))) | |
209 | (gnus-make-local-hook hook) | |
210 | (add-hook hook 'nndraft-generate-headers nil t)) | |
30b0f250 KY |
211 | (gnus-make-local-hook 'after-save-hook) |
212 | (add-hook 'after-save-hook 'nndraft-update-unread-articles nil t) | |
213 | (message-add-action '(nndraft-update-unread-articles) | |
214 | 'exit 'postpone 'kill) | |
eec82323 LMI |
215 | article)) |
216 | ||
286c4fc2 | 217 | (deffoo nndraft-request-group (group &optional server dont-check info) |
23f87bed MB |
218 | (nndraft-possibly-change-group group) |
219 | (unless dont-check | |
220 | (let* ((pathname (nnmail-group-pathname group nndraft-directory)) | |
221 | (file-name-coding-system nnmail-pathname-coding-system) | |
222 | dir file) | |
223 | (nnheader-re-read-dir pathname) | |
e9bd5782 | 224 | (setq dir (mapcar (lambda (name) (string-to-number (substring name 1))) |
23f87bed MB |
225 | (ignore-errors (directory-files |
226 | pathname nil "^#[0-9]+#$" t)))) | |
227 | (dolist (n dir) | |
228 | (unless (file-exists-p | |
229 | (setq file (expand-file-name (int-to-string n) pathname))) | |
230 | (rename-file (nndraft-auto-save-file-name file) file))))) | |
231 | (nnoo-parent-function 'nndraft | |
232 | 'nnmh-request-group | |
233 | (list group server dont-check))) | |
234 | ||
c9fc72fa | 235 | (deffoo nndraft-request-move-article (article group server accept-form |
01c52d31 | 236 | &optional last move-is-internal) |
23f87bed MB |
237 | (nndraft-possibly-change-group group) |
238 | (let ((buf (get-buffer-create " *nndraft move*")) | |
239 | result) | |
240 | (and | |
241 | (nndraft-request-article article group server) | |
20a673b2 | 242 | (with-current-buffer buf |
23f87bed MB |
243 | (erase-buffer) |
244 | (insert-buffer-substring nntp-server-buffer) | |
245 | (setq result (eval accept-form)) | |
246 | (kill-buffer (current-buffer)) | |
247 | result) | |
248 | (null (nndraft-request-expire-articles (list article) group server 'force)) | |
249 | result))) | |
250 | ||
6748645f LMI |
251 | (deffoo nndraft-request-expire-articles (articles group &optional server force) |
252 | (nndraft-possibly-change-group group) | |
253 | (let* ((nnmh-allow-delete-final t) | |
115694a3 KY |
254 | (nnmail-expiry-target 'delete) |
255 | ;; FIXME: If we want to move a draft message to an expiry group, | |
256 | ;; there are things to have to improve: | |
257 | ;; - Remove a header separator. | |
258 | ;; - Encode it, including attachments, into a MIME message. | |
259 | ;;(nnmail-expiry-target | |
260 | ;; (or (gnus-group-find-parameter | |
261 | ;; (gnus-group-prefixed-name group (list 'nndraft server)) | |
262 | ;; 'expiry-target t) | |
263 | ;; nnmail-expiry-target)) | |
6748645f LMI |
264 | (res (nnoo-parent-function 'nndraft |
265 | 'nnmh-request-expire-articles | |
266 | (list articles group server force))) | |
267 | article) | |
eec82323 LMI |
268 | ;; Delete all the "state" files of articles that have been expired. |
269 | (while articles | |
270 | (unless (memq (setq article (pop articles)) res) | |
6748645f | 271 | (let ((auto (nndraft-auto-save-file-name |
eec82323 | 272 | (nndraft-article-filename article)))) |
eec82323 | 273 | (when (file-exists-p auto) |
16409b0b GM |
274 | (funcall nnmail-delete-file-function auto))) |
275 | (dolist (backup | |
276 | (let ((kept-new-versions 1) | |
277 | (kept-old-versions 0)) | |
278 | (find-backup-file-name | |
279 | (nndraft-article-filename article)))) | |
280 | (when (file-exists-p backup) | |
281 | (funcall nnmail-delete-file-function backup))))) | |
eec82323 LMI |
282 | res)) |
283 | ||
284 | (deffoo nndraft-request-accept-article (group &optional server last noinsert) | |
6748645f LMI |
285 | (nndraft-possibly-change-group group) |
286 | (let ((gnus-verbose-backends nil)) | |
287 | (nnoo-parent-function 'nndraft 'nnmh-request-accept-article | |
288 | (list group server last noinsert)))) | |
eec82323 | 289 | |
16409b0b GM |
290 | (deffoo nndraft-request-replace-article (article group buffer) |
291 | (nndraft-possibly-change-group group) | |
292 | (let ((nnmail-file-coding-system | |
23f87bed MB |
293 | (if (member group '("drafts" "delayed")) |
294 | message-draft-coding-system | |
16409b0b GM |
295 | mm-text-coding-system))) |
296 | (nnoo-parent-function 'nndraft 'nnmh-request-replace-article | |
297 | (list article group buffer)))) | |
298 | ||
eec82323 | 299 | (deffoo nndraft-request-create-group (group &optional server args) |
6748645f LMI |
300 | (nndraft-possibly-change-group group) |
301 | (if (file-exists-p nndraft-current-directory) | |
302 | (if (file-directory-p nndraft-current-directory) | |
eec82323 LMI |
303 | t |
304 | nil) | |
305 | (condition-case () | |
306 | (progn | |
6748645f | 307 | (gnus-make-directory nndraft-current-directory) |
eec82323 LMI |
308 | t) |
309 | (file-error nil)))) | |
310 | ||
311 | \f | |
312 | ;;; Low-Level Interface | |
313 | ||
6748645f LMI |
314 | (defun nndraft-possibly-change-group (group) |
315 | (when (and group | |
316 | (not (equal group nndraft-current-group))) | |
317 | (nndraft-open-server "") | |
318 | (setq nndraft-current-group group) | |
319 | (setq nndraft-current-directory | |
320 | (nnheader-concat nndraft-directory group)))) | |
eec82323 LMI |
321 | |
322 | (defun nndraft-article-filename (article &rest args) | |
323 | (apply 'concat | |
6748645f | 324 | (file-name-as-directory nndraft-current-directory) |
eec82323 LMI |
325 | (int-to-string article) |
326 | args)) | |
327 | ||
328 | (defun nndraft-auto-save-file-name (file) | |
329 | (save-excursion | |
330 | (prog1 | |
331 | (progn | |
332 | (set-buffer (get-buffer-create " *draft tmp*")) | |
333 | (setq buffer-file-name file) | |
334 | (make-auto-save-file-name)) | |
335 | (kill-buffer (current-buffer))))) | |
336 | ||
6748645f LMI |
337 | (defun nndraft-articles () |
338 | "Return the list of messages in the group." | |
339 | (gnus-make-directory nndraft-current-directory) | |
340 | (sort | |
e9bd5782 | 341 | (mapcar 'string-to-number |
6748645f LMI |
342 | (directory-files nndraft-current-directory nil "\\`[0-9]+\\'" t)) |
343 | '<)) | |
344 | ||
345 | (nnoo-import nndraft | |
346 | (nnmh | |
347 | nnmh-retrieve-headers | |
348 | nnmh-request-group | |
349 | nnmh-close-group | |
394679ff | 350 | nnmh-request-list)) |
6748645f | 351 | |
eec82323 LMI |
352 | (provide 'nndraft) |
353 | ||
354 | ;;; nndraft.el ends here |