*** 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-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   (when (stringp gnus-command-method)
223     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
224   (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
225            (nth 1 gnus-command-method)))
226
227 (defun gnus-status-message (gnus-command-method)
228   "Return the status message from GNUS-COMMAND-METHOD.
229 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name.   The method
230 this group uses will be queried."
231   (let ((gnus-command-method
232          (if (stringp gnus-command-method)
233              (gnus-find-method-for-group gnus-command-method)
234            gnus-command-method)))
235     (funcall (gnus-get-function gnus-command-method 'status-message)
236              (nth 1 gnus-command-method))))
237
238 (defun gnus-request-regenerate (gnus-command-method)
239   "Request a data generation from GNUS-COMMAND-METHOD."
240   (when (stringp gnus-command-method)
241     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
242   (funcall (gnus-get-function gnus-command-method 'request-regenerate)
243            (nth 1 gnus-command-method)))
244
245 (defun gnus-request-group (group &optional dont-check gnus-command-method)
246   "Request GROUP.  If DONT-CHECK, no information is required."
247   (let ((gnus-command-method
248          (or gnus-command-method (inline (gnus-find-method-for-group group)))))
249     (when (stringp gnus-command-method)
250       (setq gnus-command-method
251             (inline (gnus-server-to-method gnus-command-method))))
252     (funcall (inline (gnus-get-function gnus-command-method 'request-group))
253              (gnus-group-real-name group) (nth 1 gnus-command-method)
254              dont-check)))
255
256 (defun gnus-list-active-group (group)
257   "Request active information on GROUP."
258   (let ((gnus-command-method (gnus-find-method-for-group group))
259         (func 'list-active-group))
260     (when (gnus-check-backend-function func group)
261       (funcall (gnus-get-function gnus-command-method func)
262                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
263
264 (defun gnus-request-group-description (group)
265   "Request a description of GROUP."
266   (let ((gnus-command-method (gnus-find-method-for-group group))
267         (func 'request-group-description))
268     (when (gnus-check-backend-function func group)
269       (funcall (gnus-get-function gnus-command-method func)
270                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
271
272 (defun gnus-request-group-articles (group)
273   "Request a list of existing articles in GROUP."
274   (let ((gnus-command-method (gnus-find-method-for-group group))
275         (func 'request-group-articles))
276     (when (gnus-check-backend-function func group)
277       (funcall (gnus-get-function gnus-command-method func)
278                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
279
280 (defun gnus-close-group (group)
281   "Request the GROUP be closed."
282   (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
283     (funcall (gnus-get-function gnus-command-method 'close-group)
284              (gnus-group-real-name group) (nth 1 gnus-command-method))))
285
286 (defun gnus-retrieve-headers (articles group &optional fetch-old)
287   "Request headers for ARTICLES in GROUP.
288 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
289   (let ((gnus-command-method (gnus-find-method-for-group group)))
290     (if (and gnus-use-cache (numberp (car articles)))
291         (gnus-cache-retrieve-headers articles group fetch-old)
292       (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
293                articles (gnus-group-real-name group)
294                (nth 1 gnus-command-method) fetch-old))))
295
296 (defun gnus-retrieve-articles (articles group)
297   "Request ARTICLES in GROUP."
298   (let ((gnus-command-method (gnus-find-method-for-group group)))
299     (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
300              articles (gnus-group-real-name group)
301              (nth 1 gnus-command-method))))
302
303 (defun gnus-retrieve-groups (groups gnus-command-method)
304   "Request active information on GROUPS from GNUS-COMMAND-METHOD."
305   (when (stringp gnus-command-method)
306     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
307   (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
308            groups (nth 1 gnus-command-method)))
309
310 (defun gnus-request-type (group &optional article)
311   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
312   (let ((gnus-command-method (gnus-find-method-for-group group)))
313     (if (not (gnus-check-backend-function
314               'request-type (car gnus-command-method)))
315         'unknown
316       (funcall (gnus-get-function gnus-command-method 'request-type)
317                (gnus-group-real-name group) article))))
318
319 (defun gnus-request-set-mark (group action)
320   "Set marks on articles in the backend."
321   (let ((gnus-command-method (gnus-find-method-for-group group)))
322     (if (not (gnus-check-backend-function
323              'request-set-mark (car gnus-command-method)))
324        action
325       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
326               (gnus-group-real-name group) action
327               (nth 1 gnus-command-method)))))
328
329 (defun gnus-request-update-mark (group article mark)
330   "Allow the backend to change the mark the user tries to put on an article."
331   (let ((gnus-command-method (gnus-find-method-for-group group)))
332     (if (not (gnus-check-backend-function
333               'request-update-mark (car gnus-command-method)))
334         mark
335       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
336                (gnus-group-real-name group) article mark))))
337
338 (defun gnus-request-article (article group &optional buffer)
339   "Request the ARTICLE in GROUP.
340 ARTICLE can either be an article number or an article Message-ID.
341 If BUFFER, insert the article in that group."
342   (let ((gnus-command-method (gnus-find-method-for-group group)))
343     (funcall (gnus-get-function gnus-command-method 'request-article)
344              article (gnus-group-real-name group)
345              (nth 1 gnus-command-method) buffer)))
346
347 (defun gnus-request-head (article group)
348   "Request the head of ARTICLE in GROUP."
349   (let* ((gnus-command-method (gnus-find-method-for-group group))
350          (head (gnus-get-function gnus-command-method 'request-head t))
351          res clean-up)
352     (cond
353      ;; Check the cache.
354      ((and gnus-use-cache
355            (numberp article)
356            (gnus-cache-request-article article group))
357       (setq res (cons group article)
358             clean-up t))
359      ;; Use `head' function.
360      ((fboundp head)
361       (setq res (funcall head article (gnus-group-real-name group)
362                          (nth 1 gnus-command-method))))
363      ;; Use `article' function.
364      (t
365       (setq res (gnus-request-article article group)
366             clean-up t)))
367     (when clean-up
368       (save-excursion
369         (set-buffer nntp-server-buffer)
370         (goto-char (point-min))
371         (when (search-forward "\n\n" nil t)
372           (delete-region (1- (point)) (point-max)))
373         (nnheader-fold-continuation-lines)))
374     res))
375
376 (defun gnus-request-body (article group)
377   "Request the body of ARTICLE in GROUP."
378   (let* ((gnus-command-method (gnus-find-method-for-group group))
379          (head (gnus-get-function gnus-command-method 'request-body t))
380          res clean-up)
381     (cond
382      ;; Check the cache.
383      ((and gnus-use-cache
384            (numberp article)
385            (gnus-cache-request-article article group))
386       (setq res (cons group article)
387             clean-up t))
388      ;; Use `head' function.
389      ((fboundp head)
390       (setq res (funcall head article (gnus-group-real-name group)
391                          (nth 1 gnus-command-method))))
392      ;; Use `article' function.
393      (t
394       (setq res (gnus-request-article article group)
395             clean-up t)))
396     (when clean-up
397       (save-excursion
398         (set-buffer nntp-server-buffer)
399         (goto-char (point-min))
400         (when (search-forward "\n\n" nil t)
401           (delete-region (point-min) (1- (point))))))
402     res))
403
404 (defun gnus-request-post (gnus-command-method)
405   "Post the current buffer using GNUS-COMMAND-METHOD."
406   (when (stringp gnus-command-method)
407     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
408   (funcall (gnus-get-function gnus-command-method 'request-post)
409            (nth 1 gnus-command-method)))
410
411 (defun gnus-request-scan (group gnus-command-method)
412   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
413 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
414   (when gnus-plugged
415     (let ((gnus-command-method
416            (if group (gnus-find-method-for-group group) gnus-command-method))
417           (gnus-inhibit-demon t))
418       (funcall (gnus-get-function gnus-command-method 'request-scan)
419                (and group (gnus-group-real-name group))
420                (nth 1 gnus-command-method)))))
421
422 (defsubst gnus-request-update-info (info gnus-command-method)
423   "Request that GNUS-COMMAND-METHOD update INFO."
424   (when (stringp gnus-command-method)
425     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
426   (when (gnus-check-backend-function
427          'request-update-info (car gnus-command-method))
428     (funcall (gnus-get-function gnus-command-method 'request-update-info)
429              (gnus-group-real-name (gnus-info-group info))
430              info (nth 1 gnus-command-method))))
431
432 (defun gnus-request-expire-articles (articles group &optional force)
433   (let ((gnus-command-method (gnus-find-method-for-group group)))
434     (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
435              articles (gnus-group-real-name group) (nth 1 gnus-command-method)
436              force)))
437
438 (defun gnus-request-move-article
439   (article group server accept-function &optional last)
440   (let ((gnus-command-method (gnus-find-method-for-group group)))
441     (funcall (gnus-get-function gnus-command-method 'request-move-article)
442              article (gnus-group-real-name group)
443              (nth 1 gnus-command-method) accept-function last)))
444
445 (defun gnus-request-accept-article (group &optional gnus-command-method last
446                                           no-encode)
447   ;; Make sure there's a newline at the end of the article.
448   (when (stringp gnus-command-method)
449     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
450   (when (and (not gnus-command-method)
451              (stringp group))
452     (setq gnus-command-method (gnus-group-name-to-method group)))
453   (goto-char (point-max))
454   (unless (bolp)
455     (insert "\n"))
456   (unless no-encode
457     (save-restriction
458       (message-narrow-to-head)
459       (mail-encode-encoded-word-buffer))
460     (message-encode-message-body))
461   (let ((func (car (or gnus-command-method
462                        (gnus-find-method-for-group group)))))
463     (funcall (intern (format "%s-request-accept-article" func))
464              (if (stringp group) (gnus-group-real-name group) group)
465              (cadr gnus-command-method)
466              last)))
467
468 (defun gnus-request-replace-article (article group buffer &optional no-encode)
469   (unless no-encode
470     (save-restriction
471       (message-narrow-to-head)
472       (mail-encode-encoded-word-buffer))
473     (message-encode-message-body))
474   (let ((func (car (gnus-group-name-to-method group))))
475     (funcall (intern (format "%s-request-replace-article" func))
476              article (gnus-group-real-name group) buffer)))
477
478 (defun gnus-request-associate-buffer (group)
479   (let ((gnus-command-method (gnus-find-method-for-group group)))
480     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
481              (gnus-group-real-name group))))
482
483 (defun gnus-request-restore-buffer (article group)
484   "Request a new buffer restored to the state of ARTICLE."
485   (let ((gnus-command-method (gnus-find-method-for-group group)))
486     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
487              article (gnus-group-real-name group)
488              (nth 1 gnus-command-method))))
489
490 (defun gnus-request-create-group (group &optional gnus-command-method args)
491   (when (stringp gnus-command-method)
492     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
493   (let ((gnus-command-method
494          (or gnus-command-method (gnus-find-method-for-group group))))
495     (funcall (gnus-get-function gnus-command-method 'request-create-group)
496              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
497
498 (defun gnus-request-delete-group (group &optional force)
499   (let ((gnus-command-method (gnus-find-method-for-group group)))
500     (funcall (gnus-get-function gnus-command-method 'request-delete-group)
501              (gnus-group-real-name group) force (nth 1 gnus-command-method))))
502
503 (defun gnus-request-rename-group (group new-name)
504   (let ((gnus-command-method (gnus-find-method-for-group group)))
505     (funcall (gnus-get-function gnus-command-method 'request-rename-group)
506              (gnus-group-real-name group)
507              (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
508
509 (defun gnus-close-backends ()
510   ;; Send a close request to all backends that support such a request.
511   (let ((methods gnus-valid-select-methods)
512         (gnus-inhibit-demon t)
513         func gnus-command-method)
514     (while (setq gnus-command-method (pop methods))
515       (when (fboundp (setq func (intern
516                                  (concat (car gnus-command-method)
517                                          "-request-close"))))
518         (funcall func)))))
519
520 (defun gnus-asynchronous-p (gnus-command-method)
521   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
522     (when (fboundp func)
523       (funcall func))))
524
525 (defun gnus-remove-denial (gnus-command-method)
526   (when (stringp gnus-command-method)
527     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
528   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
529          (status (cadr elem)))
530     ;; If this hasn't been opened before, we add it to the list.
531     (when (eq status 'denied)
532       ;; Set the status of this server.
533       (setcar (cdr elem) 'closed))))
534
535 (provide 'gnus-int)
536
537 ;;; gnus-int.el ends here