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