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)
61 (let* ((buf (get-buffer-create " *draft headers*"))
64 (buffer-disable-undo (current-buffer))
66 ;; We don't support fetching by Message-ID.
67 (if (stringp (car articles))
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)
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)
84 (nnheader-fold-continuation-lines)
87 (defun nndraft-open-server (server &optional defs)
88 (nnheader-change-server 'nndraft server defs)
89 (unless (assq 'nndraft-directory defs)
90 (setq nndraft-directory server))
92 ((not (file-exists-p nndraft-directory))
93 (nndraft-close-server)
94 (nnheader-report 'nndraft "No such file or directory: %s"
96 ((not (file-directory-p (file-truename nndraft-directory)))
97 (nndraft-close-server)
98 (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
100 (nnheader-report 'nndraft "Opened server %s using directory %s"
101 server nndraft-directory)
104 (defun nndraft-close-server (&optional server)
105 (setq nndraft-current-server nil)
108 (defun nndraft-server-opened (&optional server)
109 (and nntp-server-buffer
110 (get-buffer nntp-server-buffer)
111 nndraft-current-server
112 (equal nndraft-current-server server)))
114 (defun nndraft-status-message (&optional server)
115 nndraft-status-string)
117 (defun nndraft-request-article (id &optional group server buffer)
119 ;; We get the newest file of the auto-saved file and the
121 (let* ((file (nndraft-article-filename id))
122 (auto (nndraft-auto-save-file-name file))
123 (newest (if (file-newer-than-file-p file auto) file auto))
124 (nntp-server-buffer (or buffer nntp-server-buffer)))
125 (when (and (file-exists-p newest)
126 (nnmail-find-file newest))
128 (set-buffer nntp-server-buffer)
129 (goto-char (point-min))
130 ;; If there's a mail header separator in this file,
132 (when (re-search-forward
133 (concat "^" mail-header-separator "$") nil t)
134 (replace-match "" t t)))
137 (defun nndraft-request-restore-buffer (article &optional group server)
138 "Request a new buffer that is restored to the state of ARTICLE."
139 (let ((file (nndraft-article-filename article ".state"))
140 nndraft-point nndraft-mode nndraft-buffer-name)
141 (when (file-exists-p file)
143 (when nndraft-buffer-name
144 (set-buffer (get-buffer-create
145 (generate-new-buffer-name nndraft-buffer-name)))
146 (nndraft-request-article article group server (current-buffer))
147 (funcall nndraft-mode)
148 (let ((gnus-verbose-backends nil))
149 (nndraft-request-expire-articles (list article) group server t))
150 (goto-char nndraft-point))
151 nndraft-buffer-name)))
153 (defun nndraft-request-update-info (group info &optional server)
154 (setcar (cddr info) nil)
156 (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
163 group nndraft-current-server t 'noinsert)))
164 (file (nndraft-article-filename article)))
165 (setq buffer-file-name file)
166 (setq buffer-auto-save-file-name (make-auto-save-file-name))
167 (clear-visited-file-modtime)
170 (defun nndraft-request-group (group &optional server dont-check)
172 (nndraft-execute-nnmh-command
173 `(nnmh-request-group group "" ,dont-check))
174 (nnheader-report 'nndraft nnmh-status-string)))
176 (defun nndraft-request-list (&optional server dir)
177 (nndraft-execute-nnmh-command
178 `(nnmh-request-list nil ,dir)))
180 (defun nndraft-request-newgroups (date &optional server)
181 (nndraft-execute-nnmh-command
182 `(nnmh-request-newgroups ,date ,server)))
184 (defun nndraft-request-expire-articles
185 (articles group &optional server force)
186 (let ((res (nndraft-execute-nnmh-command
187 `(nnmh-request-expire-articles
188 ',articles group ,server ,force)))
190 ;; Delete all the "state" files of articles that have been expired.
192 (unless (memq (setq article (pop articles)) res)
193 (let ((file (nndraft-article-filename article ".state"))
194 (auto (nndraft-auto-save-file-name
195 (nndraft-article-filename article))))
196 (when (file-exists-p file)
197 (funcall nnmail-delete-file-function file))
198 (when (file-exists-p auto)
199 (funcall nnmail-delete-file-function auto)))))
202 (defun nndraft-request-accept-article (group &optional server last noinsert)
203 (let* ((point (point))
206 (gnus-verbose-backends nil)
207 (gart (nndraft-execute-nnmh-command
208 `(nnmh-request-accept-article group ,server ,last noinsert)))
210 (nndraft-article-filename (cdr gart) ".state")))
211 ;; Write the "state" file.
213 (nnheader-set-temp-buffer " *draft state*")
214 (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
216 nndraft-buffer-name ,name)))
217 (write-region (point-min) (point-max) state nil 'silent)
218 (kill-buffer (current-buffer)))
221 (defun nndraft-close-group (group &optional server)
224 (defun nndraft-request-create-group (group &optional server)
225 (if (file-exists-p nndraft-directory)
226 (if (file-directory-p nndraft-directory)
231 (make-directory nndraft-directory t)
236 ;;; Low-Level Interface
238 (defun nndraft-execute-nnmh-command (command)
239 (let ((dir (expand-file-name nndraft-directory)))
240 (and (string-match "/$" dir)
241 (setq dir (substring dir 0 (match-beginning 0))))
242 (string-match "/[^/]+$" dir)
243 (let ((group (substring dir (1+ (match-beginning 0))))
244 (nnmh-directory (substring dir 0 (1+ (match-beginning 0))))
245 (nnmail-keep-last-article nil)
246 (nnmh-get-new-mail nil))
249 (defun nndraft-article-filename (article &rest args)
251 (file-name-as-directory nndraft-directory)
252 (int-to-string article)
255 (defun nndraft-auto-save-file-name (file)
259 (set-buffer (get-buffer-create " *draft tmp*"))
260 (setq buffer-file-name file)
261 (make-auto-save-file-name))
262 (kill-buffer (current-buffer)))))
266 ;;; nndraft.el ends here