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>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
34 (defcustom gnus-open-server-hook nil
35 "Hook called just before opening connection to the news server."
40 ;;; Server Communication
43 (defun gnus-start-news-server (&optional confirm)
44 "Open a method for getting news.
45 If CONFIRM is non-nil, the user will be asked for an NNTP server."
47 (if gnus-current-select-method
48 ;; Stream is already opened.
51 (unless gnus-nntp-service
52 (setq gnus-nntp-server nil))
54 ;; Read server name with completion.
55 (setq gnus-nntp-server
56 (completing-read "NNTP server: "
57 (mapcar (lambda (server) (list server))
58 (cons (list gnus-nntp-server)
59 gnus-secondary-servers))
60 nil nil gnus-nntp-server)))
62 (when (and gnus-nntp-server
63 (stringp gnus-nntp-server)
64 (not (string= gnus-nntp-server "")))
65 (setq gnus-select-method
66 (cond ((or (string= gnus-nntp-server "")
67 (string= gnus-nntp-server "::"))
68 (list 'nnspool (system-name)))
69 ((string-match "^:" gnus-nntp-server)
70 (list 'nnmh gnus-nntp-server
72 (file-name-as-directory
74 (substring gnus-nntp-server 1) "~/")))
75 (list 'nnmh-get-new-mail nil)))
77 (list 'nntp gnus-nntp-server)))))
79 (setq how (car gnus-select-method))
83 (gnus-message 5 "Looking up local news spool..."))
86 (gnus-message 5 "Looking up mh spool..."))
89 (setq gnus-current-select-method gnus-select-method)
90 (gnus-run-hooks 'gnus-open-server-hook)
92 ;; gnus-open-server-hook might have opened it
93 (gnus-server-opened gnus-select-method)
94 (gnus-open-server gnus-select-method)
98 "%s (%s) open error: '%s'. Continue? "
99 (car gnus-select-method) (cadr gnus-select-method)
100 (gnus-status-message gnus-select-method)))
101 (gnus-error 1 "Couldn't open server on %s"
102 (nth 1 gnus-select-method))))))
104 (defun gnus-check-group (group)
105 "Try to make sure that the server where GROUP exists is alive."
106 (let ((method (gnus-find-method-for-group group)))
107 (or (gnus-server-opened method)
108 (gnus-open-server method))))
110 (defun gnus-check-server (&optional method silent)
111 "Check whether the connection to METHOD is down.
112 If METHOD is nil, use `gnus-select-method'.
113 If it is down, start it up (again)."
114 (let ((method (or method gnus-select-method))
116 ;; Transform virtual server names into select methods.
117 (when (stringp method)
118 (setq method (gnus-server-to-method method)))
119 (if (gnus-server-opened method)
120 ;; The stream is already opened.
124 (gnus-message 5 "Opening %s server%s..." (car method)
125 (if (equal (nth 1 method) "") ""
126 (format " on %s" (nth 1 method)))))
127 (gnus-run-hooks 'gnus-open-server-hook)
130 (setq result (gnus-open-server method))
131 (quit (message "Quit gnus-check-server")
134 (gnus-message 5 "Opening %s server%s...%s" (car method)
135 (if (equal (nth 1 method) "") ""
136 (format " on %s" (nth 1 method)))
137 (if result "done" "failed")))))))
139 (defun gnus-get-function (method function &optional noerror)
140 "Return a function symbol based on METHOD and FUNCTION."
141 ;; Translate server names into methods.
143 (error "Attempted use of a nil select method"))
144 (when (stringp method)
145 (setq method (gnus-server-to-method method)))
146 ;; Check cache of constructed names.
147 (let* ((method-sym (if gnus-agent
148 (gnus-agent-get-function method)
150 (method-fns (get method-sym 'gnus-method-functions))
151 (func (let ((method-fnlist-elt (assq function method-fns)))
152 (unless method-fnlist-elt
153 (setq method-fnlist-elt
155 (intern (format "%s-%s" method-sym function))))
156 (put method-sym 'gnus-method-functions
157 (cons method-fnlist-elt method-fns)))
158 (cdr method-fnlist-elt))))
159 ;; Maybe complain if there is no function.
160 (unless (fboundp func)
162 (error "Trying to require a method that doesn't exist"))
163 (require (car method))
164 (when (not (fboundp func))
167 (error "No such function: %s" func))))
172 ;;; Interface functions to the backends.
175 (defun gnus-open-server (gnus-command-method)
176 "Open a connection to GNUS-COMMAND-METHOD."
177 (when (stringp gnus-command-method)
178 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
179 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
180 ;; If this method was previously denied, we just return nil.
181 (if (eq (nth 1 elem) 'denied)
183 (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))))
190 ;; If this hasn't been opened before, we add it to the list.
192 (setq elem (list gnus-command-method nil)
193 gnus-opened-servers (cons elem gnus-opened-servers)))
194 ;; Set the status of this server.
195 (setcar (cdr elem) (if result 'ok 'denied))
196 ;; Return the result from the "open" call.
199 (defun gnus-close-server (gnus-command-method)
200 "Close the connection to GNUS-COMMAND-METHOD."
201 (when (stringp gnus-command-method)
202 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
203 (funcall (gnus-get-function gnus-command-method 'close-server)
204 (nth 1 gnus-command-method)))
206 (defun gnus-request-list (gnus-command-method)
207 "Request the active file from GNUS-COMMAND-METHOD."
208 (when (stringp gnus-command-method)
209 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
210 (funcall (gnus-get-function gnus-command-method 'request-list)
211 (nth 1 gnus-command-method)))
213 (defun gnus-request-list-newsgroups (gnus-command-method)
214 "Request the newsgroups file from GNUS-COMMAND-METHOD."
215 (when (stringp gnus-command-method)
216 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
217 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
218 (nth 1 gnus-command-method)))
220 (defun gnus-request-newgroups (date gnus-command-method)
221 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
222 (when (stringp gnus-command-method)
223 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
224 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
226 (funcall func date (nth 1 gnus-command-method)))))
228 (defun gnus-server-opened (gnus-command-method)
229 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
230 (unless (eq (gnus-server-status gnus-command-method)
232 (when (stringp gnus-command-method)
233 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
234 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
235 (nth 1 gnus-command-method))))
237 (defun gnus-status-message (gnus-command-method)
238 "Return the status message from GNUS-COMMAND-METHOD.
239 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
240 this group uses will be queried."
241 (let ((gnus-command-method
242 (if (stringp gnus-command-method)
243 (gnus-find-method-for-group gnus-command-method)
244 gnus-command-method)))
245 (funcall (gnus-get-function gnus-command-method 'status-message)
246 (nth 1 gnus-command-method))))
248 (defun gnus-request-regenerate (gnus-command-method)
249 "Request a data generation from GNUS-COMMAND-METHOD."
250 (when (stringp gnus-command-method)
251 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
252 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
253 (nth 1 gnus-command-method)))
255 (defun gnus-request-group (group &optional dont-check gnus-command-method)
256 "Request GROUP. If DONT-CHECK, no information is required."
257 (let ((gnus-command-method
258 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
259 (when (stringp gnus-command-method)
260 (setq gnus-command-method
261 (inline (gnus-server-to-method gnus-command-method))))
262 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
263 (gnus-group-real-name group) (nth 1 gnus-command-method)
266 (defun gnus-list-active-group (group)
267 "Request active information on GROUP."
268 (let ((gnus-command-method (gnus-find-method-for-group group))
269 (func 'list-active-group))
270 (when (gnus-check-backend-function func group)
271 (funcall (gnus-get-function gnus-command-method func)
272 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
274 (defun gnus-request-group-description (group)
275 "Request a description of GROUP."
276 (let ((gnus-command-method (gnus-find-method-for-group group))
277 (func 'request-group-description))
278 (when (gnus-check-backend-function func group)
279 (funcall (gnus-get-function gnus-command-method func)
280 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
282 (defun gnus-request-group-articles (group)
283 "Request a list of existing articles in GROUP."
284 (let ((gnus-command-method (gnus-find-method-for-group group))
285 (func 'request-group-articles))
286 (when (gnus-check-backend-function func group)
287 (funcall (gnus-get-function gnus-command-method func)
288 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
290 (defun gnus-close-group (group)
291 "Request the GROUP be closed."
292 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
293 (funcall (gnus-get-function gnus-command-method 'close-group)
294 (gnus-group-real-name group) (nth 1 gnus-command-method))))
296 (defun gnus-retrieve-headers (articles group &optional fetch-old)
297 "Request headers for ARTICLES in GROUP.
298 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
299 (let ((gnus-command-method (gnus-find-method-for-group group)))
300 (if (and gnus-use-cache (numberp (car articles)))
301 (gnus-cache-retrieve-headers articles group fetch-old)
302 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
303 articles (gnus-group-real-name group)
304 (nth 1 gnus-command-method) fetch-old))))
306 (defun gnus-retrieve-articles (articles group)
307 "Request ARTICLES in GROUP."
308 (let ((gnus-command-method (gnus-find-method-for-group group)))
309 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
310 articles (gnus-group-real-name group)
311 (nth 1 gnus-command-method))))
313 (defun gnus-retrieve-groups (groups gnus-command-method)
314 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
315 (when (stringp gnus-command-method)
316 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
317 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
318 groups (nth 1 gnus-command-method)))
320 (defun gnus-request-type (group &optional article)
321 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
322 (let ((gnus-command-method (gnus-find-method-for-group group)))
323 (if (not (gnus-check-backend-function
324 'request-type (car gnus-command-method)))
326 (funcall (gnus-get-function gnus-command-method 'request-type)
327 (gnus-group-real-name group) article))))
329 (defun gnus-request-set-mark (group action)
330 "Set marks on articles in the backend."
331 (let ((gnus-command-method (gnus-find-method-for-group group)))
332 (if (not (gnus-check-backend-function
333 'request-set-mark (car gnus-command-method)))
335 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
336 (gnus-group-real-name group) action
337 (nth 1 gnus-command-method)))))
339 (defun gnus-request-update-mark (group article mark)
340 "Allow the backend to change the mark the user tries to put on an article."
341 (let ((gnus-command-method (gnus-find-method-for-group group)))
342 (if (not (gnus-check-backend-function
343 'request-update-mark (car gnus-command-method)))
345 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
346 (gnus-group-real-name group) article mark))))
348 (defun gnus-request-article (article group &optional buffer)
349 "Request the ARTICLE in GROUP.
350 ARTICLE can either be an article number or an article Message-ID.
351 If BUFFER, insert the article in that group."
352 (let ((gnus-command-method (gnus-find-method-for-group group)))
353 (funcall (gnus-get-function gnus-command-method 'request-article)
354 article (gnus-group-real-name group)
355 (nth 1 gnus-command-method) buffer)))
357 (defun gnus-request-head (article group)
358 "Request the head of ARTICLE in GROUP."
359 (let* ((gnus-command-method (gnus-find-method-for-group group))
360 (head (gnus-get-function gnus-command-method 'request-head 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 (1- (point)) (point-max)))
383 (nnheader-fold-continuation-lines)))
386 (defun gnus-request-body (article group)
387 "Request the body of ARTICLE in GROUP."
388 (let* ((gnus-command-method (gnus-find-method-for-group group))
389 (head (gnus-get-function gnus-command-method 'request-body t))
395 (gnus-cache-request-article article group))
396 (setq res (cons group article)
398 ;; Use `head' function.
400 (setq res (funcall head article (gnus-group-real-name group)
401 (nth 1 gnus-command-method))))
402 ;; Use `article' function.
404 (setq res (gnus-request-article article group)
408 (set-buffer nntp-server-buffer)
409 (goto-char (point-min))
410 (when (search-forward "\n\n" nil t)
411 (delete-region (point-min) (1- (point))))))
414 (defun gnus-request-post (gnus-command-method)
415 "Post the current buffer using GNUS-COMMAND-METHOD."
416 (when (stringp gnus-command-method)
417 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
418 (funcall (gnus-get-function gnus-command-method 'request-post)
419 (nth 1 gnus-command-method)))
421 (defun gnus-request-scan (group gnus-command-method)
422 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
423 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
424 (let ((gnus-command-method
425 (if group (gnus-find-method-for-group group) gnus-command-method))
426 (gnus-inhibit-demon t)
427 (mail-source-plugged gnus-plugged))
428 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
429 (funcall (gnus-get-function gnus-command-method 'request-scan)
430 (and group (gnus-group-real-name group))
431 (nth 1 gnus-command-method)))))
433 (defsubst gnus-request-update-info (info gnus-command-method)
434 "Request that GNUS-COMMAND-METHOD update INFO."
435 (when (stringp gnus-command-method)
436 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
437 (when (gnus-check-backend-function
438 'request-update-info (car gnus-command-method))
439 (funcall (gnus-get-function gnus-command-method 'request-update-info)
440 (gnus-group-real-name (gnus-info-group info))
441 info (nth 1 gnus-command-method))))
443 (defun gnus-request-expire-articles (articles group &optional force)
444 (let ((gnus-command-method (gnus-find-method-for-group group)))
445 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
446 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
449 (defun gnus-request-move-article
450 (article group server accept-function &optional last)
451 (let ((gnus-command-method (gnus-find-method-for-group group)))
452 (funcall (gnus-get-function gnus-command-method 'request-move-article)
453 article (gnus-group-real-name group)
454 (nth 1 gnus-command-method) accept-function last)))
456 (defun gnus-request-accept-article (group &optional gnus-command-method last
458 ;; Make sure there's a newline at the end of the article.
459 (when (stringp gnus-command-method)
460 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
461 (when (and (not gnus-command-method)
463 (setq gnus-command-method (gnus-group-name-to-method group)))
464 (goto-char (point-max))
467 (let ((func (car (or gnus-command-method
468 (gnus-find-method-for-group group)))))
469 (funcall (intern (format "%s-request-accept-article" func))
470 (if (stringp group) (gnus-group-real-name group) group)
471 (cadr gnus-command-method)
474 (defun gnus-request-replace-article (article group buffer &optional no-encode)
475 (let ((func (car (gnus-group-name-to-method group))))
476 (funcall (intern (format "%s-request-replace-article" func))
477 article (gnus-group-real-name group) buffer)))
479 (defun gnus-request-associate-buffer (group)
480 (let ((gnus-command-method (gnus-find-method-for-group group)))
481 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
482 (gnus-group-real-name group))))
484 (defun gnus-request-restore-buffer (article group)
485 "Request a new buffer restored to the state of ARTICLE."
486 (let ((gnus-command-method (gnus-find-method-for-group group)))
487 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
488 article (gnus-group-real-name group)
489 (nth 1 gnus-command-method))))
491 (defun gnus-request-create-group (group &optional gnus-command-method args)
492 (when (stringp gnus-command-method)
493 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
494 (let ((gnus-command-method
495 (or gnus-command-method (gnus-find-method-for-group group))))
496 (funcall (gnus-get-function gnus-command-method 'request-create-group)
497 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
499 (defun gnus-request-delete-group (group &optional force)
500 (let ((gnus-command-method (gnus-find-method-for-group group)))
501 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
502 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
504 (defun gnus-request-rename-group (group new-name)
505 (let ((gnus-command-method (gnus-find-method-for-group group)))
506 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
507 (gnus-group-real-name group)
508 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
510 (defun gnus-close-backends ()
511 ;; Send a close request to all backends that support such a request.
512 (let ((methods gnus-valid-select-methods)
513 (gnus-inhibit-demon t)
514 func gnus-command-method)
515 (while (setq gnus-command-method (pop methods))
516 (when (fboundp (setq func (intern
517 (concat (car gnus-command-method)
521 (defun gnus-asynchronous-p (gnus-command-method)
522 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
526 (defun gnus-remove-denial (gnus-command-method)
527 (when (stringp gnus-command-method)
528 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
529 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
530 (status (cadr elem)))
531 ;; If this hasn't been opened before, we add it to the list.
532 (when (eq status 'denied)
533 ;; Set the status of this server.
534 (setcar (cdr elem) 'closed))))
538 ;;; gnus-int.el ends here