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