1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996,97,98 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.
28 (eval-when-compile (require 'cl))
32 (defcustom gnus-open-server-hook nil
33 "*Hook called just before opening connection to the news server."
38 ;;; Server Communication
41 (defun gnus-start-news-server (&optional confirm)
42 "Open a method for getting news.
43 If CONFIRM is non-nil, the user will be asked for an NNTP server."
45 (if gnus-current-select-method
46 ;; Stream is already opened.
49 (unless gnus-nntp-service
50 (setq gnus-nntp-server nil))
52 ;; Read server name with completion.
53 (setq gnus-nntp-server
54 (completing-read "NNTP server: "
55 (mapcar (lambda (server) (list server))
56 (cons (list gnus-nntp-server)
57 gnus-secondary-servers))
58 nil nil gnus-nntp-server)))
60 (when (and gnus-nntp-server
61 (stringp gnus-nntp-server)
62 (not (string= gnus-nntp-server "")))
63 (setq gnus-select-method
64 (cond ((or (string= gnus-nntp-server "")
65 (string= gnus-nntp-server "::"))
66 (list 'nnspool (system-name)))
67 ((string-match "^:" gnus-nntp-server)
68 (list 'nnmh gnus-nntp-server
70 (file-name-as-directory
72 (concat "~/" (substring
73 gnus-nntp-server 1)))))
74 (list 'nnmh-get-new-mail nil)))
76 (list 'nntp gnus-nntp-server)))))
78 (setq how (car gnus-select-method))
82 (gnus-message 5 "Looking up local news spool..."))
85 (gnus-message 5 "Looking up mh spool..."))
88 (setq gnus-current-select-method gnus-select-method)
89 (gnus-run-hooks 'gnus-open-server-hook)
91 ;; gnus-open-server-hook might have opened it
92 (gnus-server-opened gnus-select-method)
93 (gnus-open-server gnus-select-method)
96 "%s (%s) open error: '%s'. Continue? "
97 (car gnus-select-method) (cadr gnus-select-method)
98 (gnus-status-message gnus-select-method)))
99 (gnus-error 1 "Couldn't open server on %s"
100 (nth 1 gnus-select-method))))))
102 (defun gnus-check-group (group)
103 "Try to make sure that the server where GROUP exists is alive."
104 (let ((method (gnus-find-method-for-group group)))
105 (or (gnus-server-opened method)
106 (gnus-open-server method))))
108 (defun gnus-check-server (&optional method silent)
109 "Check whether the connection to METHOD is down.
110 If METHOD is nil, use `gnus-select-method'.
111 If it is down, start it up (again)."
112 (let ((method (or method gnus-select-method)))
113 ;; Transform virtual server names into select methods.
114 (when (stringp method)
115 (setq method (gnus-server-to-method method)))
116 (if (gnus-server-opened method)
117 ;; The stream is already opened.
121 (gnus-message 5 "Opening %s server%s..." (car method)
122 (if (equal (nth 1 method) "") ""
123 (format " on %s" (nth 1 method)))))
124 (gnus-run-hooks 'gnus-open-server-hook)
126 (gnus-open-server method)
130 (defun gnus-get-function (method function &optional noerror)
131 "Return a function symbol based on METHOD and FUNCTION."
132 ;; Translate server names into methods.
134 (error "Attempted use of a nil select method"))
135 (when (stringp method)
136 (setq method (gnus-server-to-method method)))
137 (let ((func (intern (format "%s-%s" (if gnus-agent
138 (gnus-agent-get-function method)
141 ;; If the functions isn't bound, we require the backend in
143 (unless (fboundp func)
144 (require (car method))
145 (when (and (not (fboundp func))
147 ;; This backend doesn't implement this function.
148 (error "No such function: %s" func)))
153 ;;; Interface functions to the backends.
156 (defun gnus-open-server (gnus-command-method)
157 "Open a connection to GNUS-COMMAND-METHOD."
158 (when (stringp gnus-command-method)
159 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
160 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
161 ;; If this method was previously denied, we just return nil.
162 (if (eq (nth 1 elem) 'denied)
164 (gnus-message 1 "Denied server")
168 (funcall (gnus-get-function gnus-command-method 'open-server)
169 (nth 1 gnus-command-method)
170 (nthcdr 2 gnus-command-method))))
171 ;; If this hasn't been opened before, we add it to the list.
173 (setq elem (list gnus-command-method nil)
174 gnus-opened-servers (cons elem gnus-opened-servers)))
175 ;; Set the status of this server.
176 (setcar (cdr elem) (if result 'ok 'denied))
177 ;; Return the result from the "open" call.
180 (defun gnus-close-server (gnus-command-method)
181 "Close the connection to GNUS-COMMAND-METHOD."
182 (when (stringp gnus-command-method)
183 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
184 (funcall (gnus-get-function gnus-command-method 'close-server)
185 (nth 1 gnus-command-method)))
187 (defun gnus-request-list (gnus-command-method)
188 "Request the active file from GNUS-COMMAND-METHOD."
189 (when (stringp gnus-command-method)
190 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
191 (funcall (gnus-get-function gnus-command-method 'request-list)
192 (nth 1 gnus-command-method)))
194 (defun gnus-request-list-newsgroups (gnus-command-method)
195 "Request the newsgroups file from GNUS-COMMAND-METHOD."
196 (when (stringp gnus-command-method)
197 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
198 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
199 (nth 1 gnus-command-method)))
201 (defun gnus-request-newgroups (date gnus-command-method)
202 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
203 (when (stringp gnus-command-method)
204 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
205 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
207 (funcall func date (nth 1 gnus-command-method)))))
209 (defun gnus-server-opened (gnus-command-method)
210 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
211 (when (stringp gnus-command-method)
212 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
213 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
214 (nth 1 gnus-command-method)))
216 (defun gnus-status-message (gnus-command-method)
217 "Return the status message from GNUS-COMMAND-METHOD.
218 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
219 this group uses will be queried."
220 (let ((gnus-command-method
221 (if (stringp gnus-command-method)
222 (gnus-find-method-for-group gnus-command-method)
223 gnus-command-method)))
224 (funcall (gnus-get-function gnus-command-method 'status-message)
225 (nth 1 gnus-command-method))))
227 (defun gnus-request-regenerate (gnus-command-method)
228 "Request a data generation from GNUS-COMMAND-METHOD."
229 (when (stringp gnus-command-method)
230 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
231 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
232 (nth 1 gnus-command-method)))
234 (defun gnus-request-group (group &optional dont-check gnus-command-method)
235 "Request GROUP. If DONT-CHECK, no information is required."
236 (let ((gnus-command-method
237 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
238 (when (stringp gnus-command-method)
239 (setq gnus-command-method
240 (inline (gnus-server-to-method gnus-command-method))))
241 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
242 (gnus-group-real-name group) (nth 1 gnus-command-method)
245 (defun gnus-list-active-group (group)
246 "Request active information on GROUP."
247 (let ((gnus-command-method (gnus-find-method-for-group group))
248 (func 'list-active-group))
249 (when (gnus-check-backend-function func group)
250 (funcall (gnus-get-function gnus-command-method func)
251 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
253 (defun gnus-request-group-description (group)
254 "Request a description of GROUP."
255 (let ((gnus-command-method (gnus-find-method-for-group group))
256 (func 'request-group-description))
257 (when (gnus-check-backend-function func group)
258 (funcall (gnus-get-function gnus-command-method func)
259 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
261 (defun gnus-close-group (group)
262 "Request the GROUP be closed."
263 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
264 (funcall (gnus-get-function gnus-command-method 'close-group)
265 (gnus-group-real-name group) (nth 1 gnus-command-method))))
267 (defun gnus-retrieve-headers (articles group &optional fetch-old)
268 "Request headers for ARTICLES in GROUP.
269 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
270 (let ((gnus-command-method (gnus-find-method-for-group group)))
271 (if (and gnus-use-cache (numberp (car articles)))
272 (gnus-cache-retrieve-headers articles group fetch-old)
273 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
274 articles (gnus-group-real-name group)
275 (nth 1 gnus-command-method) fetch-old))))
277 (defun gnus-retrieve-articles (articles group)
278 "Request ARTICLES in GROUP."
279 (let ((gnus-command-method (gnus-find-method-for-group group)))
280 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
281 articles (gnus-group-real-name group)
282 (nth 1 gnus-command-method))))
284 (defun gnus-retrieve-groups (groups gnus-command-method)
285 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
286 (when (stringp gnus-command-method)
287 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
288 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
289 groups (nth 1 gnus-command-method)))
291 (defun gnus-request-type (group &optional article)
292 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
293 (let ((gnus-command-method (gnus-find-method-for-group group)))
294 (if (not (gnus-check-backend-function
295 'request-type (car gnus-command-method)))
297 (funcall (gnus-get-function gnus-command-method 'request-type)
298 (gnus-group-real-name group) article))))
300 (defun gnus-request-update-mark (group article mark)
301 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
302 (let ((gnus-command-method (gnus-find-method-for-group group)))
303 (if (not (gnus-check-backend-function
304 'request-update-mark (car gnus-command-method)))
306 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
307 (gnus-group-real-name group) article mark))))
309 (defun gnus-request-article (article group &optional buffer)
310 "Request the ARTICLE in GROUP.
311 ARTICLE can either be an article number or an article Message-ID.
312 If BUFFER, insert the article in that group."
313 (let ((gnus-command-method (gnus-find-method-for-group group)))
314 (funcall (gnus-get-function gnus-command-method 'request-article)
315 article (gnus-group-real-name group)
316 (nth 1 gnus-command-method) buffer)))
318 (defun gnus-request-head (article group)
319 "Request the head of ARTICLE in GROUP."
320 (let* ((gnus-command-method (gnus-find-method-for-group group))
321 (head (gnus-get-function gnus-command-method 'request-head t))
327 (gnus-cache-request-article article group))
328 (setq res (cons group article)
330 ;; Use `head' function.
332 (setq res (funcall head article (gnus-group-real-name group)
333 (nth 1 gnus-command-method))))
334 ;; Use `article' function.
336 (setq res (gnus-request-article article group)
340 (set-buffer nntp-server-buffer)
341 (goto-char (point-min))
342 (when (search-forward "\n\n" nil t)
343 (delete-region (1- (point)) (point-max)))
344 (nnheader-fold-continuation-lines)))
347 (defun gnus-request-body (article group)
348 "Request the body of ARTICLE in GROUP."
349 (let ((gnus-command-method (gnus-find-method-for-group group)))
350 (funcall (gnus-get-function gnus-command-method 'request-body)
351 article (gnus-group-real-name group)
352 (nth 1 gnus-command-method))))
354 (defun gnus-request-post (gnus-command-method)
355 "Post the current buffer using GNUS-COMMAND-METHOD."
356 (when (stringp gnus-command-method)
357 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
358 (funcall (gnus-get-function gnus-command-method 'request-post)
359 (nth 1 gnus-command-method)))
361 (defun gnus-request-scan (group gnus-command-method)
362 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
363 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
365 (let ((gnus-command-method
366 (if group (gnus-find-method-for-group group) gnus-command-method))
367 (gnus-inhibit-demon t))
368 (funcall (gnus-get-function gnus-command-method 'request-scan)
369 (and group (gnus-group-real-name group))
370 (nth 1 gnus-command-method)))))
372 (defsubst gnus-request-update-info (info gnus-command-method)
373 "Request that GNUS-COMMAND-METHOD update INFO."
374 (when (stringp gnus-command-method)
375 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
376 (when (gnus-check-backend-function
377 'request-update-info (car gnus-command-method))
378 (funcall (gnus-get-function gnus-command-method 'request-update-info)
379 (gnus-group-real-name (gnus-info-group info))
380 info (nth 1 gnus-command-method))))
382 (defun gnus-request-expire-articles (articles group &optional force)
383 (let ((gnus-command-method (gnus-find-method-for-group group)))
384 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
385 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
388 (defun gnus-request-move-article
389 (article group server accept-function &optional last)
390 (let ((gnus-command-method (gnus-find-method-for-group group)))
391 (funcall (gnus-get-function gnus-command-method 'request-move-article)
392 article (gnus-group-real-name group)
393 (nth 1 gnus-command-method) accept-function last)))
395 (defun gnus-request-accept-article (group &optional gnus-command-method last)
396 ;; Make sure there's a newline at the end of the article.
397 (when (stringp gnus-command-method)
398 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
399 (when (and (not gnus-command-method)
401 (setq gnus-command-method (gnus-group-name-to-method group)))
402 (goto-char (point-max))
405 (let ((func (car (or gnus-command-method
406 (gnus-find-method-for-group group)))))
407 (funcall (intern (format "%s-request-accept-article" func))
408 (if (stringp group) (gnus-group-real-name group) group)
409 (cadr gnus-command-method)
412 (defun gnus-request-replace-article (article group buffer)
413 (let ((func (car (gnus-group-name-to-method group))))
414 (funcall (intern (format "%s-request-replace-article" func))
415 article (gnus-group-real-name group) buffer)))
417 (defun gnus-request-associate-buffer (group)
418 (let ((gnus-command-method (gnus-find-method-for-group group)))
419 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
420 (gnus-group-real-name group))))
422 (defun gnus-request-restore-buffer (article group)
423 "Request a new buffer restored to the state of ARTICLE."
424 (let ((gnus-command-method (gnus-find-method-for-group group)))
425 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
426 article (gnus-group-real-name group)
427 (nth 1 gnus-command-method))))
429 (defun gnus-request-create-group (group &optional gnus-command-method args)
430 (when (stringp gnus-command-method)
431 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
432 (let ((gnus-command-method
433 (or gnus-command-method (gnus-find-method-for-group group))))
434 (funcall (gnus-get-function gnus-command-method 'request-create-group)
435 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
437 (defun gnus-request-delete-group (group &optional force)
438 (let ((gnus-command-method (gnus-find-method-for-group group)))
439 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
440 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
442 (defun gnus-request-rename-group (group new-name)
443 (let ((gnus-command-method (gnus-find-method-for-group group)))
444 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
445 (gnus-group-real-name group)
446 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
448 (defun gnus-close-backends ()
449 ;; Send a close request to all backends that support such a request.
450 (let ((methods gnus-valid-select-methods)
451 (gnus-inhibit-demon t)
452 func gnus-command-method)
453 (while (setq gnus-command-method (pop methods))
454 (when (fboundp (setq func (intern
455 (concat (car gnus-command-method)
459 (defun gnus-asynchronous-p (gnus-command-method)
460 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
464 (defun gnus-remove-denial (gnus-command-method)
465 (when (stringp gnus-command-method)
466 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
467 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
468 (status (cadr elem)))
469 ;; If this hasn't been opened before, we add it to the list.
470 (when (eq status 'denied)
471 ;; Set the status of this server.
472 (setcar (cdr elem) 'closed))))
476 ;;; gnus-int.el ends here