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))
211 (gnus-message 1 (format
212 "Unable to open server due to: %s"
213 (error-message-string err)))
216 (gnus-message 1 "Quit trying to open server")
218 ;; If this hasn't been opened before, we add it to the list.
220 (setq elem (list gnus-command-method nil)
221 gnus-opened-servers (cons elem gnus-opened-servers)))
222 ;; Set the status of this server.
225 (if (eq (cadr elem) 'offline)
229 (not (eq (cadr elem) 'offline))
230 (gnus-agent-method-p gnus-command-method))
231 (or gnus-server-unopen-status
233 (format "Unable to open %s:%s, go offline? "
234 (car gnus-command-method)
235 (cadr gnus-command-method)))
239 ;; Return the result from the "open" call.
240 (cond ((eq (cadr elem) 'offline)
241 ;; I'm avoiding infinite recursion by binding unopen
242 ;; status to denied (The logic of this routine
243 ;; guarantees that I can't get to this point with
244 ;; unopen status already bound to denied).
245 (unless (eq gnus-server-unopen-status 'denied)
246 (let ((gnus-server-unopen-status 'denied))
247 (gnus-open-server gnus-command-method)))
252 (defun gnus-close-server (gnus-command-method)
253 "Close the connection to GNUS-COMMAND-METHOD."
254 (when (stringp gnus-command-method)
255 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
256 (funcall (gnus-get-function gnus-command-method 'close-server)
257 (nth 1 gnus-command-method)))
259 (defun gnus-request-list (gnus-command-method)
260 "Request the active file from GNUS-COMMAND-METHOD."
261 (when (stringp gnus-command-method)
262 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
263 (funcall (gnus-get-function gnus-command-method 'request-list)
264 (nth 1 gnus-command-method)))
266 (defun gnus-request-list-newsgroups (gnus-command-method)
267 "Request the newsgroups file from GNUS-COMMAND-METHOD."
268 (when (stringp gnus-command-method)
269 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
270 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
271 (nth 1 gnus-command-method)))
273 (defun gnus-request-newgroups (date gnus-command-method)
274 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
275 (when (stringp gnus-command-method)
276 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
277 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
279 (funcall func date (nth 1 gnus-command-method)))))
281 (defun gnus-server-opened (gnus-command-method)
282 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
283 (unless (eq (gnus-server-status gnus-command-method)
285 (when (stringp gnus-command-method)
286 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
287 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
288 (nth 1 gnus-command-method))))
290 (defun gnus-status-message (gnus-command-method)
291 "Return the status message from GNUS-COMMAND-METHOD.
292 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
293 this group uses will be queried."
294 (let ((gnus-command-method
295 (if (stringp gnus-command-method)
296 (gnus-find-method-for-group gnus-command-method)
297 gnus-command-method)))
298 (funcall (gnus-get-function gnus-command-method 'status-message)
299 (nth 1 gnus-command-method))))
301 (defun gnus-request-regenerate (gnus-command-method)
302 "Request a data generation from GNUS-COMMAND-METHOD."
303 (when (stringp gnus-command-method)
304 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
305 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
306 (nth 1 gnus-command-method)))
308 (defun gnus-request-group (group &optional dont-check gnus-command-method)
309 "Request GROUP. If DONT-CHECK, no information is required."
310 (let ((gnus-command-method
311 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
312 (when (stringp gnus-command-method)
313 (setq gnus-command-method
314 (inline (gnus-server-to-method gnus-command-method))))
315 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
316 (gnus-group-real-name group) (nth 1 gnus-command-method)
319 (defun gnus-list-active-group (group)
320 "Request active information on GROUP."
321 (let ((gnus-command-method (gnus-find-method-for-group group))
322 (func 'list-active-group))
323 (when (gnus-check-backend-function func group)
324 (funcall (gnus-get-function gnus-command-method func)
325 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
327 (defun gnus-request-group-description (group)
328 "Request a description of GROUP."
329 (let ((gnus-command-method (gnus-find-method-for-group group))
330 (func 'request-group-description))
331 (when (gnus-check-backend-function func group)
332 (funcall (gnus-get-function gnus-command-method func)
333 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
335 (defun gnus-request-group-articles (group)
336 "Request a list of existing articles in GROUP."
337 (let ((gnus-command-method (gnus-find-method-for-group group))
338 (func 'request-group-articles))
339 (when (gnus-check-backend-function func group)
340 (funcall (gnus-get-function gnus-command-method func)
341 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
343 (defun gnus-close-group (group)
344 "Request the GROUP be closed."
345 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
346 (funcall (gnus-get-function gnus-command-method 'close-group)
347 (gnus-group-real-name group) (nth 1 gnus-command-method))))
349 (defun gnus-retrieve-headers (articles group &optional fetch-old)
350 "Request headers for ARTICLES in GROUP.
351 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
352 (let ((gnus-command-method (gnus-find-method-for-group group)))
354 ((and gnus-use-cache (numberp (car articles)))
355 (gnus-cache-retrieve-headers articles group fetch-old))
356 ((and gnus-agent (gnus-online gnus-command-method)
357 (gnus-agent-method-p gnus-command-method))
358 (gnus-agent-retrieve-headers articles group fetch-old))
360 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
361 articles (gnus-group-real-name group)
362 (nth 1 gnus-command-method) fetch-old)))))
364 (defun gnus-retrieve-articles (articles group)
365 "Request ARTICLES in GROUP."
366 (let ((gnus-command-method (gnus-find-method-for-group group)))
367 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
368 articles (gnus-group-real-name group)
369 (nth 1 gnus-command-method))))
371 (defun gnus-retrieve-groups (groups gnus-command-method)
372 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
373 (when (stringp gnus-command-method)
374 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
375 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
376 groups (nth 1 gnus-command-method)))
378 (defun gnus-request-type (group &optional article)
379 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
380 (let ((gnus-command-method (gnus-find-method-for-group group)))
381 (if (not (gnus-check-backend-function
382 'request-type (car gnus-command-method)))
384 (funcall (gnus-get-function gnus-command-method 'request-type)
385 (gnus-group-real-name group) article))))
387 (defun gnus-request-set-mark (group action)
388 "Set marks on articles in the backend."
389 (let ((gnus-command-method (gnus-find-method-for-group group)))
390 (if (not (gnus-check-backend-function
391 'request-set-mark (car gnus-command-method)))
393 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
394 (gnus-group-real-name group) action
395 (nth 1 gnus-command-method)))))
397 (defun gnus-request-update-mark (group article mark)
398 "Allow the backend to change the mark the user tries to put on an article."
399 (let ((gnus-command-method (gnus-find-method-for-group group)))
400 (if (not (gnus-check-backend-function
401 'request-update-mark (car gnus-command-method)))
403 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
404 (gnus-group-real-name group) article mark))))
406 (defun gnus-request-article (article group &optional buffer)
407 "Request the ARTICLE in GROUP.
408 ARTICLE can either be an article number or an article Message-ID.
409 If BUFFER, insert the article in that group."
410 (let ((gnus-command-method (gnus-find-method-for-group group)))
411 (funcall (gnus-get-function gnus-command-method 'request-article)
412 article (gnus-group-real-name group)
413 (nth 1 gnus-command-method) buffer)))
415 (defun gnus-request-head (article group)
416 "Request the head of ARTICLE in GROUP."
417 (let* ((gnus-command-method (gnus-find-method-for-group group))
418 (head (gnus-get-function gnus-command-method 'request-head t))
424 (gnus-cache-request-article article group))
425 (setq res (cons group article)
427 ;; Check the agent cache.
428 ((gnus-agent-request-article article group)
429 (setq res (cons group article)
431 ;; Use `head' function.
433 (setq res (funcall head article (gnus-group-real-name group)
434 (nth 1 gnus-command-method))))
435 ;; Use `article' function.
437 (setq res (gnus-request-article article group)
441 (set-buffer nntp-server-buffer)
442 (goto-char (point-min))
443 (when (search-forward "\n\n" nil t)
444 (delete-region (1- (point)) (point-max)))
445 (nnheader-fold-continuation-lines)))
448 (defun gnus-request-body (article group)
449 "Request the body of ARTICLE in GROUP."
450 (let* ((gnus-command-method (gnus-find-method-for-group group))
451 (head (gnus-get-function gnus-command-method 'request-body t))
457 (gnus-cache-request-article article group))
458 (setq res (cons group article)
460 ;; Check the agent cache.
461 ((gnus-agent-request-article article group)
462 (setq res (cons group article)
464 ;; Use `head' function.
466 (setq res (funcall head article (gnus-group-real-name group)
467 (nth 1 gnus-command-method))))
468 ;; Use `article' function.
470 (setq res (gnus-request-article article group)
474 (set-buffer nntp-server-buffer)
475 (goto-char (point-min))
476 (when (search-forward "\n\n" nil t)
477 (delete-region (point-min) (1- (point))))))
480 (defun gnus-request-post (gnus-command-method)
481 "Post the current buffer using GNUS-COMMAND-METHOD."
482 (when (stringp gnus-command-method)
483 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
484 (funcall (gnus-get-function gnus-command-method 'request-post)
485 (nth 1 gnus-command-method)))
487 (defun gnus-request-scan (group gnus-command-method)
488 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
489 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
490 (let ((gnus-command-method
491 (if group (gnus-find-method-for-group group) gnus-command-method))
492 (gnus-inhibit-demon t)
493 (mail-source-plugged gnus-plugged))
494 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
496 (setq gnus-internal-registry-spool-current-method gnus-command-method)
497 (funcall (gnus-get-function gnus-command-method 'request-scan)
498 (and group (gnus-group-real-name group))
499 (nth 1 gnus-command-method))))))
501 (defsubst gnus-request-update-info (info gnus-command-method)
502 "Request that GNUS-COMMAND-METHOD update INFO."
503 (when (stringp gnus-command-method)
504 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
505 (when (gnus-check-backend-function
506 'request-update-info (car gnus-command-method))
507 (let ((group (gnus-info-group info)))
508 (and (funcall (gnus-get-function gnus-command-method
509 'request-update-info)
510 (gnus-group-real-name group)
511 info (nth 1 gnus-command-method))
512 ;; If the minimum article number is greater than 1, then all
513 ;; smaller article numbers are known not to exist; we'll
514 ;; artificially add those to the 'read range.
515 (let* ((active (gnus-active group))
518 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
519 (read (gnus-info-read info))
520 (new-read (gnus-range-add read (list range))))
521 (gnus-info-set-read info new-read)))
524 (defun gnus-request-expire-articles (articles group &optional force)
525 (let* ((gnus-command-method (gnus-find-method-for-group group))
528 (gnus-get-function gnus-command-method 'request-expire-articles)
529 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
531 (when (and gnus-agent
532 (gnus-agent-method-p gnus-command-method))
533 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
534 (when expired-articles
535 (gnus-agent-expire expired-articles group 'force))))
538 (defun gnus-request-move-article (article group server accept-function
540 (let* ((gnus-command-method (gnus-find-method-for-group group))
541 (result (funcall (gnus-get-function gnus-command-method
542 'request-move-article)
543 article (gnus-group-real-name group)
544 (nth 1 gnus-command-method) accept-function last)))
545 (when (and result gnus-agent
546 (gnus-agent-method-p gnus-command-method))
547 (gnus-agent-expire (list article) group 'force))
550 (defun gnus-request-accept-article (group &optional gnus-command-method last
552 ;; Make sure there's a newline at the end of the article.
553 (when (stringp gnus-command-method)
554 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
555 (when (and (not gnus-command-method)
557 (setq gnus-command-method (gnus-group-name-to-method group)))
558 (goto-char (point-max))
561 (let ((gnus-command-method (or gnus-command-method
562 (gnus-find-method-for-group group))))
563 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
564 (if (stringp group) (gnus-group-real-name group) group)
565 (cadr gnus-command-method)
568 (defun gnus-request-replace-article (article group buffer &optional no-encode)
569 (let ((func (car (gnus-group-name-to-method group))))
570 (funcall (intern (format "%s-request-replace-article" func))
571 article (gnus-group-real-name group) buffer)))
573 (defun gnus-request-associate-buffer (group)
574 (let ((gnus-command-method (gnus-find-method-for-group group)))
575 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
576 (gnus-group-real-name group))))
578 (defun gnus-request-restore-buffer (article group)
579 "Request a new buffer restored to the state of ARTICLE."
580 (let ((gnus-command-method (gnus-find-method-for-group group)))
581 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
582 article (gnus-group-real-name group)
583 (nth 1 gnus-command-method))))
585 (defun gnus-request-create-group (group &optional gnus-command-method args)
586 (when (stringp gnus-command-method)
587 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
588 (let ((gnus-command-method
589 (or gnus-command-method (gnus-find-method-for-group group))))
590 (funcall (gnus-get-function gnus-command-method 'request-create-group)
591 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
593 (defun gnus-request-delete-group (group &optional force)
594 (let ((gnus-command-method (gnus-find-method-for-group group)))
595 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
596 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
598 (defun gnus-request-rename-group (group new-name)
599 (let ((gnus-command-method (gnus-find-method-for-group group)))
600 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
601 (gnus-group-real-name group)
602 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
604 (defun gnus-close-backends ()
605 ;; Send a close request to all backends that support such a request.
606 (let ((methods gnus-valid-select-methods)
607 (gnus-inhibit-demon t)
608 func gnus-command-method)
609 (while (setq gnus-command-method (pop methods))
610 (when (fboundp (setq func (intern
611 (concat (car gnus-command-method)
615 (defun gnus-asynchronous-p (gnus-command-method)
616 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
620 (defun gnus-remove-denial (gnus-command-method)
621 (when (stringp gnus-command-method)
622 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
623 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
624 (status (cadr elem)))
625 ;; If this hasn't been opened before, we add it to the list.
626 (when (eq status 'denied)
627 ;; Set the status of this server.
628 (setcar (cdr elem) 'closed))))
632 ;;; gnus-int.el ends here