1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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."
40 ;;; Server Communication
43 (defun gnus-start-news-server (&optional confirm)
44 "Open a method for getting news.
45 If CONFIRM is non-nil, the user will be asked for an NNTP server."
47 (if gnus-current-select-method
48 ;; Stream is already opened.
51 (unless gnus-nntp-service
52 (setq gnus-nntp-server nil))
54 ;; Read server name with completion.
55 (setq gnus-nntp-server
56 (completing-read "NNTP server: "
57 (mapcar (lambda (server) (list server))
58 (cons (list gnus-nntp-server)
59 gnus-secondary-servers))
60 nil nil gnus-nntp-server)))
62 (when (and gnus-nntp-server
63 (stringp gnus-nntp-server)
64 (not (string= gnus-nntp-server "")))
65 (setq gnus-select-method
66 (cond ((or (string= gnus-nntp-server "")
67 (string= gnus-nntp-server "::"))
68 (list 'nnspool (system-name)))
69 ((string-match "^:" gnus-nntp-server)
70 (list 'nnmh gnus-nntp-server
72 (file-name-as-directory
74 (substring gnus-nntp-server 1) "~/")))
75 (list 'nnmh-get-new-mail nil)))
77 (list 'nntp gnus-nntp-server)))))
79 (setq how (car gnus-select-method))
83 (gnus-message 5 "Looking up local news spool..."))
86 (gnus-message 5 "Looking up mh spool..."))
89 (setq gnus-current-select-method gnus-select-method)
90 (gnus-run-hooks 'gnus-open-server-hook)
92 ;; gnus-open-server-hook might have opened it
93 (gnus-server-opened gnus-select-method)
94 (gnus-open-server gnus-select-method)
98 "%s (%s) open error: '%s'. Continue? "
99 (car gnus-select-method) (cadr gnus-select-method)
100 (gnus-status-message gnus-select-method)))
101 (gnus-error 1 "Couldn't open server on %s"
102 (nth 1 gnus-select-method))))))
104 (defun gnus-check-group (group)
105 "Try to make sure that the server where GROUP exists is alive."
106 (let ((method (gnus-find-method-for-group group)))
107 (or (gnus-server-opened method)
108 (gnus-open-server method))))
110 (defun gnus-check-server (&optional method silent)
111 "Check whether the connection to METHOD is down.
112 If METHOD is nil, use `gnus-select-method'.
113 If it is down, start it up (again)."
114 (let ((method (or method gnus-select-method)))
115 ;; Transform virtual server names into select methods.
116 (when (stringp method)
117 (setq method (gnus-server-to-method method)))
118 (if (gnus-server-opened method)
119 ;; The stream is already opened.
123 (gnus-message 5 "Opening %s server%s..." (car method)
124 (if (equal (nth 1 method) "") ""
125 (format " on %s" (nth 1 method)))))
126 (gnus-run-hooks 'gnus-open-server-hook)
129 (gnus-open-server method)
130 (quit (message "Quit gnus-check-server")
135 (defun gnus-get-function (method function &optional noerror)
136 "Return a function symbol based on METHOD and FUNCTION."
137 ;; Translate server names into methods.
139 (error "Attempted use of a nil select method"))
140 (when (stringp method)
141 (setq method (gnus-server-to-method method)))
142 ;; Check cache of constructed names.
143 (let* ((method-sym (if gnus-agent
144 (gnus-agent-get-function method)
146 (method-fns (get method-sym 'gnus-method-functions))
147 (func (let ((method-fnlist-elt (assq function method-fns)))
148 (unless method-fnlist-elt
149 (setq method-fnlist-elt
151 (intern (format "%s-%s" method-sym function))))
152 (put method-sym 'gnus-method-functions
153 (cons method-fnlist-elt method-fns)))
154 (cdr method-fnlist-elt))))
155 ;; Maybe complain if there is no function.
156 (unless (fboundp func)
158 (error "Trying to require a method that doesn't exist"))
159 (require (car method))
160 (when (not (fboundp func))
163 (error "No such function: %s" func))))
168 ;;; Interface functions to the backends.
171 (defun gnus-open-server (gnus-command-method)
172 "Open a connection to GNUS-COMMAND-METHOD."
173 (when (stringp gnus-command-method)
174 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
175 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
176 ;; If this method was previously denied, we just return nil.
177 (if (eq (nth 1 elem) 'denied)
179 (gnus-message 1 "Denied server")
183 (funcall (gnus-get-function gnus-command-method 'open-server)
184 (nth 1 gnus-command-method)
185 (nthcdr 2 gnus-command-method))))
186 ;; If this hasn't been opened before, we add it to the list.
188 (setq elem (list gnus-command-method nil)
189 gnus-opened-servers (cons elem gnus-opened-servers)))
190 ;; Set the status of this server.
191 (setcar (cdr elem) (if result 'ok 'denied))
192 ;; Return the result from the "open" call.
195 (defun gnus-close-server (gnus-command-method)
196 "Close the connection to GNUS-COMMAND-METHOD."
197 (when (stringp gnus-command-method)
198 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
199 (funcall (gnus-get-function gnus-command-method 'close-server)
200 (nth 1 gnus-command-method)))
202 (defun gnus-request-list (gnus-command-method)
203 "Request the active file from GNUS-COMMAND-METHOD."
204 (when (stringp gnus-command-method)
205 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
206 (funcall (gnus-get-function gnus-command-method 'request-list)
207 (nth 1 gnus-command-method)))
209 (defun gnus-request-list-newsgroups (gnus-command-method)
210 "Request the newsgroups file from GNUS-COMMAND-METHOD."
211 (when (stringp gnus-command-method)
212 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
213 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
214 (nth 1 gnus-command-method)))
216 (defun gnus-request-newgroups (date gnus-command-method)
217 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
218 (when (stringp gnus-command-method)
219 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
220 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
222 (funcall func date (nth 1 gnus-command-method)))))
224 (defun gnus-server-opened (gnus-command-method)
225 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
226 (unless (eq (gnus-server-status gnus-command-method)
228 (when (stringp gnus-command-method)
229 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
230 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
231 (nth 1 gnus-command-method))))
233 (defun gnus-status-message (gnus-command-method)
234 "Return the status message from GNUS-COMMAND-METHOD.
235 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
236 this group uses will be queried."
237 (let ((gnus-command-method
238 (if (stringp gnus-command-method)
239 (gnus-find-method-for-group gnus-command-method)
240 gnus-command-method)))
241 (funcall (gnus-get-function gnus-command-method 'status-message)
242 (nth 1 gnus-command-method))))
244 (defun gnus-request-regenerate (gnus-command-method)
245 "Request a data generation 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-regenerate)
249 (nth 1 gnus-command-method)))
251 (defun gnus-request-group (group &optional dont-check gnus-command-method)
252 "Request GROUP. If DONT-CHECK, no information is required."
253 (let ((gnus-command-method
254 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
255 (when (stringp gnus-command-method)
256 (setq gnus-command-method
257 (inline (gnus-server-to-method gnus-command-method))))
258 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
259 (gnus-group-real-name group) (nth 1 gnus-command-method)
262 (defun gnus-list-active-group (group)
263 "Request active information on GROUP."
264 (let ((gnus-command-method (gnus-find-method-for-group group))
265 (func 'list-active-group))
266 (when (gnus-check-backend-function func group)
267 (funcall (gnus-get-function gnus-command-method func)
268 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
270 (defun gnus-request-group-description (group)
271 "Request a description of GROUP."
272 (let ((gnus-command-method (gnus-find-method-for-group group))
273 (func 'request-group-description))
274 (when (gnus-check-backend-function func group)
275 (funcall (gnus-get-function gnus-command-method func)
276 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
278 (defun gnus-request-group-articles (group)
279 "Request a list of existing articles in GROUP."
280 (let ((gnus-command-method (gnus-find-method-for-group group))
281 (func 'request-group-articles))
282 (when (gnus-check-backend-function func group)
283 (funcall (gnus-get-function gnus-command-method func)
284 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
286 (defun gnus-close-group (group)
287 "Request the GROUP be closed."
288 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
289 (funcall (gnus-get-function gnus-command-method 'close-group)
290 (gnus-group-real-name group) (nth 1 gnus-command-method))))
292 (defun gnus-retrieve-headers (articles group &optional fetch-old)
293 "Request headers for ARTICLES in GROUP.
294 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
295 (let ((gnus-command-method (gnus-find-method-for-group group)))
296 (if (and gnus-use-cache (numberp (car articles)))
297 (gnus-cache-retrieve-headers articles group fetch-old)
298 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
299 articles (gnus-group-real-name group)
300 (nth 1 gnus-command-method) fetch-old))))
302 (defun gnus-retrieve-parsed-headers (articles group &optional fetch-old
303 dependencies force-new)
304 "Request parsed-headers for ARTICLES in GROUP.
305 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
309 (set-buffer gnus-summary-buffer)
310 gnus-newsgroup-dependencies)))
311 (let ((gnus-command-method (gnus-find-method-for-group group))
313 (if (and gnus-use-cache (numberp (car articles)))
315 (gnus-cache-retrieve-parsed-headers articles group fetch-old
316 dependencies force-new))
317 (let ((func (gnus-get-function gnus-command-method
318 'retrieve-parsed-headers 'no-error)))
320 (setq headers (funcall func articles dependencies
321 (gnus-group-real-name group)
322 (nth 1 gnus-command-method) fetch-old
324 gnus-headers-retrieved-by (car headers)
325 headers (cdr headers))
326 (setq gnus-headers-retrieved-by
328 (gnus-get-function gnus-command-method 'retrieve-headers)
329 articles (gnus-group-real-name group)
330 (nth 1 gnus-command-method) fetch-old))
333 (if (eq gnus-headers-retrieved-by 'nov)
334 (gnus-get-newsgroup-headers-xover
335 articles nil dependencies gnus-newsgroup-name t)
336 (gnus-get-newsgroup-headers dependencies)))
339 (defun gnus-retrieve-articles (articles group)
340 "Request ARTICLES in GROUP."
341 (let ((gnus-command-method (gnus-find-method-for-group group)))
342 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
343 articles (gnus-group-real-name group)
344 (nth 1 gnus-command-method))))
346 (defun gnus-retrieve-groups (groups gnus-command-method)
347 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
348 (when (stringp gnus-command-method)
349 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
350 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
351 groups (nth 1 gnus-command-method)))
353 (defun gnus-request-type (group &optional article)
354 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
355 (let ((gnus-command-method (gnus-find-method-for-group group)))
356 (if (not (gnus-check-backend-function
357 'request-type (car gnus-command-method)))
359 (funcall (gnus-get-function gnus-command-method 'request-type)
360 (gnus-group-real-name group) article))))
362 (defun gnus-request-set-mark (group action)
363 "Set marks on articles in the backend."
364 (let ((gnus-command-method (gnus-find-method-for-group group)))
365 (if (not (gnus-check-backend-function
366 'request-set-mark (car gnus-command-method)))
368 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
369 (gnus-group-real-name group) action
370 (nth 1 gnus-command-method)))))
372 (defun gnus-request-update-mark (group article mark)
373 "Allow the backend to change the mark the user tries to put on an article."
374 (let ((gnus-command-method (gnus-find-method-for-group group)))
375 (if (not (gnus-check-backend-function
376 'request-update-mark (car gnus-command-method)))
378 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
379 (gnus-group-real-name group) article mark))))
381 (defun gnus-request-article (article group &optional buffer)
382 "Request the ARTICLE in GROUP.
383 ARTICLE can either be an article number or an article Message-ID.
384 If BUFFER, insert the article in that group."
385 (let ((gnus-command-method (gnus-find-method-for-group group)))
386 (funcall (gnus-get-function gnus-command-method 'request-article)
387 article (gnus-group-real-name group)
388 (nth 1 gnus-command-method) buffer)))
390 (defun gnus-request-head (article group)
391 "Request the head of ARTICLE in GROUP."
392 (let* ((gnus-command-method (gnus-find-method-for-group group))
393 (head (gnus-get-function gnus-command-method 'request-head t))
399 (gnus-cache-request-article article group))
400 (setq res (cons group article)
402 ;; Use `head' function.
404 (setq res (funcall head article (gnus-group-real-name group)
405 (nth 1 gnus-command-method))))
406 ;; Use `article' function.
408 (setq res (gnus-request-article article group)
412 (set-buffer nntp-server-buffer)
413 (goto-char (point-min))
414 (when (search-forward "\n\n" nil t)
415 (delete-region (1- (point)) (point-max)))
416 (nnheader-fold-continuation-lines)))
419 (defun gnus-request-body (article group)
420 "Request the body of ARTICLE in GROUP."
421 (let* ((gnus-command-method (gnus-find-method-for-group group))
422 (head (gnus-get-function gnus-command-method 'request-body t))
428 (gnus-cache-request-article article group))
429 (setq res (cons group article)
431 ;; Use `head' function.
433 (setq res (funcall head article (gnus-group-real-name group)
434 (nth 1 gnus-command-method))))
435 ;; Use `article' function.
437 (setq res (gnus-request-article article group)
441 (set-buffer nntp-server-buffer)
442 (goto-char (point-min))
443 (when (search-forward "\n\n" nil t)
444 (delete-region (point-min) (1- (point))))))
447 (defun gnus-request-post (gnus-command-method)
448 "Post the current buffer using GNUS-COMMAND-METHOD."
449 (when (stringp gnus-command-method)
450 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
451 (funcall (gnus-get-function gnus-command-method 'request-post)
452 (nth 1 gnus-command-method)))
454 (defun gnus-request-scan (group gnus-command-method)
455 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
456 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
457 (let ((gnus-command-method
458 (if group (gnus-find-method-for-group group) gnus-command-method))
459 (gnus-inhibit-demon t)
460 (mail-source-plugged gnus-plugged))
461 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
462 (funcall (gnus-get-function gnus-command-method 'request-scan)
463 (and group (gnus-group-real-name group))
464 (nth 1 gnus-command-method)))))
466 (defsubst gnus-request-update-info (info gnus-command-method)
467 "Request that GNUS-COMMAND-METHOD update INFO."
468 (when (stringp gnus-command-method)
469 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
470 (when (gnus-check-backend-function
471 'request-update-info (car gnus-command-method))
472 (funcall (gnus-get-function gnus-command-method 'request-update-info)
473 (gnus-group-real-name (gnus-info-group info))
474 info (nth 1 gnus-command-method))))
476 (defun gnus-request-expire-articles (articles group &optional force)
477 (let ((gnus-command-method (gnus-find-method-for-group group)))
478 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
479 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
482 (defun gnus-request-move-article
483 (article group server accept-function &optional last)
484 (let ((gnus-command-method (gnus-find-method-for-group group)))
485 (funcall (gnus-get-function gnus-command-method 'request-move-article)
486 article (gnus-group-real-name group)
487 (nth 1 gnus-command-method) accept-function last)))
489 (defun gnus-request-accept-article (group &optional gnus-command-method last
491 ;; Make sure there's a newline at the end of the article.
492 (when (stringp gnus-command-method)
493 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
494 (when (and (not gnus-command-method)
496 (setq gnus-command-method (gnus-group-name-to-method group)))
497 (goto-char (point-max))
500 (let ((func (car (or gnus-command-method
501 (gnus-find-method-for-group group)))))
502 (funcall (intern (format "%s-request-accept-article" func))
503 (if (stringp group) (gnus-group-real-name group) group)
504 (cadr gnus-command-method)
507 (defun gnus-request-replace-article (article group buffer &optional no-encode)
508 (let ((func (car (gnus-group-name-to-method group))))
509 (funcall (intern (format "%s-request-replace-article" func))
510 article (gnus-group-real-name group) buffer)))
512 (defun gnus-request-associate-buffer (group)
513 (let ((gnus-command-method (gnus-find-method-for-group group)))
514 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
515 (gnus-group-real-name group))))
517 (defun gnus-request-restore-buffer (article group)
518 "Request a new buffer restored to the state of ARTICLE."
519 (let ((gnus-command-method (gnus-find-method-for-group group)))
520 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
521 article (gnus-group-real-name group)
522 (nth 1 gnus-command-method))))
524 (defun gnus-request-create-group (group &optional gnus-command-method args)
525 (when (stringp gnus-command-method)
526 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
527 (let ((gnus-command-method
528 (or gnus-command-method (gnus-find-method-for-group group))))
529 (funcall (gnus-get-function gnus-command-method 'request-create-group)
530 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
532 (defun gnus-request-delete-group (group &optional force)
533 (let ((gnus-command-method (gnus-find-method-for-group group)))
534 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
535 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
537 (defun gnus-request-rename-group (group new-name)
538 (let ((gnus-command-method (gnus-find-method-for-group group)))
539 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
540 (gnus-group-real-name group)
541 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
543 (defun gnus-close-backends ()
544 ;; Send a close request to all backends that support such a request.
545 (let ((methods gnus-valid-select-methods)
546 (gnus-inhibit-demon t)
547 func gnus-command-method)
548 (while (setq gnus-command-method (pop methods))
549 (when (fboundp (setq func (intern
550 (concat (car gnus-command-method)
554 (defun gnus-asynchronous-p (gnus-command-method)
555 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
559 (defun gnus-remove-denial (gnus-command-method)
560 (when (stringp gnus-command-method)
561 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
562 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
563 (status (cadr elem)))
564 ;; If this hasn't been opened before, we add it to the list.
565 (when (eq status 'denied)
566 ;; Set the status of this server.
567 (setcar (cdr elem) 'closed))))
571 ;;; gnus-int.el ends here