1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
32 (defcustom gnus-open-server-hook nil
33 "Hook called just before opening connection to the news server."
38 ;;; Server Communication
41 (defun gnus-start-news-server (&optional confirm)
42 "Open a method for getting news.
43 If CONFIRM is non-nil, the user will be asked for an NNTP server."
45 (if gnus-current-select-method
46 ;; Stream is already opened.
49 (unless gnus-nntp-service
50 (setq gnus-nntp-server nil))
52 ;; Read server name with completion.
53 (setq gnus-nntp-server
54 (completing-read "NNTP server: "
55 (mapcar (lambda (server) (list server))
56 (cons (list gnus-nntp-server)
57 gnus-secondary-servers))
58 nil nil gnus-nntp-server)))
60 (when (and gnus-nntp-server
61 (stringp gnus-nntp-server)
62 (not (string= gnus-nntp-server "")))
63 (setq gnus-select-method
64 (cond ((or (string= gnus-nntp-server "")
65 (string= gnus-nntp-server "::"))
66 (list 'nnspool (system-name)))
67 ((string-match "^:" gnus-nntp-server)
68 (list 'nnmh gnus-nntp-server
70 (file-name-as-directory
72 (concat "~/" (substring
73 gnus-nntp-server 1)))))
74 (list 'nnmh-get-new-mail nil)))
76 (list 'nntp gnus-nntp-server)))))
78 (setq how (car gnus-select-method))
82 (gnus-message 5 "Looking up local news spool..."))
85 (gnus-message 5 "Looking up mh spool..."))
88 (setq gnus-current-select-method gnus-select-method)
89 (gnus-run-hooks 'gnus-open-server-hook)
91 ;; gnus-open-server-hook might have opened it
92 (gnus-server-opened gnus-select-method)
93 (gnus-open-server gnus-select-method)
97 "%s (%s) open error: '%s'. Continue? "
98 (car gnus-select-method) (cadr gnus-select-method)
99 (gnus-status-message gnus-select-method)))
100 (gnus-error 1 "Couldn't open server on %s"
101 (nth 1 gnus-select-method))))))
103 (defun gnus-check-group (group)
104 "Try to make sure that the server where GROUP exists is alive."
105 (let ((method (gnus-find-method-for-group group)))
106 (or (gnus-server-opened method)
107 (gnus-open-server method))))
109 (defun gnus-check-server (&optional method silent)
110 "Check whether the connection to METHOD is down.
111 If METHOD is nil, use `gnus-select-method'.
112 If it is down, start it up (again)."
113 (let ((method (or method gnus-select-method)))
114 ;; Transform virtual server names into select methods.
115 (when (stringp method)
116 (setq method (gnus-server-to-method method)))
117 (if (gnus-server-opened method)
118 ;; The stream is already opened.
122 (gnus-message 5 "Opening %s server%s..." (car method)
123 (if (equal (nth 1 method) "") ""
124 (format " on %s" (nth 1 method)))))
125 (gnus-run-hooks 'gnus-open-server-hook)
127 (gnus-open-server method)
131 (defun gnus-get-function (method function &optional noerror)
132 "Return a function symbol based on METHOD and FUNCTION."
133 ;; Translate server names into methods.
135 (error "Attempted use of a nil select method"))
136 (when (stringp method)
137 (setq method (gnus-server-to-method method)))
138 ;; Check cache of constructed names.
139 (let* ((method-sym (if gnus-agent
140 (gnus-agent-get-function method)
142 (method-fns (get method-sym 'gnus-method-functions))
143 (func (let ((method-fnlist-elt (assq function method-fns)))
144 (unless method-fnlist-elt
145 (setq method-fnlist-elt
147 (intern (format "%s-%s" method-sym function))))
148 (put method-sym 'gnus-method-functions
149 (cons method-fnlist-elt method-fns)))
150 (cdr method-fnlist-elt))))
151 ;; Maybe complain if there is no function.
152 (unless (fboundp func)
154 (error "Trying to require a method that doesn't exist"))
155 (require (car method))
156 (when (not (fboundp func))
159 (error "No such function: %s" func))))
164 ;;; Interface functions to the backends.
167 (defun gnus-open-server (gnus-command-method)
168 "Open a connection to GNUS-COMMAND-METHOD."
169 (when (stringp gnus-command-method)
170 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
171 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
172 ;; If this method was previously denied, we just return nil.
173 (if (eq (nth 1 elem) 'denied)
175 (gnus-message 1 "Denied server")
179 (funcall (gnus-get-function gnus-command-method 'open-server)
180 (nth 1 gnus-command-method)
181 (nthcdr 2 gnus-command-method))))
182 ;; If this hasn't been opened before, we add it to the list.
184 (setq elem (list gnus-command-method nil)
185 gnus-opened-servers (cons elem gnus-opened-servers)))
186 ;; Set the status of this server.
187 (setcar (cdr elem) (if result 'ok 'denied))
188 ;; Return the result from the "open" call.
191 (defun gnus-close-server (gnus-command-method)
192 "Close the connection to GNUS-COMMAND-METHOD."
193 (when (stringp gnus-command-method)
194 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
195 (funcall (gnus-get-function gnus-command-method 'close-server)
196 (nth 1 gnus-command-method)))
198 (defun gnus-request-list (gnus-command-method)
199 "Request the active file from GNUS-COMMAND-METHOD."
200 (when (stringp gnus-command-method)
201 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
202 (funcall (gnus-get-function gnus-command-method 'request-list)
203 (nth 1 gnus-command-method)))
205 (defun gnus-request-list-newsgroups (gnus-command-method)
206 "Request the newsgroups file from GNUS-COMMAND-METHOD."
207 (when (stringp gnus-command-method)
208 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
209 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
210 (nth 1 gnus-command-method)))
212 (defun gnus-request-newgroups (date gnus-command-method)
213 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
214 (when (stringp gnus-command-method)
215 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
216 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
218 (funcall func date (nth 1 gnus-command-method)))))
220 (defun gnus-server-opened (gnus-command-method)
221 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
222 (when (stringp gnus-command-method)
223 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
224 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
225 (nth 1 gnus-command-method)))
227 (defun gnus-status-message (gnus-command-method)
228 "Return the status message from GNUS-COMMAND-METHOD.
229 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
230 this group uses will be queried."
231 (let ((gnus-command-method
232 (if (stringp gnus-command-method)
233 (gnus-find-method-for-group gnus-command-method)
234 gnus-command-method)))
235 (funcall (gnus-get-function gnus-command-method 'status-message)
236 (nth 1 gnus-command-method))))
238 (defun gnus-request-regenerate (gnus-command-method)
239 "Request a data generation from GNUS-COMMAND-METHOD."
240 (when (stringp gnus-command-method)
241 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
242 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
243 (nth 1 gnus-command-method)))
245 (defun gnus-request-group (group &optional dont-check gnus-command-method)
246 "Request GROUP. If DONT-CHECK, no information is required."
247 (let ((gnus-command-method
248 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
249 (when (stringp gnus-command-method)
250 (setq gnus-command-method
251 (inline (gnus-server-to-method gnus-command-method))))
252 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
253 (gnus-group-real-name group) (nth 1 gnus-command-method)
256 (defun gnus-list-active-group (group)
257 "Request active information on GROUP."
258 (let ((gnus-command-method (gnus-find-method-for-group group))
259 (func 'list-active-group))
260 (when (gnus-check-backend-function func group)
261 (funcall (gnus-get-function gnus-command-method func)
262 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
264 (defun gnus-request-group-description (group)
265 "Request a description of GROUP."
266 (let ((gnus-command-method (gnus-find-method-for-group group))
267 (func 'request-group-description))
268 (when (gnus-check-backend-function func group)
269 (funcall (gnus-get-function gnus-command-method func)
270 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
272 (defun gnus-close-group (group)
273 "Request the GROUP be closed."
274 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
275 (funcall (gnus-get-function gnus-command-method 'close-group)
276 (gnus-group-real-name group) (nth 1 gnus-command-method))))
278 (defun gnus-retrieve-headers (articles group &optional fetch-old)
279 "Request headers for ARTICLES in GROUP.
280 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
281 (let ((gnus-command-method (gnus-find-method-for-group group)))
282 (if (and gnus-use-cache (numberp (car articles)))
283 (gnus-cache-retrieve-headers articles group fetch-old)
284 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
285 articles (gnus-group-real-name group)
286 (nth 1 gnus-command-method) fetch-old))))
288 (defun gnus-retrieve-articles (articles group)
289 "Request ARTICLES in GROUP."
290 (let ((gnus-command-method (gnus-find-method-for-group group)))
291 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
292 articles (gnus-group-real-name group)
293 (nth 1 gnus-command-method))))
295 (defun gnus-retrieve-groups (groups gnus-command-method)
296 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
297 (when (stringp gnus-command-method)
298 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
299 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
300 groups (nth 1 gnus-command-method)))
302 (defun gnus-request-type (group &optional article)
303 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
304 (let ((gnus-command-method (gnus-find-method-for-group group)))
305 (if (not (gnus-check-backend-function
306 'request-type (car gnus-command-method)))
308 (funcall (gnus-get-function gnus-command-method 'request-type)
309 (gnus-group-real-name group) article))))
311 (defun gnus-request-set-mark (group action)
312 "Set marks on articles in the backend."
313 (let ((gnus-command-method (gnus-find-method-for-group group)))
314 (if (not (gnus-check-backend-function
315 'request-set-mark (car gnus-command-method)))
317 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
318 (gnus-group-real-name group) action
319 (nth 1 gnus-command-method)))))
321 (defun gnus-request-update-mark (group article mark)
322 "Allow the backend to change the mark the user tries to put on an article."
323 (let ((gnus-command-method (gnus-find-method-for-group group)))
324 (if (not (gnus-check-backend-function
325 'request-update-mark (car gnus-command-method)))
327 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
328 (gnus-group-real-name group) article mark))))
330 (defun gnus-request-article (article group &optional buffer)
331 "Request the ARTICLE in GROUP.
332 ARTICLE can either be an article number or an article Message-ID.
333 If BUFFER, insert the article in that group."
334 (let ((gnus-command-method (gnus-find-method-for-group group)))
335 (funcall (gnus-get-function gnus-command-method 'request-article)
336 article (gnus-group-real-name group)
337 (nth 1 gnus-command-method) buffer)))
339 (defun gnus-request-head (article group)
340 "Request the head of ARTICLE in GROUP."
341 (let* ((gnus-command-method (gnus-find-method-for-group group))
342 (head (gnus-get-function gnus-command-method 'request-head t))
348 (gnus-cache-request-article article group))
349 (setq res (cons group article)
351 ;; Use `head' function.
353 (setq res (funcall head article (gnus-group-real-name group)
354 (nth 1 gnus-command-method))))
355 ;; Use `article' function.
357 (setq res (gnus-request-article article group)
361 (set-buffer nntp-server-buffer)
362 (goto-char (point-min))
363 (when (search-forward "\n\n" nil t)
364 (delete-region (1- (point)) (point-max)))
365 (nnheader-fold-continuation-lines)))
368 (defun gnus-request-body (article group)
369 "Request the body of ARTICLE in GROUP."
370 (let* ((gnus-command-method (gnus-find-method-for-group group))
371 (head (gnus-get-function gnus-command-method 'request-body t))
377 (gnus-cache-request-article article group))
378 (setq res (cons group article)
380 ;; Use `head' function.
382 (setq res (funcall head article (gnus-group-real-name group)
383 (nth 1 gnus-command-method))))
384 ;; Use `article' function.
386 (setq res (gnus-request-article article group)
390 (set-buffer nntp-server-buffer)
391 (goto-char (point-min))
392 (when (search-forward "\n\n" nil t)
393 (delete-region (point-min) (1- (point))))))
396 (defun gnus-request-post (gnus-command-method)
397 "Post the current buffer using GNUS-COMMAND-METHOD."
398 (when (stringp gnus-command-method)
399 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
400 (funcall (gnus-get-function gnus-command-method 'request-post)
401 (nth 1 gnus-command-method)))
403 (defun gnus-request-scan (group gnus-command-method)
404 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
405 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
407 (let ((gnus-command-method
408 (if group (gnus-find-method-for-group group) gnus-command-method))
409 (gnus-inhibit-demon t))
410 (funcall (gnus-get-function gnus-command-method 'request-scan)
411 (and group (gnus-group-real-name group))
412 (nth 1 gnus-command-method)))))
414 (defsubst gnus-request-update-info (info gnus-command-method)
415 "Request that GNUS-COMMAND-METHOD update INFO."
416 (when (stringp gnus-command-method)
417 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
418 (when (gnus-check-backend-function
419 'request-update-info (car gnus-command-method))
420 (funcall (gnus-get-function gnus-command-method 'request-update-info)
421 (gnus-group-real-name (gnus-info-group info))
422 info (nth 1 gnus-command-method))))
424 (defun gnus-request-expire-articles (articles group &optional force)
425 (let ((gnus-command-method (gnus-find-method-for-group group)))
426 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
427 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
430 (defun gnus-request-move-article
431 (article group server accept-function &optional last)
432 (let ((gnus-command-method (gnus-find-method-for-group group)))
433 (funcall (gnus-get-function gnus-command-method 'request-move-article)
434 article (gnus-group-real-name group)
435 (nth 1 gnus-command-method) accept-function last)))
437 (defun gnus-request-accept-article (group &optional gnus-command-method last
439 ;; Make sure there's a newline at the end of the article.
440 (when (stringp gnus-command-method)
441 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
442 (when (and (not gnus-command-method)
444 (setq gnus-command-method (gnus-group-name-to-method group)))
445 (goto-char (point-max))
448 (let ((func (car (or gnus-command-method
449 (gnus-find-method-for-group group)))))
450 (funcall (intern (format "%s-request-accept-article" func))
451 (if (stringp group) (gnus-group-real-name group) group)
452 (cadr gnus-command-method)
455 (defun gnus-request-replace-article (article group buffer &optional no-encode)
458 (message-narrow-to-head)
459 (mail-encode-encoded-word-buffer))
460 (message-encode-message-body))
461 (let ((func (car (gnus-group-name-to-method group))))
462 (funcall (intern (format "%s-request-replace-article" func))
463 article (gnus-group-real-name group) buffer)))
465 (defun gnus-request-associate-buffer (group)
466 (let ((gnus-command-method (gnus-find-method-for-group group)))
467 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
468 (gnus-group-real-name group))))
470 (defun gnus-request-restore-buffer (article group)
471 "Request a new buffer restored to the state of ARTICLE."
472 (let ((gnus-command-method (gnus-find-method-for-group group)))
473 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
474 article (gnus-group-real-name group)
475 (nth 1 gnus-command-method))))
477 (defun gnus-request-create-group (group &optional gnus-command-method args)
478 (when (stringp gnus-command-method)
479 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
480 (let ((gnus-command-method
481 (or gnus-command-method (gnus-find-method-for-group group))))
482 (funcall (gnus-get-function gnus-command-method 'request-create-group)
483 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
485 (defun gnus-request-delete-group (group &optional force)
486 (let ((gnus-command-method (gnus-find-method-for-group group)))
487 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
488 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
490 (defun gnus-request-rename-group (group new-name)
491 (let ((gnus-command-method (gnus-find-method-for-group group)))
492 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
493 (gnus-group-real-name group)
494 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
496 (defun gnus-close-backends ()
497 ;; Send a close request to all backends that support such a request.
498 (let ((methods gnus-valid-select-methods)
499 (gnus-inhibit-demon t)
500 func gnus-command-method)
501 (while (setq gnus-command-method (pop methods))
502 (when (fboundp (setq func (intern
503 (concat (car gnus-command-method)
507 (defun gnus-asynchronous-p (gnus-command-method)
508 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
512 (defun gnus-remove-denial (gnus-command-method)
513 (when (stringp gnus-command-method)
514 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
515 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
516 (status (cadr elem)))
517 ;; If this hasn't been opened before, we add it to the list.
518 (when (eq status 'denied)
519 ;; Set the status of this server.
520 (setcar (cdr elem) 'closed))))
524 ;;; gnus-int.el ends here