1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996,97,98,99 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-request-group-articles (group)
273 "Request a list of existing articles in GROUP."
274 (let ((gnus-command-method (gnus-find-method-for-group group))
275 (func 'request-group-articles))
276 (when (gnus-check-backend-function func group)
277 (funcall (gnus-get-function gnus-command-method func)
278 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
280 (defun gnus-close-group (group)
281 "Request the GROUP be closed."
282 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
283 (funcall (gnus-get-function gnus-command-method 'close-group)
284 (gnus-group-real-name group) (nth 1 gnus-command-method))))
286 (defun gnus-retrieve-headers (articles group &optional fetch-old)
287 "Request headers for ARTICLES in GROUP.
288 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
289 (let ((gnus-command-method (gnus-find-method-for-group group)))
290 (if (and gnus-use-cache (numberp (car articles)))
291 (gnus-cache-retrieve-headers articles group fetch-old)
292 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
293 articles (gnus-group-real-name group)
294 (nth 1 gnus-command-method) fetch-old))))
296 (defun gnus-retrieve-articles (articles group)
297 "Request ARTICLES in GROUP."
298 (let ((gnus-command-method (gnus-find-method-for-group group)))
299 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
300 articles (gnus-group-real-name group)
301 (nth 1 gnus-command-method))))
303 (defun gnus-retrieve-groups (groups gnus-command-method)
304 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
305 (when (stringp gnus-command-method)
306 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
307 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
308 groups (nth 1 gnus-command-method)))
310 (defun gnus-request-type (group &optional article)
311 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
312 (let ((gnus-command-method (gnus-find-method-for-group group)))
313 (if (not (gnus-check-backend-function
314 'request-type (car gnus-command-method)))
316 (funcall (gnus-get-function gnus-command-method 'request-type)
317 (gnus-group-real-name group) article))))
319 (defun gnus-request-set-mark (group action)
320 "Set marks on articles in the backend."
321 (let ((gnus-command-method (gnus-find-method-for-group group)))
322 (if (not (gnus-check-backend-function
323 'request-set-mark (car gnus-command-method)))
325 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
326 (gnus-group-real-name group) action
327 (nth 1 gnus-command-method)))))
329 (defun gnus-request-update-mark (group article mark)
330 "Allow the backend to change the mark the user tries to put on an article."
331 (let ((gnus-command-method (gnus-find-method-for-group group)))
332 (if (not (gnus-check-backend-function
333 'request-update-mark (car gnus-command-method)))
335 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
336 (gnus-group-real-name group) article mark))))
338 (defun gnus-request-article (article group &optional buffer)
339 "Request the ARTICLE in GROUP.
340 ARTICLE can either be an article number or an article Message-ID.
341 If BUFFER, insert the article in that group."
342 (let ((gnus-command-method (gnus-find-method-for-group group)))
343 (funcall (gnus-get-function gnus-command-method 'request-article)
344 article (gnus-group-real-name group)
345 (nth 1 gnus-command-method) buffer)))
347 (defun gnus-request-head (article group)
348 "Request the head of ARTICLE in GROUP."
349 (let* ((gnus-command-method (gnus-find-method-for-group group))
350 (head (gnus-get-function gnus-command-method 'request-head t))
356 (gnus-cache-request-article article group))
357 (setq res (cons group article)
359 ;; Use `head' function.
361 (setq res (funcall head article (gnus-group-real-name group)
362 (nth 1 gnus-command-method))))
363 ;; Use `article' function.
365 (setq res (gnus-request-article article group)
369 (set-buffer nntp-server-buffer)
370 (goto-char (point-min))
371 (when (search-forward "\n\n" nil t)
372 (delete-region (1- (point)) (point-max)))
373 (nnheader-fold-continuation-lines)))
376 (defun gnus-request-body (article group)
377 "Request the body of ARTICLE in GROUP."
378 (let* ((gnus-command-method (gnus-find-method-for-group group))
379 (head (gnus-get-function gnus-command-method 'request-body t))
385 (gnus-cache-request-article article group))
386 (setq res (cons group article)
388 ;; Use `head' function.
390 (setq res (funcall head article (gnus-group-real-name group)
391 (nth 1 gnus-command-method))))
392 ;; Use `article' function.
394 (setq res (gnus-request-article article group)
398 (set-buffer nntp-server-buffer)
399 (goto-char (point-min))
400 (when (search-forward "\n\n" nil t)
401 (delete-region (point-min) (1- (point))))))
404 (defun gnus-request-post (gnus-command-method)
405 "Post the current buffer using GNUS-COMMAND-METHOD."
406 (when (stringp gnus-command-method)
407 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
408 (funcall (gnus-get-function gnus-command-method 'request-post)
409 (nth 1 gnus-command-method)))
411 (defun gnus-request-scan (group gnus-command-method)
412 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
413 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
415 (let ((gnus-command-method
416 (if group (gnus-find-method-for-group group) gnus-command-method))
417 (gnus-inhibit-demon t))
418 (funcall (gnus-get-function gnus-command-method 'request-scan)
419 (and group (gnus-group-real-name group))
420 (nth 1 gnus-command-method)))))
422 (defsubst gnus-request-update-info (info gnus-command-method)
423 "Request that GNUS-COMMAND-METHOD update INFO."
424 (when (stringp gnus-command-method)
425 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
426 (when (gnus-check-backend-function
427 'request-update-info (car gnus-command-method))
428 (funcall (gnus-get-function gnus-command-method 'request-update-info)
429 (gnus-group-real-name (gnus-info-group info))
430 info (nth 1 gnus-command-method))))
432 (defun gnus-request-expire-articles (articles group &optional force)
433 (let ((gnus-command-method (gnus-find-method-for-group group)))
434 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
435 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
438 (defun gnus-request-move-article
439 (article group server accept-function &optional last)
440 (let ((gnus-command-method (gnus-find-method-for-group group)))
441 (funcall (gnus-get-function gnus-command-method 'request-move-article)
442 article (gnus-group-real-name group)
443 (nth 1 gnus-command-method) accept-function last)))
445 (defun gnus-request-accept-article (group &optional gnus-command-method last
447 ;; Make sure there's a newline at the end of the article.
448 (when (stringp gnus-command-method)
449 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
450 (when (and (not gnus-command-method)
452 (setq gnus-command-method (gnus-group-name-to-method group)))
453 (goto-char (point-max))
458 (message-narrow-to-head)
459 (mail-encode-encoded-word-buffer))
460 (message-encode-message-body))
461 (let ((func (car (or gnus-command-method
462 (gnus-find-method-for-group group)))))
463 (funcall (intern (format "%s-request-accept-article" func))
464 (if (stringp group) (gnus-group-real-name group) group)
465 (cadr gnus-command-method)
468 (defun gnus-request-replace-article (article group buffer &optional no-encode)
471 (message-narrow-to-head)
472 (mail-encode-encoded-word-buffer))
473 (message-encode-message-body))
474 (let ((func (car (gnus-group-name-to-method group))))
475 (funcall (intern (format "%s-request-replace-article" func))
476 article (gnus-group-real-name group) buffer)))
478 (defun gnus-request-associate-buffer (group)
479 (let ((gnus-command-method (gnus-find-method-for-group group)))
480 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
481 (gnus-group-real-name group))))
483 (defun gnus-request-restore-buffer (article group)
484 "Request a new buffer restored to the state of ARTICLE."
485 (let ((gnus-command-method (gnus-find-method-for-group group)))
486 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
487 article (gnus-group-real-name group)
488 (nth 1 gnus-command-method))))
490 (defun gnus-request-create-group (group &optional gnus-command-method args)
491 (when (stringp gnus-command-method)
492 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
493 (let ((gnus-command-method
494 (or gnus-command-method (gnus-find-method-for-group group))))
495 (funcall (gnus-get-function gnus-command-method 'request-create-group)
496 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
498 (defun gnus-request-delete-group (group &optional force)
499 (let ((gnus-command-method (gnus-find-method-for-group group)))
500 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
501 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
503 (defun gnus-request-rename-group (group new-name)
504 (let ((gnus-command-method (gnus-find-method-for-group group)))
505 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
506 (gnus-group-real-name group)
507 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
509 (defun gnus-close-backends ()
510 ;; Send a close request to all backends that support such a request.
511 (let ((methods gnus-valid-select-methods)
512 (gnus-inhibit-demon t)
513 func gnus-command-method)
514 (while (setq gnus-command-method (pop methods))
515 (when (fboundp (setq func (intern
516 (concat (car gnus-command-method)
520 (defun gnus-asynchronous-p (gnus-command-method)
521 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
525 (defun gnus-remove-denial (gnus-command-method)
526 (when (stringp gnus-command-method)
527 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
528 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
529 (status (cadr elem)))
530 ;; If this hasn't been opened before, we add it to the list.
531 (when (eq status 'denied)
532 ;; Set the status of this server.
533 (setcar (cdr elem) 'closed))))
537 ;;; gnus-int.el ends here