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))
116 ;; Transform virtual server names into select methods.
117 (when (stringp method)
118 (setq method (gnus-server-to-method method)))
119 (if (gnus-server-opened method)
120 ;; The stream is already opened.
124 (gnus-message 5 "Opening %s server%s..." (car method)
125 (if (equal (nth 1 method) "") ""
126 (format " on %s" (nth 1 method)))))
127 (gnus-run-hooks 'gnus-open-server-hook)
130 (setq result (gnus-open-server method))
131 (quit (message "Quit gnus-check-server")
134 (gnus-message 5 "Opening %s server%s...%s" (car method)
135 (if (equal (nth 1 method) "") ""
136 (format " on %s" (nth 1 method)))
137 (if result "done" "failed")))))))
139 (defun gnus-get-function (method function &optional noerror)
140 "Return a function symbol based on METHOD and FUNCTION."
141 ;; Translate server names into methods.
143 (error "Attempted use of a nil select method"))
144 (when (stringp method)
145 (setq method (gnus-server-to-method method)))
146 ;; Check cache of constructed names.
147 (let* ((method-sym (if gnus-agent
148 (gnus-agent-get-function method)
150 (method-fns (get method-sym 'gnus-method-functions))
151 (func (let ((method-fnlist-elt (assq function method-fns)))
152 (unless method-fnlist-elt
153 (setq method-fnlist-elt
155 (intern (format "%s-%s" method-sym function))))
156 (put method-sym 'gnus-method-functions
157 (cons method-fnlist-elt method-fns)))
158 (cdr method-fnlist-elt))))
159 ;; Maybe complain if there is no function.
160 (unless (fboundp func)
162 (error "Trying to require a method that doesn't exist"))
163 (require (car method))
164 (when (not (fboundp func))
167 (error "No such function: %s" func))))
172 ;;; Interface functions to the backends.
175 (defun gnus-open-server (gnus-command-method)
176 "Open a connection to GNUS-COMMAND-METHOD."
177 (when (stringp gnus-command-method)
178 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
179 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
180 ;; If this method was previously denied, we just return nil.
181 (if (eq (nth 1 elem) 'denied)
183 (gnus-message 1 "Denied server")
188 (funcall (gnus-get-function gnus-command-method 'open-server)
189 (nth 1 gnus-command-method)
190 (nthcdr 2 gnus-command-method))
192 (message "Quit trying to open server")
194 ;; If this hasn't been opened before, we add it to the list.
196 (setq elem (list gnus-command-method nil)
197 gnus-opened-servers (cons elem gnus-opened-servers)))
198 ;; Set the status of this server.
199 (setcar (cdr elem) (if result 'ok 'denied))
200 ;; Return the result from the "open" call.
203 (defun gnus-close-server (gnus-command-method)
204 "Close the connection to GNUS-COMMAND-METHOD."
205 (when (stringp gnus-command-method)
206 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
207 (funcall (gnus-get-function gnus-command-method 'close-server)
208 (nth 1 gnus-command-method)))
210 (defun gnus-request-list (gnus-command-method)
211 "Request the active file from GNUS-COMMAND-METHOD."
212 (when (stringp gnus-command-method)
213 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
214 (funcall (gnus-get-function gnus-command-method 'request-list)
215 (nth 1 gnus-command-method)))
217 (defun gnus-request-list-newsgroups (gnus-command-method)
218 "Request the newsgroups file from GNUS-COMMAND-METHOD."
219 (when (stringp gnus-command-method)
220 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
221 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
222 (nth 1 gnus-command-method)))
224 (defun gnus-request-newgroups (date gnus-command-method)
225 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
226 (when (stringp gnus-command-method)
227 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
228 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
230 (funcall func date (nth 1 gnus-command-method)))))
232 (defun gnus-server-opened (gnus-command-method)
233 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
234 (unless (eq (gnus-server-status gnus-command-method)
236 (when (stringp gnus-command-method)
237 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
238 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
239 (nth 1 gnus-command-method))))
241 (defun gnus-status-message (gnus-command-method)
242 "Return the status message from GNUS-COMMAND-METHOD.
243 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
244 this group uses will be queried."
245 (let ((gnus-command-method
246 (if (stringp gnus-command-method)
247 (gnus-find-method-for-group gnus-command-method)
248 gnus-command-method)))
249 (funcall (gnus-get-function gnus-command-method 'status-message)
250 (nth 1 gnus-command-method))))
252 (defun gnus-request-regenerate (gnus-command-method)
253 "Request a data generation from GNUS-COMMAND-METHOD."
254 (when (stringp gnus-command-method)
255 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
256 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
257 (nth 1 gnus-command-method)))
259 (defun gnus-request-group (group &optional dont-check gnus-command-method)
260 "Request GROUP. If DONT-CHECK, no information is required."
261 (let ((gnus-command-method
262 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
263 (when (stringp gnus-command-method)
264 (setq gnus-command-method
265 (inline (gnus-server-to-method gnus-command-method))))
266 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
267 (gnus-group-real-name group) (nth 1 gnus-command-method)
270 (defun gnus-list-active-group (group)
271 "Request active information on GROUP."
272 (let ((gnus-command-method (gnus-find-method-for-group group))
273 (func 'list-active-group))
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-description (group)
279 "Request a description of GROUP."
280 (let ((gnus-command-method (gnus-find-method-for-group group))
281 (func 'request-group-description))
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-request-group-articles (group)
287 "Request a list of existing articles in GROUP."
288 (let ((gnus-command-method (gnus-find-method-for-group group))
289 (func 'request-group-articles))
290 (when (gnus-check-backend-function func group)
291 (funcall (gnus-get-function gnus-command-method func)
292 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
294 (defun gnus-close-group (group)
295 "Request the GROUP be closed."
296 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
297 (funcall (gnus-get-function gnus-command-method 'close-group)
298 (gnus-group-real-name group) (nth 1 gnus-command-method))))
300 (defun gnus-retrieve-headers (articles group &optional fetch-old)
301 "Request headers for ARTICLES in GROUP.
302 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
303 (let ((gnus-command-method (gnus-find-method-for-group group)))
304 (if (and gnus-use-cache (numberp (car articles)))
305 (gnus-cache-retrieve-headers articles group fetch-old)
306 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
307 articles (gnus-group-real-name group)
308 (nth 1 gnus-command-method) fetch-old))))
310 (defun gnus-retrieve-articles (articles group)
311 "Request ARTICLES in GROUP."
312 (let ((gnus-command-method (gnus-find-method-for-group group)))
313 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
314 articles (gnus-group-real-name group)
315 (nth 1 gnus-command-method))))
317 (defun gnus-retrieve-groups (groups gnus-command-method)
318 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
319 (when (stringp gnus-command-method)
320 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
321 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
322 groups (nth 1 gnus-command-method)))
324 (defun gnus-request-type (group &optional article)
325 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
326 (let ((gnus-command-method (gnus-find-method-for-group group)))
327 (if (not (gnus-check-backend-function
328 'request-type (car gnus-command-method)))
330 (funcall (gnus-get-function gnus-command-method 'request-type)
331 (gnus-group-real-name group) article))))
333 (defun gnus-request-set-mark (group action)
334 "Set marks on articles in the backend."
335 (let ((gnus-command-method (gnus-find-method-for-group group)))
336 (if (not (gnus-check-backend-function
337 'request-set-mark (car gnus-command-method)))
339 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
340 (gnus-group-real-name group) action
341 (nth 1 gnus-command-method)))))
343 (defun gnus-request-update-mark (group article mark)
344 "Allow the backend to change the mark the user tries to put on an article."
345 (let ((gnus-command-method (gnus-find-method-for-group group)))
346 (if (not (gnus-check-backend-function
347 'request-update-mark (car gnus-command-method)))
349 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
350 (gnus-group-real-name group) article mark))))
352 (defun gnus-request-article (article group &optional buffer)
353 "Request the ARTICLE in GROUP.
354 ARTICLE can either be an article number or an article Message-ID.
355 If BUFFER, insert the article in that group."
356 (let ((gnus-command-method (gnus-find-method-for-group group)))
357 (funcall (gnus-get-function gnus-command-method 'request-article)
358 article (gnus-group-real-name group)
359 (nth 1 gnus-command-method) buffer)))
361 (defun gnus-request-head (article group)
362 "Request the head of ARTICLE in GROUP."
363 (let* ((gnus-command-method (gnus-find-method-for-group group))
364 (head (gnus-get-function gnus-command-method 'request-head t))
370 (gnus-cache-request-article article group))
371 (setq res (cons group article)
373 ;; Use `head' function.
375 (setq res (funcall head article (gnus-group-real-name group)
376 (nth 1 gnus-command-method))))
377 ;; Use `article' function.
379 (setq res (gnus-request-article article group)
383 (set-buffer nntp-server-buffer)
384 (goto-char (point-min))
385 (when (search-forward "\n\n" nil t)
386 (delete-region (1- (point)) (point-max)))
387 (nnheader-fold-continuation-lines)))
390 (defun gnus-request-body (article group)
391 "Request the body of ARTICLE in GROUP."
392 (let* ((gnus-command-method (gnus-find-method-for-group group))
393 (head (gnus-get-function gnus-command-method 'request-body 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 (point-min) (1- (point))))))
418 (defun gnus-request-post (gnus-command-method)
419 "Post the current buffer using GNUS-COMMAND-METHOD."
420 (when (stringp gnus-command-method)
421 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
422 (funcall (gnus-get-function gnus-command-method 'request-post)
423 (nth 1 gnus-command-method)))
425 (defun gnus-request-scan (group gnus-command-method)
426 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
427 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
428 (let ((gnus-command-method
429 (if group (gnus-find-method-for-group group) gnus-command-method))
430 (gnus-inhibit-demon t)
431 (mail-source-plugged gnus-plugged))
432 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
433 (funcall (gnus-get-function gnus-command-method 'request-scan)
434 (and group (gnus-group-real-name group))
435 (nth 1 gnus-command-method)))))
437 (defsubst gnus-request-update-info (info gnus-command-method)
438 "Request that GNUS-COMMAND-METHOD update INFO."
439 (when (stringp gnus-command-method)
440 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
441 (when (gnus-check-backend-function
442 'request-update-info (car gnus-command-method))
443 (funcall (gnus-get-function gnus-command-method 'request-update-info)
444 (gnus-group-real-name (gnus-info-group info))
445 info (nth 1 gnus-command-method))))
447 (defun gnus-request-expire-articles (articles group &optional force)
448 (let ((gnus-command-method (gnus-find-method-for-group group)))
449 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
450 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
453 (defun gnus-request-move-article
454 (article group server accept-function &optional last)
455 (let ((gnus-command-method (gnus-find-method-for-group group)))
456 (funcall (gnus-get-function gnus-command-method 'request-move-article)
457 article (gnus-group-real-name group)
458 (nth 1 gnus-command-method) accept-function last)))
460 (defun gnus-request-accept-article (group &optional gnus-command-method last
462 ;; Make sure there's a newline at the end of the article.
463 (when (stringp gnus-command-method)
464 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
465 (when (and (not gnus-command-method)
467 (setq gnus-command-method (gnus-group-name-to-method group)))
468 (goto-char (point-max))
471 (let ((func (car (or gnus-command-method
472 (gnus-find-method-for-group group)))))
473 (funcall (intern (format "%s-request-accept-article" func))
474 (if (stringp group) (gnus-group-real-name group) group)
475 (cadr gnus-command-method)
478 (defun gnus-request-replace-article (article group buffer &optional no-encode)
479 (let ((func (car (gnus-group-name-to-method group))))
480 (funcall (intern (format "%s-request-replace-article" func))
481 article (gnus-group-real-name group) buffer)))
483 (defun gnus-request-associate-buffer (group)
484 (let ((gnus-command-method (gnus-find-method-for-group group)))
485 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
486 (gnus-group-real-name group))))
488 (defun gnus-request-restore-buffer (article group)
489 "Request a new buffer restored to the state of ARTICLE."
490 (let ((gnus-command-method (gnus-find-method-for-group group)))
491 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
492 article (gnus-group-real-name group)
493 (nth 1 gnus-command-method))))
495 (defun gnus-request-create-group (group &optional gnus-command-method args)
496 (when (stringp gnus-command-method)
497 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
498 (let ((gnus-command-method
499 (or gnus-command-method (gnus-find-method-for-group group))))
500 (funcall (gnus-get-function gnus-command-method 'request-create-group)
501 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
503 (defun gnus-request-delete-group (group &optional force)
504 (let ((gnus-command-method (gnus-find-method-for-group group)))
505 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
506 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
508 (defun gnus-request-rename-group (group new-name)
509 (let ((gnus-command-method (gnus-find-method-for-group group)))
510 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
511 (gnus-group-real-name group)
512 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
514 (defun gnus-close-backends ()
515 ;; Send a close request to all backends that support such a request.
516 (let ((methods gnus-valid-select-methods)
517 (gnus-inhibit-demon t)
518 func gnus-command-method)
519 (while (setq gnus-command-method (pop methods))
520 (when (fboundp (setq func (intern
521 (concat (car gnus-command-method)
525 (defun gnus-asynchronous-p (gnus-command-method)
526 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
530 (defun gnus-remove-denial (gnus-command-method)
531 (when (stringp gnus-command-method)
532 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
533 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
534 (status (cadr elem)))
535 ;; If this hasn't been opened before, we add it to the list.
536 (when (eq status 'denied)
537 ;; Set the status of this server.
538 (setcar (cdr elem) 'closed))))
542 ;;; gnus-int.el ends here