1 ;;; nndraft.el --- draft article access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
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)
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.
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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
30 (eval-and-compile (require 'cl))
33 (autoload 'mail-send-and-exit "sendmail"))
35 (defvar nndraft-directory nil
36 "Where nndraft will store its directory.")
40 (defconst nndraft-version "nndraft 1.0")
41 (defvar nndraft-status-string "")
45 (defvar nndraft-current-server nil)
46 (defvar nndraft-server-alist nil)
47 (defvar nndraft-server-variables
48 `((nndraft-directory nil)
49 (nndraft-status-string "")
50 (nndraft-group-alist)))
54 ;;; Interface functions.
57 (defun nndraft-retrieve-headers (articles &optional group server fetch-old)
59 (set-buffer nntp-server-buffer)
62 (buf (get-buffer-create " *draft headers*"))
65 (buffer-disable-undo (current-buffer))
67 ;; We don't support fetching by Message-ID.
68 (if (stringp (car articles))
72 (when (nndraft-request-article
73 (setq article (pop articles)) group server (current-buffer))
74 (goto-char (point-min))
75 (if (search-forward "\n\n" nil t)
77 (goto-char (point-max)))
78 (delete-region (point) (point-max))
79 (set-buffer nntp-server-buffer)
80 (goto-char (point-max))
81 (insert (format "221 %d Article retrieved.\n" article))
82 (insert-buffer-substring buf)
85 (nnheader-fold-continuation-lines)
88 (defun nndraft-open-server (server &optional defs)
89 (nnheader-change-server 'nndraft server defs)
90 (unless (assq 'nndraft-directory defs)
91 (setq nndraft-directory server))
93 ((not (file-exists-p nndraft-directory))
94 (nndraft-close-server)
95 (nnheader-report 'nndraft "No such file or directory: %s"
97 ((not (file-directory-p (file-truename nndraft-directory)))
98 (nndraft-close-server)
99 (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
101 (nnheader-report 'nndraft "Opened server %s using directory %s"
102 server nndraft-directory)
105 (defun nndraft-close-server (&optional server)
106 (setq nndraft-current-server nil)
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)))
115 (defun nndraft-status-message (&optional server)
116 nndraft-status-string)
118 (defun nndraft-request-article (id &optional group server buffer)
120 ;; We get the newest file of the auto-saved file and the
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))
129 (set-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 "^" mail-header-separator "$") nil t)
135 (replace-match "" t t)))
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)
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)))
154 (defun nndraft-request-update-info (group info &optional server)
155 (setcar (cdr (cdr info)) nil)
157 (setcar (nthcdr 3 info) nil)))
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)
169 (defun nndraft-request-group (group &optional server dont-check)
171 (nndraft-execute-nnmh-command
172 `(nnmh-request-group group "" ,dont-check))
173 (nnheader-report 'nndraft nnmh-status-string)))
175 (defun nndraft-request-list (&optional server dir)
176 (nndraft-execute-nnmh-command
177 `(nnmh-request-list nil ,dir)))
179 (defun nndraft-request-newgroups (date &optional server)
180 (nndraft-execute-nnmh-command
181 `(nnmh-request-newgroups ,date ,server)))
183 (defun nndraft-request-expire-articles
184 (articles group &optional server force)
185 (let ((res (nndraft-execute-nnmh-command
186 `(nnmh-request-expire-articles
187 ',articles group ,server ,force)))
189 ;; Delete all the "state" files of articles that have been expired.
191 (unless (memq (setq article (pop articles)) res)
192 (let ((file (nndraft-article-filename article ".state"))
193 (auto (nndraft-auto-save-file-name
194 (nndraft-article-filename article))))
195 (when (file-exists-p file)
196 (funcall nnmail-delete-file-function file))
197 (when (file-exists-p auto)
198 (funcall nnmail-delete-file-function auto)))))
201 (defun nndraft-request-accept-article (group &optional last noinsert)
202 (let* ((point (point))
205 (gnus-verbose-backends nil)
206 (gart (nndraft-execute-nnmh-command
207 `(nnmh-request-accept-article group ,last noinsert)))
209 (nndraft-article-filename (cdr gart) ".state")))
210 ;; Write the "state" file.
212 (nnheader-set-temp-buffer " *draft state*")
213 (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
215 nndraft-buffer-name ,name)))
216 (write-region (point-min) (point-max) state nil 'silent)
217 (kill-buffer (current-buffer)))
220 (defun nndraft-close-group (group &optional server)
223 (defun nndraft-request-create-group (group &optional server)
224 (if (file-exists-p nndraft-directory)
225 (if (file-directory-p nndraft-directory)
230 (make-directory nndraft-directory t)
235 ;;; Low-Level Interface
237 (defun nndraft-execute-nnmh-command (command)
238 (let ((dir (expand-file-name nndraft-directory)))
239 (and (string-match "/$" dir)
240 (setq dir (substring dir 0 (match-beginning 0))))
241 (string-match "/[^/]+$" dir)
242 (let ((group (substring dir (1+ (match-beginning 0))))
243 (nnmh-directory (substring dir 0 (1+ (match-beginning 0))))
244 (nnmh-get-new-mail nil))
247 (defun nndraft-article-filename (article &rest args)
249 (file-name-as-directory nndraft-directory)
250 (int-to-string article)
253 (defun nndraft-auto-save-file-name (file)
257 (set-buffer (get-buffer-create " *draft tmp*"))
258 (setq buffer-file-name file)
259 (make-auto-save-file-name))
260 (kill-buffer (current-buffer)))))
264 ;;; nndraft.el ends here