Feedback from `t-gnus-6_15-quimby' branch.
[elisp/gnus.git-] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33
34 (defcustom gnus-open-server-hook nil
35   "Hook called just before opening connection to the news server."
36   :group 'gnus-start
37   :type 'hook)
38
39 ;;;
40 ;;; Server Communication
41 ;;;
42
43 (defun gnus-start-news-server (&optional confirm)
44   "Open a method for getting news.
45 If CONFIRM is non-nil, the user will be asked for an NNTP server."
46   (let (how)
47     (if gnus-current-select-method
48         ;; Stream is already opened.
49         nil
50       ;; Open NNTP server.
51       (unless gnus-nntp-service
52         (setq gnus-nntp-server nil))
53       (when confirm
54         ;; Read server name with completion.
55         (setq gnus-nntp-server
56               (completing-read "NNTP server: "
57                                (mapcar (lambda (server) (list server))
58                                        (cons (list gnus-nntp-server)
59                                              gnus-secondary-servers))
60                                nil nil gnus-nntp-server)))
61
62       (when (and gnus-nntp-server
63                  (stringp gnus-nntp-server)
64                  (not (string= gnus-nntp-server "")))
65         (setq gnus-select-method
66               (cond ((or (string= gnus-nntp-server "")
67                          (string= gnus-nntp-server "::"))
68                      (list 'nnspool (system-name)))
69                     ((string-match "^:" gnus-nntp-server)
70                      (list 'nnmh gnus-nntp-server
71                            (list 'nnmh-directory
72                                  (file-name-as-directory
73                                   (expand-file-name
74                                    (substring 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         result)
116     ;; Transform virtual server names into select methods.
117     (when (stringp method)
118       (setq method (gnus-server-to-method method)))
119     (if (gnus-server-opened method)
120         ;; The stream is already opened.
121         t
122       ;; Open the server.
123       (unless silent
124         (gnus-message 5 "Opening %s server%s..." (car method)
125                       (if (equal (nth 1 method) "") ""
126                         (format " on %s" (nth 1 method)))))
127       (gnus-run-hooks 'gnus-open-server-hook)
128       (prog1
129           (condition-case ()
130               (setq result (gnus-open-server method))
131             (quit (message "Quit gnus-check-server")
132                   nil))
133         (unless silent
134           (gnus-message 5 "Opening %s server%s...%s" (car method)
135                         (if (equal (nth 1 method) "") ""
136                           (format " on %s" (nth 1 method)))
137                         (if result "done" "failed")))))))
138
139 (defun gnus-get-function (method function &optional noerror)
140   "Return a function symbol based on METHOD and FUNCTION."
141   ;; Translate server names into methods.
142   (unless method
143     (error "Attempted use of a nil select method"))
144   (when (stringp method)
145     (setq method (gnus-server-to-method method)))
146   ;; Check cache of constructed names.
147   (let* ((method-sym (if gnus-agent
148                          (gnus-agent-get-function method)
149                        (car method)))
150          (method-fns (get method-sym 'gnus-method-functions))
151          (func (let ((method-fnlist-elt (assq function method-fns)))
152                  (unless method-fnlist-elt
153                    (setq method-fnlist-elt
154                          (cons function
155                                (intern (format "%s-%s" method-sym function))))
156                    (put method-sym 'gnus-method-functions
157                         (cons method-fnlist-elt method-fns)))
158                  (cdr method-fnlist-elt))))
159     ;; Maybe complain if there is no function.
160     (unless (fboundp func)
161       (unless (car method)
162         (error "Trying to require a method that doesn't exist"))
163       (require (car method))
164       (when (not (fboundp func))
165         (if noerror
166             (setq func nil)
167           (error "No such function: %s" func))))
168     func))
169
170 \f
171 ;;;
172 ;;; Interface functions to the backends.
173 ;;;
174
175 (defun gnus-open-server (gnus-command-method)
176   "Open a connection to GNUS-COMMAND-METHOD."
177   (when (stringp gnus-command-method)
178     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
179   (let ((elem (assoc gnus-command-method gnus-opened-servers)))
180     ;; If this method was previously denied, we just return nil.
181     (if (eq (nth 1 elem) 'denied)
182         (progn
183           (gnus-message 1 "Denied server")
184           nil)
185       ;; Open the server.
186       (let ((result
187              (condition-case ()
188                  (funcall (gnus-get-function gnus-command-method 'open-server)
189                           (nth 1 gnus-command-method)
190                           (nthcdr 2 gnus-command-method))
191                (quit
192                 (message "Quit trying to open server")
193                 nil))))
194         ;; If this hasn't been opened before, we add it to the list.
195         (unless elem
196           (setq elem (list gnus-command-method nil)
197                 gnus-opened-servers (cons elem gnus-opened-servers)))
198         ;; Set the status of this server.
199         (setcar (cdr elem) (if result 'ok 'denied))
200         ;; Return the result from the "open" call.
201         result))))
202
203 (defun gnus-close-server (gnus-command-method)
204   "Close the connection to GNUS-COMMAND-METHOD."
205   (when (stringp gnus-command-method)
206     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
207   (funcall (gnus-get-function gnus-command-method 'close-server)
208            (nth 1 gnus-command-method)))
209
210 (defun gnus-request-list (gnus-command-method)
211   "Request the active file from GNUS-COMMAND-METHOD."
212   (when (stringp gnus-command-method)
213     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
214   (funcall (gnus-get-function gnus-command-method 'request-list)
215            (nth 1 gnus-command-method)))
216
217 (defun gnus-request-list-newsgroups (gnus-command-method)
218   "Request the newsgroups file from GNUS-COMMAND-METHOD."
219   (when (stringp gnus-command-method)
220     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
221   (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
222            (nth 1 gnus-command-method)))
223
224 (defun gnus-request-newgroups (date gnus-command-method)
225   "Request all new groups since DATE from GNUS-COMMAND-METHOD."
226   (when (stringp gnus-command-method)
227     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
228   (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
229     (when func
230       (funcall func date (nth 1 gnus-command-method)))))
231
232 (defun gnus-server-opened (gnus-command-method)
233   "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
234   (unless (eq (gnus-server-status gnus-command-method)
235               'denied)
236     (when (stringp gnus-command-method)
237       (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
238     (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
239              (nth 1 gnus-command-method))))
240
241 (defun gnus-status-message (gnus-command-method)
242   "Return the status message from GNUS-COMMAND-METHOD.
243 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name.   The method
244 this group uses will be queried."
245   (let ((gnus-command-method
246          (if (stringp gnus-command-method)
247              (gnus-find-method-for-group gnus-command-method)
248            gnus-command-method)))
249     (funcall (gnus-get-function gnus-command-method 'status-message)
250              (nth 1 gnus-command-method))))
251
252 (defun gnus-request-regenerate (gnus-command-method)
253   "Request a data generation from GNUS-COMMAND-METHOD."
254   (when (stringp gnus-command-method)
255     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
256   (funcall (gnus-get-function gnus-command-method 'request-regenerate)
257            (nth 1 gnus-command-method)))
258
259 (defun gnus-request-group (group &optional dont-check gnus-command-method)
260   "Request GROUP.  If DONT-CHECK, no information is required."
261   (let ((gnus-command-method
262          (or gnus-command-method (inline (gnus-find-method-for-group group)))))
263     (when (stringp gnus-command-method)
264       (setq gnus-command-method
265             (inline (gnus-server-to-method gnus-command-method))))
266     (funcall (inline (gnus-get-function gnus-command-method 'request-group))
267              (gnus-group-real-name group) (nth 1 gnus-command-method)
268              dont-check)))
269
270 (defun gnus-list-active-group (group)
271   "Request active information on GROUP."
272   (let ((gnus-command-method (gnus-find-method-for-group group))
273         (func 'list-active-group))
274     (when (gnus-check-backend-function func group)
275       (funcall (gnus-get-function gnus-command-method func)
276                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
277
278 (defun gnus-request-group-description (group)
279   "Request a description of GROUP."
280   (let ((gnus-command-method (gnus-find-method-for-group group))
281         (func 'request-group-description))
282     (when (gnus-check-backend-function func group)
283       (funcall (gnus-get-function gnus-command-method func)
284                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
285
286 (defun gnus-request-group-articles (group)
287   "Request a list of existing articles in GROUP."
288   (let ((gnus-command-method (gnus-find-method-for-group group))
289         (func 'request-group-articles))
290     (when (gnus-check-backend-function func group)
291       (funcall (gnus-get-function gnus-command-method func)
292                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
293
294 (defun gnus-close-group (group)
295   "Request the GROUP be closed."
296   (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
297     (funcall (gnus-get-function gnus-command-method 'close-group)
298              (gnus-group-real-name group) (nth 1 gnus-command-method))))
299
300 (defun gnus-retrieve-headers (articles group &optional fetch-old)
301   "Request headers for ARTICLES in GROUP.
302 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
303   (let ((gnus-command-method (gnus-find-method-for-group group)))
304     (if (and gnus-use-cache (numberp (car articles)))
305         (gnus-cache-retrieve-headers articles group fetch-old)
306       (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
307                articles (gnus-group-real-name group)
308                (nth 1 gnus-command-method) fetch-old))))
309
310 (defun gnus-retrieve-articles (articles group)
311   "Request ARTICLES in GROUP."
312   (let ((gnus-command-method (gnus-find-method-for-group group)))
313     (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
314              articles (gnus-group-real-name group)
315              (nth 1 gnus-command-method))))
316
317 (defun gnus-retrieve-groups (groups gnus-command-method)
318   "Request active information on GROUPS from GNUS-COMMAND-METHOD."
319   (when (stringp gnus-command-method)
320     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
321   (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
322            groups (nth 1 gnus-command-method)))
323
324 (defun gnus-request-type (group &optional article)
325   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
326   (let ((gnus-command-method (gnus-find-method-for-group group)))
327     (if (not (gnus-check-backend-function
328               'request-type (car gnus-command-method)))
329         'unknown
330       (funcall (gnus-get-function gnus-command-method 'request-type)
331                (gnus-group-real-name group) article))))
332
333 (defun gnus-request-set-mark (group action)
334   "Set marks on articles in the backend."
335   (let ((gnus-command-method (gnus-find-method-for-group group)))
336     (if (not (gnus-check-backend-function
337               'request-set-mark (car gnus-command-method)))
338         action
339       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
340                (gnus-group-real-name group) action
341                (nth 1 gnus-command-method)))))
342
343 (defun gnus-request-update-mark (group article mark)
344   "Allow the backend to change the mark the user tries to put on an article."
345   (let ((gnus-command-method (gnus-find-method-for-group group)))
346     (if (not (gnus-check-backend-function
347               'request-update-mark (car gnus-command-method)))
348         mark
349       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
350                (gnus-group-real-name group) article mark))))
351
352 (defun gnus-request-article (article group &optional buffer)
353   "Request the ARTICLE in GROUP.
354 ARTICLE can either be an article number or an article Message-ID.
355 If BUFFER, insert the article in that group."
356   (let ((gnus-command-method (gnus-find-method-for-group group)))
357     (funcall (gnus-get-function gnus-command-method 'request-article)
358              article (gnus-group-real-name group)
359              (nth 1 gnus-command-method) buffer)))
360
361 (defun gnus-request-head (article group)
362   "Request the head of ARTICLE in GROUP."
363   (let* ((gnus-command-method (gnus-find-method-for-group group))
364          (head (gnus-get-function gnus-command-method 'request-head t))
365          res clean-up)
366     (cond
367      ;; Check the cache.
368      ((and gnus-use-cache
369            (numberp article)
370            (gnus-cache-request-article article group))
371       (setq res (cons group article)
372             clean-up t))
373      ;; Use `head' function.
374      ((fboundp head)
375       (setq res (funcall head article (gnus-group-real-name group)
376                          (nth 1 gnus-command-method))))
377      ;; Use `article' function.
378      (t
379       (setq res (gnus-request-article article group)
380             clean-up t)))
381     (when clean-up
382       (save-excursion
383         (set-buffer nntp-server-buffer)
384         (goto-char (point-min))
385         (when (search-forward "\n\n" nil t)
386           (delete-region (1- (point)) (point-max)))
387         (nnheader-fold-continuation-lines)))
388     res))
389
390 (defun gnus-request-body (article group)
391   "Request the body of ARTICLE in GROUP."
392   (let* ((gnus-command-method (gnus-find-method-for-group group))
393          (head (gnus-get-function gnus-command-method 'request-body t))
394          res clean-up)
395     (cond
396      ;; Check the cache.
397      ((and gnus-use-cache
398            (numberp article)
399            (gnus-cache-request-article article group))
400       (setq res (cons group article)
401             clean-up t))
402      ;; Use `head' function.
403      ((fboundp head)
404       (setq res (funcall head article (gnus-group-real-name group)
405                          (nth 1 gnus-command-method))))
406      ;; Use `article' function.
407      (t
408       (setq res (gnus-request-article article group)
409             clean-up t)))
410     (when clean-up
411       (save-excursion
412         (set-buffer nntp-server-buffer)
413         (goto-char (point-min))
414         (when (search-forward "\n\n" nil t)
415           (delete-region (point-min) (1- (point))))))
416     res))
417
418 (defun gnus-request-post (gnus-command-method)
419   "Post the current buffer using GNUS-COMMAND-METHOD."
420   (when (stringp gnus-command-method)
421     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
422   (funcall (gnus-get-function gnus-command-method 'request-post)
423            (nth 1 gnus-command-method)))
424
425 (defun gnus-request-scan (group gnus-command-method)
426   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
427 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
428   (let ((gnus-command-method
429          (if group (gnus-find-method-for-group group) gnus-command-method))
430         (gnus-inhibit-demon t)
431         (mail-source-plugged gnus-plugged))
432     (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
433         (funcall (gnus-get-function gnus-command-method 'request-scan)
434                  (and group (gnus-group-real-name group))
435                  (nth 1 gnus-command-method)))))
436
437 (defsubst gnus-request-update-info (info gnus-command-method)
438   "Request that GNUS-COMMAND-METHOD update INFO."
439   (when (stringp gnus-command-method)
440     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
441   (when (gnus-check-backend-function
442          'request-update-info (car gnus-command-method))
443     (funcall (gnus-get-function gnus-command-method 'request-update-info)
444              (gnus-group-real-name (gnus-info-group info))
445              info (nth 1 gnus-command-method))))
446
447 (defun gnus-request-expire-articles (articles group &optional force)
448   (let ((gnus-command-method (gnus-find-method-for-group group)))
449     (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
450              articles (gnus-group-real-name group) (nth 1 gnus-command-method)
451              force)))
452
453 (defun gnus-request-move-article
454   (article group server accept-function &optional last)
455   (let ((gnus-command-method (gnus-find-method-for-group group)))
456     (funcall (gnus-get-function gnus-command-method 'request-move-article)
457              article (gnus-group-real-name group)
458              (nth 1 gnus-command-method) accept-function last)))
459
460 (defun gnus-request-accept-article (group &optional gnus-command-method last
461                                           no-encode)
462   ;; Make sure there's a newline at the end of the article.
463   (when (stringp gnus-command-method)
464     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
465   (when (and (not gnus-command-method)
466              (stringp group))
467     (setq gnus-command-method (gnus-group-name-to-method group)))
468   (goto-char (point-max))
469   (unless (bolp)
470     (insert "\n"))
471   (let ((func (car (or gnus-command-method
472                        (gnus-find-method-for-group group)))))
473     (funcall (intern (format "%s-request-accept-article" func))
474              (if (stringp group) (gnus-group-real-name group) group)
475              (cadr gnus-command-method)
476              last)))
477
478 (defun gnus-request-replace-article (article group buffer &optional no-encode)
479   (let ((func (car (gnus-group-name-to-method group))))
480     (funcall (intern (format "%s-request-replace-article" func))
481              article (gnus-group-real-name group) buffer)))
482
483 (defun gnus-request-associate-buffer (group)
484   (let ((gnus-command-method (gnus-find-method-for-group group)))
485     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
486              (gnus-group-real-name group))))
487
488 (defun gnus-request-restore-buffer (article group)
489   "Request a new buffer restored to the state of ARTICLE."
490   (let ((gnus-command-method (gnus-find-method-for-group group)))
491     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
492              article (gnus-group-real-name group)
493              (nth 1 gnus-command-method))))
494
495 (defun gnus-request-create-group (group &optional gnus-command-method args)
496   (when (stringp gnus-command-method)
497     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
498   (let ((gnus-command-method
499          (or gnus-command-method (gnus-find-method-for-group group))))
500     (funcall (gnus-get-function gnus-command-method 'request-create-group)
501              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
502
503 (defun gnus-request-delete-group (group &optional force)
504   (let ((gnus-command-method (gnus-find-method-for-group group)))
505     (funcall (gnus-get-function gnus-command-method 'request-delete-group)
506              (gnus-group-real-name group) force (nth 1 gnus-command-method))))
507
508 (defun gnus-request-rename-group (group new-name)
509   (let ((gnus-command-method (gnus-find-method-for-group group)))
510     (funcall (gnus-get-function gnus-command-method 'request-rename-group)
511              (gnus-group-real-name group)
512              (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
513
514 (defun gnus-close-backends ()
515   ;; Send a close request to all backends that support such a request.
516   (let ((methods gnus-valid-select-methods)
517         (gnus-inhibit-demon t)
518         func gnus-command-method)
519     (while (setq gnus-command-method (pop methods))
520       (when (fboundp (setq func (intern
521                                  (concat (car gnus-command-method)
522                                          "-request-close"))))
523         (funcall func)))))
524
525 (defun gnus-asynchronous-p (gnus-command-method)
526   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
527     (when (fboundp func)
528       (funcall func))))
529
530 (defun gnus-remove-denial (gnus-command-method)
531   (when (stringp gnus-command-method)
532     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
533   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
534          (status (cadr elem)))
535     ;; If this hasn't been opened before, we add it to the list.
536     (when (eq status 'denied)
537       ;; Set the status of this server.
538       (setcar (cdr elem) 'closed))))
539
540 (provide 'gnus-int)
541
542 ;;; gnus-int.el ends here