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-update-mark (group article mark)
312 "Allow the backend to change the mark the user tries to put on an article."
313 (let ((gnus-command-method (gnus-find-method-for-group group)))
314 (if (not (gnus-check-backend-function
315 'request-update-mark (car gnus-command-method)))
317 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
318 (gnus-group-real-name group) article mark))))
320 (defun gnus-request-article (article group &optional buffer)
321 "Request the ARTICLE in GROUP.
322 ARTICLE can either be an article number or an article Message-ID.
323 If BUFFER, insert the article in that group."
324 (let ((gnus-command-method (gnus-find-method-for-group group)))
325 (funcall (gnus-get-function gnus-command-method 'request-article)
326 article (gnus-group-real-name group)
327 (nth 1 gnus-command-method) buffer)))
329 (defun gnus-request-head (article group)
330 "Request the head of ARTICLE in GROUP."
331 (let* ((gnus-command-method (gnus-find-method-for-group group))
332 (head (gnus-get-function gnus-command-method 'request-head t))
338 (gnus-cache-request-article article group))
339 (setq res (cons group article)
341 ;; Use `head' function.
343 (setq res (funcall head article (gnus-group-real-name group)
344 (nth 1 gnus-command-method))))
345 ;; Use `article' function.
347 (setq res (gnus-request-article article group)
351 (set-buffer nntp-server-buffer)
352 (goto-char (point-min))
353 (when (search-forward "\n\n" nil t)
354 (delete-region (1- (point)) (point-max)))
355 (nnheader-fold-continuation-lines)))
358 (defun gnus-request-body (article group)
359 "Request the body of ARTICLE in GROUP."
360 (let* ((gnus-command-method (gnus-find-method-for-group group))
361 (head (gnus-get-function gnus-command-method 'request-body t))
367 (gnus-cache-request-article article group))
368 (setq res (cons group article)
370 ;; Use `head' function.
372 (setq res (funcall head article (gnus-group-real-name group)
373 (nth 1 gnus-command-method))))
374 ;; Use `article' function.
376 (setq res (gnus-request-article article group)
380 (set-buffer nntp-server-buffer)
381 (goto-char (point-min))
382 (when (search-forward "\n\n" nil t)
383 (delete-region (point-min) (1- (point))))))
386 (defun gnus-request-post (gnus-command-method)
387 "Post the current buffer using GNUS-COMMAND-METHOD."
388 (when (stringp gnus-command-method)
389 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
390 (funcall (gnus-get-function gnus-command-method 'request-post)
391 (nth 1 gnus-command-method)))
393 (defun gnus-request-scan (group gnus-command-method)
394 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
395 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
397 (let ((gnus-command-method
398 (if group (gnus-find-method-for-group group) gnus-command-method))
399 (gnus-inhibit-demon t))
400 (funcall (gnus-get-function gnus-command-method 'request-scan)
401 (and group (gnus-group-real-name group))
402 (nth 1 gnus-command-method)))))
404 (defsubst gnus-request-update-info (info gnus-command-method)
405 "Request that GNUS-COMMAND-METHOD update INFO."
406 (when (stringp gnus-command-method)
407 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
408 (when (gnus-check-backend-function
409 'request-update-info (car gnus-command-method))
410 (funcall (gnus-get-function gnus-command-method 'request-update-info)
411 (gnus-group-real-name (gnus-info-group info))
412 info (nth 1 gnus-command-method))))
414 (defun gnus-request-expire-articles (articles group &optional force)
415 (let ((gnus-command-method (gnus-find-method-for-group group)))
416 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
417 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
420 (defun gnus-request-move-article
421 (article group server accept-function &optional last)
422 (let ((gnus-command-method (gnus-find-method-for-group group)))
423 (funcall (gnus-get-function gnus-command-method 'request-move-article)
424 article (gnus-group-real-name group)
425 (nth 1 gnus-command-method) accept-function last)))
427 (defun gnus-request-accept-article (group &optional gnus-command-method last
429 ;; Make sure there's a newline at the end of the article.
430 (when (stringp gnus-command-method)
431 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
432 (when (and (not gnus-command-method)
434 (setq gnus-command-method (gnus-group-name-to-method group)))
435 (goto-char (point-max))
440 (message-narrow-to-head)
441 (mail-encode-encoded-word-buffer))
442 (message-encode-message-body))
443 (let ((func (car (or gnus-command-method
444 (gnus-find-method-for-group group)))))
445 (funcall (intern (format "%s-request-accept-article" func))
446 (if (stringp group) (gnus-group-real-name group) group)
447 (cadr gnus-command-method)
450 (defun gnus-request-replace-article (article group buffer)
452 (message-narrow-to-head)
453 (mail-encode-encoded-word-buffer))
454 (message-encode-message-body)
455 (let ((func (car (gnus-group-name-to-method group))))
456 (funcall (intern (format "%s-request-replace-article" func))
457 article (gnus-group-real-name group) buffer)))
459 (defun gnus-request-associate-buffer (group)
460 (let ((gnus-command-method (gnus-find-method-for-group group)))
461 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
462 (gnus-group-real-name group))))
464 (defun gnus-request-restore-buffer (article group)
465 "Request a new buffer restored to the state of ARTICLE."
466 (let ((gnus-command-method (gnus-find-method-for-group group)))
467 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
468 article (gnus-group-real-name group)
469 (nth 1 gnus-command-method))))
471 (defun gnus-request-create-group (group &optional gnus-command-method args)
472 (when (stringp gnus-command-method)
473 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
474 (let ((gnus-command-method
475 (or gnus-command-method (gnus-find-method-for-group group))))
476 (funcall (gnus-get-function gnus-command-method 'request-create-group)
477 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
479 (defun gnus-request-delete-group (group &optional force)
480 (let ((gnus-command-method (gnus-find-method-for-group group)))
481 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
482 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
484 (defun gnus-request-rename-group (group new-name)
485 (let ((gnus-command-method (gnus-find-method-for-group group)))
486 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
487 (gnus-group-real-name group)
488 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
490 (defun gnus-close-backends ()
491 ;; Send a close request to all backends that support such a request.
492 (let ((methods gnus-valid-select-methods)
493 (gnus-inhibit-demon t)
494 func gnus-command-method)
495 (while (setq gnus-command-method (pop methods))
496 (when (fboundp (setq func (intern
497 (concat (car gnus-command-method)
501 (defun gnus-asynchronous-p (gnus-command-method)
502 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
506 (defun gnus-remove-denial (gnus-command-method)
507 (when (stringp gnus-command-method)
508 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
509 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
510 (status (cadr elem)))
511 ;; If this hasn't been opened before, we add it to the list.
512 (when (eq status 'denied)
513 ;; Set the status of this server.
514 (setcar (cdr elem) 'closed))))
518 ;;; gnus-int.el ends here