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))
34 (defcustom gnus-open-server-hook nil
35 "Hook called just before opening connection to the news server."
39 (defcustom gnus-server-unopen-status nil
40 "The default status if the server is not able to open.
41 If the server is covered by Gnus agent, the possible values are
42 `denied', set the server denied; `offline', set the server offline;
43 `nil', ask user. If the server is not covered by Gnus agent, set the
46 :type '(choice (const :tag "Ask" nil)
47 (const :tag "Deny server" denied)
48 (const :tag "Unplugg Agent" offline)))
51 ;;; Server Communication
54 (defun gnus-start-news-server (&optional confirm)
55 "Open a method for getting news.
56 If CONFIRM is non-nil, the user will be asked for an NNTP server."
58 (if gnus-current-select-method
59 ;; Stream is already opened.
62 (unless gnus-nntp-service
63 (setq gnus-nntp-server nil))
65 ;; Read server name with completion.
66 (setq gnus-nntp-server
67 (completing-read "NNTP server: "
68 (mapcar (lambda (server) (list server))
69 (cons (list gnus-nntp-server)
70 gnus-secondary-servers))
71 nil nil gnus-nntp-server)))
73 (when (and gnus-nntp-server
74 (stringp gnus-nntp-server)
75 (not (string= gnus-nntp-server "")))
76 (setq gnus-select-method
77 (cond ((or (string= gnus-nntp-server "")
78 (string= gnus-nntp-server "::"))
79 (list 'nnspool (system-name)))
80 ((string-match "^:" gnus-nntp-server)
81 (list 'nnmh gnus-nntp-server
83 (file-name-as-directory
85 (substring gnus-nntp-server 1) "~/")))
86 (list 'nnmh-get-new-mail nil)))
88 (list 'nntp gnus-nntp-server)))))
90 (setq how (car gnus-select-method))
94 (gnus-message 5 "Looking up local news spool..."))
97 (gnus-message 5 "Looking up mh spool..."))
100 (setq gnus-current-select-method gnus-select-method)
101 (gnus-run-hooks 'gnus-open-server-hook)
103 ;; gnus-open-server-hook might have opened it
104 (gnus-server-opened gnus-select-method)
105 (gnus-open-server gnus-select-method)
109 "%s (%s) open error: '%s'. Continue? "
110 (car gnus-select-method) (cadr gnus-select-method)
111 (gnus-status-message gnus-select-method)))
112 (gnus-error 1 "Couldn't open server on %s"
113 (nth 1 gnus-select-method))))))
115 (defun gnus-check-group (group)
116 "Try to make sure that the server where GROUP exists is alive."
117 (let ((method (gnus-find-method-for-group group)))
118 (or (gnus-server-opened method)
119 (gnus-open-server method))))
121 (defun gnus-check-server (&optional method silent)
122 "Check whether the connection to METHOD is down.
123 If METHOD is nil, use `gnus-select-method'.
124 If it is down, start it up (again)."
125 (let ((method (or method gnus-select-method))
127 ;; Transform virtual server names into select methods.
128 (when (stringp method)
129 (setq method (gnus-server-to-method method)))
130 (if (gnus-server-opened method)
131 ;; The stream is already opened.
135 (gnus-message 5 "Opening %s server%s..." (car method)
136 (if (equal (nth 1 method) "") ""
137 (format " on %s" (nth 1 method)))))
138 (gnus-run-hooks 'gnus-open-server-hook)
141 (setq result (gnus-open-server method))
142 (quit (message "Quit gnus-check-server")
145 (gnus-message 5 "Opening %s server%s...%s" (car method)
146 (if (equal (nth 1 method) "") ""
147 (format " on %s" (nth 1 method)))
148 (if result "done" "failed")))))))
150 (defun gnus-get-function (method function &optional noerror)
151 "Return a function symbol based on METHOD and FUNCTION."
152 ;; Translate server names into methods.
154 (error "Attempted use of a nil select method"))
155 (when (stringp method)
156 (setq method (gnus-server-to-method method)))
157 ;; Check cache of constructed names.
158 (let* ((method-sym (if gnus-agent
159 (gnus-agent-get-function method)
161 (method-fns (get method-sym 'gnus-method-functions))
162 (func (let ((method-fnlist-elt (assq function method-fns)))
163 (unless method-fnlist-elt
164 (setq method-fnlist-elt
166 (intern (format "%s-%s" method-sym function))))
167 (put method-sym 'gnus-method-functions
168 (cons method-fnlist-elt method-fns)))
169 (cdr method-fnlist-elt))))
170 ;; Maybe complain if there is no function.
171 (unless (fboundp func)
173 (error "Trying to require a method that doesn't exist"))
174 (require (car method))
175 (when (not (fboundp func))
178 (error "No such function: %s" func))))
183 ;;; Interface functions to the backends.
186 (defun gnus-open-server (gnus-command-method)
187 "Open a connection to GNUS-COMMAND-METHOD."
188 (when (stringp gnus-command-method)
189 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
190 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
191 ;; If this method was previously denied, we just return nil.
192 (if (eq (nth 1 elem) 'denied)
194 (gnus-message 1 "Denied server")
199 (funcall (gnus-get-function gnus-command-method 'open-server)
200 (nth 1 gnus-command-method)
201 (nthcdr 2 gnus-command-method))
203 (message "Quit trying to open server")
205 ;; If this hasn't been opened before, we add it to the list.
207 (setq elem (list gnus-command-method nil)
208 gnus-opened-servers (cons elem gnus-opened-servers)))
209 ;; Set the status of this server.
212 (if (eq (cadr elem) 'offline)
216 (not (eq (cadr elem) 'offline))
217 (gnus-agent-method-p gnus-command-method))
218 (or gnus-server-unopen-status
220 (format "Unable to open %s:%s, go offline? "
221 (car gnus-command-method)
222 (cadr gnus-command-method)))
226 ;; Return the result from the "open" call.
227 (or (eq (cadr elem) 'offline)
230 (defun gnus-close-server (gnus-command-method)
231 "Close the connection to GNUS-COMMAND-METHOD."
232 (when (stringp gnus-command-method)
233 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
234 (funcall (gnus-get-function gnus-command-method 'close-server)
235 (nth 1 gnus-command-method)))
237 (defun gnus-request-list (gnus-command-method)
238 "Request the active file from GNUS-COMMAND-METHOD."
239 (when (stringp gnus-command-method)
240 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
241 (funcall (gnus-get-function gnus-command-method 'request-list)
242 (nth 1 gnus-command-method)))
244 (defun gnus-request-list-newsgroups (gnus-command-method)
245 "Request the newsgroups file from 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 'request-list-newsgroups)
249 (nth 1 gnus-command-method)))
251 (defun gnus-request-newgroups (date gnus-command-method)
252 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
253 (when (stringp gnus-command-method)
254 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
255 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
257 (funcall func date (nth 1 gnus-command-method)))))
259 (defun gnus-server-opened (gnus-command-method)
260 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
261 (unless (eq (gnus-server-status gnus-command-method)
263 (when (stringp gnus-command-method)
264 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
265 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
266 (nth 1 gnus-command-method))))
268 (defun gnus-status-message (gnus-command-method)
269 "Return the status message from GNUS-COMMAND-METHOD.
270 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
271 this group uses will be queried."
272 (let ((gnus-command-method
273 (if (stringp gnus-command-method)
274 (gnus-find-method-for-group gnus-command-method)
275 gnus-command-method)))
276 (funcall (gnus-get-function gnus-command-method 'status-message)
277 (nth 1 gnus-command-method))))
279 (defun gnus-request-regenerate (gnus-command-method)
280 "Request a data generation from GNUS-COMMAND-METHOD."
281 (when (stringp gnus-command-method)
282 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
283 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
284 (nth 1 gnus-command-method)))
286 (defun gnus-request-group (group &optional dont-check gnus-command-method)
287 "Request GROUP. If DONT-CHECK, no information is required."
288 (let ((gnus-command-method
289 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
290 (when (stringp gnus-command-method)
291 (setq gnus-command-method
292 (inline (gnus-server-to-method gnus-command-method))))
293 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
294 (gnus-group-real-name group) (nth 1 gnus-command-method)
297 (defun gnus-list-active-group (group)
298 "Request active information on GROUP."
299 (let ((gnus-command-method (gnus-find-method-for-group group))
300 (func 'list-active-group))
301 (when (gnus-check-backend-function func group)
302 (funcall (gnus-get-function gnus-command-method func)
303 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
305 (defun gnus-request-group-description (group)
306 "Request a description of GROUP."
307 (let ((gnus-command-method (gnus-find-method-for-group group))
308 (func 'request-group-description))
309 (when (gnus-check-backend-function func group)
310 (funcall (gnus-get-function gnus-command-method func)
311 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
313 (defun gnus-request-group-articles (group)
314 "Request a list of existing articles in GROUP."
315 (let ((gnus-command-method (gnus-find-method-for-group group))
316 (func 'request-group-articles))
317 (when (gnus-check-backend-function func group)
318 (funcall (gnus-get-function gnus-command-method func)
319 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
321 (defun gnus-close-group (group)
322 "Request the GROUP be closed."
323 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
324 (funcall (gnus-get-function gnus-command-method 'close-group)
325 (gnus-group-real-name group) (nth 1 gnus-command-method))))
327 (defun gnus-retrieve-headers (articles group &optional fetch-old)
328 "Request headers for ARTICLES in GROUP.
329 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
330 (let ((gnus-command-method (gnus-find-method-for-group group)))
332 ((and gnus-use-cache (numberp (car articles)))
333 (gnus-cache-retrieve-headers articles group fetch-old))
334 ((and gnus-agent gnus-agent-cache (gnus-online gnus-command-method)
335 (gnus-agent-method-p gnus-command-method))
336 (gnus-agent-retrieve-headers articles group fetch-old))
338 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
339 articles (gnus-group-real-name group)
340 (nth 1 gnus-command-method) fetch-old)))))
342 (defun gnus-retrieve-articles (articles group)
343 "Request ARTICLES in GROUP."
344 (let ((gnus-command-method (gnus-find-method-for-group group)))
345 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
346 articles (gnus-group-real-name group)
347 (nth 1 gnus-command-method))))
349 (defun gnus-retrieve-groups (groups gnus-command-method)
350 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
351 (when (stringp gnus-command-method)
352 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
353 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
354 groups (nth 1 gnus-command-method)))
356 (defun gnus-request-type (group &optional article)
357 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
358 (let ((gnus-command-method (gnus-find-method-for-group group)))
359 (if (not (gnus-check-backend-function
360 'request-type (car gnus-command-method)))
362 (funcall (gnus-get-function gnus-command-method 'request-type)
363 (gnus-group-real-name group) article))))
365 (defun gnus-request-set-mark (group action)
366 "Set marks on articles in the backend."
367 (let ((gnus-command-method (gnus-find-method-for-group group)))
368 (if (not (gnus-check-backend-function
369 'request-set-mark (car gnus-command-method)))
371 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
372 (gnus-group-real-name group) action
373 (nth 1 gnus-command-method)))))
375 (defun gnus-request-update-mark (group article mark)
376 "Allow the backend to change the mark the user tries to put on an article."
377 (let ((gnus-command-method (gnus-find-method-for-group group)))
378 (if (not (gnus-check-backend-function
379 'request-update-mark (car gnus-command-method)))
381 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
382 (gnus-group-real-name group) article mark))))
384 (defun gnus-request-article (article group &optional buffer)
385 "Request the ARTICLE in GROUP.
386 ARTICLE can either be an article number or an article Message-ID.
387 If BUFFER, insert the article in that group."
388 (let ((gnus-command-method (gnus-find-method-for-group group)))
389 (funcall (gnus-get-function gnus-command-method 'request-article)
390 article (gnus-group-real-name group)
391 (nth 1 gnus-command-method) buffer)))
393 (defun gnus-request-head (article group)
394 "Request the head of ARTICLE in GROUP."
395 (let* ((gnus-command-method (gnus-find-method-for-group group))
396 (head (gnus-get-function gnus-command-method 'request-head t))
402 (gnus-cache-request-article article group))
403 (setq res (cons group article)
405 ;; Check the agent cache.
406 ((and gnus-agent gnus-agent-cache gnus-plugged
408 (gnus-agent-request-article article group))
409 (setq res (cons group article)
411 ;; Use `head' function.
413 (setq res (funcall head article (gnus-group-real-name group)
414 (nth 1 gnus-command-method))))
415 ;; Use `article' function.
417 (setq res (gnus-request-article article group)
421 (set-buffer nntp-server-buffer)
422 (goto-char (point-min))
423 (when (search-forward "\n\n" nil t)
424 (delete-region (1- (point)) (point-max)))
425 (nnheader-fold-continuation-lines)))
428 (defun gnus-request-body (article group)
429 "Request the body of ARTICLE in GROUP."
430 (let* ((gnus-command-method (gnus-find-method-for-group group))
431 (head (gnus-get-function gnus-command-method 'request-body t))
437 (gnus-cache-request-article article group))
438 (setq res (cons group article)
440 ;; Check the agent cache.
441 ((and gnus-agent gnus-agent-cache gnus-plugged
443 (gnus-agent-request-article article group))
444 (setq res (cons group article)
446 ;; Use `head' function.
448 (setq res (funcall head article (gnus-group-real-name group)
449 (nth 1 gnus-command-method))))
450 ;; Use `article' function.
452 (setq res (gnus-request-article article group)
456 (set-buffer nntp-server-buffer)
457 (goto-char (point-min))
458 (when (search-forward "\n\n" nil t)
459 (delete-region (point-min) (1- (point))))))
462 (defun gnus-request-post (gnus-command-method)
463 "Post the current buffer using GNUS-COMMAND-METHOD."
464 (when (stringp gnus-command-method)
465 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
466 (funcall (gnus-get-function gnus-command-method 'request-post)
467 (nth 1 gnus-command-method)))
469 (defun gnus-request-scan (group gnus-command-method)
470 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
471 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
472 (let ((gnus-command-method
473 (if group (gnus-find-method-for-group group) gnus-command-method))
474 (gnus-inhibit-demon t)
475 (mail-source-plugged gnus-plugged))
476 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
477 (funcall (gnus-get-function gnus-command-method 'request-scan)
478 (and group (gnus-group-real-name group))
479 (nth 1 gnus-command-method)))))
481 (defsubst gnus-request-update-info (info gnus-command-method)
482 "Request that GNUS-COMMAND-METHOD update INFO."
483 (when (stringp gnus-command-method)
484 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
485 (when (gnus-check-backend-function
486 'request-update-info (car gnus-command-method))
487 (let ((group (gnus-info-group info)))
488 (and (funcall (gnus-get-function gnus-command-method
489 'request-update-info)
490 (gnus-group-real-name group)
491 info (nth 1 gnus-command-method))
492 ;; If the minimum article number is greater than 1, then all
493 ;; smaller article numbers are known not to exist; we'll
494 ;; artificially add those to the 'read range.
495 (let* ((active (gnus-active group))
498 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
499 (read (gnus-info-read info))
500 (new-read (gnus-range-add read (list range))))
501 (gnus-info-set-read info new-read)))
504 (defun gnus-request-expire-articles (articles group &optional force)
505 (let* ((gnus-command-method (gnus-find-method-for-group group))
508 (gnus-get-function gnus-command-method 'request-expire-articles)
509 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
511 (when (and gnus-agent gnus-agent-cache
512 (gnus-sorted-difference articles not-deleted))
513 (gnus-agent-expire (gnus-sorted-difference articles not-deleted)
517 (defun gnus-request-move-article (article group server accept-function &optional last)
518 (let* ((gnus-command-method (gnus-find-method-for-group group))
519 (result (funcall (gnus-get-function gnus-command-method 'request-move-article)
520 article (gnus-group-real-name group)
521 (nth 1 gnus-command-method) accept-function last)))
522 (when (and result gnus-agent gnus-agent-cache)
523 (gnus-agent-expire (list article) group 'force))
526 (defun gnus-request-accept-article (group &optional gnus-command-method last
528 ;; Make sure there's a newline at the end of the article.
529 (when (stringp gnus-command-method)
530 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
531 (when (and (not gnus-command-method)
533 (setq gnus-command-method (gnus-group-name-to-method group)))
534 (goto-char (point-max))
537 (let ((gnus-command-method (or gnus-command-method
538 (gnus-find-method-for-group group))))
539 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
540 (if (stringp group) (gnus-group-real-name group) group)
541 (cadr gnus-command-method)
544 (defun gnus-request-replace-article (article group buffer &optional no-encode)
545 (let ((func (car (gnus-group-name-to-method group))))
546 (funcall (intern (format "%s-request-replace-article" func))
547 article (gnus-group-real-name group) buffer)))
549 (defun gnus-request-associate-buffer (group)
550 (let ((gnus-command-method (gnus-find-method-for-group group)))
551 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
552 (gnus-group-real-name group))))
554 (defun gnus-request-restore-buffer (article group)
555 "Request a new buffer restored to the state of ARTICLE."
556 (let ((gnus-command-method (gnus-find-method-for-group group)))
557 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
558 article (gnus-group-real-name group)
559 (nth 1 gnus-command-method))))
561 (defun gnus-request-create-group (group &optional gnus-command-method args)
562 (when (stringp gnus-command-method)
563 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
564 (let ((gnus-command-method
565 (or gnus-command-method (gnus-find-method-for-group group))))
566 (funcall (gnus-get-function gnus-command-method 'request-create-group)
567 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
569 (defun gnus-request-delete-group (group &optional force)
570 (let ((gnus-command-method (gnus-find-method-for-group group)))
571 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
572 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
574 (defun gnus-request-rename-group (group new-name)
575 (let ((gnus-command-method (gnus-find-method-for-group group)))
576 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
577 (gnus-group-real-name group)
578 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
580 (defun gnus-close-backends ()
581 ;; Send a close request to all backends that support such a request.
582 (let ((methods gnus-valid-select-methods)
583 (gnus-inhibit-demon t)
584 func gnus-command-method)
585 (while (setq gnus-command-method (pop methods))
586 (when (fboundp (setq func (intern
587 (concat (car gnus-command-method)
591 (defun gnus-asynchronous-p (gnus-command-method)
592 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
596 (defun gnus-remove-denial (gnus-command-method)
597 (when (stringp gnus-command-method)
598 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
599 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
600 (status (cadr elem)))
601 ;; If this hasn't been opened before, we add it to the list.
602 (when (eq status 'denied)
603 ;; Set the status of this server.
604 (setcar (cdr elem) 'closed))))
608 ;;; gnus-int.el ends here