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)
96 "%s (%s) open error: '%s'. Continue? "
97 (car gnus-select-method) (cadr gnus-select-method)
98 (gnus-status-message gnus-select-method)))
99 (gnus-error 1 "Couldn't open server on %s"
100 (nth 1 gnus-select-method))))))
102 (defun gnus-check-group (group)
103 "Try to make sure that the server where GROUP exists is alive."
104 (let ((method (gnus-find-method-for-group group)))
105 (or (gnus-server-opened method)
106 (gnus-open-server method))))
108 (defun gnus-check-server (&optional method silent)
109 "Check whether the connection to METHOD is down.
110 If METHOD is nil, use `gnus-select-method'.
111 If it is down, start it up (again)."
112 (let ((method (or method gnus-select-method)))
113 ;; Transform virtual server names into select methods.
114 (when (stringp method)
115 (setq method (gnus-server-to-method method)))
116 (if (gnus-server-opened method)
117 ;; The stream is already opened.
121 (gnus-message 5 "Opening %s server%s..." (car method)
122 (if (equal (nth 1 method) "") ""
123 (format " on %s" (nth 1 method)))))
124 (gnus-run-hooks 'gnus-open-server-hook)
126 (gnus-open-server method)
130 (defun gnus-get-function (method function &optional noerror)
131 "Return a function symbol based on METHOD and FUNCTION."
132 ;; Translate server names into methods.
134 (error "Attempted use of a nil select method"))
135 (when (stringp method)
136 (setq method (gnus-server-to-method method)))
137 ;; Check cache of constructed names.
138 (let* ((method-sym (if gnus-agent
139 (gnus-agent-get-function method)
141 (method-fns (get method-sym 'gnus-method-functions))
142 (func (let ((method-fnlist-elt (assq function method-fns)))
143 (unless method-fnlist-elt
144 (setq method-fnlist-elt
146 (intern (format "%s-%s" method-sym function))))
147 (put method-sym 'gnus-method-functions
148 (cons method-fnlist-elt method-fns)))
149 (cdr method-fnlist-elt))))
150 ;; Maybe complain if there is no function.
151 (unless (fboundp func)
153 (error "Trying to require a method that doesn't exist"))
154 (require (car method))
155 (when (not (fboundp func))
158 (error "No such function: %s" func))))
163 ;;; Interface functions to the backends.
166 (defun gnus-open-server (gnus-command-method)
167 "Open a connection to GNUS-COMMAND-METHOD."
168 (when (stringp gnus-command-method)
169 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
170 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
171 ;; If this method was previously denied, we just return nil.
172 (if (eq (nth 1 elem) 'denied)
174 (gnus-message 1 "Denied server")
178 (funcall (gnus-get-function gnus-command-method 'open-server)
179 (nth 1 gnus-command-method)
180 (nthcdr 2 gnus-command-method))))
181 ;; If this hasn't been opened before, we add it to the list.
183 (setq elem (list gnus-command-method nil)
184 gnus-opened-servers (cons elem gnus-opened-servers)))
185 ;; Set the status of this server.
186 (setcar (cdr elem) (if result 'ok 'denied))
187 ;; Return the result from the "open" call.
190 (defun gnus-close-server (gnus-command-method)
191 "Close the connection to GNUS-COMMAND-METHOD."
192 (when (stringp gnus-command-method)
193 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
194 (funcall (gnus-get-function gnus-command-method 'close-server)
195 (nth 1 gnus-command-method)))
197 (defun gnus-request-list (gnus-command-method)
198 "Request the active file from GNUS-COMMAND-METHOD."
199 (when (stringp gnus-command-method)
200 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
201 (funcall (gnus-get-function gnus-command-method 'request-list)
202 (nth 1 gnus-command-method)))
204 (defun gnus-request-list-newsgroups (gnus-command-method)
205 "Request the newsgroups file from GNUS-COMMAND-METHOD."
206 (when (stringp gnus-command-method)
207 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
208 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
209 (nth 1 gnus-command-method)))
211 (defun gnus-request-newgroups (date gnus-command-method)
212 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
213 (when (stringp gnus-command-method)
214 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
215 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
217 (funcall func date (nth 1 gnus-command-method)))))
219 (defun gnus-server-opened (gnus-command-method)
220 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
221 (when (stringp gnus-command-method)
222 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
223 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
224 (nth 1 gnus-command-method)))
226 (defun gnus-status-message (gnus-command-method)
227 "Return the status message from GNUS-COMMAND-METHOD.
228 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
229 this group uses will be queried."
230 (let ((gnus-command-method
231 (if (stringp gnus-command-method)
232 (gnus-find-method-for-group gnus-command-method)
233 gnus-command-method)))
234 (funcall (gnus-get-function gnus-command-method 'status-message)
235 (nth 1 gnus-command-method))))
237 (defun gnus-request-regenerate (gnus-command-method)
238 "Request a data generation from GNUS-COMMAND-METHOD."
239 (when (stringp gnus-command-method)
240 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
241 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
242 (nth 1 gnus-command-method)))
244 (defun gnus-request-group (group &optional dont-check gnus-command-method)
245 "Request GROUP. If DONT-CHECK, no information is required."
246 (let ((gnus-command-method
247 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
248 (when (stringp gnus-command-method)
249 (setq gnus-command-method
250 (inline (gnus-server-to-method gnus-command-method))))
251 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
252 (gnus-group-real-name group) (nth 1 gnus-command-method)
255 (defun gnus-list-active-group (group)
256 "Request active information on GROUP."
257 (let ((gnus-command-method (gnus-find-method-for-group group))
258 (func 'list-active-group))
259 (when (gnus-check-backend-function func group)
260 (funcall (gnus-get-function gnus-command-method func)
261 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
263 (defun gnus-request-group-description (group)
264 "Request a description of GROUP."
265 (let ((gnus-command-method (gnus-find-method-for-group group))
266 (func 'request-group-description))
267 (when (gnus-check-backend-function func group)
268 (funcall (gnus-get-function gnus-command-method func)
269 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
271 (defun gnus-close-group (group)
272 "Request the GROUP be closed."
273 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
274 (funcall (gnus-get-function gnus-command-method 'close-group)
275 (gnus-group-real-name group) (nth 1 gnus-command-method))))
277 (defun gnus-retrieve-headers (articles group &optional fetch-old)
278 "Request headers for ARTICLES in GROUP.
279 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
280 (let ((gnus-command-method (gnus-find-method-for-group group)))
281 (if (and gnus-use-cache (numberp (car articles)))
282 (gnus-cache-retrieve-headers articles group fetch-old)
283 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
284 articles (gnus-group-real-name group)
285 (nth 1 gnus-command-method) fetch-old))))
287 (defun gnus-retrieve-articles (articles group)
288 "Request ARTICLES in GROUP."
289 (let ((gnus-command-method (gnus-find-method-for-group group)))
290 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
291 articles (gnus-group-real-name group)
292 (nth 1 gnus-command-method))))
294 (defun gnus-retrieve-groups (groups gnus-command-method)
295 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
296 (when (stringp gnus-command-method)
297 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
298 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
299 groups (nth 1 gnus-command-method)))
301 (defun gnus-request-type (group &optional article)
302 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
303 (let ((gnus-command-method (gnus-find-method-for-group group)))
304 (if (not (gnus-check-backend-function
305 'request-type (car gnus-command-method)))
307 (funcall (gnus-get-function gnus-command-method 'request-type)
308 (gnus-group-real-name group) article))))
310 (defun gnus-request-update-mark (group article mark)
311 "Allow the backend to change the mark the user tries to put on an article."
312 (let ((gnus-command-method (gnus-find-method-for-group group)))
313 (if (not (gnus-check-backend-function
314 'request-update-mark (car gnus-command-method)))
316 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
317 (gnus-group-real-name group) article mark))))
319 (defun gnus-request-article (article group &optional buffer)
320 "Request the ARTICLE in GROUP.
321 ARTICLE can either be an article number or an article Message-ID.
322 If BUFFER, insert the article in that group."
323 (let ((gnus-command-method (gnus-find-method-for-group group)))
324 (funcall (gnus-get-function gnus-command-method 'request-article)
325 article (gnus-group-real-name group)
326 (nth 1 gnus-command-method) buffer)))
328 (defun gnus-request-head (article group)
329 "Request the head of ARTICLE in GROUP."
330 (let* ((gnus-command-method (gnus-find-method-for-group group))
331 (head (gnus-get-function gnus-command-method 'request-head t))
337 (gnus-cache-request-article article group))
338 (setq res (cons group article)
340 ;; Use `head' function.
342 (setq res (funcall head article (gnus-group-real-name group)
343 (nth 1 gnus-command-method))))
344 ;; Use `article' function.
346 (setq res (gnus-request-article article group)
350 (set-buffer nntp-server-buffer)
351 (goto-char (point-min))
352 (when (search-forward "\n\n" nil t)
353 (delete-region (1- (point)) (point-max)))
354 (nnheader-fold-continuation-lines)))
357 (defun gnus-request-body (article group)
358 "Request the body of ARTICLE in GROUP."
359 (let* ((gnus-command-method (gnus-find-method-for-group group))
360 (head (gnus-get-function gnus-command-method 'request-body t))
366 (gnus-cache-request-article article group))
367 (setq res (cons group article)
369 ;; Use `head' function.
371 (setq res (funcall head article (gnus-group-real-name group)
372 (nth 1 gnus-command-method))))
373 ;; Use `article' function.
375 (setq res (gnus-request-article article group)
379 (set-buffer nntp-server-buffer)
380 (goto-char (point-min))
381 (when (search-forward "\n\n" nil t)
382 (delete-region (point-min) (1- (point))))))
385 (defun gnus-request-post (gnus-command-method)
386 "Post the current buffer using GNUS-COMMAND-METHOD."
387 (when (stringp gnus-command-method)
388 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
389 (funcall (gnus-get-function gnus-command-method 'request-post)
390 (nth 1 gnus-command-method)))
392 (defun gnus-request-scan (group gnus-command-method)
393 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
394 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
396 (let ((gnus-command-method
397 (if group (gnus-find-method-for-group group) gnus-command-method))
398 (gnus-inhibit-demon t))
399 (funcall (gnus-get-function gnus-command-method 'request-scan)
400 (and group (gnus-group-real-name group))
401 (nth 1 gnus-command-method)))))
403 (defsubst gnus-request-update-info (info gnus-command-method)
404 "Request that GNUS-COMMAND-METHOD update INFO."
405 (when (stringp gnus-command-method)
406 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
407 (when (gnus-check-backend-function
408 'request-update-info (car gnus-command-method))
409 (funcall (gnus-get-function gnus-command-method 'request-update-info)
410 (gnus-group-real-name (gnus-info-group info))
411 info (nth 1 gnus-command-method))))
413 (defun gnus-request-expire-articles (articles group &optional force)
414 (let ((gnus-command-method (gnus-find-method-for-group group)))
415 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
416 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
419 (defun gnus-request-move-article
420 (article group server accept-function &optional last)
421 (let ((gnus-command-method (gnus-find-method-for-group group)))
422 (funcall (gnus-get-function gnus-command-method 'request-move-article)
423 article (gnus-group-real-name group)
424 (nth 1 gnus-command-method) accept-function last)))
426 (defun gnus-request-accept-article (group &optional gnus-command-method last
428 ;; Make sure there's a newline at the end of the article.
429 (when (stringp gnus-command-method)
430 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
431 (when (and (not gnus-command-method)
433 (setq gnus-command-method (gnus-group-name-to-method group)))
434 (goto-char (point-max))
439 (message-narrow-to-headers)
440 (rfc2047-encode-message-header))
441 (message-encode-message-body))
442 (let ((func (car (or gnus-command-method
443 (gnus-find-method-for-group group)))))
444 (funcall (intern (format "%s-request-accept-article" func))
445 (if (stringp group) (gnus-group-real-name group) group)
446 (cadr gnus-command-method)
449 (defun gnus-request-replace-article (article group buffer)
451 (message-narrow-to-headers)
452 (rfc2047-encode-message-header))
453 (message-encode-message-body)
454 (let ((func (car (gnus-group-name-to-method group))))
455 (funcall (intern (format "%s-request-replace-article" func))
456 article (gnus-group-real-name group) buffer)))
458 (defun gnus-request-associate-buffer (group)
459 (let ((gnus-command-method (gnus-find-method-for-group group)))
460 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
461 (gnus-group-real-name group))))
463 (defun gnus-request-restore-buffer (article group)
464 "Request a new buffer restored to the state of ARTICLE."
465 (let ((gnus-command-method (gnus-find-method-for-group group)))
466 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
467 article (gnus-group-real-name group)
468 (nth 1 gnus-command-method))))
470 (defun gnus-request-create-group (group &optional gnus-command-method args)
471 (when (stringp gnus-command-method)
472 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
473 (let ((gnus-command-method
474 (or gnus-command-method (gnus-find-method-for-group group))))
475 (funcall (gnus-get-function gnus-command-method 'request-create-group)
476 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
478 (defun gnus-request-delete-group (group &optional force)
479 (let ((gnus-command-method (gnus-find-method-for-group group)))
480 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
481 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
483 (defun gnus-request-rename-group (group new-name)
484 (let ((gnus-command-method (gnus-find-method-for-group group)))
485 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
486 (gnus-group-real-name group)
487 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
489 (defun gnus-close-backends ()
490 ;; Send a close request to all backends that support such a request.
491 (let ((methods gnus-valid-select-methods)
492 (gnus-inhibit-demon t)
493 func gnus-command-method)
494 (while (setq gnus-command-method (pop methods))
495 (when (fboundp (setq func (intern
496 (concat (car gnus-command-method)
500 (defun gnus-asynchronous-p (gnus-command-method)
501 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
505 (defun gnus-remove-denial (gnus-command-method)
506 (when (stringp gnus-command-method)
507 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
508 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
509 (status (cadr elem)))
510 ;; If this hasn't been opened before, we add it to the list.
511 (when (eq status 'denied)
512 ;; Set the status of this server.
513 (setcar (cdr elem) 'closed))))
517 ;;; gnus-int.el ends here