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