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