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")
37 (defcustom gnus-open-server-hook nil
38 "Hook called just before opening connection to the news server."
42 (defcustom gnus-server-unopen-status nil
43 "The default status if the server is not able to open.
44 If the server is covered by Gnus agent, the possible values are
45 `denied', set the server denied; `offline', set the server offline;
46 nil, ask user. If the server is not covered by Gnus agent, set the
49 :type '(choice (const :tag "Ask" nil)
50 (const :tag "Deny server" denied)
51 (const :tag "Unplug Agent" offline)))
53 (defvar gnus-internal-registry-spool-current-method nil
54 "The current method, for the registry.")
57 ;;; Server Communication
60 (defun gnus-start-news-server (&optional confirm)
61 "Open a method for getting news.
62 If CONFIRM is non-nil, the user will be asked for an NNTP server."
64 (if gnus-current-select-method
65 ;; Stream is already opened.
68 (unless gnus-nntp-service
69 (setq gnus-nntp-server nil))
71 ;; Read server name with completion.
72 (setq gnus-nntp-server
73 (completing-read "NNTP server: "
74 (mapcar (lambda (server) (list server))
75 (cons (list gnus-nntp-server)
76 gnus-secondary-servers))
77 nil nil gnus-nntp-server)))
79 (when (and gnus-nntp-server
80 (stringp gnus-nntp-server)
81 (not (string= gnus-nntp-server "")))
82 (setq gnus-select-method
83 (cond ((or (string= gnus-nntp-server "")
84 (string= gnus-nntp-server "::"))
85 (list 'nnspool (system-name)))
86 ((string-match "^:" gnus-nntp-server)
87 (list 'nnmh gnus-nntp-server
89 (file-name-as-directory
91 (substring gnus-nntp-server 1) "~/")))
92 (list 'nnmh-get-new-mail nil)))
94 (list 'nntp gnus-nntp-server)))))
96 (setq how (car gnus-select-method))
100 (gnus-message 5 "Looking up local news spool..."))
103 (gnus-message 5 "Looking up mh spool..."))
106 (setq gnus-current-select-method gnus-select-method)
107 (gnus-run-hooks 'gnus-open-server-hook)
109 ;; gnus-open-server-hook might have opened it
110 (gnus-server-opened gnus-select-method)
111 (gnus-open-server gnus-select-method)
115 "%s (%s) open error: '%s'. Continue? "
116 (car gnus-select-method) (cadr gnus-select-method)
117 (gnus-status-message gnus-select-method)))
118 (gnus-error 1 "Couldn't open server on %s"
119 (nth 1 gnus-select-method))))))
121 (defun gnus-check-group (group)
122 "Try to make sure that the server where GROUP exists is alive."
123 (let ((method (gnus-find-method-for-group group)))
124 (or (gnus-server-opened method)
125 (gnus-open-server method))))
127 (defun gnus-check-server (&optional method silent)
128 "Check whether the connection to METHOD is down.
129 If METHOD is nil, use `gnus-select-method'.
130 If it is down, start it up (again)."
131 (let ((method (or method gnus-select-method))
133 ;; Transform virtual server names into select methods.
134 (when (stringp method)
135 (setq method (gnus-server-to-method method)))
136 (if (gnus-server-opened method)
137 ;; The stream is already opened.
141 (gnus-message 5 "Opening %s server%s..." (car method)
142 (if (equal (nth 1 method) "") ""
143 (format " on %s" (nth 1 method)))))
144 (gnus-run-hooks 'gnus-open-server-hook)
147 (setq result (gnus-open-server method))
148 (quit (message "Quit gnus-check-server")
151 (gnus-message 5 "Opening %s server%s...%s" (car method)
152 (if (equal (nth 1 method) "") ""
153 (format " on %s" (nth 1 method)))
154 (if result "done" "failed")))))))
156 (defun gnus-get-function (method function &optional noerror)
157 "Return a function symbol based on METHOD and FUNCTION."
158 ;; Translate server names into methods.
160 (error "Attempted use of a nil select method"))
161 (when (stringp method)
162 (setq method (gnus-server-to-method method)))
163 ;; Check cache of constructed names.
164 (let* ((method-sym (if gnus-agent
165 (gnus-agent-get-function method)
167 (method-fns (get method-sym 'gnus-method-functions))
168 (func (let ((method-fnlist-elt (assq function method-fns)))
169 (unless method-fnlist-elt
170 (setq method-fnlist-elt
172 (intern (format "%s-%s" method-sym function))))
173 (put method-sym 'gnus-method-functions
174 (cons method-fnlist-elt method-fns)))
175 (cdr method-fnlist-elt))))
176 ;; Maybe complain if there is no function.
177 (unless (fboundp func)
179 (error "Trying to require a method that doesn't exist"))
180 (require (car method))
181 (when (not (fboundp func))
184 (error "No such function: %s" func))))
189 ;;; Interface functions to the backends.
192 (defun gnus-open-server (gnus-command-method)
193 "Open a connection to GNUS-COMMAND-METHOD."
194 (when (stringp gnus-command-method)
195 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
196 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
197 ;; If this method was previously denied, we just return nil.
198 (if (eq (nth 1 elem) 'denied)
200 (gnus-message 1 "Denied server")
205 (funcall (gnus-get-function gnus-command-method 'open-server)
206 (nth 1 gnus-command-method)
207 (nthcdr 2 gnus-command-method))
209 (gnus-message 1 (format
210 "Unable to open server due to: %s"
211 (error-message-string err)))
214 (gnus-message 1 "Quit trying to open server")
216 ;; If this hasn't been opened before, we add it to the list.
218 (setq elem (list gnus-command-method nil)
219 gnus-opened-servers (cons elem gnus-opened-servers)))
220 ;; Set the status of this server.
223 (if (eq (cadr elem) 'offline)
227 (not (eq (cadr elem) 'offline))
228 (gnus-agent-method-p gnus-command-method))
229 (or gnus-server-unopen-status
231 (format "Unable to open %s:%s, go offline? "
232 (car gnus-command-method)
233 (cadr gnus-command-method)))
237 ;; Return the result from the "open" call.
238 (cond ((eq (cadr elem) 'offline)
239 ;; I'm avoiding infinite recursion by binding unopen
240 ;; status to denied (The logic of this routine
241 ;; guarantees that I can't get to this point with
242 ;; unopen status already bound to denied).
243 (unless (eq gnus-server-unopen-status 'denied)
244 (let ((gnus-server-unopen-status 'denied))
245 (gnus-open-server gnus-command-method)))
250 (defun gnus-close-server (gnus-command-method)
251 "Close the connection to GNUS-COMMAND-METHOD."
252 (when (stringp gnus-command-method)
253 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
254 (funcall (gnus-get-function gnus-command-method 'close-server)
255 (nth 1 gnus-command-method)))
257 (defun gnus-request-list (gnus-command-method)
258 "Request the active file from GNUS-COMMAND-METHOD."
259 (when (stringp gnus-command-method)
260 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
261 (funcall (gnus-get-function gnus-command-method 'request-list)
262 (nth 1 gnus-command-method)))
264 (defun gnus-request-list-newsgroups (gnus-command-method)
265 "Request the newsgroups file from 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 'request-list-newsgroups)
269 (nth 1 gnus-command-method)))
271 (defun gnus-request-newgroups (date gnus-command-method)
272 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
273 (when (stringp gnus-command-method)
274 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
275 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
277 (funcall func date (nth 1 gnus-command-method)))))
279 (defun gnus-server-opened (gnus-command-method)
280 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
281 (unless (eq (gnus-server-status gnus-command-method)
283 (when (stringp gnus-command-method)
284 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
285 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
286 (nth 1 gnus-command-method))))
288 (defun gnus-status-message (gnus-command-method)
289 "Return the status message from GNUS-COMMAND-METHOD.
290 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
291 this group uses will be queried."
292 (let ((gnus-command-method
293 (if (stringp gnus-command-method)
294 (gnus-find-method-for-group gnus-command-method)
295 gnus-command-method)))
296 (funcall (gnus-get-function gnus-command-method 'status-message)
297 (nth 1 gnus-command-method))))
299 (defun gnus-request-regenerate (gnus-command-method)
300 "Request a data generation from GNUS-COMMAND-METHOD."
301 (when (stringp gnus-command-method)
302 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
303 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
304 (nth 1 gnus-command-method)))
306 (defun gnus-request-group (group &optional dont-check gnus-command-method)
307 "Request GROUP. If DONT-CHECK, no information is required."
308 (let ((gnus-command-method
309 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
310 (when (stringp gnus-command-method)
311 (setq gnus-command-method
312 (inline (gnus-server-to-method gnus-command-method))))
313 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
314 (gnus-group-real-name group) (nth 1 gnus-command-method)
317 (defun gnus-list-active-group (group)
318 "Request active information on GROUP."
319 (let ((gnus-command-method (gnus-find-method-for-group group))
320 (func 'list-active-group))
321 (when (gnus-check-backend-function func group)
322 (funcall (gnus-get-function gnus-command-method func)
323 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
325 (defun gnus-request-group-description (group)
326 "Request a description of GROUP."
327 (let ((gnus-command-method (gnus-find-method-for-group group))
328 (func 'request-group-description))
329 (when (gnus-check-backend-function func group)
330 (funcall (gnus-get-function gnus-command-method func)
331 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
333 (defun gnus-request-group-articles (group)
334 "Request a list of existing articles in GROUP."
335 (let ((gnus-command-method (gnus-find-method-for-group group))
336 (func 'request-group-articles))
337 (when (gnus-check-backend-function func group)
338 (funcall (gnus-get-function gnus-command-method func)
339 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
341 (defun gnus-close-group (group)
342 "Request the GROUP be closed."
343 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
344 (funcall (gnus-get-function gnus-command-method 'close-group)
345 (gnus-group-real-name group) (nth 1 gnus-command-method))))
347 (defun gnus-retrieve-headers (articles group &optional fetch-old)
348 "Request headers for ARTICLES in GROUP.
349 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
350 (let ((gnus-command-method (gnus-find-method-for-group group)))
352 ((and gnus-use-cache (numberp (car articles)))
353 (gnus-cache-retrieve-headers articles group fetch-old))
354 ((and gnus-agent (gnus-online gnus-command-method)
355 (gnus-agent-method-p gnus-command-method))
356 (gnus-agent-retrieve-headers articles group fetch-old))
358 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
359 articles (gnus-group-real-name group)
360 (nth 1 gnus-command-method) fetch-old)))))
362 (defun gnus-retrieve-articles (articles group)
363 "Request ARTICLES in GROUP."
364 (let ((gnus-command-method (gnus-find-method-for-group group)))
365 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
366 articles (gnus-group-real-name group)
367 (nth 1 gnus-command-method))))
369 (defun gnus-retrieve-groups (groups gnus-command-method)
370 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
371 (when (stringp gnus-command-method)
372 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
373 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
374 groups (nth 1 gnus-command-method)))
376 (defun gnus-request-type (group &optional article)
377 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
378 (let ((gnus-command-method (gnus-find-method-for-group group)))
379 (if (not (gnus-check-backend-function
380 'request-type (car gnus-command-method)))
382 (funcall (gnus-get-function gnus-command-method 'request-type)
383 (gnus-group-real-name group) article))))
385 (defun gnus-request-set-mark (group action)
386 "Set marks on articles in the backend."
387 (let ((gnus-command-method (gnus-find-method-for-group group)))
388 (if (not (gnus-check-backend-function
389 'request-set-mark (car gnus-command-method)))
391 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
392 (gnus-group-real-name group) action
393 (nth 1 gnus-command-method)))))
395 (defun gnus-request-update-mark (group article mark)
396 "Allow the backend to change the mark the user tries to put on an article."
397 (let ((gnus-command-method (gnus-find-method-for-group group)))
398 (if (not (gnus-check-backend-function
399 'request-update-mark (car gnus-command-method)))
401 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
402 (gnus-group-real-name group) article mark))))
404 (defun gnus-request-article (article group &optional buffer)
405 "Request the ARTICLE in GROUP.
406 ARTICLE can either be an article number or an article Message-ID.
407 If BUFFER, insert the article in that group."
408 (let ((gnus-command-method (gnus-find-method-for-group group)))
409 (funcall (gnus-get-function gnus-command-method 'request-article)
410 article (gnus-group-real-name group)
411 (nth 1 gnus-command-method) buffer)))
413 (defun gnus-request-head (article group)
414 "Request the head of ARTICLE in GROUP."
415 (let* ((gnus-command-method (gnus-find-method-for-group group))
416 (head (gnus-get-function gnus-command-method 'request-head t))
422 (gnus-cache-request-article article group))
423 (setq res (cons group article)
425 ;; Check the agent cache.
426 ((gnus-agent-request-article article group)
427 (setq res (cons group article)
429 ;; Use `head' function.
431 (setq res (funcall head article (gnus-group-real-name group)
432 (nth 1 gnus-command-method))))
433 ;; Use `article' function.
435 (setq res (gnus-request-article article group)
439 (set-buffer nntp-server-buffer)
440 (goto-char (point-min))
441 (when (search-forward "\n\n" nil t)
442 (delete-region (1- (point)) (point-max)))
443 (nnheader-fold-continuation-lines)))
446 (defun gnus-request-body (article group)
447 "Request the body of ARTICLE in GROUP."
448 (let* ((gnus-command-method (gnus-find-method-for-group group))
449 (head (gnus-get-function gnus-command-method 'request-body t))
455 (gnus-cache-request-article article group))
456 (setq res (cons group article)
458 ;; Check the agent cache.
459 ((gnus-agent-request-article article group)
460 (setq res (cons group article)
462 ;; Use `head' function.
464 (setq res (funcall head article (gnus-group-real-name group)
465 (nth 1 gnus-command-method))))
466 ;; Use `article' function.
468 (setq res (gnus-request-article article group)
472 (set-buffer nntp-server-buffer)
473 (goto-char (point-min))
474 (when (search-forward "\n\n" nil t)
475 (delete-region (point-min) (1- (point))))))
478 (defun gnus-request-post (gnus-command-method)
479 "Post the current buffer using GNUS-COMMAND-METHOD."
480 (when (stringp gnus-command-method)
481 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
482 (funcall (gnus-get-function gnus-command-method 'request-post)
483 (nth 1 gnus-command-method)))
485 (defun gnus-request-scan (group gnus-command-method)
486 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
487 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
488 (let ((gnus-command-method
489 (if group (gnus-find-method-for-group group) gnus-command-method))
490 (gnus-inhibit-demon t)
491 (mail-source-plugged gnus-plugged))
492 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
494 (setq gnus-internal-registry-spool-current-method gnus-command-method)
495 (funcall (gnus-get-function gnus-command-method 'request-scan)
496 (and group (gnus-group-real-name group))
497 (nth 1 gnus-command-method))))))
499 (defsubst gnus-request-update-info (info gnus-command-method)
500 "Request that GNUS-COMMAND-METHOD update INFO."
501 (when (stringp gnus-command-method)
502 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
503 (when (gnus-check-backend-function
504 'request-update-info (car gnus-command-method))
505 (let ((group (gnus-info-group info)))
506 (and (funcall (gnus-get-function gnus-command-method
507 'request-update-info)
508 (gnus-group-real-name group)
509 info (nth 1 gnus-command-method))
510 ;; If the minimum article number is greater than 1, then all
511 ;; smaller article numbers are known not to exist; we'll
512 ;; artificially add those to the 'read range.
513 (let* ((active (gnus-active group))
516 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
517 (read (gnus-info-read info))
518 (new-read (gnus-range-add read (list range))))
519 (gnus-info-set-read info new-read)))
522 (defun gnus-request-expire-articles (articles group &optional force)
523 (let* ((gnus-command-method (gnus-find-method-for-group group))
526 (gnus-get-function gnus-command-method 'request-expire-articles)
527 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
529 (when (and gnus-agent
530 (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
538 (let* ((gnus-command-method (gnus-find-method-for-group group))
539 (result (funcall (gnus-get-function gnus-command-method
540 'request-move-article)
541 article (gnus-group-real-name group)
542 (nth 1 gnus-command-method) accept-function last)))
543 (when (and result gnus-agent
544 (gnus-agent-method-p gnus-command-method))
545 (gnus-agent-expire (list article) group 'force))
548 (defun gnus-request-accept-article (group &optional gnus-command-method last
550 ;; Make sure there's a newline at the end of the article.
551 (when (stringp gnus-command-method)
552 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
553 (when (and (not gnus-command-method)
555 (setq gnus-command-method (gnus-group-name-to-method group)))
556 (goto-char (point-max))
560 (let ((message-options message-options))
561 (message-options-set-recipient)
563 (message-narrow-to-head)
564 (let ((mail-parse-charset message-default-charset))
565 (mail-encode-encoded-word-buffer)))
566 (message-encode-message-body)))
567 (let ((gnus-command-method (or gnus-command-method
568 (gnus-find-method-for-group group))))
569 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
570 (if (stringp group) (gnus-group-real-name group) group)
571 (cadr gnus-command-method)
574 (defun gnus-request-replace-article (article group buffer &optional no-encode)
576 (let ((message-options message-options))
577 (message-options-set-recipient)
579 (message-narrow-to-head)
580 (let ((mail-parse-charset message-default-charset))
581 (mail-encode-encoded-word-buffer)))
582 (message-encode-message-body)))
583 (let ((func (car (gnus-group-name-to-method group))))
584 (funcall (intern (format "%s-request-replace-article" func))
585 article (gnus-group-real-name group) buffer)))
587 (defun gnus-request-associate-buffer (group)
588 (let ((gnus-command-method (gnus-find-method-for-group group)))
589 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
590 (gnus-group-real-name group))))
592 (defun gnus-request-restore-buffer (article group)
593 "Request a new buffer restored to the state of ARTICLE."
594 (let ((gnus-command-method (gnus-find-method-for-group group)))
595 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
596 article (gnus-group-real-name group)
597 (nth 1 gnus-command-method))))
599 (defun gnus-request-create-group (group &optional gnus-command-method args)
600 (when (stringp gnus-command-method)
601 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
602 (let ((gnus-command-method
603 (or gnus-command-method (gnus-find-method-for-group group))))
604 (funcall (gnus-get-function gnus-command-method 'request-create-group)
605 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
607 (defun gnus-request-delete-group (group &optional force)
608 (let ((gnus-command-method (gnus-find-method-for-group group)))
609 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
610 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
612 (defun gnus-request-rename-group (group new-name)
613 (let ((gnus-command-method (gnus-find-method-for-group group)))
614 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
615 (gnus-group-real-name group)
616 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
618 (defun gnus-close-backends ()
619 ;; Send a close request to all backends that support such a request.
620 (let ((methods gnus-valid-select-methods)
621 (gnus-inhibit-demon t)
622 func gnus-command-method)
623 (while (setq gnus-command-method (pop methods))
624 (when (fboundp (setq func (intern
625 (concat (car gnus-command-method)
629 (defun gnus-asynchronous-p (gnus-command-method)
630 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
634 (defun gnus-remove-denial (gnus-command-method)
635 (when (stringp gnus-command-method)
636 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
637 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
638 (status (cadr elem)))
639 ;; If this hasn't been opened before, we add it to the list.
640 (when (eq status 'denied)
641 ;; Set the status of this server.
642 (setcar (cdr elem) 'closed))))
646 ;;; gnus-int.el ends here