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