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))
33 (defcustom gnus-open-server-hook nil
34 "Hook called just before opening connection to the news server."
38 (defcustom gnus-server-unopen-status nil
39 "The default status if the server is not able to open.
40 If the server is covered by Gnus agent, the possible values are
41 `denied', set the server denied; `offline', set the server offline;
42 `nil', ask user. If the server is not covered by Gnus agent, set the
45 :type '(choice (const :tag "Ask" nil)
46 (const :tag "Deny server" denied)
47 (const :tag "Unplugg Agent" offline)))
50 ;;; Server Communication
53 (defun gnus-start-news-server (&optional confirm)
54 "Open a method for getting news.
55 If CONFIRM is non-nil, the user will be asked for an NNTP server."
57 (if gnus-current-select-method
58 ;; Stream is already opened.
61 (unless gnus-nntp-service
62 (setq gnus-nntp-server nil))
64 ;; Read server name with completion.
65 (setq gnus-nntp-server
66 (completing-read "NNTP server: "
67 (mapcar (lambda (server) (list server))
68 (cons (list gnus-nntp-server)
69 gnus-secondary-servers))
70 nil nil gnus-nntp-server)))
72 (when (and gnus-nntp-server
73 (stringp gnus-nntp-server)
74 (not (string= gnus-nntp-server "")))
75 (setq gnus-select-method
76 (cond ((or (string= gnus-nntp-server "")
77 (string= gnus-nntp-server "::"))
78 (list 'nnspool (system-name)))
79 ((string-match "^:" gnus-nntp-server)
80 (list 'nnmh gnus-nntp-server
82 (file-name-as-directory
84 (substring gnus-nntp-server 1) "~/")))
85 (list 'nnmh-get-new-mail nil)))
87 (list 'nntp gnus-nntp-server)))))
89 (setq how (car gnus-select-method))
93 (gnus-message 5 "Looking up local news spool..."))
96 (gnus-message 5 "Looking up mh spool..."))
99 (setq gnus-current-select-method gnus-select-method)
100 (gnus-run-hooks 'gnus-open-server-hook)
102 ;; gnus-open-server-hook might have opened it
103 (gnus-server-opened gnus-select-method)
104 (gnus-open-server gnus-select-method)
108 "%s (%s) open error: '%s'. Continue? "
109 (car gnus-select-method) (cadr gnus-select-method)
110 (gnus-status-message gnus-select-method)))
111 (gnus-error 1 "Couldn't open server on %s"
112 (nth 1 gnus-select-method))))))
114 (defun gnus-check-group (group)
115 "Try to make sure that the server where GROUP exists is alive."
116 (let ((method (gnus-find-method-for-group group)))
117 (or (gnus-server-opened method)
118 (gnus-open-server method))))
120 (defun gnus-check-server (&optional method silent)
121 "Check whether the connection to METHOD is down.
122 If METHOD is nil, use `gnus-select-method'.
123 If it is down, start it up (again)."
124 (let ((method (or method gnus-select-method))
126 ;; Transform virtual server names into select methods.
127 (when (stringp method)
128 (setq method (gnus-server-to-method method)))
129 (if (gnus-server-opened method)
130 ;; The stream is already opened.
134 (gnus-message 5 "Opening %s server%s..." (car method)
135 (if (equal (nth 1 method) "") ""
136 (format " on %s" (nth 1 method)))))
137 (gnus-run-hooks 'gnus-open-server-hook)
140 (setq result (gnus-open-server method))
141 (quit (message "Quit gnus-check-server")
144 (gnus-message 5 "Opening %s server%s...%s" (car method)
145 (if (equal (nth 1 method) "") ""
146 (format " on %s" (nth 1 method)))
147 (if result "done" "failed")))))))
149 (defun gnus-get-function (method function &optional noerror)
150 "Return a function symbol based on METHOD and FUNCTION."
151 ;; Translate server names into methods.
153 (error "Attempted use of a nil select method"))
154 (when (stringp method)
155 (setq method (gnus-server-to-method method)))
156 ;; Check cache of constructed names.
157 (let* ((method-sym (if gnus-agent
158 (gnus-agent-get-function method)
160 (method-fns (get method-sym 'gnus-method-functions))
161 (func (let ((method-fnlist-elt (assq function method-fns)))
162 (unless method-fnlist-elt
163 (setq method-fnlist-elt
165 (intern (format "%s-%s" method-sym function))))
166 (put method-sym 'gnus-method-functions
167 (cons method-fnlist-elt method-fns)))
168 (cdr method-fnlist-elt))))
169 ;; Maybe complain if there is no function.
170 (unless (fboundp func)
172 (error "Trying to require a method that doesn't exist"))
173 (require (car method))
174 (when (not (fboundp func))
177 (error "No such function: %s" func))))
182 ;;; Interface functions to the backends.
185 (defun gnus-open-server (gnus-command-method)
186 "Open a connection to GNUS-COMMAND-METHOD."
187 (when (stringp gnus-command-method)
188 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
189 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
190 ;; If this method was previously denied, we just return nil.
191 (if (eq (nth 1 elem) 'denied)
193 (gnus-message 1 "Denied server")
198 (funcall (gnus-get-function gnus-command-method 'open-server)
199 (nth 1 gnus-command-method)
200 (nthcdr 2 gnus-command-method))
202 (message "Quit trying to open server")
204 ;; If this hasn't been opened before, we add it to the list.
206 (setq elem (list gnus-command-method nil)
207 gnus-opened-servers (cons elem gnus-opened-servers)))
208 ;; Set the status of this server.
211 (if (eq (cadr elem) 'offline)
215 (not (eq (cadr elem) 'offline))
216 (gnus-agent-method-p gnus-command-method))
217 (or gnus-server-unopen-status
219 (format "Unable to open %s:%s, go offline? "
220 (car gnus-command-method)
221 (cadr gnus-command-method)))
225 ;; Return the result from the "open" call.
226 (or (eq (cadr elem) 'offline)
229 (defun gnus-close-server (gnus-command-method)
230 "Close the connection to GNUS-COMMAND-METHOD."
231 (when (stringp gnus-command-method)
232 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
233 (funcall (gnus-get-function gnus-command-method 'close-server)
234 (nth 1 gnus-command-method)))
236 (defun gnus-request-list (gnus-command-method)
237 "Request the active file from GNUS-COMMAND-METHOD."
238 (when (stringp gnus-command-method)
239 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
240 (funcall (gnus-get-function gnus-command-method 'request-list)
241 (nth 1 gnus-command-method)))
243 (defun gnus-request-list-newsgroups (gnus-command-method)
244 "Request the newsgroups file from 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 'request-list-newsgroups)
248 (nth 1 gnus-command-method)))
250 (defun gnus-request-newgroups (date gnus-command-method)
251 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
252 (when (stringp gnus-command-method)
253 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
254 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
256 (funcall func date (nth 1 gnus-command-method)))))
258 (defun gnus-server-opened (gnus-command-method)
259 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
260 (unless (eq (gnus-server-status gnus-command-method)
262 (when (stringp gnus-command-method)
263 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
264 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
265 (nth 1 gnus-command-method))))
267 (defun gnus-status-message (gnus-command-method)
268 "Return the status message from GNUS-COMMAND-METHOD.
269 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
270 this group uses will be queried."
271 (let ((gnus-command-method
272 (if (stringp gnus-command-method)
273 (gnus-find-method-for-group gnus-command-method)
274 gnus-command-method)))
275 (funcall (gnus-get-function gnus-command-method 'status-message)
276 (nth 1 gnus-command-method))))
278 (defun gnus-request-regenerate (gnus-command-method)
279 "Request a data generation from GNUS-COMMAND-METHOD."
280 (when (stringp gnus-command-method)
281 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
282 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
283 (nth 1 gnus-command-method)))
285 (defun gnus-request-group (group &optional dont-check gnus-command-method)
286 "Request GROUP. If DONT-CHECK, no information is required."
287 (let ((gnus-command-method
288 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
289 (when (stringp gnus-command-method)
290 (setq gnus-command-method
291 (inline (gnus-server-to-method gnus-command-method))))
292 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
293 (gnus-group-real-name group) (nth 1 gnus-command-method)
296 (defun gnus-list-active-group (group)
297 "Request active information on GROUP."
298 (let ((gnus-command-method (gnus-find-method-for-group group))
299 (func 'list-active-group))
300 (when (gnus-check-backend-function func group)
301 (funcall (gnus-get-function gnus-command-method func)
302 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
304 (defun gnus-request-group-description (group)
305 "Request a description of GROUP."
306 (let ((gnus-command-method (gnus-find-method-for-group group))
307 (func 'request-group-description))
308 (when (gnus-check-backend-function func group)
309 (funcall (gnus-get-function gnus-command-method func)
310 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
312 (defun gnus-request-group-articles (group)
313 "Request a list of existing articles in GROUP."
314 (let ((gnus-command-method (gnus-find-method-for-group group))
315 (func 'request-group-articles))
316 (when (gnus-check-backend-function func group)
317 (funcall (gnus-get-function gnus-command-method func)
318 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
320 (defun gnus-close-group (group)
321 "Request the GROUP be closed."
322 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
323 (funcall (gnus-get-function gnus-command-method 'close-group)
324 (gnus-group-real-name group) (nth 1 gnus-command-method))))
326 (defun gnus-retrieve-headers (articles group &optional fetch-old)
327 "Request headers for ARTICLES in GROUP.
328 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
329 (let ((gnus-command-method (gnus-find-method-for-group group)))
331 ((and gnus-use-cache (numberp (car articles)))
332 (gnus-cache-retrieve-headers articles group fetch-old))
333 ((and gnus-agent gnus-agent-cache (gnus-online gnus-command-method)
334 (gnus-agent-method-p gnus-command-method))
335 (gnus-agent-retrieve-headers articles group fetch-old))
337 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
338 articles (gnus-group-real-name group)
339 (nth 1 gnus-command-method) fetch-old)))))
341 (defun gnus-retrieve-articles (articles group)
342 "Request ARTICLES in GROUP."
343 (let ((gnus-command-method (gnus-find-method-for-group group)))
344 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
345 articles (gnus-group-real-name group)
346 (nth 1 gnus-command-method))))
348 (defun gnus-retrieve-groups (groups gnus-command-method)
349 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
350 (when (stringp gnus-command-method)
351 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
352 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
353 groups (nth 1 gnus-command-method)))
355 (defun gnus-request-type (group &optional article)
356 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
357 (let ((gnus-command-method (gnus-find-method-for-group group)))
358 (if (not (gnus-check-backend-function
359 'request-type (car gnus-command-method)))
361 (funcall (gnus-get-function gnus-command-method 'request-type)
362 (gnus-group-real-name group) article))))
364 (defun gnus-request-set-mark (group action)
365 "Set marks on articles in the backend."
366 (let ((gnus-command-method (gnus-find-method-for-group group)))
367 (if (not (gnus-check-backend-function
368 'request-set-mark (car gnus-command-method)))
370 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
371 (gnus-group-real-name group) action
372 (nth 1 gnus-command-method)))))
374 (defun gnus-request-update-mark (group article mark)
375 "Allow the backend to change the mark the user tries to put on an article."
376 (let ((gnus-command-method (gnus-find-method-for-group group)))
377 (if (not (gnus-check-backend-function
378 'request-update-mark (car gnus-command-method)))
380 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
381 (gnus-group-real-name group) article mark))))
383 (defun gnus-request-article (article group &optional buffer)
384 "Request the ARTICLE in GROUP.
385 ARTICLE can either be an article number or an article Message-ID.
386 If BUFFER, insert the article in that group."
387 (let ((gnus-command-method (gnus-find-method-for-group group)))
388 (funcall (gnus-get-function gnus-command-method 'request-article)
389 article (gnus-group-real-name group)
390 (nth 1 gnus-command-method) buffer)))
392 (defun gnus-request-head (article group)
393 "Request the head of ARTICLE in GROUP."
394 (let* ((gnus-command-method (gnus-find-method-for-group group))
395 (head (gnus-get-function gnus-command-method 'request-head t))
401 (gnus-cache-request-article article group))
402 (setq res (cons group article)
404 ;; Check the agent cache.
405 ((and gnus-agent gnus-agent-cache gnus-plugged
407 (gnus-agent-request-article article group))
408 (setq res (cons group article)
410 ;; Use `head' function.
412 (setq res (funcall head article (gnus-group-real-name group)
413 (nth 1 gnus-command-method))))
414 ;; Use `article' function.
416 (setq res (gnus-request-article article group)
420 (set-buffer nntp-server-buffer)
421 (goto-char (point-min))
422 (when (search-forward "\n\n" nil t)
423 (delete-region (1- (point)) (point-max)))
424 (nnheader-fold-continuation-lines)))
427 (defun gnus-request-body (article group)
428 "Request the body of ARTICLE in GROUP."
429 (let* ((gnus-command-method (gnus-find-method-for-group group))
430 (head (gnus-get-function gnus-command-method 'request-body t))
436 (gnus-cache-request-article article group))
437 (setq res (cons group article)
439 ;; Check the agent cache.
440 ((and gnus-agent gnus-agent-cache gnus-plugged
442 (gnus-agent-request-article article group))
443 (setq res (cons group article)
445 ;; Use `head' function.
447 (setq res (funcall head article (gnus-group-real-name group)
448 (nth 1 gnus-command-method))))
449 ;; Use `article' function.
451 (setq res (gnus-request-article article group)
455 (set-buffer nntp-server-buffer)
456 (goto-char (point-min))
457 (when (search-forward "\n\n" nil t)
458 (delete-region (point-min) (1- (point))))))
461 (defun gnus-request-post (gnus-command-method)
462 "Post the current buffer using GNUS-COMMAND-METHOD."
463 (when (stringp gnus-command-method)
464 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
465 (funcall (gnus-get-function gnus-command-method 'request-post)
466 (nth 1 gnus-command-method)))
468 (defun gnus-request-scan (group gnus-command-method)
469 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
470 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
471 (let ((gnus-command-method
472 (if group (gnus-find-method-for-group group) gnus-command-method))
473 (gnus-inhibit-demon t)
474 (mail-source-plugged gnus-plugged))
475 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
476 (funcall (gnus-get-function gnus-command-method 'request-scan)
477 (and group (gnus-group-real-name group))
478 (nth 1 gnus-command-method)))))
480 (defsubst gnus-request-update-info (info gnus-command-method)
481 "Request that GNUS-COMMAND-METHOD update INFO."
482 (when (stringp gnus-command-method)
483 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
484 (when (gnus-check-backend-function
485 'request-update-info (car gnus-command-method))
486 (funcall (gnus-get-function gnus-command-method 'request-update-info)
487 (gnus-group-real-name (gnus-info-group info))
488 info (nth 1 gnus-command-method))))
490 (defun gnus-request-expire-articles (articles group &optional force)
491 (let* ((gnus-command-method (gnus-find-method-for-group group))
494 (gnus-get-function gnus-command-method 'request-expire-articles)
495 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
497 (when (and gnus-agent gnus-agent-cache
498 (gnus-sorted-difference articles not-deleted))
499 (gnus-agent-expire (gnus-sorted-difference articles not-deleted)
503 (defun gnus-request-move-article (article group server accept-function &optional last)
504 (let* ((gnus-command-method (gnus-find-method-for-group group))
505 (result (funcall (gnus-get-function gnus-command-method 'request-move-article)
506 article (gnus-group-real-name group)
507 (nth 1 gnus-command-method) accept-function last)))
508 (when (and result gnus-agent gnus-agent-cache)
509 (gnus-agent-expire (list article) group 'force))
512 (defun gnus-request-accept-article (group &optional gnus-command-method last
514 ;; Make sure there's a newline at the end of the article.
515 (when (stringp gnus-command-method)
516 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
517 (when (and (not gnus-command-method)
519 (setq gnus-command-method (gnus-group-name-to-method group)))
520 (goto-char (point-max))
524 (let ((message-options message-options))
525 (message-options-set-recipient)
527 (message-narrow-to-head)
528 (let ((mail-parse-charset message-default-charset))
529 (mail-encode-encoded-word-buffer)))
530 (message-encode-message-body)))
531 (let ((gnus-command-method (or gnus-command-method
532 (gnus-find-method-for-group group))))
533 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
534 (if (stringp group) (gnus-group-real-name group) group)
535 (cadr gnus-command-method)
538 (defun gnus-request-replace-article (article group buffer &optional no-encode)
540 (let ((message-options message-options))
541 (message-options-set-recipient)
543 (message-narrow-to-head)
544 (let ((mail-parse-charset message-default-charset))
545 (mail-encode-encoded-word-buffer)))
546 (message-encode-message-body)))
547 (let ((func (car (gnus-group-name-to-method group))))
548 (funcall (intern (format "%s-request-replace-article" func))
549 article (gnus-group-real-name group) buffer)))
551 (defun gnus-request-associate-buffer (group)
552 (let ((gnus-command-method (gnus-find-method-for-group group)))
553 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
554 (gnus-group-real-name group))))
556 (defun gnus-request-restore-buffer (article group)
557 "Request a new buffer restored to the state of ARTICLE."
558 (let ((gnus-command-method (gnus-find-method-for-group group)))
559 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
560 article (gnus-group-real-name group)
561 (nth 1 gnus-command-method))))
563 (defun gnus-request-create-group (group &optional gnus-command-method args)
564 (when (stringp gnus-command-method)
565 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
566 (let ((gnus-command-method
567 (or gnus-command-method (gnus-find-method-for-group group))))
568 (funcall (gnus-get-function gnus-command-method 'request-create-group)
569 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
571 (defun gnus-request-delete-group (group &optional force)
572 (let ((gnus-command-method (gnus-find-method-for-group group)))
573 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
574 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
576 (defun gnus-request-rename-group (group new-name)
577 (let ((gnus-command-method (gnus-find-method-for-group group)))
578 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
579 (gnus-group-real-name group)
580 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
582 (defun gnus-close-backends ()
583 ;; Send a close request to all backends that support such a request.
584 (let ((methods gnus-valid-select-methods)
585 (gnus-inhibit-demon t)
586 func gnus-command-method)
587 (while (setq gnus-command-method (pop methods))
588 (when (fboundp (setq func (intern
589 (concat (car gnus-command-method)
593 (defun gnus-asynchronous-p (gnus-command-method)
594 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
598 (defun gnus-remove-denial (gnus-command-method)
599 (when (stringp gnus-command-method)
600 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
601 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
602 (status (cadr elem)))
603 ;; If this hasn't been opened before, we add it to the list.
604 (when (eq status 'denied)
605 ;; Set the status of this server.
606 (setcar (cdr elem) 'closed))))
610 ;;; gnus-int.el ends here