1 ;;; gnus-int.el --- backend interface functions for Gnus
3 ;; Copyright (C) 1996-2011 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl))
33 (autoload 'gnus-run-hook-with-args "gnus-util")
34 (autoload 'gnus-agent-expire "gnus-agent")
35 (autoload 'gnus-agent-regenerate-group "gnus-agent")
36 (autoload 'gnus-agent-read-servers-validate-native "gnus-agent")
37 (autoload 'gnus-agent-possibly-synchronize-flags-server "gnus-agent")
39 (defcustom gnus-open-server-hook nil
40 "Hook called just before opening connection to the news server."
44 (defcustom gnus-after-set-mark-hook nil
45 "Hook called just after marks are set in a group."
49 (defcustom gnus-before-update-mark-hook nil
50 "Hook called just before marks are updated in a group."
54 (defcustom gnus-server-unopen-status nil
55 "The default status if the server is not able to open.
56 If the server is covered by Gnus agent, the possible values are
57 `denied', set the server denied; `offline', set the server offline;
58 nil, ask user. If the server is not covered by Gnus agent, set the
62 :type '(choice (const :tag "Ask" nil)
63 (const :tag "Deny server" denied)
64 (const :tag "Unplug Agent" offline)))
66 (defvar gnus-internal-registry-spool-current-method nil
67 "The current method, for the registry.")
70 (defun gnus-server-opened (gnus-command-method)
71 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
72 (unless (eq (gnus-server-status gnus-command-method)
74 (when (stringp gnus-command-method)
75 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
76 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
77 (nth 1 gnus-command-method))))
79 (defun gnus-status-message (gnus-command-method)
80 "Return the status message from GNUS-COMMAND-METHOD.
81 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
82 name. The method this group uses will be queried."
83 (let ((gnus-command-method
84 (if (stringp gnus-command-method)
85 (gnus-find-method-for-group gnus-command-method)
86 gnus-command-method)))
87 (funcall (gnus-get-function gnus-command-method 'status-message)
88 (nth 1 gnus-command-method))))
91 ;;; Server Communication
94 (defun gnus-start-news-server (&optional confirm)
95 "Open a method for getting news.
96 If CONFIRM is non-nil, the user will be asked for an NNTP server."
98 (if gnus-current-select-method
99 ;; Stream is already opened.
103 ;; Read server name with completion.
104 (setq gnus-nntp-server
105 (gnus-completing-read "NNTP server"
106 (cons gnus-nntp-server
107 gnus-secondary-servers)
108 nil gnus-nntp-server)))
110 (when (and gnus-nntp-server
111 (stringp gnus-nntp-server)
112 (not (string= gnus-nntp-server "")))
113 (setq gnus-select-method
114 (cond ((or (string= gnus-nntp-server "")
115 (string= gnus-nntp-server "::"))
116 (list 'nnspool (system-name)))
117 ((string-match "^:" gnus-nntp-server)
118 (list 'nnmh gnus-nntp-server
119 (list 'nnmh-directory
120 (file-name-as-directory
122 (substring gnus-nntp-server 1) "~/")))
123 (list 'nnmh-get-new-mail nil)))
125 (list 'nntp gnus-nntp-server)))))
127 (setq how (car gnus-select-method))
131 (gnus-message 5 "Looking up local news spool..."))
134 (gnus-message 5 "Looking up mh spool..."))
137 (setq gnus-current-select-method gnus-select-method)
138 (gnus-run-hooks 'gnus-open-server-hook)
140 ;; Partially validate agent covered methods now that the
141 ;; gnus-select-method is known.
144 ;; NOTE: This is here for one purpose only. By validating
145 ;; the current select method, it converts the old 5.10.3,
146 ;; and earlier, format to the current format. That enables
147 ;; the agent code within gnus-open-server to function
149 (gnus-agent-read-servers-validate-native gnus-select-method))
152 ;; gnus-open-server-hook might have opened it
153 (gnus-server-opened gnus-select-method)
154 (gnus-open-server gnus-select-method)
158 "%s (%s) open error: '%s'. Continue? "
159 (car gnus-select-method) (cadr gnus-select-method)
160 (gnus-status-message gnus-select-method)))
161 (gnus-error 1 "Couldn't open server on %s"
162 (nth 1 gnus-select-method))))))
164 (defun gnus-check-group (group)
165 "Try to make sure that the server where GROUP exists is alive."
166 (let ((method (gnus-find-method-for-group group)))
167 (or (gnus-server-opened method)
168 (gnus-open-server method))))
170 (defun gnus-check-server (&optional method silent)
171 "Check whether the connection to METHOD is down.
172 If METHOD is nil, use `gnus-select-method'.
173 If it is down, start it up (again)."
174 (let ((method (or method gnus-select-method))
176 ;; Transform virtual server names into select methods.
177 (when (stringp method)
178 (setq method (gnus-server-to-method method)))
179 (if (gnus-server-opened method)
180 ;; The stream is already opened.
184 (gnus-message 5 "Opening %s server%s..." (car method)
185 (if (equal (nth 1 method) "") ""
186 (format " on %s" (nth 1 method)))))
187 (gnus-run-hooks 'gnus-open-server-hook)
189 (setq result (gnus-open-server method))
193 "Opening %s server%s...%s" (car method)
194 (if (equal (nth 1 method) "") ""
195 (format " on %s" (nth 1 method)))
199 (nnheader-get-report-string (car method))))))))))
201 (defun gnus-get-function (method function &optional noerror)
202 "Return a function symbol based on METHOD and FUNCTION."
203 ;; Translate server names into methods.
205 (error "Attempted use of a nil select method"))
206 (when (stringp method)
207 (setq method (gnus-server-to-method method)))
208 ;; Check cache of constructed names.
209 (let* ((method-sym (if gnus-agent
210 (inline (gnus-agent-get-function method))
212 (method-fns (get method-sym 'gnus-method-functions))
213 (func (let ((method-fnlist-elt (assq function method-fns)))
214 (unless method-fnlist-elt
215 (setq method-fnlist-elt
217 (intern (format "%s-%s" method-sym function))))
218 (put method-sym 'gnus-method-functions
219 (cons method-fnlist-elt method-fns)))
220 (cdr method-fnlist-elt))))
221 ;; Maybe complain if there is no function.
222 (unless (fboundp func)
224 (error "Trying to require a method that doesn't exist"))
225 (require (car method))
226 (when (not (fboundp func))
229 (error "No such function: %s" func))))
234 ;;; Interface functions to the backends.
237 (defun gnus-method-denied-p (method)
238 (eq (nth 1 (assoc method gnus-opened-servers))
241 (defvar gnus-backend-trace t)
243 (defun gnus-open-server (gnus-command-method)
244 "Open a connection to GNUS-COMMAND-METHOD."
245 (when (stringp gnus-command-method)
246 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
247 (when gnus-backend-trace
248 (with-current-buffer (get-buffer-create "*gnus trace*")
249 (buffer-disable-undo)
250 (goto-char (point-max))
251 (insert (format-time-string "%H:%M:%S")
252 (format " %S\n" gnus-command-method))))
253 (let ((elem (assoc gnus-command-method gnus-opened-servers))
254 (server (gnus-method-to-server-name gnus-command-method)))
255 ;; If this method was previously denied, we just return nil.
256 (if (eq (nth 1 elem) 'denied)
258 (gnus-message 1 "Denied server %s" server)
261 (let* ((open-server-function
262 (gnus-get-function gnus-command-method 'open-server))
265 (funcall open-server-function
266 (nth 1 gnus-command-method)
267 (nthcdr 2 gnus-command-method))
269 (gnus-message 1 "Unable to open server %s due to: %s"
270 server (error-message-string err))
275 (gnus-message 1 "Quit trying to open server %s" server))
278 ;; If this hasn't been opened before, we add it to the list.
280 (setq elem (list gnus-command-method nil)
281 gnus-opened-servers (cons elem gnus-opened-servers)))
282 ;; Set the status of this server.