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