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>
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 "Unplugg Agent" offline)))
55 ;;; Server Communication
58 (defun gnus-start-news-server (&optional confirm)
59 "Open a method for getting news.
60 If CONFIRM is non-nil, the user will be asked for an NNTP server."
62 (if gnus-current-select-method
63 ;; Stream is already opened.
66 (unless gnus-nntp-service
67 (setq gnus-nntp-server nil))
69 ;; Read server name with completion.
70 (setq gnus-nntp-server
71 (completing-read "NNTP server: "
72 (mapcar (lambda (server) (list server))
73 (cons (list gnus-nntp-server)
74 gnus-secondary-servers))
75 nil nil gnus-nntp-server)))
77 (when (and gnus-nntp-server
78 (stringp gnus-nntp-server)
79 (not (string= gnus-nntp-server "")))
80 (setq gnus-select-method
81 (cond ((or (string= gnus-nntp-server "")
82 (string= gnus-nntp-server "::"))
83 (list 'nnspool (system-name)))
84 ((string-match "^:" gnus-nntp-server)
85 (list 'nnmh gnus-nntp-server
87 (file-name-as-directory
89 (substring gnus-nntp-server 1) "~/")))
90 (list 'nnmh-get-new-mail nil)))
92 (list 'nntp gnus-nntp-server)))))
94 (setq how (car gnus-select-method))
98 (gnus-message 5 "Looking up local news spool..."))
101 (gnus-message 5 "Looking up mh spool..."))
104 (setq gnus-current-select-method gnus-select-method)
105 (gnus-run-hooks 'gnus-open-server-hook)
107 ;; gnus-open-server-hook might have opened it
108 (gnus-server-opened gnus-select-method)
109 (gnus-open-server gnus-select-method)
113 "%s (%s) open error: '%s'. Continue? "
114 (car gnus-select-method) (cadr gnus-select-method)
115 (gnus-status-message gnus-select-method)))
116 (gnus-error 1 "Couldn't open server on %s"
117 (nth 1 gnus-select-method))))))
119 (defun gnus-check-group (group)
120 "Try to make sure that the server where GROUP exists is alive."
121 (let ((method (gnus-find-method-for-group group)))
122 (or (gnus-server-opened method)
123 (gnus-open-server method))))
125 (defun gnus-check-server (&optional method silent)
126 "Check whether the connection to METHOD is down.
127 If METHOD is nil, use `gnus-select-method'.
128 If it is down, start it up (again)."
129 (let ((method (or method gnus-select-method))
131 ;; Transform virtual server names into select methods.
132 (when (stringp method)
133 (setq method (gnus-server-to-method method)))
134 (if (gnus-server-opened method)
135 ;; The stream is already opened.
139 (gnus-message 5 "Opening %s server%s..." (car method)
140 (if (equal (nth 1 method) "") ""
141 (format " on %s" (nth 1 method)))))
142 (gnus-run-hooks 'gnus-open-server-hook)
145 (setq result (gnus-open-server method))
146 (quit (message "Quit gnus-check-server")
149 (gnus-message 5 "Opening %s server%s...%s" (car method)
150 (if (equal (nth 1 method) "") ""
151 (format " on %s" (nth 1 method)))
152 (if result "done" "failed")))))))
154 (defun gnus-get-function (method function &optional noerror)
155 "Return a function symbol based on METHOD and FUNCTION."
156 ;; Translate server names into methods.
158 (error "Attempted use of a nil select method"))
159 (when (stringp method)
160 (setq method (gnus-server-to-method method)))
161 ;; Check cache of constructed names.
162 (let* ((method-sym (if gnus-agent
163 (gnus-agent-get-function method)
165 (method-fns (get method-sym 'gnus-method-functions))
166 (func (let ((method-fnlist-elt (assq function method-fns)))
167 (unless method-fnlist-elt
168 (setq method-fnlist-elt
170 (intern (format "%s-%s" method-sym function))))
171 (put method-sym 'gnus-method-functions
172 (cons method-fnlist-elt method-fns)))
173 (cdr method-fnlist-elt))))
174 ;; Maybe complain if there is no function.
175 (unless (fboundp func)
177 (error "Trying to require a method that doesn't exist"))
178 (require (car method))
179 (when (not (fboundp func))
182 (error "No such function: %s" func))))
187 ;;; Interface functions to the backends.
190 (defun gnus-open-server (gnus-command-method)
191 "Open a connection to GNUS-COMMAND-METHOD."
192 (when (stringp gnus-command-method)
193 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
194 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
195 ;; If this method was previously denied, we just return nil.
196 (if (eq (nth 1 elem) 'denied)
198 (gnus-message 1 "Denied server")
203 (funcall (gnus-get-function gnus-command-method 'open-server)
204 (nth 1 gnus-command-method)
205 (nthcdr 2 gnus-command-method))
207 (message "Quit trying to open server")
209 ;; If this hasn't been opened before, we add it to the list.
211 (setq elem (list gnus-command-method nil)
212 gnus-opened-servers (cons elem gnus-opened-servers)))
213 ;; Set the status of this server.
216 (if (eq (cadr elem) 'offline)
220 (not (eq (cadr elem) 'offline))
221 (gnus-agent-method-p gnus-command-method))
222 (or gnus-server-unopen-status
224 (format "Unable to open %s:%s, go offline? "
225 (car gnus-command-method)
226 (cadr gnus-command-method)))
230 ;; Return the result from the "open" call.
231 (cond ((eq (cadr elem) 'offline)
232 ;; I'm avoiding infinite recursion by binding unopen
233 ;; status to denied (The logic of this routine
234 ;; guarantees that I can't get to this point with
235 ;; unopen status already bound to denied).
236 (unless (eq gnus-server-unopen-status 'denied)
237 (let ((gnus-server-unopen-status 'denied))
238 (gnus-open-server gnus-command-method)))
243 (defun gnus-close-server (gnus-command-method)
244 "Close the connection to GNUS-COMMAND-METHOD."
245 (when (stringp gnus-command-method)
246 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
247 (funcall (gnus-get-function gnus-command-method 'close-server)
248 (nth 1 gnus-command-method)))
250 (defun gnus-request-list (gnus-command-method)
251 "Request the active file from 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 'request-list)
255 (nth 1 gnus-command-method)))
257 (defun gnus-request-list-newsgroups (gnus-command-method)
258 "Request the newsgroups 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-newsgroups)
262 (nth 1 gnus-command-method)))
264 (defun gnus-request-newgroups (date gnus-command-method)
265 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
266 (when (stringp gnus-command-method)
267 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
268 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
270 (funcall func date (nth 1 gnus-command-method)))))
272 (defun gnus-server-opened (gnus-command-method)
273 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
274 (unless (eq (gnus-server-status gnus-command-method)
276 (when (stringp gnus-command-method)
277 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
278 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
279 (nth 1 gnus-command-method))))
281 (defun gnus-status-message (gnus-command-method)
282 "Return the status message from GNUS-COMMAND-METHOD.
283 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
284 this group uses will be queried."
285 (let ((gnus-command-method
286 (if (stringp gnus-command-method)
287 (gnus-find-method-for-group gnus-command-method)
288 gnus-command-method)))
289 (funcall (gnus-get-function gnus-command-method 'status-message)
290 (nth 1 gnus-command-method))))
292 (defun gnus-request-regenerate (gnus-command-method)
293 "Request a data generation from GNUS-COMMAND-METHOD."
294 (when (stringp gnus-command-method)
295 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
296 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
297 (nth 1 gnus-command-method)))
299 (defun gnus-request-group (group &optional dont-check gnus-command-method)
300 "Request GROUP. If DONT-CHECK, no information is required."
301 (let ((gnus-command-method
302 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
303 (when (stringp gnus-command-method)
304 (setq gnus-command-method
305 (inline (gnus-server-to-method gnus-command-method))))
306 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
307 (gnus-group-real-name group) (nth 1 gnus-command-method)
310 (defun gnus-list-active-group (group)
311 "Request active information on GROUP."
312 (let ((gnus-command-method (gnus-find-method-for-group group))
313 (func 'list-active-group))
314 (when (gnus-check-backend-function func group)
315 (funcall (gnus-get-function gnus-command-method func)
316 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
318 (defun gnus-request-group-description (group)
319 "Request a description of GROUP."
320 (let ((gnus-command-method (gnus-find-method-for-group group))
321 (func 'request-group-description))
322 (when (gnus-check-backend-function func group)
323 (funcall (gnus-get-function gnus-command-method func)
324 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
326 (defun gnus-request-group-articles (group)
327 "Request a list of existing articles in GROUP."
328 (let ((gnus-command-method (gnus-find-method-for-group group))
329 (func 'request-group-articles))
330 (when (gnus-check-backend-function func group)
331 (funcall (gnus-get-function gnus-command-method func)
332 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
334 (defun gnus-close-group (group)
335 "Request the GROUP be closed."
336 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
337 (funcall (gnus-get-function gnus-command-method 'close-group)
338 (gnus-group-real-name group) (nth 1 gnus-command-method))))
340 (defun gnus-retrieve-headers (articles group &optional fetch-old)
341 "Request headers for ARTICLES in GROUP.
342 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
343 (let ((gnus-command-method (gnus-find-method-for-group group)))
345 ((and gnus-use-cache (numberp (car articles)))
346 (gnus-cache-retrieve-headers articles group fetch-old))
347 ((and gnus-agent gnus-agent-cache (gnus-online gnus-command-method)
348 (gnus-agent-method-p gnus-command-method))
349 (gnus-agent-retrieve-headers articles group fetch-old))
351 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
352 articles (gnus-group-real-name group)
353 (nth 1 gnus-command-method) fetch-old)))))
355 (defun gnus-retrieve-articles (articles group)
356 "Request ARTICLES in GROUP."
357 (let ((gnus-command-method (gnus-find-method-for-group group)))
358 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
359 articles (gnus-group-real-name group)
360 (nth 1 gnus-command-method))))
362 (defun gnus-retrieve-groups (groups gnus-command-method)
363 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
364 (when (stringp gnus-command-method)
365 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
366 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
367 groups (nth 1 gnus-command-method)))
369 (defun gnus-request-type (group &optional article)
370 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
371 (let ((gnus-command-method (gnus-find-method-for-group group)))
372 (if (not (gnus-check-backend-function
373 'request-type (car gnus-command-method)))
375 (funcall (gnus-get-function gnus-command-method 'request-type)
376 (gnus-group-real-name group) article))))
378 (defun gnus-request-set-mark (group action)
379 "Set marks on articles in the backend."
380 (let ((gnus-command-method (gnus-find-method-for-group group)))
381 (if (not (gnus-check-backend-function
382 'request-set-mark (car gnus-command-method)))
384 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
385 (gnus-group-real-name group) action
386 (nth 1 gnus-command-method)))))
388 (defun gnus-request-update-mark (group article mark)
389 "Allow the backend to change the mark the user tries to put on an article."
390 (let ((gnus-command-method (gnus-find-method-for-group group)))
391 (if (not (gnus-check-backend-function
392 'request-update-mark (car gnus-command-method)))
394 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
395 (gnus-group-real-name group) article mark))))
397 (defun gnus-request-article (article group &optional buffer)
398 "Request the ARTICLE in GROUP.
399 ARTICLE can either be an article number or an article Message-ID.
400 If BUFFER, insert the article in that group."
401 (let ((gnus-command-method (gnus-find-method-for-group group)))
402 (funcall (gnus-get-function gnus-command-method 'request-article)
403 article (gnus-group-real-name group)
404 (nth 1 gnus-command-method) buffer)))
406 (defun gnus-request-head (article group)
407 "Request the head of ARTICLE in GROUP."
408 (let* ((gnus-command-method (gnus-find-method-for-group group))
409 (head (gnus-get-function gnus-command-method 'request-head t))
415 (gnus-cache-request-article article group))
416 (setq res (cons group article)
418 ;; Check the agent cache.
419 ((and gnus-agent gnus-agent-cache gnus-plugged
421 (gnus-agent-request-article article group))
422 (setq res (cons group article)
424 ;; Use `head' function.
426 (setq res (funcall head article (gnus-group-real-name group)
427 (nth 1 gnus-command-method))))
428 ;; Use `article' function.
430 (setq res (gnus-request-article article group)
434 (set-buffer nntp-server-buffer)
435 (goto-char (point-min))
436 (when (search-forward "\n\n" nil t)
437 (delete-region (1- (point)) (point-max)))
438 (nnheader-fold-continuation-lines)))
441 (defun gnus-request-body (article group)
442 "Request the body of ARTICLE in GROUP."
443 (let* ((gnus-command-method (gnus-find-method-for-group group))
444 (head (gnus-get-function gnus-command-method 'request-body t))
450 (gnus-cache-request-article article group))
451 (setq res (cons group article)
453 ;; Check the agent cache.
454 ((and gnus-agent gnus-agent-cache gnus-plugged
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)))
490 (funcall (gnus-get-function gnus-command-method 'request-scan)
491 (and group (gnus-group-real-name group))
492 (nth 1 gnus-command-method)))))
494 (defsubst gnus-request-update-info (info gnus-command-method)
495 "Request that GNUS-COMMAND-METHOD update INFO."
496 (when (stringp gnus-command-method)
497 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
498 (when (gnus-check-backend-function
499 'request-update-info (car gnus-command-method))
500 (let ((group (gnus-info-group info)))
501 (and (funcall (gnus-get-function gnus-command-method
502 'request-update-info)
503 (gnus-group-real-name group)
504 info (nth 1 gnus-command-method))
505 ;; If the minimum article number is greater than 1, then all
506 ;; smaller article numbers are known not to exist; we'll
507 ;; artificially add those to the 'read range.
508 (let* ((active (gnus-active group))
511 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
512 (read (gnus-info-read info))
513 (new-read (gnus-range-add read (list range))))
514 (gnus-info-set-read info new-read)))
517 (defun gnus-request-expire-articles (articles group &optional force)
518 (let* ((gnus-command-method (gnus-find-method-for-group group))
521 (gnus-get-function gnus-command-method 'request-expire-articles)
522 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
524 (when (and gnus-agent gnus-agent-cache (gnus-agent-method-p gnus-command-method))
525 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
526 (when expired-articles
527 (gnus-agent-expire expired-articles group 'force))))
530 (defun gnus-request-move-article (article group server accept-function &optional last)
531 (let* ((gnus-command-method (gnus-find-method-for-group group))
532 (result (funcall (gnus-get-function gnus-command-method 'request-move-article)
533 article (gnus-group-real-name group)
534 (nth 1 gnus-command-method) accept-function last)))
535 (when (and result gnus-agent gnus-agent-cache (gnus-agent-method-p gnus-command-method))
536 (gnus-agent-expire (list article) group 'force))
539 (defun gnus-request-accept-article (group &optional gnus-command-method last
541 ;; Make sure there's a newline at the end of the article.
542 (when (stringp gnus-command-method)
543 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
544 (when (and (not gnus-command-method)
546 (setq gnus-command-method (gnus-group-name-to-method group)))
547 (goto-char (point-max))
551 (let ((message-options message-options))
552 (message-options-set-recipient)
554 (message-narrow-to-head)
555 (let ((mail-parse-charset message-default-charset))
556 (mail-encode-encoded-word-buffer)))
557 (message-encode-message-body)))
558 (let ((gnus-command-method (or gnus-command-method
559 (gnus-find-method-for-group group))))
560 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
561 (if (stringp group) (gnus-group-real-name group) group)
562 (cadr gnus-command-method)
565 (defun gnus-request-replace-article (article group buffer &optional no-encode)
567 (let ((message-options message-options))
568 (message-options-set-recipient)
570 (message-narrow-to-head)
571 (let ((mail-parse-charset message-default-charset))
572 (mail-encode-encoded-word-buffer)))
573 (message-encode-message-body)))
574 (let ((func (car (gnus-group-name-to-method group))))
575 (funcall (intern (format "%s-request-replace-article" func))
576 article (gnus-group-real-name group) buffer)))
578 (defun gnus-request-associate-buffer (group)
579 (let ((gnus-command-method (gnus-find-method-for-group group)))
580 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
581 (gnus-group-real-name group))))
583 (defun gnus-request-restore-buffer (article group)
584 "Request a new buffer restored to the state of ARTICLE."
585 (let ((gnus-command-method (gnus-find-method-for-group group)))
586 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
587 article (gnus-group-real-name group)
588 (nth 1 gnus-command-method))))
590 (defun gnus-request-create-group (group &optional gnus-command-method args)
591 (when (stringp gnus-command-method)
592 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
593 (let ((gnus-command-method
594 (or gnus-command-method (gnus-find-method-for-group group))))
595 (funcall (gnus-get-function gnus-command-method 'request-create-group)
596 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
598 (defun gnus-request-delete-group (group &optional force)
599 (let ((gnus-command-method (gnus-find-method-for-group group)))
600 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
601 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
603 (defun gnus-request-rename-group (group new-name)
604 (let ((gnus-command-method (gnus-find-method-for-group group)))
605 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
606 (gnus-group-real-name group)
607 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
609 (defun gnus-close-backends ()
610 ;; Send a close request to all backends that support such a request.
611 (let ((methods gnus-valid-select-methods)
612 (gnus-inhibit-demon t)
613 func gnus-command-method)
614 (while (setq gnus-command-method (pop methods))
615 (when (fboundp (setq func (intern
616 (concat (car gnus-command-method)
620 (defun gnus-asynchronous-p (gnus-command-method)
621 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
625 (defun gnus-remove-denial (gnus-command-method)
626 (when (stringp gnus-command-method)
627 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
628 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
629 (status (cadr elem)))
630 ;; If this hasn't been opened before, we add it to the list.
631 (when (eq status 'denied)
632 ;; Set the status of this server.
633 (setcar (cdr elem) 'closed))))
637 ;;; gnus-int.el ends here