d3fd3e06aad64893a3e02043f3f866d11212880a
[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              (funcall (gnus-get-function gnus-command-method 'open-server)
188                       (nth 1 gnus-command-method)
189                       (nthcdr 2 gnus-command-method))))
190         ;; If this hasn't been opened before, we add it to the list.
191         (unless elem
192           (setq elem (list gnus-command-method nil)
193                 gnus-opened-servers (cons elem gnus-opened-servers)))
194         ;; Set the status of this server.
195         (setcar (cdr elem) (if result 'ok 'denied))
196         ;; Return the result from the "open" call.
197         result))))
198
199 (defun gnus-close-server (gnus-command-method)
200   "Close the connection to 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 'close-server)
204            (nth 1 gnus-command-method)))
205
206 (defun gnus-request-list (gnus-command-method)
207   "Request the active 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)
211            (nth 1 gnus-command-method)))
212
213 (defun gnus-request-list-newsgroups (gnus-command-method)
214   "Request the newsgroups file from GNUS-COMMAND-METHOD."
215   (when (stringp gnus-command-method)
216     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
217   (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
218            (nth 1 gnus-command-method)))
219
220 (defun gnus-request-newgroups (date gnus-command-method)
221   "Request all new groups since DATE from GNUS-COMMAND-METHOD."
222   (when (stringp gnus-command-method)
223     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
224   (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
225     (when func
226       (funcall func date (nth 1 gnus-command-method)))))
227
228 (defun gnus-server-opened (gnus-command-method)
229   "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
230   (unless (eq (gnus-server-status gnus-command-method)
231               'denied)
232     (when (stringp gnus-command-method)
233       (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
234     (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
235              (nth 1 gnus-command-method))))
236
237 (defun gnus-status-message (gnus-command-method)
238   "Return the status message from GNUS-COMMAND-METHOD.
239 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name.   The method
240 this group uses will be queried."
241   (let ((gnus-command-method
242          (if (stringp gnus-command-method)
243              (gnus-find-method-for-group gnus-command-method)
244            gnus-command-method)))
245     (funcall (gnus-get-function gnus-command-method 'status-message)
246              (nth 1 gnus-command-method))))
247
248 (defun gnus-request-regenerate (gnus-command-method)
249   "Request a data generation from GNUS-COMMAND-METHOD."
250   (when (stringp gnus-command-method)
251     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
252   (funcall (gnus-get-function gnus-command-method 'request-regenerate)
253            (nth 1 gnus-command-method)))
254
255 (defun gnus-request-group (group &optional dont-check gnus-command-method)
256   "Request GROUP.  If DONT-CHECK, no information is required."
257   (let ((gnus-command-method
258          (or gnus-command-method (inline (gnus-find-method-for-group group)))))
259     (when (stringp gnus-command-method)
260       (setq gnus-command-method
261             (inline (gnus-server-to-method gnus-command-method))))
262     (funcall (inline (gnus-get-function gnus-command-method 'request-group))
263              (gnus-group-real-name group) (nth 1 gnus-command-method)
264              dont-check)))
265
266 (defun gnus-list-active-group (group)
267   "Request active information on GROUP."
268   (let ((gnus-command-method (gnus-find-method-for-group group))
269         (func 'list-active-group))
270     (when (gnus-check-backend-function func group)
271       (funcall (gnus-get-function gnus-command-method func)
272                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
273
274 (defun gnus-request-group-description (group)
275   "Request a description of GROUP."
276   (let ((gnus-command-method (gnus-find-method-for-group group))
277         (func 'request-group-description))
278     (when (gnus-check-backend-function func group)
279       (funcall (gnus-get-function gnus-command-method func)
280                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
281
282 (defun gnus-request-group-articles (group)
283   "Request a list of existing articles in GROUP."
284   (let ((gnus-command-method (gnus-find-method-for-group group))
285         (func 'request-group-articles))
286     (when (gnus-check-backend-function func group)
287       (funcall (gnus-get-function gnus-command-method func)
288                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
289
290 (defun gnus-close-group (group)
291   "Request the GROUP be closed."
292   (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
293     (funcall (gnus-get-function gnus-command-method 'close-group)
294              (gnus-group-real-name group) (nth 1 gnus-command-method))))
295
296 (defun gnus-retrieve-headers (articles group &optional fetch-old)
297   "Request headers for ARTICLES in GROUP.
298 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
299   (let ((gnus-command-method (gnus-find-method-for-group group)))
300     (if (and gnus-use-cache (numberp (car articles)))
301         (gnus-cache-retrieve-headers articles group fetch-old)
302       (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
303                articles (gnus-group-real-name group)
304                (nth 1 gnus-command-method) fetch-old))))
305
306 (defun gnus-retrieve-articles (articles group)
307   "Request ARTICLES in GROUP."
308   (let ((gnus-command-method (gnus-find-method-for-group group)))
309     (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
310              articles (gnus-group-real-name group)
311              (nth 1 gnus-command-method))))
312
313 (defun gnus-retrieve-groups (groups gnus-command-method)
314   "Request active information on GROUPS from GNUS-COMMAND-METHOD."
315   (when (stringp gnus-command-method)
316     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
317   (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
318            groups (nth 1 gnus-command-method)))
319
320 (defun gnus-request-type (group &optional article)
321   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
322   (let ((gnus-command-method (gnus-find-method-for-group group)))
323     (if (not (gnus-check-backend-function
324               'request-type (car gnus-command-method)))
325         'unknown
326       (funcall (gnus-get-function gnus-command-method 'request-type)
327                (gnus-group-real-name group) article))))
328
329 (defun gnus-request-set-mark (group action)
330   "Set marks on articles in the backend."
331   (let ((gnus-command-method (gnus-find-method-for-group group)))
332     (if (not (gnus-check-backend-function
333               'request-set-mark (car gnus-command-method)))
334         action
335       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
336                (gnus-group-real-name group) action
337                (nth 1 gnus-command-method)))))
338
339 (defun gnus-request-update-mark (group article mark)
340   "Allow the backend to change the mark the user tries to put on an article."
341   (let ((gnus-command-method (gnus-find-method-for-group group)))
342     (if (not (gnus-check-backend-function
343               'request-update-mark (car gnus-command-method)))
344         mark
345       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
346                (gnus-group-real-name group) article mark))))
347
348 (defun gnus-request-article (article group &optional buffer)
349   "Request the ARTICLE in GROUP.
350 ARTICLE can either be an article number or an article Message-ID.
351 If BUFFER, insert the article in that group."
352   (let ((gnus-command-method (gnus-find-method-for-group group)))
353     (funcall (gnus-get-function gnus-command-method 'request-article)
354              article (gnus-group-real-name group)
355              (nth 1 gnus-command-method) buffer)))
356
357 (defun gnus-request-head (article group)
358   "Request the head of ARTICLE in GROUP."
359   (let* ((gnus-command-method (gnus-find-method-for-group group))
360          (head (gnus-get-function gnus-command-method 'request-head t))
361          res clean-up)
362     (cond
363      ;; Check the cache.
364      ((and gnus-use-cache
365            (numberp article)
366            (gnus-cache-request-article article group))
367       (setq res (cons group article)
368             clean-up t))
369      ;; Use `head' function.
370      ((fboundp head)
371       (setq res (funcall head article (gnus-group-real-name group)
372                          (nth 1 gnus-command-method))))
373      ;; Use `article' function.
374      (t
375       (setq res (gnus-request-article article group)
376             clean-up t)))
377     (when clean-up
378       (save-excursion
379         (set-buffer nntp-server-buffer)
380         (goto-char (point-min))
381         (when (search-forward "\n\n" nil t)
382           (delete-region (1- (point)) (point-max)))
383         (nnheader-fold-continuation-lines)))
384     res))
385
386 (defun gnus-request-body (article group)
387   "Request the body of ARTICLE in GROUP."
388   (let* ((gnus-command-method (gnus-find-method-for-group group))
389          (head (gnus-get-function gnus-command-method 'request-body t))
390          res clean-up)
391     (cond
392      ;; Check the cache.
393      ((and gnus-use-cache
394            (numberp article)
395            (gnus-cache-request-article article group))
396       (setq res (cons group article)
397             clean-up t))
398      ;; Use `head' function.
399      ((fboundp head)
400       (setq res (funcall head article (gnus-group-real-name group)
401                          (nth 1 gnus-command-method))))
402      ;; Use `article' function.
403      (t
404       (setq res (gnus-request-article article group)
405             clean-up t)))
406     (when clean-up
407       (save-excursion
408         (set-buffer nntp-server-buffer)
409         (goto-char (point-min))
410         (when (search-forward "\n\n" nil t)
411           (delete-region (point-min) (1- (point))))))
412     res))
413
414 (defun gnus-request-post (gnus-command-method)
415   "Post the current buffer using GNUS-COMMAND-METHOD."
416   (when (stringp gnus-command-method)
417     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
418   (funcall (gnus-get-function gnus-command-method 'request-post)
419            (nth 1 gnus-command-method)))
420
421 (defun gnus-request-scan (group gnus-command-method)
422   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
423 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
424   (let ((gnus-command-method
425          (if group (gnus-find-method-for-group group) gnus-command-method))
426         (gnus-inhibit-demon t)
427         (mail-source-plugged gnus-plugged))
428     (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
429         (funcall (gnus-get-function gnus-command-method 'request-scan)
430                  (and group (gnus-group-real-name group))
431                  (nth 1 gnus-command-method)))))
432
433 (defsubst gnus-request-update-info (info gnus-command-method)
434   "Request that GNUS-COMMAND-METHOD update INFO."
435   (when (stringp gnus-command-method)
436     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
437   (when (gnus-check-backend-function
438          'request-update-info (car gnus-command-method))
439     (funcall (gnus-get-function gnus-command-method 'request-update-info)
440              (gnus-group-real-name (gnus-info-group info))
441              info (nth 1 gnus-command-method))))
442
443 (defun gnus-request-expire-articles (articles group &optional force)
444   (let ((gnus-command-method (gnus-find-method-for-group group)))
445     (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
446              articles (gnus-group-real-name group) (nth 1 gnus-command-method)
447              force)))
448
449 (defun gnus-request-move-article
450   (article group server accept-function &optional last)
451   (let ((gnus-command-method (gnus-find-method-for-group group)))
452     (funcall (gnus-get-function gnus-command-method 'request-move-article)
453              article (gnus-group-real-name group)
454              (nth 1 gnus-command-method) accept-function last)))
455
456 (defun gnus-request-accept-article (group &optional gnus-command-method last
457                                           no-encode)
458   ;; Make sure there's a newline at the end of the article.
459   (when (stringp gnus-command-method)
460     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
461   (when (and (not gnus-command-method)
462              (stringp group))
463     (setq gnus-command-method (gnus-group-name-to-method group)))
464   (goto-char (point-max))
465   (unless (bolp)
466     (insert "\n"))
467   (let ((func (car (or gnus-command-method
468                        (gnus-find-method-for-group group)))))
469     (funcall (intern (format "%s-request-accept-article" func))
470              (if (stringp group) (gnus-group-real-name group) group)
471              (cadr gnus-command-method)
472              last)))
473
474 (defun gnus-request-replace-article (article group buffer &optional no-encode)
475   (let ((func (car (gnus-group-name-to-method group))))
476     (funcall (intern (format "%s-request-replace-article" func))
477              article (gnus-group-real-name group) buffer)))
478
479 (defun gnus-request-associate-buffer (group)
480   (let ((gnus-command-method (gnus-find-method-for-group group)))
481     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
482              (gnus-group-real-name group))))
483
484 (defun gnus-request-restore-buffer (article group)
485   "Request a new buffer restored to the state of ARTICLE."
486   (let ((gnus-command-method (gnus-find-method-for-group group)))
487     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
488              article (gnus-group-real-name group)
489              (nth 1 gnus-command-method))))
490
491 (defun gnus-request-create-group (group &optional gnus-command-method args)
492   (when (stringp gnus-command-method)
493     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
494   (let ((gnus-command-method
495          (or gnus-command-method (gnus-find-method-for-group group))))
496     (funcall (gnus-get-function gnus-command-method 'request-create-group)
497              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
498
499 (defun gnus-request-delete-group (group &optional force)
500   (let ((gnus-command-method (gnus-find-method-for-group group)))
501     (funcall (gnus-get-function gnus-command-method 'request-delete-group)
502              (gnus-group-real-name group) force (nth 1 gnus-command-method))))
503
504 (defun gnus-request-rename-group (group new-name)
505   (let ((gnus-command-method (gnus-find-method-for-group group)))
506     (funcall (gnus-get-function gnus-command-method 'request-rename-group)
507              (gnus-group-real-name group)
508              (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
509
510 (defun gnus-close-backends ()
511   ;; Send a close request to all backends that support such a request.
512   (let ((methods gnus-valid-select-methods)
513         (gnus-inhibit-demon t)
514         func gnus-command-method)
515     (while (setq gnus-command-method (pop methods))
516       (when (fboundp (setq func (intern
517                                  (concat (car gnus-command-method)
518                                          "-request-close"))))
519         (funcall func)))))
520
521 (defun gnus-asynchronous-p (gnus-command-method)
522   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
523     (when (fboundp func)
524       (funcall func))))
525
526 (defun gnus-remove-denial (gnus-command-method)
527   (when (stringp gnus-command-method)
528     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
529   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
530          (status (cadr elem)))
531     ;; If this hasn't been opened before, we add it to the list.
532     (when (eq status 'denied)
533       ;; Set the status of this server.
534       (setcar (cdr elem) 'closed))))
535
536 (provide 'gnus-int)
537
538 ;;; gnus-int.el ends here