Commit resolve.
[gnus] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend interface functions for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (require 'gnus)
30 (require 'message)
31 (require 'gnus-range)
32
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")
38
39 (defcustom gnus-open-server-hook nil
40   "Hook called just before opening connection to the news server."
41   :group 'gnus-start
42   :type 'hook)
43
44 (defcustom gnus-after-set-mark-hook nil
45   "Hook called just after marks are set in a group."
46   :group 'gnus-start
47   :type 'hook)
48
49 (defcustom gnus-before-update-mark-hook nil
50   "Hook called just before marks are updated in a group."
51   :group 'gnus-start
52   :type 'hook)
53
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
59 server denied."
60   :version "22.1"
61   :group 'gnus-start
62   :type '(choice (const :tag "Ask" nil)
63                  (const :tag "Deny server" denied)
64                  (const :tag "Unplug Agent" offline)))
65
66 (defvar gnus-internal-registry-spool-current-method nil
67   "The current method, for the registry.")
68
69
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)
73               'denied)
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))))
78
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))))
89
90 ;;;
91 ;;; Server Communication
92 ;;;
93
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."
97   (let (how)
98     (if gnus-current-select-method
99         ;; Stream is already opened.
100         nil
101       ;; Open NNTP server.
102       (when confirm
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)))
109
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
121                                   (expand-file-name
122                                    (substring gnus-nntp-server 1) "~/")))
123                            (list 'nnmh-get-new-mail nil)))
124                     (t
125                      (list 'nntp gnus-nntp-server)))))
126
127       (setq how (car gnus-select-method))
128       (cond
129        ((eq how 'nnspool)
130         (require 'nnspool)
131         (gnus-message 5 "Looking up local news spool..."))
132        ((eq how 'nnmh)
133         (require 'nnmh)
134         (gnus-message 5 "Looking up mh spool..."))
135        (t
136         (require 'nntp)))
137       (setq gnus-current-select-method gnus-select-method)
138       (gnus-run-hooks 'gnus-open-server-hook)
139
140       ;; Partially validate agent covered methods now that the
141       ;; gnus-select-method is known.
142
143       (if gnus-agent
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
148           ;; correctly.
149           (gnus-agent-read-servers-validate-native gnus-select-method))
150
151       (or
152        ;; gnus-open-server-hook might have opened it
153        (gnus-server-opened gnus-select-method)
154        (gnus-open-server gnus-select-method)
155        gnus-batch-mode
156        (gnus-y-or-n-p
157         (format
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))))))
163
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))))
169
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))
175         result)
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.
181         t
182       ;; Open the server.
183       (unless silent
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)
188       (prog1
189           (setq result (gnus-open-server method))
190         (unless silent
191           (gnus-message
192            (if result 5 3)
193            "Opening %s server%s...%s" (car method)
194            (if (equal (nth 1 method) "") ""
195              (format " on %s" (nth 1 method)))
196            (if result
197                "done"
198              (format "failed: %s"
199                      (nnheader-get-report-string (car method))))))))))
200
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.
204   (unless method
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))
211                        (car 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
216                          (cons function
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)
223       (unless (car method)
224         (error "Trying to require a method that doesn't exist"))
225       (require (car method))
226       (when (not (fboundp func))
227         (if noerror
228             (setq func nil)
229           (error "No such function: %s" func))))
230     func))
231
232 \f
233 ;;;
234 ;;; Interface functions to the backends.
235 ;;;
236
237 (defun gnus-method-denied-p (method)
238   (eq (nth 1 (assoc method gnus-opened-servers))
239       'denied))
240
241 (defvar gnus-backend-trace t)
242
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)
257         (progn
258           (gnus-message 1 "Denied server %s" server)
259           nil)
260       ;; Open the server.
261       (let* ((open-server-function
262               (gnus-get-function gnus-command-method 'open-server))
263              (result
264               (condition-case err
265                   (funcall open-server-function
266                            (nth 1 gnus-command-method)
267                            (nthcdr 2 gnus-command-method))
268                 (error
269                  (gnus-message 1 "Unable to open server %s due to: %s"
270                                server (error-message-string err))
271                  nil)
272                 (quit
273                  (if debug-on-quit
274                      (debug "Quit")
275                    (gnus-message 1 "Quit trying to open server %s" server))
276                  nil)))
277              open-offline)
278         ;; If this hasn't been opened before, we add it to the list.
279         (unless elem
280           (setq elem (list gnus-command-method nil)
281                 gnus-opened-servers (cons elem gnus-opened-servers)))
282         ;; Set the status of this server.
283         (setcar
284          (cdr elem)