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