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