1 ;;; gnus-int.el --- backend interface functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
33 (defcustom gnus-open-server-hook nil
34 "Hook called just before opening connection to the news server."
39 ;;; Server Communication
42 (defun gnus-start-news-server (&optional confirm)
43 "Open a method for getting news.
44 If CONFIRM is non-nil, the user will be asked for an NNTP server."
46 (if gnus-current-select-method
47 ;; Stream is already opened.
50 (unless gnus-nntp-service
51 (setq gnus-nntp-server nil))
53 ;; Read server name with completion.
54 (setq gnus-nntp-server
55 (completing-read "NNTP server: "
56 (mapcar (lambda (server) (list server))
57 (cons (list gnus-nntp-server)
58 gnus-secondary-servers))
59 nil nil gnus-nntp-server)))
61 (when (and gnus-nntp-server
62 (stringp gnus-nntp-server)
63 (not (string= gnus-nntp-server "")))
64 (setq gnus-select-method
65 (cond ((or (string= gnus-nntp-server "")
66 (string= gnus-nntp-server "::"))
67 (list 'nnspool (system-name)))
68 ((string-match "^:" gnus-nntp-server)
69 (list 'nnmh gnus-nntp-server
71 (file-name-as-directory
73 (substring 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))
115 ;; Transform virtual server names into select methods.
116 (when (stringp method)
117 (setq method (gnus-server-to-method method)))
118 (if (gnus-server-opened method)
119 ;; The stream is already opened.
123 (gnus-message 5 "Opening %s server%s..." (car method)
124 (if (equal (nth 1 method) "") ""
125 (format " on %s" (nth 1 method)))))
126 (gnus-run-hooks 'gnus-open-server-hook)
129 (setq result (gnus-open-server method))
130 (quit (message "Quit gnus-check-server")
133 (gnus-message 5 "Opening %s server%s...%s" (car method)
134 (if (equal (nth 1 method) "") ""
135 (format " on %s" (nth 1 method)))
136 (if result "done" "failed")))))))
138 (defun gnus-get-function (method function &optional noerror)
139 "Return a function symbol based on METHOD and FUNCTION."
140 ;; Translate server names into methods.
142 (error "Attempted use of a nil select method"))
143 (when (stringp method)
144 (setq method (gnus-server-to-method method)))
145 ;; Check cache of constructed names.
146 (let* ((method-sym (if gnus-agent
147 (gnus-agent-get-function method)
149 (method-fns (get method-sym 'gnus-method-functions))
150 (func (let ((method-fnlist-elt (assq function method-fns)))
151 (unless method-fnlist-elt
152 (setq method-fnlist-elt
154 (intern (format "%s-%s" method-sym function))))
155 (put method-sym 'gnus-method-functions
156 (cons method-fnlist-elt method-fns)))
157 (cdr method-fnlist-elt))))
158 ;; Maybe complain if there is no function.
159 (unless (fboundp func)
161 (error "Trying to require a method that doesn't exist"))
162 (require (car method))
163 (when (not (fboundp func))
166 (error "No such function: %s" func))))
171 ;;; Interface functions to the backends.
174 (defun gnus-open-server (gnus-command-method)
175 "Open a connection to GNUS-COMMAND-METHOD."
176 (when (stringp gnus-command-method)
177 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
178 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
179 ;; If this method was previously denied, we just return nil.
180 (if (eq (nth 1 elem) 'denied)
182 (gnus-message 1 "Denied server")
187 (funcall (gnus-get-function gnus-command-method 'open-server)
188 (nth 1 gnus-command-method)
189 (nthcdr 2 gnus-command-method))
191 (message "Quit trying to open server")
193 ;; If this hasn't been opened before, we add it to the list.
195 (setq elem (list gnus-command-method nil)
196 gnus-opened-servers (cons elem gnus-opened-servers)))
197 ;; Set the status of this server.
198 (setcar (cdr elem) (if result 'ok 'denied))
199 ;; Return the result from the "open" call.
202 (defun gnus-close-server (gnus-command-method)
203 "Close the connection to GNUS-COMMAND-METHOD."
204 (when (stringp gnus-command-method)
205 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
206 (funcall (gnus-get-function gnus-command-method 'close-server)
207 (nth 1 gnus-command-method)))
209 (defun gnus-request-list (gnus-command-method)
210 "Request the active file from GNUS-COMMAND-METHOD."
211 (when (stringp gnus-command-method)
212 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
213 (funcall (gnus-get-function gnus-command-method 'request-list)
214 (nth 1 gnus-command-method)))
216 (defun gnus-request-list-newsgroups (gnus-command-method)
217 "Request the newsgroups file from GNUS-COMMAND-METHOD."
218 (when (stringp gnus-command-method)
219 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
220 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
221 (nth 1 gnus-command-method)))
223 (defun gnus-request-newgroups (date gnus-command-method)
224 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
225 (when (stringp gnus-command-method)
226 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
227 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
229 (funcall func date (nth 1 gnus-command-method)))))
231 (defun gnus-server-opened (gnus-command-method)
232 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
233 (unless (eq (gnus-server-status gnus-command-method)
235 (when (stringp gnus-command-method)
236 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
237 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
238 (nth 1 gnus-command-method))))
240 (defun gnus-status-message (gnus-command-method)
241 "Return the status message from GNUS-COMMAND-METHOD.
242 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
243 this group uses will be queried."
244 (let ((gnus-command-method
245 (if (stringp gnus-command-method)
246 (gnus-find-method-for-group gnus-command-method)
247 gnus-command-method)))
248 (funcall (gnus-get-function gnus-command-method 'status-message)
249 (nth 1 gnus-command-method))))
251 (defun gnus-request-regenerate (gnus-command-method)
252 "Request a data generation from GNUS-COMMAND-METHOD."
253 (when (stringp gnus-command-method)
254 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
255 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
256 (nth 1 gnus-command-method)))
258 (defun gnus-request-group (group &optional dont-check gnus-command-method)
259 "Request GROUP. If DONT-CHECK, no information is required."
260 (let ((gnus-command-method
261 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
262 (when (stringp gnus-command-method)
263 (setq gnus-command-method
264 (inline (gnus-server-to-method gnus-command-method))))
265 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
266 (gnus-group-real-name group) (nth 1 gnus-command-method)
269 (defun gnus-list-active-group (group)
270 "Request active information on GROUP."
271 (let ((gnus-command-method (gnus-find-method-for-group group))
272 (func 'list-active-group))
273 (when (gnus-check-backend-function func group)
274 (funcall (gnus-get-function gnus-command-method func)
275 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
277 (defun gnus-request-group-description (group)
278 "Request a description of GROUP."
279 (let ((gnus-command-method (gnus-find-method-for-group group))
280 (func 'request-group-description))
281 (when (gnus-check-backend-function func group)
282 (funcall (gnus-get-function gnus-command-method func)
283 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
285 (defun gnus-request-group-articles (group)
286 "Request a list of existing articles in GROUP."
287 (let ((gnus-command-method (gnus-find-method-for-group group))
288 (func 'request-group-articles))
289 (when (gnus-check-backend-function func group)
290 (funcall (gnus-get-function gnus-command-method func)
291 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
293 (defun gnus-close-group (group)
294 "Request the GROUP be closed."
295 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
296 (funcall (gnus-get-function gnus-command-method 'close-group)
297 (gnus-group-real-name group) (nth 1 gnus-command-method))))
299 (defun gnus-retrieve-headers (articles group &optional fetch-old)
300 "Request headers for ARTICLES in GROUP.
301 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
302 (let ((gnus-command-method (gnus-find-method-for-group group)))
303 (if (and gnus-use-cache (numberp (car articles)))
304 (gnus-cache-retrieve-headers articles group fetch-old)
305 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
306 articles (gnus-group-real-name group)
307 (nth 1 gnus-command-method) fetch-old))))
309 (defun gnus-retrieve-articles (articles group)
310 "Request ARTICLES in GROUP."
311 (let ((gnus-command-method (gnus-find-method-for-group group)))
312 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
313 articles (gnus-group-real-name group)
314 (nth 1 gnus-command-method))))
316 (defun gnus-retrieve-groups (groups gnus-command-method)
317 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
318 (when (stringp gnus-command-method)
319 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
320 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
321 groups (nth 1 gnus-command-method)))
323 (defun gnus-request-type (group &optional article)
324 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
325 (let ((gnus-command-method (gnus-find-method-for-group group)))
326 (if (not (gnus-check-backend-function
327 'request-type (car gnus-command-method)))
329 (funcall (gnus-get-function gnus-command-method 'request-type)
330 (gnus-group-real-name group) article))))
332 (defun gnus-request-set-mark (group action)
333 "Set marks on articles in the backend."
334 (let ((gnus-command-method (gnus-find-method-for-group group)))
335 (if (not (gnus-check-backend-function
336 'request-set-mark (car gnus-command-method)))
338 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
339 (gnus-group-real-name group) action
340 (nth 1 gnus-command-method)))))
342 (defun gnus-request-update-mark (group article mark)
343 "Allow the backend to change the mark the user tries to put on an article."
344 (let ((gnus-command-method (gnus-find-method-for-group group)))
345 (if (not (gnus-check-backend-function
346 'request-update-mark (car gnus-command-method)))
348 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
349 (gnus-group-real-name group) article mark))))
351 (defun gnus-request-article (article group &optional buffer)
352 "Request the ARTICLE in GROUP.
353 ARTICLE can either be an article number or an article Message-ID.
354 If BUFFER, insert the article in that group."
355 (let ((gnus-command-method (gnus-find-method-for-group group)))
356 (funcall (gnus-get-function gnus-command-method 'request-article)
357 article (gnus-group-real-name group)
358 (nth 1 gnus-command-method) buffer)))
360 (defun gnus-request-head (article group)
361 "Request the head of ARTICLE in GROUP."
362 (let* ((gnus-command-method (gnus-find-method-for-group group))
363 (head (gnus-get-function gnus-command-method 'request-head t))
369 (gnus-cache-request-article article group))
370 (setq res (cons group article)
372 ;; Use `head' function.
374 (setq res (funcall head article (gnus-group-real-name group)
375 (nth 1 gnus-command-method))))
376 ;; Use `article' function.
378 (setq res (gnus-request-article article group)
382 (set-buffer nntp-server-buffer)
383 (goto-char (point-min))
384 (when (search-forward "\n\n" nil t)
385 (delete-region (1- (point)) (point-max)))
386 (nnheader-fold-continuation-lines)))
389 (defun gnus-request-body (article group)
390 "Request the body of ARTICLE in GROUP."
391 (let* ((gnus-command-method (gnus-find-method-for-group group))
392 (head (gnus-get-function gnus-command-method 'request-body t))
398 (gnus-cache-request-article article group))
399 (setq res (cons group article)
401 ;; Use `head' function.
403 (setq res (funcall head article (gnus-group-real-name group)
404 (nth 1 gnus-command-method))))
405 ;; Use `article' function.
407 (setq res (gnus-request-article article group)
411 (set-buffer nntp-server-buffer)
412 (goto-char (point-min))
413 (when (search-forward "\n\n" nil t)
414 (delete-region (point-min) (1- (point))))))
417 (defun gnus-request-post (gnus-command-method)
418 "Post the current buffer using GNUS-COMMAND-METHOD."
419 (when (stringp gnus-command-method)
420 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
421 (funcall (gnus-get-function gnus-command-method 'request-post)
422 (nth 1 gnus-command-method)))
424 (defun gnus-request-scan (group gnus-command-method)
425 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
426 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
427 (let ((gnus-command-method
428 (if group (gnus-find-method-for-group group) gnus-command-method))
429 (gnus-inhibit-demon t)
430 (mail-source-plugged gnus-plugged))
431 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
432 (funcall (gnus-get-function gnus-command-method 'request-scan)
433 (and group (gnus-group-real-name group))
434 (nth 1 gnus-command-method)))))
436 (defsubst gnus-request-update-info (info gnus-command-method)
437 "Request that GNUS-COMMAND-METHOD update INFO."
438 (when (stringp gnus-command-method)
439 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
440 (when (gnus-check-backend-function
441 'request-update-info (car gnus-command-method))
442 (funcall (gnus-get-function gnus-command-method 'request-update-info)
443 (gnus-group-real-name (gnus-info-group info))
444 info (nth 1 gnus-command-method))))
446 (defun gnus-request-expire-articles (articles group &optional force)
447 (let ((gnus-command-method (gnus-find-method-for-group group)))
448 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
449 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
452 (defun gnus-request-move-article
453 (article group server accept-function &optional last)
454 (let ((gnus-command-method (gnus-find-method-for-group group)))
455 (funcall (gnus-get-function gnus-command-method 'request-move-article)
456 article (gnus-group-real-name group)
457 (nth 1 gnus-command-method) accept-function last)))
459 (defun gnus-request-accept-article (group &optional gnus-command-method last
461 ;; Make sure there's a newline at the end of the article.
462 (when (stringp gnus-command-method)
463 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
464 (when (and (not gnus-command-method)
466 (setq gnus-command-method (gnus-group-name-to-method group)))
467 (goto-char (point-max))
471 (let ((message-options message-options))
472 (message-options-set-recipient)
474 (message-narrow-to-head)
475 (let ((mail-parse-charset message-default-charset))
476 (mail-encode-encoded-word-buffer)))
477 (message-encode-message-body)))
478 (let ((func (car (or gnus-command-method
479 (gnus-find-method-for-group group)))))
480 (funcall (intern (format "%s-request-accept-article" func))
481 (if (stringp group) (gnus-group-real-name group) group)
482 (cadr gnus-command-method)
485 (defun gnus-request-replace-article (article group buffer &optional no-encode)
487 (let ((message-options message-options))
488 (message-options-set-recipient)
490 (message-narrow-to-head)
491 (let ((mail-parse-charset message-default-charset))
492 (mail-encode-encoded-word-buffer)))
493 (message-encode-message-body)))
494 (let ((func (car (gnus-group-name-to-method group))))
495 (funcall (intern (format "%s-request-replace-article" func))
496 article (gnus-group-real-name group) buffer)))
498 (defun gnus-request-associate-buffer (group)
499 (let ((gnus-command-method (gnus-find-method-for-group group)))
500 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
501 (gnus-group-real-name group))))
503 (defun gnus-request-restore-buffer (article group)
504 "Request a new buffer restored to the state of ARTICLE."
505 (let ((gnus-command-method (gnus-find-method-for-group group)))
506 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
507 article (gnus-group-real-name group)
508 (nth 1 gnus-command-method))))
510 (defun gnus-request-create-group (group &optional gnus-command-method args)
511 (when (stringp gnus-command-method)
512 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
513 (let ((gnus-command-method
514 (or gnus-command-method (gnus-find-method-for-group group))))
515 (funcall (gnus-get-function gnus-command-method 'request-create-group)
516 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
518 (defun gnus-request-delete-group (group &optional force)
519 (let ((gnus-command-method (gnus-find-method-for-group group)))
520 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
521 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
523 (defun gnus-request-rename-group (group new-name)
524 (let ((gnus-command-method (gnus-find-method-for-group group)))
525 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
526 (gnus-group-real-name group)
527 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
529 (defun gnus-close-backends ()
530 ;; Send a close request to all backends that support such a request.
531 (let ((methods gnus-valid-select-methods)
532 (gnus-inhibit-demon t)
533 func gnus-command-method)
534 (while (setq gnus-command-method (pop methods))
535 (when (fboundp (setq func (intern
536 (concat (car gnus-command-method)
540 (defun gnus-asynchronous-p (gnus-command-method)
541 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
545 (defun gnus-remove-denial (gnus-command-method)
546 (when (stringp gnus-command-method)
547 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
548 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
549 (status (cadr elem)))
550 ;; If this hasn't been opened before, we add it to the list.
551 (when (eq status 'denied)
552 ;; Set the status of this server.
553 (setcar (cdr elem) 'closed))))
557 ;;; gnus-int.el ends here