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")
186 (funcall (gnus-get-function gnus-command-method 'open-server)
187 (nth 1 gnus-command-method)
188 (nthcdr 2 gnus-command-method))))
189 ;; If this hasn't been opened before, we add it to the list.
191 (setq elem (list gnus-command-method nil)
192 gnus-opened-servers (cons elem gnus-opened-servers)))
193 ;; Set the status of this server.
194 (setcar (cdr elem) (if result 'ok 'denied))
195 ;; Return the result from the "open" call.
198 (defun gnus-close-server (gnus-command-method)
199 "Close the connection to 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 'close-server)
203 (nth 1 gnus-command-method)))
205 (defun gnus-request-list (gnus-command-method)
206 "Request the active 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)
210 (nth 1 gnus-command-method)))
212 (defun gnus-request-list-newsgroups (gnus-command-method)
213 "Request the newsgroups file from GNUS-COMMAND-METHOD."
214 (when (stringp gnus-command-method)
215 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
216 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
217 (nth 1 gnus-command-method)))
219 (defun gnus-request-newgroups (date gnus-command-method)
220 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
221 (when (stringp gnus-command-method)
222 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
223 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
225 (funcall func date (nth 1 gnus-command-method)))))
227 (defun gnus-server-opened (gnus-command-method)
228 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
229 (unless (eq (gnus-server-status gnus-command-method)
231 (when (stringp gnus-command-method)
232 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
233 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
234 (nth 1 gnus-command-method))))
236 (defun gnus-status-message (gnus-command-method)
237 "Return the status message from GNUS-COMMAND-METHOD.
238 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
239 this group uses will be queried."
240 (let ((gnus-command-method
241 (if (stringp gnus-command-method)
242 (gnus-find-method-for-group gnus-command-method)
243 gnus-command-method)))
244 (funcall (gnus-get-function gnus-command-method 'status-message)
245 (nth 1 gnus-command-method))))
247 (defun gnus-request-regenerate (gnus-command-method)
248 "Request a data generation from GNUS-COMMAND-METHOD."
249 (when (stringp gnus-command-method)
250 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
251 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
252 (nth 1 gnus-command-method)))
254 (defun gnus-request-group (group &optional dont-check gnus-command-method)
255 "Request GROUP. If DONT-CHECK, no information is required."
256 (let ((gnus-command-method
257 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
258 (when (stringp gnus-command-method)
259 (setq gnus-command-method
260 (inline (gnus-server-to-method gnus-command-method))))
261 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
262 (gnus-group-real-name group) (nth 1 gnus-command-method)
265 (defun gnus-list-active-group (group)
266 "Request active information on GROUP."
267 (let ((gnus-command-method (gnus-find-method-for-group group))
268 (func 'list-active-group))
269 (when (gnus-check-backend-function func group)
270 (funcall (gnus-get-function gnus-command-method func)
271 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
273 (defun gnus-request-group-description (group)
274 "Request a description of GROUP."
275 (let ((gnus-command-method (gnus-find-method-for-group group))
276 (func 'request-group-description))
277 (when (gnus-check-backend-function func group)
278 (funcall (gnus-get-function gnus-command-method func)
279 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
281 (defun gnus-request-group-articles (group)
282 "Request a list of existing articles in GROUP."
283 (let ((gnus-command-method (gnus-find-method-for-group group))
284 (func 'request-group-articles))
285 (when (gnus-check-backend-function func group)
286 (funcall (gnus-get-function gnus-command-method func)
287 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
289 (defun gnus-close-group (group)
290 "Request the GROUP be closed."
291 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
292 (funcall (gnus-get-function gnus-command-method 'close-group)
293 (gnus-group-real-name group) (nth 1 gnus-command-method))))
295 (defun gnus-retrieve-headers (articles group &optional fetch-old)
296 "Request headers for ARTICLES in GROUP.
297 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
298 (let ((gnus-command-method (gnus-find-method-for-group group)))
299 (if (and gnus-use-cache (numberp (car articles)))
300 (gnus-cache-retrieve-headers articles group fetch-old)
301 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
302 articles (gnus-group-real-name group)
303 (nth 1 gnus-command-method) fetch-old))))
305 (defun gnus-retrieve-articles (articles group)
306 "Request ARTICLES in GROUP."
307 (let ((gnus-command-method (gnus-find-method-for-group group)))
308 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
309 articles (gnus-group-real-name group)
310 (nth 1 gnus-command-method))))
312 (defun gnus-retrieve-groups (groups gnus-command-method)
313 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
314 (when (stringp gnus-command-method)
315 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
316 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
317 groups (nth 1 gnus-command-method)))
319 (defun gnus-request-type (group &optional article)
320 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
321 (let ((gnus-command-method (gnus-find-method-for-group group)))
322 (if (not (gnus-check-backend-function
323 'request-type (car gnus-command-method)))
325 (funcall (gnus-get-function gnus-command-method 'request-type)
326 (gnus-group-real-name group) article))))
328 (defun gnus-request-set-mark (group action)
329 "Set marks on articles in the backend."
330 (let ((gnus-command-method (gnus-find-method-for-group group)))
331 (if (not (gnus-check-backend-function
332 'request-set-mark (car gnus-command-method)))
334 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
335 (gnus-group-real-name group) action
336 (nth 1 gnus-command-method)))))
338 (defun gnus-request-update-mark (group article mark)
339 "Allow the backend to change the mark the user tries to put on an article."
340 (let ((gnus-command-method (gnus-find-method-for-group group)))
341 (if (not (gnus-check-backend-function
342 'request-update-mark (car gnus-command-method)))
344 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
345 (gnus-group-real-name group) article mark))))
347 (defun gnus-request-article (article group &optional buffer)
348 "Request the ARTICLE in GROUP.
349 ARTICLE can either be an article number or an article Message-ID.
350 If BUFFER, insert the article in that group."
351 (let ((gnus-command-method (gnus-find-method-for-group group)))
352 (funcall (gnus-get-function gnus-command-method 'request-article)
353 article (gnus-group-real-name group)
354 (nth 1 gnus-command-method) buffer)))
356 (defun gnus-request-head (article group)
357 "Request the head of ARTICLE in GROUP."
358 (let* ((gnus-command-method (gnus-find-method-for-group group))
359 (head (gnus-get-function gnus-command-method 'request-head t))
365 (gnus-cache-request-article article group))
366 (setq res (cons group article)
368 ;; Use `head' function.
370 (setq res (funcall head article (gnus-group-real-name group)
371 (nth 1 gnus-command-method))))
372 ;; Use `article' function.
374 (setq res (gnus-request-article article group)
378 (set-buffer nntp-server-buffer)
379 (goto-char (point-min))
380 (when (search-forward "\n\n" nil t)
381 (delete-region (1- (point)) (point-max)))
382 (nnheader-fold-continuation-lines)))
385 (defun gnus-request-body (article group)
386 "Request the body of ARTICLE in GROUP."
387 (let* ((gnus-command-method (gnus-find-method-for-group group))
388 (head (gnus-get-function gnus-command-method 'request-body t))
394 (gnus-cache-request-article article group))
395 (setq res (cons group article)
397 ;; Use `head' function.
399 (setq res (funcall head article (gnus-group-real-name group)
400 (nth 1 gnus-command-method))))
401 ;; Use `article' function.
403 (setq res (gnus-request-article article group)
407 (set-buffer nntp-server-buffer)
408 (goto-char (point-min))
409 (when (search-forward "\n\n" nil t)
410 (delete-region (point-min) (1- (point))))))
413 (defun gnus-request-post (gnus-command-method)
414 "Post the current buffer using GNUS-COMMAND-METHOD."
415 (when (stringp gnus-command-method)
416 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
417 (funcall (gnus-get-function gnus-command-method 'request-post)
418 (nth 1 gnus-command-method)))
420 (defun gnus-request-scan (group gnus-command-method)
421 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
422 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
423 (let ((gnus-command-method
424 (if group (gnus-find-method-for-group group) gnus-command-method))
425 (gnus-inhibit-demon t)
426 (mail-source-plugged gnus-plugged))
427 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
428 (funcall (gnus-get-function gnus-command-method 'request-scan)
429 (and group (gnus-group-real-name group))
430 (nth 1 gnus-command-method)))))
432 (defsubst gnus-request-update-info (info gnus-command-method)
433 "Request that GNUS-COMMAND-METHOD update INFO."
434 (when (stringp gnus-command-method)
435 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
436 (when (gnus-check-backend-function
437 'request-update-info (car gnus-command-method))
438 (funcall (gnus-get-function gnus-command-method 'request-update-info)
439 (gnus-group-real-name (gnus-info-group info))
440 info (nth 1 gnus-command-method))))
442 (defun gnus-request-expire-articles (articles group &optional force)
443 (let ((gnus-command-method (gnus-find-method-for-group group)))
444 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
445 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
448 (defun gnus-request-move-article
449 (article group server accept-function &optional last)
450 (let ((gnus-command-method (gnus-find-method-for-group group)))
451 (funcall (gnus-get-function gnus-command-method 'request-move-article)
452 article (gnus-group-real-name group)
453 (nth 1 gnus-command-method) accept-function last)))
455 (defun gnus-request-accept-article (group &optional gnus-command-method last
457 ;; Make sure there's a newline at the end of the article.
458 (when (stringp gnus-command-method)
459 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
460 (when (and (not gnus-command-method)
462 (setq gnus-command-method (gnus-group-name-to-method group)))
463 (goto-char (point-max))
467 (let ((message-options message-options))
468 (message-options-set-recipient)
470 (message-narrow-to-head)
471 (let ((mail-parse-charset message-default-charset))
472 (mail-encode-encoded-word-buffer)))
473 (message-encode-message-body)))
474 (let ((func (car (or gnus-command-method
475 (gnus-find-method-for-group group)))))
476 (funcall (intern (format "%s-request-accept-article" func))
477 (if (stringp group) (gnus-group-real-name group) group)
478 (cadr gnus-command-method)
481 (defun gnus-request-replace-article (article group buffer &optional no-encode)
483 (let ((message-options message-options))
484 (message-options-set-recipient)
486 (message-narrow-to-head)
487 (let ((mail-parse-charset message-default-charset))
488 (mail-encode-encoded-word-buffer)))
489 (message-encode-message-body)))
490 (let ((func (car (gnus-group-name-to-method group))))
491 (funcall (intern (format "%s-request-replace-article" func))
492 article (gnus-group-real-name group) buffer)))
494 (defun gnus-request-associate-buffer (group)
495 (let ((gnus-command-method (gnus-find-method-for-group group)))
496 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
497 (gnus-group-real-name group))))
499 (defun gnus-request-restore-buffer (article group)
500 "Request a new buffer restored to the state of ARTICLE."
501 (let ((gnus-command-method (gnus-find-method-for-group group)))
502 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
503 article (gnus-group-real-name group)
504 (nth 1 gnus-command-method))))
506 (defun gnus-request-create-group (group &optional gnus-command-method args)
507 (when (stringp gnus-command-method)
508 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
509 (let ((gnus-command-method
510 (or gnus-command-method (gnus-find-method-for-group group))))
511 (funcall (gnus-get-function gnus-command-method 'request-create-group)
512 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
514 (defun gnus-request-delete-group (group &optional force)
515 (let ((gnus-command-method (gnus-find-method-for-group group)))
516 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
517 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
519 (defun gnus-request-rename-group (group new-name)
520 (let ((gnus-command-method (gnus-find-method-for-group group)))
521 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
522 (gnus-group-real-name group)
523 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
525 (defun gnus-close-backends ()
526 ;; Send a close request to all backends that support such a request.
527 (let ((methods gnus-valid-select-methods)
528 (gnus-inhibit-demon t)
529 func gnus-command-method)
530 (while (setq gnus-command-method (pop methods))
531 (when (fboundp (setq func (intern
532 (concat (car gnus-command-method)
536 (defun gnus-asynchronous-p (gnus-command-method)
537 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
541 (defun gnus-remove-denial (gnus-command-method)
542 (when (stringp gnus-command-method)
543 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
544 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
545 (status (cadr elem)))
546 ;; If this hasn't been opened before, we add it to the list.
547 (when (eq status 'denied)
548 ;; Set the status of this server.
549 (setcar (cdr elem) 'closed))))
553 ;;; gnus-int.el ends here