* gnus-agent.el (gnus-agent-load-alist): Nix out the alist if the file
[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           (setq result (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-method-denied-p (method)
226   (eq (nth 1 (assoc method gnus-opened-servers))
227       'denied))
228
229 (defun gnus-open-server (gnus-command-method)
230   "Open a connection to GNUS-COMMAND-METHOD."
231   (when (stringp gnus-command-method)
232     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
233   (let ((elem (assoc gnus-command-method gnus-opened-servers))
234         (server (gnus-method-to-server-name gnus-command-method)))
235     ;; If this method was previously denied, we just return nil.
236     (if (eq (nth 1 elem) 'denied)
237         (progn
238           (gnus-message 1 "Denied server %s" server)
239           nil)
240       ;; Open the server.
241       (let* ((open-server-function (gnus-get-function gnus-command-method 'open-server))
242              (result
243              (condition-case err
244                  (funcall open-server-function
245                           (nth 1 gnus-command-method)
246                           (nthcdr 2 gnus-command-method))
247                (error
248                 (gnus-message 1 (format
249                                  "Unable to open server %s due to: %s"
250                                  server (error-message-string err)))
251                 nil)
252                (quit
253                 (gnus-message 1 "Quit trying to open server %s" server)
254                 nil)))
255             open-offline)
256         ;; If this hasn't been opened before, we add it to the list.
257         (unless elem
258           (setq elem (list gnus-command-method nil)
259                 gnus-opened-servers (cons elem gnus-opened-servers)))
260         ;; Set the status of this server.
261         (setcar (cdr elem)
262                 (cond (result
263                        (if (eq open-server-function #'nnagent-open-server)
264                            ;; The agent's backend has a "special" status
265                            'offline
266                          'ok))
267                       ((and gnus-agent
268                             (gnus-agent-method-p gnus-command-method))
269                        (cond (gnus-server-unopen-status
270                               ;; Set the server's status to the unopen
271                               ;; status.  If that status is offline,
272                               ;; recurse to open the agent's backend.
273                               (setq open-offline (eq gnus-server-unopen-status 'offline))
274                               gnus-server-unopen-status)
275                              ((and
276                                (not gnus-batch-mode)
277                                (gnus-y-or-n-p
278                                 (format
279                                  "Unable to open server %s, go offline? "
280                                  server)))
281                               (setq open-offline t)
282                               'offline)
283                              (t
284                               ;; This agentized server was still denied
285                               'denied)))
286                       (t
287                        ;; This unagentized server must be denied
288                        'denied)))
289
290         ;; NOTE: I MUST set the server's status to offline before this
291         ;; recursive call as this status will drive the
292         ;; gnus-get-function (called above) to return the agent's
293         ;; backend.
294         (if open-offline
295             ;; Recursively open this offline server to perform the
296             ;; open-server function of the agent's backend.
297             (let ((gnus-server-unopen-status 'denied))
298               ;; Bind gnus-server-unopen-status to avoid recursively
299               ;; prompting with "go offline?".  This is only a concern
300               ;; when the agent's backend fails to open the server.
301               (gnus-open-server gnus-command-method))
302           (when (and (eq (cadr elem) 'ok) gnus-agent
303                      (gnus-agent-method-p gnus-command-method))
304             (save-excursion
305               (gnus-agent-possibly-synchronize-flags-server
306                gnus-command-method)))
307           result)))))
308
309 (defun gnus-close-server (gnus-command-method)
310   "Close the connection to GNUS-COMMAND-METHOD."
311   (when (stringp gnus-command-method)
312     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
313   (funcall (gnus-get-function gnus-command-method 'close-server)
314            (nth 1 gnus-command-method)))
315
316 (defun gnus-request-list (gnus-command-method)
317   "Request the active file from GNUS-COMMAND-METHOD."
318   (when (stringp gnus-command-method)
319     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
320   (funcall (gnus-get-function gnus-command-method 'request-list)
321            (nth 1 gnus-command-method)))
322
323 (defun gnus-finish-retrieve-group-infos (gnus-command-method infos data)
324   "Read and update infos from GNUS-COMMAND-METHOD."
325   (when (stringp gnus-command-method)
326     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
327   (funcall (gnus-get-function gnus-command-method 'finish-retrieve-group-infos)
328            (nth 1 gnus-command-method)
329            infos data))
330
331 (defun gnus-retrieve-group-data-early (gnus-command-method infos)
332   "Start early async retrival of data from GNUS-COMMAND-METHOD."
333   (when (stringp gnus-command-method)
334     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
335   (funcall (gnus-get-function gnus-command-method 'retrieve-group-data-early)
336            (nth 1 gnus-command-method)
337            infos))
338
339 (defun gnus-request-list-newsgroups (gnus-command-method)
340   "Request the newsgroups file from GNUS-COMMAND-METHOD."
341   (when (stringp gnus-command-method)
342     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
343   (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
344            (nth 1 gnus-command-method)))
345
346 (defun gnus-request-newgroups (date gnus-command-method)
347   "Request all new groups since DATE from GNUS-COMMAND-METHOD."
348   (when (stringp gnus-command-method)
349     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
350   (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
351     (when func
352       (funcall func date (nth 1 gnus-command-method)))))
353
354 (defun gnus-request-regenerate (gnus-command-method)
355   "Request a data generation from GNUS-COMMAND-METHOD."
356   (when (stringp gnus-command-method)
357     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
358   (funcall (gnus-get-function gnus-command-method 'request-regenerate)
359            (nth 1 gnus-command-method)))
360
361 (defun gnus-request-compact-group (group)
362   (let* ((method (gnus-find-method-for-group group))
363          (gnus-command-method method)
364          (result
365           (funcall (gnus-get-function gnus-command-method
366                                       'request-compact-group)
367                    (gnus-group-real-name group)
368                    (nth 1 gnus-command-method) t)))
369     result))
370
371 (defun gnus-request-compact (gnus-command-method)
372   "Request groups compaction  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-compact)
376            (nth 1 gnus-command-method)))
377
378 (defun gnus-request-group (group &optional dont-check gnus-command-method info)
379   "Request GROUP.  If DONT-CHECK, no information is required."
380   (let ((gnus-command-method
381          (or gnus-command-method (inline (gnus-find-method-for-group group)))))
382     (when (stringp gnus-command-method)
383       (setq gnus-command-method
384             (inline (gnus-server-to-method gnus-command-method))))
385     (funcall (inline (gnus-get-function gnus-command-method 'request-group))
386              (gnus-group-real-name group) (nth 1 gnus-command-method)
387              dont-check
388              info)))
389
390 (defun gnus-list-active-group (group)
391   "Request active information on GROUP."
392   (let ((gnus-command-method (gnus-find-method-for-group group))
393         (func 'list-active-group))
394     (when (gnus-check-backend-function func group)
395       (funcall (gnus-get-function gnus-command-method func)
396                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
397
398 (defun gnus-request-group-description (group)
399   "Request a description of GROUP."
400   (let ((gnus-command-method (gnus-find-method-for-group group))
401         (func 'request-group-description))
402     (when (gnus-check-backend-function func group)
403       (funcall (gnus-get-function gnus-command-method func)
404                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
405
406 (defun gnus-request-group-articles (group)
407   "Request a list of existing articles in GROUP."
408   (let ((gnus-command-method (gnus-find-method-for-group group))
409         (func 'request-group-articles))
410     (when (gnus-check-backend-function func group)
411       (funcall (gnus-get-function gnus-command-method func)
412                (gnus-group-real-name group) (nth 1 gnus-command-method)))))
413
414 (defun gnus-close-group (group)
415   "Request the GROUP be closed."
416   (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
417     (funcall (gnus-get-function gnus-command-method 'close-group)
418              (gnus-group-real-name group) (nth 1 gnus-command-method))))
419
420 (defun gnus-retrieve-headers (articles group &optional fetch-old)
421   "Request headers for ARTICLES in GROUP.
422 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
423   (let ((gnus-command-method (gnus-find-method-for-group group)))
424     (cond
425      ((and gnus-use-cache (numberp (car articles)))
426       (gnus-cache-retrieve-headers articles group fetch-old))
427      ((and gnus-agent (gnus-online gnus-command-method)
428            (gnus-agent-method-p gnus-command-method))
429       (gnus-agent-retrieve-headers articles group fetch-old))
430      (t
431       (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
432                articles (gnus-group-real-name group)
433                (nth 1 gnus-command-method) fetch-old)))))
434
435 (defun gnus-retrieve-articles (articles group)
436   "Request ARTICLES in GROUP."
437   (let ((gnus-command-method (gnus-find-method-for-group group)))
438     (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
439              articles (gnus-group-real-name group)
440              (nth 1 gnus-command-method))))
441
442 (defun gnus-retrieve-groups (groups gnus-command-method)
443   "Request active information on GROUPS from GNUS-COMMAND-METHOD."
444   (when (stringp gnus-command-method)
445     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
446   (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
447            groups (nth 1 gnus-command-method)))
448
449 (defun gnus-request-type (group &optional article)
450   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
451   (let ((gnus-command-method (gnus-find-method-for-group group)))
452     (if (not (gnus-check-backend-function
453               'request-type (car gnus-command-method)))
454         'unknown
455       (funcall (gnus-get-function gnus-command-method 'request-type)
456                (gnus-group-real-name group) article))))
457
458 (defun gnus-request-set-mark (group action)
459   "Set marks on articles in the back end."
460   (let ((gnus-command-method (gnus-find-method-for-group group)))
461     (if (not (gnus-check-backend-function
462               'request-set-mark (car gnus-command-method)))
463         action
464       (funcall (gnus-get-function gnus-command-method 'request-set-mark)
465                (gnus-group-real-name group) action
466                (nth 1 gnus-command-method)))))
467
468 (defun gnus-request-update-mark (group article mark)
469   "Allow the back end to change the mark the user tries to put on an article."
470   (let ((gnus-command-method (gnus-find-method-for-group group)))
471     (if (not (gnus-check-backend-function
472               'request-update-mark (car gnus-command-method)))
473         mark
474       (funcall (gnus-get-function gnus-command-method 'request-update-mark)
475                (gnus-group-real-name group) article mark))))
476
477 (defun gnus-request-article (article group &optional buffer)
478   "Request the ARTICLE in GROUP.
479 ARTICLE can either be an article number or an article Message-ID.
480 If BUFFER, insert the article in that group."
481   (let ((gnus-command-method (gnus-find-method-for-group group)))
482     (funcall (gnus-get-function gnus-command-method 'request-article)
483              article (gnus-group-real-name group)
484              (nth 1 gnus-command-method) buffer)))
485
486 (defun gnus-request-head (article group)
487   "Request the head of ARTICLE in GROUP."
488   (let* ((gnus-command-method (gnus-find-method-for-group group))
489          (head (gnus-get-function gnus-command-method 'request-head t))
490          res clean-up)
491     (cond
492      ;; Check the cache.
493      ((and gnus-use-cache
494            (numberp article)
495            (gnus-cache-request-article article group))
496       (setq res (cons group article)
497             clean-up t))
498      ;; Check the agent cache.
499      ((gnus-agent-request-article article group)
500       (setq res (cons group article)
501             clean-up t))
502      ;; Use `head' function.
503      ((fboundp head)
504       (setq res (funcall head article (gnus-group-real-name group)
505                          (nth 1 gnus-command-method))))
506      ;; Use `article' function.
507      (t
508       (setq res (gnus-request-article article group)
509             clean-up t)))
510     (when clean-up
511       (with-current-buffer nntp-server-buffer
512         (goto-char (point-min))
513         (when (search-forward "\n\n" nil t)
514           (delete-region (1- (point)) (point-max)))
515         (nnheader-fold-continuation-lines)))
516     res))
517
518 (defun gnus-request-body (article group)
519   "Request the body of ARTICLE in GROUP."
520   (let* ((gnus-command-method (gnus-find-method-for-group group))
521          (head (gnus-get-function gnus-command-method 'request-body t))
522          res clean-up)
523     (cond
524      ;; Check the cache.
525      ((and gnus-use-cache
526            (numberp article)
527            (gnus-cache-request-article article group))
528       (setq res (cons group article)
529             clean-up t))
530      ;; Check the agent cache.
531      ((gnus-agent-request-article article group)
532       (setq res (cons group article)
533             clean-up t))
534      ;; Use `head' function.
535      ((fboundp head)
536       (setq res (funcall head article (gnus-group-real-name group)
537                          (nth 1 gnus-command-method))))
538      ;; Use `article' function.
539      (t
540       (setq res (gnus-request-article article group)
541             clean-up t)))
542     (when clean-up
543       (with-current-buffer nntp-server-buffer
544         (goto-char (point-min))
545         (when (search-forward "\n\n" nil t)
546           (delete-region (point-min) (1- (point))))))
547     res))
548
549 (defun gnus-request-post (gnus-command-method)
550   "Post the current buffer using GNUS-COMMAND-METHOD."
551   (when (stringp gnus-command-method)
552     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
553   (funcall (gnus-get-function gnus-command-method 'request-post)
554            (nth 1 gnus-command-method)))
555
556 (defun gnus-request-scan (group gnus-command-method)
557   "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
558 If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
559   (let ((gnus-command-method
560          (if group (gnus-find-method-for-group group) gnus-command-method))
561         (gnus-inhibit-demon t)
562         (mail-source-plugged gnus-plugged))
563     (when (or gnus-plugged
564               (not (gnus-agent-method-p gnus-command-method)))
565       (setq gnus-internal-registry-spool-current-method gnus-command-method)
566       (funcall (gnus-get-function gnus-command-method 'request-scan)
567                (and group (gnus-group-real-name group))
568                (nth 1 gnus-command-method)))))
569
570 (defsubst gnus-request-update-info (info gnus-command-method)
571   "Request that GNUS-COMMAND-METHOD update INFO."
572   (when (stringp gnus-command-method)
573     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
574   (when (gnus-check-backend-function
575          'request-update-info (car gnus-command-method))
576     (let ((group (gnus-info-group info)))
577       (and (funcall (gnus-get-function gnus-command-method
578                                        'request-update-info)
579                     (gnus-group-real-name group)
580                     info (nth 1 gnus-command-method))
581            ;; If the minimum article number is greater than 1, then all
582            ;; smaller article numbers are known not to exist; we'll
583            ;; artificially add those to the 'read range.
584            (let* ((active (gnus-active group))
585                   (min (car active)))
586              (when (> min 1)
587                (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
588                       (read (gnus-info-read info))
589                       (new-read (gnus-range-add read (list range))))
590                  (gnus-info-set-read info new-read)))
591              info)))))
592
593 (defun gnus-request-expire-articles (articles group &optional force)
594   (let* ((gnus-command-method (gnus-find-method-for-group group))
595          (not-deleted
596           (funcall
597            (gnus-get-function gnus-command-method 'request-expire-articles)
598            articles (gnus-group-real-name group) (nth 1 gnus-command-method)
599            force)))
600     (when (and gnus-agent
601                (gnus-agent-method-p gnus-command-method))
602       (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
603         (when expired-articles
604           (gnus-agent-expire expired-articles group 'force))))
605     not-deleted))
606
607 (defun gnus-request-move-article (article group server accept-function
608                                           &optional last move-is-internal)
609   (let* ((gnus-command-method (gnus-find-method-for-group group))
610          (result (funcall (gnus-get-function gnus-command-method
611                                              'request-move-article)
612                           article (gnus-group-real-name group)
613                           (nth 1 gnus-command-method) accept-function last move-is-internal)))
614     (when (and result gnus-agent
615                (gnus-agent-method-p gnus-command-method))
616       (gnus-agent-unfetch-articles group (list article)))
617     result))
618
619 (defun gnus-request-accept-article (group &optional gnus-command-method last
620                                           no-encode)
621   ;; Make sure there's a newline at the end of the article.
622   (when (stringp gnus-command-method)
623     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
624   (when (and (not gnus-command-method)
625              (stringp group))
626     (setq gnus-command-method (or (gnus-find-method-for-group group)
627                                   (gnus-group-name-to-method group))))
628   (goto-char (point-max))
629   (unless (bolp)
630     (insert "\n"))
631   (unless no-encode
632     (let ((message-options message-options))
633       (message-options-set-recipient)
634       (save-restriction
635         (message-narrow-to-head)
636         (let ((mail-parse-charset message-default-charset))
637           (mail-encode-encoded-word-buffer)))
638       (message-encode-message-body)))
639   (let ((gnus-command-method (or gnus-command-method
640                                  (gnus-find-method-for-group group)))
641         (result
642          (funcall
643           (gnus-get-function gnus-command-method 'request-accept-article)
644           (if (stringp group) (gnus-group-real-name group) group)
645           (cadr gnus-command-method)
646           last)))
647     (when (and gnus-agent (gnus-agent-method-p gnus-command-method))
648       (gnus-agent-regenerate-group group (list (cdr result))))
649     result))
650
651 (defun gnus-request-replace-article (article group buffer &optional no-encode)
652   (unless no-encode
653     (let ((message-options message-options))
654       (message-options-set-recipient)
655       (save-restriction
656         (message-narrow-to-head)
657         (let ((mail-parse-charset message-default-charset))
658           (mail-encode-encoded-word-buffer)))
659       (message-encode-message-body)))
660   (let* ((func (car (gnus-group-name-to-method group)))
661          (result (funcall (intern (format "%s-request-replace-article" func))
662                           article (gnus-group-real-name group) buffer)))
663     (when (and gnus-agent (gnus-agent-method-p gnus-command-method))
664       (gnus-agent-regenerate-group group (list article)))
665     result))
666
667 (defun gnus-request-associate-buffer (group)
668   (let ((gnus-command-method (gnus-find-method-for-group group)))
669     (funcall (gnus-get-function gnus-command-method 'request-associate-buffer)
670              (gnus-group-real-name group))))
671
672 (defun gnus-request-restore-buffer (article group)
673   "Request a new buffer restored to the state of ARTICLE."
674   (let ((gnus-command-method (gnus-find-method-for-group group)))
675     (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
676              article (gnus-group-real-name group)
677              (nth 1 gnus-command-method))))
678
679 (defun gnus-request-create-group (group &optional gnus-command-method args)
680   (when (stringp gnus-command-method)
681     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
682   (let ((gnus-command-method
683          (or gnus-command-method (gnus-find-method-for-group group))))
684     (funcall (gnus-get-function gnus-command-method 'request-create-group)
685              (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
686
687 (defun gnus-request-delete-group (group &optional force)
688   (let* ((gnus-command-method (gnus-find-method-for-group group))
689          (result
690           (funcall (gnus-get-function gnus-command-method 'request-delete-group)
691                    (gnus-group-real-name group) force (nth 1 gnus-command-method))))
692     (when result
693       (gnus-cache-delete-group group)
694       (gnus-agent-delete-group group))
695     result))
696
697 (defun gnus-request-rename-group (group new-name)
698   (let* ((gnus-command-method (gnus-find-method-for-group group))
699          (result
700           (funcall (gnus-get-function gnus-command-method 'request-rename-group)
701                    (gnus-group-real-name group)
702                    (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
703     (when result
704       (gnus-cache-rename-group group new-name)
705       (gnus-agent-rename-group group new-name))
706     result))
707
708 (defun gnus-close-backends ()
709   ;; Send a close request to all backends that support such a request.
710   (let ((methods gnus-valid-select-methods)
711         (gnus-inhibit-demon t)
712         func gnus-command-method)
713     (while (setq gnus-command-method (pop methods))
714       (when (fboundp (setq func (intern
715                                  (concat (car gnus-command-method)
716                                          "-request-close"))))
717         (funcall func)))))
718
719 (defun gnus-asynchronous-p (gnus-command-method)
720   (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
721     (when (fboundp func)
722       (funcall func))))
723
724 (defun gnus-remove-denial (gnus-command-method)
725   (when (stringp gnus-command-method)
726     (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
727   (let* ((elem (assoc gnus-command-method gnus-opened-servers))
728          (status (cadr elem)))
729     ;; If this hasn't been opened before, we add it to the list.
730     (when (eq status 'denied)
731       ;; Set the status of this server.
732       (setcar (cdr elem) 'closed))))
733
734 (provide 'gnus-int)
735
736 ;;; gnus-int.el ends here