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