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