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