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