1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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)
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.
29 (eval-when-compile (require 'cl))
33 (defcustom gnus-open-server-hook nil
34 "Hook called just before opening connection to the news server."
38 (defvar gnus-server-unopen-status nil
39 "The default status if the server is not able to open.
40 If the server is covered by Gnus agent, the possible values are
41 `denied', set the server denied; `offline', set the server offline;
42 `nil', ask user. If the server is not covered by Gnus agent, set the
46 ;;; Server Communication
49 (defun gnus-start-news-server (&optional confirm)
50 "Open a method for getting news.
51 If CONFIRM is non-nil, the user will be asked for an NNTP server."
53 (if gnus-current-select-method
54 ;; Stream is already opened.
57 (unless gnus-nntp-service
58 (setq gnus-nntp-server nil))
60 ;; Read server name with completion.
61 (setq gnus-nntp-server
62 (completing-read "NNTP server: "
63 (mapcar (lambda (server) (list server))
64 (cons (list gnus-nntp-server)
65 gnus-secondary-servers))
66 nil nil gnus-nntp-server)))
68 (when (and gnus-nntp-server
69 (stringp gnus-nntp-server)
70 (not (string= gnus-nntp-server "")))
71 (setq gnus-select-method
72 (cond ((or (string= gnus-nntp-server "")
73 (string= gnus-nntp-server "::"))
74 (list 'nnspool (system-name)))
75 ((string-match "^:" gnus-nntp-server)
76 (list 'nnmh gnus-nntp-server
78 (file-name-as-directory
80 (substring gnus-nntp-server 1) "~/")))
81 (list 'nnmh-get-new-mail nil)))
83 (list 'nntp gnus-nntp-server)))))
85 (setq how (car gnus-select-method))
89 (gnus-message 5 "Looking up local news spool..."))
92 (gnus-message 5 "Looking up mh spool..."))
95 (setq gnus-current-select-method gnus-select-method)
96 (gnus-run-hooks 'gnus-open-server-hook)
98 ;; gnus-open-server-hook might have opened it
99 (gnus-server-opened gnus-select-method)
100 (gnus-open-server gnus-select-method)
104 "%s (%s) open error: '%s'. Continue? "
105 (car gnus-select-method) (cadr gnus-select-method)
106 (gnus-status-message gnus-select-method)))
107 (gnus-error 1 "Couldn't open server on %s"
108 (nth 1 gnus-select-method))))))
110 (defun gnus-check-group (group)
111 "Try to make sure that the server where GROUP exists is alive."
112 (let ((method (gnus-find-method-for-group group)))
113 (or (gnus-server-opened method)
114 (gnus-open-server method))))
116 (defun gnus-check-server (&optional method silent)
117 "Check whether the connection to METHOD is down.
118 If METHOD is nil, use `gnus-select-method'.
119 If it is down, start it up (again)."
120 (let ((method (or method gnus-select-method))
122 ;; Transform virtual server names into select methods.
123 (when (stringp method)
124 (setq method (gnus-server-to-method method)))
125 (if (gnus-server-opened method)
126 ;; The stream is already opened.
130 (gnus-message 5 "Opening %s server%s..." (car method)
131 (if (equal (nth 1 method) "") ""
132 (format " on %s" (nth 1 method)))))
133 (gnus-run-hooks 'gnus-open-server-hook)
136 (setq result (gnus-open-server method))
137 (quit (message "Quit gnus-check-server")
140 (gnus-message 5 "Opening %s server%s...%s" (car method)
141 (if (equal (nth 1 method) "") ""
142 (format " on %s" (nth 1 method)))
143 (if result "done" "failed")))))))
145 (defun gnus-get-function (method function &optional noerror)
146 "Return a function symbol based on METHOD and FUNCTION."
147 ;; Translate server names into methods.
149 (error "Attempted use of a nil select method"))
150 (when (stringp method)
151 (setq method (gnus-server-to-method method)))
152 ;; Check cache of constructed names.
153 (let* ((method-sym (if gnus-agent
154 (gnus-agent-get-function method)
156 (method-fns (get method-sym 'gnus-method-functions))
157 (func (let ((method-fnlist-elt (assq function method-fns)))
158 (unless method-fnlist-elt
159 (setq method-fnlist-elt
161 (intern (format "%s-%s" method-sym function))))
162 (put method-sym 'gnus-method-functions
163 (cons method-fnlist-elt method-fns)))
164 (cdr method-fnlist-elt))))
165 ;; Maybe complain if there is no function.
166 (unless (fboundp func)
168 (error "Trying to require a method that doesn't exist"))
169 (require (car method))
170 (when (not (fboundp func))
173 (error "No such function: %s" func))))
178 ;;; Interface functions to the backends.
181 (defun gnus-open-server (gnus-command-method)
182 "Open a connection to GNUS-COMMAND-METHOD."
183 (when (stringp gnus-command-method)
184 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
185 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
186 ;; If this method was previously denied, we just return nil.
187 (if (eq (nth 1 elem) 'denied)
189 (gnus-message 1 "Denied server")
194 (funcall (gnus-get-function gnus-command-method 'open-server)
195 (nth 1 gnus-command-method)
196 (nthcdr 2 gnus-command-method))
198 (message "Quit trying to open server")
200 ;; If this hasn't been opened before, we add it to the list.
202 (setq elem (list gnus-command-method nil)
203 gnus-opened-servers (cons elem gnus-opened-servers)))
204 ;; Set the status of this server.
207 (if (eq (cadr elem) 'offline)
211 (not (eq (cadr elem) 'offline))
212 (gnus-agent-method-p gnus-command-method))
213 (or gnus-server-unopen-status
215 (format "Unable to open %s:%s, go offline? "
216 (car gnus-command-method)
217 (cadr gnus-command-method)))
221 ;; Return the result from the "open" call.
222 (or (eq (cadr elem) 'offline)
225 (defun gnus-close-server (gnus-command-method)
226 "Close the connection to GNUS-COMMAND-METHOD."
227 (when (stringp gnus-command-method)
228 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
229 (funcall (gnus-get-function gnus-command-method 'close-server)
230 (nth 1 gnus-command-method)))
232 (defun gnus-request-list (gnus-command-method)
233 "Request the active file from GNUS-COMMAND-METHOD."
234 (when (stringp gnus-command-method)
235 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
236 (funcall (gnus-get-function gnus-command-method 'request-list)
237 (nth 1 gnus-command-method)))
239 (defun gnus-request-list-newsgroups (gnus-command-method)
240 "Request the newsgroups file 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-list-newsgroups)
244 (nth 1 gnus-command-method)))
246 (defun gnus-request-newgroups (date gnus-command-method)
247 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
248 (when (stringp gnus-command-method)
249 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
250 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
252 (funcall func date (nth 1 gnus-command-method)))))
254 (defun gnus-server-opened (gnus-command-method)
255 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
256 (unless (eq (gnus-server-status gnus-command-method)
258 (when (stringp gnus-command-method)
259 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
260 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
261 (nth 1 gnus-command-method))))
263 (defun gnus-status-message (gnus-command-method)
264 "Return the status message from GNUS-COMMAND-METHOD.
265 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
266 this group uses will be queried."
267 (let ((gnus-command-method
268 (if (stringp gnus-command-method)
269 (gnus-find-method-for-group gnus-command-method)
270 gnus-command-method)))
271 (funcall (gnus-get-function gnus-command-method 'status-message)
272 (nth 1 gnus-command-method))))
274 (defun gnus-request-regenerate (gnus-command-method)
275 "Request a data generation from GNUS-COMMAND-METHOD."
276 (when (stringp gnus-command-method)
277 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
278 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
279 (nth 1 gnus-command-method)))
281 (defun gnus-request-group (group &optional dont-check gnus-command-method)
282 "Request GROUP. If DONT-CHECK, no information is required."
283 (let ((gnus-command-method
284 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
285 (when (stringp gnus-command-method)
286 (setq gnus-command-method
287 (inline (gnus-server-to-method gnus-command-method))))
288 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
289 (gnus-group-real-name group) (nth 1 gnus-command-method)
292 (defun gnus-list-active-group (group)
293 "Request active information on GROUP."
294 (let ((gnus-command-method (gnus-find-method-for-group group))
295 (func 'list-active-group))
296 (when (gnus-check-backend-function func group)
297 (funcall (gnus-get-function gnus-command-method func)
298 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
300 (defun gnus-request-group-description (group)
301 "Request a description of GROUP."
302 (let ((gnus-command-method (gnus-find-method-for-group group))
303 (func 'request-group-description))
304 (when (gnus-check-backend-function func group)
305 (funcall (gnus-get-function gnus-command-method func)
306 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
308 (defun gnus-request-group-articles (group)
309 "Request a list of existing articles in GROUP."
310 (let ((gnus-command-method (gnus-find-method-for-group group))
311 (func 'request-group-articles))
312 (when (gnus-check-backend-function func group)
313 (funcall (gnus-get-function gnus-command-method func)
314 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
316 (defun gnus-close-group (group)
317 "Request the GROUP be closed."
318 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
319 (funcall (gnus-get-function gnus-command-method 'close-group)
320 (gnus-group-real-name group) (nth 1 gnus-command-method))))
322 (defun gnus-retrieve-headers (articles group &optional fetch-old)
323 "Request headers for ARTICLES in GROUP.
324 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
325 (let ((gnus-command-method (gnus-find-method-for-group group)))
327 ((and gnus-use-cache (numberp (car articles)))
328 (gnus-cache-retrieve-headers articles group fetch-old))
329 ((and gnus-agent gnus-agent-cache (gnus-online gnus-command-method)
330 (gnus-agent-method-p gnus-command-method))
331 (gnus-agent-retrieve-headers articles group fetch-old))
333 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
334 articles (gnus-group-real-name group)
335 (nth 1 gnus-command-method) fetch-old)))))
337 (defun gnus-retrieve-articles (articles group)
338 "Request ARTICLES in GROUP."
339 (let ((gnus-command-method (gnus-find-method-for-group group)))
340 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
341 articles (gnus-group-real-name group)
342 (nth 1 gnus-command-method))))
344 (defun gnus-retrieve-groups (groups gnus-command-method)
345 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
346 (when (stringp gnus-command-method)
347 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
348 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
349 groups (nth 1 gnus-command-method)))
351 (defun gnus-request-type (group &optional article)
352 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
353 (let ((gnus-command-method (gnus-find-method-for-group group)))
354 (if (not (gnus-check-backend-function
355 'request-type (car gnus-command-method)))
357 (funcall (gnus-get-function gnus-command-method 'request-type)
358 (gnus-group-real-name group) article))))
360 (defun gnus-request-set-mark (group action)
361 "Set marks on articles in the backend."
362 (let ((gnus-command-method (gnus-find-method-for-group group)))
363 (if (not (gnus-check-backend-function
364 'request-set-mark (car gnus-command-method)))
366 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
367 (gnus-group-real-name group) action
368 (nth 1 gnus-command-method)))))
370 (defun gnus-request-update-mark (group article mark)
371 "Allow the backend to change the mark the user tries to put on an article."
372 (let ((gnus-command-method (gnus-find-method-for-group group)))
373 (if (not (gnus-check-backend-function
374 'request-update-mark (car gnus-command-method)))
376 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
377 (gnus-group-real-name group) article mark))))
379 (defun gnus-request-article (article group &optional buffer)
380 "Request the ARTICLE in GROUP.
381 ARTICLE can either be an article number or an article Message-ID.
382 If BUFFER, insert the article in that group."
383 (let ((gnus-command-method (gnus-find-method-for-group group)))
384 (funcall (gnus-get-function gnus-command-method 'request-article)
385 article (gnus-group-real-name group)
386 (nth 1 gnus-command-method) buffer)))
388 (defun gnus-request-head (article group)
389 "Request the head of ARTICLE in GROUP."
390 (let* ((gnus-command-method (gnus-find-method-for-group group))
391 (head (gnus-get-function gnus-command-method 'request-head t))
397 (gnus-cache-request-article article group))
398 (setq res (cons group article)
400 ((and gnus-agent gnus-agent-cache gnus-plugged
402 (gnus-agent-request-article article group))
403 (setq res (cons group article)
405 ;; Use `head' function.
407 (setq res (funcall head article (gnus-group-real-name group)
408 (nth 1 gnus-command-method))))
409 ;; Use `article' function.
411 (setq res (gnus-request-article article group)
415 (set-buffer nntp-server-buffer)
416 (goto-char (point-min))
417 (when (search-forward "\n\n" nil t)
418 (delete-region (1- (point)) (point-max)))
419 (nnheader-fold-continuation-lines)))
422 (defun gnus-request-body (article group)
423 "Request the body of ARTICLE in GROUP."
424 (let* ((gnus-command-method (gnus-find-method-for-group group))
425 (head (gnus-get-function gnus-command-method 'request-body t))
431 (gnus-cache-request-article article group))
432 (setq res (cons group article)
434 ;; Check the agent cache.
435 ((and gnus-agent gnus-agent-cache gnus-plugged
437 (gnus-agent-request-article article group))
438 (setq res (cons group article)
440 ;; Use `head' function.
442 (setq res (funcall head article (gnus-group-real-name group)
443 (nth 1 gnus-command-method))))
444 ;; Use `article' function.
446 (setq res (gnus-request-article article group)
450 (set-buffer nntp-server-buffer)
451 (goto-char (point-min))
452 (when (search-forward "\n\n" nil t)
453 (delete-region (point-min) (1- (point))))))
456 (defun gnus-request-post (gnus-command-method)
457 "Post the current buffer using GNUS-COMMAND-METHOD."
458 (when (stringp gnus-command-method)
459 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
460 (funcall (gnus-get-function gnus-command-method 'request-post)
461 (nth 1 gnus-command-method)))
463 (defun gnus-request-scan (group gnus-command-method)
464 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
465 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
466 (let ((gnus-command-method
467 (if group (gnus-find-method-for-group group) gnus-command-method))
468 (gnus-inhibit-demon t)
469 (mail-source-plugged gnus-plugged))
470 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
471 (funcall (gnus-get-function gnus-command-method 'request-scan)
472 (and group (gnus-group-real-name group))
473 (nth 1 gnus-command-method)))))
475 (defsubst gnus-request-update-info (info gnus-command-method)
476 "Request that GNUS-COMMAND-METHOD update INFO."
477 (when (stringp gnus-command-method)
478 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
479 (when (gnus-check-backend-function
480 'request-update-info (car gnus-command-method))
481 (funcall (gnus-get-function gnus-command-method 'request-update-info)
482 (gnus-group-real-name (gnus-info-group info))
483 info (nth 1 gnus-command-method))))
485 (defun gnus-request-expire-articles (articles group &optional force)
486 (let ((gnus-command-method (gnus-find-method-for-group group)))
487 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
488 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
491 (defun gnus-request-move-article
492 (article group server accept-function &optional last)
493 (let ((gnus-command-method (gnus-find-method-for-group group)))
494 (funcall (gnus-get-function gnus-command-method 'request-move-article)
495 article (gnus-group-real-name group)
496 (nth 1 gnus-command-method) accept-function last)))
498 (defun gnus-request-accept-article (group &optional gnus-command-method last
500 ;; Make sure there's a newline at the end of the article.
501 (when (stringp gnus-command-method)
502 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
503 (when (and (not gnus-command-method)
505 (setq gnus-command-method (gnus-group-name-to-method group)))
506 (goto-char (point-max))
510 (let ((message-options message-options))
511 (message-options-set-recipient)
513 (message-narrow-to-head)
514 (let ((mail-parse-charset message-default-charset))
515 (mail-encode-encoded-word-buffer)))
516 (message-encode-message-body)))
517 (let ((gnus-command-method (or gnus-command-method
518 (gnus-find-method-for-group group))))
519 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
520 (if (stringp group) (gnus-group-real-name group) group)
521 (cadr gnus-command-method)
524 (defun gnus-request-replace-article (article group buffer &optional no-encode)
526 (let ((message-options message-options))
527 (message-options-set-recipient)
529 (message-narrow-to-head)
530 (let ((mail-parse-charset message-default-charset))
531 (mail-encode-encoded-word-buffer)))
532 (message-encode-message-body)))
533 (let ((func (car (gnus-group-name-to-method group))))
534 (funcall (intern (format "%s-request-replace-article" func))
535 article (gnus-group-real-name group) buffer)))
537 (defun gnus-request-associate-buffer (group)
538 (let ((gnus-command-method (gnus-find-method-for-group group)))
539 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
540 (gnus-group-real-name group))))
542 (defun gnus-request-restore-buffer (article group)
543 "Request a new buffer restored to the state of ARTICLE."
544 (let ((gnus-command-method (gnus-find-method-for-group group)))
545 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
546 article (gnus-group-real-name group)
547 (nth 1 gnus-command-method))))
549 (defun gnus-request-create-group (group &optional gnus-command-method args)
550 (when (stringp gnus-command-method)
551 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
552 (let ((gnus-command-method
553 (or gnus-command-method (gnus-find-method-for-group group))))
554 (funcall (gnus-get-function gnus-command-method 'request-create-group)
555 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
557 (defun gnus-request-delete-group (group &optional force)
558 (let ((gnus-command-method (gnus-find-method-for-group group)))
559 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
560 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
562 (defun gnus-request-rename-group (group new-name)
563 (let ((gnus-command-method (gnus-find-method-for-group group)))
564 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
565 (gnus-group-real-name group)
566 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
568 (defun gnus-close-backends ()
569 ;; Send a close request to all backends that support such a request.
570 (let ((methods gnus-valid-select-methods)
571 (gnus-inhibit-demon t)
572 func gnus-command-method)
573 (while (setq gnus-command-method (pop methods))
574 (when (fboundp (setq func (intern
575 (concat (car gnus-command-method)
579 (defun gnus-asynchronous-p (gnus-command-method)
580 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
584 (defun gnus-remove-denial (gnus-command-method)
585 (when (stringp gnus-command-method)
586 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
587 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
588 (status (cadr elem)))
589 ;; If this hasn't been opened before, we add it to the list.
590 (when (eq status 'denied)
591 ;; Set the status of this server.
592 (setcar (cdr elem) 'closed))))
596 ;;; gnus-int.el ends here