1 ;;; gnus-int.el --- backend inteface functions for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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.
31 (defvar gnus-open-server-hook nil
32 "*A hook called just before opening connection to the news server.")
35 ;;; Server Communication
38 (defun gnus-start-news-server (&optional confirm)
39 "Open a method for getting news.
40 If CONFIRM is non-nil, the user will be asked for an NNTP server."
42 (if gnus-current-select-method
43 ;; Stream is already opened.
46 (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
49 ;; Read server name with completion.
50 (setq gnus-nntp-server
51 (completing-read "NNTP server: "
52 (mapcar (lambda (server) (list server))
53 (cons (list gnus-nntp-server)
54 gnus-secondary-servers))
55 nil nil gnus-nntp-server))))
57 (if (and gnus-nntp-server
58 (stringp gnus-nntp-server)
59 (not (string= gnus-nntp-server "")))
60 (setq gnus-select-method
61 (cond ((or (string= gnus-nntp-server "")
62 (string= gnus-nntp-server "::"))
63 (list 'nnspool (system-name)))
64 ((string-match "^:" gnus-nntp-server)
65 (list 'nnmh gnus-nntp-server
67 (file-name-as-directory
69 (concat "~/" (substring
70 gnus-nntp-server 1)))))
71 (list 'nnmh-get-new-mail nil)))
73 (list 'nntp gnus-nntp-server)))))
75 (setq how (car gnus-select-method))
76 (cond ((eq how 'nnspool)
78 (gnus-message 5 "Looking up local news spool..."))
81 (gnus-message 5 "Looking up mh spool..."))
84 (setq gnus-current-select-method gnus-select-method)
85 (run-hooks 'gnus-open-server-hook)
87 ;; gnus-open-server-hook might have opened it
88 (gnus-server-opened gnus-select-method)
89 (gnus-open-server gnus-select-method)
92 "%s (%s) open error: '%s'. Continue? "
93 (car gnus-select-method) (cadr gnus-select-method)
94 (gnus-status-message gnus-select-method)))
95 (gnus-error 1 "Couldn't open server on %s"
96 (nth 1 gnus-select-method))))))
98 (defun gnus-check-group (group)
99 "Try to make sure that the server where GROUP exists is alive."
100 (let ((method (gnus-find-method-for-group group)))
101 (or (gnus-server-opened method)
102 (gnus-open-server method))))
104 (defun gnus-check-server (&optional method silent)
105 "Check whether the connection to METHOD is down.
106 If METHOD is nil, use `gnus-select-method'.
107 If it is down, start it up (again)."
108 (let ((method (or method gnus-select-method)))
109 ;; Transform virtual server names into select methods.
110 (when (stringp method)
111 (setq method (gnus-server-to-method method)))
112 (if (gnus-server-opened method)
113 ;; The stream is already opened.
117 (gnus-message 5 "Opening %s server%s..." (car method)
118 (if (equal (nth 1 method) "") ""
119 (format " on %s" (nth 1 method)))))
120 (run-hooks 'gnus-open-server-hook)
122 (gnus-open-server method)
126 (defun gnus-get-function (method function &optional noerror)
127 "Return a function symbol based on METHOD and FUNCTION."
128 ;; Translate server names into methods.
130 (error "Attempted use of a nil select method"))
131 (when (stringp method)
132 (setq method (gnus-server-to-method method)))
133 (let ((func (intern (format "%s-%s" (car method) function))))
134 ;; If the functions isn't bound, we require the backend in
136 (unless (fboundp func)
137 (require (car method))
138 (when (and (not (fboundp func))
140 ;; This backend doesn't implement this function.
141 (error "No such function: %s" func)))
146 ;;; Interface functions to the backends.
149 (defun gnus-open-server (method)
150 "Open a connection to METHOD."
151 (when (stringp method)
152 (setq method (gnus-server-to-method method)))
153 (let ((elem (assoc method gnus-opened-servers)))
154 ;; If this method was previously denied, we just return nil.
155 (if (eq (nth 1 elem) 'denied)
157 (gnus-message 1 "Denied server")
161 (funcall (gnus-get-function method 'open-server)
162 (nth 1 method) (nthcdr 2 method))))
163 ;; If this hasn't been opened before, we add it to the list.
165 (setq elem (list method nil)
166 gnus-opened-servers (cons elem gnus-opened-servers)))
167 ;; Set the status of this server.
168 (setcar (cdr elem) (if result 'ok 'denied))
169 ;; Return the result from the "open" call.
172 (defun gnus-close-server (method)
173 "Close the connection to METHOD."
174 (when (stringp method)
175 (setq method (gnus-server-to-method method)))
176 (funcall (gnus-get-function method 'close-server) (nth 1 method)))
178 (defun gnus-request-list (method)
179 "Request the active file from METHOD."
180 (when (stringp method)
181 (setq method (gnus-server-to-method method)))
182 (funcall (gnus-get-function method 'request-list) (nth 1 method)))
184 (defun gnus-request-list-newsgroups (method)
185 "Request the newsgroups file from METHOD."
186 (when (stringp method)
187 (setq method (gnus-server-to-method method)))
188 (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
190 (defun gnus-request-newgroups (date method)
191 "Request all new groups since DATE from METHOD."
192 (when (stringp method)
193 (setq method (gnus-server-to-method method)))
194 (funcall (gnus-get-function method 'request-newgroups)
195 date (nth 1 method)))
197 (defun gnus-server-opened (method)
198 "Check whether a connection to METHOD has been opened."
199 (when (stringp method)
200 (setq method (gnus-server-to-method method)))
201 (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
203 (defun gnus-status-message (method)
204 "Return the status message from METHOD.
205 If METHOD is a string, it is interpreted as a group name. The method
206 this group uses will be queried."
207 (let ((method (if (stringp method) (gnus-find-method-for-group method)
209 (funcall (gnus-get-function method 'status-message) (nth 1 method))))
211 (defun gnus-request-group (group &optional dont-check method)
212 "Request GROUP. If DONT-CHECK, no information is required."
213 (let ((method (or method (gnus-find-method-for-group group))))
214 (when (stringp method)
215 (setq method (gnus-server-to-method method)))
216 (funcall (gnus-get-function method 'request-group)
217 (gnus-group-real-name group) (nth 1 method) dont-check)))
219 (defun gnus-list-active-group (group)
220 "Request active information on GROUP."
221 (let ((method (gnus-find-method-for-group group))
222 (func 'list-active-group))
223 (when (gnus-check-backend-function func group)
224 (funcall (gnus-get-function method func)
225 (gnus-group-real-name group) (nth 1 method)))))
227 (defun gnus-request-group-description (group)
228 "Request a description of GROUP."
229 (let ((method (gnus-find-method-for-group group))
230 (func 'request-group-description))
231 (when (gnus-check-backend-function func group)
232 (funcall (gnus-get-function method func)
233 (gnus-group-real-name group) (nth 1 method)))))
235 (defun gnus-close-group (group)
236 "Request the GROUP be closed."
237 (let ((method (gnus-find-method-for-group group)))
238 (funcall (gnus-get-function method 'close-group)
239 (gnus-group-real-name group) (nth 1 method))))
241 (defun gnus-retrieve-headers (articles group &optional fetch-old)
242 "Request headers for ARTICLES in GROUP.
243 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
244 (let ((method (gnus-find-method-for-group group)))
245 (if (and gnus-use-cache (numberp (car articles)))
246 (gnus-cache-retrieve-headers articles group fetch-old)
247 (funcall (gnus-get-function method 'retrieve-headers)
248 articles (gnus-group-real-name group) (nth 1 method)
251 (defun gnus-retrieve-groups (groups method)
252 "Request active information on GROUPS from METHOD."
253 (when (stringp method)
254 (setq method (gnus-server-to-method method)))
255 (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
257 (defun gnus-request-type (group &optional article)
258 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
259 (let ((method (gnus-find-method-for-group group)))
260 (if (not (gnus-check-backend-function 'request-type (car method)))
262 (funcall (gnus-get-function method 'request-type)
263 (gnus-group-real-name group) article))))
265 (defun gnus-request-update-mark (group article mark)
266 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
267 (let ((method (gnus-find-method-for-group group)))
268 (if (not (gnus-check-backend-function 'request-update-mark (car method)))
270 (funcall (gnus-get-function method 'request-update-mark)
271 (gnus-group-real-name group) article mark))))
273 (defun gnus-request-article (article group &optional buffer)
274 "Request the ARTICLE in GROUP.
275 ARTICLE can either be an article number or an article Message-ID.
276 If BUFFER, insert the article in that group."
277 (let ((method (gnus-find-method-for-group group)))
278 (funcall (gnus-get-function method 'request-article)
279 article (gnus-group-real-name group) (nth 1 method) buffer)))
281 (defun gnus-request-head (article group)
282 "Request the head of ARTICLE in GROUP."
283 (let* ((method (gnus-find-method-for-group group))
284 (head (gnus-get-function method 'request-head t)))
286 (funcall head article (gnus-group-real-name group) (nth 1 method))
287 (let ((res (gnus-request-article article group)))
290 (set-buffer nntp-server-buffer)
291 (goto-char (point-min))
292 (when (search-forward "\n\n" nil t)
293 (delete-region (1- (point)) (point-max)))
294 (nnheader-fold-continuation-lines)))
297 (defun gnus-request-body (article group)
298 "Request the body of ARTICLE in GROUP."
299 (let ((method (gnus-find-method-for-group group)))
300 (funcall (gnus-get-function method 'request-body)
301 article (gnus-group-real-name group) (nth 1 method))))
303 (defun gnus-request-post (method)
304 "Post the current buffer using METHOD."
305 (when (stringp method)
306 (setq method (gnus-server-to-method method)))
307 (funcall (gnus-get-function method 'request-post) (nth 1 method)))
309 (defun gnus-request-scan (group method)
310 "Request a SCAN being performed in GROUP from METHOD.
311 If GROUP is nil, all groups on METHOD are scanned."
312 (let ((method (if group (gnus-find-method-for-group group) method)))
313 (funcall (gnus-get-function method 'request-scan)
314 (and group (gnus-group-real-name group)) (nth 1 method))))
316 (defsubst gnus-request-update-info (info method)
317 "Request that METHOD update INFO."
318 (when (stringp method)
319 (setq method (gnus-server-to-method method)))
320 (when (gnus-check-backend-function 'request-update-info (car method))
321 (funcall (gnus-get-function method 'request-update-info)
322 (gnus-group-real-name (gnus-info-group info))
323 info (nth 1 method))))
325 (defun gnus-request-expire-articles (articles group &optional force)
326 (let ((method (gnus-find-method-for-group group)))
327 (funcall (gnus-get-function method 'request-expire-articles)
328 articles (gnus-group-real-name group) (nth 1 method)
331 (defun gnus-request-move-article
332 (article group server accept-function &optional last)
333 (let ((method (gnus-find-method-for-group group)))
334 (funcall (gnus-get-function method 'request-move-article)
335 article (gnus-group-real-name group)
336 (nth 1 method) accept-function last)))
338 (defun gnus-request-accept-article (group method &optional last)
339 ;; Make sure there's a newline at the end of the article.
340 (when (stringp method)
341 (setq method (gnus-server-to-method method)))
342 (when (and (not method)
344 (setq method (gnus-group-name-to-method group)))
345 (goto-char (point-max))
348 (let ((func (car (or method (gnus-find-method-for-group group)))))
349 (funcall (intern (format "%s-request-accept-article" func))
350 (if (stringp group) (gnus-group-real-name group) group)
354 (defun gnus-request-replace-article (article group buffer)
355 (let ((func (car (gnus-find-method-for-group group))))
356 (funcall (intern (format "%s-request-replace-article" func))
357 article (gnus-group-real-name group) buffer)))
359 (defun gnus-request-associate-buffer (group)
360 (let ((method (gnus-find-method-for-group group)))
361 (funcall (gnus-get-function method 'request-associate-buffer)
362 (gnus-group-real-name group))))
364 (defun gnus-request-restore-buffer (article group)
365 "Request a new buffer restored to the state of ARTICLE."
366 (let ((method (gnus-find-method-for-group group)))
367 (funcall (gnus-get-function method 'request-restore-buffer)
368 article (gnus-group-real-name group) (nth 1 method))))
370 (defun gnus-request-create-group (group &optional method)
371 (when (stringp method)
372 (setq method (gnus-server-to-method method)))
373 (let ((method (or method (gnus-find-method-for-group group))))
374 (funcall (gnus-get-function method 'request-create-group)
375 (gnus-group-real-name group) (nth 1 method))))
377 (defun gnus-request-delete-group (group &optional force)
378 (let ((method (gnus-find-method-for-group group)))
379 (funcall (gnus-get-function method 'request-delete-group)
380 (gnus-group-real-name group) force (nth 1 method))))
382 (defun gnus-request-rename-group (group new-name)
383 (let ((method (gnus-find-method-for-group group)))
384 (funcall (gnus-get-function method 'request-rename-group)
385 (gnus-group-real-name group)
386 (gnus-group-real-name new-name) (nth 1 method))))
388 (defun gnus-close-backends ()
389 ;; Send a close request to all backends that support such a request.
390 (let ((methods gnus-valid-select-methods)
393 (if (fboundp (setq func (intern (concat (caar methods)
396 (setq methods (cdr methods)))))
398 (defun gnus-asynchronous-p (method)
399 (let ((func (gnus-get-function method 'asynchronous-p t)))
405 ;;; gnus-int.el ends here