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)))
304 ((and gnus-use-cache (numberp (car articles)))
305 (gnus-cache-retrieve-headers articles group fetch-old))
306 ((and gnus-agent gnus-agent-cache gnus-plugged
307 (gnus-agent-method-p gnus-command-method))
308 (gnus-agent-retrieve-headers articles group fetch-old))
310 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
311 articles (gnus-group-real-name group)
312 (nth 1 gnus-command-method) fetch-old)))))
314 (defun gnus-retrieve-articles (articles group)
315 "Request ARTICLES in GROUP."
316 (let ((gnus-command-method (gnus-find-method-for-group group)))
317 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
318 articles (gnus-group-real-name group)
319 (nth 1 gnus-command-method))))
321 (defun gnus-retrieve-groups (groups gnus-command-method)
322 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
323 (when (stringp gnus-command-method)
324 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
325 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
326 groups (nth 1 gnus-command-method)))
328 (defun gnus-request-type (group &optional article)
329 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
330 (let ((gnus-command-method (gnus-find-method-for-group group)))
331 (if (not (gnus-check-backend-function
332 'request-type (car gnus-command-method)))
334 (funcall (gnus-get-function gnus-command-method 'request-type)
335 (gnus-group-real-name group) article))))
337 (defun gnus-request-set-mark (group action)
338 "Set marks on articles in the backend."
339 (let ((gnus-command-method (gnus-find-method-for-group group)))
340 (if (not (gnus-check-backend-function
341 'request-set-mark (car gnus-command-method)))
343 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
344 (gnus-group-real-name group) action
345 (nth 1 gnus-command-method)))))
347 (defun gnus-request-update-mark (group article mark)
348 "Allow the backend to change the mark the user tries to put on an article."
349 (let ((gnus-command-method (gnus-find-method-for-group group)))
350 (if (not (gnus-check-backend-function
351 'request-update-mark (car gnus-command-method)))
353 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
354 (gnus-group-real-name group) article mark))))
356 (defun gnus-request-article (article group &optional buffer)
357 "Request the ARTICLE in GROUP.
358 ARTICLE can either be an article number or an article Message-ID.
359 If BUFFER, insert the article in that group."
360 (let ((gnus-command-method (gnus-find-method-for-group group)))
361 (funcall (gnus-get-function gnus-command-method 'request-article)
362 article (gnus-group-real-name group)
363 (nth 1 gnus-command-method) buffer)))
365 (defun gnus-request-head (article group)
366 "Request the head of ARTICLE in GROUP."
367 (let* ((gnus-command-method (gnus-find-method-for-group group))
368 (head (gnus-get-function gnus-command-method 'request-head t))
374 (gnus-cache-request-article article group))
375 (setq res (cons group article)
377 ((and gnus-agent gnus-agent-cache gnus-plugged
379 (gnus-agent-request-article article group))
380 (setq res (cons group article)
382 ;; Use `head' function.
384 (setq res (funcall head article (gnus-group-real-name group)
385 (nth 1 gnus-command-method))))
386 ;; Use `article' function.
388 (setq res (gnus-request-article article group)
392 (set-buffer nntp-server-buffer)
393 (goto-char (point-min))
394 (when (search-forward "\n\n" nil t)
395 (delete-region (1- (point)) (point-max)))
396 (nnheader-fold-continuation-lines)))
399 (defun gnus-request-body (article group)
400 "Request the body of ARTICLE in GROUP."
401 (let* ((gnus-command-method (gnus-find-method-for-group group))
402 (head (gnus-get-function gnus-command-method 'request-body t))
408 (gnus-cache-request-article article group))
409 (setq res (cons group article)
411 ;; Check the agent cache.
412 ((and gnus-agent gnus-agent-cache gnus-plugged
414 (gnus-agent-request-article article group))
415 (setq res (cons group article)
417 ;; Use `head' function.
419 (setq res (funcall head article (gnus-group-real-name group)
420 (nth 1 gnus-command-method))))
421 ;; Use `article' function.
423 (setq res (gnus-request-article article group)
427 (set-buffer nntp-server-buffer)
428 (goto-char (point-min))
429 (when (search-forward "\n\n" nil t)
430 (delete-region (point-min) (1- (point))))))
433 (defun gnus-request-post (gnus-command-method)
434 "Post the current buffer using GNUS-COMMAND-METHOD."
435 (when (stringp gnus-command-method)
436 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
437 (funcall (gnus-get-function gnus-command-method 'request-post)
438 (nth 1 gnus-command-method)))
440 (defun gnus-request-scan (group gnus-command-method)
441 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
442 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
443 (let ((gnus-command-method
444 (if group (gnus-find-method-for-group group) gnus-command-method))
445 (gnus-inhibit-demon t)
446 (mail-source-plugged gnus-plugged))
447 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
448 (funcall (gnus-get-function gnus-command-method 'request-scan)
449 (and group (gnus-group-real-name group))
450 (nth 1 gnus-command-method)))))
452 (defsubst gnus-request-update-info (info gnus-command-method)
453 "Request that GNUS-COMMAND-METHOD update INFO."
454 (when (stringp gnus-command-method)
455 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
456 (when (gnus-check-backend-function
457 'request-update-info (car gnus-command-method))
458 (funcall (gnus-get-function gnus-command-method 'request-update-info)
459 (gnus-group-real-name (gnus-info-group info))
460 info (nth 1 gnus-command-method))))
462 (defun gnus-request-expire-articles (articles group &optional force)
463 (let ((gnus-command-method (gnus-find-method-for-group group)))
464 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
465 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
468 (defun gnus-request-move-article
469 (article group server accept-function &optional last)
470 (let ((gnus-command-method (gnus-find-method-for-group group)))
471 (funcall (gnus-get-function gnus-command-method 'request-move-article)
472 article (gnus-group-real-name group)
473 (nth 1 gnus-command-method) accept-function last)))
475 (defun gnus-request-accept-article (group &optional gnus-command-method last
477 ;; Make sure there's a newline at the end of the article.
478 (when (stringp gnus-command-method)
479 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
480 (when (and (not gnus-command-method)
482 (setq gnus-command-method (gnus-group-name-to-method group)))
483 (goto-char (point-max))
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 (or gnus-command-method
495 (gnus-find-method-for-group group)))))
496 (funcall (intern (format "%s-request-accept-article" func))
497 (if (stringp group) (gnus-group-real-name group) group)
498 (cadr gnus-command-method)
501 (defun gnus-request-replace-article (article group buffer &optional no-encode)
503 (let ((message-options message-options))
504 (message-options-set-recipient)
506 (message-narrow-to-head)
507 (let ((mail-parse-charset message-default-charset))
508 (mail-encode-encoded-word-buffer)))
509 (message-encode-message-body)))
510 (let ((func (car (gnus-group-name-to-method group))))
511 (funcall (intern (format "%s-request-replace-article" func))
512 article (gnus-group-real-name group) buffer)))
514 (defun gnus-request-associate-buffer (group)
515 (let ((gnus-command-method (gnus-find-method-for-group group)))
516 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
517 (gnus-group-real-name group))))
519 (defun gnus-request-restore-buffer (article group)
520 "Request a new buffer restored to the state of ARTICLE."
521 (let ((gnus-command-method (gnus-find-method-for-group group)))
522 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
523 article (gnus-group-real-name group)
524 (nth 1 gnus-command-method))))
526 (defun gnus-request-create-group (group &optional gnus-command-method args)
527 (when (stringp gnus-command-method)
528 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
529 (let ((gnus-command-method
530 (or gnus-command-method (gnus-find-method-for-group group))))
531 (funcall (gnus-get-function gnus-command-method 'request-create-group)
532 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
534 (defun gnus-request-delete-group (group &optional force)
535 (let ((gnus-command-method (gnus-find-method-for-group group)))
536 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
537 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
539 (defun gnus-request-rename-group (group new-name)
540 (let ((gnus-command-method (gnus-find-method-for-group group)))
541 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
542 (gnus-group-real-name group)
543 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
545 (defun gnus-close-backends ()
546 ;; Send a close request to all backends that support such a request.
547 (let ((methods gnus-valid-select-methods)
548 (gnus-inhibit-demon t)
549 func gnus-command-method)
550 (while (setq gnus-command-method (pop methods))
551 (when (fboundp (setq func (intern
552 (concat (car gnus-command-method)
556 (defun gnus-asynchronous-p (gnus-command-method)
557 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
561 (defun gnus-remove-denial (gnus-command-method)
562 (when (stringp gnus-command-method)
563 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
564 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
565 (status (cadr elem)))
566 ;; If this hasn't been opened before, we add it to the list.
567 (when (eq status 'denied)
568 ;; Set the status of this server.
569 (setcar (cdr elem) 'closed))))
573 ;;; gnus-int.el ends here