*** empty log message ***
[emacs.git] / lisp / gnus / gnus-int.el
blobd5a507599f9a48c828fcbf0f3ba01637e21b8af0
1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
31 (require 'gnus)
33 (defcustom gnus-open-server-hook nil
34 "Hook called just before opening connection to the news server."
35 :group 'gnus-start
36 :type 'hook)
38 ;;;
39 ;;; Server Communication
40 ;;;
42 (defun gnus-start-news-server (&optional confirm)
43 "Open a method for getting news.
44 If CONFIRM is non-nil, the user will be asked for an NNTP server."
45 (let (how)
46 (if gnus-current-select-method
47 ;; Stream is already opened.
48 nil
49 ;; Open NNTP server.
50 (unless gnus-nntp-service
51 (setq gnus-nntp-server nil))
52 (when confirm
53 ;; Read server name with completion.
54 (setq gnus-nntp-server
55 (completing-read "NNTP server: "
56 (mapcar (lambda (server) (list server))
57 (cons (list gnus-nntp-server)
58 gnus-secondary-servers))
59 nil nil gnus-nntp-server)))
61 (when (and gnus-nntp-server
62 (stringp gnus-nntp-server)
63 (not (string= gnus-nntp-server "")))
64 (setq gnus-select-method
65 (cond ((or (string= gnus-nntp-server "")
66 (string= gnus-nntp-server "::"))
67 (list 'nnspool (system-name)))
68 ((string-match "^:" gnus-nntp-server)
69 (list 'nnmh gnus-nntp-server
70 (list 'nnmh-directory
71 (file-name-as-directory
72 (expand-file-name
73 (substring gnus-nntp-server 1) "~/")))
74 (list 'nnmh-get-new-mail nil)))
76 (list 'nntp gnus-nntp-server)))))
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..."))
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))))))
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))))
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.
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 ""))))))
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))
164 ;;; Interface functions to the backends.
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))))
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)))
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)))
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)))
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)))))
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))))
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))))
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)))
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)))
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)))))
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)))))
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)))))
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))))
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))))
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))))
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)))
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))))
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)))))
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))))
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)))
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.
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))
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.
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))
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)))
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)))))
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))))
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)))
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)))
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 (let ((mail-parse-charset message-default-charset))
463 (mail-encode-encoded-word-buffer)))
464 (message-encode-message-body))
465 (let ((func (car (or gnus-command-method
466 (gnus-find-method-for-group group)))))
467 (funcall (intern (format "%s-request-accept-article" func))
468 (if (stringp group) (gnus-group-real-name group) group)
469 (cadr gnus-command-method)
470 last)))
472 (defun gnus-request-replace-article (article group buffer &optional no-encode)
473 (unless no-encode
474 (save-restriction
475 (message-narrow-to-head)
476 (let ((mail-parse-charset message-default-charset))
477 (mail-encode-encoded-word-buffer)))
478 (message-encode-message-body))
479 (let ((func (car (gnus-group-name-to-method group))))
480 (funcall (intern (format "%s-request-replace-article" func))
481 article (gnus-group-real-name group) buffer)))
483 (defun gnus-request-associate-buffer (group)
484 (let ((gnus-command-method (gnus-find-method-for-group group)))
485 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
486 (gnus-group-real-name group))))
488 (defun gnus-request-restore-buffer (article group)
489 "Request a new buffer restored to the state of ARTICLE."
490 (let ((gnus-command-method (gnus-find-method-for-group group)))
491 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
492 article (gnus-group-real-name group)
493 (nth 1 gnus-command-method))))
495 (defun gnus-request-create-group (group &optional gnus-command-method args)
496 (when (stringp gnus-command-method)
497 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
498 (let ((gnus-command-method
499 (or gnus-command-method (gnus-find-method-for-group group))))
500 (funcall (gnus-get-function gnus-command-method 'request-create-group)
501 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
503 (defun gnus-request-delete-group (group &optional force)
504 (let ((gnus-command-method (gnus-find-method-for-group group)))
505 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
506 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
508 (defun gnus-request-rename-group (group new-name)
509 (let ((gnus-command-method (gnus-find-method-for-group group)))
510 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
511 (gnus-group-real-name group)
512 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
514 (defun gnus-close-backends ()
515 ;; Send a close request to all backends that support such a request.
516 (let ((methods gnus-valid-select-methods)
517 (gnus-inhibit-demon t)
518 func gnus-command-method)
519 (while (setq gnus-command-method (pop methods))
520 (when (fboundp (setq func (intern
521 (concat (car gnus-command-method)
522 "-request-close"))))
523 (funcall func)))))
525 (defun gnus-asynchronous-p (gnus-command-method)
526 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
527 (when (fboundp func)
528 (funcall func))))
530 (defun gnus-remove-denial (gnus-command-method)
531 (when (stringp gnus-command-method)
532 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
533 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
534 (status (cadr elem)))
535 ;; If this hasn't been opened before, we add it to the list.
536 (when (eq status 'denied)
537 ;; Set the status of this server.
538 (setcar (cdr elem) 'closed))))
540 (provide 'gnus-int)
542 ;;; gnus-int.el ends here