Update.
[elisp/gnus.git-] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32
33 (defcustom gnus-open-server-hook nil
34   "Hook called just before opening connection to the news server."
35   :group 'gnus-start
36   :type 'hook)
37
38 ;;;
39 ;;; Server Communication
40 ;;;
41
42 (defun gnus-start-news-server (&optional confirm)
43   "Open a method for getting news.
44 If CONFIRM is non-nil, the user will be asked for an NNTP server."
45   (let (how)
46     (if gnus-current-select-method
47         ;; Stream is already opened.
48         nil
49       ;; Open NNTP server.
50       (unless gnus-nntp-service
51         (setq gnus-nntp-server nil))
52       (when confirm
53         ;; Read server name with completion.
54         (setq gnus-nntp-server
55               (completing-read "NNTP server: "
56                                (mapcar (lambda (server) (list server))
57                                        (cons (list gnus-nntp-server)
58                                              gnus-secondary-servers))
59                                nil nil gnus-nntp-server)))
60
61       (when (and gnus-nntp-server
62                  (stringp gnus-nntp-server)
63                  (not (string= gnus-nntp-server "")))
64         (setq gnus-select-method
65               (cond ((or (string= gnus-nntp-server "")
66                          (string= gnus-nntp-server "::"))
67                      (list 'nnspool (system-name)))
68                     ((string-match "^:" gnus-nntp-server)
69                      (list 'nnmh gnus-nntp-server
70                            (list 'nnmh-directory
71                                  (file-name-as-directory
72                                   (expand-file-name
73                                    (concat "~/" (substring
74                                                  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   (when (stringp gnus-command-method)
224     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
225   (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
226            (nth 1 gnus-command-method)))
227
228 (defun gnus-status-message (gnus-command-method)
229   "Return the status message from GNUS-COMMAND-METHOD.
230 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name.   The method
231 this group uses will be queried."
232   (let ((gnus-command-method
233          (if (stringp gnus-command-method)
234              (gnus-find-method-for-group gnus-command-method)
235            gnus-command-method)))
236     (funcall (gnus-get-function gnus-command-method 'status-message)
237              (nth 1 gnus-command-method))))
238
239 (defun gnus-request-regenerate (gnus-command-method)
240   "Request a data generation from GNUS-COMMAND-METHOD."
241   (when (stringp gnus-command-method)
242     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
243   (funcall (gnus-get-function gnus-command-method 'request-regenerate)
244            (nth 1 gnus-command-method)))
245
246 (defun gnus-request-group (group &optional dont-check gnus-command-method)
247   "Request GROUP.  If DONT-CHECK, no information is required."
248   (let ((gnus-command-method
249          (or gnus-command-method (inline (gnus-find-method-for-group group)))))
250     (when (stringp gnus-command-method)
251       (setq gnus-command-method
252             (inline (gnus-server-to-method gnus-command-method))))
253     (funcall (inline (gnus-get-function gnus-command-method 'request-group))
254              (gnus-group-real-name group) (nth 1 gnus-command-method)
255              dont-check)))
256
257 (defun gnus-list-active-group (group)
258   "Request active information on GROUP."
259   (let ((gnus-command-method (gnus-find-method-for-group group))
260         (func 'list-active-group))
261     (when (gnus-check-backend-function func group)
262       (funcall (gnus-get-function gnus-command-method func)
263                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
264
265 (defun gnus-request-group-description (group)
266   "Request a description of GROUP."
267   (let ((gnus-command-method (gnus-find-method-for-group group))
268         (func 'request-group-description))
269     (when (gnus-check-backend-function func group)
270       (funcall (gnus-get-function gnus-command-method func)
271                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
272
273 (defun gnus-request-group-articles (group)
274   "Request a list of existing articles in GROUP."
275   (let ((gnus-command-method (gnus-find-method-for-group group))
276         (func 'request-group-articles))
277     (when (gnus-check-backend-function func group)
278       (funcall (gnus-get-function gnus-command-method func)
279                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
280
281 (defun gnus-close-group (group)
282   "Request the GROUP be closed."
283   (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
284     (funcall (gnus-get-function gnus-command-method 'close-group)
285              (gnus-group-real-name group) (nth 1 gnus-command-method))))
286
287 (defun gnus-retrieve-headers (articles group &optional fetch-old)
288   "Request headers for ARTICLES in GROUP.
289 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
290   (let ((gnus-command-method (gnus-find-method-for-group group)))
291     (if (and gnus-use-cache (numberp (car articles)))
292         (gnus-cache-retrieve-headers articles group fetch-old)
293       (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
294                articles (gnus-group-real-name group)
295                (nth 1 gnus-command-method) fetch-old))))
296
297 (defun gnus-retrieve-parsed-headers (articles group &optional fetch-old
298                                               dependencies force-new)
299   "Request parsed-headers for ARTICLES in GROUP.
300 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
301   (unless dependencies
302     (setq dependencies
303           (save-excursion
304             (set-buffer gnus-summary-buffer)
305             gnus-newsgroup-dependencies)))
306   (let ((gnus-command-method (gnus-find-method-for-group group))
307         headers)
308     (if (and gnus-use-cache (numberp (car articles)))
309         (setq headers
310               (gnus-cache-retrieve-parsed-headers articles group fetch-old
311                                                   dependencies force-new))
312       (let ((func (gnus-get-function gnus-command-method
313                                      'retrieve-parsed-headers 'no-error)))
314         (if func
315             (setq headers (funcall func articles dependencies
316                                    (gnus-group-real-name group)
317                                    (nth 1 gnus-command-method) fetch-old
318                                    force-new)
319                   gnus-headers-retrieved-by (car headers)
320                   headers (cdr headers))
321           (setq gnus-headers-retrieved-by
322                 (funcall
323                  (gnus-get-function gnus-command-method 'retrieve-headers)
324                  articles (gnus-group-real-name group)
325                  (nth 1 gnus-command-method) fetch-old))
326           )))
327     (or headers
328         (if (eq gnus-headers-retrieved-by 'nov)
329             (gnus-get-newsgroup-headers-xover
330              articles nil dependencies gnus-newsgroup-name t)
331           (gnus-get-newsgroup-headers dependencies)))
332     ))
333
334 (defun gnus-retrieve-articles (articles group)
335   "Request ARTICLES in GROUP."
336   (let ((gnus-command-method (gnus-find-method-for-group group)))
337     (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
338              articles (gnus-group-real-name group)
339              (nth 1 gnus-command-method))))
340
341 (defun gnus-retrieve-groups (groups gnus-command-method)
342   "Request active information on GROUPS from GNUS-COMMAND-METHOD."
343   (when (stringp gnus-command-method)
344     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
345   (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
346            groups (nth 1 gnus-command-method)))
347
348 (defun gnus-request-type (group &optional article)
349   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
350   (let ((gnus-command-method (gnus-find-method-for-group group)))
351     (if (not (gnus-check-backend-function
352               'request-type (car gnus-command-method)))
353         'unknown
354       (funcall (gnus-get-function gnus-command-method 'request-type)
355                (gnus-group-real-name group) article))))
356
357 (defun gnus-request-set-mark (group action)
358   "Set marks on articles in the backend."
359   (let ((gnus-command-method (gnus-find-method-for-group group)))
360     (if (not (gnus-check-backend-function
361               'request-set-mark (car gnus-command-method)))
362         action
363       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
364                (gnus-group-real-name group) action
365                (nth 1 gnus-command-method)))))
366
367 (defun gnus-request-update-mark (group article mark)
368   "Allow the backend to change the mark the user tries to put on an article."
369   (let ((gnus-command-method (gnus-find-method-for-group group)))
370     (if (not (gnus-check-backend-function
371               'request-update-mark (car gnus-command-method)))
372         mark
373       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
374                (gnus-group-real-name group) article mark))))
375
376 (defun gnus-request-article (article group &optional buffer)
377   "Request the ARTICLE in GROUP.
378 ARTICLE can either be an article number or an article Message-ID.
379 If BUFFER, insert the article in that group."
380   (let ((gnus-command-method (gnus-find-method-for-group group)))
381     (funcall (gnus-get-function gnus-command-method 'request-article)
382              article (gnus-group-real-name group)
383              (nth 1 gnus-command-method) buffer)))
384
385 (defun gnus-request-head (article group)
386   "Request the head of ARTICLE in GROUP."
387   (let* ((gnus-command-method (gnus-find-method-for-group group))
388          (head (gnus-get-function gnus-command-method 'request-head t))
389          res clean-up)
390     (cond
391      ;; Check the cache.
392      ((and gnus-use-cache
393            (numberp article)
394            (gnus-cache-request-article article group))
395       (setq res (cons group article)
396             clean-up t))
397      ;; Use `head' function.
398      ((fboundp head)
399       (setq res (funcall head article (gnus-group-real-name group)
400                          (nth 1 gnus-command-method))))
401      ;; Use `article' function.
402      (t
403       (setq res (gnus-request-article article group)
404             clean-up t)))
405     (when clean-up
406       (save-excursion
407         (set-buffer nntp-server-buffer)
408         (goto-char (point-min))
409         (when (search-forward "\n\n" nil t)
410           (delete-region (1- (point)) (point-max)))
411         (nnheader-fold-continuation-lines)))
412     res))
413
414 (defun gnus-request-body (article group)
415   "Request the body of ARTICLE in GROUP."
416   (let* ((gnus-command-method (gnus-find-method-for-group group))
417          (head (gnus-get-function gnus-command-method 'request-body t))
418          res clean-up)
419     (cond
420      ;; Check the cache.
421      ((and gnus-use-cache
422            (numberp article)
423            (gnus-cache-request-article article group))
424       (setq res (cons group article)
425             clean-up t))
426      ;; Use `head' function.
427      ((fboundp head)
428       (setq res (funcall head article (gnus-group-real-name group)
429                          (nth 1 gnus-command-method))))
430      ;; Use `article' function.
431      (t
432       (setq res (gnus-request-article article group)
433             clean-up t)))
434     (when clean-up
435       (save-excursion
436         (set-buffer nntp-server-buffer)
437         (goto-char (point-min))
438         (when (search-forward "\n\n" nil t)
439           (delete-region (point-min) (1- (point))))))
440     res))
441
442 (defun gnus-request-post (gnus-command-method)
443   "Post the current buffer using GNUS-COMMAND-METHOD."
444   (when (stringp gnus-command-method)
445     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
446   (funcall (gnus-get-function gnus-command-method 'request-post)
447            (nth 1 gnus-command-method)))
448
449 (defun gnus-request-scan (group gnus-command-method)
450   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
451 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
452   (when gnus-plugged
453     (let ((gnus-command-method
454            (if group (gnus-find-method-for-group group) gnus-command-method))
455           (gnus-inhibit-demon t))
456       (funcall (gnus-get-function gnus-command-method 'request-scan)
457                (and group (gnus-group-real-name group))
458                (nth 1 gnus-command-method)))))
459
460 (defsubst gnus-request-update-info (info gnus-command-method)
461   "Request that GNUS-COMMAND-METHOD update INFO."
462   (when (stringp gnus-command-method)
463     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
464   (when (gnus-check-backend-function
465          'request-update-info (car gnus-command-method))
466     (funcall (gnus-get-function gnus-command-method 'request-update-info)
467              (gnus-group-real-name (gnus-info-group info))
468              info (nth 1 gnus-command-method))))
469
470 (defun gnus-request-expire-articles (articles group &optional force)
471   (let ((gnus-command-method (gnus-find-method-for-group group)))
472     (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
473              articles (gnus-group-real-name group) (nth 1 gnus-command-method)
474              force)))
475
476 (defun gnus-request-move-article
477   (article group server accept-function &optional last)
478   (let ((gnus-command-method (gnus-find-method-for-group group)))
479     (funcall (gnus-get-function gnus-command-method 'request-move-article)
480              article (gnus-group-real-name group)
481              (nth 1 gnus-command-method) accept-function last)))
482
483 (defun gnus-request-accept-article (group &optional gnus-command-method last
484                                           no-encode)
485   ;; Make sure there's a newline at the end of the article.
486   (when (stringp gnus-command-method)
487     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
488   (when (and (not gnus-command-method)
489              (stringp group))
490     (setq gnus-command-method (gnus-group-name-to-method group)))
491   (goto-char (point-max))
492   (unless (bolp)
493     (insert "\n"))
494   (let ((func (car (or gnus-command-method
495                        (gnus-find-method-for-group group)))))
496     (funcall (intern (format "%s-request-accept-article" func))
497              (if (stringp group) (gnus-group-real-name group) group)
498              (cadr gnus-command-method)
499              last)))
500
501 (defun gnus-request-replace-article (article group buffer &optional no-encode)
502   (let ((func (car (gnus-group-name-to-method group))))
503     (funcall (intern (format "%s-request-replace-article" func))
504              article (gnus-group-real-name group) buffer)))
505
506 (defun gnus-request-associate-buffer (group)
507   (let ((gnus-command-method (gnus-find-method-for-group group)))
508     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
509              (gnus-group-real-name group))))
510
511 (defun gnus-request-restore-buffer (article group)
512   "Request a new buffer restored to the state of ARTICLE."
513   (let ((gnus-command-method (gnus-find-method-for-group group)))
514     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
515              article (gnus-group-real-name group)
516              (nth 1 gnus-command-method))))
517
518 (defun gnus-request-create-group (group &optional gnus-command-method args)
519   (when (stringp gnus-command-method)
520     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
521   (let ((gnus-command-method
522          (or gnus-command-method (gnus-find-method-for-group group))))
523     (funcall (gnus-get-function gnus-command-method 'request-create-group)
524              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
525
526 (defun gnus-request-delete-group (group &optional force)
527   (let ((gnus-command-method (gnus-find-method-for-group group)))
528     (funcall (gnus-get-function gnus-command-method 'request-delete-group)
529              (gnus-group-real-name group) force (nth 1 gnus-command-method))))
530
531 (defun gnus-request-rename-group (group new-name)
532   (let ((gnus-command-method (gnus-find-method-for-group group)))
533     (funcall (gnus-get-function gnus-command-method 'request-rename-group)
534              (gnus-group-real-name group)
535              (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
536
537 (defun gnus-close-backends ()
538   ;; Send a close request to all backends that support such a request.
539   (let ((methods gnus-valid-select-methods)
540         (gnus-inhibit-demon t)
541         func gnus-command-method)
542     (while (setq gnus-command-method (pop methods))
543       (when (fboundp (setq func (intern
544                                  (concat (car gnus-command-method)
545                                          "-request-close"))))
546         (funcall func)))))
547
548 (defun gnus-asynchronous-p (gnus-command-method)
549   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
550     (when (fboundp func)
551       (funcall func))))
552
553 (defun gnus-remove-denial (gnus-command-method)
554   (when (stringp gnus-command-method)
555     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
556   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
557          (status (cadr elem)))
558     ;; If this hasn't been opened before, we add it to the list.
559     (when (eq status 'denied)
560       ;; Set the status of this server.
561       (setcar (cdr elem) 'closed))))
562
563 (provide 'gnus-int)
564
565 ;;; gnus-int.el ends here