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 ;; Check cache of constructed names.
138 (let* ((method-sym (if gnus-agent
139 (gnus-agent-get-function method)
141 (method-fns (get method-sym 'gnus-method-functions))
142 (func (let ((method-fnlist-elt (assq function method-fns)))
143 (unless method-fnlist-elt
144 (setq method-fnlist-elt
146 (intern (format "%s-%s" method-sym function))))
147 (put method-sym 'gnus-method-functions
148 (cons method-fnlist-elt method-fns)))
149 (cdr method-fnlist-elt))))
150 ;; Maybe complain if there is no function.
151 (unless (fboundp func)
152 (require (car method))
153 (when (and (not (fboundp func))
155 (error "No such function: %s" func)))
160 ;;; Interface functions to the backends.
163 (defun gnus-open-server (gnus-command-method)
164 "Open a connection to GNUS-COMMAND-METHOD."
165 (when (stringp gnus-command-method)
166 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
167 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
168 ;; If this method was previously denied, we just return nil.
169 (if (eq (nth 1 elem) 'denied)
171 (gnus-message 1 "Denied server")
175 (funcall (gnus-get-function gnus-command-method 'open-server)
176 (nth 1 gnus-command-method)
177 (nthcdr 2 gnus-command-method))))
178 ;; If this hasn't been opened before, we add it to the list.
180 (setq elem (list gnus-command-method nil)
181 gnus-opened-servers (cons elem gnus-opened-servers)))
182 ;; Set the status of this server.
183 (setcar (cdr elem) (if result 'ok 'denied))
184 ;; Return the result from the "open" call.
187 (defun gnus-close-server (gnus-command-method)
188 "Close the connection to 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 'close-server)
192 (nth 1 gnus-command-method)))
194 (defun gnus-request-list (gnus-command-method)
195 "Request the active 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)
199 (nth 1 gnus-command-method)))
201 (defun gnus-request-list-newsgroups (gnus-command-method)
202 "Request the newsgroups file from GNUS-COMMAND-METHOD."
203 (when (stringp gnus-command-method)
204 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
205 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
206 (nth 1 gnus-command-method)))
208 (defun gnus-request-newgroups (date gnus-command-method)
209 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
210 (when (stringp gnus-command-method)
211 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
212 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
214 (funcall func date (nth 1 gnus-command-method)))))
216 (defun gnus-server-opened (gnus-command-method)
217 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
218 (when (stringp gnus-command-method)
219 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
220 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
221 (nth 1 gnus-command-method)))
223 (defun gnus-status-message (gnus-command-method)
224 "Return the status message from GNUS-COMMAND-METHOD.
225 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
226 this group uses will be queried."
227 (let ((gnus-command-method
228 (if (stringp gnus-command-method)
229 (gnus-find-method-for-group gnus-command-method)
230 gnus-command-method)))
231 (funcall (gnus-get-function gnus-command-method 'status-message)
232 (nth 1 gnus-command-method))))
234 (defun gnus-request-regenerate (gnus-command-method)
235 "Request a data generation from GNUS-COMMAND-METHOD."
236 (when (stringp gnus-command-method)
237 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
238 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
239 (nth 1 gnus-command-method)))
241 (defun gnus-request-group (group &optional dont-check gnus-command-method)
242 "Request GROUP. If DONT-CHECK, no information is required."
243 (let ((gnus-command-method
244 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
245 (when (stringp gnus-command-method)
246 (setq gnus-command-method
247 (inline (gnus-server-to-method gnus-command-method))))
248 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
249 (gnus-group-real-name group) (nth 1 gnus-command-method)
252 (defun gnus-list-active-group (group)
253 "Request active information on GROUP."
254 (let ((gnus-command-method (gnus-find-method-for-group group))
255 (func 'list-active-group))
256 (when (gnus-check-backend-function func group)
257 (funcall (gnus-get-function gnus-command-method func)
258 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
260 (defun gnus-request-group-description (group)
261 "Request a description of GROUP."
262 (let ((gnus-command-method (gnus-find-method-for-group group))
263 (func 'request-group-description))
264 (when (gnus-check-backend-function func group)
265 (funcall (gnus-get-function gnus-command-method func)
266 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
268 (defun gnus-close-group (group)
269 "Request the GROUP be closed."
270 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
271 (funcall (gnus-get-function gnus-command-method 'close-group)
272 (gnus-group-real-name group) (nth 1 gnus-command-method))))
274 (defun gnus-retrieve-headers (articles group &optional fetch-old)
275 "Request headers for ARTICLES in GROUP.
276 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
277 (let ((gnus-command-method (gnus-find-method-for-group group)))
278 (if (and gnus-use-cache (numberp (car articles)))
279 (gnus-cache-retrieve-headers articles group fetch-old)
280 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
281 articles (gnus-group-real-name group)
282 (nth 1 gnus-command-method) fetch-old))))
284 (defun gnus-retrieve-articles (articles group)
285 "Request ARTICLES in GROUP."
286 (let ((gnus-command-method (gnus-find-method-for-group group)))
287 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
288 articles (gnus-group-real-name group)
289 (nth 1 gnus-command-method))))
291 (defun gnus-retrieve-groups (groups gnus-command-method)
292 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
293 (when (stringp gnus-command-method)
294 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
295 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
296 groups (nth 1 gnus-command-method)))
298 (defun gnus-request-type (group &optional article)
299 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
300 (let ((gnus-command-method (gnus-find-method-for-group group)))
301 (if (not (gnus-check-backend-function
302 'request-type (car gnus-command-method)))
304 (funcall (gnus-get-function gnus-command-method 'request-type)
305 (gnus-group-real-name group) article))))
307 (defun gnus-request-update-mark (group article mark)
308 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
309 (let ((gnus-command-method (gnus-find-method-for-group group)))
310 (if (not (gnus-check-backend-function
311 'request-update-mark (car gnus-command-method)))
313 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
314 (gnus-group-real-name group) article mark))))
316 (defun gnus-request-article (article group &optional buffer)
317 "Request the ARTICLE in GROUP.
318 ARTICLE can either be an article number or an article Message-ID.
319 If BUFFER, insert the article in that group."
320 (let ((gnus-command-method (gnus-find-method-for-group group)))
321 (funcall (gnus-get-function gnus-command-method 'request-article)
322 article (gnus-group-real-name group)
323 (nth 1 gnus-command-method) buffer)))
325 (defun gnus-request-head (article group)
326 "Request the head of ARTICLE in GROUP."
327 (let* ((gnus-command-method (gnus-find-method-for-group group))
328 (head (gnus-get-function gnus-command-method 'request-head t))
334 (gnus-cache-request-article article group))
335 (setq res (cons group article)
337 ;; Use `head' function.
339 (setq res (funcall head article (gnus-group-real-name group)
340 (nth 1 gnus-command-method))))
341 ;; Use `article' function.
343 (setq res (gnus-request-article article group)
347 (set-buffer nntp-server-buffer)
348 (goto-char (point-min))
349 (when (search-forward "\n\n" nil t)
350 (delete-region (1- (point)) (point-max)))
351 (nnheader-fold-continuation-lines)))
354 (defun gnus-request-body (article group)
355 "Request the body of ARTICLE in GROUP."
356 (let* ((gnus-command-method (gnus-find-method-for-group group))
357 (head (gnus-get-function gnus-command-method 'request-body t))
363 (gnus-cache-request-article article group))
364 (setq res (cons group article)
366 ;; Use `head' function.
368 (setq res (funcall head article (gnus-group-real-name group)
369 (nth 1 gnus-command-method))))
370 ;; Use `article' function.
372 (setq res (gnus-request-article article group)
376 (set-buffer nntp-server-buffer)
377 (goto-char (point-min))
378 (when (search-forward "\n\n" nil t)
379 (delete-region (point-min) (1- (point))))))
382 (defun gnus-request-post (gnus-command-method)
383 "Post the current buffer using GNUS-COMMAND-METHOD."
384 (when (stringp gnus-command-method)
385 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
386 (funcall (gnus-get-function gnus-command-method 'request-post)
387 (nth 1 gnus-command-method)))
389 (defun gnus-request-scan (group gnus-command-method)
390 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
391 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
393 (let ((gnus-command-method
394 (if group (gnus-find-method-for-group group) gnus-command-method))
395 (gnus-inhibit-demon t))
396 (funcall (gnus-get-function gnus-command-method 'request-scan)
397 (and group (gnus-group-real-name group))
398 (nth 1 gnus-command-method)))))
400 (defsubst gnus-request-update-info (info gnus-command-method)
401 "Request that GNUS-COMMAND-METHOD update INFO."
402 (when (stringp gnus-command-method)
403 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
404 (when (gnus-check-backend-function
405 'request-update-info (car gnus-command-method))
406 (funcall (gnus-get-function gnus-command-method 'request-update-info)
407 (gnus-group-real-name (gnus-info-group info))
408 info (nth 1 gnus-command-method))))
410 (defun gnus-request-expire-articles (articles group &optional force)
411 (let ((gnus-command-method (gnus-find-method-for-group group)))
412 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
413 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
416 (defun gnus-request-move-article
417 (article group server accept-function &optional last)
418 (let ((gnus-command-method (gnus-find-method-for-group group)))
419 (funcall (gnus-get-function gnus-command-method 'request-move-article)
420 article (gnus-group-real-name group)
421 (nth 1 gnus-command-method) accept-function last)))
423 (defun gnus-request-accept-article (group &optional gnus-command-method last)
424 ;; Make sure there's a newline at the end of the article.
425 (when (stringp gnus-command-method)
426 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
427 (when (and (not gnus-command-method)
429 (setq gnus-command-method (gnus-group-name-to-method group)))
430 (goto-char (point-max))
433 (let ((func (car (or gnus-command-method
434 (gnus-find-method-for-group group)))))
435 (funcall (intern (format "%s-request-accept-article" func))
436 (if (stringp group) (gnus-group-real-name group) group)
437 (cadr gnus-command-method)
440 (defun gnus-request-replace-article (article group buffer)
441 (let ((func (car (gnus-group-name-to-method group))))
442 (funcall (intern (format "%s-request-replace-article" func))
443 article (gnus-group-real-name group) buffer)))
445 (defun gnus-request-associate-buffer (group)
446 (let ((gnus-command-method (gnus-find-method-for-group group)))
447 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
448 (gnus-group-real-name group))))
450 (defun gnus-request-restore-buffer (article group)
451 "Request a new buffer restored to the state of ARTICLE."
452 (let ((gnus-command-method (gnus-find-method-for-group group)))
453 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
454 article (gnus-group-real-name group)
455 (nth 1 gnus-command-method))))
457 (defun gnus-request-create-group (group &optional gnus-command-method args)
458 (when (stringp gnus-command-method)
459 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
460 (let ((gnus-command-method
461 (or gnus-command-method (gnus-find-method-for-group group))))
462 (funcall (gnus-get-function gnus-command-method 'request-create-group)
463 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
465 (defun gnus-request-delete-group (group &optional force)
466 (let ((gnus-command-method (gnus-find-method-for-group group)))
467 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
468 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
470 (defun gnus-request-rename-group (group new-name)
471 (let ((gnus-command-method (gnus-find-method-for-group group)))
472 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
473 (gnus-group-real-name group)
474 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
476 (defun gnus-close-backends ()
477 ;; Send a close request to all backends that support such a request.
478 (let ((methods gnus-valid-select-methods)
479 (gnus-inhibit-demon t)
480 func gnus-command-method)
481 (while (setq gnus-command-method (pop methods))
482 (when (fboundp (setq func (intern
483 (concat (car gnus-command-method)
487 (defun gnus-asynchronous-p (gnus-command-method)
488 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
492 (defun gnus-remove-denial (gnus-command-method)
493 (when (stringp gnus-command-method)
494 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
495 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
496 (status (cadr elem)))
497 ;; If this hasn't been opened before, we add it to the list.
498 (when (eq status 'denied)
499 ;; Set the status of this server.
500 (setcar (cdr elem) 'closed))))
504 ;;; gnus-int.el ends here