1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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))
35 (autoload 'gnus-agent-expire "gnus-agent")
36 (autoload 'gnus-agent-read-servers-validate-native "gnus-agent")
38 (defcustom gnus-open-server-hook nil
39 "Hook called just before opening connection to the news server."
43 (defcustom gnus-server-unopen-status nil
44 "The default status if the server is not able to open.
45 If the server is covered by Gnus agent, the possible values are
46 `denied', set the server denied; `offline', set the server offline;
47 nil, ask user. If the server is not covered by Gnus agent, set the
50 :type '(choice (const :tag "Ask" nil)
51 (const :tag "Deny server" denied)
52 (const :tag "Unplug Agent" offline)))
54 (defvar gnus-internal-registry-spool-current-method nil
55 "The current method, for the registry.")
58 ;;; Server Communication
61 (defun gnus-start-news-server (&optional confirm)
62 "Open a method for getting news.
63 If CONFIRM is non-nil, the user will be asked for an NNTP server."
65 (if gnus-current-select-method
66 ;; Stream is already opened.
69 (unless gnus-nntp-service
70 (setq gnus-nntp-server nil))
72 ;; Read server name with completion.
73 (setq gnus-nntp-server
74 (completing-read "NNTP server: "
75 (mapcar (lambda (server) (list server))
76 (cons (list gnus-nntp-server)
77 gnus-secondary-servers))
78 nil nil gnus-nntp-server)))
80 (when (and gnus-nntp-server
81 (stringp gnus-nntp-server)
82 (not (string= gnus-nntp-server "")))
83 (setq gnus-select-method
84 (cond ((or (string= gnus-nntp-server "")
85 (string= gnus-nntp-server "::"))
86 (list 'nnspool (system-name)))
87 ((string-match "^:" gnus-nntp-server)
88 (list 'nnmh gnus-nntp-server
90 (file-name-as-directory
92 (substring gnus-nntp-server 1) "~/")))
93 (list 'nnmh-get-new-mail nil)))
95 (list 'nntp gnus-nntp-server)))))
97 (setq how (car gnus-select-method))
101 (gnus-message 5 "Looking up local news spool..."))
104 (gnus-message 5 "Looking up mh spool..."))
107 (setq gnus-current-select-method gnus-select-method)
108 (gnus-run-hooks 'gnus-open-server-hook)
110 ;; Partially validate agent covered methods now that the
111 ;; gnus-select-method is known.
114 ;; NOTE: This is here for one purpose only. By validating
115 ;; the current select method, it converts the old 5.10.3,
116 ;; and earlier, format to the current format. That enables
117 ;; the agent code within gnus-open-server to function
119 (gnus-agent-read-servers-validate-native gnus-select-method))
122 ;; gnus-open-server-hook might have opened it
123 (gnus-server-opened gnus-select-method)
124 (gnus-open-server gnus-select-method)
128 "%s (%s) open error: '%s'. Continue? "
129 (car gnus-select-method) (cadr gnus-select-method)
130 (gnus-status-message gnus-select-method)))
131 (gnus-error 1 "Couldn't open server on %s"
132 (nth 1 gnus-select-method))))))
134 (defun gnus-check-group (group)
135 "Try to make sure that the server where GROUP exists is alive."
136 (let ((method (gnus-find-method-for-group group)))
137 (or (gnus-server-opened method)
138 (gnus-open-server method))))
140 (defun gnus-check-server (&optional method silent)
141 "Check whether the connection to METHOD is down.
142 If METHOD is nil, use `gnus-select-method'.
143 If it is down, start it up (again)."
144 (let ((method (or method gnus-select-method))
146 ;; Transform virtual server names into select methods.
147 (when (stringp method)
148 (setq method (gnus-server-to-method method)))
149 (if (gnus-server-opened method)
150 ;; The stream is already opened.
154 (gnus-message 5 "Opening %s server%s..." (car method)
155 (if (equal (nth 1 method) "") ""
156 (format " on %s" (nth 1 method)))))
157 (gnus-run-hooks 'gnus-open-server-hook)
160 (setq result (gnus-open-server method))
161 (quit (message "Quit gnus-check-server")
164 (gnus-message 5 "Opening %s server%s...%s" (car method)
165 (if (equal (nth 1 method) "") ""
166 (format " on %s" (nth 1 method)))
167 (if result "done" "failed")))))))
169 (defun gnus-get-function (method function &optional noerror)
170 "Return a function symbol based on METHOD and FUNCTION."
171 ;; Translate server names into methods.
173 (error "Attempted use of a nil select method"))
174 (when (stringp method)
175 (setq method (gnus-server-to-method method)))
176 ;; Check cache of constructed names.
177 (let* ((method-sym (if gnus-agent
178 (gnus-agent-get-function method)
180 (method-fns (get method-sym 'gnus-method-functions))
181 (func (let ((method-fnlist-elt (assq function method-fns)))
182 (unless method-fnlist-elt
183 (setq method-fnlist-elt
185 (intern (format "%s-%s" method-sym function))))
186 (put method-sym 'gnus-method-functions
187 (cons method-fnlist-elt method-fns)))
188 (cdr method-fnlist-elt))))
189 ;; Maybe complain if there is no function.
190 (unless (fboundp func)
192 (error "Trying to require a method that doesn't exist"))
193 (require (car method))
194 (when (not (fboundp func))
197 (error "No such function: %s" func))))
202 ;;; Interface functions to the backends.
205 (defun gnus-open-server (gnus-command-method)
206 "Open a connection to GNUS-COMMAND-METHOD."
207 (when (stringp gnus-command-method)
208 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
209 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
210 ;; If this method was previously denied, we just return nil.
211 (if (eq (nth 1 elem) 'denied)
213 (gnus-message 1 "Denied server")
218 (funcall (gnus-get-function gnus-command-method 'open-server)
219 (nth 1 gnus-command-method)
220 (nthcdr 2 gnus-command-method))
222 (gnus-message 1 (format
223 "Unable to open server due to: %s"
224 (error-message-string err)))
227 (gnus-message 1 "Quit trying to open server")
229 ;; If this hasn't been opened before, we add it to the list.
231 (setq elem (list gnus-command-method nil)
232 gnus-opened-servers (cons elem gnus-opened-servers)))
233 ;; Set the status of this server.
236 (if (eq (cadr elem) 'offline)
240 (not (eq (cadr elem) 'offline))
241 (gnus-agent-method-p gnus-command-method))
242 (or gnus-server-unopen-status
244 (format "Unable to open %s:%s, go offline? "
245 (car gnus-command-method)
246 (cadr gnus-command-method)))
250 ;; Return the result from the "open" call.
251 (cond ((eq (cadr elem) 'offline)
252 ;; I'm avoiding infinite recursion by binding unopen
253 ;; status to denied (The logic of this routine
254 ;; guarantees that I can't get to this point with
255 ;; unopen status already bound to denied).
256 (unless (eq gnus-server-unopen-status 'denied)
257 (let ((gnus-server-unopen-status 'denied))
258 (gnus-open-server gnus-command-method)))
263 (defun gnus-close-server (gnus-command-method)
264 "Close the connection to GNUS-COMMAND-METHOD."
265 (when (stringp gnus-command-method)
266 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
267 (funcall (gnus-get-function gnus-command-method 'close-server)
268 (nth 1 gnus-command-method)))
270 (defun gnus-request-list (gnus-command-method)
271 "Request the active file from GNUS-COMMAND-METHOD."
272 (when (stringp gnus-command-method)
273 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
274 (funcall (gnus-get-function gnus-command-method 'request-list)
275 (nth 1 gnus-command-method)))
277 (defun gnus-request-list-newsgroups (gnus-command-method)
278 "Request the newsgroups file from GNUS-COMMAND-METHOD."
279 (when (stringp gnus-command-method)
280 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
281 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
282 (nth 1 gnus-command-method)))
284 (defun gnus-request-newgroups (date gnus-command-method)
285 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
286 (when (stringp gnus-command-method)
287 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
288 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
290 (funcall func date (nth 1 gnus-command-method)))))
292 (defun gnus-server-opened (gnus-command-method)
293 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
294 (unless (eq (gnus-server-status gnus-command-method)
296 (when (stringp gnus-command-method)
297 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
298 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
299 (nth 1 gnus-command-method))))
301 (defun gnus-status-message (gnus-command-method)
302 "Return the status message from GNUS-COMMAND-METHOD.
303 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
304 name. The method this group uses will be queried."
305 (let ((gnus-command-method
306 (if (stringp gnus-command-method)
307 (gnus-find-method-for-group gnus-command-method)
308 gnus-command-method)))
309 (funcall (gnus-get-function gnus-command-method 'status-message)
310 (nth 1 gnus-command-method))))
312 (defun gnus-request-regenerate (gnus-command-method)
313 "Request a data generation from GNUS-COMMAND-METHOD."
314 (when (stringp gnus-command-method)
315 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
316 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
317 (nth 1 gnus-command-method)))
319 (defun gnus-request-group (group &optional dont-check gnus-command-method)
320 "Request GROUP. If DONT-CHECK, no information is required."
321 (let ((gnus-command-method
322 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
323 (when (stringp gnus-command-method)
324 (setq gnus-command-method
325 (inline (gnus-server-to-method gnus-command-method))))
326 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
327 (gnus-group-real-name group) (nth 1 gnus-command-method)
330 (defun gnus-list-active-group (group)
331 "Request active information on GROUP."
332 (let ((gnus-command-method (gnus-find-method-for-group group))
333 (func 'list-active-group))
334 (when (gnus-check-backend-function func group)
335 (funcall (gnus-get-function gnus-command-method func)
336 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
338 (defun gnus-request-group-description (group)
339 "Request a description of GROUP."
340 (let ((gnus-command-method (gnus-find-method-for-group group))
341 (func 'request-group-description))
342 (when (gnus-check-backend-function func group)
343 (funcall (gnus-get-function gnus-command-method func)
344 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
346 (defun gnus-request-group-articles (group)
347 "Request a list of existing articles in GROUP."
348 (let ((gnus-command-method (gnus-find-method-for-group group))
349 (func 'request-group-articles))
350 (when (gnus-check-backend-function func group)
351 (funcall (gnus-get-function gnus-command-method func)
352 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
354 (defun gnus-close-group (group)
355 "Request the GROUP be closed."
356 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
357 (funcall (gnus-get-function gnus-command-method 'close-group)
358 (gnus-group-real-name group) (nth 1 gnus-command-method))))
360 (defun gnus-retrieve-headers (articles group &optional fetch-old)
361 "Request headers for ARTICLES in GROUP.
362 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
363 (let ((gnus-command-method (gnus-find-method-for-group group)))
365 ((and gnus-use-cache (numberp (car articles)))
366 (gnus-cache-retrieve-headers articles group fetch-old))
367 ((and gnus-agent (gnus-online gnus-command-method)
368 (gnus-agent-method-p gnus-command-method))
369 (gnus-agent-retrieve-headers articles group fetch-old))
371 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
372 articles (gnus-group-real-name group)
373 (nth 1 gnus-command-method) fetch-old)))))
375 (defun gnus-retrieve-articles (articles group)
376 "Request ARTICLES in GROUP."
377 (let ((gnus-command-method (gnus-find-method-for-group group)))
378 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
379 articles (gnus-group-real-name group)
380 (nth 1 gnus-command-method))))
382 (defun gnus-retrieve-groups (groups gnus-command-method)
383 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
384 (when (stringp gnus-command-method)
385 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
386 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
387 groups (nth 1 gnus-command-method)))
389 (defun gnus-request-type (group &optional article)
390 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
391 (let ((gnus-command-method (gnus-find-method-for-group group)))
392 (if (not (gnus-check-backend-function
393 'request-type (car gnus-command-method)))
395 (funcall (gnus-get-function gnus-command-method 'request-type)
396 (gnus-group-real-name group) article))))
398 (defun gnus-request-set-mark (group action)
399 "Set marks on articles in the back end."
400 (let ((gnus-command-method (gnus-find-method-for-group group)))
401 (if (not (gnus-check-backend-function
402 'request-set-mark (car gnus-command-method)))
404 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
405 (gnus-group-real-name group) action
406 (nth 1 gnus-command-method)))))
408 (defun gnus-request-update-mark (group article mark)
409 "Allow the back end to change the mark the user tries to put on an article."
410 (let ((gnus-command-method (gnus-find-method-for-group group)))
411 (if (not (gnus-check-backend-function
412 'request-update-mark (car gnus-command-method)))
414 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
415 (gnus-group-real-name group) article mark))))
417 (defun gnus-request-article (article group &optional buffer)
418 "Request the ARTICLE in GROUP.
419 ARTICLE can either be an article number or an article Message-ID.
420 If BUFFER, insert the article in that group."
421 (let ((gnus-command-method (gnus-find-method-for-group group)))
422 (funcall (gnus-get-function gnus-command-method 'request-article)
423 article (gnus-group-real-name group)
424 (nth 1 gnus-command-method) buffer)))
426 (defun gnus-request-head (article group)
427 "Request the head of ARTICLE in GROUP."
428 (let* ((gnus-command-method (gnus-find-method-for-group group))
429 (head (gnus-get-function gnus-command-method 'request-head t))
435 (gnus-cache-request-article article group))
436 (setq res (cons group article)
438 ;; Check the agent cache.
439 ((gnus-agent-request-article article group)
440 (setq res (cons group article)
442 ;; Use `head' function.
444 (setq res (funcall head article (gnus-group-real-name group)
445 (nth 1 gnus-command-method))))
446 ;; Use `article' function.
448 (setq res (gnus-request-article article group)
452 (set-buffer nntp-server-buffer)
453 (goto-char (point-min))
454 (when (search-forward "\n\n" nil t)
455 (delete-region (1- (point)) (point-max)))
456 (nnheader-fold-continuation-lines)))
459 (defun gnus-request-body (article group)
460 "Request the body of ARTICLE in GROUP."
461 (let* ((gnus-command-method (gnus-find-method-for-group group))
462 (head (gnus-get-function gnus-command-method 'request-body t))
468 (gnus-cache-request-article article group))
469 (setq res (cons group article)
471 ;; Check the agent cache.
472 ((gnus-agent-request-article article group)
473 (setq res (cons group article)
475 ;; Use `head' function.
477 (setq res (funcall head article (gnus-group-real-name group)
478 (nth 1 gnus-command-method))))
479 ;; Use `article' function.
481 (setq res (gnus-request-article article group)
485 (set-buffer nntp-server-buffer)
486 (goto-char (point-min))
487 (when (search-forward "\n\n" nil t)
488 (delete-region (point-min) (1- (point))))))
491 (defun gnus-request-post (gnus-command-method)
492 "Post the current buffer using GNUS-COMMAND-METHOD."
493 (when (stringp gnus-command-method)
494 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
495 (funcall (gnus-get-function gnus-command-method 'request-post)
496 (nth 1 gnus-command-method)))
498 (defun gnus-request-scan (group gnus-command-method)
499 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
500 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
501 (let ((gnus-command-method
502 (if group (gnus-find-method-for-group group) gnus-command-method))
503 (gnus-inhibit-demon t)
504 (mail-source-plugged gnus-plugged))
505 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
507 (setq gnus-internal-registry-spool-current-method gnus-command-method)
508 (funcall (gnus-get-function gnus-command-method 'request-scan)
509 (and group (gnus-group-real-name group))
510 (nth 1 gnus-command-method))))))
512 (defsubst gnus-request-update-info (info gnus-command-method)
513 "Request that GNUS-COMMAND-METHOD update INFO."
514 (when (stringp gnus-command-method)
515 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
516 (when (gnus-check-backend-function
517 'request-update-info (car gnus-command-method))
518 (let ((group (gnus-info-group info)))
519 (and (funcall (gnus-get-function gnus-command-method
520 'request-update-info)
521 (gnus-group-real-name group)
522 info (nth 1 gnus-command-method))
523 ;; If the minimum article number is greater than 1, then all
524 ;; smaller article numbers are known not to exist; we'll
525 ;; artificially add those to the 'read range.
526 (let* ((active (gnus-active group))
529 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
530 (read (gnus-info-read info))
531 (new-read (gnus-range-add read (list range))))
532 (gnus-info-set-read info new-read)))
535 (defun gnus-request-expire-articles (articles group &optional force)
536 (let* ((gnus-command-method (gnus-find-method-for-group group))
539 (gnus-get-function gnus-command-method 'request-expire-articles)
540 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
542 (when (and gnus-agent
543 (gnus-agent-method-p gnus-command-method))
544 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
545 (when expired-articles
546 (gnus-agent-expire expired-articles group 'force))))
549 (defun gnus-request-move-article (article group server accept-function
551 (let* ((gnus-command-method (gnus-find-method-for-group group))
552 (result (funcall (gnus-get-function gnus-command-method
553 'request-move-article)
554 article (gnus-group-real-name group)
555 (nth 1 gnus-command-method) accept-function last)))
556 (when (and result gnus-agent
557 (gnus-agent-method-p gnus-command-method))
558 (gnus-agent-expire (list article) group 'force))
561 (defun gnus-request-accept-article (group &optional gnus-command-method last
563 ;; Make sure there's a newline at the end of the article.
564 (when (stringp gnus-command-method)
565 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
566 (when (and (not gnus-command-method)
568 (setq gnus-command-method (gnus-group-name-to-method group)))
569 (goto-char (point-max))
573 (let ((message-options message-options))
574 (message-options-set-recipient)
576 (message-narrow-to-head)
577 (let ((mail-parse-charset message-default-charset))
578 (mail-encode-encoded-word-buffer)))
579 (message-encode-message-body)))
580 (let ((gnus-command-method (or gnus-command-method
581 (gnus-find-method-for-group group))))
582 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
583 (if (stringp group) (gnus-group-real-name group) group)
584 (cadr gnus-command-method)
587 (defun gnus-request-replace-article (article group buffer &optional no-encode)
589 (let ((message-options message-options))
590 (message-options-set-recipient)
592 (message-narrow-to-head)
593 (let ((mail-parse-charset message-default-charset))
594 (mail-encode-encoded-word-buffer)))
595 (message-encode-message-body)))
596 (let ((func (car (gnus-group-name-to-method group))))
597 (funcall (intern (format "%s-request-replace-article" func))
598 article (gnus-group-real-name group) buffer)))
600 (defun gnus-request-associate-buffer (group)
601 (let ((gnus-command-method (gnus-find-method-for-group group)))
602 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
603 (gnus-group-real-name group))))
605 (defun gnus-request-restore-buffer (article group)
606 "Request a new buffer restored to the state of ARTICLE."
607 (let ((gnus-command-method (gnus-find-method-for-group group)))
608 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
609 article (gnus-group-real-name group)
610 (nth 1 gnus-command-method))))
612 (defun gnus-request-create-group (group &optional gnus-command-method args)
613 (when (stringp gnus-command-method)
614 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
615 (let ((gnus-command-method
616 (or gnus-command-method (gnus-find-method-for-group group))))
617 (funcall (gnus-get-function gnus-command-method 'request-create-group)
618 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
620 (defun gnus-request-delete-group (group &optional force)
621 (let ((gnus-command-method (gnus-find-method-for-group group)))
622 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
623 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
625 (defun gnus-request-rename-group (group new-name)
626 (let ((gnus-command-method (gnus-find-method-for-group group)))
627 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
628 (gnus-group-real-name group)
629 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
631 (defun gnus-close-backends ()
632 ;; Send a close request to all backends that support such a request.
633 (let ((methods gnus-valid-select-methods)
634 (gnus-inhibit-demon t)
635 func gnus-command-method)
636 (while (setq gnus-command-method (pop methods))
637 (when (fboundp (setq func (intern
638 (concat (car gnus-command-method)
642 (defun gnus-asynchronous-p (gnus-command-method)
643 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
647 (defun gnus-remove-denial (gnus-command-method)
648 (when (stringp gnus-command-method)
649 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
650 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
651 (status (cadr elem)))
652 ;; If this hasn't been opened before, we add it to the list.
653 (when (eq status 'denied)
654 ;; Set the status of this server.
655 (setcar (cdr elem) 'closed))))
659 ;;; gnus-int.el ends here