9f69536122a4d86eb75df325fbcbb238c45db7a4
[gnus] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend interface functions for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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 3 of the License, or
13 ;; (at your option) any later version.
14
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.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (require 'gnus)
30 (require 'message)
31 (require 'gnus-range)
32
33 (autoload 'gnus-run-hook-with-args "gnus-util")
34 (autoload 'gnus-agent-expire "gnus-agent")
35 (autoload 'gnus-agent-regenerate-group "gnus-agent")
36 (autoload 'gnus-agent-read-servers-validate-native "gnus-agent")
37 (autoload 'gnus-agent-possibly-synchronize-flags-server "gnus-agent")
38
39 (defcustom gnus-open-server-hook nil
40   "Hook called just before opening connection to the news server."
41   :group 'gnus-start
42   :type 'hook)
43
44 (defcustom gnus-after-set-mark-hook nil
45   "Hook called just after marks are set in a group."
46   :group 'gnus-start
47   :type 'hook)
48
49 (defcustom gnus-before-update-mark-hook nil
50   "Hook called just before marks are updated in a group."
51   :group 'gnus-start
52   :type 'hook)
53
54 (defcustom gnus-server-unopen-status nil
55   "The default status if the server is not able to open.
56 If the server is covered by Gnus agent, the possible values are
57 `denied', set the server denied; `offline', set the server offline;
58 nil, ask user.  If the server is not covered by Gnus agent, set the
59 server denied."
60   :version "22.1"
61   :group 'gnus-start
62   :type '(choice (const :tag "Ask" nil)
63                  (const :tag "Deny server" denied)
64                  (const :tag "Unplug Agent" offline)))
65
66 (defvar gnus-internal-registry-spool-current-method nil
67   "The current method, for the registry.")
68
69
70 (defun gnus-server-opened (gnus-command-method)
71   "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
72   (unless (eq (gnus-server-status gnus-command-method)
73               'denied)
74     (when (stringp gnus-command-method)
75       (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
76     (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
77              (nth 1 gnus-command-method))))
78
79 (defun gnus-status-message (gnus-command-method)
80   "Return the status message from GNUS-COMMAND-METHOD.
81 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
82 name.  The method this group uses will be queried."
83   (let ((gnus-command-method
84          (if (stringp gnus-command-method)
85              (gnus-find-method-for-group gnus-command-method)
86            gnus-command-method)))
87     (funcall (gnus-get-function gnus-command-method 'status-message)
88              (nth 1 gnus-command-method))))
89
90 ;;;
91 ;;; Server Communication
92 ;;;
93
94 (defun gnus-start-news-server (&optional confirm)
95   "Open a method for getting news.
96 If CONFIRM is non-nil, the user will be asked for an NNTP server."
97   (let (how)
98     (if gnus-current-select-method
99         ;; Stream is already opened.
100         nil
101       ;; Open NNTP server.
102       (when confirm
103         ;; Read server name with completion.
104         (setq gnus-nntp-server
105               (gnus-completing-read "NNTP server"
106                                     (cons gnus-nntp-server
107                                           gnus-secondary-servers)
108                                     nil gnus-nntp-server)))
109
110       (when (and gnus-nntp-server
111                  (stringp gnus-nntp-server)
112                  (not (string= gnus-nntp-server "")))
113         (setq gnus-select-method
114               (cond ((or (string= gnus-nntp-server "")
115                          (string= gnus-nntp-server "::"))
116                      (list 'nnspool (system-name)))
117                     ((string-match "^:" gnus-nntp-server)
118                      (list 'nnmh gnus-nntp-server
119                            (list 'nnmh-directory
120                                  (file-name-as-directory
121                                   (expand-file-name
122                                    (substring gnus-nntp-server 1) "~/")))
123                            (list 'nnmh-get-new-mail nil)))
124                     (t
125                      (list 'nntp gnus-nntp-server)))))
126
127       (setq how (car gnus-select-method))
128       (cond
129        ((eq how 'nnspool)
130         (require 'nnspool)
131         (gnus-message 5 "Looking up local news spool..."))
132        ((eq how 'nnmh)
133         (require 'nnmh)
134         (gnus-message 5 "Looking up mh spool..."))
135        (t
136         (require 'nntp)))
137       (setq gnus-current-select-method gnus-select-method)
138       (gnus-run-hooks 'gnus-open-server-hook)
139
140       ;; Partially validate agent covered methods now that the
141       ;; gnus-select-method is known.
142
143       (if gnus-agent
144           ;; NOTE: This is here for one purpose only.  By validating
145           ;; the current select method, it converts the old 5.10.3,
146           ;; and earlier, format to the current format.  That enables
147           ;; the agent code within gnus-open-server to function
148           ;; correctly.
149           (gnus-agent-read-servers-validate-native gnus-select-method))
150
151       (or
152        ;; gnus-open-server-hook might have opened it
153        (gnus-server-opened gnus-select-method)
154        (gnus-open-server gnus-select-method)
155        gnus-batch-mode
156        (gnus-y-or-n-p
157         (format
158          "%s (%s) open error: '%s'.  Continue? "
159          (car gnus-select-method) (cadr gnus-select-method)
160          (gnus-status-message gnus-select-method)))
161        (gnus-error 1 "Couldn't open server on %s"
162                    (nth 1 gnus-select-method))))))
163
164 (defun gnus-check-group (group)
165   "Try to make sure that the server where GROUP exists is alive."
166   (let ((method (gnus-find-method-for-group group)))
167     (or (gnus-server-opened method)
168         (gnus-open-server method))))
169
170 (defun gnus-check-server (&optional method silent)
171   "Check whether the connection to METHOD is down.
172 If METHOD is nil, use `gnus-select-method'.
173 If it is down, start it up (again)."
174   (let ((method (or method gnus-select-method))
175         result)
176     ;; Transform virtual server names into select methods.
177     (when (stringp method)
178       (setq method (gnus-server-to-method method)))
179     (if (gnus-server-opened method)
180         ;; The stream is already opened.
181         t
182       ;; Open the server.
183       (unless silent
184         (gnus-message 5 "Opening %s server%s..." (car method)
185                       (if (equal (nth 1 method) "") ""
186                         (format " on %s" (nth 1 method)))))
187       (gnus-run-hooks 'gnus-open-server-hook)
188       (prog1
189           (setq result (gnus-open-server method))
190         (unless silent
191           (gnus-message
192            (if result 5 3)
193            "Opening %s server%s...%s" (car method)
194            (if (equal (nth 1 method) "") ""
195              (format " on %s" (nth 1 method)))
196            (if result
197                "done"
198              (format "failed: %s"
199                      (nnheader-get-report-string (car method))))))))))
200
201 (defun gnus-get-function (method function &optional noerror)
202   "Return a function symbol based on METHOD and FUNCTION."
203   ;; Translate server names into methods.
204   (unless method
205     (error "Attempted use of a nil select method"))
206   (when (stringp method)
207     (setq method (gnus-server-to-method method)))
208   ;; Check cache of constructed names.
209   (let* ((method-sym (if gnus-agent
210                          (inline (gnus-agent-get-function method))
211                        (car method)))
212          (method-fns (get method-sym 'gnus-method-functions))
213          (func (let ((method-fnlist-elt (assq function method-fns)))
214                  (unless method-fnlist-elt
215                    (setq method-fnlist-elt
216                          (cons function
217                                (intern (format "%s-%s" method-sym function))))
218                    (put method-sym 'gnus-method-functions
219                         (cons method-fnlist-elt method-fns)))
220                  (cdr method-fnlist-elt))))
221     ;; Maybe complain if there is no function.
222     (unless (fboundp func)
223       (unless (car method)
224         (error "Trying to require a method that doesn't exist"))
225       (require (car method))
226       (when (not (fboundp func))
227         (if noerror
228             (setq func nil)
229           (error "No such function: %s" func))))
230     func))
231
232 \f
233 ;;;
234 ;;; Interface functions to the backends.
235 ;;;
236
237 (defun gnus-method-denied-p (method)
238   (eq (nth 1 (assoc method gnus-opened-servers))
239       'denied))
240
241 (defvar gnus-backend-trace t)
242
243 (defun gnus-open-server (gnus-command-method)
244   "Open a connection to GNUS-COMMAND-METHOD."
245   (when (stringp gnus-command-method)
246     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
247   (when gnus-backend-trace
248     (with-current-buffer (get-buffer-create "*gnus trace*")
249       (buffer-disable-undo)
250       (goto-char (point-max))
251       (insert (format-time-string "%H:%M:%S")
252               (format " %S\n" gnus-command-method))))
253   (let ((elem (assoc gnus-command-method gnus-opened-servers))
254         (server (gnus-method-to-server-name gnus-command-method)))
255     ;; If this method was previously denied, we just return nil.
256     (if (eq (nth 1 elem) 'denied)
257         (progn
258           (gnus-message 1 "Denied server %s" server)
259           nil)
260       ;; Open the server.
261       (let* ((open-server-function
262               (gnus-get-function gnus-command-method 'open-server))
263              (result
264               (condition-case err
265                   (funcall open-server-function
266                            (nth 1 gnus-command-method)
267                            (nthcdr 2 gnus-command-method))
268                 (error
269                  (gnus-message 1 "Unable to open server %s due to: %s"
270                                server (error-message-string err))
271                  nil)
272                 (quit
273                  (gnus-message 1 "Quit trying to open server %s" server)
274                  nil)))
275              open-offline)
276         ;; If this hasn't been opened before, we add it to the list.
277         (unless elem
278           (setq elem (list gnus-command-method nil)
279                 gnus-opened-servers (cons elem gnus-opened-servers)))
280         ;; Set the status of this server.
281         (setcar
282          (cdr elem)
283          (cond (result
284                 (if (eq open-server-function #'nnagent-open-server)
285                     ;; The agent's backend has a "special" status
286                     'offline
287                   'ok))
288                ((and gnus-agent
289                      (gnus-agent-method-p gnus-command-method))
290                 (cond
291                  (gnus-server-unopen-status
292                   ;; Set the server's status to the unopen
293                   ;; status.  If that status is offline,
294                   ;; recurse to open the agent's backend.
295                   (setq open-offline (eq gnus-server-unopen-status 'offline))
296                   gnus-server-unopen-status)
297                  ((not gnus-batch-mode)
298                   (setq open-offline t)
299                   'offline)
300                  (t
301                   ;; This agentized server was still denied
302                   'denied)))
303                (t
304                 ;; This unagentized server must be denied
305                 'denied)))
306
307         ;; NOTE: I MUST set the server's status to offline before this
308         ;; recursive call as this status will drive the
309         ;; gnus-get-function (called above) to return the agent's
310         ;; backend.
311         (if open-offline
312             ;; Recursively open this offline server to perform the
313             ;; open-server function of the agent's backend.
314             (let ((gnus-server-unopen-status 'denied))
315               ;; Bind gnus-server-unopen-status to avoid recursively
316               ;; prompting with "go offline?".  This is only a concern
317               ;; when the agent's backend fails to open the server.
318               (gnus-open-server gnus-command-method))
319           (when (and (eq (cadr elem) 'ok) gnus-agent
320                      (gnus-agent-method-p gnus-command-method))
321             (save-excursion
322               (gnus-agent-possibly-synchronize-flags-server
323                gnus-command-method)))
324           result)))))
325
326 (defun gnus-close-server (gnus-command-method)
327   "Close the connection to GNUS-COMMAND-METHOD."
328   (when (stringp gnus-command-method)
329     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
330   (funcall (gnus-get-function gnus-command-method 'close-server)
331            (nth 1 gnus-command-method)))
332
333 (defun gnus-request-list (gnus-command-method)
334   "Request the active file from GNUS-COMMAND-METHOD."
335   (when (stringp gnus-command-method)
336     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
337   (funcall (gnus-get-function gnus-command-method 'request-list)
338            (nth 1 gnus-command-method)))
339
340 (defun gnus-finish-retrieve-group-infos (gnus-command-method infos data)
341   "Read and update infos from GNUS-COMMAND-METHOD."
342   (when (stringp gnus-command-method)
343     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
344   (funcall (gnus-get-function gnus-command-method 'finish-retrieve-group-infos)
345            (nth 1 gnus-command-method)
346            infos data))
347
348 (defun gnus-retrieve-group-data-early (gnus-command-method infos)
349   "Start early async retrival of data from GNUS-COMMAND-METHOD."
350   (when (stringp gnus-command-method)
351     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
352   (funcall (gnus-get-function gnus-command-method 'retrieve-group-data-early)
353            (nth 1 gnus-command-method)
354            infos))
355
356 (defun gnus-request-list-newsgroups (gnus-command-method)
357   "Request the newsgroups file from GNUS-COMMAND-METHOD."
358   (when (stringp gnus-command-method)
359     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
360   (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
361            (nth 1 gnus-command-method)))
362
363 (defun gnus-request-newgroups (date gnus-command-method)
364   "Request all new groups since DATE from GNUS-COMMAND-METHOD."
365   (when (stringp gnus-command-method)
366     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
367   (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
368     (when func
369       (funcall func date (nth 1 gnus-command-method)))))
370
371 (defun gnus-request-regenerate (gnus-command-method)
372   "Request a data generation from GNUS-COMMAND-METHOD."
373   (when (stringp gnus-command-method)
374     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
375   (funcall (gnus-get-function gnus-command-method 'request-regenerate)
376            (nth 1 gnus-command-method)))
377
378 (defun gnus-request-compact-group (group)
379   (let* ((method (gnus-find-method-for-group group))
380          (gnus-command-method method)
381          (result
382           (funcall (gnus-get-function gnus-command-method
383                                       'request-compact-group)
384                    (gnus-group-real-name group)
385                    (nth 1 gnus-command-method) t)))
386     result))
387
388 (defun gnus-request-compact (gnus-command-method)
389   "Request groups compaction  from GNUS-COMMAND-METHOD."
390   (when (stringp gnus-command-method)
391     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
392   (funcall (gnus-get-function gnus-command-method 'request-compact)
393            (nth 1 gnus-command-method)))
394
395 (defun gnus-request-group (group &optional dont-check gnus-command-method info)
396   "Request GROUP.  If DONT-CHECK, no information is required."
397   (let ((gnus-command-method
398          (or gnus-command-method (inline (gnus-find-method-for-group group)))))
399     (when (stringp gnus-command-method)
400       (setq gnus-command-method
401             (inline (gnus-server-to-method gnus-command-method))))
402     (funcall (inline (gnus-get-function gnus-command-method 'request-group))
403              (gnus-group-real-name group) (nth 1 gnus-command-method)
404              dont-check
405              info)))
406
407 (defun gnus-list-active-group (group)
408   "Request active information on GROUP."
409   (let ((gnus-command-method (gnus-find-method-for-group group))
410         (func 'list-active-group))
411     (when (gnus-check-backend-function func group)
412       (funcall (gnus-get-function gnus-command-method func)
413                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
414
415 (defun gnus-request-group-description (group)
416   "Request a description of GROUP."
417   (let ((gnus-command-method (gnus-find-method-for-group group))
418         (func 'request-group-description))
419     (when (gnus-check-backend-function func group)
420       (funcall (gnus-get-function gnus-command-method func)
421                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
422
423 (defun gnus-request-group-articles (group)
424   "Request a list of existing articles in GROUP."
425   (let ((gnus-command-method (gnus-find-method-for-group group))
426         (func 'request-group-articles))
427     (when (gnus-check-backend-function func group)
428       (funcall (gnus-get-function gnus-command-method func)
429                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
430
431 (defun gnus-close-group (group)
432   "Request the GROUP be closed."
433   (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
434     (funcall (gnus-get-function gnus-command-method 'close-group)
435              (gnus-group-real-name group) (nth 1 gnus-command-method))))
436
437 (defun gnus-retrieve-headers (articles group &optional fetch-old)
438   "Request headers for ARTICLES in GROUP.
439 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
440   (let ((gnus-command-method (gnus-find-method-for-group group)))
441     (cond
442      ((and gnus-use-cache (numberp (car articles)))
443       (gnus-cache-retrieve-headers articles group fetch-old))
444      ((and gnus-agent (gnus-online gnus-command-method)
445            (gnus-agent-method-p gnus-command-method))
446       (gnus-agent-retrieve-headers articles group fetch-old))
447      (t
448       (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
449                articles (gnus-group-real-name group)
450                (nth 1 gnus-command-method) fetch-old)))))
451
452 (defun gnus-retrieve-articles (articles group)
453   "Request ARTICLES in GROUP."
454   (let ((gnus-command-method (gnus-find-method-for-group group)))
455     (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
456              articles (gnus-group-real-name group)
457              (nth 1 gnus-command-method))))
458
459 (defun gnus-retrieve-groups (groups gnus-command-method)
460   "Request active information on GROUPS from GNUS-COMMAND-METHOD."
461   (when (stringp gnus-command-method)
462     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
463   (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
464            groups (nth 1 gnus-command-method)))
465
466 (defun gnus-request-type (group &optional article)
467   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
468   (let ((gnus-command-method (gnus-find-method-for-group group)))
469     (if (not (gnus-check-backend-function
470               'request-type (car gnus-command-method)))
471         'unknown
472       (funcall (gnus-get-function gnus-command-method 'request-type)
473                (gnus-group-real-name group) article))))
474
475 (defun gnus-request-update-group-status (group status)
476   "Change the status of a group.
477 Valid statuses include `subscribe' and `unsubscribe'."
478   (let ((gnus-command-method (gnus-find-method-for-group group)))
479     (if (not (gnus-check-backend-function
480               'request-update-group-status (car gnus-command-method)))
481         nil
482       (funcall
483        (gnus-get-function gnus-command-method 'request-update-group-status)
484        (gnus-group-real-name group) status
485        (nth 1 gnus-command-method)))))
486
487 (defun gnus-request-set-mark (group action)
488   "Set marks on articles in the back end."
489   (let ((gnus-command-method (gnus-find-method-for-group group)))
490     (if (not (gnus-check-backend-function
491               'request-set-mark (car gnus-command-method)))
492         action
493       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
494                (gnus-group-real-name group) action
495                (nth 1 gnus-command-method))
496       (gnus-run-hook-with-args gnus-after-set-mark-hook group action))))
497
498 (defun gnus-request-update-mark (group article mark)
499   "Allow the back end to change the mark the user tries to put on an article."
500   (let ((gnus-command-method (gnus-find-method-for-group group)))
501     (if (not (gnus-check-backend-function
502               'request-update-mark (car gnus-command-method)))
503         mark
504       (gnus-run-hook-with-args gnus-before-update-mark-hook group article mark)
505       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
506                (gnus-group-real-name group) article mark))))
507
508 (defun gnus-request-article (article group &optional buffer)
509   "Request the ARTICLE in GROUP.
510 ARTICLE can either be an article number or an article Message-ID.
511 If BUFFER, insert the article in that group."
512   (let ((gnus-command-method (gnus-find-method-for-group group)))
513     (funcall (gnus-get-function gnus-command-method 'request-article)
514              article (gnus-group-real-name group)
515              (nth 1 gnus-command-method) buffer)))
516
517 (defun gnus-request-thread (header)
518   "Request the headers in the thread containing the article specified by HEADER."
519   (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
520     (funcall (gnus-get-function gnus-command-method 'request-thread)
521              header)))
522
523 (defun gnus-warp-to-article ()
524   "Warps from an article in a virtual group to the article in its
525 real group. Does nothing on a real group."
526   (interactive)
527   (let ((gnus-command-method
528          (gnus-find-method-for-group gnus-newsgroup-name)))
529     (when (gnus-check-backend-function
530            'warp-to-article (car gnus-command-method))
531       (funcall (gnus-get-function gnus-command-method 'warp-to-article)))))
532
533 (defun gnus-request-head (article group)
534   "Request the head of ARTICLE in GROUP."
535   (let* ((gnus-command-method (gnus-find-method-for-group group))
536          (head (gnus-get-function gnus-command-method 'request-head t))
537          res clean-up)
538     (cond
539      ;; Check the cache.
540      ((and gnus-use-cache
541            (numberp article)
542            (gnus-cache-request-article article group))
543       (setq res (cons group article)
544             clean-up t))
545      ;; Check the agent cache.
546      ((gnus-agent-request-article article group)
547       (setq res (cons group article)
548             clean-up t))
549      ;; Use `head' function.
550      ((fboundp head)
551       (setq res (funcall head article (gnus-group-real-name group)
552                          (nth 1 gnus-command-method))))
553      ;; Use `article' function.
554      (t
555       (setq res (gnus-request-article article group)
556             clean-up t)))
557     (when clean-up
558       (with-current-buffer nntp-server-buffer
559         (goto-char (point-min))
560         (when (search-forward "\n\n" nil t)
561           (delete-region (1- (point)) (point-max)))
562         (nnheader-fold-continuation-lines)))
563     res))
564
565 (defun gnus-request-body (article group)
566   "Request the body of ARTICLE in GROUP."
567   (let* ((gnus-command-method (gnus-find-method-for-group group))
568          (head (gnus-get-function gnus-command-method 'request-body t))
569          res clean-up)
570     (cond
571      ;; Check the cache.
572      ((and gnus-use-cache
573            (numberp article)
574            (gnus-cache-request-article article group))
575       (setq res (cons group article)
576             clean-up t))
577      ;; Check the agent cache.
578      ((gnus-agent-request-article article group)
579       (setq res (cons group article)
580             clean-up t))
581      ;; Use `head' function.
582      ((fboundp head)
583       (setq res (funcall head article (gnus-group-real-name group)
584                          (nth 1 gnus-command-method))))
585      ;; Use `article' function.
586      (t
587       (setq res (gnus-request-article article group)
588             clean-up t)))
589     (when clean-up
590       (with-current-buffer nntp-server-buffer
591         (goto-char (point-min))
592         (when (search-forward "\n\n" nil t)
593           (delete-region (point-min) (1- (point))))))
594     res))
595
596 (defun gnus-request-post (gnus-command-method)
597   "Post the current buffer using GNUS-COMMAND-METHOD."
598   (when (stringp gnus-command-method)
599     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
600   (funcall (gnus-get-function gnus-command-method 'request-post)
601            (nth 1 gnus-command-method)))
602
603 (defun gnus-request-expunge-group (group gnus-command-method)
604   "Expunge GROUP, which is removing articles that have been marked as deleted."
605   (when (stringp gnus-command-method)
606     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
607   (funcall (gnus-get-function gnus-command-method 'request-expunge-group)
608            (gnus-group-real-name group)
609            (nth 1 gnus-command-method)))
610
611 (defun gnus-request-scan (group gnus-command-method)
612   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
613 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
614   (let ((gnus-command-method
615          (if group (gnus-find-method-for-group group) gnus-command-method))
616         (gnus-inhibit-demon t)
617         (mail-source-plugged gnus-plugged))
618     (when (or gnus-plugged
619               (not (gnus-agent-method-p gnus-command-method)))
620       (setq gnus-internal-registry-spool-current-method gnus-command-method)
621       (funcall (gnus-get-function gnus-command-method 'request-scan)
622                (and group (gnus-group-real-name group))
623                (nth 1 gnus-command-method)))))
624
625 (defun gnus-request-update-info (info gnus-command-method)
626   (when (gnus-check-backend-function
627          'request-update-info (car gnus-command-method))
628     (when (stringp gnus-command-method)
629       (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
630     (funcall (gnus-get-function gnus-command-method 'request-update-info)
631              (gnus-group-real-name (gnus-info-group info)) info
632              (nth 1 gnus-command-method))))
633
634 (defsubst gnus-request-marks (info gnus-command-method)
635   "Request that GNUS-COMMAND-METHOD update INFO."
636   (when (stringp gnus-command-method)
637     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
638   (when (gnus-check-backend-function
639          'request-marks (car gnus-command-method))
640     (let ((group (gnus-info-group info)))
641       (and (funcall (gnus-get-function gnus-command-method
642                                        'request-update-info)
643                     (gnus-group-real-name group)
644                     info (nth 1 gnus-command-method))
645            ;; If the minimum article number is greater than 1, then all
646            ;; smaller article numbers are known not to exist; we'll
647            ;; artificially add those to the 'read range.
648            (let* ((active (gnus-active group))
649                   (min (car active)))
650              (when (> min 1)
651                (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
652                       (read (gnus-info-read info))
653                       (new-read (gnus-range-add read (list range))))
654                  (gnus-info-set-read info new-read)))
655              info)))))
656
657 (defun gnus-request-expire-articles (articles group &optional force)
658   (let* ((gnus-command-method (gnus-find-method-for-group group))
659          (gnus-inhibit-demon t)
660          (not-deleted
661           (funcall
662            (gnus-get-function gnus-command-method 'request-expire-articles)
663            articles (gnus-group-real-name group) (nth 1 gnus-command-method)
664            force)))
665     (when (and gnus-agent
666                (gnus-agent-method-p gnus-command-method))
667       (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
668         (when expired-articles
669           (gnus-agent-expire expired-articles group 'force))))
670     not-deleted))
671
672 (defun gnus-request-move-article (article group server accept-function
673                                           &optional last move-is-internal)
674   (let* ((gnus-command-method (gnus-find-method-for-group group))
675          (result (funcall (gnus-get-function gnus-command-method
676                                              'request-move-article)
677                           article (gnus-group-real-name group)
678                           (nth 1 gnus-command-method) accept-function
679                           last move-is-internal)))
680     (when (and result gnus-agent
681                (gnus-agent-method-p gnus-command-method))
682       (gnus-agent-unfetch-articles group (list article)))
683     result))
684
685 (defun gnus-request-accept-article (group &optional gnus-command-method last
686                                           no-encode)
687   ;; Make sure there's a newline at the end of the article.
688   (when (stringp gnus-command-method)
689     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
690   (when (and (not gnus-command-method)
691              (stringp group))
692     (setq gnus-command-method (or (gnus-find-method-for-group group)
693                                   (gnus-group-name-to-method group))))
694   (goto-char (point-max))
695   (unless (bolp)
696     (insert "\n"))
697   (unless no-encode
698     (let ((message-options message-options))
699       (message-options-set-recipient)
700       (save-restriction
701         (message-narrow-to-head)
702         (let ((mail-parse-charset message-default-charset))
703           (mail-encode-encoded-word-buffer)))
704       (message-encode-message-body)))
705   (let ((gnus-command-method (or gnus-command-method
706                                  (gnus-find-method-for-group group)))
707         (result
708          (funcall
709           (gnus-get-function gnus-command-method 'request-accept-article)
710           (if (stringp group) (gnus-group-real-name group) group)
711           (cadr gnus-command-method)
712           last)))
713     (when (and gnus-agent
714                (gnus-agent-method-p gnus-command-method)
715                (cdr result))
716       (gnus-agent-regenerate-group group (list (cdr result))))
717     result))
718
719 (defun gnus-request-replace-article (article group buffer &optional no-encode)
720   (unless no-encode
721     (let ((message-options message-options))
722       (message-options-set-recipient)
723       (save-restriction
724         (message-narrow-to-head)
725         (let ((mail-parse-charset message-default-charset))
726           (mail-encode-encoded-word-buffer)))
727       (message-encode-message-body)))
728   (let* ((func (car (gnus-group-name-to-method group)))
729          (result (funcall (intern (format "%s-request-replace-article" func))
730                           article (gnus-group-real-name group) buffer)))
731     (when (and gnus-agent (gnus-agent-method-p gnus-command-method))
732       (gnus-agent-regenerate-group group (list article)))
733     result))
734
735 (defun gnus-request-associate-buffer (group)
736   (let ((gnus-command-method (gnus-find-method-for-group group)))
737     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
738              (gnus-group-real-name group))))
739
740 (defun gnus-request-restore-buffer (article group)
741   "Request a new buffer restored to the state of ARTICLE."
742   (let ((gnus-command-method (gnus-find-method-for-group group)))
743     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
744              article (gnus-group-real-name group)
745              (nth 1 gnus-command-method))))
746
747 (defun gnus-request-create-group (group &optional gnus-command-method args)
748   (when (stringp gnus-command-method)
749     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
750   (let ((gnus-command-method
751          (or gnus-command-method (gnus-find-method-for-group group))))
752     (funcall (gnus-get-function gnus-command-method 'request-create-group)
753              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
754
755 (defun gnus-request-delete-group (group &optional force)
756   (let* ((gnus-command-method (gnus-find-method-for-group group))
757          (result
758           (funcall (gnus-get-function gnus-command-method 'request-delete-group)
759                    (gnus-group-real-name group) force (nth 1 gnus-command-method))))
760     (when result
761       (gnus-cache-delete-group group)
762       (gnus-agent-delete-group group))
763     result))
764
765 (defun gnus-request-rename-group (group new-name)
766   (let* ((gnus-command-method (gnus-find-method-for-group group))
767          (result
768           (funcall (gnus-get-function gnus-command-method 'request-rename-group)
769                    (gnus-group-real-name group)
770                    (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
771     (when result
772       (gnus-cache-rename-group group new-name)
773       (gnus-agent-rename-group group new-name))
774     result))
775
776 (defun gnus-close-backends ()
777   ;; Send a close request to all backends that support such a request.
778   (let ((methods gnus-valid-select-methods)
779         (gnus-inhibit-demon t)
780         func gnus-command-method)
781     (while (setq gnus-command-method (pop methods))
782       (when (fboundp (setq func (intern
783                                  (concat (car gnus-command-method)
784                                          "-request-close"))))
785         (funcall func)))))
786
787 (defun gnus-asynchronous-p (gnus-command-method)
788   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
789     (when (fboundp func)
790       (funcall func))))
791
792 (defun gnus-remove-denial (gnus-command-method)
793   (when (stringp gnus-command-method)
794     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
795   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
796          (status (cadr elem)))
797     ;; If this hasn't been opened before, we add it to the list.
798     (when (eq status 'denied)
799       ;; Set the status of this server.
800       (setcar (cdr elem) 'closed))))
801
802 (provide 'gnus-int)
803
804 ;;; gnus-int.el ends here