1 ;;; gnus-int.el --- backend interface 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.
30 (defcustom gnus-open-server-hook nil
31 "Hook called just before opening connection to the news server."
36 ;;; Server Communication
39 (defun gnus-start-news-server (&optional confirm)
40 "Open a method for getting news.
41 If CONFIRM is non-nil, the user will be asked for an NNTP server."
43 (if gnus-current-select-method
44 ;; Stream is already opened.
47 (unless gnus-nntp-service
48 (setq gnus-nntp-server nil))
50 ;; Read server name with completion.
51 (setq gnus-nntp-server
52 (completing-read "NNTP server: "
53 (mapcar (lambda (server) (list server))
54 (cons (list gnus-nntp-server)
55 gnus-secondary-servers))
56 nil nil gnus-nntp-server)))
58 (when (and gnus-nntp-server
59 (stringp gnus-nntp-server)
60 (not (string= gnus-nntp-server "")))
61 (setq gnus-select-method
62 (cond ((or (string= gnus-nntp-server "")
63 (string= gnus-nntp-server "::"))
64 (list 'nnspool (system-name)))
65 ((string-match "^:" gnus-nntp-server)
66 (list 'nnmh gnus-nntp-server
68 (file-name-as-directory
70 (concat "~/" (substring
71 gnus-nntp-server 1)))))
72 (list 'nnmh-get-new-mail nil)))
74 (list 'nntp gnus-nntp-server)))))
76 (setq how (car gnus-select-method))
80 (gnus-message 5 "Looking up local news spool..."))
83 (gnus-message 5 "Looking up mh spool..."))
86 (setq gnus-current-select-method gnus-select-method)
87 (run-hooks 'gnus-open-server-hook)
89 ;; gnus-open-server-hook might have opened it
90 (gnus-server-opened gnus-select-method)
91 (gnus-open-server gnus-select-method)
94 "%s (%s) open error: '%s'. Continue? "
95 (car gnus-select-method) (cadr gnus-select-method)
96 (gnus-status-message gnus-select-method)))
97 (gnus-error 1 "Couldn't open server on %s"
98 (nth 1 gnus-select-method))))))
100 (defun gnus-check-group (group)
101 "Try to make sure that the server where GROUP exists is alive."
102 (let ((method (gnus-find-method-for-group group)))
103 (or (gnus-server-opened method)
104 (gnus-open-server method))))
106 (defun gnus-check-server (&optional method silent)
107 "Check whether the connection to METHOD is down.
108 If METHOD is nil, use `gnus-select-method'.
109 If it is down, start it up (again)."
110 (let ((method (or method gnus-select-method)))
111 ;; Transform virtual server names into select methods.
112 (when (stringp method)
113 (setq method (gnus-server-to-method method)))
114 (if (gnus-server-opened method)
115 ;; The stream is already opened.
119 (gnus-message 5 "Opening %s server%s..." (car method)
120 (if (equal (nth 1 method) "") ""
121 (format " on %s" (nth 1 method)))))
122 (run-hooks 'gnus-open-server-hook)
124 (gnus-open-server method)
128 (defun gnus-get-function (method function &optional noerror)
129 "Return a function symbol based on METHOD and FUNCTION."
130 ;; Translate server names into methods.
132 (error "Attempted use of a nil select method"))
133 (when (stringp method)
134 (setq method (gnus-server-to-method method)))
135 (let ((func (intern (format "%s-%s" (car method) function))))
136 ;; If the functions isn't bound, we require the backend in
138 (unless (fboundp func)
139 (require (car method))
140 (when (and (not (fboundp func))
142 ;; This backend doesn't implement this function.
143 (error "No such function: %s" func)))
148 ;;; Interface functions to the backends.
151 (defun gnus-open-server (method)
152 "Open a connection to METHOD."
153 (when (stringp method)
154 (setq method (gnus-server-to-method method)))
155 (let ((elem (assoc method gnus-opened-servers)))
156 ;; If this method was previously denied, we just return nil.
157 (if (eq (nth 1 elem) 'denied)
159 (gnus-message 1 "Denied server")
163 (funcall (gnus-get-function method 'open-server)
164 (nth 1 method) (nthcdr 2 method))))
165 ;; If this hasn't been opened before, we add it to the list.
167 (setq elem (list method nil)
168 gnus-opened-servers (cons elem gnus-opened-servers)))
169 ;; Set the status of this server.
170 (setcar (cdr elem) (if result 'ok 'denied))
171 ;; Return the result from the "open" call.
174 (defun gnus-close-server (method)
175 "Close the connection to METHOD."
176 (when (stringp method)
177 (setq method (gnus-server-to-method method)))
178 (funcall (gnus-get-function method 'close-server) (nth 1 method)))
180 (defun gnus-request-list (method)
181 "Request the active file from METHOD."
182 (when (stringp method)
183 (setq method (gnus-server-to-method method)))
184 (funcall (gnus-get-function method 'request-list) (nth 1 method)))
186 (defun gnus-request-list-newsgroups (method)
187 "Request the newsgroups file from METHOD."
188 (when (stringp method)
189 (setq method (gnus-server-to-method method)))
190 (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
192 (defun gnus-request-newgroups (date method)
193 "Request all new groups since DATE from METHOD."
194 (when (stringp method)
195 (setq method (gnus-server-to-method method)))
196 (let ((func (gnus-get-function method 'request-newgroups t)))
198 (funcall func date (nth 1 method)))))
200 (defun gnus-server-opened (method)
201 "Check whether a connection to METHOD has been opened."
202 (when (stringp method)
203 (setq method (gnus-server-to-method method)))
204 (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
206 (defun gnus-status-message (method)
207 "Return the status message from METHOD.
208 If METHOD is a string, it is interpreted as a group name. The method
209 this group uses will be queried."
210 (let ((method (if (stringp method) (gnus-find-method-for-group method)
212 (funcall (gnus-get-function method 'status-message) (nth 1 method))))
214 (defun gnus-request-group (group &optional dont-check method)
215 "Request GROUP. If DONT-CHECK, no information is required."
216 (let ((method (or method (gnus-find-method-for-group group))))
217 (when (stringp method)
218 (setq method (gnus-server-to-method method)))
219 (funcall (gnus-get-function method 'request-group)
220 (gnus-group-real-name group) (nth 1 method) dont-check)))
222 (defun gnus-list-active-group (group)
223 "Request active information on GROUP."
224 (let ((method (gnus-find-method-for-group group))
225 (func 'list-active-group))
226 (when (gnus-check-backend-function func group)
227 (funcall (gnus-get-function method func)
228 (gnus-group-real-name group) (nth 1 method)))))
230 (defun gnus-request-group-description (group)
231 "Request a description of GROUP."
232 (let ((method (gnus-find-method-for-group group))
233 (func 'request-group-description))
234 (when (gnus-check-backend-function func group)
235 (funcall (gnus-get-function method func)
236 (gnus-group-real-name group) (nth 1 method)))))
238 (defun gnus-close-group (group)
239 "Request the GROUP be closed."
240 (let ((method (gnus-find-method-for-group group)))
241 (funcall (gnus-get-function method 'close-group)
242 (gnus-group-real-name group) (nth 1 method))))
244 (defun gnus-retrieve-headers (articles group &optional fetch-old)
245 "Request headers for ARTICLES in GROUP.
246 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
247 (let ((method (gnus-find-method-for-group group)))
248 (if (and gnus-use-cache (numberp (car articles)))
249 (gnus-cache-retrieve-headers articles group fetch-old)
250 (funcall (gnus-get-function method 'retrieve-headers)
251 articles (gnus-group-real-name group) (nth 1 method)
254 (defun gnus-retrieve-groups (groups method)
255 "Request active information on GROUPS from METHOD."
256 (when (stringp method)
257 (setq method (gnus-server-to-method method)))
258 (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
260 (defun gnus-request-type (group &optional article)
261 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
262 (let ((method (gnus-find-method-for-group group)))
263 (if (not (gnus-check-backend-function 'request-type (car method)))
265 (funcall (gnus-get-function method 'request-type)
266 (gnus-group-real-name group) article))))
268 (defun gnus-request-update-mark (group article mark)
269 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
270 (let ((method (gnus-find-method-for-group group)))
271 (if (not (gnus-check-backend-function 'request-update-mark (car method)))
273 (funcall (gnus-get-function method 'request-update-mark)
274 (gnus-group-real-name group) article mark))))
276 (defun gnus-request-article (article group &optional buffer)
277 "Request the ARTICLE in GROUP.
278 ARTICLE can either be an article number or an article Message-ID.
279 If BUFFER, insert the article in that group."
280 (let ((method (gnus-find-method-for-group group)))
281 (funcall (gnus-get-function method 'request-article)
282 article (gnus-group-real-name group) (nth 1 method) buffer)))
284 (defun gnus-request-head (article group)
285 "Request the head of ARTICLE in GROUP."
286 (let* ((method (gnus-find-method-for-group group))
287 (head (gnus-get-function method 'request-head t))
293 (gnus-cache-request-article article group))
294 (setq res (cons group article)
296 ;; Use `head' function.
298 (setq res (funcall head article (gnus-group-real-name group)
300 ;; Use `article' function.
302 (setq res (gnus-request-article article group)
306 (set-buffer nntp-server-buffer)
307 (goto-char (point-min))
308 (when (search-forward "\n\n" nil t)
309 (delete-region (1- (point)) (point-max)))
310 (nnheader-fold-continuation-lines)))
313 (defun gnus-request-body (article group)
314 "Request the body of ARTICLE in GROUP."
315 (let ((method (gnus-find-method-for-group group)))
316 (funcall (gnus-get-function method 'request-body)
317 article (gnus-group-real-name group) (nth 1 method))))
319 (defun gnus-request-post (method)
320 "Post the current buffer using METHOD."
321 (when (stringp method)
322 (setq method (gnus-server-to-method method)))
323 (funcall (gnus-get-function method 'request-post) (nth 1 method)))
325 (defun gnus-request-scan (group method)
326 "Request a SCAN being performed in GROUP from METHOD.
327 If GROUP is nil, all groups on METHOD are scanned."
328 (let ((method (if group (gnus-find-method-for-group group) method)))
329 (funcall (gnus-get-function method 'request-scan)
330 (and group (gnus-group-real-name group)) (nth 1 method))))
332 (defsubst gnus-request-update-info (info method)
333 "Request that METHOD update INFO."
334 (when (stringp method)
335 (setq method (gnus-server-to-method method)))
336 (when (gnus-check-backend-function 'request-update-info (car method))
337 (funcall (gnus-get-function method 'request-update-info)
338 (gnus-group-real-name (gnus-info-group info))
339 info (nth 1 method))))
341 (defun gnus-request-expire-articles (articles group &optional force)
342 (let ((method (gnus-find-method-for-group group)))
343 (funcall (gnus-get-function method 'request-expire-articles)
344 articles (gnus-group-real-name group) (nth 1 method)
347 (defun gnus-request-move-article
348 (article group server accept-function &optional last)
349 (let ((method (gnus-find-method-for-group group)))
350 (funcall (gnus-get-function method 'request-move-article)
351 article (gnus-group-real-name group)
352 (nth 1 method) accept-function last)))
354 (defun gnus-request-accept-article (group method &optional last)
355 ;; Make sure there's a newline at the end of the article.
356 (when (stringp method)
357 (setq method (gnus-server-to-method method)))
358 (when (and (not method)
360 (setq method (gnus-group-name-to-method group)))
361 (goto-char (point-max))
364 (let ((func (car (or method (gnus-find-method-for-group group)))))
365 (funcall (intern (format "%s-request-accept-article" func))
366 (if (stringp group) (gnus-group-real-name group) group)
370 (defun gnus-request-replace-article (article group buffer)
371 (let ((func (car (gnus-find-method-for-group group))))
372 (funcall (intern (format "%s-request-replace-article" func))
373 article (gnus-group-real-name group) buffer)))
375 (defun gnus-request-associate-buffer (group)
376 (let ((method (gnus-find-method-for-group group)))
377 (funcall (gnus-get-function method 'request-associate-buffer)
378 (gnus-group-real-name group))))
380 (defun gnus-request-restore-buffer (article group)
381 "Request a new buffer restored to the state of ARTICLE."
382 (let ((method (gnus-find-method-for-group group)))
383 (funcall (gnus-get-function method 'request-restore-buffer)
384 article (gnus-group-real-name group) (nth 1 method))))
386 (defun gnus-request-create-group (group &optional method args)
387 (when (stringp method)
388 (setq method (gnus-server-to-method method)))
389 (let ((method (or method (gnus-find-method-for-group group))))
390 (funcall (gnus-get-function method 'request-create-group)
391 (gnus-group-real-name group) (nth 1 method) args)))
393 (defun gnus-request-delete-group (group &optional force)
394 (let ((method (gnus-find-method-for-group group)))
395 (funcall (gnus-get-function method 'request-delete-group)
396 (gnus-group-real-name group) force (nth 1 method))))
398 (defun gnus-request-rename-group (group new-name)
399 (let ((method (gnus-find-method-for-group group)))
400 (funcall (gnus-get-function method 'request-rename-group)
401 (gnus-group-real-name group)
402 (gnus-group-real-name new-name) (nth 1 method))))
404 (defun gnus-close-backends ()
405 ;; Send a close request to all backends that support such a request.
406 (let ((methods gnus-valid-select-methods)
408 (while (setq method (pop methods))
409 (when (fboundp (setq func (intern
410 (concat (car method) "-request-close"))))
413 (defun gnus-asynchronous-p (method)
414 (let ((func (gnus-get-function method 'asynchronous-p t)))
418 (defun gnus-remove-denial (method)
419 (when (stringp method)
420 (setq method (gnus-server-to-method method)))
421 (let* ((elem (assoc method gnus-opened-servers))
422 (status (cadr elem)))
423 ;; If this hasn't been opened before, we add it to the list.
424 (when (eq status 'denied)
425 ;; Set the status of this server.
426 (setcar (cdr elem) 'closed))))
430 ;;; gnus-int.el ends here