More improvements to thread-referral.
[gnus] / lisp / gnus-int.el
1 ;;; gnus-int.el --- backend interface functions for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 ;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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 3 of the License, or
14 ;; (at your option) any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'message)
32 (require 'gnus-range)
33
34 (autoload 'gnus-run-hook-with-args "gnus-util")
35 (autoload 'gnus-agent-expire "gnus-agent")
36 (autoload 'gnus-agent-regenerate-group "gnus-agent")
37 (autoload 'gnus-agent-read-servers-validate-native "gnus-agent")
38 (autoload 'gnus-agent-possibly-synchronize-flags-server "gnus-agent")
39
40 (defcustom gnus-open-server-hook nil
41   "Hook called just before opening connection to the news server."
42   :group 'gnus-start
43   :type 'hook)
44
45 (defcustom gnus-after-set-mark-hook nil
46   "Hook called just after marks are set in a group."
47   :group 'gnus-start
48   :type 'hook)
49
50 (defcustom gnus-before-update-mark-hook nil
51   "Hook called just before marks are updated in a group."
52   :group 'gnus-start
53   :type 'hook)
54
55 (defcustom gnus-server-unopen-status nil
56   "The default status if the server is not able to open.
57 If the server is covered by Gnus agent, the possible values are
58 `denied', set the server denied; `offline', set the server offline;
59 nil, ask user.  If the server is not covered by Gnus agent, set the
60 server denied."
61   :version "22.1"
62   :group 'gnus-start
63   :type '(choice (const :tag "Ask" nil)
64                  (const :tag "Deny server" denied)
65                  (const :tag "Unplug Agent" offline)))
66
67 (defvar gnus-internal-registry-spool-current-method nil
68   "The current method, for the registry.")
69
70
71 (defun gnus-server-opened (gnus-command-method)
72   "Check whether a connection to GNUS-COMMAND-METHOD has been opened."
73   (unless (eq (gnus-server-status gnus-command-method)
74               'denied)
75     (when (stringp gnus-command-method)
76       (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
77     (funcall (inline (gnus-get-function gnus-command-method 'server-opened))
78              (nth 1 gnus-command-method))))
79
80 (defun gnus-status-message (gnus-command-method)
81   "Return the status message from GNUS-COMMAND-METHOD.
82 If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
83 name.  The method this group uses will be queried."
84   (let ((gnus-command-method
85          (if (stringp gnus-command-method)
86              (gnus-find-method-for-group gnus-command-method)
87            gnus-command-method)))
88     (funcall (gnus-get-function gnus-command-method 'status-message)
89              (nth 1 gnus-command-method))))
90
91 ;;;
92 ;;; Server Communication
93 ;;;
94
95 (defun gnus-start-news-server (&optional confirm)
96   "Open a method for getting news.
97 If CONFIRM is non-nil, the user will be asked for an NNTP server."
98   (let (how)
99     (if gnus-current-select-method
100         ;; Stream is already opened.
101         nil
102       ;; Open NNTP server.
103       (when confirm
104         ;; Read server name with completion.
105         (setq gnus-nntp-server
106               (gnus-completing-read "NNTP server"
107                                     (cons gnus-nntp-server
108                                           gnus-secondary-servers)
109                                     nil gnus-nntp-server)))
110
111       (when (and gnus-nntp-server
112                  (stringp gnus-nntp-server)
113                  (not (string= gnus-nntp-server "")))
114         (setq gnus-select-method
115               (cond ((or (string= gnus-nntp-server "")
116                          (string= gnus-nntp-server "::"))
117                      (list 'nnspool (system-name)))
118                     ((string-match "^:" gnus-nntp-server)
119                      (list 'nnmh gnus-nntp-server
120                            (list 'nnmh-directory
121                                  (file-name-as-directory
122                                   (expand-file-name
123                                    (substring gnus-nntp-server 1) "~/")))
124                            (list 'nnmh-get-new-mail nil)))
125                     (t
126                      (list 'nntp gnus-nntp-server)))))
127
128       (setq how (car gnus-select-method))
129       (cond
130        ((eq how 'nnspool)
131         (require 'nnspool)
132         (gnus-message 5 "Looking up local news spool..."))
133        ((eq how 'nnmh)
134         (require 'nnmh)
135         (gnus-message 5 "Looking up mh spool..."))
136        (t
137         (require 'nntp)))
138       (setq gnus-current-select-method gnus-select-method)
139       (gnus-run-hooks 'gnus-open-server-hook)
140
141       ;; Partially validate agent covered methods now that the
142       ;; gnus-select-method is known.
143
144       (if gnus-agent
145           ;; NOTE: This is here for one purpose only.  By validating
146           ;; the current select method, it converts the old 5.10.3,
147           ;; and earlier, format to the current format.  That enables
148           ;; the agent code within gnus-open-server to function
149           ;; correctly.
150           (gnus-agent-read-servers-validate-native gnus-select-method))
151
152       (or
153        ;; gnus-open-server-hook might have opened it
154        (gnus-server-opened gnus-select-method)
155        (gnus-open-server gnus-select-method)
156        gnus-batch-mode
157        (gnus-y-or-n-p
158         (format
159          "%s (%s) open error: '%s'.  Continue? "
160          (car gnus-select-method) (cadr gnus-select-method)
161          (gnus-status-message gnus-select-method)))
162        (gnus-error 1 "Couldn't open server on %s"
163                    (nth 1 gnus-select-method))))))
164
165 (defun gnus-check-group (group)
166   "Try to make sure that the server where GROUP exists is alive."
167   (let ((method (gnus-find-method-for-group group)))
168     (or (gnus-server-opened method)
169         (gnus-open-server method))))
170
171 (defun gnus-check-server (&optional method silent)
172   "Check whether the connection to METHOD is down.
173 If METHOD is nil, use `gnus-select-method'.
174 If it is down, start it up (again)."
175   (let ((method (or method gnus-select-method))
176         result)
177     ;; Transform virtual server names into select methods.
178     (when (stringp method)
179       (setq method (gnus-server-to-method method)))
180     (if (gnus-server-opened method)
181         ;; The stream is already opened.
182         t
183       ;; Open the server.
184       (unless silent
185         (gnus-message 5 "Opening %s server%s..." (car method)
186                       (if (equal (nth 1 method) "") ""
187                         (format " on %s" (nth 1 method)))))
188       (gnus-run-hooks 'gnus-open-server-hook)
189       (prog1
190           (setq result (gnus-open-server method))
191         (unless silent
192           (gnus-message
193            (if result 5 3)
194            "Opening %s server%s...%s" (car method)
195            (if (equal (nth 1 method) "") ""
196              (format " on %s" (nth 1 method)))
197            (if result
198                "done"
199              (format "failed: %s"
200                      (nnheader-get-report-string (car method))))))))))
201
202 (defun gnus-get-function (method function &optional noerror)
203   "Return a function symbol based on METHOD and FUNCTION."
204   ;; Translate server names into methods.
205   (unless method
206     (error "Attempted use of a nil select method"))
207   (when (stringp method)
208     (setq method (gnus-server-to-method method)))
209   ;; Check cache of constructed names.
210   (let* ((method-sym (if gnus-agent
211                          (inline (gnus-agent-get-function method))
212                        (car method)))
213          (method-fns (get method-sym 'gnus-method-functions))
214          (func (let ((method-fnlist-elt (assq function method-fns)))
215                  (unless method-fnlist-elt
216                    (setq method-fnlist-elt
217                          (cons function
218                                (intern (format "%s-%s" method-sym function))))
219                    (put method-sym 'gnus-method-functions
220                         (cons method-fnlist-elt method-fns)))
221                  (cdr method-fnlist-elt))))
222     ;; Maybe complain if there is no function.
223     (unless (fboundp func)
224       (unless (car method)
225         (error "Trying to require a method that doesn't exist"))
226       (require (car method))
227       (when (not (fboundp func))
228         (if noerror
229             (setq func nil)
230           (error "No such function: %s" func))))
231     func))
232
233 \f
234 ;;;
235 ;;; Interface functions to the backends.
236 ;;;
237
238 (defun gnus-method-denied-p (method)
239   (eq (nth 1 (assoc method gnus-opened-servers))
240       'denied))
241
242 (defvar gnus-backend-trace t)
243
244 (defun gnus-open-server (gnus-command-method)
245   "Open a connection to GNUS-COMMAND-METHOD."
246   (when (stringp gnus-command-method)
247     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
248   (when gnus-backend-trace
249     (with-current-buffer (get-buffer-create "*gnus trace*")
250       (buffer-disable-undo)
251       (goto-char (point-max))
252       (insert (format-time-string "%H:%M:%S")
253               (format " %S\n" gnus-command-method))))
254   (let ((elem (assoc gnus-command-method gnus-opened-servers))
255         (server (gnus-method-to-server-name gnus-command-method)))
256     ;; If this method was previously denied, we just return nil.
257     (if (eq (nth 1 elem) 'denied)
258         (progn
259           (gnus-message 1 "Denied server %s" server)
260           nil)
261       ;; Open the server.
262       (let* ((open-server-function (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-set-mark (group action)
476   "Set marks on articles in the back end."
477   (let ((gnus-command-method (gnus-find-method-for-group group)))
478     (if (not (gnus-check-backend-function
479               'request-set-mark (car gnus-command-method)))
480         action
481       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
482                (gnus-group-real-name group) action
483                (nth 1 gnus-command-method))
484       (gnus-run-hook-with-args gnus-after-set-mark-hook group action))))
485
486 (defun gnus-request-update-mark (group article mark)
487   "Allow the back end to change the mark the user tries to put on an article."
488   (let ((gnus-command-method (gnus-find-method-for-group group)))
489     (if (not (gnus-check-backend-function
490               'request-update-mark (car gnus-command-method)))
491         mark
492       (gnus-run-hook-with-args gnus-before-update-mark-hook group article mark)
493       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
494                (gnus-group-real-name group) article mark))))
495
496 (defun gnus-request-article (article group &optional buffer)
497   "Request the ARTICLE in GROUP.
498 ARTICLE can either be an article number or an article Message-ID.
499 If BUFFER, insert the article in that group."
500   (let ((gnus-command-method (gnus-find-method-for-group group)))
501     (funcall (gnus-get-function gnus-command-method 'request-article)
502              article (gnus-group-real-name group)
503              (nth 1 gnus-command-method) buffer)))
504
505 (defun gnus-request-thread (id)
506   "Request the headers in the thread containing the article
507 specified by Message-ID id."
508   (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
509     (funcall (gnus-get-function gnus-command-method 'request-thread)
510              id)))
511
512 (defun gnus-warp-to-article ()
513   "Warps from an article in a virtual group to the article in its
514 real group. Does nothing on a real group."
515   (interactive)
516   (let ((gnus-command-method
517          (gnus-find-method-for-group gnus-newsgroup-name)))
518     (when (gnus-check-backend-function
519            'warp-to-article (car gnus-command-method))
520       (funcall (gnus-get-function gnus-command-method 'warp-to-article)))))
521
522 (defun gnus-request-head (article group)
523   "Request the head of ARTICLE in GROUP."
524   (let* ((gnus-command-method (gnus-find-method-for-group group))
525          (head (gnus-get-function gnus-command-method 'request-head t))
526          res clean-up)
527     (cond
528      ;; Check the cache.
529      ((and gnus-use-cache
530            (numberp article)
531            (gnus-cache-request-article article group))
532       (setq res (cons group article)
533             clean-up t))
534      ;; Check the agent cache.
535      ((gnus-agent-request-article article group)
536       (setq res (cons group article)
537             clean-up t))
538      ;; Use `head' function.
539      ((fboundp head)
540       (setq res (funcall head article (gnus-group-real-name group)
541                          (nth 1 gnus-command-method))))
542      ;; Use `article' function.
543      (t
544       (setq res (gnus-request-article article group)
545             clean-up t)))
546     (when clean-up
547       (with-current-buffer nntp-server-buffer
548         (goto-char (point-min))
549         (when (search-forward "\n\n" nil t)
550           (delete-region (1- (point)) (point-max)))
551         (nnheader-fold-continuation-lines)))
552     res))
553
554 (defun gnus-request-body (article group)
555   "Request the body of ARTICLE in GROUP."
556   (let* ((gnus-command-method (gnus-find-method-for-group group))
557          (head (gnus-get-function gnus-command-method 'request-body t))
558          res clean-up)
559     (cond
560      ;; Check the cache.
561      ((and gnus-use-cache
562            (numberp article)
563            (gnus-cache-request-article article group))
564       (setq res (cons group article)
565             clean-up t))
566      ;; Check the agent cache.
567      ((gnus-agent-request-article article group)
568       (setq res (cons group article)
569             clean-up t))
570      ;; Use `head' function.
571      ((fboundp head)
572       (setq res (funcall head article (gnus-group-real-name group)
573                          (nth 1 gnus-command-method))))
574      ;; Use `article' function.
575      (t
576       (setq res (gnus-request-article article group)
577             clean-up t)))
578     (when clean-up
579       (with-current-buffer nntp-server-buffer
580         (goto-char (point-min))
581         (when (search-forward "\n\n" nil t)
582           (delete-region (point-min) (1- (point))))))
583     res))
584
585 (defun gnus-request-post (gnus-command-method)
586   "Post the current buffer using GNUS-COMMAND-METHOD."
587   (when (stringp gnus-command-method)
588     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
589   (funcall (gnus-get-function gnus-command-method 'request-post)
590            (nth 1 gnus-command-method)))
591
592 (defun gnus-request-expunge-group (group gnus-command-method)
593   "Expunge GROUP, which is removing articles that have been marked as deleted."
594   (when (stringp gnus-command-method)
595     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
596   (funcall (gnus-get-function gnus-command-method 'request-expunge-group)
597            (gnus-group-real-name group)
598            (nth 1 gnus-command-method)))
599
600 (defun gnus-request-scan (group gnus-command-method)
601   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
602 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
603   (let ((gnus-command-method
604          (if group (gnus-find-method-for-group group) gnus-command-method))
605         (gnus-inhibit-demon t)
606         (mail-source-plugged gnus-plugged))
607     (when (or gnus-plugged
608               (not (gnus-agent-method-p gnus-command-method)))
609       (setq gnus-internal-registry-spool-current-method gnus-command-method)
610       (funcall (gnus-get-function gnus-command-method 'request-scan)
611                (and group (gnus-group-real-name group))
612                (nth 1 gnus-command-method)))))
613
614 (defun gnus-request-update-info (info gnus-command-method)
615   (when (gnus-check-backend-function
616          'request-update-info (car gnus-command-method))
617     (when (stringp gnus-command-method)
618       (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
619     (funcall (gnus-get-function gnus-command-method 'request-update-info)
620              (gnus-group-real-name (gnus-info-group info)) info
621              (nth 1 gnus-command-method))))
622
623 (defsubst gnus-request-marks (info gnus-command-method)
624   "Request that GNUS-COMMAND-METHOD update INFO."
625   (when (stringp gnus-command-method)
626     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
627   (when (gnus-check-backend-function
628          'request-marks (car gnus-command-method))
629     (let ((group (gnus-info-group info)))
630       (and (funcall (gnus-get-function gnus-command-method
631                                        'request-update-info)
632                     (gnus-group-real-name group)
633                     info (nth 1 gnus-command-method))
634            ;; If the minimum article number is greater than 1, then all
635            ;; smaller article numbers are known not to exist; we'll
636            ;; artificially add those to the 'read range.
637            (let* ((active (gnus-active group))
638                   (min (car active)))
639              (when (> min 1)
640                (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
641                       (read (gnus-info-read info))
642                       (new-read (gnus-range-add read (list range))))
643                  (gnus-info-set-read info new-read)))
644              info)))))
645
646 (defun gnus-request-expire-articles (articles group &optional force)
647   (let* ((gnus-command-method (gnus-find-method-for-group group))
648          (gnus-inhibit-demon t)
649          (not-deleted
650           (funcall
651            (gnus-get-function gnus-command-method 'request-expire-articles)
652            articles (gnus-group-real-name group) (nth 1 gnus-command-method)
653            force)))
654     (when (and gnus-agent
655                (gnus-agent-method-p gnus-command-method))
656       (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
657         (when expired-articles
658           (gnus-agent-expire expired-articles group 'force))))
659     not-deleted))
660
661 (defun gnus-request-move-article (article group server accept-function
662                                           &optional last move-is-internal)
663   (let* ((gnus-command-method (gnus-find-method-for-group group))
664          (result (funcall (gnus-get-function gnus-command-method
665                                              'request-move-article)
666                           article (gnus-group-real-name group)
667                           (nth 1 gnus-command-method) accept-function
668                           last move-is-internal)))
669     (when (and result gnus-agent
670                (gnus-agent-method-p gnus-command-method))
671       (gnus-agent-unfetch-articles group (list article)))
672     result))
673
674 (defun gnus-request-accept-article (group &optional gnus-command-method last
675                                           no-encode)
676   ;; Make sure there's a newline at the end of the article.
677   (when (stringp gnus-command-method)
678     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
679   (when (and (not gnus-command-method)
680              (stringp group))
681     (setq gnus-command-method (or (gnus-find-method-for-group group)
682                                   (gnus-group-name-to-method group))))
683   (goto-char (point-max))
684   (unless (bolp)
685     (insert "\n"))
686   (unless no-encode
687     (let ((message-options message-options))
688       (message-options-set-recipient)
689       (save-restriction
690         (message-narrow-to-head)
691         (let ((mail-parse-charset message-default-charset))
692           (mail-encode-encoded-word-buffer)))
693       (message-encode-message-body)))
694   (let ((gnus-command-method (or gnus-command-method
695                                  (gnus-find-method-for-group group)))
696         (result
697          (funcall
698           (gnus-get-function gnus-command-method 'request-accept-article)
699           (if (stringp group) (gnus-group-real-name group) group)
700           (cadr gnus-command-method)
701           last)))
702     (when (and gnus-agent (gnus-agent-method-p gnus-command-method))
703       (gnus-agent-regenerate-group group (list (cdr result))))
704     result))
705
706 (defun gnus-request-replace-article (article group buffer &optional no-encode)
707   (unless no-encode
708     (let ((message-options message-options))
709       (message-options-set-recipient)
710       (save-restriction
711         (message-narrow-to-head)
712         (let ((mail-parse-charset message-default-charset))
713           (mail-encode-encoded-word-buffer)))
714       (message-encode-message-body)))
715   (let* ((func (car (gnus-group-name-to-method group)))
716          (result (funcall (intern (format "%s-request-replace-article" func))
717                           article (gnus-group-real-name group) buffer)))
718     (when (and gnus-agent (gnus-agent-method-p gnus-command-method))
719       (gnus-agent-regenerate-group group (list article)))
720     result))
721
722 (defun gnus-request-associate-buffer (group)
723   (let ((gnus-command-method (gnus-find-method-for-group group)))
724     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
725              (gnus-group-real-name group))))
726
727 (defun gnus-request-restore-buffer (article group)
728   "Request a new buffer restored to the state of ARTICLE."
729   (let ((gnus-command-method (gnus-find-method-for-group group)))
730     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
731              article (gnus-group-real-name group)
732              (nth 1 gnus-command-method))))
733
734 (defun gnus-request-create-group (group &optional gnus-command-method args)
735   (when (stringp gnus-command-method)
736     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
737   (let ((gnus-command-method
738          (or gnus-command-method (gnus-find-method-for-group group))))
739     (funcall (gnus-get-function gnus-command-method 'request-create-group)
740              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
741
742 (defun gnus-request-delete-group (group &optional force)
743   (let* ((gnus-command-method (gnus-find-method-for-group group))
744          (result
745           (funcall (gnus-get-function gnus-command-method 'request-delete-group)
746                    (gnus-group-real-name group) force (nth 1 gnus-command-method))))
747     (when result
748       (gnus-cache-delete-group group)
749       (gnus-agent-delete-group group))
750     result))
751
752 (defun gnus-request-rename-group (group new-name)
753   (let* ((gnus-command-method (gnus-find-method-for-group group))
754          (result
755           (funcall (gnus-get-function gnus-command-method 'request-rename-group)
756                    (gnus-group-real-name group)
757                    (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
758     (when result
759       (gnus-cache-rename-group group new-name)
760       (gnus-agent-rename-group group new-name))
761     result))
762
763 (defun gnus-close-backends ()
764   ;; Send a close request to all backends that support such a request.
765   (let ((methods gnus-valid-select-methods)
766         (gnus-inhibit-demon t)
767         func gnus-command-method)
768     (while (setq gnus-command-method (pop methods))
769       (when (fboundp (setq func (intern
770                                  (concat (car gnus-command-method)
771                                          "-request-close"))))
772         (funcall func)))))
773
774 (defun gnus-asynchronous-p (gnus-command-method)
775   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
776     (when (fboundp func)
777       (funcall func))))
778
779 (defun gnus-remove-denial (gnus-command-method)
780   (when (stringp gnus-command-method)
781     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
782   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
783          (status (cadr elem)))
784     ;; If this hasn't been opened before, we add it to the list.
785     (when (eq status 'denied)
786       ;; Set the status of this server.
787       (setcar (cdr elem) 'closed))))
788
789 (provide 'gnus-int)
790
791 ;;; gnus-int.el ends here