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