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