*** 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   (funcall (gnus-get-function method 'request-newgroups)
196            date (nth 1 method)))
197
198 (defun gnus-server-opened (method)
199   "Check whether a connection to METHOD has been opened."
200   (when (stringp method)
201     (setq method (gnus-server-to-method method)))
202   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
203
204 (defun gnus-status-message (method)
205   "Return the status message from METHOD.
206 If METHOD is a string, it is interpreted as a group name.   The method
207 this group uses will be queried."
208   (let ((method (if (stringp method) (gnus-find-method-for-group method)
209                   method)))
210     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
211
212 (defun gnus-request-group (group &optional dont-check method)
213   "Request GROUP.  If DONT-CHECK, no information is required."
214   (let ((method (or method (gnus-find-method-for-group group))))
215     (when (stringp method)
216       (setq method (gnus-server-to-method method)))
217     (funcall (gnus-get-function method 'request-group)
218              (gnus-group-real-name group) (nth 1 method) dont-check)))
219
220 (defun gnus-list-active-group (group)
221   "Request active information on GROUP."
222   (let ((method (gnus-find-method-for-group group))
223         (func 'list-active-group))
224     (when (gnus-check-backend-function func group)
225       (funcall (gnus-get-function method func)
226                (gnus-group-real-name group) (nth 1 method)))))
227
228 (defun gnus-request-group-description (group)
229   "Request a description of GROUP."
230   (let ((method (gnus-find-method-for-group group))
231         (func 'request-group-description))
232     (when (gnus-check-backend-function func group)
233       (funcall (gnus-get-function method func)
234                (gnus-group-real-name group) (nth 1 method)))))
235
236 (defun gnus-close-group (group)
237   "Request the GROUP be closed."
238   (let ((method (gnus-find-method-for-group group)))
239     (funcall (gnus-get-function method 'close-group)
240              (gnus-group-real-name group) (nth 1 method))))
241
242 (defun gnus-retrieve-headers (articles group &optional fetch-old)
243   "Request headers for ARTICLES in GROUP.
244 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
245   (let ((method (gnus-find-method-for-group group)))
246     (if (and gnus-use-cache (numberp (car articles)))
247         (gnus-cache-retrieve-headers articles group fetch-old)
248       (funcall (gnus-get-function method 'retrieve-headers)
249                articles (gnus-group-real-name group) (nth 1 method)
250                fetch-old))))
251
252 (defun gnus-retrieve-groups (groups method)
253   "Request active information on GROUPS from METHOD."
254   (when (stringp method)
255     (setq method (gnus-server-to-method method)))
256   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
257
258 (defun gnus-request-type (group &optional article)
259   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
260   (let ((method (gnus-find-method-for-group group)))
261     (if (not (gnus-check-backend-function 'request-type (car method)))
262         'unknown
263       (funcall (gnus-get-function method 'request-type)
264                (gnus-group-real-name group) article))))
265
266 (defun gnus-request-update-mark (group article mark)
267   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
268   (let ((method (gnus-find-method-for-group group)))
269     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
270         mark
271       (funcall (gnus-get-function method 'request-update-mark)
272                (gnus-group-real-name group) article mark))))
273
274 (defun gnus-request-article (article group &optional buffer)
275   "Request the ARTICLE in GROUP.
276 ARTICLE can either be an article number or an article Message-ID.
277 If BUFFER, insert the article in that group."
278   (let ((method (gnus-find-method-for-group group)))
279     (funcall (gnus-get-function method 'request-article)
280              article (gnus-group-real-name group) (nth 1 method) buffer)))
281
282 (defun gnus-request-head (article group)
283   "Request the head of ARTICLE in GROUP."
284   (let* ((method (gnus-find-method-for-group group))
285          (head (gnus-get-function method 'request-head t)))
286     (if (fboundp head)
287         (funcall head article (gnus-group-real-name group) (nth 1 method))
288       (let ((res (gnus-request-article article group)))
289         (when res
290           (save-excursion
291             (set-buffer nntp-server-buffer)
292             (goto-char (point-min))
293             (when (search-forward "\n\n" nil t)
294               (delete-region (1- (point)) (point-max)))
295             (nnheader-fold-continuation-lines)))
296         res))))
297
298 (defun gnus-request-body (article group)
299   "Request the body of ARTICLE in GROUP."
300   (let ((method (gnus-find-method-for-group group)))
301     (funcall (gnus-get-function method 'request-body)
302              article (gnus-group-real-name group) (nth 1 method))))
303
304 (defun gnus-request-post (method)
305   "Post the current buffer using METHOD."
306   (when (stringp method)
307     (setq method (gnus-server-to-method method)))
308   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
309
310 (defun gnus-request-scan (group method)
311   "Request a SCAN being performed in GROUP from METHOD.
312 If GROUP is nil, all groups on METHOD are scanned."
313   (let ((method (if group (gnus-find-method-for-group group) method)))
314     (funcall (gnus-get-function method 'request-scan)
315              (and group (gnus-group-real-name group)) (nth 1 method))))
316
317 (defsubst gnus-request-update-info (info method)
318   "Request that METHOD update INFO."
319   (when (stringp method)
320     (setq method (gnus-server-to-method method)))
321   (when (gnus-check-backend-function 'request-update-info (car method))
322     (funcall (gnus-get-function method 'request-update-info)
323              (gnus-group-real-name (gnus-info-group info))
324              info (nth 1 method))))
325
326 (defun gnus-request-expire-articles (articles group &optional force)
327   (let ((method (gnus-find-method-for-group group)))
328     (funcall (gnus-get-function method 'request-expire-articles)
329              articles (gnus-group-real-name group) (nth 1 method)
330              force)))
331
332 (defun gnus-request-move-article
333   (article group server accept-function &optional last)
334   (let ((method (gnus-find-method-for-group group)))
335     (funcall (gnus-get-function method 'request-move-article)
336              article (gnus-group-real-name group)
337              (nth 1 method) accept-function last)))
338
339 (defun gnus-request-accept-article (group method &optional last)
340   ;; Make sure there's a newline at the end of the article.
341   (when (stringp method)
342     (setq method (gnus-server-to-method method)))
343   (when (and (not method)
344              (stringp group))
345     (setq method (gnus-group-name-to-method group)))
346   (goto-char (point-max))
347   (unless (bolp)
348     (insert "\n"))
349   (let ((func (car (or method (gnus-find-method-for-group group)))))
350     (funcall (intern (format "%s-request-accept-article" func))
351              (if (stringp group) (gnus-group-real-name group) group)
352              (cadr method)
353              last)))
354
355 (defun gnus-request-replace-article (article group buffer)
356   (let ((func (car (gnus-find-method-for-group group))))
357     (funcall (intern (format "%s-request-replace-article" func))
358              article (gnus-group-real-name group) buffer)))
359
360 (defun gnus-request-associate-buffer (group)
361   (let ((method (gnus-find-method-for-group group)))
362     (funcall (gnus-get-function method 'request-associate-buffer)
363              (gnus-group-real-name group))))
364
365 (defun gnus-request-restore-buffer (article group)
366   "Request a new buffer restored to the state of ARTICLE."
367   (let ((method (gnus-find-method-for-group group)))
368     (funcall (gnus-get-function method 'request-restore-buffer)
369              article (gnus-group-real-name group) (nth 1 method))))
370
371 (defun gnus-request-create-group (group &optional method)
372   (when (stringp method)
373     (setq method (gnus-server-to-method method)))
374   (let ((method (or method (gnus-find-method-for-group group))))
375     (funcall (gnus-get-function method 'request-create-group)
376              (gnus-group-real-name group) (nth 1 method))))
377
378 (defun gnus-request-delete-group (group &optional force)
379   (let ((method (gnus-find-method-for-group group)))
380     (funcall (gnus-get-function method 'request-delete-group)
381              (gnus-group-real-name group) force (nth 1 method))))
382
383 (defun gnus-request-rename-group (group new-name)
384   (let ((method (gnus-find-method-for-group group)))
385     (funcall (gnus-get-function method 'request-rename-group)
386              (gnus-group-real-name group)
387              (gnus-group-real-name new-name) (nth 1 method))))
388
389 (defun gnus-close-backends ()
390   ;; Send a close request to all backends that support such a request.
391   (let ((methods gnus-valid-select-methods)
392         func)
393     (while methods
394       (if (fboundp (setq func (intern (concat (caar methods)
395                                               "-request-close"))))
396           (funcall func))
397       (setq methods (cdr methods)))))
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 (provide 'gnus-int)
405
406 ;;; gnus-int.el ends here