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>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
36 (autoload 'gnus-agent-expire "gnus-agent")
37 (autoload 'gnus-agent-read-servers-validate-native "gnus-agent")
39 (defcustom gnus-open-server-hook nil
40 "Hook called just before opening connection to the news server."
44 (defcustom gnus-server-unopen-status nil
45 "The default status if the server is not able to open.
46 If the server is covered by Gnus agent, the possible values are
47 `denied', set the server denied; `offline', set the server offline;
48 nil, ask user. If the server is not covered by Gnus agent, set the
51 :type '(choice (const :tag "Ask" nil)
52 (const :tag "Deny server" denied)
53 (const :tag "Unplug Agent" offline)))
55 (defvar gnus-internal-registry-spool-current-method nil
56 "The current method, for the registry.")
59 ;;; Server Communication
62 (defun gnus-start-news-server (&optional confirm)
63 "Open a method for getting news.
64 If CONFIRM is non-nil, the user will be asked for an NNTP server."
66 (if gnus-current-select-method
67 ;; Stream is already opened.
70 (unless gnus-nntp-service
71 (setq gnus-nntp-server nil))
73 ;; Read server name with completion.
74 (setq gnus-nntp-server
75 (completing-read "NNTP server: "
76 (mapcar (lambda (server) (list server))
77 (cons (list gnus-nntp-server)
78 gnus-secondary-servers))
79 nil nil gnus-nntp-server)))
81 (when (and gnus-nntp-server
82 (stringp gnus-nntp-server)
83 (not (string= gnus-nntp-server "")))
84 (setq gnus-select-method
85 (cond ((or (string= gnus-nntp-server "")
86 (string= gnus-nntp-server "::"))
87 (list 'nnspool (system-name)))
88 ((string-match "^:" gnus-nntp-server)
89 (list 'nnmh gnus-nntp-server
91 (file-name-as-directory
93 (substring gnus-nntp-server 1) "~/")))
94 (list 'nnmh-get-new-mail nil)))
96 (list 'nntp gnus-nntp-server)))))
98 (setq how (car gnus-select-method))
102 (gnus-message 5 "Looking up local news spool..."))
105 (gnus-message 5 "Looking up mh spool..."))
108 (setq gnus-current-select-method gnus-select-method)
109 (gnus-run-hooks 'gnus-open-server-hook)
111 ;; Partially validate agent covered methods now that the
112 ;; gnus-select-method is known.
115 ;; NOTE: This is here for one purpose only. By validating
116 ;; the current select method, it converts the old 5.10.3,
117 ;; and earlier, format to the current format. That enables
118 ;; the agent code within gnus-open-server to function
120 (gnus-agent-read-servers-validate-native gnus-select-method))
123 ;; gnus-open-server-hook might have opened it
124 (gnus-server-opened gnus-select-method)
125 (gnus-open-server gnus-select-method)
129 "%s (%s) open error: '%s'. Continue? "
130 (car gnus-select-method) (cadr gnus-select-method)
131 (gnus-status-message gnus-select-method)))
132 (gnus-error 1 "Couldn't open server on %s"
133 (nth 1 gnus-select-method))))))
135 (defun gnus-check-group (group)
136 "Try to make sure that the server where GROUP exists is alive."
137 (let ((method (gnus-find-method-for-group group)))
138 (or (gnus-server-opened method)
139 (gnus-open-server method))))
141 (defun gnus-check-server (&optional method silent)
142 "Check whether the connection to METHOD is down.
143 If METHOD is nil, use `gnus-select-method'.
144 If it is down, start it up (again)."
145 (let ((method (or method gnus-select-method))
147 ;; Transform virtual server names into select methods.
148 (when (stringp method)
149 (setq method (gnus-server-to-method method)))
150 (if (gnus-server-opened method)
151 ;; The stream is already opened.
155 (gnus-message 5 "Opening %s server%s..." (car method)
156 (if (equal (nth 1 method) "") ""
157 (format " on %s" (nth 1 method)))))
158 (gnus-run-hooks 'gnus-open-server-hook)
161 (setq result (gnus-open-server method))
162 (quit (message "Quit gnus-check-server")
165 (gnus-message 5 "Opening %s server%s...%s" (car method)
166 (if (equal (nth 1 method) "") ""
167 (format " on %s" (nth 1 method)))
168 (if result "done" "failed")))))))
170 (defun gnus-get-function (method function &optional noerror)
171 "Return a function symbol based on METHOD and FUNCTION."
172 ;; Translate server names into methods.
174 (error "Attempted use of a nil select method"))
175 (when (stringp method)
176 (setq method (gnus-server-to-method method)))
177 ;; Check cache of constructed names.
178 (let* ((method-sym (if gnus-agent
179 (gnus-agent-get-function method)
181 (method-fns (get method-sym 'gnus-method-functions))
182 (func (let ((method-fnlist-elt (assq function method-fns)))
183 (unless method-fnlist-elt
184 (setq method-fnlist-elt
186 (intern (format "%s-%s" method-sym function))))
187 (put method-sym 'gnus-method-functions
188 (cons method-fnlist-elt method-fns)))
189 (cdr method-fnlist-elt))))
190 ;; Maybe complain if there is no function.
191 (unless (fboundp func)
193 (error "Trying to require a method that doesn't exist"))
194 (require (car method))
195 (when (not (fboundp func))
198 (error "No such function: %s" func))))
203 ;;; Interface functions to the backends.
206 (defun gnus-open-server (gnus-command-method)
207 "Open a connection to GNUS-COMMAND-METHOD."
208 (when (stringp gnus-command-method)
209 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
210 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
211 ;; If this method was previously denied, we just return nil.
212 (if (eq (nth 1 elem) 'denied)
214 (gnus-message 1 "Denied server")
219 (funcall (gnus-get-function gnus-command-method 'open-server)
220 (nth 1 gnus-command-method)
221 (nthcdr 2 gnus-command-method))
223 (gnus-message 1 (format
224 "Unable to open server due to: %s"
225 (error-message-string err)))
228 (gnus-message 1 "Quit trying to open server")
230 ;; If this hasn't been opened before, we add it to the list.
232 (setq elem (list gnus-command-method nil)
233 gnus-opened-servers (cons elem gnus-opened-servers)))
234 ;; Set the status of this server.
237 (if (eq (cadr elem) 'offline)
241 (not (eq (cadr elem) 'offline))
242 (gnus-agent-method-p gnus-command-method))
243 (or gnus-server-unopen-status
245 (format "Unable to open %s:%s, go offline? "
246 (car gnus-command-method)
247 (cadr gnus-command-method)))
251 ;; Return the result from the "open" call.
252 (cond ((eq (cadr elem) 'offline)
253 ;; I'm avoiding infinite recursion by binding unopen
254 ;; status to denied (The logic of this routine
255 ;; guarantees that I can't get to this point with
256 ;; unopen status already bound to denied).
257 (unless (eq gnus-server-unopen-status 'denied)
258 (let ((gnus-server-unopen-status 'denied))
259 (gnus-open-server gnus-command-method)))
264 (defun gnus-close-server (gnus-command-method)
265 "Close the connection to GNUS-COMMAND-METHOD."
266 (when (stringp gnus-command-method)
267 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
268 (funcall (gnus-get-function gnus-command-method 'close-server)
269 (nth 1 gnus-command-method)))
271 (defun gnus-request-list (gnus-command-method)
272 "Request the active file from GNUS-COMMAND-METHOD."
273 (when (stringp gnus-command-method)
274 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
275 (funcall (gnus-get-function gnus-command-method 'request-list)
276 (nth 1 gnus-command-method)))
278 (defun gnus-request-list-newsgroups (gnus-command-method)
279 "Request the newsgroups file from GNUS-COMMAND-METHOD."
280 (when (stringp gnus-command-method)
281 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
282 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
283 (nth 1 gnus-command-method)))
285 (defun gnus-request-newgroups (date gnus-command-method)
286 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
287 (when (stringp gnus-command-method)
288 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
289 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
291 (funcall func date (nth 1 gnus-command-method)))))
293 (defun gnus-server-opened (gnus-command-method)
294 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
295 (unless (eq (gnus-server-status gnus-command-method)
297 (when (stringp gnus-command-method)
298 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
299 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
300 (nth 1 gnus-command-method))))
302 (defun gnus-status-message (gnus-command-method)
303 "Return the status message from GNUS-COMMAND-METHOD.
304 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
305 name. The method this group uses will be queried."
306 (let ((gnus-command-method
307 (if (stringp gnus-command-method)
308 (gnus-find-method-for-group gnus-command-method)
309 gnus-command-method)))
310 (funcall (gnus-get-function gnus-command-method 'status-message)
311 (nth 1 gnus-command-method))))
313 (defun gnus-request-regenerate (gnus-command-method)
314 "Request a data generation from GNUS-COMMAND-METHOD."
315 (when (stringp gnus-command-method)
316 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
317 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
318 (nth 1 gnus-command-method)))
320 (defun gnus-request-group (group &optional dont-check gnus-command-method)
321 "Request GROUP. If DONT-CHECK, no information is required."
322 (let ((gnus-command-method
323 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
324 (when (stringp gnus-command-method)
325 (setq gnus-command-method
326 (inline (gnus-server-to-method gnus-command-method))))
327 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
328 (gnus-group-real-name group) (nth 1 gnus-command-method)
331 (defun gnus-list-active-group (group)
332 "Request active information on GROUP."
333 (let ((gnus-command-method (gnus-find-method-for-group group))
334 (func 'list-active-group))
335 (when (gnus-check-backend-function func group)
336 (funcall (gnus-get-function gnus-command-method func)
337 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
339 (defun gnus-request-group-description (group)
340 "Request a description of GROUP."
341 (let ((gnus-command-method (gnus-find-method-for-group group))
342 (func 'request-group-description))
343 (when (gnus-check-backend-function func group)
344 (funcall (gnus-get-function gnus-command-method func)
345 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
347 (defun gnus-request-group-articles (group)
348 "Request a list of existing articles in GROUP."
349 (let ((gnus-command-method (gnus-find-method-for-group group))
350 (func 'request-group-articles))
351 (when (gnus-check-backend-function func group)
352 (funcall (gnus-get-function gnus-command-method func)
353 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
355 (defun gnus-close-group (group)
356 "Request the GROUP be closed."
357 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
358 (funcall (gnus-get-function gnus-command-method 'close-group)
359 (gnus-group-real-name group) (nth 1 gnus-command-method))))
361 (defun gnus-retrieve-headers (articles group &optional fetch-old)
362 "Request headers for ARTICLES in GROUP.
363 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
364 (let ((gnus-command-method (gnus-find-method-for-group group)))
366 ((and gnus-use-cache (numberp (car articles)))
367 (gnus-cache-retrieve-headers articles group fetch-old))
368 ((and gnus-agent (gnus-online gnus-command-method)
369 (gnus-agent-method-p gnus-command-method))
370 (gnus-agent-retrieve-headers articles group fetch-old))
372 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
373 articles (gnus-group-real-name group)
374 (nth 1 gnus-command-method) fetch-old)))))
376 (defun gnus-retrieve-articles (articles group)
377 "Request ARTICLES in GROUP."
378 (let ((gnus-command-method (gnus-find-method-for-group group)))
379 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
380 articles (gnus-group-real-name group)
381 (nth 1 gnus-command-method))))
383 (defun gnus-retrieve-groups (groups gnus-command-method)
384 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
385 (when (stringp gnus-command-method)
386 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
387 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
388 groups (nth 1 gnus-command-method)))
390 (defun gnus-request-type (group &optional article)
391 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
392 (let ((gnus-command-method (gnus-find-method-for-group group)))
393 (if (not (gnus-check-backend-function
394 'request-type (car gnus-command-method)))
396 (funcall (gnus-get-function gnus-command-method 'request-type)
397 (gnus-group-real-name group) article))))
399 (defun gnus-request-set-mark (group action)
400 "Set marks on articles in the back end."
401 (let ((gnus-command-method (gnus-find-method-for-group group)))
402 (if (not (gnus-check-backend-function
403 'request-set-mark (car gnus-command-method)))
405 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
406 (gnus-group-real-name group) action
407 (nth 1 gnus-command-method)))))
409 (defun gnus-request-update-mark (group article mark)
410 "Allow the back end to change the mark the user tries to put on an article."
411 (let ((gnus-command-method (gnus-find-method-for-group group)))
412 (if (not (gnus-check-backend-function
413 'request-update-mark (car gnus-command-method)))
415 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
416 (gnus-group-real-name group) article mark))))
418 (defun gnus-request-article (article group &optional buffer)
419 "Request the ARTICLE in GROUP.
420 ARTICLE can either be an article number or an article Message-ID.
421 If BUFFER, insert the article in that group."
422 (let ((gnus-command-method (gnus-find-method-for-group group)))
423 (funcall (gnus-get-function gnus-command-method 'request-article)
424 article (gnus-group-real-name group)
425 (nth 1 gnus-command-method) buffer)))
427 (defun gnus-request-head (article group)
428 "Request the head of ARTICLE in GROUP."
429 (let* ((gnus-command-method (gnus-find-method-for-group group))
430 (head (gnus-get-function gnus-command-method 'request-head t))
436 (gnus-cache-request-article article group))
437 (setq res (cons group article)
439 ;; Check the agent cache.
440 ((gnus-agent-request-article article group)
441 (setq res (cons group article)
443 ;; Use `head' function.
445 (setq res (funcall head article (gnus-group-real-name group)
446 (nth 1 gnus-command-method))))
447 ;; Use `article' function.
449 (setq res (gnus-request-article article group)
453 (set-buffer nntp-server-buffer)
454 (goto-char (point-min))
455 (when (search-forward "\n\n" nil t)
456 (delete-region (1- (point)) (point-max)))
457 (nnheader-fold-continuation-lines)))
460 (defun gnus-request-body (article group)
461 "Request the body of ARTICLE in GROUP."
462 (let* ((gnus-command-method (gnus-find-method-for-group group))
463 (head (gnus-get-function gnus-command-method 'request-body t))
469 (gnus-cache-request-article article group))
470 (setq res (cons group article)
472 ;; Check the agent cache.
473 ((gnus-agent-request-article article group)
474 (setq res (cons group article)
476 ;; Use `head' function.
478 (setq res (funcall head article (gnus-group-real-name group)
479 (nth 1 gnus-command-method))))
480 ;; Use `article' function.
482 (setq res (gnus-request-article article group)
486 (set-buffer nntp-server-buffer)
487 (goto-char (point-min))
488 (when (search-forward "\n\n" nil t)
489 (delete-region (point-min) (1- (point))))))
492 (defun gnus-request-post (gnus-command-method)
493 "Post the current buffer using GNUS-COMMAND-METHOD."
494 (when (stringp gnus-command-method)
495 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
496 (funcall (gnus-get-function gnus-command-method 'request-post)
497 (nth 1 gnus-command-method)))
499 (defun gnus-request-scan (group gnus-command-method)
500 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
501 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
502 (let ((gnus-command-method
503 (if group (gnus-find-method-for-group group) gnus-command-method))
504 (gnus-inhibit-demon t)
505 (mail-source-plugged gnus-plugged))
506 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
508 (setq gnus-internal-registry-spool-current-method gnus-command-method)
509 (funcall (gnus-get-function gnus-command-method 'request-scan)
510 (and group (gnus-group-real-name group))
511 (nth 1 gnus-command-method))))))
513 (defsubst gnus-request-update-info (info gnus-command-method)
514 "Request that GNUS-COMMAND-METHOD update INFO."
515 (when (stringp gnus-command-method)
516 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
517 (when (gnus-check-backend-function
518 'request-update-info (car gnus-command-method))
519 (let ((group (gnus-info-group info)))
520 (and (funcall (gnus-get-function gnus-command-method
521 'request-update-info)
522 (gnus-group-real-name group)
523 info (nth 1 gnus-command-method))
524 ;; If the minimum article number is greater than 1, then all
525 ;; smaller article numbers are known not to exist; we'll
526 ;; artificially add those to the 'read range.
527 (let* ((active (gnus-active group))
530 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
531 (read (gnus-info-read info))
532 (new-read (gnus-range-add read (list range))))
533 (gnus-info-set-read info new-read)))
536 (defun gnus-request-expire-articles (articles group &optional force)
537 (let* ((gnus-command-method (gnus-find-method-for-group group))
540 (gnus-get-function gnus-command-method 'request-expire-articles)
541 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
543 (when (and gnus-agent
544 (gnus-agent-method-p gnus-command-method))
545 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
546 (when expired-articles
547 (gnus-agent-expire expired-articles group 'force))))
550 (defun gnus-request-move-article (article group server accept-function
552 (let* ((gnus-command-method (gnus-find-method-for-group group))
553 (result (funcall (gnus-get-function gnus-command-method
554 'request-move-article)
555 article (gnus-group-real-name group)
556 (nth 1 gnus-command-method) accept-function last)))
557 (when (and result gnus-agent
558 (gnus-agent-method-p gnus-command-method))
559 (gnus-agent-expire (list article) group 'force))
562 (defun gnus-request-accept-article (group &optional gnus-command-method last
564 ;; Make sure there's a newline at the end of the article.
565 (when (stringp gnus-command-method)
566 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
567 (when (and (not gnus-command-method)
569 (setq gnus-command-method (gnus-group-name-to-method group)))
570 (goto-char (point-max))
573 (let ((gnus-command-method (or gnus-command-method
574 (gnus-find-method-for-group group))))
575 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
576 (if (stringp group) (gnus-group-real-name group) group)
577 (cadr gnus-command-method)
580 (defun gnus-request-replace-article (article group buffer &optional no-encode)
581 (let ((func (car (gnus-group-name-to-method group))))
582 (funcall (intern (format "%s-request-replace-article" func))
583 article (gnus-group-real-name group) buffer)))
585 (defun gnus-request-associate-buffer (group)
586 (let ((gnus-command-method (gnus-find-method-for-group group)))
587 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
588 (gnus-group-real-name group))))
590 (defun gnus-request-restore-buffer (article group)
591 "Request a new buffer restored to the state of ARTICLE."
592 (let ((gnus-command-method (gnus-find-method-for-group group)))
593 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
594 article (gnus-group-real-name group)
595 (nth 1 gnus-command-method))))
597 (defun gnus-request-create-group (group &optional gnus-command-method args)
598 (when (stringp gnus-command-method)
599 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
600 (let ((gnus-command-method
601 (or gnus-command-method (gnus-find-method-for-group group))))
602 (funcall (gnus-get-function gnus-command-method 'request-create-group)
603 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
605 (defun gnus-request-delete-group (group &optional force)
606 (let ((gnus-command-method (gnus-find-method-for-group group)))
607 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
608 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
610 (defun gnus-request-rename-group (group new-name)
611 (let ((gnus-command-method (gnus-find-method-for-group group)))
612 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
613 (gnus-group-real-name group)
614 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
616 (defun gnus-close-backends ()
617 ;; Send a close request to all backends that support such a request.
618 (let ((methods gnus-valid-select-methods)
619 (gnus-inhibit-demon t)
620 func gnus-command-method)
621 (while (setq gnus-command-method (pop methods))
622 (when (fboundp (setq func (intern
623 (concat (car gnus-command-method)
627 (defun gnus-asynchronous-p (gnus-command-method)
628 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
632 (defun gnus-remove-denial (gnus-command-method)
633 (when (stringp gnus-command-method)
634 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
635 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
636 (status (cadr elem)))
637 ;; If this hasn't been opened before, we add it to the list.
638 (when (eq status 'denied)
639 ;; Set the status of this server.
640 (setcar (cdr elem) 'closed))))
644 ;;; gnus-int.el ends here