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)))
56 ;;; Server Communication
59 (defun gnus-start-news-server (&optional confirm)
60 "Open a method for getting news.
61 If CONFIRM is non-nil, the user will be asked for an NNTP server."
63 (if gnus-current-select-method
64 ;; Stream is already opened.
67 (unless gnus-nntp-service
68 (setq gnus-nntp-server nil))
70 ;; Read server name with completion.
71 (setq gnus-nntp-server
72 (completing-read "NNTP server: "
73 (mapcar (lambda (server) (list server))
74 (cons (list gnus-nntp-server)
75 gnus-secondary-servers))
76 nil nil gnus-nntp-server)))
78 (when (and gnus-nntp-server
79 (stringp gnus-nntp-server)
80 (not (string= gnus-nntp-server "")))
81 (setq gnus-select-method
82 (cond ((or (string= gnus-nntp-server "")
83 (string= gnus-nntp-server "::"))
84 (list 'nnspool (system-name)))
85 ((string-match "^:" gnus-nntp-server)
86 (list 'nnmh gnus-nntp-server
88 (file-name-as-directory
90 (substring gnus-nntp-server 1) "~/")))
91 (list 'nnmh-get-new-mail nil)))
93 (list 'nntp gnus-nntp-server)))))
95 (setq how (car gnus-select-method))
99 (gnus-message 5 "Looking up local news spool..."))
102 (gnus-message 5 "Looking up mh spool..."))
105 (setq gnus-current-select-method gnus-select-method)
106 (gnus-run-hooks 'gnus-open-server-hook)
108 ;; gnus-open-server-hook might have opened it
109 (gnus-server-opened gnus-select-method)
110 (gnus-open-server gnus-select-method)
114 "%s (%s) open error: '%s'. Continue? "
115 (car gnus-select-method) (cadr gnus-select-method)
116 (gnus-status-message gnus-select-method)))
117 (gnus-error 1 "Couldn't open server on %s"
118 (nth 1 gnus-select-method))))))
120 (defun gnus-check-group (group)
121 "Try to make sure that the server where GROUP exists is alive."
122 (let ((method (gnus-find-method-for-group group)))
123 (or (gnus-server-opened method)
124 (gnus-open-server method))))
126 (defun gnus-check-server (&optional method silent)
127 "Check whether the connection to METHOD is down.
128 If METHOD is nil, use `gnus-select-method'.
129 If it is down, start it up (again)."
130 (let ((method (or method gnus-select-method))
132 ;; Transform virtual server names into select methods.
133 (when (stringp method)
134 (setq method (gnus-server-to-method method)))
135 (if (gnus-server-opened method)
136 ;; The stream is already opened.
140 (gnus-message 5 "Opening %s server%s..." (car method)
141 (if (equal (nth 1 method) "") ""
142 (format " on %s" (nth 1 method)))))
143 (gnus-run-hooks 'gnus-open-server-hook)
146 (setq result (gnus-open-server method))
147 (quit (message "Quit gnus-check-server")
150 (gnus-message 5 "Opening %s server%s...%s" (car method)
151 (if (equal (nth 1 method) "") ""
152 (format " on %s" (nth 1 method)))
153 (if result "done" "failed")))))))
155 (defun gnus-get-function (method function &optional noerror)
156 "Return a function symbol based on METHOD and FUNCTION."
157 ;; Translate server names into methods.
159 (error "Attempted use of a nil select method"))
160 (when (stringp method)
161 (setq method (gnus-server-to-method method)))
162 ;; Check cache of constructed names.
163 (let* ((method-sym (if gnus-agent
164 (gnus-agent-get-function method)
166 (method-fns (get method-sym 'gnus-method-functions))
167 (func (let ((method-fnlist-elt (assq function method-fns)))
168 (unless method-fnlist-elt
169 (setq method-fnlist-elt
171 (intern (format "%s-%s" method-sym function))))
172 (put method-sym 'gnus-method-functions
173 (cons method-fnlist-elt method-fns)))
174 (cdr method-fnlist-elt))))
175 ;; Maybe complain if there is no function.
176 (unless (fboundp func)
178 (error "Trying to require a method that doesn't exist"))
179 (require (car method))
180 (when (not (fboundp func))
183 (error "No such function: %s" func))))
188 ;;; Interface functions to the backends.
191 (defun gnus-open-server (gnus-command-method)
192 "Open a connection to GNUS-COMMAND-METHOD."
193 (when (stringp gnus-command-method)
194 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
195 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
196 ;; If this method was previously denied, we just return nil.
197 (if (eq (nth 1 elem) 'denied)
199 (gnus-message 1 "Denied server")
204 (funcall (gnus-get-function gnus-command-method 'open-server)
205 (nth 1 gnus-command-method)
206 (nthcdr 2 gnus-command-method))
208 (message "Quit trying to open server")
210 ;; If this hasn't been opened before, we add it to the list.
212 (setq elem (list gnus-command-method nil)
213 gnus-opened-servers (cons elem gnus-opened-servers)))
214 ;; Set the status of this server.
217 (if (eq (cadr elem) 'offline)
221 (not (eq (cadr elem) 'offline))
222 (gnus-agent-method-p gnus-command-method))
223 (or gnus-server-unopen-status
225 (format "Unable to open %s:%s, go offline? "
226 (car gnus-command-method)
227 (cadr gnus-command-method)))
231 ;; Return the result from the "open" call.
232 (cond ((eq (cadr elem) 'offline)
233 ;; I'm avoiding infinite recursion by binding unopen
234 ;; status to denied (The logic of this routine
235 ;; guarantees that I can't get to this point with
236 ;; unopen status already bound to denied).
237 (unless (eq gnus-server-unopen-status 'denied)
238 (let ((gnus-server-unopen-status 'denied))
239 (gnus-open-server gnus-command-method)))
244 (defun gnus-close-server (gnus-command-method)
245 "Close the connection to GNUS-COMMAND-METHOD."
246 (when (stringp gnus-command-method)
247 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
248 (funcall (gnus-get-function gnus-command-method 'close-server)
249 (nth 1 gnus-command-method)))
251 (defun gnus-request-list (gnus-command-method)
252 "Request the active file from GNUS-COMMAND-METHOD."
253 (when (stringp gnus-command-method)
254 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
255 (funcall (gnus-get-function gnus-command-method 'request-list)
256 (nth 1 gnus-command-method)))
258 (defun gnus-request-list-newsgroups (gnus-command-method)
259 "Request the newsgroups file from GNUS-COMMAND-METHOD."
260 (when (stringp gnus-command-method)
261 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
262 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
263 (nth 1 gnus-command-method)))
265 (defun gnus-request-newgroups (date gnus-command-method)
266 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
267 (when (stringp gnus-command-method)
268 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
269 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
271 (funcall func date (nth 1 gnus-command-method)))))
273 (defun gnus-server-opened (gnus-command-method)
274 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
275 (unless (eq (gnus-server-status gnus-command-method)
277 (when (stringp gnus-command-method)
278 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
279 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
280 (nth 1 gnus-command-method))))
282 (defun gnus-status-message (gnus-command-method)
283 "Return the status message from GNUS-COMMAND-METHOD.
284 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
285 this group uses will be queried."
286 (let ((gnus-command-method
287 (if (stringp gnus-command-method)
288 (gnus-find-method-for-group gnus-command-method)
289 gnus-command-method)))
290 (funcall (gnus-get-function gnus-command-method 'status-message)
291 (nth 1 gnus-command-method))))
293 (defun gnus-request-regenerate (gnus-command-method)
294 "Request a data generation from GNUS-COMMAND-METHOD."
295 (when (stringp gnus-command-method)
296 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
297 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
298 (nth 1 gnus-command-method)))
300 (defun gnus-request-group (group &optional dont-check gnus-command-method)
301 "Request GROUP. If DONT-CHECK, no information is required."
302 (let ((gnus-command-method
303 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
304 (when (stringp gnus-command-method)
305 (setq gnus-command-method
306 (inline (gnus-server-to-method gnus-command-method))))
307 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
308 (gnus-group-real-name group) (nth 1 gnus-command-method)
311 (defun gnus-list-active-group (group)
312 "Request active information on GROUP."
313 (let ((gnus-command-method (gnus-find-method-for-group group))
314 (func 'list-active-group))
315 (when (gnus-check-backend-function func group)
316 (funcall (gnus-get-function gnus-command-method func)
317 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
319 (defun gnus-request-group-description (group)
320 "Request a description of GROUP."
321 (let ((gnus-command-method (gnus-find-method-for-group group))
322 (func 'request-group-description))
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-articles (group)
328 "Request a list of existing articles in GROUP."
329 (let ((gnus-command-method (gnus-find-method-for-group group))
330 (func 'request-group-articles))
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-close-group (group)
336 "Request the GROUP be closed."
337 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
338 (funcall (gnus-get-function gnus-command-method 'close-group)
339 (gnus-group-real-name group) (nth 1 gnus-command-method))))
341 (defun gnus-retrieve-headers (articles group &optional fetch-old)
342 "Request headers for ARTICLES in GROUP.
343 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
344 (let ((gnus-command-method (gnus-find-method-for-group group)))
346 ((and gnus-use-cache (numberp (car articles)))
347 (gnus-cache-retrieve-headers articles group fetch-old))
348 ((and gnus-agent gnus-agent-cache (gnus-online gnus-command-method)
349 (gnus-agent-method-p gnus-command-method))
350 (gnus-agent-retrieve-headers articles group fetch-old))
352 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
353 articles (gnus-group-real-name group)
354 (nth 1 gnus-command-method) fetch-old)))))
356 (defun gnus-retrieve-articles (articles group)
357 "Request ARTICLES in GROUP."
358 (let ((gnus-command-method (gnus-find-method-for-group group)))
359 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
360 articles (gnus-group-real-name group)
361 (nth 1 gnus-command-method))))
363 (defun gnus-retrieve-groups (groups gnus-command-method)
364 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
365 (when (stringp gnus-command-method)
366 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
367 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
368 groups (nth 1 gnus-command-method)))
370 (defun gnus-request-type (group &optional article)
371 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
372 (let ((gnus-command-method (gnus-find-method-for-group group)))
373 (if (not (gnus-check-backend-function
374 'request-type (car gnus-command-method)))
376 (funcall (gnus-get-function gnus-command-method 'request-type)
377 (gnus-group-real-name group) article))))
379 (defun gnus-request-set-mark (group action)
380 "Set marks on articles in the backend."
381 (let ((gnus-command-method (gnus-find-method-for-group group)))
382 (if (not (gnus-check-backend-function
383 'request-set-mark (car gnus-command-method)))
385 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
386 (gnus-group-real-name group) action
387 (nth 1 gnus-command-method)))))
389 (defun gnus-request-update-mark (group article mark)
390 "Allow the backend to change the mark the user tries to put on an article."
391 (let ((gnus-command-method (gnus-find-method-for-group group)))
392 (if (not (gnus-check-backend-function
393 'request-update-mark (car gnus-command-method)))
395 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
396 (gnus-group-real-name group) article mark))))
398 (defun gnus-request-article (article group &optional buffer)
399 "Request the ARTICLE in GROUP.
400 ARTICLE can either be an article number or an article Message-ID.
401 If BUFFER, insert the article in that group."
402 (let ((gnus-command-method (gnus-find-method-for-group group)))
403 (funcall (gnus-get-function gnus-command-method 'request-article)
404 article (gnus-group-real-name group)
405 (nth 1 gnus-command-method) buffer)))
407 (defun gnus-request-head (article group)
408 "Request the head of ARTICLE in GROUP."
409 (let* ((gnus-command-method (gnus-find-method-for-group group))
410 (head (gnus-get-function gnus-command-method 'request-head t))
416 (gnus-cache-request-article article group))
417 (setq res (cons group article)
419 ;; Check the agent cache.
420 ((and gnus-agent gnus-agent-cache gnus-plugged
422 (gnus-agent-request-article article group))
423 (setq res (cons group article)
425 ;; Use `head' function.
427 (setq res (funcall head article (gnus-group-real-name group)
428 (nth 1 gnus-command-method))))
429 ;; Use `article' function.
431 (setq res (gnus-request-article article group)
435 (set-buffer nntp-server-buffer)
436 (goto-char (point-min))
437 (when (search-forward "\n\n" nil t)
438 (delete-region (1- (point)) (point-max)))
439 (nnheader-fold-continuation-lines)))
442 (defun gnus-request-body (article group)
443 "Request the body of ARTICLE in GROUP."
444 (let* ((gnus-command-method (gnus-find-method-for-group group))
445 (head (gnus-get-function gnus-command-method 'request-body t))
451 (gnus-cache-request-article article group))
452 (setq res (cons group article)
454 ;; Check the agent cache.
455 ((and gnus-agent gnus-agent-cache gnus-plugged
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)))
491 (funcall (gnus-get-function gnus-command-method 'request-scan)
492 (and group (gnus-group-real-name group))
493 (nth 1 gnus-command-method)))))
495 (defsubst gnus-request-update-info (info gnus-command-method)
496 "Request that GNUS-COMMAND-METHOD update INFO."
497 (when (stringp gnus-command-method)
498 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
499 (when (gnus-check-backend-function
500 'request-update-info (car gnus-command-method))
501 (let ((group (gnus-info-group info)))
502 (and (funcall (gnus-get-function gnus-command-method
503 'request-update-info)
504 (gnus-group-real-name group)
505 info (nth 1 gnus-command-method))
506 ;; If the minimum article number is greater than 1, then all
507 ;; smaller article numbers are known not to exist; we'll
508 ;; artificially add those to the 'read range.
509 (let* ((active (gnus-active group))
512 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
513 (read (gnus-info-read info))
514 (new-read (gnus-range-add read (list range))))
515 (gnus-info-set-read info new-read)))
518 (defun gnus-request-expire-articles (articles group &optional force)
519 (let* ((gnus-command-method (gnus-find-method-for-group group))
522 (gnus-get-function gnus-command-method 'request-expire-articles)
523 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
525 (when (and gnus-agent gnus-agent-cache (gnus-agent-method-p gnus-command-method))
526 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
527 (when expired-articles
528 (gnus-agent-expire expired-articles group 'force))))
531 (defun gnus-request-move-article (article group server accept-function &optional last)
532 (let* ((gnus-command-method (gnus-find-method-for-group group))
533 (result (funcall (gnus-get-function gnus-command-method 'request-move-article)
534 article (gnus-group-real-name group)
535 (nth 1 gnus-command-method) accept-function last)))
536 (when (and result gnus-agent gnus-agent-cache (gnus-agent-method-p gnus-command-method))
537 (gnus-agent-expire (list article) group 'force))
540 (defun gnus-request-accept-article (group &optional gnus-command-method last
542 ;; Make sure there's a newline at the end of the article.
543 (when (stringp gnus-command-method)
544 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
545 (when (and (not gnus-command-method)
547 (setq gnus-command-method (gnus-group-name-to-method group)))
548 (goto-char (point-max))
551 (let ((gnus-command-method (or gnus-command-method
552 (gnus-find-method-for-group group))))
553 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
554 (if (stringp group) (gnus-group-real-name group) group)
555 (cadr gnus-command-method)
558 (defun gnus-request-replace-article (article group buffer &optional no-encode)
559 (let ((func (car (gnus-group-name-to-method group))))
560 (funcall (intern (format "%s-request-replace-article" func))
561 article (gnus-group-real-name group) buffer)))
563 (defun gnus-request-associate-buffer (group)
564 (let ((gnus-command-method (gnus-find-method-for-group group)))
565 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
566 (gnus-group-real-name group))))
568 (defun gnus-request-restore-buffer (article group)
569 "Request a new buffer restored to the state of ARTICLE."
570 (let ((gnus-command-method (gnus-find-method-for-group group)))
571 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
572 article (gnus-group-real-name group)
573 (nth 1 gnus-command-method))))
575 (defun gnus-request-create-group (group &optional gnus-command-method args)
576 (when (stringp gnus-command-method)
577 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
578 (let ((gnus-command-method
579 (or gnus-command-method (gnus-find-method-for-group group))))
580 (funcall (gnus-get-function gnus-command-method 'request-create-group)
581 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
583 (defun gnus-request-delete-group (group &optional force)
584 (let ((gnus-command-method (gnus-find-method-for-group group)))
585 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
586 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
588 (defun gnus-request-rename-group (group new-name)
589 (let ((gnus-command-method (gnus-find-method-for-group group)))
590 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
591 (gnus-group-real-name group)
592 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
594 (defun gnus-close-backends ()
595 ;; Send a close request to all backends that support such a request.
596 (let ((methods gnus-valid-select-methods)
597 (gnus-inhibit-demon t)
598 func gnus-command-method)
599 (while (setq gnus-command-method (pop methods))
600 (when (fboundp (setq func (intern
601 (concat (car gnus-command-method)
605 (defun gnus-asynchronous-p (gnus-command-method)
606 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
610 (defun gnus-remove-denial (gnus-command-method)
611 (when (stringp gnus-command-method)
612 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
613 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
614 (status (cadr elem)))
615 ;; If this hasn't been opened before, we add it to the list.
616 (when (eq status 'denied)
617 ;; Set the status of this server.
618 (setcar (cdr elem) 'closed))))
622 ;;; gnus-int.el ends here