1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
33 (defcustom gnus-open-server-hook nil
34 "Hook called just before opening connection to the news server."
39 ;;; Server Communication
42 (defun gnus-start-news-server (&optional confirm)
43 "Open a method for getting news.
44 If CONFIRM is non-nil, the user will be asked for an NNTP server."
46 (if gnus-current-select-method
47 ;; Stream is already opened.
50 (unless gnus-nntp-service
51 (setq gnus-nntp-server nil))
53 ;; Read server name with completion.
54 (setq gnus-nntp-server
55 (completing-read "NNTP server: "
56 (mapcar (lambda (server) (list server))
57 (cons (list gnus-nntp-server)
58 gnus-secondary-servers))
59 nil nil gnus-nntp-server)))
61 (when (and gnus-nntp-server
62 (stringp gnus-nntp-server)
63 (not (string= gnus-nntp-server "")))
64 (setq gnus-select-method
65 (cond ((or (string= gnus-nntp-server "")
66 (string= gnus-nntp-server "::"))
67 (list 'nnspool (system-name)))
68 ((string-match "^:" gnus-nntp-server)
69 (list 'nnmh gnus-nntp-server
71 (file-name-as-directory
73 (concat "~/" (substring
74 gnus-nntp-server 1)))))
75 (list 'nnmh-get-new-mail nil)))
77 (list 'nntp gnus-nntp-server)))))
79 (setq how (car gnus-select-method))
83 (gnus-message 5 "Looking up local news spool..."))
86 (gnus-message 5 "Looking up mh spool..."))
89 (setq gnus-current-select-method gnus-select-method)
90 (gnus-run-hooks 'gnus-open-server-hook)
92 ;; gnus-open-server-hook might have opened it
93 (gnus-server-opened gnus-select-method)
94 (gnus-open-server gnus-select-method)
98 "%s (%s) open error: '%s'. Continue? "
99 (car gnus-select-method) (cadr gnus-select-method)
100 (gnus-status-message gnus-select-method)))
101 (gnus-error 1 "Couldn't open server on %s"
102 (nth 1 gnus-select-method))))))
104 (defun gnus-check-group (group)
105 "Try to make sure that the server where GROUP exists is alive."
106 (let ((method (gnus-find-method-for-group group)))
107 (or (gnus-server-opened method)
108 (gnus-open-server method))))
110 (defun gnus-check-server (&optional method silent)
111 "Check whether the connection to METHOD is down.
112 If METHOD is nil, use `gnus-select-method'.
113 If it is down, start it up (again)."
114 (let ((method (or method gnus-select-method)))
115 ;; Transform virtual server names into select methods.
116 (when (stringp method)
117 (setq method (gnus-server-to-method method)))
118 (if (gnus-server-opened method)
119 ;; The stream is already opened.
123 (gnus-message 5 "Opening %s server%s..." (car method)
124 (if (equal (nth 1 method) "") ""
125 (format " on %s" (nth 1 method)))))
126 (gnus-run-hooks 'gnus-open-server-hook)
128 (gnus-open-server method)
132 (defun gnus-get-function (method function &optional noerror)
133 "Return a function symbol based on METHOD and FUNCTION."
134 ;; Translate server names into methods.
136 (error "Attempted use of a nil select method"))
137 (when (stringp method)
138 (setq method (gnus-server-to-method method)))
139 ;; Check cache of constructed names.
140 (let* ((method-sym (if gnus-agent
141 (gnus-agent-get-function method)
143 (method-fns (get method-sym 'gnus-method-functions))
144 (func (let ((method-fnlist-elt (assq function method-fns)))
145 (unless method-fnlist-elt
146 (setq method-fnlist-elt
148 (intern (format "%s-%s" method-sym function))))
149 (put method-sym 'gnus-method-functions
150 (cons method-fnlist-elt method-fns)))
151 (cdr method-fnlist-elt))))
152 ;; Maybe complain if there is no function.
153 (unless (fboundp func)
155 (error "Trying to require a method that doesn't exist"))
156 (require (car method))
157 (when (not (fboundp func))
160 (error "No such function: %s" func))))
165 ;;; Interface functions to the backends.
168 (defun gnus-open-server (gnus-command-method)
169 "Open a connection to GNUS-COMMAND-METHOD."
170 (when (stringp gnus-command-method)
171 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
172 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
173 ;; If this method was previously denied, we just return nil.
174 (if (eq (nth 1 elem) 'denied)
176 (gnus-message 1 "Denied server")
180 (funcall (gnus-get-function gnus-command-method 'open-server)
181 (nth 1 gnus-command-method)
182 (nthcdr 2 gnus-command-method))))
183 ;; If this hasn't been opened before, we add it to the list.
185 (setq elem (list gnus-command-method nil)
186 gnus-opened-servers (cons elem gnus-opened-servers)))
187 ;; Set the status of this server.
188 (setcar (cdr elem) (if result 'ok 'denied))
189 ;; Return the result from the "open" call.
192 (defun gnus-close-server (gnus-command-method)
193 "Close the connection to GNUS-COMMAND-METHOD."
194 (when (stringp gnus-command-method)
195 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))