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)))
198 (let ((state (or (assoc gnus-command-method gnus-opened-servers)
199 (car (setq gnus-opened-servers
200 (cons (list gnus-command-method nil)
201 gnus-opened-servers))))))
202 (cond ((eq (nth 1 state) 'denied)
203 ;; If this method was previously denied, we just return nil.
205 (gnus-message 1 "Denied server")
207 ((eq (nth 1 state) 'offline)
208 ;; If this method was previously opened offline, we just return t.
211 ;; I'm opening servers while unplugged. Set the status to
212 ;; either 'offline or 'denied without asking (I'm assuming
213 ;; that the user wants to go 'offline on every agentized
214 ;; server when opening while unplugged.)
215 (setcar (cdr state) (if (and gnus-agent
216 (gnus-agent-method-p gnus-command-method))
217 (or gnus-server-unopen-status
221 (if (eq (nth 1 state) 'offline)
222 ;; Invoke the agent's backend to open the offline server.
223 (funcall (gnus-get-function gnus-command-method 'open-server)
224 (nth 1 gnus-command-method)
225 (nthcdr 2 gnus-command-method))))
228 (funcall (gnus-get-function gnus-command-method 'open-server)
229 (nth 1 gnus-command-method)
230 (nthcdr 2 gnus-command-method))
232 (gnus-message 1 (format
233 "Unable to open server due to: %s"
234 (error-message-string err)))
237 (gnus-message 1 "Quit trying to open server")
239 ;; I successfully opened the server.
240 (setcar (cdr state) 'ok))
242 ;; I couldn't open the server so decide whether to mark it
243 ;; 'denied or to open it 'offline.
246 (not (eq (cadr state) 'offline))
247 (gnus-agent-method-p gnus-command-method))
248 (or gnus-server-unopen-status
250 (format "Unable to open %s:%s, go offline? "
251 (car gnus-command-method)
252 (cadr gnus-command-method)))
256 (if (eq (nth 1 state) 'offline)
257 ;; Invoke the agent's backend to open the offline server.
258 (funcall (gnus-get-function gnus-command-method 'open-server)
259 (nth 1 gnus-command-method)
260 (nthcdr 2 gnus-command-method)))))))
262 (defun gnus-close-server (gnus-command-method)
263 "Close the connection to GNUS-COMMAND-METHOD."
264 (when (stringp gnus-command-method)
265 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
266 (funcall (gnus-get-function gnus-command-method 'close-server)
267 (nth 1 gnus-command-method)))
269 (defun gnus-request-list (gnus-command-method)
270 "Request the active file from GNUS-COMMAND-METHOD."
271 (when (stringp gnus-command-method)
272 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
273 (funcall (gnus-get-function gnus-command-method 'request-list)
274 (nth 1 gnus-command-method)))
276 (defun gnus-request-list-newsgroups (gnus-command-method)
277 "Request the newsgroups file from GNUS-COMMAND-METHOD."
278 (when (stringp gnus-command-method)
279 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
280 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
281 (nth 1 gnus-command-method)))
283 (defun gnus-request-newgroups (date gnus-command-method)
284 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
285 (when (stringp gnus-command-method)
286 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
287 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
289 (funcall func date (nth 1 gnus-command-method)))))
291 (defun gnus-server-opened (gnus-command-method)
292 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
293 (unless (eq (gnus-server-status gnus-command-method)
295 (when (stringp gnus-command-method)
296 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
297 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
298 (nth 1 gnus-command-method))))
300 (defun gnus-status-message (gnus-command-method)
301 "Return the status message from GNUS-COMMAND-METHOD.
302 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
303 this group uses will be queried."
304 (let ((gnus-command-method
305 (if (stringp gnus-command-method)
306 (gnus-find-method-for-group gnus-command-method)
307 gnus-command-method)))
308 (funcall (gnus-get-function gnus-command-method 'status-message)
309 (nth 1 gnus-command-method))))
311 (defun gnus-request-regenerate (gnus-command-method)
312 "Request a data generation from GNUS-COMMAND-METHOD."
313 (when (stringp gnus-command-method)
314 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
315 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
316 (nth 1 gnus-command-method)))
318 (defun gnus-request-group (group &optional dont-check gnus-command-method)
319 "Request GROUP. If DONT-CHECK, no information is required."
320 (let ((gnus-command-method
321 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
322 (when (stringp gnus-command-method)
323 (setq gnus-command-method
324 (inline (gnus-server-to-method gnus-command-method))))
325 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
326 (gnus-group-real-name group) (nth 1 gnus-command-method)
329 (defun gnus-list-active-group (group)
330 "Request active information on GROUP."
331 (let ((gnus-command-method (gnus-find-method-for-group group))
332 (func 'list-active-group))
333 (when (gnus-check-backend-function func group)
334 (funcall (gnus-get-function gnus-command-method func)
335 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
337 (defun gnus-request-group-description (group)
338 "Request a description of GROUP."
339 (let ((gnus-command-method (gnus-find-method-for-group group))
340 (func 'request-group-description))
341 (when (gnus-check-backend-function func group)
342 (funcall (gnus-get-function gnus-command-method func)
343 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
345 (defun gnus-request-group-articles (group)
346 "Request a list of existing articles in GROUP."
347 (let ((gnus-command-method (gnus-find-method-for-group group))
348 (func 'request-group-articles))
349 (when (gnus-check-backend-function func group)
350 (funcall (gnus-get-function gnus-command-method func)
351 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
353 (defun gnus-close-group (group)
354 "Request the GROUP be closed."
355 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
356 (funcall (gnus-get-function gnus-command-method 'close-group)
357 (gnus-group-real-name group) (nth 1 gnus-command-method))))
359 (defun gnus-retrieve-headers (articles group &optional fetch-old)
360 "Request headers for ARTICLES in GROUP.
361 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
362 (let ((gnus-command-method (gnus-find-method-for-group group)))
364 ((and gnus-use-cache (numberp (car articles)))
365 (gnus-cache-retrieve-headers articles group fetch-old))
366 ((and gnus-agent (gnus-online gnus-command-method)
367 (gnus-agent-method-p gnus-command-method))
368 (gnus-agent-retrieve-headers articles group fetch-old))
370 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
371 articles (gnus-group-real-name group)
372 (nth 1 gnus-command-method) fetch-old)))))
374 (defun gnus-retrieve-articles (articles group)
375 "Request ARTICLES in GROUP."
376 (let ((gnus-command-method (gnus-find-method-for-group group)))
377 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
378 articles (gnus-group-real-name group)
379 (nth 1 gnus-command-method))))
381 (defun gnus-retrieve-groups (groups gnus-command-method)
382 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
383 (when (stringp gnus-command-method)
384 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
385 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
386 groups (nth 1 gnus-command-method)))
388 (defun gnus-request-type (group &optional article)
389 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
390 (let ((gnus-command-method (gnus-find-method-for-group group)))
391 (if (not (gnus-check-backend-function
392 'request-type (car gnus-command-method)))
394 (funcall (gnus-get-function gnus-command-method 'request-type)
395 (gnus-group-real-name group) article))))
397 (defun gnus-request-set-mark (group action)
398 "Set marks on articles in the backend."
399 (let ((gnus-command-method (gnus-find-method-for-group group)))
400 (if (not (gnus-check-backend-function
401 'request-set-mark (car gnus-command-method)))
403 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
404 (gnus-group-real-name group) action
405 (nth 1 gnus-command-method)))))
407 (defun gnus-request-update-mark (group article mark)
408 "Allow the backend to change the mark the user tries to put on an article."
409 (let ((gnus-command-method (gnus-find-method-for-group group)))
410 (if (not (gnus-check-backend-function
411 'request-update-mark (car gnus-command-method)))
413 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
414 (gnus-group-real-name group) article mark))))
416 (defun gnus-request-article (article group &optional buffer)
417 "Request the ARTICLE in GROUP.
418 ARTICLE can either be an article number or an article Message-ID.
419 If BUFFER, insert the article in that group."
420 (let ((gnus-command-method (gnus-find-method-for-group group)))
421 (funcall (gnus-get-function gnus-command-method 'request-article)
422 article (gnus-group-real-name group)
423 (nth 1 gnus-command-method) buffer)))
425 (defun gnus-request-head (article group)
426 "Request the head of ARTICLE in GROUP."
427 (let* ((gnus-command-method (gnus-find-method-for-group group))
428 (head (gnus-get-function gnus-command-method 'request-head t))
434 (gnus-cache-request-article article group))
435 (setq res (cons group article)
437 ;; Check the agent cache.
438 ((gnus-agent-request-article article group)
439 (setq res (cons group article)
441 ;; Use `head' function.
443 (setq res (funcall head article (gnus-group-real-name group)
444 (nth 1 gnus-command-method))))
445 ;; Use `article' function.
447 (setq res (gnus-request-article article group)
451 (set-buffer nntp-server-buffer)
452 (goto-char (point-min))
453 (when (search-forward "\n\n" nil t)
454 (delete-region (1- (point)) (point-max)))
455 (nnheader-fold-continuation-lines)))
458 (defun gnus-request-body (article group)
459 "Request the body of ARTICLE in GROUP."
460 (let* ((gnus-command-method (gnus-find-method-for-group group))
461 (head (gnus-get-function gnus-command-method 'request-body t))
467 (gnus-cache-request-article article group))
468 (setq res (cons group article)
470 ;; Check the agent cache.
471 ((gnus-agent-request-article article group)
472 (setq res (cons group article)
474 ;; Use `head' function.
476 (setq res (funcall head article (gnus-group-real-name group)
477 (nth 1 gnus-command-method))))
478 ;; Use `article' function.
480 (setq res (gnus-request-article article group)
484 (set-buffer nntp-server-buffer)
485 (goto-char (point-min))
486 (when (search-forward "\n\n" nil t)
487 (delete-region (point-min) (1- (point))))))
490 (defun gnus-request-post (gnus-command-method)
491 "Post the current buffer using GNUS-COMMAND-METHOD."
492 (when (stringp gnus-command-method)
493 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
494 (funcall (gnus-get-function gnus-command-method 'request-post)
495 (nth 1 gnus-command-method)))
497 (defun gnus-request-scan (group gnus-command-method)
498 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
499 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
500 (let ((gnus-command-method
501 (if group (gnus-find-method-for-group group) gnus-command-method))
502 (gnus-inhibit-demon t)
503 (mail-source-plugged gnus-plugged))
504 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
506 (setq gnus-internal-registry-spool-current-method gnus-command-method)
507 (funcall (gnus-get-function gnus-command-method 'request-scan)
508 (and group (gnus-group-real-name group))
509 (nth 1 gnus-command-method))))))
511 (defsubst gnus-request-update-info (info gnus-command-method)
512 "Request that GNUS-COMMAND-METHOD update INFO."
513 (when (stringp gnus-command-method)
514 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
515 (when (gnus-check-backend-function
516 'request-update-info (car gnus-command-method))
517 (let ((group (gnus-info-group info)))
518 (and (funcall (gnus-get-function gnus-command-method
519 'request-update-info)
520 (gnus-group-real-name group)
521 info (nth 1 gnus-command-method))
522 ;; If the minimum article number is greater than 1, then all
523 ;; smaller article numbers are known not to exist; we'll
524 ;; artificially add those to the 'read range.
525 (let* ((active (gnus-active group))
528 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
529 (read (gnus-info-read info))
530 (new-read (gnus-range-add read (list range))))
531 (gnus-info-set-read info new-read)))
534 (defun gnus-request-expire-articles (articles group &optional force)
535 (let* ((gnus-command-method (gnus-find-method-for-group group))
538 (gnus-get-function gnus-command-method 'request-expire-articles)
539 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
541 (when (and gnus-agent
542 (gnus-agent-method-p gnus-command-method))
543 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
544 (when expired-articles
545 (gnus-agent-expire expired-articles group 'force))))
548 (defun gnus-request-move-article (article group server accept-function
550 (let* ((gnus-command-method (gnus-find-method-for-group group))
551 (result (funcall (gnus-get-function gnus-command-method
552 'request-move-article)
553 article (gnus-group-real-name group)
554 (nth 1 gnus-command-method) accept-function last)))
555 (when (and result gnus-agent
556 (gnus-agent-method-p gnus-command-method))
557 (gnus-agent-expire (list article) group 'force))
560 (defun gnus-request-accept-article (group &optional gnus-command-method last
562 ;; Make sure there's a newline at the end of the article.
563 (when (stringp gnus-command-method)
564 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
565 (when (and (not gnus-command-method)
567 (setq gnus-command-method (gnus-group-name-to-method group)))
568 (goto-char (point-max))
572 (let ((message-options message-options))
573 (message-options-set-recipient)
575 (message-narrow-to-head)
576 (let ((mail-parse-charset message-default-charset))
577 (mail-encode-encoded-word-buffer)))
578 (message-encode-message-body)))
579 (let ((gnus-command-method (or gnus-command-method
580 (gnus-find-method-for-group group))))
581 (funcall (gnus-get-function gnus-command-method 'request-accept-article)
582 (if (stringp group) (gnus-group-real-name group) group)
583 (cadr gnus-command-method)
586 (defun gnus-request-replace-article (article group buffer &optional no-encode)
588 (let ((message-options message-options))
589 (message-options-set-recipient)
591 (message-narrow-to-head)
592 (let ((mail-parse-charset message-default-charset))
593 (mail-encode-encoded-word-buffer)))
594 (message-encode-message-body)))
595 (let ((func (car (gnus-group-name-to-method group))))
596 (funcall (intern (format "%s-request-replace-article" func))
597 article (gnus-group-real-name group) buffer)))
599 (defun gnus-request-associate-buffer (group)
600 (let ((gnus-command-method (gnus-find-method-for-group group)))
601 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
602 (gnus-group-real-name group))))
604 (defun gnus-request-restore-buffer (article group)
605 "Request a new buffer restored to the state of ARTICLE."
606 (let ((gnus-command-method (gnus-find-method-for-group group)))
607 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
608 article (gnus-group-real-name group)
609 (nth 1 gnus-command-method))))
611 (defun gnus-request-create-group (group &optional gnus-command-method args)
612 (when (stringp gnus-command-method)
613 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
614 (let ((gnus-command-method
615 (or gnus-command-method (gnus-find-method-for-group group))))
616 (funcall (gnus-get-function gnus-command-method 'request-create-group)
617 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
619 (defun gnus-request-delete-group (group &optional force)
620 (let ((gnus-command-method (gnus-find-method-for-group group)))
621 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
622 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
624 (defun gnus-request-rename-group (group new-name)
625 (let ((gnus-command-method (gnus-find-method-for-group group)))
626 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
627 (gnus-group-real-name group)
628 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
630 (defun gnus-close-backends ()
631 ;; Send a close request to all backends that support such a request.
632 (let ((methods gnus-valid-select-methods)
633 (gnus-inhibit-demon t)
634 func gnus-command-method)
635 (while (setq gnus-command-method (pop methods))
636 (when (fboundp (setq func (intern
637 (concat (car gnus-command-method)
641 (defun gnus-asynchronous-p (gnus-command-method)
642 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
646 (defun gnus-remove-denial (gnus-command-method)
647 (when (stringp gnus-command-method)
648 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
649 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
650 (status (cadr elem)))
651 ;; If this hasn't been opened before, we add it to the list.
652 (when (eq status 'denied)
653 ;; Set the status of this server.
654 (setcar (cdr elem) 'closed))))
658 ;;; gnus-int.el ends here