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>
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 "Unplugg 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 (message "Quit trying to open server")
213 ;; If this hasn't been opened before, we add it to the list.
215 (setq elem (list gnus-command-method nil)
216 gnus-opened-servers (cons elem gnus-opened-servers)))
217 ;; Set the status of this server.
220 (if (eq (cadr elem) 'offline)
224 (not (eq (cadr elem) 'offline))
225 (gnus-agent-method-p gnus-command-method))
226 (or gnus-server-unopen-status
228 (format "Unable to open %s:%s, go offline? "
229 (car gnus-command-method)
230 (cadr gnus-command-method)))
234 ;; Return the result from the "open" call.
235 (cond ((eq (cadr elem) 'offline)
236 ;; I'm avoiding infinite recursion by binding unopen
237 ;; status to denied (The logic of this routine
238 ;; guarantees that I can't get to this point with
239 ;; unopen status already bound to denied).
240 (unless (eq gnus-server-unopen-status 'denied)
241 (let ((gnus-server-unopen-status 'denied))
242 (gnus-open-server gnus-command-method)))
247 (defun gnus-close-server (gnus-command-method)
248 "Close the connection to GNUS-COMMAND-METHOD."
249 (when (stringp gnus-command-method)
250 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
251 (funcall (gnus-get-function gnus-command-method 'close-server)
252 (nth 1 gnus-command-method)))
254 (defun gnus-request-list (gnus-command-method)
255 "Request the active file from GNUS-COMMAND-METHOD."
256 (when (stringp gnus-command-method)
257 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
258 (funcall (gnus-get-function gnus-command-method 'request-list)
259 (nth 1 gnus-command-method)))
261 (defun gnus-request-list-newsgroups (gnus-command-method)
262 "Request the newsgroups file from GNUS-COMMAND-METHOD."
263 (when (stringp gnus-command-method)
264 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
265 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
266 (nth 1 gnus-command-method)))
268 (defun gnus-request-newgroups (date gnus-command-method)
269 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
270 (when (stringp gnus-command-method)
271 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
272 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
274 (funcall func date (nth 1 gnus-command-method)))))
276 (defun gnus-server-opened (gnus-command-method)
277 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
278 (unless (eq (gnus-server-status gnus-command-method)
280 (when (stringp gnus-command-method)
281 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
282 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
283 (nth 1 gnus-command-method))))
285 (defun gnus-status-message (gnus-command-method)
286 "Return the status message from GNUS-COMMAND-METHOD.
287 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
288 this group uses will be queried."
289 (let ((gnus-command-method
290 (if (stringp gnus-command-method)
291 (gnus-find-method-for-group gnus-command-method)
292 gnus-command-method)))
293 (funcall (gnus-get-function gnus-command-method 'status-message)
294 (nth 1 gnus-command-method))))
296 (defun gnus-request-regenerate (gnus-command-method)
297 "Request a data generation from GNUS-COMMAND-METHOD."
298 (when (stringp gnus-command-method)
299 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
300 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
301 (nth 1 gnus-command-method)))
303 (defun gnus-request-group (group &optional dont-check gnus-command-method)
304 "Request GROUP. If DONT-CHECK, no information is required."
305 (let ((gnus-command-method
306 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
307 (when (stringp gnus-command-method)
308 (setq gnus-command-method
309 (inline (gnus-server-to-method gnus-command-method))))
310 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
311 (gnus-group-real-name group) (nth 1 gnus-command-method)
314 (defun gnus-list-active-group (group)
315 "Request active information on GROUP."
316 (let ((gnus-command-method (gnus-find-method-for-group group))
317 (func 'list-active-group))
318 (when (gnus-check-backend-function func group)
319 (funcall (gnus-get-function gnus-command-method func)
320 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
322 (defun gnus-request-group-description (group)
323 "Request a description of GROUP."
324 (let ((gnus-command-method (gnus-find-method-for-group group))
325 (func 'request-group-description))
326 (when (gnus-check-backend-function func group)
327 (funcall (gnus-get-function gnus-command-method func)
328 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
330 (defun gnus-request-group-articles (group)
331 "Request a list of existing articles in GROUP."
332 (let ((gnus-command-method (gnus-find-method-for-group group))
333 (func 'request-group-articles))
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-close-group (group)
339 "Request the GROUP be closed."
340 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
341 (funcall (gnus-get-function gnus-command-method 'close-group)
342 (gnus-group-real-name group) (nth 1 gnus-command-method))))
344 (defun gnus-retrieve-headers (articles group &optional fetch-old)
345 "Request headers for ARTICLES in GROUP.
346 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
347 (let ((gnus-command-method (gnus-find-method-for-group group)))
349 ((and gnus-use-cache (numberp (car articles)))
350 (gnus-cache-retrieve-headers articles group fetch-old))
351 ((and gnus-agent gnus-agent-cache (gnus-online gnus-command-method)
352 (gnus-agent-method-p gnus-command-method))
353 (gnus-agent-retrieve-headers articles group fetch-old))
355 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
356 articles (gnus-group-real-name group)
357 (nth 1 gnus-command-method) fetch-old)))))
359 (defun gnus-retrieve-articles (articles group)
360 "Request ARTICLES in GROUP."
361 (let ((gnus-command-method (gnus-find-method-for-group group)))
362 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
363 articles (gnus-group-real-name group)
364 (nth 1 gnus-command-method))))
366 (defun gnus-retrieve-groups (groups gnus-command-method)
367 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
368 (when (stringp gnus-command-method)
369 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
370 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
371 groups (nth 1 gnus-command-method)))
373 (defun gnus-request-type (group &optional article)
374 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
375 (let ((gnus-command-method (gnus-find-method-for-group group)))
376 (if (not (gnus-check-backend-function
377 'request-type (car gnus-command-method)))
379 (funcall (gnus-get-function gnus-command-method 'request-type)
380 (gnus-group-real-name group) article))))
382 (defun gnus-request-set-mark (group action)
383 "Set marks on articles in the backend."
384 (let ((gnus-command-method (gnus-find-method-for-group group)))
385 (if (not (gnus-check-backend-function
386 'request-set-mark (car gnus-command-method)))
388 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
389 (gnus-group-real-name group) action
390 (nth 1 gnus-command-method)))))
392 (defun gnus-request-update-mark (group article mark)
393 "Allow the backend to change the mark the user tries to put on an article."
394 (let ((gnus-command-method (gnus-find-method-for-group group)))
395 (if (not (gnus-check-backend-function
396 'request-update-mark (car gnus-command-method)))
398 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
399 (gnus-group-real-name group) article mark))))
401 (defun gnus-request-article (article group &optional buffer)
402 "Request the ARTICLE in GROUP.
403 ARTICLE can either be an article number or an article Message-ID.
404 If BUFFER, insert the article in that group."
405 (let ((gnus-command-method (gnus-find-method-for-group group)))
406 (funcall (gnus-get-function gnus-command-method 'request-article)
407 article (gnus-group-real-name group)
408 (nth 1 gnus-command-method) buffer)))
410 (defun gnus-request-head (article group)
411 "Request the head of ARTICLE in GROUP."
412 (let* ((gnus-command-method (gnus-find-method-for-group group))
413 (head (gnus-get-function gnus-command-method 'request-head t))
419 (gnus-cache-request-article article group))
420 (setq res (cons group article)
422 ;; Check the agent cache.
423 ((and gnus-agent gnus-agent-cache gnus-plugged
425 (gnus-agent-request-article article group))
426 (setq res (cons group article)
428 ;; Use `head' function.
430 (setq res (funcall head article (gnus-group-real-name group)
431 (nth 1 gnus-command-method))))
432 ;; Use `article' function.
434 (setq res (gnus-request-article article group)
438 (set-buffer nntp-server-buffer)
439 (goto-char (point-min))
440 (when (search-forward "\n\n" nil t)
441 (delete-region (1- (point)) (point-max)))
442 (nnheader-fold-continuation-lines)))
445 (defun gnus-request-body (article group)
446 "Request the body of ARTICLE in GROUP."
447 (let* ((gnus-command-method (gnus-find-method-for-group group))
448 (head (gnus-get-function gnus-command-method 'request-body t))
454 (gnus-cache-request-article article group))
455 (setq res (cons group article)
457 ;; Check the agent cache.
458 ((and gnus-agent gnus-agent-cache gnus-plugged
460 (gnus-agent-request-article article group))
461 (setq res (cons group article)
463 ;; Use `head' function.
465 (setq res (funcall head article (gnus-group-real-name group)
466 (nth 1 gnus-command-method))))
467 ;; Use `article' function.
469 (setq res (gnus-request-article article group)
473 (set-buffer nntp-server-buffer)
474 (goto-char (point-min))
475 (when (search-forward "\n\n" nil t)
476 (delete-region (point-min) (1- (point))))))
479 (defun gnus-request-post (gnus-command-method)
480 "Post the current buffer using GNUS-COMMAND-METHOD."
481 (when (stringp gnus-command-method)
482 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
483 (funcall (gnus-get-function gnus-command-method 'request-post)
484 (nth 1 gnus-command-method)))
486 (defun gnus-request-scan (group gnus-command-method)
487 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
488 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
489 (let ((gnus-command-method
490 (if group (gnus-find-method-for-group group) gnus-command-method))
491 (gnus-inhibit-demon t)
492 (mail-source-plugged gnus-plugged))
493 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
495 (setq gnus-internal-registry-spool-current-method gnus-command-method)
496 (funcall (gnus-get-function gnus-command-method 'request-scan)
497 (and group (gnus-group-real-name group))
498 (nth 1 gnus-command-method))))))
500 (defsubst gnus-request-update-info (info gnus-command-method)
501 "Request that GNUS-COMMAND-METHOD update INFO."
502 (when (stringp gnus-command-method)
503 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
504 (when (gnus-check-backend-function
505 'request-update-info (car gnus-command-method))
506 (let ((group (gnus-info-group info)))
507 (and (funcall (gnus-get-function gnus-command-method
508 'request-update-info)
509 (gnus-group-real-name group)
510 info (nth 1 gnus-command-method))
511 ;; If the minimum article number is greater than 1, then all
512 ;; smaller article numbers are known not to exist; we'll
513 ;; artificially add those to the 'read range.
514 (let* ((active (gnus-active group))
517 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
518 (read (gnus-info-read info))
519 (new-read (gnus-range-add read (list range))))
520 (gnus-info-set-read info new-read)))
523 (defun gnus-request-expire-articles (articles group &optional force)
524 (let* ((gnus-command-method (gnus-find-method-for-group group))
527 (gnus-get-function gnus-command-method 'request-expire-articles)
528 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
530 (when (and gnus-agent gnus-agent-cache (gnus-agent-method-p gnus-command-method))
531 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
532 (when expired-articles
533 (gnus-agent-expire expired-articles group 'force))))
536 (defun gnus-request-move-article (article group server accept-function &optional last)
537 (let* ((gnus-command-method (gnus-find-method-for-group group))
538 (result (funcall (gnus-get-function gnus-command-method '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 gnus-agent-cache (gnus-agent-method-p gnus-command-method))
542 (gnus-agent-expire (list article) group 'force))
545 (defun gnus-request-accept-article (group &optional gnus-command-method last
547 ;; Make sure there's a newline at the end of the article.
548 (when (stringp gnus-command-method)
549 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
550 (when (and (not gnus-command-method)
552 (setq gnus-command-method (gnus-group-name-to-method group)))
553 (goto-char (point-max))
556 (let ((gnus-command-method (or gnus-command-method
557 (gnus-find-method-for-group group))))
558 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
559 (if (stringp group) (gnus-group-real-name group) group)
560 (cadr gnus-command-method)
563 (defun gnus-request-replace-article (article group buffer &optional no-encode)
564 (let ((func (car (gnus-group-name-to-method group))))
565 (funcall (intern (format "%s-request-replace-article" func))
566 article (gnus-group-real-name group) buffer)))
568 (defun gnus-request-associate-buffer (group)
569 (let ((gnus-command-method (gnus-find-method-for-group group)))
570 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
571 (gnus-group-real-name group))))
573 (defun gnus-request-restore-buffer (article group)
574 "Request a new buffer restored to the state of ARTICLE."
575 (let ((gnus-command-method (gnus-find-method-for-group group)))
576 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
577 article (gnus-group-real-name group)
578 (nth 1 gnus-command-method))))
580 (defun gnus-request-create-group (group &optional gnus-command-method args)
581 (when (stringp gnus-command-method)
582 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
583 (let ((gnus-command-method
584 (or gnus-command-method (gnus-find-method-for-group group))))
585 (funcall (gnus-get-function gnus-command-method 'request-create-group)
586 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
588 (defun gnus-request-delete-group (group &optional force)
589 (let ((gnus-command-method (gnus-find-method-for-group group)))
590 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
591 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
593 (defun gnus-request-rename-group (group new-name)
594 (let ((gnus-command-method (gnus-find-method-for-group group)))
595 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
596 (gnus-group-real-name group)
597 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
599 (defun gnus-close-backends ()
600 ;; Send a close request to all backends that support such a request.
601 (let ((methods gnus-valid-select-methods)
602 (gnus-inhibit-demon t)
603 func gnus-command-method)
604 (while (setq gnus-command-method (pop methods))
605 (when (fboundp (setq func (intern
606 (concat (car gnus-command-method)
610 (defun gnus-asynchronous-p (gnus-command-method)
611 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
615 (defun gnus-remove-denial (gnus-command-method)
616 (when (stringp gnus-command-method)
617 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
618 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
619 (status (cadr elem)))
620 ;; If this hasn't been opened before, we add it to the list.
621 (when (eq status 'denied)
622 ;; Set the status of this server.
623 (setcar (cdr elem) 'closed))))
627 ;;; gnus-int.el ends here