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)))
114 ;; Transform virtual server names into select methods.
115 (when (stringp method)
116 (setq method (gnus-server-to-method method)))
117 (if (gnus-server-opened method)
118 ;; The stream is already opened.
122 (gnus-message 5 "Opening %s server%s..." (car method)
123 (if (equal (nth 1 method) "") ""
124 (format " on %s" (nth 1 method)))))
125 (gnus-run-hooks 'gnus-open-server-hook)
128 (gnus-open-server method)
129 (quit (message "Quit gnus-check-server")
134 (defun gnus-get-function (method function &optional noerror)
135 "Return a function symbol based on METHOD and FUNCTION."
136 ;; Translate server names into methods.
138 (error "Attempted use of a nil select method"))
139 (when (stringp method)
140 (setq method (gnus-server-to-method method)))
141 ;; Check cache of constructed names.
142 (let* ((method-sym (if gnus-agent
143 (gnus-agent-get-function method)
145 (method-fns (get method-sym 'gnus-method-functions))
146 (func (let ((method-fnlist-elt (assq function method-fns)))
147 (unless method-fnlist-elt
148 (setq method-fnlist-elt
150 (intern (format "%s-%s" method-sym function))))
151 (put method-sym 'gnus-method-functions
152 (cons method-fnlist-elt method-fns)))
153 (cdr method-fnlist-elt))))
154 ;; Maybe complain if there is no function.
155 (unless (fboundp func)
157 (error "Trying to require a method that doesn't exist"))
158 (require (car method))
159 (when (not (fboundp func))
162 (error "No such function: %s" func))))
167 ;;; Interface functions to the backends.
170 (defun gnus-open-server (gnus-command-method)
171 "Open a connection to GNUS-COMMAND-METHOD."
172 (when (stringp gnus-command-method)
173 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
174 (let ((elem (assoc gnus-command-method gnus-opened-servers)))
175 ;; If this method was previously denied, we just return nil.
176 (if (eq (nth 1 elem) 'denied)
178 (gnus-message 1 "Denied server")
182 (funcall (gnus-get-function gnus-command-method 'open-server)
183 (nth 1 gnus-command-method)
184 (nthcdr 2 gnus-command-method))))
185 ;; If this hasn't been opened before, we add it to the list.
187 (setq elem (list gnus-command-method nil)
188 gnus-opened-servers (cons elem gnus-opened-servers)))
189 ;; Set the status of this server.
190 (setcar (cdr elem) (if result 'ok 'denied))
191 ;; Return the result from the "open" call.
194 (defun gnus-close-server (gnus-command-method)
195 "Close the connection to GNUS-COMMAND-METHOD."
196 (when (stringp gnus-command-method)
197 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
198 (funcall (gnus-get-function gnus-command-method 'close-server)
199 (nth 1 gnus-command-method)))
201 (defun gnus-request-list (gnus-command-method)
202 "Request the active file from GNUS-COMMAND-METHOD."
203 (when (stringp gnus-command-method)
204 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
205 (funcall (gnus-get-function gnus-command-method 'request-list)
206 (nth 1 gnus-command-method)))
208 (defun gnus-request-list-newsgroups (gnus-command-method)
209 "Request the newsgroups file from GNUS-COMMAND-METHOD."
210 (when (stringp gnus-command-method)
211 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
212 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
213 (nth 1 gnus-command-method)))
215 (defun gnus-request-newgroups (date gnus-command-method)
216 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
217 (when (stringp gnus-command-method)
218 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
219 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
221 (funcall func date (nth 1 gnus-command-method)))))
223 (defun gnus-server-opened (gnus-command-method)
224 "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
225 (unless (eq (gnus-server-status gnus-command-method)
227 (when (stringp gnus-command-method)
228 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
229 (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
230 (nth 1 gnus-command-method))))
232 (defun gnus-status-message (gnus-command-method)
233 "Return the status message from GNUS-COMMAND-METHOD.
234 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group name. The method
235 this group uses will be queried."
236 (let ((gnus-command-method
237 (if (stringp gnus-command-method)
238 (gnus-find-method-for-group gnus-command-method)
239 gnus-command-method)))
240 (funcall (gnus-get-function gnus-command-method 'status-message)
241 (nth 1 gnus-command-method))))
243 (defun gnus-request-regenerate (gnus-command-method)
244 "Request a data generation from GNUS-COMMAND-METHOD."
245 (when (stringp gnus-command-method)
246 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
247 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
248 (nth 1 gnus-command-method)))
250 (defun gnus-request-group (group &optional dont-check gnus-command-method)
251 "Request GROUP. If DONT-CHECK, no information is required."
252 (let ((gnus-command-method
253 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
254 (when (stringp gnus-command-method)
255 (setq gnus-command-method
256 (inline (gnus-server-to-method gnus-command-method))))
257 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
258 (gnus-group-real-name group) (nth 1 gnus-command-method)
261 (defun gnus-list-active-group (group)
262 "Request active information on GROUP."
263 (let ((gnus-command-method (gnus-find-method-for-group group))
264 (func 'list-active-group))
265 (when (gnus-check-backend-function func group)
266 (funcall (gnus-get-function gnus-command-method func)
267 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
269 (defun gnus-request-group-description (group)
270 "Request a description of GROUP."
271 (let ((gnus-command-method (gnus-find-method-for-group group))
272 (func 'request-group-description))
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-articles (group)
278 "Request a list of existing articles in GROUP."
279 (let ((gnus-command-method (gnus-find-method-for-group group))
280 (func 'request-group-articles))
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-close-group (group)
286 "Request the GROUP be closed."
287 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
288 (funcall (gnus-get-function gnus-command-method 'close-group)
289 (gnus-group-real-name group) (nth 1 gnus-command-method))))
291 (defun gnus-retrieve-headers (articles group &optional fetch-old)
292 "Request headers for ARTICLES in GROUP.
293 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
294 (let ((gnus-command-method (gnus-find-method-for-group group)))
295 (if (and gnus-use-cache (numberp (car articles)))
296 (gnus-cache-retrieve-headers articles group fetch-old)
297 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
298 articles (gnus-group-real-name group)
299 (nth 1 gnus-command-method) fetch-old))))
301 (defun gnus-retrieve-articles (articles group)
302 "Request ARTICLES in GROUP."
303 (let ((gnus-command-method (gnus-find-method-for-group group)))
304 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
305 articles (gnus-group-real-name group)
306 (nth 1 gnus-command-method))))
308 (defun gnus-retrieve-groups (groups gnus-command-method)
309 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
310 (when (stringp gnus-command-method)
311 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
312 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
313 groups (nth 1 gnus-command-method)))
315 (defun gnus-request-type (group &optional article)
316 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
317 (let ((gnus-command-method (gnus-find-method-for-group group)))
318 (if (not (gnus-check-backend-function
319 'request-type (car gnus-command-method)))
321 (funcall (gnus-get-function gnus-command-method 'request-type)
322 (gnus-group-real-name group) article))))
324 (defun gnus-request-set-mark (group action)
325 "Set marks on articles in the backend."
326 (let ((gnus-command-method (gnus-find-method-for-group group)))
327 (if (not (gnus-check-backend-function
328 'request-set-mark (car gnus-command-method)))
330 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
331 (gnus-group-real-name group) action
332 (nth 1 gnus-command-method)))))
334 (defun gnus-request-update-mark (group article mark)
335 "Allow the backend to change the mark the user tries to put on an article."
336 (let ((gnus-command-method (gnus-find-method-for-group group)))
337 (if (not (gnus-check-backend-function
338 'request-update-mark (car gnus-command-method)))
340 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
341 (gnus-group-real-name group) article mark))))
343 (defun gnus-request-article (article group &optional buffer)
344 "Request the ARTICLE in GROUP.
345 ARTICLE can either be an article number or an article Message-ID.
346 If BUFFER, insert the article in that group."
347 (let ((gnus-command-method (gnus-find-method-for-group group)))
348 (funcall (gnus-get-function gnus-command-method 'request-article)
349 article (gnus-group-real-name group)
350 (nth 1 gnus-command-method) buffer)))
352 (defun gnus-request-head (article group)
353 "Request the head of ARTICLE in GROUP."
354 (let* ((gnus-command-method (gnus-find-method-for-group group))
355 (head (gnus-get-function gnus-command-method 'request-head t))
361 (gnus-cache-request-article article group))
362 (setq res (cons group article)
364 ;; Use `head' function.
366 (setq res (funcall head article (gnus-group-real-name group)
367 (nth 1 gnus-command-method))))
368 ;; Use `article' function.
370 (setq res (gnus-request-article article group)
374 (set-buffer nntp-server-buffer)
375 (goto-char (point-min))
376 (when (search-forward "\n\n" nil t)
377 (delete-region (1- (point)) (point-max)))
378 (nnheader-fold-continuation-lines)))
381 (defun gnus-request-body (article group)
382 "Request the body of ARTICLE in GROUP."
383 (let* ((gnus-command-method (gnus-find-method-for-group group))
384 (head (gnus-get-function gnus-command-method 'request-body t))
390 (gnus-cache-request-article article group))
391 (setq res (cons group article)
393 ;; Use `head' function.
395 (setq res (funcall head article (gnus-group-real-name group)
396 (nth 1 gnus-command-method))))
397 ;; Use `article' function.
399 (setq res (gnus-request-article article group)
403 (set-buffer nntp-server-buffer)
404 (goto-char (point-min))
405 (when (search-forward "\n\n" nil t)
406 (delete-region (point-min) (1- (point))))))
409 (defun gnus-request-post (gnus-command-method)
410 "Post the current buffer using GNUS-COMMAND-METHOD."
411 (when (stringp gnus-command-method)
412 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
413 (funcall (gnus-get-function gnus-command-method 'request-post)
414 (nth 1 gnus-command-method)))
416 (defun gnus-request-scan (group gnus-command-method)
417 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
418 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
419 (let ((gnus-command-method
420 (if group (gnus-find-method-for-group group) gnus-command-method))
421 (gnus-inhibit-demon t)
422 (mail-source-plugged gnus-plugged))
423 (if (or gnus-plugged (not (gnus-agent-method-p gnus-command-method)))
424 (funcall (gnus-get-function gnus-command-method 'request-scan)
425 (and group (gnus-group-real-name group))
426 (nth 1 gnus-command-method)))))
428 (defsubst gnus-request-update-info (info gnus-command-method)
429 "Request that GNUS-COMMAND-METHOD update INFO."
430 (when (stringp gnus-command-method)
431 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
432 (when (gnus-check-backend-function
433 'request-update-info (car gnus-command-method))
434 (funcall (gnus-get-function gnus-command-method 'request-update-info)
435 (gnus-group-real-name (gnus-info-group info))
436 info (nth 1 gnus-command-method))))
438 (defun gnus-request-expire-articles (articles group &optional force)
439 (let ((gnus-command-method (gnus-find-method-for-group group)))
440 (funcall (gnus-get-function gnus-command-method 'request-expire-articles)
441 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
444 (defun gnus-request-move-article
445 (article group server accept-function &optional last)
446 (let ((gnus-command-method (gnus-find-method-for-group group)))
447 (funcall (gnus-get-function gnus-command-method 'request-move-article)
448 article (gnus-group-real-name group)
449 (nth 1 gnus-command-method) accept-function last)))
451 (defun gnus-request-accept-article (group &optional gnus-command-method last
453 ;; Make sure there's a newline at the end of the article.
454 (when (stringp gnus-command-method)
455 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
456 (when (and (not gnus-command-method)
458 (setq gnus-command-method (gnus-group-name-to-method group)))
459 (goto-char (point-max))
463 (let ((message-options message-options))
464 (message-options-set-recipient)
466 (message-narrow-to-head)
467 (let ((mail-parse-charset message-default-charset))
468 (mail-encode-encoded-word-buffer)))
469 (message-encode-message-body)))
470 (let ((func (car (or gnus-command-method
471 (gnus-find-method-for-group group)))))
472 (funcall (intern (format "%s-request-accept-article" func))
473 (if (stringp group) (gnus-group-real-name group) group)
474 (cadr gnus-command-method)
477 (defun gnus-request-replace-article (article group buffer &optional no-encode)
479 (let ((message-options message-options))
480 (message-options-set-recipient)
482 (message-narrow-to-head)
483 (let ((mail-parse-charset message-default-charset))
484 (mail-encode-encoded-word-buffer)))
485 (message-encode-message-body)))
486 (let ((func (car (gnus-group-name-to-method group))))
487 (funcall (intern (format "%s-request-replace-article" func))
488 article (gnus-group-real-name group) buffer)))
490 (defun gnus-request-associate-buffer (group)
491 (let ((gnus-command-method (gnus-find-method-for-group group)))
492 (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
493 (gnus-group-real-name group))))
495 (defun gnus-request-restore-buffer (article group)
496 "Request a new buffer restored to the state of ARTICLE."
497 (let ((gnus-command-method (gnus-find-method-for-group group)))
498 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
499 article (gnus-group-real-name group)
500 (nth 1 gnus-command-method))))
502 (defun gnus-request-create-group (group &optional gnus-command-method args)
503 (when (stringp gnus-command-method)
504 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
505 (let ((gnus-command-method
506 (or gnus-command-method (gnus-find-method-for-group group))))
507 (funcall (gnus-get-function gnus-command-method 'request-create-group)
508 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
510 (defun gnus-request-delete-group (group &optional force)
511 (let ((gnus-command-method (gnus-find-method-for-group group)))
512 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
513 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
515 (defun gnus-request-rename-group (group new-name)
516 (let ((gnus-command-method (gnus-find-method-for-group group)))
517 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
518 (gnus-group-real-name group)
519 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
521 (defun gnus-close-backends ()
522 ;; Send a close request to all backends that support such a request.
523 (let ((methods gnus-valid-select-methods)
524 (gnus-inhibit-demon t)
525 func gnus-command-method)
526 (while (setq gnus-command-method (pop methods))
527 (when (fboundp (setq func (intern
528 (concat (car gnus-command-method)
532 (defun gnus-asynchronous-p (gnus-command-method)
533 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
537 (defun gnus-remove-denial (gnus-command-method)
538 (when (stringp gnus-command-method)
539 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
540 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
541 (status (cadr elem)))
542 ;; If this hasn't been opened before, we add it to the list.
543 (when (eq status 'denied)
544 ;; Set the status of this server.
545 (setcar (cdr elem) 'closed))))
549 ;;; gnus-int.el ends here