1 ;;; gnus-int.el --- backend interface functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
36 (autoload 'gnus-agent-expire "gnus-agent")
37 (autoload 'gnus-agent-regenerate-group "gnus-agent")
38 (autoload 'gnus-agent-read-servers-validate-native "gnus-agent")
40 (defcustom gnus-open-server-hook nil
41 "Hook called just before opening connection to the news server."
45 (defcustom gnus-server-unopen-status nil
46 "The default status if the server is not able to open.
47 If the server is covered by Gnus agent, the possible values are
48 `denied', set the server denied; `offline', set the server offline;
49 nil, ask user. If the server is not covered by Gnus agent, set the
53 :type '(choice (const :tag "Ask" nil)
54 (const :tag "Deny server" denied)
55 (const :tag "Unplug Agent" offline)))
57 (defvar gnus-internal-registry-spool-current-method nil
58 "The current method, for the registry.")
61 ;;; Server Communication
64 (defun gnus-start-news-server (&optional confirm)
65 "Open a method for getting news.
66 If CONFIRM is non-nil, the user will be asked for an NNTP server."
68 (if gnus-current-select-method
69 ;; Stream is already opened.
72 (unless gnus-nntp-service
73 (setq gnus-nntp-server nil))
75 ;; Read server name with completion.
76 (setq gnus-nntp-server
77 (completing-read "NNTP server: "
79 (cons (list gnus-nntp-server)
80 gnus-secondary-servers))
81 nil nil gnus-nntp-server)))
83 (when (and gnus-nntp-server
84 (stringp gnus-nntp-server)
85 (not (string= gnus-nntp-server "")))
86 (setq gnus-select-method
87 (cond ((or (string= gnus-nntp-server "")
88 (string= gnus-nntp-server "::"))
89 (list 'nnspool (system-name)))
90 ((string-match "^:" gnus-nntp-server)
91 (list 'nnmh gnus-nntp-server
93 (file-name-as-directory
95 (substring gnus-nntp-server 1) "~/")))
96 (list 'nnmh-get-new-mail nil)))
98 (list 'nntp gnus-nntp-server)))))
100 (setq how (car gnus-select-method))
104 (gnus-message 5 "Looking up local news spool..."))
107 (gnus-message 5 "Looking up mh spool..."))
110 (setq gnus-current-select-method gnus-select-method)
111 (gnus-run-hooks 'gnus-open-server-hook)
113 ;; Partially validate agent covered methods now that the
114 ;; gnus-select-method is known.
117 ;; NOTE: This is here for one purpose only. By validating
118 ;; the current select method, it converts the old 5.10.3,
119 ;; and earlier, format to the current format. That enables
120 ;; the agent code within gnus-open-server to function
122 (gnus-agent-read-servers-validate-native gnus-select-method))
125 ;; gnus-open-server-hook might have opened it
126 (gnus-server-opened gnus-select-method)
127 (gnus-open-server gnus-select-method)
131 "%s (%s) open error: '%s'. Continue? "
132 (car gnus-select-method) (cadr gnus-select-method)
133 (gnus-status-message gnus-select-method)))
134 (gnus-error 1 "Couldn't open server on %s"
135 (nth 1 gnus-select-method))))))
137 (defun gnus-check-group (group)
138 "Try to make sure that the server where GROUP exists is alive."
139 (let ((method (gnus-find-method-for-group group)))
140 (or (gnus-server-opened method)
141 (gnus-open-server method))))
143 (defun gnus-check-server (&optional method silent)
144 "Check whether the connection to METHOD is down.
145 If METHOD is nil, use `gnus-select-method'.
146 If it is down, start it up (again)."
147 (let ((method (or method gnus-select-method))
149 ;; Transform virtual server names into select methods.
150 (when (stringp method)
151 (setq method (gnus-server-to-method method)))
152 (if (gnus-server-opened method)
153 ;; The stream is already opened.
157 (gnus-message 5 "Opening %s server%s..." (car method)
158 (if (equal (nth 1 method) "") ""
159 (format " on %s" (nth 1 method)))))
160 (gnus-run-hooks 'gnus-open-server-hook)
163 (setq result (gnus-open-server method))
164 (quit (message "Quit gnus-check-server")
167 (gnus-message 5 "Opening %s server%s...%s" (car method)
168 (if (equal (nth 1 method) "") ""
169 (format " on %s" (nth 1 method)))
170 (if result "done" "failed")))))))
172 (defun gnus-get-function (method function &optional noerror)
173 "Return a function symbol based on METHOD and FUNCTION."
174 ;; Translate server names into methods.
176 (error "Attempted use of a nil select method"))
177 (when (stringp method)
178 (setq method (gnus-server-to-method method)))
179 ;; Check cache of constructed names.
180 (let* ((method-sym (if gnus-agent
181 (inline (gnus-agent-get-function method))
183 (method-fns (get method-sym 'gnus-method-functions))
184 (func (let ((method-fnlist-elt (assq function method-fns)))
185 (unless method-fnlist-elt
186 (setq method-fnlist-elt
188 (intern (format "%s-%s" method-sym function))))
189 (put method-sym 'gnus-method-functions
190 (cons method-fnlist-elt method-fns)))
191 (cdr method-fnlist-elt))))
192 ;; Maybe complain if there is no function.
193 (unless (fboundp func)
195 (error "Trying to require a method that doesn't exist"))
196 (require (car method))
197 (when (not (fboundp func))
200 (error "No such function: %s" func))))
205 ;;; Interface functions to the backends.
208 (defun gnus-open-server (gnus-command-method)
209 "Open a connection to GNUS-COMMAND-METHOD."
210 (when (stringp gnus-command-method)
211 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
212 (let ((elem (assoc gnus-command-method gnus-opened-servers))
213 (server (gnus-method-to-server-name gnus-command-method)))
214 ;; If this method was previously denied, we just return nil.
215 (if (eq (nth 1 elem) 'denied)
217 (gnus-message 1 "Denied server %s" server)
220 (let* ((open-server-function (gnus-get-function gnus-command-method 'open-server))
223 (funcall open-server-function
224 (nth 1 gnus-command-method)
225 (nthcdr 2 gnus-command-method))
227 (gnus-message 1 (format
228 "Unable to open server %s due to: %s"
229 server (error-message-string err)))
232 (gnus-message 1 "Quit trying to open server %s" server)
235 ;; If this hasn't been opened before, we add it to the list.
237 (setq elem (list gnus-command-method nil)
238 gnus-opened-servers (cons elem gnus-opened-servers)))
239 ;; Set the status of this server.
242 (if (eq open-server-function #'nnagent-open-server)
243 ;; The agent's backend has a "special" status
247 (gnus-agent-method-p gnus-command-method))
248 (cond (gnus-server-unopen-status
249 ;; Set the server's status to the unopen
250 ;; status. If that status is offline,
251 ;; recurse to open the agent's backend.
252 (setq open-offline (eq gnus-server-unopen-status 'offline))
253 gnus-server-unopen-status)
255 (not gnus-batch-mode)
258 "Unable to open server %s, go offline? "
260 (setq open-offline t)
263 ;; This agentized server was still denied
266 ;; This unagentized server must be denied
269 ;; NOTE: I MUST set the server's status to offline before this
270 ;; recursive call as this status will drive the
271 ;; gnus-get-function (called above) to return the agent's
274 ;; Recursively open this offline server to perform the
275 ;; open-server function of the agent's backend.
276 (let ((gnus-server-unopen-status 'denied))
277 ;; Bind gnus-server-unopen-status to avoid recursively
278 ;; prompting with "go offline?". This is only a concern
279 ;; when the agent's backend fails to open the server.
280 (gnus-open-server gnus-command-method))
283 (defun gnus-close-server (gnus-command-method)
284 "Close the connection to GNUS-COMMAND-METHOD."
285 (when (stringp gnus-command-method)
286 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
287 (funcall (gnus-get-function gnus-command-method 'close-server)
288 (nth 1 gnus-command-method)))
290 (defun gnus-request-list (gnus-command-method)
291 "Request the active file from GNUS-COMMAND-METHOD."
292 (when (stringp gnus-command-method)
293 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
294 (funcall (gnus-get-function gnus-command-method 'request-list)
295 (nth 1 gnus-command-method)))
297 (defun gnus-request-list-newsgroups (gnus-command-method)
298 "Request the newsgroups file from GNUS-COMMAND-METHOD."
299 (when (stringp gnus-command-method)
300 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
301 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
302 (nth 1 gnus-command-method)))
304 (defun gnus-request-newgroups (date gnus-command-method)
305 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
306 (when (stringp gnus-command-method)
307 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
308 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
310 (funcall func date (nth 1 gnus-command-method)))))
312 (defun gnus-server-opened (gnus-command-method)
313 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
314 (unless (eq (gnus-server-status gnus-command-method)
316 (when (stringp gnus-command-method)
317 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
318 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
319 (nth 1 gnus-command-method))))
321 (defun gnus-status-message (gnus-command-method)
322 "Return the status message from GNUS-COMMAND-METHOD.
323 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
324 name. The method this group uses will be queried."
325 (let ((gnus-command-method
326 (if (stringp gnus-command-method)
327 (gnus-find-method-for-group gnus-command-method)
328 gnus-command-method)))
329 (funcall (gnus-get-function gnus-command-method 'status-message)
330 (nth 1 gnus-command-method))))
332 (defun gnus-request-regenerate (gnus-command-method)
333 "Request a data generation from GNUS-COMMAND-METHOD."
334 (when (stringp gnus-command-method)
335 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
336 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
337 (nth 1 gnus-command-method)))
339 (defun gnus-request-compact-group (group)
340 (let* ((method (gnus-find-method-for-group group))
341 (gnus-command-method method)
343 (funcall (gnus-get-function gnus-command-method
344 'request-compact-group)
345 (gnus-group-real-name group)
346 (nth 1 gnus-command-method) t)))
349 (defun gnus-request-compact (gnus-command-method)
350 "Request groups compaction from GNUS-COMMAND-METHOD."
351 (when (stringp gnus-command-method)
352 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
353 (funcall (gnus-get-function gnus-command-method 'request-compact)
354 (nth 1 gnus-command-method)))
356 (defun gnus-request-group (group &optional dont-check gnus-command-method)
357 "Request GROUP. If DONT-CHECK, no information is required."
358 (let ((gnus-command-method
359 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
360 (when (stringp gnus-command-method)
361 (setq gnus-command-method
362 (inline (gnus-server-to-method gnus-command-method))))
363 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
364 (gnus-group-real-name group) (nth 1 gnus-command-method)
367 (defun gnus-list-active-group (group)
368 "Request active information on GROUP."
369 (let ((gnus-command-method (gnus-find-method-for-group group))
370 (func 'list-active-group))
371 (when (gnus-check-backend-function func group)
372 (funcall (gnus-get-function gnus-command-method func)
373 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
375 (defun gnus-request-group-description (group)
376 "Request a description of GROUP."
377 (let ((gnus-command-method (gnus-find-method-for-group group))
378 (func 'request-group-description))
379 (when (gnus-check-backend-function func group)
380 (funcall (gnus-get-function gnus-command-method func)
381 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
383 (defun gnus-request-group-articles (group)
384 "Request a list of existing articles in GROUP."
385 (let ((gnus-command-method (gnus-find-method-for-group group))
386 (func 'request-group-articles))
387 (when (gnus-check-backend-function func group)
388 (funcall (gnus-get-function gnus-command-method func)
389 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
391 (defun gnus-close-group (group)
392 "Request the GROUP be closed."
393 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
394 (funcall (gnus-get-function gnus-command-method 'close-group)
395 (gnus-group-real-name group) (nth 1 gnus-command-method))))
397 (defun gnus-retrieve-headers (articles group &optional fetch-old)
398 "Request headers for ARTICLES in GROUP.
399 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
400 (let ((gnus-command-method (gnus-find-method-for-group group)))
402 ((and gnus-use-cache (numberp (car articles)))
403 (gnus-cache-retrieve-headers articles group fetch-old))
404 ((and gnus-agent (gnus-online gnus-command-method)
405 (gnus-agent-method-p gnus-command-method))
406 (gnus-agent-retrieve-headers articles group fetch-old))
408 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
409 articles (gnus-group-real-name group)
410 (nth 1 gnus-command-method) fetch-old)))))
412 (defun gnus-retrieve-articles (articles group)
413 "Request ARTICLES in GROUP."
414 (let ((gnus-command-method (gnus-find-method-for-group group)))
415 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
416 articles (gnus-group-real-name group)
417 (nth 1 gnus-command-method))))
419 (defun gnus-retrieve-groups (groups gnus-command-method)
420 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
421 (when (stringp gnus-command-method)
422 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
423 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
424 groups (nth 1 gnus-command-method)))
426 (defun gnus-request-type (group &optional article)
427 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
428 (let ((gnus-command-method (gnus-find-method-for-group group)))
429 (if (not (gnus-check-backend-function
430 'request-type (car gnus-command-method)))
432 (funcall (gnus-get-function gnus-command-method 'request-type)
433 (gnus-group-real-name group) article))))
435 (defun gnus-request-set-mark (group action)
436 "Set marks on articles in the back end."
437 (let ((gnus-command-method (gnus-find-method-for-group group)))
438 (if (not (gnus-check-backend-function
439 'request-set-mark (car gnus-command-method)))
441 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
442 (gnus-group-real-name group) action