(gnus-uu-default-view-rules): Don't use `xv'.
[bpt/emacs.git] / lisp / gnus / gnus-int.el
CommitLineData
eec82323 1;;; gnus-int.el --- backend interface functions for Gnus
6748645f 2;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
eec82323 3
6748645f 4;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
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
249ffa67
RS
28(eval-when-compile (require 'cl))
29
6748645f
LMI
30(eval-when-compile (require 'cl))
31
eec82323
LMI
32(require 'gnus)
33
34(defcustom gnus-open-server-hook nil
35 "Hook called just before opening connection to the news server."
36 :group 'gnus-start
37 :type 'hook)
38
39;;;
40;;; Server Communication
41;;;
42
43(defun gnus-start-news-server (&optional confirm)
44 "Open a method for getting news.
45If CONFIRM is non-nil, the user will be asked for an NNTP server."
46 (let (how)
47 (if gnus-current-select-method
48 ;; Stream is already opened.
49 nil
50 ;; Open NNTP server.
51 (unless gnus-nntp-service
52 (setq gnus-nntp-server nil))
53 (when confirm
54 ;; Read server name with completion.
55 (setq gnus-nntp-server
56 (completing-read "NNTP server: "
57 (mapcar (lambda (server) (list server))
58 (cons (list gnus-nntp-server)
59 gnus-secondary-servers))
60 nil nil gnus-nntp-server)))
61
62 (when (and gnus-nntp-server
63 (stringp gnus-nntp-server)
64 (not (string= gnus-nntp-server "")))
65 (setq gnus-select-method
66 (cond ((or (string= gnus-nntp-server "")
67 (string= gnus-nntp-server "::"))
68 (list 'nnspool (system-name)))
69 ((string-match "^:" gnus-nntp-server)
70 (list 'nnmh gnus-nntp-server
71 (list 'nnmh-directory
72 (file-name-as-directory
73 (expand-file-name
74 (concat "~/" (substring
75 gnus-nntp-server 1)))))
76 (list 'nnmh-get-new-mail nil)))
77 (t
78 (list 'nntp gnus-nntp-server)))))
79
80 (setq how (car gnus-select-method))
81 (cond
82 ((eq how 'nnspool)
83 (require 'nnspool)
84 (gnus-message 5 "Looking up local news spool..."))
85 ((eq how 'nnmh)
86 (require 'nnmh)
87 (gnus-message 5 "Looking up mh spool..."))
88 (t
89 (require 'nntp)))
90 (setq gnus-current-select-method gnus-select-method)
6748645f 91 (gnus-run-hooks 'gnus-open-server-hook)
eec82323
LMI
92 (or
93 ;; gnus-open-server-hook might have opened it
94 (gnus-server-opened gnus-select-method)
95 (gnus-open-server gnus-select-method)
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.
112If METHOD is nil, use `gnus-select-method'.
113If 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)))))
6748645f 126 (gnus-run-hooks 'gnus-open-server-hook)
eec82323
LMI
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)))
6748645f
LMI
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.
eec82323 153 (unless (fboundp func)
6748645f
LMI
154 (unless (car method)
155 (error "Trying to require a method that doesn't exist"))
eec82323 156 (require (car method))
6748645f
LMI
157 (when (not (fboundp func))
158 (if noerror
159 (setq func nil)
160 (error "No such function: %s" func))))
eec82323
LMI
161 func))
162
163\f
164;;;
165;;; Interface functions to the backends.
166;;;
167
6748645f
LMI
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)))
eec82323
LMI
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
6748645f
LMI
180 (funcall (gnus-get-function gnus-command-method 'open-server)
181 (nth 1 gnus-command-method)
182 (nthcdr 2 gnus-command-method))))
eec82323
LMI
183 ;; If this hasn't been opened before, we add it to the list.
184 (unless elem
6748645f 185 (setq elem (list gnus-command-method nil)
eec82323
LMI
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
6748645f
LMI
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)))
eec82323 218 (when func
6748645f
LMI
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 (when (stringp gnus-command-method)
224 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
225 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
226 (nth 1 gnus-command-method)))
227
228(defun gnus-status-message (gnus-command-method)
229 "Return the status message from GNUS-COMMAND-METHOD.
230If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
eec82323 231this group uses will be queried."
6748645f
LMI
232 (let ((gnus-command-method
233 (if (stringp gnus-command-method)
234 (gnus-find-method-for-group gnus-command-method)
235 gnus-command-method)))
236 (funcall (gnus-get-function gnus-command-method 'status-message)
237 (nth 1 gnus-command-method))))
238
239(defun gnus-request-regenerate (gnus-command-method)
240 "Request a data generation from GNUS-COMMAND-METHOD."
241 (when (stringp gnus-command-method)
242 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
243 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
244 (nth 1 gnus-command-method)))
245
246(defun gnus-request-group (group &optional dont-check gnus-command-method)
eec82323 247 "Request GROUP. If DONT-CHECK, no information is required."
6748645f
LMI
248 (let ((gnus-command-method
249 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
250 (when (stringp gnus-command-method)
251 (setq gnus-command-method
252 (inline (gnus-server-to-method gnus-command-method))))
253 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
254 (gnus-group-real-name group) (nth 1 gnus-command-method)
255 dont-check)))
eec82323
LMI
256
257(defun gnus-list-active-group (group)
258 "Request active information on GROUP."
6748645f 259 (let ((gnus-command-method (gnus-find-method-for-group group))
eec82323
LMI
260 (func 'list-active-group))
261 (when (gnus-check-backend-function func group)
6748645f
LMI
262 (funcall (gnus-get-function gnus-command-method func)
263 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
eec82323
LMI
264
265(defun gnus-request-group-description (group)
266 "Request a description of GROUP."
6748645f 267 (let ((gnus-command-method (gnus-find-method-for-group group))
eec82323
LMI
268 (func 'request-group-description))
269 (when (gnus-check-backend-function func group)
6748645f
LMI
270 (funcall (gnus-get-function gnus-command-method func)
271 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
eec82323
LMI
272
273(defun gnus-close-group (group)
274 "Request the GROUP be closed."
6748645f
LMI
275 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
276 (funcall (gnus-get-function gnus-command-method 'close-group)
277 (gnus-group-real-name group) (nth 1 gnus-command-method))))
eec82323
LMI
278
279(defun gnus-retrieve-headers (articles group &optional fetch-old)
280 "Request headers for ARTICLES in GROUP.
281If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
6748645f 282 (let ((gnus-command-method (gnus-find-method-for-group group)))
eec82323
LMI
283 (if (and gnus-use-cache (numberp (car articles)))
284 (gnus-cache-retrieve-headers articles group fetch-old)
6748645f
LMI
285 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
286 articles (gnus-group-real-name group)
287 (nth 1 gnus-command-method) fetch-old))))
288
289(defun gnus-retrieve-articles (articles group)
290 "Request ARTICLES in GROUP."
291 (let ((gnus-command-method (gnus-find-method-for-group group)))
292 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
293 articles (gnus-group-real-name group)
294 (nth 1 gnus-command-method))))
295
296(defun gnus-retrieve-groups (groups gnus-command-method)
297 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
298 (when (stringp gnus-command-method)
299 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
300 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
301 groups (nth 1 gnus-command-method)))
eec82323
LMI
302
303(defun gnus-request-type (group &optional article)
304 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
6748645f
LMI
305 (let ((gnus-command-method (gnus-find-method-for-group group)))
306 (if (not (gnus-check-backend-function
307 'request-type (car gnus-command-method)))
eec82323 308 'unknown
6748645f 309 (funcall (gnus-get-function gnus-command-method 'request-type)
eec82323
LMI
310 (gnus-group-real-name group) article))))
311
312(defun gnus-request-update-mark (group article mark)
6748645f
LMI
313 "Allow the backend to change the mark the user tries to put on an article."
314 (let ((gnus-command-method (gnus-find-method-for-group group)))
315 (if (not (gnus-check-backend-function
316 'request-update-mark (car gnus-command-method)))
eec82323 317 mark
6748645f 318 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
eec82323
LMI
319 (gnus-group-real-name group) article mark))))
320
321(defun gnus-request-article (article group &optional buffer)
322 "Request the ARTICLE in GROUP.
323ARTICLE can either be an article number or an article Message-ID.
324If BUFFER, insert the article in that group."
6748645f
LMI
325 (let ((gnus-command-method (gnus-find-method-for-group group)))
326 (funcall (gnus-get-function gnus-command-method 'request-article)
327 article (gnus-group-real-name group)
328 (nth 1 gnus-command-method) buffer)))
eec82323
LMI
329
330(defun gnus-request-head (article group)
331 "Request the head of ARTICLE in GROUP."
6748645f
LMI
332 (let* ((gnus-command-method (gnus-find-method-for-group group))
333 (head (gnus-get-function gnus-command-method 'request-head t))
eec82323
LMI
334 res clean-up)
335 (cond
336 ;; Check the cache.
337 ((and gnus-use-cache
338 (numberp article)
339 (gnus-cache-request-article article group))
340 (setq res (cons group article)
341 clean-up t))
342 ;; Use `head' function.
343 ((fboundp head)
344 (setq res (funcall head article (gnus-group-real-name group)
6748645f 345 (nth 1 gnus-command-method))))
eec82323
LMI
346 ;; Use `article' function.
347 (t
348 (setq res (gnus-request-article article group)
349 clean-up t)))
350 (when clean-up
351 (save-excursion
352 (set-buffer nntp-server-buffer)
353 (goto-char (point-min))
354 (when (search-forward "\n\n" nil t)
355 (delete-region (1- (point)) (point-max)))
356 (nnheader-fold-continuation-lines)))
357 res))
358
359(defun gnus-request-body (article group)
360 "Request the body of ARTICLE in GROUP."
6748645f
LMI
361 (let* ((gnus-command-method (gnus-find-method-for-group group))
362 (head (gnus-get-function gnus-command-method 'request-body t))
363 res clean-up)
364 (cond
365 ;; Check the cache.
366 ((and gnus-use-cache
367 (numberp article)
368 (gnus-cache-request-article article group))
369 (setq res (cons group article)
370 clean-up t))
371 ;; Use `head' function.
372 ((fboundp head)
373 (setq res (funcall head article (gnus-group-real-name group)
374 (nth 1 gnus-command-method))))
375 ;; Use `article' function.
376 (t
377 (setq res (gnus-request-article article group)
378 clean-up t)))
379 (when clean-up
380 (save-excursion
381 (set-buffer nntp-server-buffer)
382 (goto-char (point-min))
383 (when (search-forward "\n\n" nil t)
384 (delete-region (point-min) (1- (point))))))
385 res))
eec82323 386
6748645f
LMI
387(defun gnus-request-post (gnus-command-method)
388 "Post the current buffer using GNUS-COMMAND-METHOD."
389 (when (stringp gnus-command-method)
390 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
391 (funcall (gnus-get-function gnus-command-method 'request-post)
392 (nth 1 gnus-command-method)))
393
394(defun gnus-request-scan (group gnus-command-method)
395 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
396If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
397 (when gnus-plugged
398 (let ((gnus-command-method
399 (if group (gnus-find-method-for-group group) gnus-command-method))
400 (gnus-inhibit-demon t))
401 (funcall (gnus-get-function gnus-command-method 'request-scan)
402 (and group (gnus-group-real-name group))
403 (nth 1 gnus-command-method)))))
404
405(defsubst gnus-request-update-info (info gnus-command-method)
406 "Request that GNUS-COMMAND-METHOD update INFO."
407 (when (stringp gnus-command-method)
408 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
409 (when (gnus-check-backend-function
410 'request-update-info (car gnus-command-method))
411 (funcall (gnus-get-function gnus-command-method 'request-update-info)
eec82323 412 (gnus-group-real-name (gnus-info-group info))
6748645f 413 info (nth 1 gnus-command-method))))
eec82323
LMI
414
415(defun gnus-request-expire-articles (articles group &optional force)
6748645f
LMI
416 (let ((gnus-command-method (gnus-find-method-for-group group)))
417 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
418 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
eec82323
LMI
419 force)))
420
421(defun gnus-request-move-article
422 (article group server accept-function &optional last)
6748645f
LMI
423 (let ((gnus-command-method (gnus-find-method-for-group group)))
424 (funcall (gnus-get-function gnus-command-method 'request-move-article)
eec82323 425 article (gnus-group-real-name group)
6748645f 426 (nth 1 gnus-command-method) accept-function last)))
eec82323 427
6748645f 428(defun gnus-request-accept-article (group &optional gnus-command-method last)
eec82323 429 ;; Make sure there's a newline at the end of the article.
6748645f
LMI
430 (when (stringp gnus-command-method)
431 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
432 (when (and (not gnus-command-method)
eec82323 433 (stringp group))
6748645f 434 (setq gnus-command-method (gnus-group-name-to-method group)))
eec82323
LMI
435 (goto-char (point-max))
436 (unless (bolp)
437 (insert "\n"))
6748645f
LMI
438 (let ((func (car (or gnus-command-method
439 (gnus-find-method-for-group group)))))
eec82323
LMI
440 (funcall (intern (format "%s-request-accept-article" func))
441 (if (stringp group) (gnus-group-real-name group) group)
6748645f 442 (cadr gnus-command-method)
eec82323
LMI
443 last)))
444
445(defun gnus-request-replace-article (article group buffer)
a8151ef7 446 (let ((func (car (gnus-group-name-to-method group))))
eec82323
LMI
447 (funcall (intern (format "%s-request-replace-article" func))
448 article (gnus-group-real-name group) buffer)))
449
450(defun gnus-request-associate-buffer (group)
6748645f
LMI
451 (let ((gnus-command-method (gnus-find-method-for-group group)))
452 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
eec82323
LMI
453 (gnus-group-real-name group))))
454
455(defun gnus-request-restore-buffer (article group)
456 "Request a new buffer restored to the state of ARTICLE."
6748645f
LMI
457 (let ((gnus-command-method (gnus-find-method-for-group group)))
458 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
459 article (gnus-group-real-name group)
460 (nth 1 gnus-command-method))))
eec82323 461
6748645f
LMI
462(defun gnus-request-create-group (group &optional gnus-command-method args)
463 (when (stringp gnus-command-method)
464 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
465 (let ((gnus-command-method
466 (or gnus-command-method (gnus-find-method-for-group group))))
467 (funcall (gnus-get-function gnus-command-method 'request-create-group)
468 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
eec82323
LMI
469
470(defun gnus-request-delete-group (group &optional force)
6748645f
LMI
471 (let ((gnus-command-method (gnus-find-method-for-group group)))
472 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
473 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
eec82323
LMI
474
475(defun gnus-request-rename-group (group new-name)
6748645f
LMI
476 (let ((gnus-command-method (gnus-find-method-for-group group)))
477 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
eec82323 478 (gnus-group-real-name group)
6748645f 479 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
eec82323
LMI
480
481(defun gnus-close-backends ()
482 ;; Send a close request to all backends that support such a request.
483 (let ((methods gnus-valid-select-methods)
484 (gnus-inhibit-demon t)
6748645f
LMI
485 func gnus-command-method)
486 (while (setq gnus-command-method (pop methods))
eec82323 487 (when (fboundp (setq func (intern
6748645f
LMI
488 (concat (car gnus-command-method)
489 "-request-close"))))
eec82323
LMI
490 (funcall func)))))
491
6748645f
LMI
492(defun gnus-asynchronous-p (gnus-command-method)
493 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
eec82323
LMI
494 (when (fboundp func)
495 (funcall func))))
496
6748645f
LMI
497(defun gnus-remove-denial (gnus-command-method)
498 (when (stringp gnus-command-method)
499 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
500 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
eec82323
LMI
501 (status (cadr elem)))
502 ;; If this hasn't been opened before, we add it to the list.
503 (when (eq status 'denied)
504 ;; Set the status of this server.
505 (setcar (cdr elem) 'closed))))
506
507(provide 'gnus-int)
508
509;;; gnus-int.el ends here