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 (unless gnus-nntp-service
47 (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 (when (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))
79 (gnus-message 5 "Looking up local news spool..."))
82 (gnus-message 5 "Looking up mh spool..."))
85 (setq gnus-current-select-method gnus-select-method)
86 (run-hooks 'gnus-open-server-hook)
88 ;; gnus-open-server-hook might have opened it
89 (gnus-server-opened gnus-select-method)
90 (gnus-open-server gnus-select-method)
93 "%s (%s) open error: '%s'. Continue? "
94 (car gnus-select-method) (cadr gnus-select-method)
95 (gnus-status-message gnus-select-method)))
96 (gnus-error 1 "Couldn't open server on %s"
97 (nth 1 gnus-select-method))))))
99 (defun gnus-check-group (group)
100 "Try to make sure that the server where GROUP exists is alive."
101 (let ((method (gnus-find-method-for-group group)))
102 (or (gnus-server-opened method)
103 (gnus-open-server method))))
105 (defun gnus-check-server (&optional method silent)
106 "Check whether the connection to METHOD is down.
107 If METHOD is nil, use `gnus-select-method'.
108 If it is down, start it up (again)."
109 (let ((method (or method gnus-select-method)))
110 ;; Transform virtual server names into select methods.
111 (when (stringp method)
112 (setq method (gnus-server-to-method method)))
113 (if (gnus-server-opened method)
114 ;; The stream is already opened.
118 (gnus-message 5 "Opening %s server%s..." (car method)
119 (if (equal (nth 1 method) "") ""
120 (format " on %s" (nth 1 method)))))
121 (run-hooks 'gnus-open-server-hook)
123 (gnus-open-server method)
127 (defun gnus-get-function (method function &optional noerror)
128 "Return a function symbol based on METHOD and FUNCTION."
129 ;; Translate server names into methods.
131 (error "Attempted use of a nil select method"))
132 (when (stringp method)
133 (setq method (gnus-server-to-method method)))
134 (let ((func (intern (format "%s-%s" (car method) function))))
135 ;; If the functions isn't bound, we require the backend in
137 (unless (fboundp func)
138 (require (car method))
139 (when (and (not (fboundp func))
141 ;; This backend doesn't implement this function.
142 (error "No such function: %s" func)))
147 ;;; Interface functions to the backends.
150 (defun gnus-open-server (method)
151 "Open a connection to METHOD."
152 (when (stringp method)
153 (setq method (gnus-server-to-method method)))
154 (let ((elem (assoc method gnus-opened-servers)))
155 ;; If this method was previously denied, we just return nil.
156 (if (eq (nth 1 elem) 'denied)
158 (gnus-message 1 "Denied server")
162 (funcall (gnus-get-function method 'open-server)
163 (nth 1 method) (nthcdr 2 method))))
164 ;; If this hasn't been opened before, we add it to the list.
166 (setq elem (list method nil)
167 gnus-opened-servers (cons elem gnus-opened-servers)))
168 ;; Set the status of this server.
169 (setcar (cdr elem) (if result 'ok 'denied))
170 ;; Return the result from the "open" call.
173 (defun gnus-close-server (method)
174 "Close the connection to METHOD."
175 (when (stringp method)
176 (setq method (gnus-server-to-method method)))
177 (funcall (gnus-get-function method 'close-server) (nth 1 method)))
179 (defun gnus-request-list (method)
180 "Request the active file from METHOD."
181 (when (stringp method)
182 (setq method (gnus-server-to-method method)))
183 (funcall (gnus-get-function method 'request-list) (nth 1 method)))
185 (defun gnus-request-list-newsgroups (method)
186 "Request the newsgroups file from METHOD."
187 (when (stringp method)
188 (setq method (gnus-server-to-method method)))
189 (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
191 (defun gnus-request-newgroups (date method)
192 "Request all new groups since DATE from METHOD."
193 (when (stringp method)
194 (setq method (gnus-server-to-method method)))
195 (let ((func (gnus-get-function method 'request-newgroups t)))
197 (funcall func date (nth 1 method)))))
199 (defun gnus-server-opened (method)
200 "Check whether a connection to METHOD has been opened."
201 (when (stringp method)
202 (setq method (gnus-server-to-method method)))
203 (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
205 (defun gnus-status-message (method)
206 "Return the status message from METHOD.
207 If METHOD is a string, it is interpreted as a group name. The method
208 this group uses will be queried."
209 (let ((method (if (stringp method) (gnus-find-method-for-group method)
211 (funcall (gnus-get-function method 'status-message) (nth 1 method))))
213 (defun gnus-request-group (group &optional dont-check method)
214 "Request GROUP. If DONT-CHECK, no information is required."
215 (let ((method (or method (gnus-find-method-for-group group))))
216 (when (stringp method)
217 (setq method (gnus-server-to-method method)))
218 (funcall (gnus-get-function method 'request-group)
219 (gnus-group-real-name group) (nth 1 method) dont-check)))
221 (defun gnus-list-active-group (group)
222 "Request active information on GROUP."
223 (let ((method (gnus-find-method-for-group group))
224 (func 'list-active-group))
225 (when (gnus-check-backend-function func group)
226 (funcall (gnus-get-function method func)
227 (gnus-group-real-name group) (nth 1 method)))))
229 (defun gnus-request-group-description (group)
230 "Request a description of GROUP."
231 (let ((method (gnus-find-method-for-group group))
232 (func 'request-group-description))
233 (when (gnus-check-backend-function func group)
234 (funcall (gnus-get-function method func)
235 (gnus-group-real-name group) (nth 1 method)))))
237 (defun gnus-close-group (group)
238 "Request the GROUP be closed."
239 (let ((method (gnus-find-method-for-group group)))
240 (funcall (gnus-get-function method 'close-group)
241 (gnus-group-real-name group) (nth 1 method))))
243 (defun gnus-retrieve-headers (articles group &optional fetch-old)
244 "Request headers for ARTICLES in GROUP.
245 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
246 (let ((method (gnus-find-method-for-group group)))
247 (if (and gnus-use-cache (numberp (car articles)))
248 (gnus-cache-retrieve-headers articles group fetch-old)
249 (funcall (gnus-get-function method 'retrieve-headers)
250 articles (gnus-group-real-name group) (nth 1 method)
253 (defun gnus-retrieve-groups (groups method)
254 "Request active information on GROUPS from METHOD."
255 (when (stringp method)
256 (setq method (gnus-server-to-method method)))
257 (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
259 (defun gnus-request-type (group &optional article)
260 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
261 (let ((method (gnus-find-method-for-group group)))
262 (if (not (gnus-check-backend-function 'request-type (car method)))
264 (funcall (gnus-get-function method 'request-type)
265 (gnus-group-real-name group) article))))
267 (defun gnus-request-update-mark (group article mark)
268 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
269 (let ((method (gnus-find-method-for-group group)))
270 (if (not (gnus-check-backend-function 'request-update-mark (car method)))
272 (funcall (gnus-get-function method 'request-update-mark)
273 (gnus-group-real-name group) article mark))))
275 (defun gnus-request-article (article group &optional buffer)
276 "Request the ARTICLE in GROUP.
277 ARTICLE can either be an article number or an article Message-ID.
278 If BUFFER, insert the article in that group."
279 (let ((method (gnus-find-method-for-group group)))
280 (funcall (gnus-get-function method 'request-article)
281 article (gnus-group-real-name group) (nth 1 method) buffer)))
283 (defun gnus-request-head (article group)
284 "Request the head of ARTICLE in GROUP."
285 (let* ((method (gnus-find-method-for-group group))
286 (head (gnus-get-function method 'request-head t)))
288 (funcall head article (gnus-group-real-name group) (nth 1 method))
289 (let ((res (gnus-request-article article group)))
292 (set-buffer nntp-server-buffer)
293 (goto-char (point-min))
294 (when (search-forward "\n\n" nil t)
295 (delete-region (1- (point)) (point-max)))
296 (nnheader-fold-continuation-lines)))
299 (defun gnus-request-body (article group)
300 "Request the body of ARTICLE in GROUP."
301 (let ((method (gnus-find-method-for-group group)))
302 (funcall (gnus-get-function method 'request-body)
303 article (gnus-group-real-name group) (nth 1 method))))
305 (defun gnus-request-post (method)
306 "Post the current buffer using METHOD."
307 (when (stringp method)
308 (setq method (gnus-server-to-method method)))
309 (funcall (gnus-get-function method 'request-post) (nth 1 method)))
311 (defun gnus-request-scan (group method)
312 "Request a SCAN being performed in GROUP from METHOD.
313 If GROUP is nil, all groups on METHOD are scanned."
314 (let ((method (if group (gnus-find-method-for-group group) method)))
315 (funcall (gnus-get-function method 'request-scan)
316 (and group (gnus-group-real-name group)) (nth 1 method))))
318 (defsubst gnus-request-update-info (info method)
319 "Request that METHOD update INFO."
320 (when (stringp method)
321 (setq method (gnus-server-to-method method)))
322 (when (gnus-check-backend-function 'request-update-info (car method))
323 (funcall (gnus-get-function method 'request-update-info)
324 (gnus-group-real-name (gnus-info-group info))
325 info (nth 1 method))))
327 (defun gnus-request-expire-articles (articles group &optional force)
328 (let ((method (gnus-find-method-for-group group)))
329 (funcall (gnus-get-function method 'request-expire-articles)
330 articles (gnus-group-real-name group) (nth 1 method)
333 (defun gnus-request-move-article
334 (article group server accept-function &optional last)
335 (let ((method (gnus-find-method-for-group group)))
336 (funcall (gnus-get-function method 'request-move-article)
337 article (gnus-group-real-name group)
338 (nth 1 method) accept-function last)))
340 (defun gnus-request-accept-article (group method &optional last)
341 ;; Make sure there's a newline at the end of the article.
342 (when (stringp method)
343 (setq method (gnus-server-to-method method)))
344 (when (and (not method)
346 (setq method (gnus-group-name-to-method group)))
347 (goto-char (point-max))
350 (let ((func (car (or method (gnus-find-method-for-group group)))))
351 (funcall (intern (format "%s-request-accept-article" func))
352 (if (stringp group) (gnus-group-real-name group) group)
356 (defun gnus-request-replace-article (article group buffer)
357 (let ((func (car (gnus-find-method-for-group group))))
358 (funcall (intern (format "%s-request-replace-article" func))
359 article (gnus-group-real-name group) buffer)))
361 (defun gnus-request-associate-buffer (group)
362 (let ((method (gnus-find-method-for-group group)))
363 (funcall (gnus-get-function method 'request-associate-buffer)
364 (gnus-group-real-name group))))
366 (defun gnus-request-restore-buffer (article group)
367 "Request a new buffer restored to the state of ARTICLE."
368 (let ((method (gnus-find-method-for-group group)))
369 (funcall (gnus-get-function method 'request-restore-buffer)
370 article (gnus-group-real-name group) (nth 1 method))))
372 (defun gnus-request-create-group (group &optional method)
373 (when (stringp method)
374 (setq method (gnus-server-to-method method)))
375 (let ((method (or method (gnus-find-method-for-group group))))
376 (funcall (gnus-get-function method 'request-create-group)
377 (gnus-group-real-name group) (nth 1 method))))
379 (defun gnus-request-delete-group (group &optional force)
380 (let ((method (gnus-find-method-for-group group)))
381 (funcall (gnus-get-function method 'request-delete-group)
382 (gnus-group-real-name group) force (nth 1 method))))
384 (defun gnus-request-rename-group (group new-name)
385 (let ((method (gnus-find-method-for-group group)))
386 (funcall (gnus-get-function method 'request-rename-group)
387 (gnus-group-real-name group)
388 (gnus-group-real-name new-name) (nth 1 method))))
390 (defun gnus-close-backends ()
391 ;; Send a close request to all backends that support such a request.
392 (let ((methods gnus-valid-select-methods)
394 (while (setq method (pop methods))
395 (when (fboundp (setq func (intern
396 (concat (car method) "-request-close"))))
399 (defun gnus-asynchronous-p (method)
400 (let ((func (gnus-get-function method 'asynchronous-p t)))
404 (defun gnus-remove-denial (method)
405 (when (stringp method)
406 (setq method (gnus-server-to-method method)))
407 (let* ((elem (assoc method gnus-opened-servers))
408 (status (cadr elem)))
409 ;; If this hasn't been opened before, we add it to the list.
410 (when (eq status 'denied)
411 ;; Set the status of this server.
412 (setcar (cdr elem) 'closed))))
416 ;;; gnus-int.el ends here