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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 (eval-and-compile (require 'cl))
32 (autoload 'mail-send-and-exit "sendmail"))
34 (defvar nndraft-directory nil)
38 (defconst nndraft-version "nndraft 1.0")
39 (defvar nndraft-status-string "")
43 (defvar nndraft-current-server nil)
44 (defvar nndraft-server-alist nil)
45 (defvar nndraft-server-variables
47 '(nndraft-directory nil)
48 '(nndraft-status-string "")
49 '(nndraft-group-alist)))
53 ;;; Interface functions.
56 (defun nndraft-retrieve-headers (articles &optional group server fetch-old)
58 (set-buffer nntp-server-buffer)
61 (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-init-server-buffer)
89 (if (equal server nndraft-current-server)
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)))
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)))
106 (defun nndraft-close-server (&optional server)
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-post (&optional server)
184 (mail-send-and-exit nil))
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)))))
192 ;; Delete all the "state" files of articles that have been expired.
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)))))
204 (defun nndraft-request-accept-article (group &optional last noinsert)
205 (let* ((point (point))
208 (gnus-verbose-backends nil)
209 (gart (nndraft-execute-nnmh-command
210 (` (nnmh-request-accept-article group (, last) noinsert))))
212 (nndraft-article-filename (cdr gart) ".state")))
213 ;; Write the "state" file.
215 (nnheader-set-temp-buffer " *draft state*")
216 (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
218 nndraft-buffer-name ,name)))
219 (write-region (point-min) (point-max) state nil 'silent)
220 (kill-buffer (current-buffer)))
223 (defun nndraft-close-group (group &optional server)
226 (defun nndraft-request-create-group (group &optional server)
227 (if (file-exists-p nndraft-directory)
228 (if (file-directory-p nndraft-directory)
233 (make-directory nndraft-directory t)
238 ;;; Low-Level Interface
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))
250 (defun nndraft-article-filename (article &rest args)
252 (file-name-as-directory nndraft-directory)
253 (int-to-string article)
256 (defun nndraft-auto-save-file-name (file)
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)))))
267 ;;; nndraft.el ends here