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))
37 (defun gnus-agent-expire (&optional a b c)))
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 ;; gnus-open-server-hook might have opened it
112 (gnus-server-opened gnus-select-method)
113 (gnus-open-server gnus-select-method)
117 "%s (%s) open error: '%s'. Continue? "
118 (car gnus-select-method) (cadr gnus-select-method)
119 (gnus-status-message gnus-select-method)))
120 (gnus-error 1 "Couldn't open server on %s"
121 (nth 1 gnus-select-method))))))
123 (defun gnus-check-group (group)
124 "Try to make sure that the server where GROUP exists is alive."
125 (let ((method (gnus-find-method-for-group group)))
126 (or (gnus-server-opened method)
127 (gnus-open-server method))))
129 (defun gnus-check-server (&optional method silent)
130 "Check whether the connection to METHOD is down.
131 If METHOD is nil, use `gnus-select-method'.
132 If it is down, start it up (again)."
133 (let ((method (or method gnus-select-method))
135 ;; Transform virtual server names into select methods.
136 (when (stringp method)
137 (setq method (gnus-server-to-method method)))
138 (if (gnus-server-opened method)
139 ;; The stream is already opened.
143 (gnus-message 5 "Opening %s server%s..." (car method)
144 (if (equal (nth 1 method) "") ""
145 (format " on %s" (nth 1 method)))))
146 (gnus-run-hooks 'gnus-open-server-hook)
149 (setq result (gnus-open-server method))
150 (quit (message "Quit gnus-check-server")
153 (gnus-message 5 "Opening %s server%s...%s" (car method)
154 (if (equal (nth 1 method) "") ""
155 (format " on %s" (nth 1 method)))
156 (if result "done" "failed")))))))
158 (defun gnus-get-function (method function &optional noerror)
159 "Return a function symbol based on METHOD and FUNCTION."
160 ;; Translate server names into methods.
162 (error "Attempted use of a nil select method"))
163 (when (stringp method)
164 (setq method (gnus-server-to-method method)))
165 ;; Check cache of constructed names.
166 (let* ((method-sym (if gnus-agent
167 (gnus-agent-get-function method)
169 (method-fns (get method-sym 'gnus-method-functions))
170 (func (let ((method-fnlist-elt (assq function method-fns)))
171 (unless method-fnlist-elt
172 (setq method-fnlist-elt
174 (intern (format "%s-%s" method-sym function))))
175 (put method-sym 'gnus-method-functions
176 (cons method-fnlist-elt method-fns)))
177 (cdr method-fnlist-elt))))
178 ;; Maybe complain if there is no function.
179 (unless (fboundp func)
181 (error "Trying to require a method that doesn't exist"))
182 (require (car method))
183 (when (not (fboundp func))
186 (error "No such function: %s" func))))
191 ;;; Interface functions to the backends.
194 (defun gnus-open-server (gnus-command-method)
195 "Open a connection to GNUS-COMMAND-METHOD."
196 (when (stringp gnus-command-method)
197 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
198 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
199 ;; If this method was previously denied, we just return nil.
200 (if (eq (nth 1 elem) 'denied)
202 (gnus-message 1 "Denied server")
207 (funcall (gnus-get-function gnus-command-method 'open-server)
208 (nth 1 gnus-command-method)
209 (nthcdr 2 gnus-command-method))
212 (message "Quit trying to open server")
214 ;; If this hasn't been opened before, we add it to the list.
216 (setq elem (list gnus-command-method nil)
217 gnus-opened-servers (cons elem gnus-opened-servers)))
218 ;; Set the status of this server.
221 (if (eq (cadr elem) 'offline)
225 (not (eq (cadr elem) 'offline))
226 (gnus-agent-method-p gnus-command-method))
227 (or gnus-server-unopen-status
229 (format "Unable to open %s:%s, go offline? "
230 (car gnus-command-method)
231 (cadr gnus-command-method)))
235 ;; Return the result from the "open" call.
236 (cond ((eq (cadr elem) 'offline)
237 ;; I'm avoiding infinite recursion by binding unopen
238 ;; status to denied (The logic of this routine
239 ;; guarantees that I can't get to this point with
240 ;; unopen status already bound to denied).
241 (unless (eq gnus-server-unopen-status 'denied)
242 (let ((gnus-server-unopen-status 'denied))
243 (gnus-open-server gnus-command-method)))
248 (defun gnus-close-server (gnus-command-method)
249 "Close the connection to GNUS-COMMAND-METHOD."
250 (when (stringp gnus-command-method)
251 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
252 (funcall (gnus-get-function gnus-command-method 'close-server)
253 (nth 1 gnus-command-method)))
255 (defun gnus-request-list (gnus-command-method)
256 "Request the active file from GNUS-COMMAND-METHOD."
257 (when (stringp gnus-command-method)
258 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
259 (funcall (gnus-get-function gnus-command-method 'request-list)
260 (nth 1 gnus-command-method)))
262 (defun gnus-request-list-newsgroups (gnus-command-method)
263 "Request the newsgroups file from GNUS-COMMAND-METHOD."
264 (when (stringp gnus-command-method)
265 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
266 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
267 (nth 1 gnus-command-method)))
269 (defun gnus-request-newgroups (date gnus-command-method)
270 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
271 (when (stringp gnus-command-method)
272 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
273 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
275 (funcall func date (nth 1 gnus-command-method)))))
277 (defun gnus-server-opened (gnus-command-method)
278 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
279 (unless (eq (gnus-server-status gnus-command-method)
281 (when (stringp gnus-command-method)
282 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
283 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
284 (nth 1 gnus-command-method))))
286 (defun gnus-status-message (gnus-command-method)
287 "Return the status message from GNUS-COMMAND-METHOD.
288 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
289 this group uses will be queried."
290 (let ((gnus-command-method
291 (if (stringp gnus-command-method)
292 (gnus-find-method-for-group gnus-command-method)
293 gnus-command-method)))
294 (funcall (gnus-get-function gnus-command-method 'status-message)
295 (nth 1 gnus-command-method))))
297 (defun gnus-request-regenerate (gnus-command-method)
298 "Request a data generation from GNUS-COMMAND-METHOD."
299 (when (stringp gnus-command-method)
300 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
301 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
302 (nth 1 gnus-command-method)))
304 (defun gnus-request-group (group &optional dont-check gnus-command-method)
305 "Request GROUP. If DONT-CHECK, no information is required."
306 (let ((gnus-command-method
307 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
308 (when (stringp gnus-command-method)
309 (setq gnus-command-method
310 (inline (gnus-server-to-method gnus-command-method))))
311 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
312 (gnus-group-real-name group) (nth 1 gnus-command-method)
315 (defun gnus-list-active-group (group)
316 "Request active information on GROUP."
317 (let ((gnus-command-method (gnus-find-method-for-group group))
318 (func 'list-active-group))
319 (when (gnus-check-backend-function func group)
320 (funcall (gnus-get-function gnus-command-method func)
321 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
323 (defun gnus-request-group-description (group)
324 "Request a description of GROUP."
325 (let ((gnus-command-method (gnus-find-method-for-group group))
326 (func 'request-group-description))
327 (when (gnus-check-backend-function func group)
328 (funcall (gnus-get-function gnus-command-method func)
329 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
331 (defun gnus-request-group-articles (group)
332 "Request a list of existing articles in GROUP."
333 (let ((gnus-command-method (gnus-find-method-for-group group))
334 (func 'request-group-articles))
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-close-group (group)
340 "Request the GROUP be closed."
341 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
342 (funcall (gnus-get-function gnus-command-method 'close-group)
343 (gnus-group-real-name group) (nth 1 gnus-command-method))))
345 (defun gnus-retrieve-headers (articles group &optional fetch-old)
346 "Request headers for ARTICLES in GROUP.
347 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
348 (let ((gnus-command-method (gnus-find-method-for-group group)))
350 ((and gnus-use-cache (numberp (car articles)))
351 (gnus-cache-retrieve-headers articles group fetch-old))
352 ((and gnus-agent (gnus-online gnus-command-method)
353 (gnus-agent-method-p gnus-command-method))
354 (gnus-agent-retrieve-headers articles group fetch-old))
356 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
357 articles (gnus-group-real-name group)
358 (nth 1 gnus-command-method) fetch-old)))))
360 (defun gnus-retrieve-articles (articles group)
361 "Request ARTICLES in GROUP."
362 (let ((gnus-command-method (gnus-find-method-for-group group)))
363 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
364 articles (gnus-group-real-name group)
365 (nth 1 gnus-command-method))))
367 (defun gnus-retrieve-groups (groups gnus-command-method)
368 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
369 (when (stringp gnus-command-method)
370 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
371 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
372 groups (nth 1 gnus-command-method)))
374 (defun gnus-request-type (group &optional article)
375 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
376 (let ((gnus-command-method (gnus-find-method-for-group group)))
377 (if (not (gnus-check-backend-function
378 'request-type (car gnus-command-method)))
380 (funcall (gnus-get-function gnus-command-method 'request-type)
381 (gnus-group-real-name group) article))))
383 (defun gnus-request-set-mark (group action)
384 "Set marks on articles in the backend."
385 (let ((gnus-command-method (gnus-find-method-for-group group)))
386 (if (not (gnus-check-backend-function
387 'request-set-mark (car gnus-command-method)))
389 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
390 (gnus-group-real-name group) action
391 (nth 1 gnus-command-method)))))
393 (defun gnus-request-update-mark (group article mark)
394 "Allow the backend to change the mark the user tries to put on an article."
395 (let ((gnus-command-method (gnus-find-method-for-group group)))
396 (if (not (gnus-check-backend-function
397 'request-update-mark (car gnus-command-method)))
399 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
400 (gnus-group-real-name group) article mark))))
402 (defun gnus-request-article (article group &optional buffer)
403 "Request the ARTICLE in GROUP.
404 ARTICLE can either be an article number or an article Message-ID.
405 If BUFFER, insert the article in that group."
406 (let ((gnus-command-method (gnus-find-method-for-group group)))
407 (funcall (gnus-get-function gnus-command-method 'request-article)
408 article (gnus-group-real-name group)
409 (nth 1 gnus-command-method) buffer)))
411 (defun gnus-request-head (article group)
412 "Request the head of ARTICLE in GROUP."
413 (let* ((gnus-command-method (gnus-find-method-for-group group))
414 (head (gnus-get-function gnus-command-method 'request-head t))
420 (gnus-cache-request-article article group))
421 (setq res (cons group article)
423 ;; Check the agent cache.
424 ((gnus-agent-request-article article group)
425 (setq res (cons group article)
427 ;; Use `head' function.
429 (setq res (funcall head article (gnus-group-real-name group)
430 (nth 1 gnus-command-method))))
431 ;; Use `article' function.
433 (setq res (gnus-request-article article group)
437 (set-buffer nntp-server-buffer)
438 (goto-char (point-min))
439 (when (search-forward "\n\n" nil t)
440 (delete-region (1- (point)) (point-max)))
441 (nnheader-fold-continuation-lines)))
444 (defun gnus-request-body (article group)
445 "Request the body of ARTICLE in GROUP."
446 (let* ((gnus-command-method (gnus-find-method-for-group group))
447 (head (gnus-get-function gnus-command-method 'request-body t))
453 (gnus-cache-request-article article group))
454 (setq res (cons group article)
456 ;; Check the agent cache.
457 ((gnus-agent-request-article article group)
458 (setq res (cons group article)
460 ;; Use `head' function.
462 (setq res (funcall head article (gnus-group-real-name group)
463 (nth 1 gnus-command-method))))
464 ;; Use `article' function.
466 (setq res (gnus-request-article article group)
470 (set-buffer nntp-server-buffer)
471 (goto-char (point-min))
472 (when (search-forward "\n\n" nil t)
473 (delete-region (point-min) (1- (point))))))
476 (defun gnus-request-post (gnus-command-method)
477 "Post the current buffer using GNUS-COMMAND-METHOD."
478 (when (stringp gnus-command-method)
479 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
480 (funcall (gnus-get-function gnus-command-method 'request-post)
481 (nth 1 gnus-command-method)))
483 (defun gnus-request-scan (group gnus-command-method)
484 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
485 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
486 (let ((gnus-command-method
487 (if group (gnus-find-method-for-group group) gnus-command-method))
488 (gnus-inhibit-demon t)
489 (mail-source-plugged gnus-plugged))
490 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
492 (setq gnus-internal-registry-spool-current-method gnus-command-method)
493 (funcall (gnus-get-function gnus-command-method 'request-scan)
494 (and group (gnus-group-real-name group))
495 (nth 1 gnus-command-method))))))
497 (defsubst gnus-request-update-info (info gnus-command-method)
498 "Request that GNUS-COMMAND-METHOD update INFO."
499 (when (stringp gnus-command-method)
500 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
501 (when (gnus-check-backend-function
502 'request-update-info (car gnus-command-method))
503 (let ((group (gnus-info-group info)))
504 (and (funcall (gnus-get-function gnus-command-method
505 'request-update-info)
506 (gnus-group-real-name group)
507 info (nth 1 gnus-command-method))
508 ;; If the minimum article number is greater than 1, then all
509 ;; smaller article numbers are known not to exist; we'll
510 ;; artificially add those to the 'read range.
511 (let* ((active (gnus-active group))
514 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
515 (read (gnus-info-read info))
516 (new-read (gnus-range-add read (list range))))
517 (gnus-info-set-read info new-read)))
520 (defun gnus-request-expire-articles (articles group &optional force)
521 (let* ((gnus-command-method (gnus-find-method-for-group group))
524 (gnus-get-function gnus-command-method 'request-expire-articles)
525 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
527 (when (and gnus-agent
528 (gnus-agent-method-p gnus-command-method))
529 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
530 (when expired-articles
531 (gnus-agent-expire expired-articles group 'force))))
534 (defun gnus-request-move-article (article group server accept-function
536 (let* ((gnus-command-method (gnus-find-method-for-group group))
537 (result (funcall (gnus-get-function gnus-command-method
538 'request-move-article)
539 article (gnus-group-real-name group)
540 (nth 1 gnus-command-method) accept-function last)))
541 (when (and result gnus-agent
542 (gnus-agent-method-p gnus-command-method))
543 (gnus-agent-expire (list article) group 'force))
546 (defun gnus-request-accept-article (group &optional gnus-command-method last
548 ;; Make sure there's a newline at the end of the article.
549 (when (stringp gnus-command-method)
550 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
551 (when (and (not gnus-command-method)
553 (setq gnus-command-method (gnus-group-name-to-method group)))
554 (goto-char (point-max))
557 (let ((gnus-command-method (or gnus-command-method
558 (gnus-find-method-for-group group))))
559 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
560 (if (stringp group) (gnus-group-real-name group) group)
561 (cadr gnus-command-method)
564 (defun gnus-request-replace-article (article group buffer &optional no-encode)
565 (let ((func (car (gnus-group-name-to-method group))))
566 (funcall (intern (format "%s-request-replace-article" func))
567 article (gnus-group-real-name group) buffer)))
569 (defun gnus-request-associate-buffer (group)
570 (let ((gnus-command-method (gnus-find-method-for-group group)))
571 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
572 (gnus-group-real-name group))))
574 (defun gnus-request-restore-buffer (article group)
575 "Request a new buffer restored to the state of ARTICLE."
576 (let ((gnus-command-method (gnus-find-method-for-group group)))
577 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
578 article (gnus-group-real-name group)
579 (nth 1 gnus-command-method))))
581 (defun gnus-request-create-group (group &optional gnus-command-method args)
582 (when (stringp gnus-command-method)
583 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
584 (let ((gnus-command-method
585 (or gnus-command-method (gnus-find-method-for-group group))))
586 (funcall (gnus-get-function gnus-command-method 'request-create-group)
587 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
589 (defun gnus-request-delete-group (group &optional force)
590 (let ((gnus-command-method (gnus-find-method-for-group group)))
591 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
592 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
594 (defun gnus-request-rename-group (group new-name)
595 (let ((gnus-command-method (gnus-find-method-for-group group)))
596 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
597 (gnus-group-real-name group)
598 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
600 (defun gnus-close-backends ()
601 ;; Send a close request to all backends that support such a request.
602 (let ((methods gnus-valid-select-methods)
603 (gnus-inhibit-demon t)
604 func gnus-command-method)
605 (while (setq gnus-command-method (pop methods))
606 (when (fboundp (setq func (intern
607 (concat (car gnus-command-method)
611 (defun gnus-asynchronous-p (gnus-command-method)
612 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
616 (defun gnus-remove-denial (gnus-command-method)
617 (when (stringp gnus-command-method)
618 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
619 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
620 (status (cadr elem)))
621 ;; If this hasn't been opened before, we add it to the list.
622 (when (eq status 'denied)
623 ;; Set the status of this server.
624 (setcar (cdr elem) 'closed))))
628 ;;; gnus-int.el ends here