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