To strip or not.
[gnus] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        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 2, or (at your option)
13 ;; 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; 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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32
33 (defcustom gnus-open-server-hook nil
34   "Hook called just before opening connection to the news server."
35   :group 'gnus-start
36   :type 'hook)
37
38 ;;;
39 ;;; Server Communication
40 ;;;
41
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."
45   (let (how)
46     (if gnus-current-select-method
47         ;; Stream is already opened.
48         nil
49       ;; Open NNTP server.
50       (unless gnus-nntp-service
51         (setq gnus-nntp-server nil))
52       (when confirm
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)))
60
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
70                            (list 'nnmh-directory
71                                  (file-name-as-directory
72                                   (expand-file-name
73                                    (concat "~/" (substring
74                                                  gnus-nntp-server 1)))))
75                            (list 'nnmh-get-new-mail nil)))
76                     (t
77                      (list 'nntp gnus-nntp-server)))))
78
79       (setq how (car gnus-select-method))
80       (cond
81        ((eq how 'nnspool)
82         (require 'nnspool)
83         (gnus-message 5 "Looking up local news spool..."))
84        ((eq how 'nnmh)
85         (require 'nnmh)
86         (gnus-message 5 "Looking up mh spool..."))
87        (t
88         (require 'nntp)))
89       (setq gnus-current-select-method gnus-select-method)
90       (gnus-run-hooks 'gnus-open-server-hook)
91       (or
92        ;; gnus-open-server-hook might have opened it
93        (gnus-server-opened gnus-select-method)
94        (gnus-open-server gnus-select-method)
95        gnus-batch-mode
96        (gnus-y-or-n-p
97         (format
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))))))
103
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))))
109
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.
120         t
121       ;; Open the server.
122       (unless silent
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)
127       (prog1
128           (gnus-open-server method)
129         (unless silent
130           (message ""))))))
131
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.
135   (unless method
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)
142                        (car 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
147                          (cons function
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)
154       (unless (car method)
155         (error "Trying to require a method that doesn't exist"))
156       (require (car method))
157       (when (not (fboundp func))
158         (if noerror
159             (setq func nil)
160           (error "No such function: %s" func))))
161     func))
162
163 \f
164 ;;;
165 ;;; Interface functions to the backends.
166 ;;;
167
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)
175         (progn
176           (gnus-message 1 "Denied server")
177           nil)
178       ;; Open the server.
179       (let ((result
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.
184         (unless elem
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.
190         result))))
191
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)))
196   (funcall (gnus-get-