*** empty log message ***
[gnus] / lisp / nndraft.el
1 ;;; nndraft.el --- draft article access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'nnheader)
28 (require 'nnmh)
29 (eval-and-compile (require 'cl))
30
31 (eval-and-compile
32   (autoload 'mail-send-and-exit "sendmail"))
33
34 (defvar nndraft-directory nil)
35
36 \f
37
38 (defconst nndraft-version "nndraft 1.0")
39 (defvar nndraft-status-string "")
40
41 \f
42
43 (defvar nndraft-current-server nil)
44 (defvar nndraft-server-alist nil)
45 (defvar nndraft-server-variables 
46   (list
47    '(nndraft-directory nil)
48    '(nndraft-status-string "")
49    '(nndraft-group-alist)))
50
51 \f
52
53 ;;; Interface functions.
54
55
56 (defun nndraft-retrieve-headers (articles &optional group server fetch-old)
57   (save-excursion
58     (set-buffer nntp-server-buffer)
59     (erase-buffer)
60     (let* ((file nil)
61            (buf (get-buffer-create " *draft headers*"))
62            beg article)
63       (set-buffer buf)
64       (buffer-disable-undo (current-buffer))
65       (erase-buffer)
66       ;; We don't support fetching by Message-ID.
67       (if (stringp (car articles))
68           'headers
69         (while articles
70           (set-buffer buf)
71           (when (nndraft-request-article 
72                  (setq article (pop articles)) group server (current-buffer))
73             (goto-char (point-min))
74             (if (search-forward "\n\n" nil t)
75                 (forward-line -1)
76               (goto-char (point-max)))
77             (delete-region (point) (point-max))
78             (set-buffer nntp-server-buffer)
79             (goto-char (point-max))
80             (insert (format "221 %d Article retrieved.\n" article))
81             (insert-buffer-substring buf)
82             (insert ".\n")))
83
84         (nnheader-fold-continuation-lines)
85         'headers))))
86
87 (defun nndraft-open-server (server &optional defs)
88   (nnheader-init-server-buffer)
89   (if (equal server nndraft-current-server)
90       t
91     (if nndraft-current-server
92         (setq nndraft-server-alist 
93               (cons (list nndraft-current-server
94                           (nnheader-save-variables nndraft-server-variables))
95                     nndraft-server-alist)))
96     (let ((state (assoc server nndraft-server-alist)))
97       (if state 
98           (progn
99             (nnheader-restore-variables (nth 1 state))
100             (setq nndraft-server-alist (delq state nndraft-server-alist)))
101         (nnheader-set-init-variables nndraft-server-variables defs))
102       (or (assq 'nndraft-directory defs)
103           (setq nndraft-directory server)))
104     (setq nndraft-current-server server)))
105
106 (defun nndraft-close-server (&optional server)
107   t)
108
109 (defun nndraft-server-opened (&optional server)
110   (and nntp-server-buffer
111        (get-buffer nntp-server-buffer)
112        nndraft-current-server
113        (equal nndraft-current-server server)))
114
115 (defun nndraft-status-message (&optional server)
116   nndraft-status-string)
117
118 (defun nndraft-request-article (id &optional group server buffer)
119   (when (numberp id)
120     ;; We get the newest file of the auto-saved file and the 
121     ;; "real" file.
122     (let* ((file (nndraft-article-filename id))
123            (auto (nndraft-auto-save-file-name file))
124            (newest (if (file-newer-than-file-p file auto) file auto))
125            (nntp-server-buffer (or buffer nntp-server-buffer)))
126       (when (and (file-exists-p newest)
127                  (nnmail-find-file newest))
128         (save-excursion 
129           (set-buffer nntp-server-buffer)
130           (goto-char (point-min))
131           ;; If there's a mail header separator in this file, 
132           ;; we remove it.
133           (when (re-search-forward
134                  (concat "^" mail-header-separator "$") nil t)
135             (replace-match "" t t)))
136         t))))
137
138 (defun nndraft-request-restore-buffer (article &optional group server)
139   "Request a new buffer that is restored to the state of ARTICLE."
140   (let ((file (nndraft-article-filename article ".state"))
141         nndraft-point nndraft-mode nndraft-buffer-name)
142     (when (file-exists-p file)
143       (load file t t t)
144       (when nndraft-buffer-name
145         (set-buffer (get-buffer-create
146                      (generate-new-buffer-name nndraft-buffer-name)))
147         (nndraft-request-article article group server (current-buffer))
148         (funcall nndraft-mode)
149         (let ((gnus-verbose-backends nil))
150           (nndraft-request-expire-articles (list article) group server t))
151         (goto-char nndraft-point))
152       nndraft-buffer-name)))
153
154 (defun nndraft-request-update-info (group info &optional server)
155   (setcar (cdr (cdr info)) nil)
156   (when (nth 3 info)
157     (setcar (nthcdr 3 info) nil)))
158
159 (defun nndraft-request-associate-buffer (group)
160   "Associate the current buffer with some article in the draft group."
161   (let* ((gnus-verbose-backends nil)
162          (article (cdr (nndraft-request-accept-article group t 'noinsert)))
163          (file (nndraft-article-filename article)))
164     (setq buffer-file-name file)
165     (setq buffer-auto-save-file-name (make-auto-save-file-name))
166     (clear-visited-file-modtime)
167     article))
168
169 (defun nndraft-request-group (group &optional server dont-check)
170   (prog1
171       (nndraft-execute-nnmh-command
172        (` (nnmh-request-group group "" (, dont-check))))
173     (nnheader-report 'nndraft nnmh-status-string)))
174
175 (defun nndraft-request-list (&optional server dir)
176   (nndraft-execute-nnmh-command
177    (` (nnmh-request-list nil (, dir)))))
178
179 (defun nndraft-request-newgroups (date &optional server)
180   (nndraft-execute-nnmh-command
181    (` (nnmh-request-newgroups (, date) (, server)))))
182
183 (defun nndraft-request-post (&optional server)
184   (mail-send-and-exit nil))
185
186 (defun nndraft-request-expire-articles 
187   (articles group &optional server force)
188   (let ((res (nndraft-execute-nnmh-command
189               (` (nnmh-request-expire-articles
190                   (quote (, articles)) group (, server) (, force)))))
191         article)
192     ;; Delete all the "state" files of articles that have been expired.
193     (while articles
194       (unless (memq (setq article (pop articles)) res)
195         (let ((file (nndraft-article-filename article ".state"))
196               (auto (nndraft-auto-save-file-name
197                      (nndraft-article-filename article))))
198           (when (file-exists-p file)
199             (funcall nnmail-delete-file-function file))
200           (when (file-exists-p auto)
201             (funcall nnmail-delete-file-function auto)))))
202     res))
203
204 (defun nndraft-request-accept-article (group &optional last noinsert)
205   (let* ((point (point))
206          (mode major-mode)
207          (name (buffer-name))
208          (gnus-verbose-backends nil)
209          (gart (nndraft-execute-nnmh-command
210                 (` (nnmh-request-accept-article group (, last) noinsert))))
211          (state
212           (nndraft-article-filename (cdr gart) ".state")))
213     ;; Write the "state" file.
214     (save-excursion
215       (nnheader-set-temp-buffer " *draft state*")
216       (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
217                                     nndraft-point ,point
218                                     nndraft-buffer-name ,name)))
219       (write-region (point-min) (point-max) state nil 'silent)
220       (kill-buffer (current-buffer)))
221     gart))
222
223 (defun nndraft-close-group (group &optional server)
224   t)
225
226 (defun nndraft-request-create-group (group &optional server)
227   (if (file-exists-p nndraft-directory)
228       (if (file-directory-p nndraft-directory)
229           t
230         nil)
231     (condition-case ()
232         (progn
233           (make-directory nndraft-directory t)
234           t)
235       (file-error nil))))
236
237 \f
238 ;;; Low-Level Interface
239
240 (defun nndraft-execute-nnmh-command (command)
241   (let ((dir (expand-file-name nndraft-directory)))
242     (and (string-match "/$" dir)
243          (setq dir (substring dir 0 (match-beginning 0))))
244     (string-match "/[^/]+$" dir)
245     (let ((group (substring dir (1+ (match-beginning 0))))
246           (nnmh-directory (substring dir 0 (1+ (match-beginning 0))))
247           (nnmh-get-new-mail nil))
248       (eval command))))
249
250 (defun nndraft-article-filename (article &rest args)
251   (apply 'concat
252          (file-name-as-directory nndraft-directory)
253          (int-to-string article)
254          args))
255
256 (defun nndraft-auto-save-file-name (file)
257   (save-excursion
258     (prog1
259         (progn
260           (set-buffer (get-buffer-create " *draft tmp*"))
261           (setq buffer-file-name file)
262           (make-auto-save-file-name))
263       (kill-buffer (current-buffer)))))
264
265 (provide 'nndraft)
266
267 ;;; nndraft.el ends here