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