lisp/gnus/gnus-int.el (gnus-backend-trace): Also note the elapsed seconds
[bpt/emacs.git] / lisp / gnus / gnus-int.el
CommitLineData
eec82323 1;;; gnus-int.el --- backend interface functions for Gnus
e84b4b86 2
acaf905b 3;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
eec82323 4
6748645f 5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
eec82323
LMI
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
5e809f55 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eec82323
LMI
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
22
23;;; Commentary:
24
25;;; Code:
26
249ffa67
RS
27(eval-when-compile (require 'cl))
28
eec82323 29(require 'gnus)
23f87bed
MB
30(require 'message)
31(require 'gnus-range)
32
0be56f17 33(autoload 'gnus-run-hook-with-args "gnus-util")
23f87bed 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
cbabe91f
TZ
44(defcustom gnus-after-set-mark-hook nil
45 "Hook called just after marks are set in a group."
2bed3f04 46 :version "24.1"
cbabe91f
TZ
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."
2bed3f04 52 :version "24.1"
cbabe91f
TZ
53 :group 'gnus-start
54 :type 'hook)
55
23f87bed
MB
56(defcustom gnus-server-unopen-status nil
57 "The default status if the server is not able to open.
58If the server is covered by Gnus agent, the possible values are
59`denied', set the server denied; `offline', set the server offline;
60nil, ask user. If the server is not covered by Gnus agent, set the
61server denied."
bf247b6e 62 :version "22.1"
23f87bed
MB
63 :group 'gnus-start
64 :type '(choice (const :tag "Ask" nil)
65 (const :tag "Deny server" denied)
66 (const :tag "Unplug Agent" offline)))
67
aa357b80
SM
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
23f87bed
MB
75(defvar gnus-internal-registry-spool-current-method nil
76 "The current method, for the registry.")
77
fe86b5e0
GM
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.
90If GNUS-COMMAND-METHOD is a string, it is interpreted as a group
91name. 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
eec82323
LMI
99;;;
100;;; Server Communication
101;;;
102
103(defun gnus-start-news-server (&optional confirm)
104 "Open a method for getting news.
105If 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.
eec82323
LMI
111 (when confirm
112 ;; Read server name with completion.
113 (setq gnus-nntp-server
229b59da
G
114 (gnus-completing-read "NNTP server"
115 (cons gnus-nntp-server
116 gnus-secondary-servers)
117 nil gnus-nntp-server)))
eec82323
LMI
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
31f35b6f 131 (substring gnus-nntp-server 1) "~/")))
eec82323
LMI
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)
6748645f 147 (gnus-run-hooks 'gnus-open-server-hook)
23f87bed
MB
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
eec82323
LMI
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)
16409b0b 164 gnus-batch-mode
eec82323
LMI
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.
181If METHOD is nil, use `gnus-select-method'.
182If it is down, start it up (again)."
23f87bed
MB
183 (let ((method (or method gnus-select-method))
184 result)
eec82323
LMI
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)))))
6748645f 196 (gnus-run-hooks 'gnus-open-server-hook)
eec82323 197 (prog1
20a673b2 198 (setq result (gnus-open-server method))
eec82323 199 (unless silent
8ccbef23
G
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))))))))))
eec82323
LMI
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)))
6748645f
LMI
217 ;; Check cache of constructed names.
218 (let* ((method-sym (if gnus-agent
54506618 219 (inline (gnus-agent-get-function method))
6748645f
LMI
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.
eec82323 231 (unless (fboundp func)
6748645f
LMI
232 (unless (car method)
233 (error "Trying to require a method that doesn't exist"))
eec82323 234 (require (car method))
6748645f
LMI
235 (when (not (fboundp func))
236 (if noerror
237 (setq func nil)
238 (error "No such function: %s" func))))
eec82323
LMI
239 func))
240
241\f
242;;;
243;;; Interface functions to the backends.
244;;;
245
20a673b2
KY
246(defun gnus-method-denied-p (method)
247 (eq (nth 1 (assoc method gnus-opened-servers))
248 'denied))
249
cd001b8c 250(defvar gnus-backend-trace nil)
b069e5a6 251
1a8b65e0
G
252(defun gnus-backend-trace (type form)
253 (with-current-buffer (get-buffer-create "*gnus trace*")
254 (buffer-disable-undo)
24d0bb6b
LI
255 (goto-char (point-max))
256 (insert (format-time-string "%H:%M:%S")
257 (format " %.2fs %s %S\n"
258 (if (numberp gnus-backend-trace)
259 (- (float-time) gnus-backend-trace)
260 0)
261 type form))
262 (setq gnus-backend-trace (float-time))))
1a8b65e0 263
6748645f
LMI
264(defun gnus-open-server (gnus-command-method)
265 "Open a connection to GNUS-COMMAND-METHOD."
266 (when (stringp gnus-command-method)
267 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
1a8b65e0 268 (gnus-backend-trace :opening gnus-command-method)
01c52d31
MB
269 (let ((elem (assoc gnus-command-method gnus-opened-servers))
270 (server (gnus-method-to-server-name gnus-command-method)))
eec82323
LMI
271 ;; If this method was previously denied, we just return nil.
272 (if (eq (nth 1 elem) 'denied)
273 (progn
e21bac42
G
274 (gnus-message
275 1 "Server %s previously determined to be down; not retrying" server)
eec82323
LMI
276 nil)
277 ;; Open the server.
04db63bc
G
278 (let* ((open-server-function
279 (gnus-get-function gnus-command-method 'open-server))
23f87bed 280 (result
04db63bc
G
281 (condition-case err
282 (funcall open-server-function
283 (nth 1 gnus-command-method)
284 (nthcdr 2 gnus-command-method))
285 (error
286 (gnus-message 1 "Unable to open server %s due to: %s"
287 server (error-message-string err))
288 nil)
289 (quit
a123622d
G
290 (if debug-on-quit
291 (debug "Quit")
292 (gnus-message 1 "Quit trying to open server %s" server))
04db63bc
G
293 nil)))
294 open-offline)
eec82323
LMI
295 ;; If this hasn't been opened before, we add it to the list.
296 (unless elem
6748645f 297 (setq elem (list gnus-command-method nil)
eec82323
LMI
298 gnus-opened-servers (cons elem gnus-opened-servers)))
299 ;; Set the status of this server.
8ccbef23
G
300 (setcar
301 (cdr elem)
302 (cond (result
303 (if (eq open-server-function #'nnagent-open-server)
304 ;; The agent's backend has a "special" status
305 'offline
306 'ok))
307 ((and gnus-agent
308 (gnus-agent-method-p gnus-command-method))
309 (cond
310 (gnus-server-unopen-status
311 ;; Set the server's status to the unopen
312 ;; status. If that status is offline,
313 ;; recurse to open the agent's backend.
314 (setq open-offline (eq gnus-server-unopen-status 'offline))
315 gnus-server-unopen-status)
316 ((not gnus-batch-mode)
317 (setq open-offline t)
318 'offline)
319 (t
320 ;; This agentized server was still denied
321 'denied)))
322 (t
323 ;; This unagentized server must be denied
324 'denied)))
23f87bed
MB
325
326 ;; NOTE: I MUST set the server's status to offline before this
327 ;; recursive call as this status will drive the
328 ;; gnus-get-function (called above) to return the agent's
329 ;; backend.
330 (if open-offline
331 ;; Recursively open this offline server to perform the
332 ;; open-server function of the agent's backend.
333 (let ((gnus-server-unopen-status 'denied))
334 ;; Bind gnus-server-unopen-status to avoid recursively
335 ;; prompting with "go offline?". This is only a concern
336 ;; when the agent's backend fails to open the server.
337 (gnus-open-server gnus-command-method))
b890d447
MB
338 (when (and (eq (cadr elem) 'ok) gnus-agent
339 (gnus-agent-method-p gnus-command-method))
340 (save-excursion
341 (gnus-agent-possibly-synchronize-flags-server
342 gnus-command-method)))
1a8b65e0 343 (gnus-backend-trace :opened gnus-command-method)
23f87bed 344 result)))))
eec82323 345
6748645f
LMI
346(defun gnus-close-server (gnus-command-method)
347 "Close the connection to GNUS-COMMAND-METHOD."
348 (when (stringp gnus-command-method)
349 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
350 (funcall (gnus-get-function gnus-command-method 'close-server)
351 (nth 1 gnus-command-method)))
352
353(defun gnus-request-list (gnus-command-method)
354 "Request the active file from GNUS-COMMAND-METHOD."
355 (when (stringp gnus-command-method)
356 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
357 (funcall (gnus-get-function gnus-command-method 'request-list)
358 (nth 1 gnus-command-method)))
359
20a673b2
KY
360(defun gnus-finish-retrieve-group-infos (gnus-command-method infos data)
361 "Read and update infos from GNUS-COMMAND-METHOD."
362 (when (stringp gnus-command-method)
363 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
1a8b65e0
G
364 (gnus-backend-trace :finishing gnus-command-method)
365 (prog1
366 (funcall (gnus-get-function gnus-command-method
367 'finish-retrieve-group-infos)
368 (nth 1 gnus-command-method)
369 infos data)
370 (gnus-backend-trace :finished gnus-command-method)))
20a673b2
KY
371
372(defun gnus-retrieve-group-data-early (gnus-command-method infos)
9858f6c3 373 "Start early async retrieval of data from GNUS-COMMAND-METHOD."
20a673b2
KY
374 (when (stringp gnus-command-method)
375 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
376 (funcall (gnus-get-function gnus-command-method 'retrieve-group-data-early)
377 (nth 1 gnus-command-method)
378 infos))
379
6748645f
LMI
380(defun gnus-request-list-newsgroups (gnus-command-method)
381 "Request the newsgroups file from GNUS-COMMAND-METHOD."
382 (when (stringp gnus-command-method)
383 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
384 (funcall (gnus-get-function gnus-command-method 'request-list-newsgroups)
385 (nth 1 gnus-command-method)))
386
387(defun gnus-request-newgroups (date gnus-command-method)
388 "Request all new groups since DATE from GNUS-COMMAND-METHOD."
389 (when (stringp gnus-command-method)
390 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
391 (let ((func (gnus-get-function gnus-command-method 'request-newgroups t)))
eec82323 392 (when func
6748645f
LMI
393 (funcall func date (nth 1 gnus-command-method)))))
394
6748645f
LMI
395(defun gnus-request-regenerate (gnus-command-method)
396 "Request a data generation from GNUS-COMMAND-METHOD."
397 (when (stringp gnus-command-method)
398 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
399 (funcall (gnus-get-function gnus-command-method 'request-regenerate)
400 (nth 1 gnus-command-method)))
401
01c52d31
MB
402(defun gnus-request-compact-group (group)
403 (let* ((method (gnus-find-method-for-group group))
404 (gnus-command-method method)
405 (result
406 (funcall (gnus-get-function gnus-command-method
407 'request-compact-group)
408 (gnus-group-real-name group)
409 (nth 1 gnus-command-method) t)))
410 result))
411
412(defun gnus-request-compact (gnus-command-method)
e7719ea1 413 "Request groups compaction from GNUS-COMMAND-METHOD."
01c52d31
MB
414 (when (stringp gnus-command-method)
415 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
416 (funcall (gnus-get-function gnus-command-method 'request-compact)
417 (nth 1 gnus-command-method)))
418
286c4fc2 419(defun gnus-request-group (group &optional dont-check gnus-command-method info)
eec82323 420 "Request GROUP. If DONT-CHECK, no information is required."
6748645f
LMI
421 (let ((gnus-command-method
422 (or gnus-command-method (inline (gnus-find-method-for-group group)))))
423 (when (stringp gnus-command-method)
424 (setq gnus-command-method
425 (inline (gnus-server-to-method gnus-command-method))))
988818b8 426 (funcall (inline (gnus-get-function gnus-command-method 'request-group))
6748645f 427 (gnus-group-real-name group) (nth 1 gnus-command-method)
286c4fc2
LMI
428 dont-check
429 info)))
eec82323 430
eec82323
LMI
431(defun gnus-request-group-description (group)
432 "Request a description of GROUP."
6748645f 433 (let ((gnus-command-method (gnus-find-method-for-group group))
eec82323
LMI
434 (func 'request-group-description))
435 (when (gnus-check-backend-function func group)
6748645f
LMI
436 (funcall (gnus-get-function gnus-command-method func)
437 (gnus-group-real-name group) (nth 1 gnus-command-method)))))
eec82323
LMI
438
439(defun gnus-close-group (group)
440 "Request the GROUP be closed."
6748645f
LMI
441 (let ((gnus-command-method (inline (gnus-find-method-for-group group))))
442 (funcall (gnus-get-function gnus-command-method 'close-group)
443 (gnus-group-real-name group) (nth 1 gnus-command-method))))
eec82323
LMI
444
445(defun gnus-retrieve-headers (articles group &optional fetch-old)
446 "Request headers for ARTICLES in GROUP.
447If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
6748645f 448 (let ((gnus-command-method (gnus-find-method-for-group group)))
23f87bed
MB
449 (cond
450 ((and gnus-use-cache (numberp (car articles)))
451 (gnus-cache-retrieve-headers articles group fetch-old))
452 ((and gnus-agent (gnus-online gnus-command-method)
453 (gnus-agent-method-p gnus-command-method))
454 (gnus-agent-retrieve-headers articles group fetch-old))
455 (t
6748645f
LMI
456 (funcall (gnus-get-function gnus-command-method 'retrieve-headers)
457 articles (gnus-group-real-name group)
23f87bed 458 (nth 1 gnus-command-method) fetch-old)))))
6748645f
LMI
459
460(defun gnus-retrieve-articles (articles group)
461 "Request ARTICLES in GROUP."
462 (let ((gnus-command-method (gnus-find-method-for-group group)))
463 (funcall (gnus-get-function gnus-command-method 'retrieve-articles)
464 articles (gnus-group-real-name group)
465 (nth 1 gnus-command-method))))
466
467(defun gnus-retrieve-groups (groups gnus-command-method)
468 "Request active information on GROUPS from GNUS-COMMAND-METHOD."
469 (when (stringp gnus-command-method)
470 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
471 (funcall (gnus-get-function gnus-command-method 'retrieve-groups)
472 groups (nth 1 gnus-command-method)))
eec82323
LMI
473
474(defun gnus-request-type (group &optional article)
475 "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
6748645f
LMI
476 (let ((gnus-command-method (gnus-find-method-for-group group)))
477 (if (not (gnus-check-backend-function
478 'request-type (car gnus-command-method)))
eec82323 479 'unknown
6748645f 480 (funcall (gnus-get-function gnus-command-method 'request-type)
eec82323
LMI
481 (gnus-group-real-name group) article))))
482
549c9aed
G
483(defun gnus-request-update-group-status (group status)
484 "Change the status of a group.
485Valid statuses include `subscribe' and `unsubscribe'."
486 (let ((gnus-command-method (gnus-find-method-for-group group)))
487 (if (not (gnus-check-backend-function
488 'request-update-group-status (car gnus-command-method)))
489 nil
490 (funcall
491 (gnus-get-function gnus-command-method 'request-update-group-status)
492 (gnus-group-real-name group) status
493 (nth 1 gnus-command-method)))))
494
16409b0b 495(defun gnus-request-set-mark (group action)
23f87bed 496 "Set marks on articles in the back end."
16409b0b
GM
497 (let ((gnus-command-method (gnus-find-method-for-group group)))
498 (if (not (gnus-check-backend-function
499 'request-set-mark (car gnus-command-method)))
500 action
501 (funcall (gnus-get-function gnus-command-method 'request-set-mark)
502 (gnus-group-real-name group) action
cbabe91f
TZ
503 (nth 1 gnus-command-method))
504 (gnus-run-hook-with-args gnus-after-set-mark-hook group action))))
16409b0b 505
eec82323 506(defun gnus-request-update-mark (group article mark)
23f87bed 507 "Allow the back end to change the mark the user tries to put on an article."
6748645f
LMI
508 (let ((gnus-command-method (gnus-find-method-for-group group)))
509 (if (not (gnus-check-backend-function
510 'request-update-mark (car gnus-command-method)))
eec82323 511 mark
cbabe91f 512 (gnus-run-hook-with-args gnus-before-update-mark-hook group article mark)
6748645f 513 (funcall (gnus-get-function gnus-command-method 'request-update-mark)
eec82323
LMI
514 (gnus-group-real-name group) article mark))))
515
516(defun gnus-request-article (article group &optional buffer)
517 "Request the ARTICLE in GROUP.
518ARTICLE can either be an article number or an article Message-ID.
519If BUFFER, insert the article in that group."
6748645f
LMI
520 (let ((gnus-command-method (gnus-find-method-for-group group)))
521 (funcall (gnus-get-function gnus-command-method 'request-article)
522 article (gnus-group-real-name group)
523 (nth 1 gnus-command-method) buffer)))
eec82323 524
0c136286 525(defun gnus-request-thread (header group)
b31b26b4 526 "Request the headers in the thread containing the article specified by HEADER."
0c136286 527 (let ((gnus-command-method (gnus-find-method-for-group group)))
030158f3 528 (funcall (gnus-get-function gnus-command-method 'request-thread)
0c136286
AC
529 header
530 (gnus-group-real-name group))))
030158f3 531
89b163db
G
532(defun gnus-select-group-with-message-id (group message-id)
533 "Activate and select GROUP with the given MESSAGE-ID selected.
534Returns the article number of the message.
535
536If GROUP is not already selected, the message will be the only one in
537the group's summary.
538"
539 ;; TODO: is there a way to know at this point whether the group will
540 ;; be newly-selected? If so we could clean up the logic at the end
541 ;;
542 ;; save the new group's display parameter, if any, so we
543 ;; can replace it temporarily with zero.
544 (let ((saved-display
545 (gnus-group-get-parameter group 'display :allow-list)))
546
547 ;; Tell gnus we really don't want any articles
548 (gnus-group-set-parameter group 'display 0)
549
550 (unwind-protect
551 (gnus-summary-read-group-1
552 group (not :show-all) :no-article (not :kill-buffer)
553 ;; The combination of no-display and this dummy list of
554 ;; articles to select somehow makes it possible to open a
555 ;; group with no articles in it. Black magic.
556 :no-display '(-1); select-articles
557 )
558 ;; Restore the new group's display parameter
559 (gnus-group-set-parameter group 'display saved-display)))
560
561 ;; The summary buffer was suppressed by :no-display above.
562 ;; Create it now and insert the message
563 (let ((group-is-new (gnus-summary-setup-buffer group)))
564 (condition-case err
565 (let ((article-number
566 (gnus-summary-insert-subject message-id)))
567 (unless article-number
568 (signal 'error "message-id not in group"))
569 (gnus-summary-select-article nil nil nil article-number)
570 article-number)
571 ;; Clean up the new summary and propagate the error
572 (error (when group-is-new (gnus-summary-exit))
573 (apply 'signal err)))))
574
575(defun gnus-simplify-group-name (group)
576 "Return the simplest representation of the name of GROUP.
577This is the string that Gnus uses to identify the group."
578 (gnus-group-prefixed-name
579 (gnus-group-real-name group)
580 (gnus-group-method group)))
581
4ddab346
G
582(defun gnus-warp-to-article ()
583 "Warps from an article in a virtual group to the article in its
584real group. Does nothing on a real group."
585 (interactive)
cef5bb19
G
586 (when (gnus-virtual-group-p gnus-newsgroup-name)
587 (let ((gnus-command-method
89b163db
G
588 (gnus-find-method-for-group gnus-newsgroup-name)))
589 (or
590 (when (gnus-check-backend-function
591 'warp-to-article (car gnus-command-method))
592 (funcall (gnus-get-function gnus-command-method 'warp-to-article)))
593 (and (bound-and-true-p gnus-registry-enabled)
594 (gnus-try-warping-via-registry))))))
4ddab346 595
eec82323
LMI
596(defun gnus-request-head (article group)
597 "Request the head of ARTICLE in GROUP."
6748645f
LMI
598 (let* ((gnus-command-method (gnus-find-method-for-group group))
599 (head (gnus-get-function gnus-command-method 'request-head t))
eec82323
LMI
600 res clean-up)
601 (cond
602 ;; Check the cache.
603 ((and gnus-use-cache
604 (numberp article)
605 (gnus-cache-request-article article group))
606 (setq res (cons group article)
607 clean-up t))
23f87bed
MB
608 ;; Check the agent cache.
609 ((gnus-agent-request-article article group)
610 (setq res (cons group article)
611 clean-up t))
eec82323
LMI
612 ;; Use `head' function.
613 ((fboundp head)
c20643e2
DA
614 (setq res (funcall head article
615 (and (not gnus-override-method) (gnus-group-real-name group))
6748645f 616 (nth 1 gnus-command-method))))
eec82323
LMI
617 ;; Use `article' function.
618 (t
619 (setq res (gnus-request-article article group)
620 clean-up t)))
621 (when clean-up
20a673b2 622 (with-current-buffer nntp-server-buffer
eec82323
LMI
623 (goto-char (point-min))
624 (when (search-forward "\n\n" nil t)
625 (delete-region (1- (point)) (point-max)))
626 (nnheader-fold-continuation-lines)))
627 res))
628
629(defun gnus-request-body (article group)
630 "Request the body of ARTICLE in GROUP."
6748645f
LMI
631 (let* ((gnus-command-method (gnus-find-method-for-group group))
632 (head (gnus-get-function gnus-command-method 'request-body t))
633 res clean-up)
634 (cond
635 ;; Check the cache.
636 ((and gnus-use-cache
637 (numberp article)
638 (gnus-cache-request-article article group))
639 (setq res (cons group article)
640 clean-up t))
23f87bed
MB
641 ;; Check the agent cache.
642 ((gnus-agent-request-article article group)
643 (setq res (cons group article)
644 clean-up t))
6748645f
LMI
645 ;; Use `head' function.
646 ((fboundp head)
647 (setq res (funcall head article (gnus-group-real-name group)
648 (nth 1 gnus-command-method))))
649 ;; Use `article' function.
650 (t
651 (setq res (gnus-request-article article group)
652 clean-up t)))
653 (when clean-up
20a673b2 654 (with-current-buffer nntp-server-buffer
6748645f
LMI
655 (goto-char (point-min))
656 (when (search-forward "\n\n" nil t)
657 (delete-region (point-min) (1- (point))))))
658 res))
eec82323 659
6748645f
LMI
660(defun gnus-request-post (gnus-command-method)
661 "Post the current buffer using GNUS-COMMAND-METHOD."
662 (when (stringp gnus-command-method)
663 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
664 (funcall (gnus-get-function gnus-command-method 'request-post)
665 (nth 1 gnus-command-method)))
666
0617bb00
LMI
667(defun gnus-request-expunge-group (group gnus-command-method)
668 "Expunge GROUP, which is removing articles that have been marked as deleted."
669 (when (stringp gnus-command-method)
670 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
671 (funcall (gnus-get-function gnus-command-method 'request-expunge-group)
672 (gnus-group-real-name group)
673 (nth 1 gnus-command-method)))
674
6748645f
LMI
675(defun gnus-request-scan (group gnus-command-method)
676 "Request a SCAN being performed in GROUP from GNUS-COMMAND-METHOD.
677If GROUP is nil, all groups on GNUS-COMMAND-METHOD are scanned."
16409b0b
GM
678 (let ((gnus-command-method
679 (if group (gnus-find-method-for-group group) gnus-command-method))
680 (gnus-inhibit-demon t)
681 (mail-source-plugged gnus-plugged))
8c3e17f8
LMI
682 (when (or gnus-plugged
683 (not (gnus-agent-method-p gnus-command-method)))
01c52d31
MB
684 (setq gnus-internal-registry-spool-current-method gnus-command-method)
685 (funcall (gnus-get-function gnus-command-method 'request-scan)
686 (and group (gnus-group-real-name group))
687 (nth 1 gnus-command-method)))))
6748645f 688
b1ae92ba
G
689(defun gnus-request-update-info (info gnus-command-method)
690 (when (gnus-check-backend-function
691 'request-update-info (car gnus-command-method))
692 (when (stringp gnus-command-method)
693 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
694 (funcall (gnus-get-function gnus-command-method 'request-update-info)
695 (gnus-group-real-name (gnus-info-group info)) info
696 (nth 1 gnus-command-method))))
697
698(defsubst gnus-request-marks (info gnus-command-method)
6748645f
LMI
699 "Request that GNUS-COMMAND-METHOD update INFO."
700 (when (stringp gnus-command-method)
701 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
702 (when (gnus-check-backend-function
b1ae92ba 703 'request-marks (car gnus-command-method))
23f87bed 704 (let ((group (gnus-info-group info)))
9da02ea1 705 (and (funcall (gnus-get-function gnus-command-method 'request-marks)
23f87bed
MB
706 (gnus-group-real-name group)
707 info (nth 1 gnus-command-method))
708 ;; If the minimum article number is greater than 1, then all
709 ;; smaller article numbers are known not to exist; we'll
710 ;; artificially add those to the 'read range.
711 (let* ((active (gnus-active group))
712 (min (car active)))
713 (when (> min 1)
714 (let* ((range (if (= min 2) 1 (cons 1 (1- min))))
715 (read (gnus-info-read info))
716 (new-read (gnus-range-add read (list range))))
717 (gnus-info-set-read info new-read)))
718 info)))))
eec82323
LMI
719
720(defun gnus-request-expire-articles (articles group &optional force)
23f87bed 721 (let* ((gnus-command-method (gnus-find-method-for-group group))
c20643e2
DA
722 ;; Filter out any negative article numbers; they can't be
723 ;; expired here.
724 (articles
725 (delq nil (mapcar (lambda (n) (and (>= n 0) n)) articles)))
b069e5a6 726 (gnus-inhibit-demon t)
23f87bed
MB
727 (not-deleted
728 (funcall
729 (gnus-get-function gnus-command-method 'request-expire-articles)
730 articles (gnus-group-real-name group) (nth 1 gnus-command-method)
731 force)))
732 (when (and gnus-agent
733 (gnus-agent-method-p gnus-command-method))
734 (let ((expired-articles (gnus-sorted-difference articles not-deleted)))
735 (when expired-articles
736 (gnus-agent-expire expired-articles group 'force))))
737 not-deleted))
738
739(defun gnus-request-move-article (article group server accept-function
01c52d31 740 &optional last move-is-internal)
23f87bed
MB
741 (let* ((gnus-command-method (gnus-find-method-for-group group))
742 (result (funcall (gnus-get-function gnus-command-method
743 'request-move-article)
744 article (gnus-group-real-name group)
b5c575e6
G
745 (nth 1 gnus-command-method) accept-function
746 last move-is-internal)))
23f87bed
MB
747 (when (and result gnus-agent
748 (gnus-agent-method-p gnus-command-method))
54506618 749 (gnus-agent-unfetch-articles group (list article)))
23f87bed 750 result))
bf247b6e 751
16409b0b
GM
752(defun gnus-request-accept-article (group &optional gnus-command-method last
753 no-encode)
eec82323 754 ;; Make sure there's a newline at the end of the article.
6748645f
LMI
755 (when (stringp gnus-command-method)
756 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
757 (when (and (not gnus-command-method)
eec82323 758 (stringp group))
54506618
MB
759 (setq gnus-command-method (or (gnus-find-method-for-group group)
760 (gnus-group-name-to-method group))))
eec82323
LMI
761 (goto-char (point-max))
762 (unless (bolp)
763 (insert "\n"))
16409b0b 764 (unless no-encode
23f87bed
MB
765 (let ((message-options message-options))
766 (message-options-set-recipient)
767 (save-restriction
768 (message-narrow-to-head)
769 (let ((mail-parse-charset message-default-charset))
770 (mail-encode-encoded-word-buffer)))
771 (message-encode-message-body)))
01c52d31 772 (let ((gnus-command-method (or gnus-command-method
54506618 773 (gnus-find-method-for-group group)))
bf247b6e
KS
774 (result
775 (funcall
54506618
MB
776 (gnus-get-function gnus-command-method 'request-accept-article)
777 (if (stringp group) (gnus-group-real-name group) group)
778 (cadr gnus-command-method)
779 last)))
c516cd6d
LMI
780 (when (and gnus-agent
781 (gnus-agent-method-p gnus-command-method)
782 (cdr result))
54506618
MB
783 (gnus-agent-regenerate-group group (list (cdr result))))
784 result))
eec82323 785
16409b0b
GM
786(defun gnus-request-replace-article (article group buffer &optional no-encode)
787 (unless no-encode
23f87bed
MB
788 (let ((message-options message-options))
789 (message-options-set-recipient)
790 (save-restriction
791 (message-narrow-to-head)
792 (let ((mail-parse-charset message-default-charset))
793 (mail-encode-encoded-word-buffer)))
794 (message-encode-message-body)))
54506618
MB
795 (let* ((func (car (gnus-group-name-to-method group)))
796 (result (funcall (intern (format "%s-request-replace-article" func))
797 article (gnus-group-real-name group) buffer)))
798 (when (and gnus-agent (gnus-agent-method-p gnus-command-method))
799 (gnus-agent-regenerate-group group (list article)))
800 result))
eec82323 801
eec82323
LMI
802(defun gnus-request-restore-buffer (article group)
803 "Request a new buffer restored to the state of ARTICLE."
6748645f
LMI
804 (let ((gnus-command-method (gnus-find-method-for-group group)))
805 (funcall (gnus-get-function gnus-command-method 'request-restore-buffer)
806 article (gnus-group-real-name group)
807 (nth 1 gnus-command-method))))
eec82323 808
6748645f
LMI
809(defun gnus-request-create-group (group &optional gnus-command-method args)
810 (when (stringp gnus-command-method)
811 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
812 (let ((gnus-command-method
813 (or gnus-command-method (gnus-find-method-for-group group))))
814 (funcall (gnus-get-function gnus-command-method 'request-create-group)
815 (gnus-group-real-name group) (nth 1 gnus-command-method) args)))
eec82323
LMI
816
817(defun gnus-request-delete-group (group &optional force)
54506618
MB
818 (let* ((gnus-command-method (gnus-find-method-for-group group))
819 (result
820 (funcall (gnus-get-function gnus-command-method 'request-delete-group)
821 (gnus-group-real-name group) force (nth 1 gnus-command-method))))
822 (when result
823 (gnus-cache-delete-group group)
824 (gnus-agent-delete-group group))
825 result))
eec82323
LMI
826
827(defun gnus-request-rename-group (group new-name)
54506618
MB
828 (let* ((gnus-command-method (gnus-find-method-for-group group))
829 (result
830 (funcall (gnus-get-function gnus-command-method 'request-rename-group)
831 (gnus-group-real-name group)
832 (gnus-group-real-name new-name) (nth 1 gnus-command-method))))
833 (when result
834 (gnus-cache-rename-group group new-name)
835 (gnus-agent-rename-group group new-name))
836 result))
eec82323
LMI
837
838(defun gnus-close-backends ()
839 ;; Send a close request to all backends that support such a request.
840 (let ((methods gnus-valid-select-methods)
841 (gnus-inhibit-demon t)
6748645f
LMI
842 func gnus-command-method)
843 (while (setq gnus-command-method (pop methods))
eec82323 844 (when (fboundp (setq func (intern
6748645f
LMI
845 (concat (car gnus-command-method)
846 "-request-close"))))
eec82323
LMI
847 (funcall func)))))
848
6748645f
LMI
849(defun gnus-asynchronous-p (gnus-command-method)
850 (let ((func (gnus-get-function gnus-command-method 'asynchronous-p t)))
eec82323
LMI
851 (when (fboundp func)
852 (funcall func))))
853
6748645f
LMI
854(defun gnus-remove-denial (gnus-command-method)
855 (when (stringp gnus-command-method)
856 (setq gnus-command-method (gnus-server-to-method gnus-command-method)))
857 (let* ((elem (assoc gnus-command-method gnus-opened-servers))
eec82323
LMI
858 (status (cadr elem)))
859 ;; If this hasn't been opened before, we add it to the list.
860 (when (eq status 'denied)
861 ;; Set the status of this server.
862 (setcar (cdr elem) 'closed))))
863
864(provide 'gnus-int)
865
866;;; gnus-int.el ends here