t-gnus-6_15-quimby: New branch for developing and synchronizing with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
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     ;; Transform virtual server names into select methods.
116     (when (stringp method)
117       (setq method (gnus-server-to-method method)))
118     (if (gnus-server-opened method)
119         ;; The stream is already opened.
120         t
121       ;; Open the server.
122       (unless silent
123         (gnus-message 5 "Opening %s server%s..." (car method)
124                       (if (equal (nth 1 method) "") ""
125                         (format " on %s" (nth 1 method)))))
126       (gnus-run-hooks 'gnus-open-server-hook)
127       (prog1
128           (gnus-open-server method)
129         (unless silent
130           (message ""))))))
131
132 (defun gnus-get-function (method function &optional noerror)
133   "Return a function symbol based on METHOD and FUNCTION."
134   ;; Translate server names into methods.
135   (unless method
136     (error "Attempted use of a nil select method"))
137   (when (stringp method)
138     (setq method (gnus-server-to-method method)))
139   ;; Check cache of constructed names.
140   (let* ((method-sym (if gnus-agent
141                          (gnus-agent-get-function method)
142                        (car method)))
143          (method-fns (get method-sym 'gnus-method-functions))
144          (func (let ((method-fnlist-elt (assq function method-fns)))
145                  (unless method-fnlist-elt
146                    (setq method-fnlist-elt
147                          (cons function
148                                (intern (format "%s-%s" method-sym function))))
149                    (put method-sym 'gnus-method-functions
150                         (cons method-fnlist-elt method-fns)))
151                  (cdr method-fnlist-elt))))
152     ;; Maybe complain if there is no function.
153     (unless (fboundp func)
154       (unless (car method)
155         (error "Trying to require a method that doesn't exist"))
156       (require (car method))
157       (when (not (fboundp func))
158         (if noerror
159             (setq func nil)
160           (error "No such function: %s" func))))
161     func))
162
163 \f
164 ;;;
165 ;;; Interface functions to the backends.
166 ;;;
167
168 (defun gnus-open-server (gnus-command-method)
169   "Open a connection to GNUS-COMMAND-METHOD."
170   (when (stringp gnus-command-method)
171     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
172   (let ((elem (assoc gnus-command-method gnus-opened-servers)))
173     ;; If this method was previously denied, we just return nil.
174     (if (eq (nth 1 elem) 'denied)
175         (progn
176           (gnus-message 1 "Denied server")
177           nil)
178       ;; Open the server.
179       (let ((result
180              (funcall (gnus-get-function gnus-command-method 'open-server)
181                       (nth 1 gnus-command-method)
182                       (nthcdr 2 gnus-command-method))))
183         ;; If this hasn't been opened before, we add it to the list.
184         (unless elem
185           (setq elem (list gnus-command-method nil)
186                 gnus-opened-servers (cons elem gnus-opened-servers)))
187         ;; Set the status of this server.
188         (setcar (cdr elem) (if result 'ok 'denied))
189         ;; Return the result from the "open" call.
190         result))))
191
192 (defun gnus-close-server (gnus-command-method)
193   "Close the connection to GNUS-COMMAND-METHOD."
194   (when (stringp gnus-command-method)
195     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
196   (funcall (gnus-get-function gnus-command-method 'close-server)
197            (nth 1 gnus-command-method)))
198
199 (defun gnus-request-list (gnus-command-method)
200   "Request the active file from GNUS-COMMAND-METHOD."
201   (when (stringp gnus-command-method)
202     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
203   (funcall (gnus-get-function gnus-command-method 'request-list)
204            (nth 1 gnus-command-method)))
205
206 (defun gnus-request-list-newsgroups (gnus-command-method)
207   "Request the newsgroups file from GNUS-COMMAND-METHOD."
208   (when (stringp gnus-command-method)
209     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
210   (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
211            (nth 1 gnus-command-method)))
212
213 (defun gnus-request-newgroups (date gnus-command-method)
214   "Request all new groups since DATE from GNUS-COMMAND-METHOD."
215   (when (stringp gnus-command-method)
216     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
217   (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
218     (when func
219       (funcall func date (nth 1 gnus-command-method)))))
220
221 (defun gnus-server-opened (gnus-command-method)
222   "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
223   (unless (eq (gnus-server-status gnus-command-method)
224               'denied)
225     (when (stringp gnus-command-method)
226       (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
227     (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
228              (nth 1 gnus-command-method))))
229
230 (defun gnus-status-message (gnus-command-method)
231   "Return the status message from GNUS-COMMAND-METHOD.
232 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name.   The method
233 this group uses will be queried."
234   (let ((gnus-command-method
235          (if (stringp gnus-command-method)
236              (gnus-find-method-for-group gnus-command-method)
237            gnus-command-method)))
238     (funcall (gnus-get-function gnus-command-method 'status-message)
239              (nth 1 gnus-command-method))))
240
241 (defun gnus-request-regenerate (gnus-command-method)
242   "Request a data generation from GNUS-COMMAND-METHOD."
243   (when (stringp gnus-command-method)
244     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
245   (funcall (gnus-get-function gnus-command-method 'request-regenerate)
246            (nth 1 gnus-command-method)))
247
248 (defun gnus-request-group (group &optional dont-check gnus-command-method)
249   "Request GROUP.  If DONT-CHECK, no information is required."
250   (let ((gnus-command-method
251          (or gnus-command-method (inline (gnus-find-method-for-group group)))))
252     (when (stringp gnus-command-method)
253       (setq gnus-command-method
254             (inline (gnus-server-to-method gnus-command-method))))
255     (funcall (inline (gnus-get-function gnus-command-method 'request-group))
256              (gnus-group-real-name group) (nth 1 gnus-command-method)
257              dont-check)))
258
259 (defun gnus-list-active-group (group)
260   "Request active information on GROUP."
261   (let ((gnus-command-method (gnus-find-method-for-group group))
262         (func 'list-active-group))
263     (when (gnus-check-backend-function func group)
264       (funcall (gnus-get-function gnus-command-method func)
265                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
266
267 (defun gnus-request-group-description (group)
268   "Request a description of GROUP."
269   (let ((gnus-command-method (gnus-find-method-for-group group))
270         (func 'request-group-description))
271     (when (gnus-check-backend-function func group)
272       (funcall (gnus-get-function gnus-command-method func)
273                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
274
275 (defun gnus-request-group-articles (group)
276   "Request a list of existing articles in GROUP."
277   (let ((gnus-command-method (gnus-find-method-for-group group))
278         (func 'request-group-articles))
279     (when (gnus-check-backend-function func group)
280       (funcall (gnus-get-function gnus-command-method func)
281                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
282
283 (defun gnus-close-group (group)
284   "Request the GROUP be closed."
285   (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
286     (funcall (gnus-get-function gnus-command-method 'close-group)
287              (gnus-group-real-name group) (nth 1 gnus-command-method))))
288
289 (defun gnus-retrieve-headers (articles group &optional fetch-old)
290   "Request headers for ARTICLES in GROUP.
291 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
292   (let ((gnus-command-method (gnus-find-method-for-group group)))
293     (if (and gnus-use-cache (numberp (car articles)))
294         (gnus-cache-retrieve-headers articles group fetch-old)
295       (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
296                articles (gnus-group-real-name group)
297                (nth 1 gnus-command-method) fetch-old))))
298
299 (defun gnus-retrieve-parsed-headers (articles group &optional fetch-old
300                                               dependencies force-new)
301   "Request parsed-headers for ARTICLES in GROUP.
302 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
303   (unless dependencies
304     (setq dependencies
305           (save-excursion
306             (set-buffer gnus-summary-buffer)
307             gnus-newsgroup-dependencies)))
308   (let ((gnus-command-method (gnus-find-method-for-group group))
309         headers)
310     (if (and gnus-use-cache (numberp (car articles)))
311         (setq headers
312               (gnus-cache-retrieve-parsed-headers articles group fetch-old
313                                                   dependencies force-new))
314       (let ((func (gnus-get-function gnus-command-method
315                                      'retrieve-parsed-headers 'no-error)))
316         (if func
317             (setq headers (funcall func articles dependencies
318                                    (gnus-group-real-name group)
319                                    (nth 1 gnus-command-method) fetch-old
320                                    force-new)
321                   gnus-headers-retrieved-by (car headers)
322                   headers (cdr headers))
323           (setq gnus-headers-retrieved-by
324                 (funcall
325                  (gnus-get-function gnus-command-method 'retrieve-headers)
326                  articles (gnus-group-real-name group)
327                  (nth 1 gnus-command-method) fetch-old))
328           )))
329     (or headers
330         (if (eq gnus-headers-retrieved-by 'nov)
331             (gnus-get-newsgroup-headers-xover
332              articles nil dependencies gnus-newsgroup-name t)
333           (gnus-get-newsgroup-headers dependencies)))
334     ))
335
336 (defun gnus-retrieve-articles (articles group)
337   "Request ARTICLES in GROUP."
338   (let ((gnus-command-method (gnus-find-method-for-group group)))
339     (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
340              articles (gnus-group-real-name group)
341              (nth 1 gnus-command-method))))
342
343 (defun gnus-retrieve-groups (groups gnus-command-method)
344   "Request active information on GROUPS from GNUS-COMMAND-METHOD."
345   (when (stringp gnus-command-method)
346     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
347   (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
348            groups (nth 1 gnus-command-method)))
349
350 (defun gnus-request-type (group &optional article)
351   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
352   (let ((gnus-command-method (gnus-find-method-for-group group)))
353     (if (not (gnus-check-backend-function
354               'request-type (car gnus-command-method)))
355         'unknown
356       (funcall (gnus-get-function gnus-command-method 'request-type)
357                (gnus-group-real-name group) article))))
358
359 (defun gnus-request-set-mark (group action)
360   "Set marks on articles in the backend."
361   (let ((gnus-command-method (gnus-find-method-for-group group)))
362     (if (not (gnus-check-backend-function
363               'request-set-mark (car gnus-command-method)))
364         action
365       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
366                (gnus-group-real-name group) action
367                (nth 1 gnus-command-method)))))
368
369 (defun gnus-request-update-mark (group article mark)
370   "Allow the backend to change the mark the user tries to put on an article."
371   (let ((gnus-command-method (gnus-find-method-for-group group)))
372     (if (not (gnus-check-backend-function
373               'request-update-mark (car gnus-command-method)))
374         mark
375       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
376                (gnus-group-real-name group) article mark))))
377
378 (defun gnus-request-article (article group &optional buffer)
379   "Request the ARTICLE in GROUP.
380 ARTICLE can either be an article number or an article Message-ID.
381 If BUFFER, insert the article in that group."
382   (let ((gnus-command-method (gnus-find-method-for-group group)))
383     (funcall (gnus-get-function gnus-command-method 'request-article)
384              article (gnus-group-real-name group)
385              (nth 1 gnus-command-method) buffer)))
386
387 (defun gnus-request-head (article group)
388   "Request the head of ARTICLE in GROUP."
389   (let* ((gnus-command-method (gnus-find-method-for-group group))
390          (head (gnus-get-function gnus-command-method 'request-head t))
391          res clean-up)
392     (cond
393      ;; Check the cache.
394      ((and gnus-use-cache
395            (numberp article)
396            (gnus-cache-request-article article group))
397       (setq res (cons group article)
398             clean-up t))
399      ;; Use `head' function.
400      ((fboundp head)
401       (setq res (funcall head article (gnus-group-real-name group)
402                          (nth 1 gnus-command-method))))
403      ;; Use `article' function.
404      (t
405       (setq res (gnus-request-article article group)
406             clean-up t)))
407     (when clean-up
408       (save-excursion
409         (set-buffer nntp-server-buffer)
410         (goto-char (point-min))
411         (when (search-forward "\n\n" nil t)
412           (delete-region (1- (point)) (point-max)))
413         (nnheader-fold-continuation-lines)))
414     res))
415
416 (defun gnus-request-body (article group)
417   "Request the body of ARTICLE in GROUP."
418   (let* ((gnus-command-method (gnus-find-method-for-group group))
419          (head (gnus-get-function gnus-command-method 'request-body t))
420          res clean-up)
421     (cond
422      ;; Check the cache.
423      ((and gnus-use-cache
424            (numberp article)
425            (gnus-cache-request-article article group))
426       (setq res (cons group article)
427             clean-up t))
428      ;; Use `head' function.
429      ((fboundp head)
430       (setq res (funcall head article (gnus-group-real-name group)
431                          (nth 1 gnus-command-method))))
432      ;; Use `article' function.
433      (t
434       (setq res (gnus-request-article article group)
435             clean-up t)))
436     (when clean-up
437       (save-excursion
438         (set-buffer nntp-server-buffer)
439         (goto-char (point-min))
440         (when (search-forward "\n\n" nil t)
441           (delete-region (point-min) (1- (point))))))
442     res))
443
444 (defun gnus-request-post (gnus-command-method)
445   "Post the current buffer using GNUS-COMMAND-METHOD."
446   (when (stringp gnus-command-method)
447     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
448   (funcall (gnus-get-function gnus-command-method 'request-post)
449            (nth 1 gnus-command-method)))
450
451 (defun gnus-request-scan (group gnus-command-method)
452   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
453 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
454   (let ((gnus-command-method
455          (if group (gnus-find-method-for-group group) gnus-command-method))
456         (gnus-inhibit-demon t)
457         (mail-source-plugged gnus-plugged))
458     (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
459         (funcall (gnus-get-function gnus-command-method 'request-scan)
460                  (and group (gnus-group-real-name group))
461                  (nth 1 gnus-command-method)))))
462
463 (defsubst gnus-request-update-info (info gnus-command-method)
464   "Request that GNUS-COMMAND-METHOD update INFO."
465   (when (stringp gnus-command-method)
466     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
467   (when (gnus-check-backend-function
468          'request-update-info (car gnus-command-method))
469     (funcall (gnus-get-function gnus-command-method 'request-update-info)
470              (gnus-group-real-name (gnus-info-group info))
471              info (nth 1 gnus-command-method))))
472
473 (defun gnus-request-expire-articles (articles group &optional force)
474   (let ((gnus-command-method (gnus-find-method-for-group group)))
475     (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
476              articles (gnus-group-real-name group) (nth 1 gnus-command-method)
477              force)))
478
479 (defun gnus-request-move-article
480   (article group server accept-function &optional last)
481   (let ((gnus-command-method (gnus-find-method-for-group group)))
482     (funcall (gnus-get-function gnus-command-method 'request-move-article)
483              article (gnus-group-real-name group)
484              (nth 1 gnus-command-method) accept-function last)))
485
486 (defun gnus-request-accept-article (group &optional gnus-command-method last
487                                           no-encode)
488   ;; Make sure there's a newline at the end of the article.
489   (when (stringp gnus-command-method)
490     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
491   (when (and (not gnus-command-method)
492              (stringp group))
493     (setq gnus-command-method (gnus-group-name-to-method group)))
494   (goto-char (point-max))
495   (unless (bolp)
496     (insert "\n"))
497   (let ((func (car (or gnus-command-method
498                        (gnus-find-method-for-group group)))))
499     (funcall (intern (format "%s-request-accept-article" func))
500              (if (stringp group) (gnus-group-real-name group) group)
501              (cadr gnus-command-method)
502              last)))
503
504 (defun gnus-request-replace-article (article group buffer &optional no-encode)
505   (let ((func (car (gnus-group-name-to-method group))))
506     (funcall (intern (format "%s-request-replace-article" func))
507              article (gnus-group-real-name group) buffer)))
508
509 (defun gnus-request-associate-buffer (group)
510   (let ((gnus-command-method (gnus-find-method-for-group group)))
511     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
512              (gnus-group-real-name group))))
513
514 (defun gnus-request-restore-buffer (article group)
515   "Request a new buffer restored to the state of ARTICLE."
516   (let ((gnus-command-method (gnus-find-method-for-group group)))
517     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
518              article (gnus-group-real-name group)
519              (nth 1 gnus-command-method))))
520
521 (defun gnus-request-create-group (group &optional gnus-command-method args)
522   (when (stringp gnus-command-method)
523     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
524   (let ((gnus-command-method
525          (or gnus-command-method (gnus-find-method-for-group group))))
526     (funcall (gnus-get-function gnus-command-method 'request-create-group)
527              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
528
529 (defun gnus-request-delete-group (group &optional force)
530   (let ((gnus-command-method (gnus-find-method-for-group group)))
531     (funcall (gnus-get-function gnus-command-method 'request-delete-group)
532              (gnus-group-real-name group) force (nth 1 gnus-command-method))))
533
534 (defun gnus-request-rename-group (group new-name)
535   (let ((gnus-command-method (gnus-find-method-for-group group)))
536     (funcall (gnus-get-function gnus-command-method 'request-rename-group)
537              (gnus-group-real-name group)
538              (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
539
540 (defun gnus-close-backends ()
541   ;; Send a close request to all backends that support such a request.
542   (let ((methods gnus-valid-select-methods)
543         (gnus-inhibit-demon t)
544         func gnus-command-method)
545     (while (setq gnus-command-method (pop methods))
546       (when (fboundp (setq func (intern
547                                  (concat (car gnus-command-method)
548                                          "-request-close"))))
549         (funcall func)))))
550
551 (defun gnus-asynchronous-p (gnus-command-method)
552   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
553     (when (fboundp func)
554       (funcall func))))
555
556 (defun gnus-remove-denial (gnus-command-method)
557   (when (stringp gnus-command-method)
558     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
559   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
560          (status (cadr elem)))
561     ;; If this hasn't been opened before, we add it to the list.
562     (when (eq status 'denied)
563       ;; Set the status of this server.
564       (setcar (cdr elem) 'closed))))
565
566 (provide 'gnus-int)
567
568 ;;; gnus-int.el ends here