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