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