1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
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 (concat "~/" (substring
75 gnus-nntp-server 1)))))
76 (list 'nnmh-get-new-mail nil)))
78 (list 'nntp gnus-nntp-server)))))
80 (setq how (car gnus-select-method))
84 (gnus-message 5 "Looking up local news spool..."))
87 (gnus-message 5 "Looking up mh spool..."))
90 (setq gnus-current-select-method gnus-select-method)
91 (gnus-run-hooks 'gnus-open-server-hook)
93 ;; gnus-open-server-hook might have opened it
94 (gnus-server-opened gnus-select-method)
95 (gnus-open-server gnus-select-method)
99 "%s (%s) open error: '%s'. Continue? "
100 (car gnus-select-method) (cadr gnus-select-method)
101 (gnus-status-message gnus-select-method)))
102 (gnus-error 1 "Couldn't open server on %s"
103 (nth 1 gnus-select-method))))))
105 (defun gnus-check-group (group)
106 "Try to make sure that the server where GROUP exists is alive."
107 (let ((method (gnus-find-method-for-group group)))
108 (or (gnus-server-opened method)
109 (gnus-open-server method))))
111 (defun gnus-check-server (&optional method silent)
112 "Check whether the connection to METHOD is down.
113 If METHOD is nil, use `gnus-select-method'.
114 If it is down, start it up (again)."
115 (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)
129 (gnus-open-server method)
133 (defun gnus-get-function (method function &optional noerror)
134 "Return a function symbol based on METHOD and FUNCTION."
135 ;; Translate server names into methods.
137 (error "Attempted use of a nil select method"))
138 (when (stringp method)
139 (setq method (gnus-server-to-method method)))
140 ;; Check cache of constructed names.
141 (let* ((method-sym (if gnus-agent
142 (gnus-agent-get-function method)
144 (method-fns (get method-sym 'gnus-method-functions))
145 (func (let ((method-fnlist-elt (assq function method-fns)))
146 (unless method-fnlist-elt
147 (setq method-fnlist-elt
149 (intern (format "%s-%s" method-sym function))))
150 (put method-sym 'gnus-method-functions
151 (cons method-fnlist-elt method-fns)))
152 (cdr method-fnlist-elt))))
153 ;; Maybe complain if there is no function.
154 (unless (fboundp func)
156 (error "Trying to require a method that doesn't exist"))
157 (require (car method))
158 (when (not (fboundp func))
161 (error "No such function: %s" func))))
166 ;;; Interface functions to the backends.
169 (defun gnus-open-server (gnus-command-method)
170 "Open a connection to GNUS-COMMAND-METHOD."
171 (when (stringp gnus-command-method)
172 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
173 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
174 ;; If this method was previously denied, we just return nil.
175 (if (eq (nth 1 elem) 'denied)
177 (gnus-message 1 "Denied server")
181 (funcall (gnus-get-function gnus-command-method 'open-server)
182 (nth 1 gnus-command-method)
183 (nthcdr 2 gnus-command-method))))
184 ;; If this hasn't been opened before, we add it to the list.
186 (setq elem (list gnus-command-method nil)
187 gnus-opened-servers (cons elem gnus-opened-servers)))
188 ;; Set the status of this server.
189 (setcar (cdr elem) (if result 'ok 'denied))
190 ;; Return the result from the "open" call.
193 (defun gnus-close-server (gnus-command-method)
194 "Close the connection to GNUS-COMMAND-METHOD."
195 (when (stringp gnus-command-method)
196 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
197 (funcall (gnus-get-function gnus-command-method 'close-server)
198 (nth 1 gnus-command-method)))
200 (defun gnus-request-list (gnus-command-method)
201 "Request the active file from GNUS-COMMAND-METHOD."
202 (when (stringp gnus-command-method)
203 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
204 (funcall (gnus-get-function gnus-command-method 'request-list)
205 (nth 1 gnus-command-method)))
207 (defun gnus-request-list-newsgroups (gnus-command-method)
208 "Request the newsgroups file from GNUS-COMMAND-METHOD."
209 (when (stringp gnus-command-method)
210 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
211 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
212 (nth 1 gnus-command-method)))
214 (defun gnus-request-newgroups (date gnus-command-method)
215 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
216 (when (stringp gnus-command-method)
217 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
218 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
220 (funcall func date (nth 1 gnus-command-method)))))
222 (defun gnus-server-opened (gnus-command-method)
223 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
224 (unless (eq (gnus-server-status gnus-command-method)
226 (when (stringp gnus-command-method)
227 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
228 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
229 (nth 1 gnus-command-method))))
231 (defun gnus-status-message (gnus-command-method)
232 "Return the status message from GNUS-COMMAND-METHOD.
233 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
234 this group uses will be queried."
235 (let ((gnus-command-method
236 (if (stringp gnus-command-method)
237 (gnus-find-method-for-group gnus-command-method)
238 gnus-command-method)))
239 (funcall (gnus-get-function gnus-command-method 'status-message)
240 (nth 1 gnus-command-method))))
242 (defun gnus-request-regenerate (gnus-command-method)
243 "Request a data generation from GNUS-COMMAND-METHOD."
244 (when (stringp gnus-command-method)
245 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
246 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
247 (nth 1 gnus-command-method)))
249 (defun gnus-request-group (group &optional dont-check gnus-command-method)
250 "Request GROUP. If DONT-CHECK, no information is required."
251 (let ((gnus-command-method
252 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
253 (when (stringp gnus-command-method)
254 (setq gnus-command-method
255 (inline (gnus-server-to-method gnus-command-method))))
256 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
257 (gnus-group-real-name group) (nth 1 gnus-command-method)
260 (defun gnus-list-active-group (group)
261 "Request active information on GROUP."
262 (let ((gnus-command-method (gnus-find-method-for-group group))
263 (func 'list-active-group))
264 (when (gnus-check-backend-function func group)
265 (funcall (gnus-get-function gnus-command-method func)
266 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
268 (defun gnus-request-group-description (group)
269 "Request a description of GROUP."
270 (let ((gnus-command-method (gnus-find-method-for-group group))
271 (func 'request-group-description))
272 (when (gnus-check-backend-function func group)
273 (funcall (gnus-get-function gnus-command-method func)
274 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
276 (defun gnus-request-group-articles (group)
277 "Request a list of existing articles in GROUP."
278 (let ((gnus-command-method (gnus-find-method-for-group group))
279 (func 'request-group-articles))
280 (when (gnus-check-backend-function func group)
281 (funcall (gnus-get-function gnus-command-method func)
282 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
284 (defun gnus-close-group (group)
285 "Request the GROUP be closed."
286 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
287 (funcall (gnus-get-function gnus-command-method 'close-group)
288 (gnus-group-real-name group) (nth 1 gnus-command-method))))
290 (defun gnus-retrieve-headers (articles group &optional fetch-old)
291 "Request headers for ARTICLES in GROUP.
292 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
293 (let ((gnus-command-method (gnus-find-method-for-group group)))
294 (if (and gnus-use-cache (numberp (car articles)))
295 (gnus-cache-retrieve-headers articles group fetch-old)
296 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
297 articles (gnus-group-real-name group)
298 (nth 1 gnus-command-method) fetch-old))))
300 (defun gnus-retrieve-parsed-headers (articles group &optional fetch-old
301 dependencies force-new)
302 "Request parsed-headers for ARTICLES in GROUP.
303 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
307 (set-buffer gnus-summary-buffer)
308 gnus-newsgroup-dependencies)))
309 (let ((gnus-command-method (gnus-find-method-for-group group))
311 (if (and gnus-use-cache (numberp (car articles)))
313 (gnus-cache-retrieve-parsed-headers articles group fetch-old
314 dependencies force-new))
315 (let ((func (gnus-get-function gnus-command-method
316 'retrieve-parsed-headers 'no-error)))
318 (setq headers (funcall func articles dependencies
319 (gnus-group-real-name group)
320 (nth 1 gnus-command-method) fetch-old
322 gnus-headers-retrieved-by (car headers)
323 headers (cdr headers))
324 (setq gnus-headers-retrieved-by
326 (gnus-get-function gnus-command-method 'retrieve-headers)
327 articles (gnus-group-real-name group)
328 (nth 1 gnus-command-method) fetch-old))
331 (if (eq gnus-headers-retrieved-by 'nov)
332 (gnus-get-newsgroup-headers-xover
333 articles nil dependencies gnus-newsgroup-name t)
334 (gnus-get-newsgroup-headers dependencies)))
337 (defun gnus-retrieve-articles (articles group)
338 "Request ARTICLES in GROUP."
339 (let ((gnus-command-method (gnus-find-method-for-group group)))
340 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
341 articles (gnus-group-real-name group)
342 (nth 1 gnus-command-method))))
344 (defun gnus-retrieve-groups (groups gnus-command-method)
345 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
346 (when (stringp gnus-command-method)
347 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
348 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
349 groups (nth 1 gnus-command-method)))
351 (defun gnus-request-type (group &optional article)
352 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
353 (let ((gnus-command-method (gnus-find-method-for-group group)))
354 (if (not (gnus-check-backend-function
355 'request-type (car gnus-command-method)))
357 (funcall (gnus-get-function gnus-command-method 'request-type)
358 (gnus-group-real-name group) article))))
360 (defun gnus-request-set-mark (group action)
361 "Set marks on articles in the backend."
362 (let ((gnus-command-method (gnus-find-method-for-group group)))
363 (if (not (gnus-check-backend-function
364 'request-set-mark (car gnus-command-method)))
366 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
367 (gnus-group-real-name group) action
368 (nth 1 gnus-command-method)))))
370 (defun gnus-request-update-mark (group article mark)
371 "Allow the backend to change the mark the user tries to put on an article."
372 (let ((gnus-command-method (gnus-find-method-for-group group)))
373 (if (not (gnus-check-backend-function
374 'request-update-mark (car gnus-command-method)))
376 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
377 (gnus-group-real-name group) article mark))))
379 (defun gnus-request-article (article group &optional buffer)
380 "Request the ARTICLE in GROUP.
381 ARTICLE can either be an article number or an article Message-ID.
382 If BUFFER, insert the article in that group."
383 (let ((gnus-command-method (gnus-find-method-for-group group)))
384 (funcall (gnus-get-function gnus-command-method 'request-article)
385 article (gnus-group-real-name group)
386 (nth 1 gnus-command-method) buffer)))
388 (defun gnus-request-head (article group)
389 "Request the head of ARTICLE in GROUP."
390 (let* ((gnus-command-method (gnus-find-method-for-group group))
391 (head (gnus-get-function gnus-command-method 'request-head t))
397 (gnus-cache-request-article article group))
398 (setq res (cons group article)
400 ;; Use `head' function.
402 (setq res (funcall head article (gnus-group-real-name group)
403 (nth 1 gnus-command-method))))
404 ;; Use `article' function.
406 (setq res (gnus-request-article article group)
410 (set-buffer nntp-server-buffer)
411 (goto-char (point-min))
412 (when (search-forward "\n\n" nil t)
413 (delete-region (1- (point)) (point-max)))
414 (nnheader-fold-continuation-lines)))
417 (defun gnus-request-body (article group)
418 "Request the body of ARTICLE in GROUP."
419 (let* ((gnus-command-method (gnus-find-method-for-group group))
420 (head (gnus-get-function gnus-command-method 'request-body t))
426 (gnus-cache-request-article article group))
427 (setq res (cons group article)
429 ;; Use `head' function.
431 (setq res (funcall head article (gnus-group-real-name group)
432 (nth 1 gnus-command-method))))
433 ;; Use `article' function.
435 (setq res (gnus-request-article article group)
439 (set-buffer nntp-server-buffer)
440 (goto-char (point-min))
441 (when (search-forward "\n\n" nil t)
442 (delete-region (point-min) (1- (point))))))
445 (defun gnus-request-post (gnus-command-method)
446 "Post the current buffer using GNUS-COMMAND-METHOD."
447 (when (stringp gnus-command-method)
448 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
449 (funcall (gnus-get-function gnus-command-method 'request-post)
450 (nth 1 gnus-command-method)))
452 (defun gnus-request-scan (group gnus-command-method)
453 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
454 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
455 (let ((gnus-command-method
456 (if group (gnus-find-method-for-group group) gnus-command-method))
457 (gnus-inhibit-demon t)
458 (mail-source-plugged gnus-plugged))
459 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
460 (funcall (gnus-get-function gnus-command-method 'request-scan)
461 (and group (gnus-group-real-name group))
462 (nth 1 gnus-command-method)))))
464 (defsubst gnus-request-update-info (info gnus-command-method)
465 "Request that GNUS-COMMAND-METHOD update INFO."
466 (when (stringp gnus-command-method)
467 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
468 (when (gnus-check-backend-function
469 'request-update-info (car gnus-command-method))
470 (funcall (gnus-get-function gnus-command-method 'request-update-info)
471 (gnus-group-real-name (gnus-info-group info))
472 info (nth 1 gnus-command-method))))
474 (defun gnus-request-expire-articles (articles group &optional force)
475 (let ((gnus-command-method (gnus-find-method-for-group group)))
476 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
477 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
480 (defun gnus-request-move-article
481 (article group server accept-function &optional last)
482 (let ((gnus-command-method (gnus-find-method-for-group group)))
483 (funcall (gnus-get-function gnus-command-method 'request-move-article)
484 article (gnus-group-real-name group)
485 (nth 1 gnus-command-method) accept-function last)))
487 (defun gnus-request-accept-article (group &optional gnus-command-method last
489 ;; Make sure there's a newline at the end of the article.
490 (when (stringp gnus-command-method)
491 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
492 (when (and (not gnus-command-method)
494 (setq gnus-command-method (gnus-group-name-to-method group)))
495 (goto-char (point-max))
498 (let ((func (car (or gnus-command-method
499 (gnus-find-method-for-group group)))))
500 (funcall (intern (format "%s-request-accept-article" func))
501 (if (stringp group) (gnus-group-real-name group) group)
502 (cadr gnus-command-method)
505 (defun gnus-request-replace-article (article group buffer &optional no-encode)
506 (let ((func (car (gnus-group-name-to-method group))))
507 (funcall (intern (format "%s-request-replace-article" func))
508 article (gnus-group-real-name group) buffer)))
510 (defun gnus-request-associate-buffer (group)
511 (let ((gnus-command-method (gnus-find-method-for-group group)))
512 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
513 (gnus-group-real-name group))))
515 (defun gnus-request-restore-buffer (article group)
516 "Request a new buffer restored to the state of ARTICLE."
517 (let ((gnus-command-method (gnus-find-method-for-group group)))
518 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
519 article (gnus-group-real-name group)
520 (nth 1 gnus-command-method))))
522 (defun gnus-request-create-group (group &optional gnus-command-method args)
523 (when (stringp gnus-command-method)
524 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
525 (let ((gnus-command-method
526 (or gnus-command-method (gnus-find-method-for-group group))))
527 (funcall (gnus-get-function gnus-command-method 'request-create-group)
528 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
530 (defun gnus-request-delete-group (group &optional force)
531 (let ((gnus-command-method (gnus-find-method-for-group group)))
532 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
533 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
535 (defun gnus-request-rename-group (group new-name)
536 (let ((gnus-command-method (gnus-find-method-for-group group)))
537 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
538 (gnus-group-real-name group)
539 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
541 (defun gnus-close-backends ()
542 ;; Send a close request to all backends that support such a request.
543 (let ((methods gnus-valid-select-methods)
544 (gnus-inhibit-demon t)
545 func gnus-command-method)
546 (while (setq gnus-command-method (pop methods))
547 (when (fboundp (setq func (intern
548 (concat (car gnus-command-method)
552 (defun gnus-asynchronous-p (gnus-command-method)
553 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
557 (defun gnus-remove-denial (gnus-command-method)
558 (when (stringp gnus-command-method)
559 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
560 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
561 (status (cadr elem)))
562 ;; If this hasn't been opened before, we add it to the list.
563 (when (eq status 'denied)
564 ;; Set the status of this server.
565 (setcar (cdr elem) 'closed))))
569 ;;; gnus-int.el ends here