*** empty log message ***
[gnus] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend inteface functions for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus-load)
29 (require 'gnus)
30
31 (defvar gnus-open-server-hook nil
32   "*A hook called just before opening connection to the news server.")
33
34 ;;;
35 ;;; Server Communication
36 ;;;
37
38 (defun gnus-start-news-server (&optional confirm)
39   "Open a method for getting news.
40 If CONFIRM is non-nil, the user will be asked for an NNTP server."
41   (let (how)
42     (if gnus-current-select-method
43         ;; Stream is already opened.
44         nil
45       ;; Open NNTP server.
46       (unless gnus-nntp-service
47         (setq gnus-nntp-server nil))
48       (when confirm
49         ;; Read server name with completion.
50         (setq gnus-nntp-server
51               (completing-read "NNTP server: "
52                                (mapcar (lambda (server) (list server))
53                                        (cons (list gnus-nntp-server)
54                                              gnus-secondary-servers))
55                                nil nil gnus-nntp-server)))
56
57       (when (and gnus-nntp-server
58                  (stringp gnus-nntp-server)
59                  (not (string= gnus-nntp-server "")))
60         (setq gnus-select-method
61               (cond ((or (string= gnus-nntp-server "")
62                          (string= gnus-nntp-server "::"))
63                      (list 'nnspool (system-name)))
64                     ((string-match "^:" gnus-nntp-server)
65                      (list 'nnmh gnus-nntp-server
66                            (list 'nnmh-directory
67                                  (file-name-as-directory
68                                   (expand-file-name
69                                    (concat "~/" (substring
70                                                  gnus-nntp-server 1)))))
71                            (list 'nnmh-get-new-mail nil)))
72                     (t
73                      (list 'nntp gnus-nntp-server)))))
74
75       (setq how (car gnus-select-method))
76       (cond
77        ((eq how 'nnspool)
78         (require 'nnspool)
79         (gnus-message 5 "Looking up local news spool..."))
80        ((eq how 'nnmh)
81         (require 'nnmh)
82         (gnus-message 5 "Looking up mh spool..."))
83        (t
84         (require 'nntp)))
85       (setq gnus-current-select-method gnus-select-method)
86       (run-hooks 'gnus-open-server-hook)
87       (or
88        ;; gnus-open-server-hook might have opened it
89        (gnus-server-opened gnus-select-method)
90        (gnus-open-server gnus-select-method)
91        (gnus-y-or-n-p
92         (format
93          "%s (%s) open error: '%s'.  Continue? "
94          (car gnus-select-method) (cadr gnus-select-method)
95          (gnus-status-message gnus-select-method)))
96        (gnus-error 1 "Couldn't open server on %s"
97                    (nth 1 gnus-select-method))))))
98
99 (defun gnus-check-group (group)
100   "Try to make sure that the server where GROUP exists is alive."
101   (let ((method (gnus-find-method-for-group group)))
102     (or (gnus-server-opened method)
103         (gnus-open-server method))))
104
105 (defun gnus-check-server (&optional method silent)
106   "Check whether the connection to METHOD is down.
107 If METHOD is nil, use `gnus-select-method'.
108 If it is down, start it up (again)."
109   (let ((method (or method gnus-select-method)))
110     ;; Transform virtual server names into select methods.
111     (when (stringp method)
112       (setq method (gnus-server-to-method method)))
113     (if (gnus-server-opened method)
114         ;; The stream is already opened.
115         t
116       ;; Open the server.
117       (unless silent
118         (gnus-message 5 "Opening %s server%s..." (car method)
119                       (if (equal (nth 1 method) "") ""
120                         (format " on %s" (nth 1 method)))))
121       (run-hooks 'gnus-open-server-hook)
122       (prog1
123           (gnus-open-server method)
124         (unless silent
125           (message ""))))))
126
127 (defun gnus-get-function (method function &optional noerror)
128   "Return a function symbol based on METHOD and FUNCTION."
129   ;; Translate server names into methods.
130   (unless method
131     (error "Attempted use of a nil select method"))
132   (when (stringp method)
133     (setq method (gnus-server-to-method method)))
134   (let ((func (intern (format "%s-%s" (car method) function))))
135     ;; If the functions isn't bound, we require the backend in
136     ;; question.
137     (unless (fboundp func)
138       (require (car method))
139       (when (and (not (fboundp func))
140                  (not noerror))
141         ;; This backend doesn't implement this function.
142         (error "No such function: %s" func)))
143     func))
144
145 \f
146 ;;;
147 ;;; Interface functions to the backends.
148 ;;;
149
150 (defun gnus-open-server (method)
151   "Open a connection to METHOD."
152   (when (stringp method)
153     (setq method (gnus-server-to-method method)))
154   (let ((elem (assoc method gnus-opened-servers)))
155     ;; If this method was previously denied, we just return nil.
156     (if (eq (nth 1 elem) 'denied)
157         (progn
158           (gnus-message 1 "Denied server")
159           nil)
160       ;; Open the server.
161       (let ((result
162              (funcall (gnus-get-function method 'open-server)
163                       (nth 1 method) (nthcdr 2 method))))
164         ;; If this hasn't been opened before, we add it to the list.
165         (unless elem
166           (setq elem (list method nil)
167                 gnus-opened-servers (cons elem gnus-opened-servers)))
168         ;; Set the status of this server.
169         (setcar (cdr elem) (if result 'ok 'denied))
170         ;; Return the result from the "open" call.
171         result))))
172
173 (defun gnus-close-server (method)
174   "Close the connection to METHOD."
175   (when (stringp method)
176     (setq method (gnus-server-to-method method)))
177   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
178
179 (defun gnus-request-list (method)
180   "Request the active file from METHOD."
181   (when (stringp method)
182     (setq method (gnus-server-to-method method)))
183   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
184
185 (defun gnus-request-list-newsgroups (method)
186   "Request the newsgroups file from METHOD."
187   (when (stringp method)
188     (setq method (gnus-server-to-method method)))
189   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
190
191 (defun gnus-request-newgroups (date method)
192   "Request all new groups since DATE from METHOD."
193   (when (stringp method)
194     (setq method (gnus-server-to-method method)))
195   (let ((func (gnus-get-function method 'request-newgroups t)))
196     (when func
197       (funcall func date (nth 1 method)))))
198
199 (defun gnus-server-opened (method)
200   "Check whether a connection to METHOD has been opened."
201   (when (stringp method)
202     (setq method (gnus-server-to-method method)))
203   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
204
205 (defun gnus-status-message (method)
206   "Return the status message from METHOD.
207 If METHOD is a string, it is interpreted as a group name.   The method
208 this group uses will be queried."
209   (let ((method (if (stringp method) (gnus-find-method-for-group method)
210                   method)))
211     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
212
213 (defun gnus-request-group (group &optional dont-check method)
214   "Request GROUP.  If DONT-CHECK, no information is required."
215   (let ((method (or method (gnus-find-method-for-group group))))
216     (when (stringp method)
217       (setq method (gnus-server-to-method method)))
218     (funcall (gnus-get-function method 'request-group)
219              (gnus-group-real-name group) (nth 1 method) dont-check)))
220
221 (defun gnus-list-active-group (group)
222   "Request active information on GROUP."
223   (let ((method (gnus-find-method-for-group group))
224         (func 'list-active-group))
225     (when (gnus-check-backend-function func group)
226       (funcall (gnus-get-function method func)
227                (gnus-group-real-name group) (nth 1 method)))))
228
229 (defun gnus-request-group-description (group)
230   "Request a description of GROUP."
231   (let ((method (gnus-find-method-for-group group))
232         (func 'request-group-description))
233     (when (gnus-check-backend-function func group)
234       (funcall (gnus-get-function method func)
235                (gnus-group-real-name group) (nth 1 method)))))
236
237 (defun gnus-close-group (group)
238   "Request the GROUP be closed."
239   (let ((method (gnus-find-method-for-group group)))
240     (funcall (gnus-get-function method 'close-group)
241              (gnus-group-real-name group) (nth 1 method))))
242
243 (defun gnus-retrieve-headers (articles group &optional fetch-old)
244   "Request headers for ARTICLES in GROUP.
245 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
246   (let ((method (gnus-find-method-for-group group)))
247     (if (and gnus-use-cache (numberp (car articles)))
248         (gnus-cache-retrieve-headers articles group fetch-old)
249       (funcall (gnus-get-function method 'retrieve-headers)
250                articles (gnus-group-real-name group) (nth 1 method)
251                fetch-old))))
252
253 (defun gnus-retrieve-groups (groups method)
254   "Request active information on GROUPS from METHOD."
255   (when (stringp method)
256     (setq method (gnus-server-to-method method)))
257   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
258
259 (defun gnus-request-type (group &optional article)
260   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
261   (let ((method (gnus-find-method-for-group group)))
262     (if (not (gnus-check-backend-function 'request-type (car method)))
263         'unknown
264       (funcall (gnus-get-function method 'request-type)
265                (gnus-group-real-name group) article))))
266
267 (defun gnus-request-update-mark (group article mark)
268   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
269   (let ((method (gnus-find-method-for-group group)))
270     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
271         mark
272       (funcall (gnus-get-function method 'request-update-mark)
273                (gnus-group-real-name group) article mark))))
274
275 (defun gnus-request-article (article group &optional buffer)
276   "Request the ARTICLE in GROUP.
277 ARTICLE can either be an article number or an article Message-ID.
278 If BUFFER, insert the article in that group."
279   (let ((method (gnus-find-method-for-group group)))
280     (funcall (gnus-get-function method 'request-article)
281              article (gnus-group-real-name group) (nth 1 method) buffer)))
282
283 (defun gnus-request-head (article group)
284   "Request the head of ARTICLE in GROUP."
285   (let* ((method (gnus-find-method-for-group group))
286          (head (gnus-get-function method 'request-head t)))
287     (if (fboundp head)
288         (funcall head article (gnus-group-real-name group) (nth 1 method))
289       (let ((res (gnus-request-article article group)))
290         (when res
291           (save-excursion
292             (set-buffer nntp-server-buffer)
293             (goto-char (point-min))
294             (when (search-forward "\n\n" nil t)
295               (delete-region (1- (point)) (point-max)))
296             (nnheader-fold-continuation-lines)))
297         res))))
298
299 (defun gnus-request-body (article group)
300   "Request the body of ARTICLE in GROUP."
301   (let ((method (gnus-find-method-for-group group)))
302     (funcall (gnus-get-function method 'request-body)
303              article (gnus-group-real-name group) (nth 1 method))))
304
305 (defun gnus-request-post (method)
306   "Post the current buffer using METHOD."
307   (when (stringp method)
308     (setq method (gnus-server-to-method method)))
309   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
310
311 (defun gnus-request-scan (group method)
312   "Request a SCAN being performed in GROUP from METHOD.
313 If GROUP is nil, all groups on METHOD are scanned."
314   (let ((method (if group (gnus-find-method-for-group group) method)))
315     (funcall (gnus-get-function method 'request-scan)
316              (and group (gnus-group-real-name group)) (nth 1 method))))
317
318 (defsubst gnus-request-update-info (info method)
319   "Request that METHOD update INFO."
320   (when (stringp method)
321     (setq method (gnus-server-to-method method)))
322   (when (gnus-check-backend-function 'request-update-info (car method))
323     (funcall (gnus-get-function method 'request-update-info)
324              (gnus-group-real-name (gnus-info-group info))
325              info (nth 1 method))))
326
327 (defun gnus-request-expire-articles (articles group &optional force)
328   (let ((method (gnus-find-method-for-group group)))
329     (funcall (gnus-get-function method 'request-expire-articles)
330              articles (gnus-group-real-name group) (nth 1 method)
331              force)))
332
333 (defun gnus-request-move-article
334   (article group server accept-function &optional last)
335   (let ((method (gnus-find-method-for-group group)))
336     (funcall (gnus-get-function method 'request-move-article)
337              article (gnus-group-real-name group)
338              (nth 1 method) accept-function last)))
339
340 (defun gnus-request-accept-article (group method &optional last)
341   ;; Make sure there's a newline at the end of the article.
342   (when (stringp method)
343     (setq method (gnus-server-to-method method)))
344   (when (and (not method)
345              (stringp group))
346     (setq method (gnus-group-name-to-method group)))
347   (goto-char (point-max))
348   (unless (bolp)
349     (insert "\n"))
350   (let ((func (car (or method (gnus-find-method-for-group group)))))
351     (funcall (intern (format "%s-request-accept-article" func))
352              (if (stringp group) (gnus-group-real-name group) group)
353              (cadr method)
354              last)))
355
356 (defun gnus-request-replace-article (article group buffer)
357   (let ((func (car (gnus-find-method-for-group group))))
358     (funcall (intern (format "%s-request-replace-article" func))
359              article (gnus-group-real-name group) buffer)))
360
361 (defun gnus-request-associate-buffer (group)
362   (let ((method (gnus-find-method-for-group group)))
363     (funcall (gnus-get-function method 'request-associate-buffer)
364              (gnus-group-real-name group))))
365
366 (defun gnus-request-restore-buffer (article group)
367   "Request a new buffer restored to the state of ARTICLE."
368   (let ((method (gnus-find-method-for-group group)))
369     (funcall (gnus-get-function method 'request-restore-buffer)
370              article (gnus-group-real-name group) (nth 1 method))))
371
372 (defun gnus-request-create-group (group &optional method)
373   (when (stringp method)
374     (setq method (gnus-server-to-method method)))
375   (let ((method (or method (gnus-find-method-for-group group))))
376     (funcall (gnus-get-function method 'request-create-group)
377              (gnus-group-real-name group) (nth 1 method))))
378
379 (defun gnus-request-delete-group (group &optional force)
380   (let ((method (gnus-find-method-for-group group)))
381     (funcall (gnus-get-function method 'request-delete-group)
382              (gnus-group-real-name group) force (nth 1 method))))
383
384 (defun gnus-request-rename-group (group new-name)
385   (let ((method (gnus-find-method-for-group group)))
386     (funcall (gnus-get-function method 'request-rename-group)
387              (gnus-group-real-name group)
388              (gnus-group-real-name new-name) (nth 1 method))))
389
390 (defun gnus-close-backends ()
391   ;; Send a close request to all backends that support such a request.
392   (let ((methods gnus-valid-select-methods)
393         func method)
394     (while (setq method (pop methods))
395       (when (fboundp (setq func (intern
396                                  (concat (car method) "-request-close"))))
397         (funcall func)))))
398
399 (defun gnus-asynchronous-p (method)
400   (let ((func (gnus-get-function method 'asynchronous-p t)))
401     (when (fboundp func)
402       (funcall func))))
403
404 (defun gnus-remove-denial (method)
405   (when (stringp method)
406     (setq method (gnus-server-to-method method)))
407   (let* ((elem (assoc method gnus-opened-servers))
408          (status (cadr elem)))
409     ;; If this hasn't been opened before, we add it to the list.
410     (when (eq status 'denied)
411       ;; Set the status of this server.
412       (setcar (cdr elem) 'closed))))
413
414 (provide 'gnus-int)
415
416 ;;; gnus-int.el ends here