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