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)
39 (defconst nndraft-version "nndraft 1.0")
40 (defvar nndraft-status-string "")
44 (defvar nndraft-current-server nil)
45 (defvar nndraft-server-alist nil)
46 (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-init-server-buffer)
90 (if (equal server nndraft-current-server)
92 (if nndraft-current-server
93 (setq nndraft-server-alist
94 (cons (list nndraft-current-server
95 (nnheader-save-variables nndraft-server-variables))
96 nndraft-server-alist)))
97 (let ((state (assoc server nndraft-server-alist)))
100 (nnheader-restore-variables (nth 1 state))
101 (setq nndraft-server-alist (delq state nndraft-server-alist)))
102 (nnheader-set-init-variables nndraft-server-variables defs))
103 (or (assq 'nndraft-directory defs)
104 (setq nndraft-directory server)))
105 (setq nndraft-current-server server)))
107 (defun nndraft-close-server (&optional server)
110 (defun nndraft-server-opened (&optional server)
111 (and nntp-server-buffer
112 (get-buffer nntp-server-buffer)
113 nndraft-current-server
114 (equal nndraft-current-server server)))
116 (defun nndraft-status-message (&optional server)
117 nndraft-status-string)
119 (defun nndraft-request-article (id &optional group server buffer)
121 ;; We get the newest file of the auto-saved file and the
123 (let* ((file (nndraft-article-filename id))
124 (auto (nndraft-auto-save-file-name file))
125 (newest (if (file-newer-than-file-p file auto) file auto))
126 (nntp-server-buffer (or buffer nntp-server-buffer)))
127 (when (and (file-exists-p newest)
128 (nnmail-find-file newest))
130 (set-buffer nntp-server-buffer)
131 (goto-char (point-min))
132 ;; If there's a mail header separator in this file,
134 (when (re-search-forward
135 (concat "^" mail-header-separator "$") nil t)
136 (replace-match "" t t)))
139 (defun nndraft-request-restore-buffer (article &optional group server)
140 "Request a new buffer that is restored to the state of ARTICLE."
141 (let ((file (nndraft-article-filename article ".state"))
142 nndraft-point nndraft-mode nndraft-buffer-name)
143 (when (file-exists-p file)
145 (when nndraft-buffer-name
146 (set-buffer (get-buffer-create
147 (generate-new-buffer-name nndraft-buffer-name)))
148 (nndraft-request-article article group server (current-buffer))
149 (funcall nndraft-mode)
150 (let ((gnus-verbose-backends nil))
151 (nndraft-request-expire-articles (list article) group server t))
152 (goto-char nndraft-point))
153 nndraft-buffer-name)))
155 (defun nndraft-request-update-info (group info &optional server)
156 (setcar (cdr (cdr info)) nil)
158 (setcar (nthcdr 3 info) nil)))
160 (defun nndraft-request-associate-buffer (group)
161 "Associate the current buffer with some article in the draft group."
162 (let* ((gnus-verbose-backends nil)
163 (article (cdr (nndraft-request-accept-article group 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-post (&optional server)
185 (mail-send-and-exit nil))
187 (defun nndraft-request-expire-articles
188 (articles group &optional server force)
189 (let ((res (nndraft-execute-nnmh-command
190 (` (nnmh-request-expire-articles
191 (quote (, articles)) group (, server) (, force)))))
193 ;; Delete all the "state" files of articles that have been expired.
195 (unless (memq (setq article (pop articles)) res)
196 (let ((file (nndraft-article-filename article ".state"))
197 (auto (nndraft-auto-save-file-name
198 (nndraft-article-filename article))))
199 (when (file-exists-p file)
200 (funcall nnmail-delete-file-function file))
201 (when (file-exists-p auto)
202 (funcall nnmail-delete-file-function auto)))))
205 (defun nndraft-request-accept-article (group &optional last noinsert)
206 (let* ((point (point))
209 (gnus-verbose-backends nil)
210 (gart (nndraft-execute-nnmh-command
211 (` (nnmh-request-accept-article group (, last) noinsert))))
213 (nndraft-article-filename (cdr gart) ".state")))
214 ;; Write the "state" file.
216 (nnheader-set-temp-buffer " *draft state*")
217 (insert (format "%S\n" `(setq nndraft-mode (quote ,mode)
219 nndraft-buffer-name ,name)))
220 (write-region (point-min) (point-max) state nil 'silent)
221 (kill-buffer (current-buffer)))
224 (defun nndraft-close-group (group &optional server)
227 (defun nndraft-request-create-group (group &optional server)
228 (if (file-exists-p nndraft-directory)
229 (if (file-directory-p nndraft-directory)
234 (make-directory nndraft-directory t)
239 ;;; Low-Level Interface
241 (defun nndraft-execute-nnmh-command (command)
242 (let ((dir (expand-file-name nndraft-directory)))
243 (and (string-match "/$" dir)
244 (setq dir (substring dir 0 (match-beginning 0))))
245 (string-match "/[^/]+$" dir)
246 (let ((group (substring dir (1+ (match-beginning 0))))
247 (nnmh-directory (substring dir 0 (1+ (match-beginning 0))))
248 (nnmh-get-new-mail nil))
251 (defun nndraft-article-filename (article &rest args)
253 (file-name-as-directory nndraft-directory)
254 (int-to-string article)
257 (defun nndraft-auto-save-file-name (file)
261 (set-buffer (get-buffer-create " *draft tmp*"))
262 (setq buffer-file-name file)
263 (make-auto-save-file-name))
264 (kill-buffer (current-buffer)))))
268 ;;; nndraft.el ends here