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))
36 (defun gnus-agent-expire (a b c)))
38 (defcustom gnus-open-server-hook nil
39 "Hook called just before opening connection to the news server."
43 (defcustom gnus-server-unopen-status nil
44 "The default status if the server is not able to open.
45 If the server is covered by Gnus agent, the possible values are
46 `denied', set the server denied; `offline', set the server offline;
47 nil, ask user. If the server is not covered by Gnus agent, set the
50 :type '(choice (const :tag "Ask" nil)
51 (const :tag "Deny server" denied)
52 (const :tag "Unplug Agent" offline)))
54 (defvar gnus-internal-registry-spool-current-method nil
55 "The current method, for the registry.")
58 ;;; Server Communication
61 (defun gnus-start-news-server (&optional confirm)
62 "Open a method for getting news.
63 If CONFIRM is non-nil, the user will be asked for an NNTP server."
65 (if gnus-current-select-method
66 ;; Stream is already opened.
69 (unless gnus-nntp-service
70 (setq gnus-nntp-server nil))
72 ;; Read server name with completion.
73 (setq gnus-nntp-server
74 (completing-read "NNTP server: "
75 (mapcar (lambda (server) (list server))
76 (cons (list gnus-nntp-server)
77 gnus-secondary-servers))
78 nil nil gnus-nntp-server)))
80 (when (and gnus-nntp-server
81 (stringp gnus-nntp-server)
82 (not (string= gnus-nntp-server "")))
83 (setq gnus-select-method
84 (cond ((or (string= gnus-nntp-server "")
85 (string= gnus-nntp-server "::"))
86 (list 'nnspool (system-name)))
87 ((string-match "^:" gnus-nntp-server)
88 (list 'nnmh gnus-nntp-server
90 (file-name-as-directory
92 (substring gnus-nntp-server 1) "~/")))
93 (list 'nnmh-get-new-mail nil)))
95 (list 'nntp gnus-nntp-server)))))
97 (setq how (car gnus-select-method))
101 (gnus-message 5 "Looking up local news spool..."))
104 (gnus-message 5 "Looking up mh spool..."))
107 (setq gnus-current-select-method gnus-select-method)
108 (gnus-run-hooks 'gnus-open-server-hook)
110 ;; gnus-open-server-hook might have opened it
111 (gnus-server-opened gnus-select-method)
112 (gnus-open-server gnus-select-method)
116 "%s (%s) open error: '%s'. Continue? "
117 (car gnus-select-method) (cadr gnus-select-method)
118 (gnus-status-message gnus-select-method)))
119 (gnus-error 1 "Couldn't open server on %s"
120 (nth 1 gnus-select-method))))))
122 (defun gnus-check-group (group)
123 "Try to make sure that the server where GROUP exists is alive."
124 (let ((method (gnus-find-method-for-group group)))
125 (or (gnus-server-opened method)
126 (gnus-open-server method))))
128 (defun gnus-check-server (&optional method silent)
129 "Check whether the connection to METHOD is down.
130 If METHOD is nil, use `gnus-select-method'.
131 If it is down, start it up (again)."
132 (let ((method (or method gnus-select-method))
134 ;; Transform virtual server names into select methods.
135 (when (stringp method)
136 (setq method (gnus-server-to-method method)))
137 (if (gnus-server-opened method)
138 ;; The stream is already opened.
142 (gnus-message 5 "Opening %s server%s..." (car method)
143 (if (equal (nth 1 method) "") ""
144 (format " on %s" (nth 1 method)))))
145 (gnus-run-hooks 'gnus-open-server-hook)
148 (setq result (gnus-open-server method))
149 (quit (message "Quit gnus-check-server")
152 (gnus-message 5 "Opening %s server%s...%s" (car method)
153 (if (equal (nth 1 method) "") ""
154 (format " on %s" (nth 1 method)))
155 (if result "done" "failed")))))))
157 (defun gnus-get-function (method function &optional noerror)
158 "Return a function symbol based on METHOD and FUNCTION."
159 ;; Translate server names into methods.
161 (error "Attempted use of a nil select method"))
162 (when (stringp method)
163 (setq method (gnus-server-to-method method)))
164 ;; Check cache of constructed names.
165 (let* ((method-sym (if gnus-agent
166 (gnus-agent-get-function method)
168 (method-fns (get method-sym 'gnus-method-functions))
169 (func (let ((method-fnlist-elt (assq function method-fns)))
170 (unless method-fnlist-elt
171 (setq method-fnlist-elt
173 (intern (format "%s-%s" method-sym function))))
174 (put method-sym 'gnus-method-functions
175 (cons method-fnlist-elt method-fns)))
176 (cdr method-fnlist-elt))))
177 ;; Maybe complain if there is no function.
178 (unless (fboundp func)
180 (error "Trying to require a method that doesn't exist"))
181 (require (car method))
182 (when (not (fboundp func))
185 (error "No such function: %s" func))))
190 ;;; Interface functions to the backends.
193 (defun gnus-open-server (gnus-command-method)
194 "Open a connection to GNUS-COMMAND-METHOD."
195 (when (stringp gnus-command-method)
196 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
197 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
198 ;; If this method was previously denied, we just return nil.
199 (if (eq (nth 1 elem) 'denied)
201 (gnus-message 1 "Denied server")
206 (funcall (gnus-get-function gnus-command-method 'open-server)
207 (nth 1 gnus-command-method)
208 (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-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 ((gnus-agent-request-article article group)
424 (setq res (cons group article)
426 ;; Use `head' function.
428 (setq res (funcall head article (gnus-group-real-name group)
429 (nth 1 gnus-command-method))))
430 ;; Use `article' function.
432 (setq res (gnus-request-article article group)
436 (set-buffer nntp-server-buffer)
437 (goto-char (point-min))
438 (when (search-forward "\n\n" nil t)
439 (delete-region (1- (point)) (point-max)))
440 (nnheader-fold-continuation-lines)))
443 (defun gnus-request-body (article group)
444 "Request the body of ARTICLE in GROUP."
445 (let* ((gnus-command-method (gnus-find-method-for-group group))
446 (head (gnus-get-function gnus-command-method 'request-body t))
452 (gnus-cache-request-article article group))
453 (setq res (cons group article)
455 ;; Check the agent cache.
456 ((gnus-agent-request-article article group)
457 (setq res (cons group article)
459 ;; Use `head' function.
461 (setq res (funcall head article (gnus-group-real-name group)
462 (nth 1 gnus-command-method))))
463 ;; Use `article' function.
465 (setq res (gnus-request-article article group)
469 (set-buffer nntp-server-buffer)
470 (goto-char (point-min))
471 (when (search-forward "\n\n" nil t)
472 (delete-region (point-min) (1- (point))))))
475 (defun gnus-request-post (gnus-command-method)
476 "Post the current buffer using GNUS-COMMAND-METHOD."
477 (when (stringp gnus-command-method)
478 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
479 (funcall (gnus-get-function gnus-command-method 'request-post)
480 (nth 1 gnus-command-method)))
482 (defun gnus-request-scan (group gnus-command-method)
483 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
484 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
485 (let ((gnus-command-method
486 (if group (gnus-find-method-for-group group) gnus-command-method))
487 (gnus-inhibit-demon t)
488 (mail-source-plugged gnus-plugged))
489 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
491 (setq gnus-internal-registry-spool-current-method gnus-command-method)
492 (funcall (gnus-get-function gnus-command-method 'request-scan)
493 (and group (gnus-group-real-name group))
494 (nth 1 gnus-command-method))))))
496 (defsubst gnus-request-update-info (info gnus-command-method)
497 "Request that GNUS-COMMAND-METHOD update INFO."
498 (when (stringp gnus-command-method)
499 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
500 (when (gnus-check-backend-function
501 'request-update-info (car gnus-command-method))
502 (let ((group (gnus-info-group info)))
503 (and (funcall (gnus-get-function gnus-command-method
504 'request-update-info)
505 (gnus-group-real-name group)
506 info (nth 1 gnus-command-method))
507 ;; If the minimum article number is greater than 1, then all
508 ;; smaller article numbers are known not to exist; we'll
509 ;; artificially add those to the 'read range.
510 (let* ((active (gnus-active group))
513 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
514 (read (gnus-info-read info))
515 (new-read (gnus-range-add read (list range))))
516 (gnus-info-set-read info new-read)))
519 (defun gnus-request-expire-articles (articles group &optional force)
520 (let* ((gnus-command-method (gnus-find-method-for-group group))
523 (gnus-get-function gnus-command-method 'request-expire-articles)
524 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
526 (when (and gnus-agent
527 (gnus-agent-method-p gnus-command-method))
528 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
529 (when expired-articles
530 (gnus-agent-expire expired-articles group 'force))))
533 (defun gnus-request-move-article (article group server accept-function
535 (let* ((gnus-command-method (gnus-find-method-for-group group))
536 (result (funcall (gnus-get-function gnus-command-method
537 'request-move-article)
538 article (gnus-group-real-name group)
539 (nth 1 gnus-command-method) accept-function last)))
540 (when (and result gnus-agent
541 (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))
557 (let ((message-options message-options))
558 (message-options-set-recipient)
560 (message-narrow-to-head)
561 (let ((mail-parse-charset message-default-charset))
562 (mail-encode-encoded-word-buffer)))
563 (message-encode-message-body)))
564 (let ((gnus-command-method (or gnus-command-method
565 (gnus-find-method-for-group group))))
566 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
567 (if (stringp group) (gnus-group-real-name group) group)
568 (cadr gnus-command-method)
571 (defun gnus-request-replace-article (article group buffer &optional no-encode)
573 (let ((message-options message-options))
574 (message-options-set-recipient)
576 (message-narrow-to-head)
577 (let ((mail-parse-charset message-default-charset))
578 (mail-encode-encoded-word-buffer)))
579 (message-encode-message-body)))
580 (let ((func (car (gnus-group-name-to-method group))))
581 (funcall (intern (format "%s-request-replace-article" func))
582 article (gnus-group-real-name group) buffer)))
584 (defun gnus-request-associate-buffer (group)
585 (let ((gnus-command-method (gnus-find-method-for-group group)))
586 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
587 (gnus-group-real-name group))))
589 (defun gnus-request-restore-buffer (article group)
590 "Request a new buffer restored to the state of ARTICLE."
591 (let ((gnus-command-method (gnus-find-method-for-group group)))
592 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
593 article (gnus-group-real-name group)
594 (nth 1 gnus-command-method))))
596 (defun gnus-request-create-group (group &optional gnus-command-method args)
597 (when (stringp gnus-command-method)
598 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
599 (let ((gnus-command-method
600 (or gnus-command-method (gnus-find-method-for-group group))))
601 (funcall (gnus-get-function gnus-command-method 'request-create-group)
602 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
604 (defun gnus-request-delete-group (group &optional force)
605 (let ((gnus-command-method (gnus-find-method-for-group group)))
606 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
607 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
609 (defun gnus-request-rename-group (group new-name)
610 (let ((gnus-command-method (gnus-find-method-for-group group)))
611 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
612 (gnus-group-real-name group)
613 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
615 (defun gnus-close-backends ()
616 ;; Send a close request to all backends that support such a request.
617 (let ((methods gnus-valid-select-methods)
618 (gnus-inhibit-demon t)
619 func gnus-command-method)
620 (while (setq gnus-command-method (pop methods))
621 (when (fboundp (setq func (intern
622 (concat (car gnus-command-method)
626 (defun gnus-asynchronous-p (gnus-command-method)
627 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
631 (defun gnus-remove-denial (gnus-command-method)
632 (when (stringp gnus-command-method)
633 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
634 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
635 (status (cadr elem)))
636 ;; If this hasn't been opened before, we add it to the list.
637 (when (eq status 'denied)
638 ;; Set the status of this server.
639 (setcar (cdr elem) 'closed))))
643 ;;; gnus-int.el ends here