Merge changes made in Gnus trunk.
[bpt/emacs.git] / lisp / gnus / gnus-start.el
CommitLineData
eec82323 1;;; gnus-start.el --- startup functions for Gnus
e84b4b86 2
73b0cd50 3;; Copyright (C) 1996-2011 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
de768922 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
27(require 'gnus)
28(require 'gnus-win)
29(require 'gnus-int)
30(require 'gnus-spec)
31(require 'gnus-range)
32(require 'gnus-util)
23f87bed
MB
33(autoload 'message-make-date "message")
34(autoload 'gnus-agent-read-servers-validate "gnus-agent")
54506618 35(autoload 'gnus-agent-save-local "gnus-agent")
23f87bed 36(autoload 'gnus-agent-possibly-alter-active "gnus-agent")
54506618 37
bf247b6e 38(eval-when-compile
9efa445f 39 (require 'cl))
54506618 40
9efa445f
DN
41(defvar gnus-agent-covered-methods)
42(defvar gnus-agent-file-loading-local)
43(defvar gnus-agent-file-loading-cache)
eec82323
LMI
44
45(defcustom gnus-startup-file (nnheader-concat gnus-home-directory ".newsrc")
46 "Your `.newsrc' file.
47`.newsrc-SERVER' will be used instead if that exists."
48 :group 'gnus-start
49 :type 'file)
50
23f87bed 51(defcustom gnus-backup-startup-file 'never
bf46b4d4 52 "Control use of version numbers for backups of `gnus-startup-file'.
23f87bed
MB
53This variable takes the same values as the `version-control'
54variable."
bf247b6e 55 :version "22.1"
23f87bed
MB
56 :group 'gnus-start
57 :type '(choice (const :tag "Never" never)
58 (const :tag "If existing" nil)
59 (other :tag "Always" t)))
60
61(defcustom gnus-save-startup-file-via-temp-buffer t
62 "Whether to write the startup file contents to a buffer then save
63the buffer or write directly to the file. The buffer is faster
64because all of the contents are written at once. The direct write
65uses considerably less memory."
bf247b6e 66 :version "22.1"
23f87bed
MB
67 :group 'gnus-start
68 :type '(choice (const :tag "Write via buffer" t)
69 (const :tag "Write directly to file" nil)))
70
eec82323 71(defcustom gnus-init-file (nnheader-concat gnus-home-directory ".gnus")
83be5c8c
RS
72 "Your Gnus Emacs-Lisp startup file name.
73If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
eec82323
LMI
74 :group 'gnus-start
75 :type 'file)
76
77(defcustom gnus-site-init-file
cb75201b 78 (condition-case nil
83be5c8c
RS
79 (concat (file-name-directory
80 (directory-file-name installation-directory))
81 "site-lisp/gnus-init")
cb75201b 82 (error nil))
16409b0b 83 "The site-wide Gnus Emacs-Lisp startup file name, or nil if none.
83be5c8c 84If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
eec82323 85 :group 'gnus-start
83be5c8c 86 :type '(choice file (const nil)))
eec82323 87
eec82323
LMI
88(defcustom gnus-use-dribble-file t
89 "*Non-nil means that Gnus will use a dribble file to store user updates.
90If Emacs should crash without saving the .newsrc files, complete
91information can be restored from the dribble file."
92 :group 'gnus-dribble-file
93 :type 'boolean)
94
95(defcustom gnus-dribble-directory nil
96 "*The directory where dribble files will be saved.
97If this variable is nil, the directory where the .newsrc files are
98saved will be used."
99 :group 'gnus-dribble-file
100 :type '(choice directory (const nil)))
101
a8151ef7 102(defcustom gnus-check-new-newsgroups 'ask-server
6748645f 103 "*Non-nil means that Gnus will run `gnus-find-new-newsgroups' at startup.
eec82323
LMI
104This normally finds new newsgroups by comparing the active groups the
105servers have already reported with those Gnus already knows, either alive
106or killed.
107
6748645f 108When any of the following are true, `gnus-find-new-newsgroups' will instead
eec82323
LMI
109ask the servers (primary, secondary, and archive servers) to list new
110groups since the last time it checked:
111 1. This variable is `ask-server'.
112 2. This variable is a list of select methods (see below).
113 3. `gnus-read-active-file' is nil or `some'.
6748645f 114 4. A prefix argument is given to `gnus-find-new-newsgroups' interactively.
eec82323
LMI
115
116Thus, if this variable is `ask-server' or a list of select methods or
117`gnus-read-active-file' is nil or `some', then the killed list is no
118longer necessary, so you could safely set `gnus-save-killed-list' to nil.
119
120This variable can be a list of select methods which Gnus will query with
121the `ask-server' method in addition to the primary, secondary, and archive
122servers.
123
124Eg.
125 (setq gnus-check-new-newsgroups
126 '((nntp \"some.server\") (nntp \"other.server\")))
127
128If this variable is nil, then you have to tell Gnus explicitly to
129check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups]."
130 :group 'gnus-start
131 :type '(choice (const :tag "no" nil)
132 (const :tag "by brute force" t)
133 (const :tag "ask servers" ask-server)
134 (repeat :menu-tag "ask additional servers"
135 :tag "ask additional servers"
136 :value ((nntp ""))
137 (sexp :format "%v"))))
138
139(defcustom gnus-check-bogus-newsgroups nil
140 "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
141If this variable is nil, then you have to tell Gnus explicitly to
142check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups]."
143 :group 'gnus-start-server
144 :type 'boolean)
145
a8151ef7 146(defcustom gnus-read-active-file 'some
eec82323
LMI
147 "*Non-nil means that Gnus will read the entire active file at startup.
148If this variable is nil, Gnus will only know about the groups in your
149`.newsrc' file.
150
151If this variable is `some', Gnus will try to only read the relevant
152parts of the active file from the server. Not all servers support
153this, and it might be quite slow with other servers, but this should
154generally be faster than both the t and nil value.
155
156If you set this variable to nil or `some', you probably still want to
157be told about new newsgroups that arrive. To do that, set
158`gnus-check-new-newsgroups' to `ask-server'. This may not work
159properly with all servers."
160 :group 'gnus-start-server
161 :type '(choice (const nil)
162 (const some)
163 (const t)))
164
16409b0b
GM
165(defconst gnus-level-subscribed 5
166 "Groups with levels less than or equal to this variable are subscribed.")
eec82323 167
16409b0b
GM
168(defconst gnus-level-unsubscribed 7
169 "Groups with levels less than or equal to this variable are unsubscribed.
60b0b668
MB
170
171Groups with levels less than `gnus-level-subscribed', which
172should be less than this variable, are subscribed. Groups with
173levels from `gnus-level-subscribed' (exclusive) upto this
174variable (inclusive) are unsubscribed. See also
e1b9db1a 175`gnus-level-zombie', `gnus-level-killed' and the Info node `(gnus)Group
60b0b668 176Levels' for details.")
eec82323 177
16409b0b
GM
178(defconst gnus-level-zombie 8
179 "Groups with this level are zombie groups.")
eec82323 180
16409b0b
GM
181(defconst gnus-level-killed 9
182 "Groups with this level are killed.")
eec82323
LMI
183
184(defcustom gnus-level-default-subscribed 3
185 "*New subscribed groups will be subscribed at this level."
186 :group 'gnus-group-levels
187 :type 'integer)
188
189(defcustom gnus-level-default-unsubscribed 6
190 "*New unsubscribed groups will be unsubscribed at this level."
191 :group 'gnus-group-levels
192 :type 'integer)
193
194(defcustom gnus-activate-level (1+ gnus-level-subscribed)
195 "*Groups higher than this level won't be activated on startup.
196Setting this variable to something low might save lots of time when
197you have many groups that you aren't interested in."
198 :group 'gnus-group-levels
199 :type 'integer)
200
201(defcustom gnus-activate-foreign-newsgroups 4
202 "*If nil, Gnus will not check foreign newsgroups at startup.
203If it is non-nil, it should be a number between one and nine. Foreign
204newsgroups that have a level lower or equal to this number will be
205activated on startup. For instance, if you want to active all
206subscribed newsgroups, but not the rest, you'd set this variable to
207`gnus-level-subscribed'.
208
209If you subscribe to lots of newsgroups from different servers, startup
210might take a while. By setting this variable to nil, you'll save time,
211but you won't be told how many unread articles there are in the
212groups."
213 :group 'gnus-group-levels
6748645f
LMI
214 :type '(choice integer
215 (const :tag "none" nil)))
eec82323 216
16409b0b
GM
217(defcustom gnus-read-newsrc-file t
218 "*Non-nil means that Gnus will read the `.newsrc' file.
219Gnus always reads its own startup file, which is called
220\".newsrc.eld\". The file called \".newsrc\" is in a format that can
221be readily understood by other newsreaders. If you don't plan on
222using other newsreaders, set this variable to nil to save some time on
223entry."
0e5f03c1 224 :version "21.1"
16409b0b
GM
225 :group 'gnus-newsrc
226 :type 'boolean)
227
eec82323
LMI
228(defcustom gnus-save-newsrc-file t
229 "*Non-nil means that Gnus will save the `.newsrc' file.
230Gnus always saves its own startup file, which is called
231\".newsrc.eld\". The file called \".newsrc\" is in a format that can
232be readily understood by other newsreaders. If you don't plan on
233using other newsreaders, set this variable to nil to save some time on
234exit."
235 :group 'gnus-newsrc
236 :type 'boolean)
237
238(defcustom gnus-save-killed-list t
239 "*If non-nil, save the list of killed groups to the startup file.
240If you set this variable to nil, you'll save both time (when starting
241and quitting) and space (both memory and disk), but it will also mean
242that Gnus has no record of which groups are new and which are old, so
243the automatic new newsgroups subscription methods become meaningless.
244
245You should always set `gnus-check-new-newsgroups' to `ask-server' or
246nil if you set this variable to nil.
247
248This variable can also be a regexp. In that case, all groups that do
249not match this regexp will be removed before saving the list."
250 :group 'gnus-newsrc
23f87bed
MB
251 :type '(radio (sexp :format "Non-nil\n"
252 :match (lambda (widget value)
253 (and value (not (stringp value))))
254 :value t)
255 (const nil)
ad136a7c 256 regexp))
eec82323
LMI
257
258(defcustom gnus-ignored-newsgroups
16409b0b
GM
259 (mapconcat 'identity
260 '("^to\\." ; not "real" groups
23f87bed 261 "^[0-9. \t]+\\( \\|$\\)" ; all digits in name
bdaa75c7 262 "^[\"][\"#'()]" ; bogus characters
16409b0b
GM
263 )
264 "\\|")
6748645f 265 "*A regexp to match uninteresting newsgroups in the active file.
eec82323
LMI
266Any lines in the active file matching this regular expression are
267removed from the newsgroup list before anything else is done to it,
268thus making them effectively non-existent."
269 :group 'gnus-group-new
270 :type 'regexp)
271
272(defcustom gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
23f87bed 273 "*Function(s) called with a group name when new group is detected.
eec82323
LMI
274A few pre-made functions are supplied: `gnus-subscribe-randomly'
275inserts new groups at the beginning of the list of groups;
276`gnus-subscribe-alphabetically' inserts new groups in strict
277alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
278in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
279for your decision; `gnus-subscribe-killed' kills all new groups;
16409b0b
GM
280`gnus-subscribe-zombies' will make all new groups into zombies;
281`gnus-subscribe-topics' will enter groups into the topics that
282claim them."
eec82323
LMI
283 :group 'gnus-group-new
284 :type '(radio (function-item gnus-subscribe-randomly)
285 (function-item gnus-subscribe-alphabetically)
286 (function-item gnus-subscribe-hierarchically)
287 (function-item gnus-subscribe-interactively)
288 (function-item gnus-subscribe-killed)
289 (function-item gnus-subscribe-zombies)
16409b0b 290 (function-item gnus-subscribe-topics)
23f87bed
MB
291 function
292 (repeat function)))
293
294(defcustom gnus-subscribe-newsgroup-hooks nil
295 "*Hooks run after you subscribe to a new group.
296The hooks will be called with new group's name as argument."
bf247b6e 297 :version "22.1"
23f87bed
MB
298 :group 'gnus-group-new
299 :type 'hook)
eec82323 300
eec82323
LMI
301(defcustom gnus-subscribe-options-newsgroup-method
302 'gnus-subscribe-alphabetically
23f87bed 303 "*Function(s) called to subscribe newsgroups mentioned on \"options -n\" lines.
eec82323
LMI
304If, for instance, you want to subscribe to all newsgroups in the
305\"no\" and \"alt\" hierarchies, you'd put the following in your
306.newsrc file:
307
308options -n no.all alt.all
309
763bb2d4
MB
310Gnus will then subscribe all new newsgroups in these hierarchies
311with the subscription method in this variable."
eec82323
LMI
312 :group 'gnus-group-new
313 :type '(radio (function-item gnus-subscribe-randomly)
314 (function-item gnus-subscribe-alphabetically)
315 (function-item gnus-subscribe-hierarchically)
316 (function-item gnus-subscribe-interactively)
317 (function-item gnus-subscribe-killed)
318 (function-item gnus-subscribe-zombies)
23f87bed
MB
319 (function-item gnus-subscribe-topics)
320 function
321 (repeat function)))
eec82323
LMI
322
323(defcustom gnus-subscribe-hierarchical-interactive nil
324 "*If non-nil, Gnus will offer to subscribe hierarchically.
325When a new hierarchy appears, Gnus will ask the user:
326
327'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
328
329If the user pressed `d', Gnus will descend the hierarchy, `y' will
330subscribe to all newsgroups in the hierarchy and `s' will skip this
331hierarchy in its entirety."
332 :group 'gnus-group-new
333 :type 'boolean)
334
7410c270
G
335(defcustom gnus-auto-subscribed-categories '(mail post-mail)
336 "*New groups from methods of these categories will be subscribed automatically.
337Note that this variable only deals with new groups. It has no
338effect whatsoever on old groups. The default is to automatically
339subscribe all groups from mail-like backends."
340 :version "24.1"
341 :group 'gnus-group-new
342 :type '(repeat symbol))
343
eec82323 344(defcustom gnus-auto-subscribed-groups
7410c270 345 "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl\\|^nnmaildir\\|^nnimap"
eec82323
LMI
346 "*All new groups that match this regexp will be subscribed automatically.
347Note that this variable only deals with new groups. It has no effect
348whatsoever on old groups.
349
350New groups that match this regexp will not be handled by
351`gnus-subscribe-newsgroup-method'. Instead, they will
352be subscribed using `gnus-subscribe-options-newsgroup-method'."
353 :group 'gnus-group-new
354 :type 'regexp)
355
356(defcustom gnus-options-subscribe nil
357 "*All new groups matching this regexp will be subscribed unconditionally.
de768922 358Note that this variable deals only with new newsgroups. This variable
eec82323
LMI
359does not affect old newsgroups.
360
361New groups that match this regexp will not be handled by
362`gnus-subscribe-newsgroup-method'. Instead, they will
363be subscribed using `gnus-subscribe-options-newsgroup-method'."
364 :group 'gnus-group-new
365 :type '(choice regexp
366 (const :tag "none" nil)))
367
368(defcustom gnus-options-not-subscribe nil
369 "*All new groups matching this regexp will be ignored.
de768922 370Note that this variable deals only with new newsgroups. This variable
eec82323
LMI
371does not affect old (already subscribed) newsgroups."
372 :group 'gnus-group-new
373 :type '(choice regexp
374 (const :tag "none" nil)))
375
376(defcustom gnus-modtime-botch nil
377 "*Non-nil means .newsrc should be deleted prior to save.
378Its use is due to the bogus appearance that .newsrc was modified on
379disc."
380 :group 'gnus-newsrc
381 :type 'boolean)
382
383(defcustom gnus-check-bogus-groups-hook nil
384 "A hook run after removing bogus groups."
385 :group 'gnus-start-server
386 :type 'hook)
387
388(defcustom gnus-startup-hook nil
389 "A hook called at startup.
390This hook is called after Gnus is connected to the NNTP server."
391 :group 'gnus-start
392 :type 'hook)
393
6748645f 394(defcustom gnus-before-startup-hook nil
e3e955fe 395 "A hook called before startup.
6748645f
LMI
396This hook is called as the first thing when Gnus is started."
397 :group 'gnus-start
398 :type 'hook)
399
eec82323
LMI
400(defcustom gnus-started-hook nil
401 "A hook called as the last thing after startup."
402 :group 'gnus-start
403 :type 'hook)
404
b1ae92ba 405(defcustom gnus-setup-news-hook nil
6748645f
LMI
406 "A hook after reading the .newsrc file, but before generating the buffer."
407 :group 'gnus-start
408 :type 'hook)
409
23f87bed
MB
410(defcustom gnus-get-top-new-news-hook nil
411 "A hook run just before Gnus checks for new news globally."
bf247b6e 412 :version "22.1"
23f87bed
MB
413 :group 'gnus-group-new
414 :type 'hook)
415
eec82323
LMI
416(defcustom gnus-get-new-news-hook nil
417 "A hook run just before Gnus checks for new news."
418 :group 'gnus-group-new
419 :type 'hook)
420
421(defcustom gnus-after-getting-new-news-hook
b1ae92ba 422 '(gnus-display-time-event-handler)
16409b0b 423 "*A hook run after Gnus checks for new news when Gnus is already running."
3431395c 424 :version "24.1"
eec82323
LMI
425 :group 'gnus-group-new
426 :type 'hook)
427
23f87bed
MB
428(defcustom gnus-read-newsrc-el-hook nil
429 "A hook called after reading the newsrc.eld? file."
430 :group 'gnus-newsrc
431 :type 'hook)
432
eec82323
LMI
433(defcustom gnus-save-newsrc-hook nil
434 "A hook called before saving any of the newsrc files."
435 :group 'gnus-newsrc
436 :type 'hook)
437
438(defcustom gnus-save-quick-newsrc-hook nil
439 "A hook called just before saving the quick newsrc file.
440Can be used to turn version control on or off."
441 :group 'gnus-newsrc
442 :type 'hook)
443
444(defcustom gnus-save-standard-newsrc-hook nil
445 "A hook called just before saving the standard newsrc file.
446Can be used to turn version control on or off."
447 :group 'gnus-newsrc
448 :type 'hook)
449
23f87bed
MB
450(defcustom gnus-group-mode-hook nil
451 "Hook for Gnus group mode."
452 :group 'gnus-group-various
453 :options '(gnus-topic-mode)
454 :type 'hook)
455
6748645f 456(defcustom gnus-always-read-dribble-file nil
16409b0b 457 "Unconditionally read the dribble file."
6748645f
LMI
458 :group 'gnus-newsrc
459 :type 'boolean)
460
2d2820a4
GM
461;;; Internal variables
462
eb75e087
DL
463;; Fixme: deal with old emacs-mule when mm-universal-coding-system is
464;; utf-8-emacs.
39d74434
SZ
465(defvar gnus-ding-file-coding-system mm-universal-coding-system
466 "Coding system for ding file.")
eec82323
LMI
467
468(defvar gnus-newsrc-file-version nil)
469(defvar gnus-override-subscribe-method nil)
470(defvar gnus-dribble-buffer nil)
471(defvar gnus-newsrc-options nil
472 "Options line in the .newsrc file.")
473
474(defvar gnus-newsrc-options-n nil
475 "List of regexps representing groups to be subscribed/ignored unconditionally.")
476
477(defvar gnus-newsrc-last-checked-date nil
478 "Date Gnus last asked server for new newsgroups.")
479
480(defvar gnus-current-startup-file nil
481 "Startup file for the current host.")
482
483;; Byte-compiler warning.
484(defvar gnus-group-line-format)
485
486;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
487(defvar gnus-init-inhibit nil)
488(defun gnus-read-init-file (&optional inhibit-next)
489 ;; Don't load .gnus if the -q option was used.
490 (when init-file-user
491 (if gnus-init-inhibit
492 (setq gnus-init-inhibit nil)
493 (setq gnus-init-inhibit inhibit-next)
39d74434
SZ
494 (dolist (file (list gnus-site-init-file gnus-init-file))
495 (when (and file
496 (locate-library file))
497 (if (or debug-on-error debug-on-quit)
498 (load file nil t)
499 (condition-case var
500 (load file nil t)
501 (error
23f87bed 502 (error "Error in %s: %s" file (cadr var))))))))))
eec82323
LMI
503
504;; For subscribing new newsgroup
505
506(defun gnus-subscribe-hierarchical-interactive (groups)
507 (let ((groups (sort groups 'string<))
01c52d31 508 prefixes prefix start ans group starts real-group)
eec82323
LMI
509 (while groups
510 (setq prefixes (list "^"))
511 (while (and groups prefixes)
01c52d31
MB
512 (while (not (string-match (car prefixes)
513 (gnus-group-real-name (car groups))))
eec82323
LMI
514 (setq prefixes (cdr prefixes)))
515 (setq prefix (car prefixes))
516 (setq start (1- (length prefix)))
01c52d31
MB
517 (if (and (string-match "[^\\.]\\." (gnus-group-real-name (car groups))
518 start)
eec82323
LMI
519 (cdr groups)
520 (setq prefix
01c52d31
MB
521 (concat "^" (substring
522 (gnus-group-real-name (car groups))
523 0 (match-end 0))))
524 (string-match prefix (gnus-group-real-name (cadr groups))))
eec82323
LMI
525 (progn
526 (push prefix prefixes)
527 (message "Descend hierarchy %s? ([y]nsq): "
528 (substring prefix 1 (1- (length prefix))))
6748645f
LMI
529 (while (not (memq (setq ans (read-char-exclusive))
530 '(?y ?\n ?\r ?n ?s ?q)))
eec82323
LMI
531 (ding)
532 (message "Descend hierarchy %s? ([y]nsq): "
533 (substring prefix 1 (1- (length prefix)))))
534 (cond ((= ans ?n)
535 (while (and groups
01c52d31
MB
536 (setq group (car groups)
537 real-group (gnus-group-real-name group))
538 (string-match prefix real-group))
eec82323
LMI
539 (push group gnus-killed-list)
540 (gnus-sethash group group gnus-killed-hashtb)
541 (setq groups (cdr groups)))
542 (setq starts (cdr starts)))
543 ((= ans ?s)
544 (while (and groups
01c52d31
MB
545 (setq group (car groups)
546 real-group (gnus-group-real-name group))
547 (string-match prefix real-group))
eec82323
LMI
548 (gnus-sethash group group gnus-killed-hashtb)
549 (gnus-subscribe-alphabetically (car groups))
550 (setq groups (cdr groups)))
551 (setq starts (cdr starts)))
552 ((= ans ?q)
553 (while groups
554 (setq group (car groups))
555 (push group gnus-killed-list)
556 (gnus-sethash group group gnus-killed-hashtb)
557 (setq groups (cdr groups))))
558 (t nil)))
559 (message "Subscribe %s? ([n]yq)" (car groups))
6748645f
LMI
560 (while (not (memq (setq ans (read-char-exclusive))
561 '(?y ?\n ?\r ?q ?n)))
eec82323
LMI
562 (ding)
563 (message "Subscribe %s? ([n]yq)" (car groups)))
564 (setq group (car groups))
565 (cond ((= ans ?y)
566 (gnus-subscribe-alphabetically (car groups))
567 (gnus-sethash group group gnus-killed-hashtb))
568 ((= ans ?q)
569 (while groups
570 (setq group (car groups))
571 (push group gnus-killed-list)
572 (gnus-sethash group group gnus-killed-hashtb)
573 (setq groups (cdr groups))))
574 (t
575 (push group gnus-killed-list)
576 (gnus-sethash group group gnus-killed-hashtb)))
577 (setq groups (cdr groups)))))))
578
579(defun gnus-subscribe-randomly (newsgroup)
580 "Subscribe new NEWSGROUP by making it the first newsgroup."
581 (gnus-subscribe-newsgroup newsgroup))
582
583(defun gnus-subscribe-alphabetically (newgroup)
23f87bed 584 "Subscribe new NEWGROUP and insert it in alphabetical order."
eec82323
LMI
585 (let ((groups (cdr gnus-newsrc-alist))
586 before)
587 (while (and (not before) groups)
588 (if (string< newgroup (caar groups))
589 (setq before (caar groups))
590 (setq groups (cdr groups))))
591 (gnus-subscribe-newsgroup newgroup before)))
592
593(defun gnus-subscribe-hierarchically (newgroup)
23f87bed 594 "Subscribe new NEWGROUP and insert it in hierarchical newsgroup order."
eec82323 595 ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
20a673b2 596 (with-current-buffer (nnheader-find-file-noselect gnus-current-startup-file)
23f87bed
MB
597 (prog1
598 (let ((groupkey newgroup) before)
599 (while (and (not before) groupkey)
600 (goto-char (point-min))
601 (let ((groupkey-re
602 (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
603 (while (and (re-search-forward groupkey-re nil t)
604 (progn
605 (setq before (match-string 1))
606 (string< before newgroup)))))
607 ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
608 (setq groupkey
609 (when (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
610 (substring groupkey (match-beginning 1) (match-end 1)))))
611 (gnus-subscribe-newsgroup newgroup before))
612 (kill-buffer (current-buffer)))))
eec82323
LMI
613
614(defun gnus-subscribe-interactively (group)
615 "Subscribe the new GROUP interactively.
616It is inserted in hierarchical newsgroup order if subscribed. If not,
617it is killed."
66841476 618 (if (gnus-y-or-n-p (format "Subscribe new newsgroup %s? " group))
eec82323
LMI
619 (gnus-subscribe-hierarchically group)
620 (push group gnus-killed-list)))
621
622(defun gnus-subscribe-zombies (group)
623 "Make the new GROUP into a zombie group."
624 (push group gnus-zombie-list))
625
626(defun gnus-subscribe-killed (group)
627 "Make the new GROUP a killed group."
628 (push group gnus-killed-list))
629
630(defun gnus-subscribe-newsgroup (newsgroup &optional next)
631 "Subscribe new NEWSGROUP.
de768922 632If NEXT is non-nil, it is inserted before NEXT. Otherwise it is made
eec82323
LMI
633the first newsgroup."
634 (save-excursion
635 (goto-char (point-min))
636 ;; We subscribe the group by changing its level to `subscribed'.
637 (gnus-group-change-level
638 newsgroup gnus-level-default-subscribed
01c52d31 639 gnus-level-killed (gnus-group-entry (or next "dummy.group")))
549c9aed 640 (gnus-request-update-group-status newsgroup 'subscribe)
23f87bed
MB
641 (gnus-message 5 "Subscribe newsgroup: %s" newsgroup)
642 (run-hook-with-args 'gnus-subscribe-newsgroup-hooks newsgroup)
643 t))
eec82323
LMI
644
645(defun gnus-read-active-file-p ()
646 "Say whether the active file has been read from `gnus-select-method'."
647 (memq gnus-select-method gnus-have-read-active-file))
648
649;;; General various misc type functions.
650
651;; Silence byte-compiler.
9efa445f
DN
652(defvar gnus-current-headers)
653(defvar gnus-thread-indent-array)
654(defvar gnus-newsgroup-name)
655(defvar gnus-newsgroup-headers)
656(defvar gnus-group-list-mode)
657(defvar gnus-group-mark-positions)
658(defvar gnus-newsgroup-data)
659(defvar gnus-newsgroup-unreads)
660(defvar nnoo-state-alist)
661(defvar gnus-current-select-method)
662(defvar mail-sources)
663(defvar nnmail-scan-directory-mail-source-once)
664(defvar nnmail-split-history)
665(defvar nnmail-spool-file)
23f87bed
MB
666
667(defun gnus-close-all-servers ()
668 "Close all servers."
669 (interactive)
670 (dolist (server gnus-opened-servers)
671 (gnus-close-server (car server))))
6748645f 672
eec82323
LMI
673(defun gnus-clear-system ()
674 "Clear all variables and buffers."
675 ;; Clear Gnus variables.
23f87bed 676 (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
eec82323
LMI
677 (while variables
678 (set (car variables) nil)
679 (setq variables (cdr variables))))
680 ;; Clear other internal variables.
681 (setq gnus-list-of-killed-groups nil
682 gnus-have-read-active-file nil
23f87bed 683 gnus-agent-covered-methods nil
54506618
MB
684 gnus-agent-file-loading-local nil
685 gnus-agent-file-loading-cache nil
23f87bed 686 gnus-server-method-cache nil
eec82323
LMI
687 gnus-newsrc-alist nil
688 gnus-newsrc-hashtb nil
689 gnus-killed-list nil
690 gnus-zombie-list nil
691 gnus-killed-hashtb nil
692 gnus-active-hashtb nil
693 gnus-moderated-hashtb nil
694 gnus-description-hashtb nil
695 gnus-current-headers nil
696 gnus-thread-indent-array nil
697 gnus-newsgroup-headers nil
698 gnus-newsgroup-name nil
699 gnus-server-alist nil
700 gnus-group-list-mode nil
701 gnus-opened-servers nil
702 gnus-group-mark-positions nil
703 gnus-newsgroup-data nil
704 gnus-newsgroup-unreads nil
705 nnoo-state-alist nil
6748645f 706 gnus-current-select-method nil
16409b0b 707 nnmail-split-history nil
0617bb00 708 gnus-extended-servers nil
6748645f 709 gnus-ephemeral-servers nil)
eec82323
LMI
710 (gnus-shutdown 'gnus)
711 ;; Kill the startup file.
712 (and gnus-current-startup-file
713 (get-file-buffer gnus-current-startup-file)
714 (kill-buffer (get-file-buffer gnus-current-startup-file)))
715 ;; Clear the dribble buffer.
716 (gnus-dribble-clear)
717 ;; Kill global KILL file buffer.
718 (when (get-file-buffer (gnus-newsgroup-kill-file nil))
719 (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
720 (gnus-kill-buffer nntp-server-buffer)
721 ;; Kill Gnus buffers.
23f87bed
MB
722 (dolist (buffer (gnus-buffers))
723 (gnus-kill-buffer buffer))
eec82323
LMI
724 ;; Remove Gnus frames.
725 (gnus-kill-gnus-frames))
726
727(defun gnus-no-server-1 (&optional arg slave)
728 "Read network news.
3031d8b0
MB
729If ARG is a positive number, Gnus will use that as the startup
730level. If ARG is nil, Gnus will be started at level 2
731\(`gnus-level-default-subscribed' minus one). If ARG is non-nil
732and not a positive number, Gnus will prompt the user for the name
733of an NNTP server to use. As opposed to \\[gnus], this command
734will not connect to the local server."
eec82323
LMI
735 (interactive "P")
736 (let ((val (or arg (1- gnus-level-default-subscribed))))
737 (gnus val t slave)
738 (make-local-variable 'gnus-group-use-permanent-levels)
739 (setq gnus-group-use-permanent-levels val)))
740
741(defun gnus-1 (&optional arg dont-connect slave)
742 "Read network news.
743If ARG is non-nil and a positive number, Gnus will use that as the
de768922 744startup level. If ARG is non-nil and not a positive number, Gnus will
eec82323
LMI
745prompt the user for the name of an NNTP server to use."
746 (interactive "P")
747
6748645f 748 (if (gnus-alive-p)
eec82323
LMI
749 (progn
750 (switch-to-buffer gnus-group-buffer)
751 (gnus-group-get-new-news
752 (and (numberp arg)
753 (> arg 0)
754 (max (car gnus-group-list-mode) arg))))
755
eec82323 756 (gnus-clear-system)
6748645f
LMI
757 (gnus-splash)
758 (gnus-run-hooks 'gnus-before-startup-hook)
eec82323 759 (nnheader-init-server-buffer)
eec82323 760 (setq gnus-slave slave)
a8151ef7 761 (gnus-read-init-file)
01c52d31
MB
762
763 ;; Add "native" to gnus-predefined-server-alist just to have a
764 ;; name for the native select method.
765 (when gnus-select-method
766 (push (cons "native" gnus-select-method)
767 gnus-predefined-server-alist))
c9fc72fa 768
23f87bed
MB
769 (if gnus-agent
770 (gnus-agentize))
eec82323 771
eec82323
LMI
772 (let ((level (and (numberp arg) (> arg 0) arg))
773 did-connect)
774 (unwind-protect
775 (progn
776 (unless dont-connect
777 (setq did-connect
778 (gnus-start-news-server (and arg (not level))))))
779 (if (and (not dont-connect)
780 (not did-connect))
7410c270 781 ;; Couldn't connect to the server, so bail out.
eec82323 782 (gnus-group-quit)
6748645f 783 (gnus-run-hooks 'gnus-startup-hook)
eec82323
LMI
784 ;; Find the current startup file name.
785 (setq gnus-current-startup-file
786 (gnus-make-newsrc-file gnus-startup-file))
787
788 ;; Read the dribble file.
789 (when (or gnus-slave gnus-use-dribble-file)
790 (gnus-dribble-read-file))
791
eec82323
LMI
792 ;; Do the actual startup.
793 (gnus-setup-news nil level dont-connect)
6748645f 794 (gnus-run-hooks 'gnus-setup-news-hook)
7410c270
G
795 (when gnus-agent
796 (gnus-request-create-group "queue" '(nndraft "")))
6748645f 797 (gnus-start-draft-setup)
eec82323
LMI
798 ;; Generate the group buffer.
799 (gnus-group-list-groups level)
800 (gnus-group-first-unread-group)
801 (gnus-configure-windows 'group)
802 (gnus-group-set-mode-line)
6748645f
LMI
803 (gnus-run-hooks 'gnus-started-hook))))))
804
805(defun gnus-start-draft-setup ()
806 "Make sure the draft group exists."
4e815860 807 (interactive)
6748645f 808 (gnus-request-create-group "drafts" '(nndraft ""))
01c52d31 809 (unless (gnus-group-entry "nndraft:drafts")
6748645f 810 (let ((gnus-level-default-subscribed 1))
7410c270
G
811 (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
812 (setcar (gnus-group-entry "nndraft:drafts") 0))
3031d8b0
MB
813 (unless (equal (gnus-group-get-parameter "nndraft:drafts" 'gnus-dummy t)
814 '((gnus-draft-mode)))
6748645f
LMI
815 (gnus-group-set-parameter
816 "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
eec82323 817
eec82323
LMI
818\f
819;;;
820;;; Dribble file
821;;;
822
823(defvar gnus-dribble-ignore nil)
824(defvar gnus-dribble-eval-file nil)
825
826(defun gnus-dribble-file-name ()
827 "Return the dribble file for the current .newsrc."
828 (concat
829 (if gnus-dribble-directory
830 (concat (file-name-as-directory gnus-dribble-directory)
831 (file-name-nondirectory gnus-current-startup-file))
832 gnus-current-startup-file)
833 "-dribble"))
834
835(defun gnus-dribble-enter (string)
836 "Enter STRING into the dribble buffer."
837 (when (and (not gnus-dribble-ignore)
838 gnus-dribble-buffer
839 (buffer-name gnus-dribble-buffer))
840 (let ((obuf (current-buffer)))
841 (set-buffer gnus-dribble-buffer)
842 (goto-char (point-max))
843 (insert string "\n")
23f87bed
MB
844 ;; This has been commented by Josh Huber <huber@alum.wpi.edu>
845 ;; It causes problems with both XEmacs and Emacs 21, and doesn't
846 ;; seem to be of much value. (FIXME: remove this after we make sure
847 ;; it's not needed).
848 ;; (set-window-point (get-buffer-window (current-buffer)) (point-max))
eec82323 849 (bury-buffer gnus-dribble-buffer)
20a673b2 850 (with-current-buffer gnus-group-buffer
6748645f 851 (gnus-group-set-mode-line))
eec82323
LMI
852 (set-buffer obuf))))
853
854(defun gnus-dribble-touch ()
855 "Touch the dribble buffer."
856 (gnus-dribble-enter ""))
857
858(defun gnus-dribble-read-file ()
859 "Read the dribble file from disk."
860 (let ((dribble-file (gnus-dribble-file-name)))
8bff7c00
KY
861 (unless (file-exists-p (file-name-directory dribble-file))
862 (make-directory (file-name-directory dribble-file) t))
20a673b2
KY
863 (with-current-buffer (setq gnus-dribble-buffer
864 (gnus-get-buffer-create
865 (file-name-nondirectory dribble-file)))
58090a8d 866 (set (make-local-variable 'file-precious-flag) t)
eec82323
LMI
867 (erase-buffer)
868 (setq buffer-file-name dribble-file)
869 (auto-save-mode t)
16409b0b 870 (buffer-disable-undo)
eec82323
LMI
871 (bury-buffer (current-buffer))
872 (set-buffer-modified-p nil)
873 (let ((auto (make-auto-save-file-name))
874 (gnus-dribble-ignore t)
23f87bed 875 (purpose nil)
eec82323
LMI
876 modes)
877 (when (or (file-exists-p auto) (file-exists-p dribble-file))
878 ;; Load whichever file is newest -- the auto save file
879 ;; or the "real" file.
880 (if (file-newer-than-file-p auto dribble-file)
881 (nnheader-insert-file-contents auto)
882 (nnheader-insert-file-contents dribble-file))
883 (unless (zerop (buffer-size))
884 (set-buffer-modified-p t))
885 ;; Set the file modes to reflect the .newsrc file modes.
886 (save-buffer)
887 (when (and (file-exists-p gnus-current-startup-file)
a8151ef7 888 (file-exists-p dribble-file)
eec82323 889 (setq modes (file-modes gnus-current-startup-file)))
01c52d31 890 (gnus-set-file-modes dribble-file modes))
23f87bed
MB
891 (goto-char (point-min))
892 (when (search-forward "Gnus was exited on purpose" nil t)
893 (setq purpose t))
eec82323 894 ;; Possibly eval the file later.
6748645f
LMI
895 (when (or gnus-always-read-dribble-file
896 (gnus-y-or-n-p
23f87bed
MB
897 (if purpose
898 "Gnus exited on purpose without saving; read auto-save file anyway? "
899 "Gnus auto-save file exists. Do you want to read it? ")))
eec82323
LMI
900 (setq gnus-dribble-eval-file t)))))))
901
902(defun gnus-dribble-eval-file ()
903 (when gnus-dribble-eval-file
904 (setq gnus-dribble-eval-file nil)
905 (save-excursion
906 (let ((gnus-dribble-ignore t))
907 (set-buffer gnus-dribble-buffer)
908 (eval-buffer (current-buffer))))))
909
910(defun gnus-dribble-delete-file ()
911 (when (file-exists-p (gnus-dribble-file-name))
912 (delete-file (gnus-dribble-file-name)))
913 (when gnus-dribble-buffer
20a673b2 914 (with-current-buffer gnus-dribble-buffer
eec82323
LMI
915 (let ((auto (make-auto-save-file-name)))
916 (when (file-exists-p auto)
917 (delete-file auto))
918 (erase-buffer)
919 (set-buffer-modified-p nil)))))
920
921(defun gnus-dribble-save ()
922 (when (and gnus-dribble-buffer
923 (buffer-name gnus-dribble-buffer))
20a673b2 924 (with-current-buffer gnus-dribble-buffer
eec82323
LMI
925 (save-buffer))))
926
927(defun gnus-dribble-clear ()
928 (when (gnus-buffer-exists-p gnus-dribble-buffer)
20a673b2 929 (with-current-buffer gnus-dribble-buffer
eec82323
LMI
930 (erase-buffer)
931 (set-buffer-modified-p nil)
932 (setq buffer-saved-size (buffer-size)))))
933
934\f
935;;;
936;;; Active & Newsrc File Handling
937;;;
938
939(defun gnus-setup-news (&optional rawfile level dont-connect)
940 "Setup news information.
941If RAWFILE is non-nil, the .newsrc file will also be read.
942If LEVEL is non-nil, the news will be set up at level LEVEL."
16409b0b
GM
943 (require 'nnmail)
944 (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
945 ;; Binding this variable will inhibit multiple fetchings
946 ;; of the same mail source.
947 (nnmail-fetched-sources (list t)))
eec82323
LMI
948
949 (when init
950 ;; Clear some variables to re-initialize news information.
951 (setq gnus-newsrc-alist nil
952 gnus-active-hashtb nil)
953 ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
954 (gnus-read-newsrc-file rawfile))
955
6748645f 956 ;; Make sure the archive server is available to all and sundry.
01c52d31
MB
957 (let ((method (or (and (stringp gnus-message-archive-method)
958 (gnus-server-to-method
959 gnus-message-archive-method))
960 gnus-message-archive-method)))
961 ;; Check whether the archive method is writable.
962 (unless (or (not method)
963 (stringp method)
964 (memq 'respool (assoc (format "%s" (car method))
965 gnus-valid-select-methods)))
966 (setq method "archive")) ;; The default.
967 (when (stringp method)
968 (setq method `(nnfolder
969 ,method
970 (nnfolder-directory
971 ,(nnheader-concat message-directory method))
972 (nnfolder-active-file
973 ,(nnheader-concat message-directory
974 (concat method "/active")))
975 (nnfolder-get-new-mail nil)
976 (nnfolder-inhibit-expiry t))))
977 (if (assoc "archive" gnus-server-alist)
978 (when gnus-update-message-archive-method
979 (if method
980 (setcdr (assoc "archive" gnus-server-alist) method)
981 (setq gnus-server-alist (delq (assoc "archive" gnus-server-alist)
982 gnus-server-alist))))
983 (when method
984 (push (cons "archive" method) gnus-server-alist))))
eec82323
LMI
985
986 ;; If we don't read the complete active file, we fill in the
987 ;; hashtb here.
988 (when (or (null gnus-read-active-file)
989 (eq gnus-read-active-file 'some))
990 (gnus-update-active-hashtb-from-killed))
eec82323
LMI
991 (unless gnus-active-hashtb
992 (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
eec82323
LMI
993 ;; Initialize the cache.
994 (when gnus-use-cache
995 (gnus-cache-open))
996
997 ;; Possibly eval the dribble file.
998 (and init
999 (or gnus-use-dribble-file gnus-slave)
1000 (gnus-dribble-eval-file))
1001
1002 ;; Slave Gnusii should then clear the dribble buffer.
1003 (when (and init gnus-slave)
1004 (gnus-dribble-clear))
1005
1006 (gnus-update-format-specifications)
1007
1008 ;; See whether we need to read the description file.
1009 (when (and (boundp 'gnus-group-line-format)
23f87bed 1010 (stringp gnus-group-line-format)
a8151ef7
LMI
1011 (let ((case-fold-search nil))
1012 (string-match "%[-,0-9]*D" gnus-group-line-format))
eec82323
LMI
1013 (not gnus-description-hashtb)
1014 (not dont-connect)
1015 gnus-read-active-file)
1016 (gnus-read-all-descriptions-files))
1017
1018 ;; Find new newsgroups and treat them.
1019 (when (and init gnus-check-new-newsgroups (not level)
1020 (gnus-check-server gnus-select-method)
6748645f
LMI
1021 (not gnus-slave)
1022 gnus-plugged)
eec82323
LMI
1023 (gnus-find-new-newsgroups))
1024
23f87bed
MB
1025 ;; Check and remove bogus newsgroups.
1026 (when (and init gnus-check-bogus-newsgroups
1027 gnus-read-active-file (not level)
1028 (gnus-server-opened gnus-select-method))
1029 (gnus-check-bogus-newsgroups))
1030
eec82323
LMI
1031 ;; Read any slave files.
1032 (gnus-master-read-slave-newsrc)
1033
1034 ;; Find the number of unread articles in each non-dead group.
1035 (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
23f87bed
MB
1036 (gnus-get-unread-articles level))))
1037
1038(defun gnus-call-subscribe-functions (method group)
1039 "Call METHOD to subscribe GROUP.
1040If no function returns `non-nil', call `gnus-subscribe-zombies'."
1041 (unless (cond
1042 ((functionp method)
1043 (funcall method group))
1044 ((listp method)
1045 (catch 'found
1046 (dolist (func method)
1047 (if (funcall func group)
1048 (throw 'found t)))
1049 nil))
1050 (t nil))
1051 (gnus-subscribe-zombies group)))
eec82323
LMI
1052
1053(defun gnus-find-new-newsgroups (&optional arg)
1054 "Search for new newsgroups and add them.
16409b0b 1055Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
eec82323 1056The `-n' option line from .newsrc is respected.
6748645f
LMI
1057
1058With 1 C-u, use the `ask-server' method to query the server for new
1059groups.
1060With 2 C-u's, use most complete method possible to query the server
1061for new groups, and subscribe the new groups as zombies."
1062 (interactive "p")
1063 (let* ((gnus-subscribe-newsgroup-method
1064 gnus-subscribe-newsgroup-method)
1065 (check (cond
16409b0b
GM
1066 ((or (and (= (or arg 1) 4)
1067 (not (listp gnus-check-new-newsgroups)))
1068 (null gnus-read-active-file)
1069 (eq gnus-read-active-file 'some))
1070 'ask-server)
1071 ((= (or arg 1) 16)
1072 (setq gnus-subscribe-newsgroup-method
1073 'gnus-subscribe-zombies)
1074 t)
1075 (t gnus-check-new-newsgroups))))
837ba704
JD
1076 (if (or (consp check)
1077 (eq check 'ask-server))
1078 ;; Ask the server for new groups.
1079 (gnus-ask-server-for-new-groups)
1080 ;; Go through the active hashtb and look for new groups.
1081 (let ((groups 0)
1082 group new-newsgroups)
1083 (gnus-message 5 "Looking for new newsgroups...")
1084 (unless gnus-have-read-active-file
1085 (gnus-read-active-file))
1086 (setq gnus-newsrc-last-checked-date (message-make-date))
1087 (unless gnus-killed-hashtb
1088 (gnus-make-hashtable-from-killed))
1089 ;; Go though every newsgroup in `gnus-active-hashtb' and compare
1090 ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
1091 (mapatoms
1092 (lambda (sym)
1093 (if (or (null (setq group (symbol-name sym)))
1094 (not (boundp sym))
1095 (null (symbol-value sym))
1096 (gnus-gethash group gnus-killed-hashtb)
1097 (gnus-gethash group gnus-newsrc-hashtb))
1098 ()
1099 (let ((do-sub (gnus-matches-options-n group)))
1100 (cond
1101 ((eq do-sub 'subscribe)
1102 (setq groups (1+ groups))
1103 (gnus-sethash group group gnus-killed-hashtb)
1104 (gnus-call-subscribe-functions
1105 gnus-subscribe-options-newsgroup-method group))
1106 ((eq do-sub 'ignore)
1107 nil)
1108 (t
1109 (setq groups (1+ groups))
1110 (gnus-sethash group group gnus-killed-hashtb)
1111 (if gnus-subscribe-hierarchical-interactive
1112 (push group new-newsgroups)
1113 (gnus-call-subscribe-functions
1114 gnus-subscribe-newsgroup-method group)))))))
1115 gnus-active-hashtb)
1116 (when new-newsgroups
1117 (gnus-subscribe-hierarchical-interactive new-newsgroups))
1118 (if (> groups 0)
1119 (gnus-message 5 "%d new newsgroup%s arrived."
1120 groups (if (> groups 1) "s have" " has"))
9310f19d
LMI
1121 (gnus-message 5 "No new newsgroups."))
1122 groups))))
eec82323
LMI
1123
1124(defun gnus-matches-options-n (group)
1125 ;; Returns `subscribe' if the group is to be unconditionally
1126 ;; subscribed, `ignore' if it is to be ignored, and nil if there is
1127 ;; no match for the group.
1128
1129 ;; First we check the two user variables.
1130 (cond
1131 ((and gnus-options-subscribe
1132 (string-match gnus-options-subscribe group))
1133 'subscribe)
7410c270
G
1134 ((let ((do-subscribe nil))
1135 (dolist (category gnus-auto-subscribed-categories)
1136 (when (gnus-member-of-valid category group)
1137 (setq do-subscribe t)))
1138 do-subscribe)
1139 'subscribe)
eec82323
LMI
1140 ((and gnus-auto-subscribed-groups
1141 (string-match gnus-auto-subscribed-groups group))
1142 'subscribe)
1143 ((and gnus-options-not-subscribe
1144 (string-match gnus-options-not-subscribe group))
1145 'ignore)
1146 ;; Then we go through the list that was retrieved from the .newsrc
1147 ;; file. This list has elements on the form
1148 ;; `(REGEXP . {ignore,subscribe})'. The first match found (the list
1149 ;; is in the reverse order of the options line) is returned.
1150 (t
1151 (let ((regs gnus-newsrc-options-n))
1152 (while (and regs
1153 (not (string-match (caar regs) group)))
1154 (setq regs (cdr regs)))
1155 (and regs (cdar regs))))))
1156
1157(defun gnus-ask-server-for-new-groups ()
7c972da0
GM
1158 (let* ((new-date (message-make-date))
1159 (date (or gnus-newsrc-last-checked-date new-date))
eec82323
LMI
1160 (methods (cons gnus-select-method
1161 (nconc
1162 (when (gnus-archive-server-wanted-p)
1163 (list "archive"))
1164 (append
1165 (and (consp gnus-check-new-newsgroups)
1166 gnus-check-new-newsgroups)
1167 gnus-secondary-select-methods))))
1168 (groups 0)
eec82323
LMI
1169 group new-newsgroups got-new method hashtb
1170 gnus-override-subscribe-method)
6748645f
LMI
1171 (unless gnus-killed-hashtb
1172 (gnus-make-hashtable-from-killed))
eec82323
LMI
1173 ;; Go through both primary and secondary select methods and
1174 ;; request new newsgroups.
1175 (while (setq method (gnus-server-get-method nil (pop methods)))
6748645f
LMI
1176 (setq new-newsgroups nil
1177 gnus-override-subscribe-method method)
eec82323
LMI
1178 (when (and (gnus-check-server method)
1179 (gnus-request-newgroups date method))
1180 (save-excursion
6748645f
LMI
1181 (setq got-new t
1182 hashtb (gnus-make-hashtable 100))
eec82323
LMI
1183 (set-buffer nntp-server-buffer)
1184 ;; Enter all the new groups into a hashtable.
1185 (gnus-active-to-gnus-format method hashtb 'ignore))
1186 ;; Now all new groups from `method' are in `hashtb'.
1187 (mapatoms
1188 (lambda (group-sym)
1189 (if (or (null (setq group (symbol-name group-sym)))
1190 (not (boundp group-sym))
1191 (null (symbol-value group-sym))
1192 (gnus-gethash group gnus-newsrc-hashtb)
1193 (member group gnus-zombie-list)
1194 (member group gnus-killed-list))
1195 ;; The group is already known.
1196 ()
1197 ;; Make this group active.
1198 (when (symbol-value group-sym)
1199 (gnus-set-active group (symbol-value group-sym)))
1200 ;; Check whether we want it or not.
1201 (let ((do-sub (gnus-matches-options-n group)))
1202 (cond
1203 ((eq do-sub 'subscribe)
1204 (incf groups)
1205 (gnus-sethash group group gnus-killed-hashtb)
23f87bed
MB
1206 (gnus-call-subscribe-functions
1207 gnus-subscribe-options-newsgroup-method group))
eec82323
LMI
1208 ((eq do-sub 'ignore)
1209 nil)
1210 (t
1211 (incf groups)
1212 (gnus-sethash group group gnus-killed-hashtb)
1213 (if gnus-subscribe-hierarchical-interactive
1214 (push group new-newsgroups)
23f87bed
MB
1215 (gnus-call-subscribe-functions
1216 gnus-subscribe-newsgroup-method group)))))))
eec82323
LMI
1217 hashtb))
1218 (when new-newsgroups
1219 (gnus-subscribe-hierarchical-interactive new-newsgroups)))
16409b0b
GM
1220 (if (> groups 0)
1221 (gnus-message 5 "%d new newsgroup%s arrived"
1222 groups (if (> groups 1) "s have" " has"))
1223 (gnus-message 5 "No new newsgroups"))
eec82323
LMI
1224 (when got-new
1225 (setq gnus-newsrc-last-checked-date new-date))
9310f19d 1226 new-newsgroups))
eec82323 1227
16409b0b 1228(defun gnus-subscribe-group (group &optional previous method)
23f87bed 1229 "Subscribe GROUP and put it after PREVIOUS."
eec82323
LMI
1230 (gnus-group-change-level
1231 (if method
1232 (list t group gnus-level-default-subscribed nil nil method)
1233 group)
16409b0b
GM
1234 gnus-level-default-subscribed gnus-level-killed previous t)
1235 t)
eec82323
LMI
1236
1237;; `gnus-group-change-level' is the fundamental function for changing
1238;; subscription levels of newsgroups. This might mean just changing
1239;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1240;; again, which subscribes/unsubscribes a group, which is equally
1241;; trivial. Changing from 1-7 to 8-9 means that you kill a group, and
1242;; from 8-9 to 1-7 means that you remove the group from the list of
1243;; killed (or zombie) groups and add them to the (kinda) subscribed
1244;; groups. And last but not least, moving from 8 to 9 and 9 to 8,
1245;; which is trivial.
1246;; ENTRY can either be a string (newsgroup name) or a list (if
1247;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1248;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1249;; entries.
1250;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1251;; PREVIOUS is the group (in hashtb entry format) to insert this group
1252;; after.
1253(defun gnus-group-change-level (entry level &optional oldlevel
1254 previous fromkilled)
1255 (let (group info active num)
1256 ;; Glean what info we can from the arguments
1257 (if (consp entry)
1258 (if fromkilled (setq group (nth 1 entry))
1259 (setq group (car (nth 2 entry))))
1260 (setq group entry))
1261 (when (and (stringp entry)
1262 oldlevel
1263 (< oldlevel gnus-level-zombie))
01c52d31 1264 (setq entry (gnus-group-entry entry)))
eec82323
LMI
1265 (if (and (not oldlevel)
1266 (consp entry))
1267 (setq oldlevel (gnus-info-level (nth 2 entry)))
6748645f 1268 (setq oldlevel (or oldlevel gnus-level-killed)))
eec82323 1269 (when (stringp previous)
01c52d31 1270 (setq previous (gnus-group-entry previous)))
eec82323
LMI
1271
1272 (if (and (>= oldlevel gnus-level-zombie)
01c52d31 1273 (gnus-group-entry group))
eec82323
LMI
1274 ;; We are trying to subscribe a group that is already
1275 ;; subscribed.
1276 () ; Do nothing.
1277
1278 (unless (gnus-ephemeral-group-p group)
1279 (gnus-dribble-enter
1280 (format "(gnus-group-change-level %S %S %S %S %S)"
1281 group level oldlevel (car (nth 2 previous)) fromkilled)))
1282
1283 ;; Then we remove the newgroup from any old structures, if needed.
1284 ;; If the group was killed, we remove it from the killed or zombie
1285 ;; list. If not, and it is in fact going to be killed, we remove
1286 ;; it from the newsrc hash table and assoc.
1287 (cond
1288 ((>= oldlevel gnus-level-zombie)
23f87bed
MB
1289 ;; oldlevel could be wrong.
1290 (setq gnus-zombie-list (delete group gnus-zombie-list))
1291 (setq gnus-killed-list (delete group gnus-killed-list)))
eec82323
LMI
1292 (t
1293 (when (and (>= level gnus-level-zombie)
1294 entry)
1295 (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1296 (when (nth 3 entry)
01c52d31 1297 (setcdr (gnus-group-entry (car (nth 3 entry)))
eec82323
LMI
1298 (cdr entry)))
1299 (setcdr (cdr entry) (cdddr entry)))))
1300
1301 ;; Finally we enter (if needed) the list where it is supposed to
1302 ;; go, and change the subscription level. If it is to be killed,
1303 ;; we enter it into the killed or zombie list.
1304 (cond
1305 ((>= level gnus-level-zombie)
1306 ;; Remove from the hash table.
1307 (gnus-sethash group nil gnus-newsrc-hashtb)
1308 ;; We do not enter foreign groups into the list of dead
1309 ;; groups.
1310 (unless (gnus-group-foreign-p group)
1311 (if (= level gnus-level-zombie)
1312 (push group gnus-zombie-list)
23f87bed
MB
1313 (if (= oldlevel gnus-level-killed)
1314 ;; Remove from active hashtb.
1315 (unintern group gnus-active-hashtb)
1316 ;; Don't add it into killed-list if it was killed.
1317 (push group gnus-killed-list)))))
eec82323
LMI
1318 (t
1319 ;; If the list is to be entered into the newsrc assoc, and
1320 ;; it was killed, we have to create an entry in the newsrc
1321 ;; hashtb format and fix the pointers in the newsrc assoc.
1322 (if (< oldlevel gnus-level-zombie)
1323 ;; It was alive, and it is going to stay alive, so we
1324 ;; just change the level and don't change any pointers or
1325 ;; hash table entries.
1326 (setcar (cdaddr entry) level)
1327 (if (listp entry)
1328 (setq info (cdr entry)
1329 num (car entry))
1330 (setq active (gnus-active group))
1331 (setq num
1332 (if active (- (1+ (cdr active)) (car active)) t))
16409b0b
GM
1333 ;; Shorten the select method if possible, if we need to
1334 ;; store it at all (native groups).
1335 (let ((method (gnus-method-simplify
1336 (or gnus-override-subscribe-method
1337 (gnus-group-method group)))))
1338 (if method
1339 (setq info (list group level nil nil method))
1340 (setq info (list group level nil)))))
eec82323
LMI
1341 (unless previous
1342 (setq previous
1343 (let ((p gnus-newsrc-alist))
1344 (while (cddr p)
1345 (setq p (cdr p)))
1346 p)))
1347 (setq entry (cons info (cddr previous)))
1348 (if (cdr previous)
1349 (progn
1350 (setcdr (cdr previous) entry)
1351 (gnus-sethash group (cons num (cdr previous))
1352 gnus-newsrc-hashtb))
1353 (setcdr previous entry)
1354 (gnus-sethash group (cons num previous)
1355 gnus-newsrc-hashtb))
1356 (when (cdr entry)
01c52d31 1357 (setcdr (gnus-group-entry (caadr entry)) entry))
eec82323
LMI
1358 (gnus-dribble-enter
1359 (format
1360 "(gnus-group-set-info '%S)" info)))))
1361 (when gnus-group-change-level-function
a8151ef7
LMI
1362 (funcall gnus-group-change-level-function
1363 group level oldlevel previous)))))
eec82323
LMI
1364
1365(defun gnus-kill-newsgroup (newsgroup)
1366 "Obsolete function. Kills a newsgroup."
1367 (gnus-group-change-level
01c52d31 1368 (gnus-group-entry newsgroup) gnus-level-killed))
eec82323
LMI
1369
1370(defun gnus-check-bogus-newsgroups (&optional confirm)
1371 "Remove bogus newsgroups.
1372If CONFIRM is non-nil, the user has to confirm the deletion of every
1373newsgroup."
1374 (let ((newsrc (cdr gnus-newsrc-alist))
1375 bogus group entry info)
1376 (gnus-message 5 "Checking bogus newsgroups...")
1377 (unless (gnus-read-active-file-p)
1378 (gnus-read-active-file t))
1379 (when (gnus-read-active-file-p)
1380 ;; Find all bogus newsgroup that are subscribed.
1381 (while newsrc
1382 (setq info (pop newsrc)
1383 group (gnus-info-group info))
1384 (unless (or (gnus-active group) ; Active
23f87bed
MB
1385 (and (gnus-info-method info)
1386 (not (gnus-secondary-method-p
1387 (gnus-info-method info))))) ; Foreign
eec82323
LMI
1388 ;; Found a bogus newsgroup.
1389 (push group bogus)))
1390 (if confirm
1391 (map-y-or-n-p
a3f57c41 1392 (format "Remove bogus group %%s (of %d groups)? " (length bogus))
eec82323
LMI
1393 (lambda (group)
1394 ;; Remove all bogus subscribed groups by first killing them, and
1395 ;; then removing them from the list of killed groups.
01c52d31 1396 (when (setq entry (gnus-group-entry group))
eec82323
LMI
1397 (gnus-group-change-level entry gnus-level-killed)
1398 (setq gnus-killed-list (delete group gnus-killed-list))))
1399 bogus '("group" "groups" "remove"))
1400 (while (setq group (pop bogus))
1401 ;; Remove all bogus subscribed groups by first killing them, and
1402 ;; then removing them from the list of killed groups.
01c52d31 1403 (when (setq entry (gnus-group-entry group))
eec82323
LMI
1404 (gnus-group-change-level entry gnus-level-killed)
1405 (setq gnus-killed-list (delete group gnus-killed-list)))))
1406 ;; Then we remove all bogus groups from the list of killed and
1407 ;; zombie groups. They are removed without confirmation.
1408 (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1409 killed)
1410 (while dead-lists
1411 (setq killed (symbol-value (car dead-lists)))
1412 (while killed
1413 (unless (gnus-active (setq group (pop killed)))
1414 ;; The group is bogus.
1415 ;; !!!Slow as hell.
1416 (set (car dead-lists)
1417 (delete group (symbol-value (car dead-lists))))))
1418 (setq dead-lists (cdr dead-lists))))
6748645f 1419 (gnus-run-hooks 'gnus-check-bogus-groups-hook)
eec82323
LMI
1420 (gnus-message 5 "Checking bogus newsgroups...done"))))
1421
1422(defun gnus-check-duplicate-killed-groups ()
1423 "Remove duplicates from the list of killed groups."
1424 (interactive)
1425 (let ((killed gnus-killed-list))
1426 (while killed
1427 (gnus-message 9 "%d" (length killed))
1428 (setcdr killed (delete (car killed) (cdr killed)))
1429 (setq killed (cdr killed)))))
1430
1431;; We want to inline a function from gnus-cache, so we cheat here:
9efa445f 1432(defvar gnus-cache-active-hashtb)
eec82323 1433(eval-when-compile
eec82323
LMI
1434 (defun gnus-cache-possibly-alter-active (group active)
1435 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1436 (when gnus-cache-active-hashtb
1437 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
a8151ef7
LMI
1438 (when cache-active
1439 (when (< (car cache-active) (car active))
1440 (setcar active (car cache-active)))
1441 (when (> (cdr cache-active) (cdr active))
1442 (setcdr active (cdr cache-active))))))))
eec82323 1443
c15c8017
LMI
1444(defun gnus-activate-group (group &optional scan dont-check method
1445 dont-sub-check)
531e5812
MB
1446 "Check whether a group has been activated or not.
1447If SCAN, request a scan of that group as well."
eec82323
LMI
1448 (let ((method (or method (inline (gnus-find-method-for-group group))))
1449 active)
1450 (and (inline (gnus-check-server method))
1451 ;; We escape all bugs and quit here to make it possible to
1452 ;; continue if a group is so out-there that it reports bugs
1453 ;; and stuff.
1454 (progn
1455 (and scan
1456 (gnus-check-backend-function 'request-scan (car method))
1457 (gnus-request-scan group method))
1458 t)
23f87bed 1459 (if (or debug-on-error debug-on-quit)
c15c8017 1460 (inline (gnus-request-group group (or dont-sub-check dont-check)
286c4fc2
LMI
1461 method
1462 (gnus-get-info group)))
23f87bed 1463 (condition-case nil
c15c8017 1464 (inline (gnus-request-group group (or dont-sub-check dont-check)
286c4fc2
LMI
1465 method
1466 (gnus-get-info group)))
23f87bed
MB
1467 ;;(error nil)
1468 (quit
1469 (message "Quit activating %s" group)
1470 nil)))
1471 (unless dont-check
1472 (setq active (gnus-parse-active))
1473 ;; If there are no articles in the group, the GROUP
1474 ;; command may have responded with the `(0 . 0)'. We
1475 ;; ignore this if we already have an active entry
1476 ;; for the group.
01c52d31
MB
1477 (if (and (zerop (or (car active) 0))
1478 (zerop (or (cdr active) 0))
23f87bed
MB
1479 (gnus-active group))
1480 (gnus-active group)
1481
54506618
MB
1482 ;; If a cache is present, we may have to alter the active info.
1483 (when gnus-use-cache
1484 (inline (gnus-cache-possibly-alter-active
1485 group active)))
1486
1487 ;; If the agent is enabled, we may have to alter the active info.
1488 (when gnus-agent
1489 (gnus-agent-possibly-alter-active group active))
1490
23f87bed
MB
1491 (gnus-set-active group active)
1492 ;; Return the new active info.
1493 active)))))
eec82323
LMI
1494
1495(defun gnus-get-unread-articles-in-group (info active &optional update)
54506618 1496 (when (and info active)
eec82323
LMI
1497 ;; Allow the backend to update the info in the group.
1498 (when (and update
1499 (gnus-request-update-info
1500 info (inline (gnus-find-method-for-group
1501 (gnus-info-group info)))))
1502 (gnus-activate-group (gnus-info-group info) nil t))
6748645f 1503
9e928ac9 1504 ;; Allow backends to update marks,
5415d076 1505 (when gnus-propagate-marks
b1ae92ba
G
1506 (let ((method (inline (gnus-find-method-for-group
1507 (gnus-info-group info)))))
1508 (when (gnus-check-backend-function 'request-marks (car method))
1509 (gnus-request-marks info method))))
1510
eec82323
LMI
1511 (let* ((range (gnus-info-read info))
1512 (num 0))
54506618
MB
1513
1514 ;; These checks are present in gnus-activate-group but skipped
1515 ;; due to setting dont-check in the preceeding call.
1516
eec82323
LMI
1517 ;; If a cache is present, we may have to alter the active info.
1518 (when (and gnus-use-cache info)
1519 (inline (gnus-cache-possibly-alter-active
1520 (gnus-info-group info) active)))
23f87bed
MB
1521
1522 ;; If the agent is enabled, we may have to alter the active info.
1523 (when (and gnus-agent info)
54506618 1524 (gnus-agent-possibly-alter-active (gnus-info-group info) active info))
23f87bed 1525
eec82323
LMI
1526 ;; Modify the list of read articles according to what articles
1527 ;; are available; then tally the unread articles and add the
1528 ;; number to the group hash table entry.
1529 (cond
1530 ((zerop (cdr active))
1531 (setq num 0))
1532 ((not range)
1533 (setq num (- (1+ (cdr active)) (car active))))
1534 ((not (listp (cdr range)))
1535 ;; Fix a single (num . num) range according to the
1536 ;; active hash table.
1537 ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1538 (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1539 (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1540 ;; Compute number of unread articles.
1541 (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1542 (t
1543 ;; The read list is a list of ranges. Fix them according to
1544 ;; the active hash table.
1545 ;; First peel off any elements that are below the lower
1546 ;; active limit.
1547 (while (and (cdr range)
1548 (>= (car active)
1549 (or (and (atom (cadr range)) (cadr range))
1550 (caadr range))))
1551 (if (numberp (car range))
1552 (setcar range
1553 (cons (car range)
1554 (or (and (numberp (cadr range))
1555 (cadr range))
1556 (cdadr range))))
1557 (setcdr (car range)
1558 (or (and (numberp (nth 1 range)) (nth 1 range))
1559 (cdadr range))))
1560 (setcdr range (cddr range)))
1561 ;; Adjust the first element to be the same as the lower limit.
1562 (when (and (not (atom (car range)))
1563 (< (cdar range) (car active)))
1564 (setcdr (car range) (1- (car active))))
1565 ;; Then we want to peel off any elements that are higher
1566 ;; than the upper active limit.
1567 (let ((srange range))
16409b0b 1568 ;; Go past all valid elements.
eec82323
LMI
1569 (while (and (cdr srange)
1570 (<= (or (and (atom (cadr srange))
1571 (cadr srange))
1572 (caadr srange))
1573 (cdr active)))
1574 (setq srange (cdr srange)))
1575 (when (cdr srange)
16409b0b 1576 ;; Nuke all remaining invalid elements.
eec82323
LMI
1577 (setcdr srange nil))
1578
1579 ;; Adjust the final element.
1580 (when (and (not (atom (car srange)))
1581 (> (cdar srange) (cdr active)))
1582 (setcdr (car srange) (cdr active))))
1583 ;; Compute the number of unread articles.
1584 (while range
1585 (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1586 (cdar range)))
1587 (or (and (atom (car range)) (car range))
1588 (caar range)))))
1589 (setq range (cdr range)))
1590 (setq num (max 0 (- (cdr active) num)))))
1591 ;; Set the number of unread articles.
23f87bed 1592 (when (and info
01c52d31
MB
1593 (gnus-group-entry (gnus-info-group info)))
1594 (setcar (gnus-group-entry (gnus-info-group info)) num))
eec82323
LMI
1595 num)))
1596
1597;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1598;; and compute how many unread articles there are in each group.
1599(defun gnus-get-unread-articles (&optional level)
23f87bed 1600 (setq gnus-server-method-cache nil)
3d319c8f 1601 (require 'gnus-agent)
eec82323 1602 (let* ((newsrc (cdr gnus-newsrc-alist))
b890d447 1603 (alevel (or level gnus-activate-level (1+ gnus-level-subscribed)))
eec82323 1604 (foreign-level
cdad7ab2
LMI
1605 (or
1606 level
1607 (min
1608 (cond ((and gnus-activate-foreign-newsgroups
1609 (not (numberp gnus-activate-foreign-newsgroups)))
1610 (1+ gnus-level-subscribed))
1611 ((numberp gnus-activate-foreign-newsgroups)
1612 gnus-activate-foreign-newsgroups)
1613 (t 0))
1614 alevel)))
23f87bed
MB
1615 (methods-cache nil)
1616 (type-cache nil)
4469385c 1617 (gnus-agent-article-local-times 0)
cc4244d3 1618 (archive-method (gnus-server-to-method "archive"))
8c3e17f8 1619 infos info group active method cmethod
55c9cbb0 1620 method-type method-group-list entry)
23f87bed 1621 (gnus-message 6 "Checking new news...")
eec82323
LMI
1622
1623 (while newsrc
1624 (setq active (gnus-active (setq group (gnus-info-group
01c52d31 1625 (setq info (pop newsrc))))))
8c3e17f8
LMI
1626 ;; First go through all the groups, see what select methods they
1627 ;; belong to, and then collect them into lists per unique select
1628 ;; method.
1629 (if (not (setq method (gnus-info-method info)))
1630 (setq method gnus-select-method)
0617bb00
LMI
1631 ;; There may be several similar methods. Possibly extend the
1632 ;; method.
23f87bed
MB
1633 (if (setq cmethod (assoc method methods-cache))
1634 (setq method (cdr cmethod))
0617bb00
LMI
1635 (setq cmethod (if (stringp method)
1636 (gnus-server-to-method method)
1637 (inline (gnus-find-method-for-group
1638 (gnus-info-group info) info))))
23f87bed
MB
1639 (push (cons method cmethod) methods-cache)
1640 (setq method cmethod)))
8c3e17f8
LMI
1641 (setq method-group-list (assoc method type-cache))
1642 (unless method-group-list
23f87bed 1643 (setq method-type
01c52d31 1644 (cond
cc4244d3
LMI
1645 ((or (gnus-secondary-method-p method)
1646 (and (gnus-archive-server-wanted-p)
1647 (gnus-methods-equal-p archive-method method)))
01c52d31
MB
1648 'secondary)
1649 ((inline (gnus-server-equal gnus-select-method method))
1650 'primary)
1651 (t
1652 'foreign)))
20a673b2 1653 (push (setq method-group-list (list method method-type nil nil))
8c3e17f8 1654 type-cache))
a83e5f19 1655 ;; Only add groups that need updating.
55c9cbb0
KY
1656 (if (<= (gnus-info-level info)
1657 (if (eq (cadr method-group-list) 'foreign)
1658 foreign-level
1659 alevel))
1660 (setcar (nthcdr 2 method-group-list)
1661 (cons info (nth 2 method-group-list)))
1662 ;; The group is inactive, so we nix out the number of unread articles.
1663 ;; It leads `(gnus-group-unread group)' to return t. See also
1664 ;; `gnus-group-prepare-flat'.
1665 (unless active
1666 (when (setq entry (gnus-group-entry group))
1667 (setcar entry t)))))
8c3e17f8
LMI
1668
1669 ;; Sort the methods based so that the primary and secondary
1670 ;; methods come first. This is done for legacy reasons to try to
1671 ;; ensure that side-effect behaviour doesn't change from previous
1672 ;; Gnus versions.
1673 (setq type-cache
1674 (sort (nreverse type-cache)
1675 (lambda (c1 c2)
1676 (< (gnus-method-rank (cadr c1) (car c1))
1677 (gnus-method-rank (cadr c2) (car c2))))))
1678
20a673b2
KY
1679 ;; Start early async retrieval of data.
1680 (dolist (elem type-cache)
1681 (destructuring-bind (method method-type infos dummy) elem
1682 (when (and method infos
bdaa75c7 1683 (not (gnus-method-denied-p method)))
1fe0787f
LMI
1684 ;; If the open-server method doesn't exist, then the method
1685 ;; itself doesn't exist, so we ignore it.
1686 (if (not (ignore-errors (gnus-get-function method 'open-server)))
1687 (setq type-cache (delq elem type-cache))
1688 (unless (gnus-server-opened method)
1689 (gnus-open-server method))
1690 (when (and
1691 (gnus-server-opened method)
1692 (gnus-check-backend-function
1693 'retrieve-group-data-early (car method)))
1694 (when (gnus-check-backend-function 'request-scan (car method))
1695 (gnus-request-scan nil method))
c1d5851d
LI
1696 ;; Store the token we get back from -early so that we
1697 ;; can pass it to -finish later.
1fe0787f
LMI
1698 (setcar (nthcdr 3 elem)
1699 (gnus-retrieve-group-data-early method infos)))))))
20a673b2 1700
0d1c2cc8
G
1701 ;; If we have primary/secondary select methods, but no groups from
1702 ;; them, we still want to issue a retrieval request from them.
1703 (dolist (method (cons gnus-select-method
1704 gnus-secondary-select-methods))
1705 (when (and (not (assoc method type-cache))
1706 (gnus-check-backend-function 'request-list (car method)))
1707 (with-current-buffer nntp-server-buffer
1708 (gnus-read-active-file-1 method nil))))
1709
20a673b2
KY
1710 ;; Do the rest of the retrieval.
1711 (dolist (elem type-cache)
1712 (destructuring-bind (method method-type infos early-data) elem
1713 (when (and method infos)
b1ae92ba
G
1714 (let ((updatep (gnus-check-backend-function
1715 'request-update-info (car method))))
1716 ;; See if any of the groups from this method require updating.
1717 (gnus-read-active-for-groups method infos early-data)
1718 (dolist (info infos)
1719 (inline (gnus-get-unread-articles-in-group
1720 info (gnus-active (gnus-info-group info))
1721 updatep)))))))
23f87bed 1722 (gnus-message 6 "Checking new news...done")))
eec82323 1723
8c3e17f8
LMI
1724(defun gnus-method-rank (type method)
1725 (cond
d65835d9
LMI
1726 ;; Get info for virtual groups last.
1727 ((eq (car method) 'nnvirtual)
1728 200)
8c3e17f8
LMI
1729 ((eq type 'primary)
1730 1)
1731 ;; Compute the rank of the secondary methods based on where they
1732 ;; are in the secondary select list.
1733 ((eq type 'secondary)
1734 (let ((i 2))
1735 (block nil
1736 (dolist (smethod gnus-secondary-select-methods)
3cf628e8 1737 (when (equal method smethod)
8c3e17f8
LMI
1738 (return i))
1739 (incf i))
1740 i)))
1741 ;; Just say that all foreign groups have the same rank.
1742 (t
1743 100)))
1744
20a673b2 1745(defun gnus-read-active-for-groups (method infos early-data)
8c3e17f8
LMI
1746 (with-current-buffer nntp-server-buffer
1747 (cond
c1d5851d
LI
1748 ;; Finish up getting the data from the methods that have -early
1749 ;; methods.
20a673b2
KY
1750 ((and
1751 (gnus-check-backend-function 'finish-retrieve-group-infos (car method))
1752 (or (not (gnus-agent-method-p method))
1753 (gnus-online method)))
e8861cd2
LMI
1754 (gnus-finish-retrieve-group-infos method infos early-data)
1755 (gnus-agent-save-active method))
c1d5851d 1756 ;; Most backends have -retrieve-groups.
0d1c2cc8 1757 ((gnus-check-backend-function 'retrieve-groups (car method))
26f96aa0 1758 (when (gnus-check-backend-function 'request-scan (car method))
b069e5a6 1759 (gnus-request-scan nil method))
39486331
KY
1760 (let (groups)
1761 (gnus-read-active-file-2
1762 (dolist (info infos (nreverse groups))
1763 (push (gnus-group-real-name (gnus-info-group info)) groups))
1764 method)))
c1d5851d 1765 ;; Virtually all backends have -request-list.
74769e8b 1766 ((gnus-check-backend-function 'request-list (car method))
c1d5851d
LI
1767 (gnus-read-active-file-1 method nil))
1768 ;; Except nnvirtual and friends, where we request each group, one
1769 ;; by one.
8c3e17f8
LMI
1770 (t
1771 (dolist (info infos)
74769e8b 1772 (gnus-activate-group (gnus-info-group info) nil nil method t))))))
8c3e17f8 1773
eec82323
LMI
1774;; Create a hash table out of the newsrc alist. The `car's of the
1775;; alist elements are used as keys.
1776(defun gnus-make-hashtable-from-newsrc-alist ()
1777 (let ((alist gnus-newsrc-alist)
1778 (ohashtb gnus-newsrc-hashtb)
01c52d31 1779 prev info method rest methods)
eec82323
LMI
1780 (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1781 (setq alist
1782 (setq prev (setq gnus-newsrc-alist
1783 (if (equal (caar gnus-newsrc-alist)
1784 "dummy.group")
1785 gnus-newsrc-alist
1786 (cons (list "dummy.group" 0 nil) alist)))))
1787 (while alist
01c52d31
MB
1788 (setq info (car alist))
1789 ;; Make the same select-methods identical Lisp objects.
1790 (when (setq method (gnus-info-method info))
1791 (if (setq rest (member method methods))
1792 (gnus-info-set-method info (car rest))
1793 (push method methods)))
fb994703
LMI
1794 ;; Check for duplicates.
1795 (if (gnus-gethash (car info) gnus-newsrc-hashtb)
1796 ;; Remove this entry from the alist.
1797 (setcdr prev (cddr prev))
1798 (gnus-sethash
1799 (car info)
1800 ;; Preserve number of unread articles in groups.
1801 (cons (and ohashtb (car (gnus-gethash (car info) ohashtb)))
1802 prev)
1803 gnus-newsrc-hashtb)
1804 (setq prev alist))
1805 (setq alist (cdr alist)))
01c52d31
MB
1806 ;; Make the same select-methods in `gnus-server-alist' identical
1807 ;; as well.
1808 (while methods
1809 (setq method (pop methods))
1810 (when (setq rest (rassoc method gnus-server-alist))
1811 (setcdr rest method)))))
eec82323
LMI
1812
1813(defun gnus-make-hashtable-from-killed ()
1814 "Create a hash table from the killed and zombie lists."
1815 (let ((lists '(gnus-killed-list gnus-zombie-list))
1816 list)
1817 (setq gnus-killed-hashtb
1818 (gnus-make-hashtable
1819 (+ (length gnus-killed-list) (length gnus-zombie-list))))
1820 (while lists
1821 (setq list (symbol-value (pop lists)))
1822 (while list
1823 (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1824
1825(defun gnus-parse-active ()
1826 "Parse active info in the nntp server buffer."
20a673b2 1827 (with-current-buffer nntp-server-buffer
eec82323
LMI
1828 (goto-char (point-min))
1829 ;; Parse the result we got from `gnus-request-group'.
1830 (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1831 (goto-char (match-beginning 1))
1832 (cons (read (current-buffer))
1833 (read (current-buffer))))))
1834
1835(defun gnus-make-articles-unread (group articles)
1836 "Mark ARTICLES in GROUP as unread."
01c52d31
MB
1837 (let* ((info (nth 2 (or (gnus-group-entry group)
1838 (gnus-group-entry
1839 (gnus-group-real-name group)))))
eec82323
LMI
1840 (ranges (gnus-info-read info))
1841 news article)
1842 (while articles
1843 (when (gnus-member-of-range
1844 (setq article (pop articles)) ranges)
1845 (push article news)))
1846 (when news
23f87bed 1847 ;; Enter this list into the group info.
eec82323
LMI
1848 (gnus-info-set-read
1849 info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
23f87bed
MB
1850
1851 ;; Set the number of unread articles in gnus-newsrc-hashtb.
1852 (gnus-get-unread-articles-in-group info (gnus-active group))
1853
1854 ;; Insert the change into the group buffer and the dribble file.
1855 (gnus-group-update-group group t))))
1856
1857(defun gnus-make-ascending-articles-unread (group articles)
1858 "Mark ascending ARTICLES in GROUP as unread."
01c52d31
MB
1859 (let* ((entry (or (gnus-group-entry group)
1860 (gnus-group-entry (gnus-group-real-name group))))
23f87bed
MB
1861 (info (nth 2 entry))
1862 (ranges (gnus-info-read info))
1863 (r ranges)
1864 modified)
1865
1866 (while articles
1867 (let ((article (pop articles))) ; get the next article to remove from ranges
1868 (while (let ((range (car ranges))) ; note the current range
1869 (if (atom range) ; single value range
1870 (cond ((not range)
1871 ;; the articles extend past the end of the ranges
1872 ;; OK - I'm done
1873 (setq articles nil))
1874 ((< range article)
1875 ;; this range preceeds the article. Leave the range unmodified.
1876 (pop ranges)
1877 ranges)
1878 ((= range article)
1879 ;; this range exactly matches the article; REMOVE THE RANGE.
1880 ;; NOTE: When the range being removed is the last range, the list is corrupted by inserting null at its end.
1881 (setcar ranges (cadr ranges))
1882 (setcdr ranges (cddr ranges))
1883 (setq modified (if (car ranges) t 'remove-null))
1884 nil))
1885 (let ((min (car range))
1886 (max (cdr range)))
1887 ;; I have a min/max range to consider
1888 (cond ((> min max) ; invalid range introduced by splitter
1889 (setcar ranges (cadr ranges))
1890 (setcdr ranges (cddr ranges))
1891 (setq modified (if (car ranges) t 'remove-null))
1892 ranges)
1893 ((= min max)
1894 ;; replace min/max range with a single-value range
1895 (setcar ranges min)
1896 ranges)
1897 ((< max article)
1898 ;; this range preceeds the article. Leave the range unmodified.
1899 (pop ranges)
1900 ranges)
1901 ((< article min)
1902 ;; this article preceeds the range. Return null to move to the
1903 ;; next article
1904 nil)
1905 (t
1906 ;; this article splits the range into two parts
1907 (setcdr ranges (cons (cons (1+ article) max) (cdr ranges)))
1908 (setcdr range (1- article))
1909 (setq modified t)
1910 ranges))))))))
bf247b6e 1911
23f87bed
MB
1912 (when modified
1913 (when (eq modified 'remove-null)
1914 (setq r (delq nil r)))
1915 ;; Enter this list into the group info.
1916 (gnus-info-set-read info r)
1917
1918 ;; Set the number of unread articles in gnus-newsrc-hashtb.
1919 (gnus-get-unread-articles-in-group info (gnus-active group))
1920
1921 ;; Insert the change into the group buffer and the dribble file.
eec82323
LMI
1922 (gnus-group-update-group group t))))
1923
1924;; Enter all dead groups into the hashtb.
1925(defun gnus-update-active-hashtb-from-killed ()
1926 (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1927 (lists (list gnus-killed-list gnus-zombie-list))
1928 killed)
1929 (while lists
1930 (setq killed (car lists))
1931 (while killed
01c52d31 1932 (gnus-sethash (mm-string-as-unibyte (car killed)) nil hashtb)
eec82323
LMI
1933 (setq killed (cdr killed)))
1934 (setq lists (cdr lists)))))
1935
1936(defun gnus-get-killed-groups ()
1937 "Go through the active hashtb and mark all unknown groups as killed."
1938 ;; First make sure active file has been read.
1939 (unless (gnus-read-active-file-p)
1940 (let ((gnus-read-active-file t))
1941 (gnus-read-active-file)))
1942 (unless gnus-killed-hashtb
1943 (gnus-make-hashtable-from-killed))
1944 ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1945 (mapatoms
1946 (lambda (sym)
1947 (let ((groups 0)
1948 (group (symbol-name sym)))
1949 (if (or (null group)
1950 (gnus-gethash group gnus-killed-hashtb)
1951 (gnus-gethash group gnus-newsrc-hashtb))
1952 ()
1953 (let ((do-sub (gnus-matches-options-n group)))
1954 (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1955 ()
1956 (setq groups (1+ groups))
1957 (push group gnus-killed-list)
1958 (gnus-sethash group group gnus-killed-hashtb))))))
1959 gnus-active-hashtb)
1960 (gnus-dribble-touch))
1961
1962;; Get the active file(s) from the backend(s).
a8151ef7 1963(defun gnus-read-active-file (&optional force not-native)
eec82323
LMI
1964 (gnus-group-set-mode-line)
1965 (let ((methods
16409b0b
GM
1966 (mapcar
1967 (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1968 (append
1969 (if (and (not not-native)
1970 (gnus-check-server gnus-select-method))
1971 ;; The native server is available.
1972 (cons gnus-select-method gnus-secondary-select-methods)
1973 ;; The native server is down, so we just do the
1974 ;; secondary ones.
1975 gnus-secondary-select-methods)
1976 ;; Also read from the archive server.
1977 (when (gnus-archive-server-wanted-p)
1978 (list "archive")))))
1979 method)
eec82323 1980 (setq gnus-have-read-active-file nil)
20a673b2 1981 (with-current-buffer nntp-server-buffer
16409b0b
GM
1982 (while (setq method (pop methods))
1983 ;; Only do each method once, in case the methods appear more
1984 ;; than once in this list.
1fe0787f
LMI
1985 (when (and (not (member method methods))
1986 ;; Check whether the backend exists.
1987 (ignore-errors (gnus-get-function method 'open-server)))
23f87bed 1988 (if (or debug-on-error debug-on-quit)
16409b0b 1989 (gnus-read-active-file-1 method force)
23f87bed
MB
1990 (condition-case ()
1991 (gnus-read-active-file-1 method force)
1992 ;; We catch C-g so that we can continue past servers
1993 ;; that do not respond.
1994 (quit
1995 (message "Quit reading the active file")
1996 nil))))))))
16409b0b 1997
c1d5851d 1998(defun gnus-read-active-file-1 (method force)
16409b0b
GM
1999 (let (where mesg)
2000 (setq where (nth 1 method)
2001 mesg (format "Reading active file%s via %s..."
2002 (if (and where (not (zerop (length where))))
2003 (concat " from " where) "")
2004 (car method)))
bdaa75c7 2005 (gnus-message 5 "%s" mesg)
16409b0b
GM
2006 (when (gnus-check-server method)
2007 ;; Request that the backend scan its incoming messages.
a9ec34f4
LMI
2008 (when (and (or (and gnus-agent
2009 (gnus-online method))
2010 (not gnus-agent))
8c3e17f8 2011 (gnus-check-backend-function 'request-scan (car method)))
b069e5a6 2012 (gnus-request-scan nil method))
16409b0b
GM
2013 (cond
2014 ((and (eq gnus-read-active-file 'some)
2015 (gnus-check-backend-function 'retrieve-groups (car method))
2016 (not force))
2017 (let ((newsrc (cdr gnus-newsrc-alist))
2018 (gmethod (gnus-server-get-method nil method))
2019 groups info)
2020 (while (setq info (pop newsrc))
2021 (when (inline
2022 (gnus-server-equal
54506618
MB
2023 (inline
2024 (gnus-find-method-for-group
2025 (gnus-info-group info) info))
2026 gmethod))
16409b0b
GM
2027 (push (gnus-group-real-name (gnus-info-group info))
2028 groups)))
2029 (gnus-read-active-file-2 groups method)))
2030 ((null method)
2031 t)
2032 (t
2033 (if (not (gnus-request-list method))
2034 (unless (equal method gnus-message-archive-method)
2035 (gnus-error 1 "Cannot read active file from %s server"
2036 (car method)))
bdaa75c7 2037 (gnus-message 5 "%s" mesg)
16409b0b
GM
2038 (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
2039 ;; We mark this active file as read.
e0da801a 2040 (add-to-list 'gnus-have-read-active-file method)
16409b0b
GM
2041 (gnus-message 5 "%sdone" mesg)))))))
2042
2043(defun gnus-read-active-file-2 (groups method)
23f87bed 2044 "Read an active file for GROUPS in METHOD using `gnus-retrieve-groups'."
16409b0b 2045 (when groups
20a673b2 2046 (with-current-buffer nntp-server-buffer
16409b0b
GM
2047 (gnus-check-server method)
2048 (let ((list-type (gnus-retrieve-groups groups method)))
2049 (cond ((not list-type)
2050 (gnus-error
2051 1.2 "Cannot read partial active file from %s server."
2052 (car method)))
2053 ((eq list-type 'active)
2054 (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
2055 (t
2056 (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
eec82323
LMI
2057
2058;; Read an active file and place the results in `gnus-active-hashtb'.
6748645f
LMI
2059(defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
2060 real-active)
eec82323
LMI
2061 (unless method
2062 (setq method gnus-select-method))
2063 (let ((cur (current-buffer))
2064 (hashtb (or hashtb
2065 (if (and gnus-active-hashtb
2066 (not (equal method gnus-select-method)))
2067 gnus-active-hashtb
2068 (setq gnus-active-hashtb
2069 (if (equal method gnus-select-method)
2070 (gnus-make-hashtable
2071 (count-lines (point-min) (point-max)))
1428d46b
MB
2072 (gnus-make-hashtable 4096))))))
2073 group max min)
a8151ef7 2074 ;; Delete unnecessary lines.
eec82323 2075 (goto-char (point-min))
16409b0b
GM
2076 (cond
2077 ((string= gnus-ignored-newsgroups "")
2078 (delete-matching-lines "^to\\."))
2079 (t
2080 (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
2081
2082 (goto-char (point-min))
2083 (unless (re-search-forward "[\\\"]" nil t)
2084 ;; Make the group names readable as a lisp expression even if they
2085 ;; contain special characters.
2086 (goto-char (point-max))
2087 (while (re-search-backward "[][';?()#]" nil t)
2088 (insert ?\\)))
eec82323 2089
6748645f 2090 ;; Let the Gnus agent save the active file.
23f87bed 2091 (when (and gnus-agent real-active (gnus-online method))
6748645f
LMI
2092 (gnus-agent-save-active method))
2093
eec82323
LMI
2094 ;; If these are groups from a foreign select method, we insert the
2095 ;; group prefix in front of the group names.
a8151ef7
LMI
2096 (when (not (gnus-server-equal
2097 (gnus-server-get-method nil method)
2098 (gnus-server-get-method nil gnus-select-method)))
2099 (let ((prefix (gnus-group-prefixed-name "" method)))
2100 (goto-char (point-min))
2101 (while (and (not (eobp))
16409b0b
GM
2102 (progn
2103 (when (= (following-char) ?\")
2104 (forward-char 1))
2105 (insert prefix)
2106 (zerop (forward-line 1)))))))
eec82323 2107 ;; Store the active file in a hash table.
1428d46b
MB
2108 ;; Use a unibyte buffer in order to make `read' read non-ASCII
2109 ;; group names (which have been encoded) as unibyte strings.
2110 (mm-with-unibyte-buffer
2111 (insert-buffer-substring cur)
2112 (setq cur (current-buffer))
2113 (goto-char (point-min))
eec82323 2114 (while (not (eobp))
23f87bed 2115 (condition-case ()
eec82323 2116 (progn
01c52d31 2117 (narrow-to-region (point) (point-at-eol))
eec82323
LMI
2118 ;; group gets set to a symbol interned in the hash table
2119 ;; (what a hack!!) - jwz
2120 (setq group (let ((obarray hashtb)) (read cur)))
16409b0b
GM
2121 ;; ### The extended group name scheme makes
2122 ;; the previous optimization strategy sort of pointless...
2123 (when (stringp group)
2124 (setq group (intern group hashtb)))
eec82323
LMI
2125 (if (and (numberp (setq max (read cur)))
2126 (numberp (setq min (read cur)))
2127 (progn
2128 (skip-chars-forward " \t")
2129 (not
16409b0b
GM
2130 (or (eq (char-after) ?=)
2131 (eq (char-after) ?x)
2132 (eq (char-after) ?j)))))
eec82323
LMI
2133 (progn
2134 (set group (cons min max))
2135 ;; if group is moderated, stick in moderation table
16409b0b 2136 (when (eq (char-after) ?m)
eec82323
LMI
2137 (unless gnus-moderated-hashtb
2138 (setq gnus-moderated-hashtb (gnus-make-hashtable)))
2139 (gnus-sethash (symbol-name group) t
2140 gnus-moderated-hashtb)))
2141 (set group nil)))
2142 (error
2143 (and group
2144 (symbolp group)
2145 (set group nil))
2146 (unless ignore-errors
16409b0b 2147 (gnus-message 3 "Warning - invalid active: %s"
eec82323 2148 (buffer-substring
01c52d31 2149 (point-at-bol) (point-at-eol))))))
eec82323
LMI
2150 (widen)
2151 (forward-line 1)))))
2152
6748645f 2153(defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
eec82323
LMI
2154 ;; Parse a "groups" active file.
2155 (let ((cur (current-buffer))
2156 (hashtb (or hashtb
2157 (if (and method gnus-active-hashtb)
2158 gnus-active-hashtb
2159 (setq gnus-active-hashtb
2160 (gnus-make-hashtable
2161 (count-lines (point-min) (point-max)))))))
2162 (prefix (and method
2163 (not (gnus-server-equal
2164 (gnus-server-get-method nil method)
2165 (gnus-server-get-method nil gnus-select-method)))
2166 (gnus-group-prefixed-name "" method))))
2167
6748645f 2168 ;; Let the Gnus agent save the active file.
16409b0b
GM
2169 (if (and gnus-agent
2170 real-active
23f87bed 2171 (gnus-online method)
16409b0b
GM
2172 (gnus-agent-method-p method))
2173 (progn
54506618 2174 (gnus-agent-save-active method)
16409b0b
GM
2175 (gnus-active-to-gnus-format method hashtb nil real-active))
2176
2177 (goto-char (point-min))
2178 ;; We split this into to separate loops, one with the prefix
2179 ;; and one without to speed the reading up somewhat.
2180 (if prefix
2181 (let (min max opoint group)
2182 (while (not (eobp))
2183 (condition-case ()
2184 (progn
2185 (read cur) (read cur)
2186 (setq min (read cur)
2187 max (read cur)
2188 opoint (point))
2189 (skip-chars-forward " \t")
2190 (insert prefix)
2191 (goto-char opoint)
2192 (set (let ((obarray hashtb)) (read cur))
2193 (cons min max)))
2194 (error (and group (symbolp group) (set group nil))))
2195 (forward-line 1)))
2196 (let (min max group)
eec82323
LMI
2197 (while (not (eobp))
2198 (condition-case ()
16409b0b 2199 (when (eq (char-after) ?2)
eec82323
LMI
2200 (read cur) (read cur)
2201 (setq min (read cur)
16409b0b
GM
2202 max (read cur))
2203 (set (setq group (let ((obarray hashtb)) (read cur)))
eec82323
LMI
2204 (cons min max)))
2205 (error (and group (symbolp group) (set group nil))))
16409b0b 2206 (forward-line 1)))))))
eec82323
LMI
2207
2208(defun gnus-read-newsrc-file (&optional force)
2209 "Read startup file.
2210If FORCE is non-nil, the .newsrc file is read."
2211 ;; Reset variables that might be defined in the .newsrc.eld file.
23f87bed 2212 (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
eec82323
LMI
2213 (while variables
2214 (set (car variables) nil)
2215 (setq variables (cdr variables))))
2216 (let* ((newsrc-file gnus-current-startup-file)
2217 (quick-file (concat newsrc-file ".el")))
2218 (save-excursion
2219 ;; We always load the .newsrc.eld file. If always contains
2220 ;; much information that can not be gotten from the .newsrc
2221 ;; file (ticked articles, killed groups, foreign methods, etc.)
2222 (gnus-read-newsrc-el-file quick-file)
2223
16409b0b
GM
2224 (when (and gnus-read-newsrc-file
2225 (file-exists-p gnus-current-startup-file)
eec82323
LMI
2226 (or force
2227 (and (file-newer-than-file-p newsrc-file quick-file)
2228 (file-newer-than-file-p newsrc-file
2229 (concat quick-file "d")))
2230 (not gnus-newsrc-alist)))
2231 ;; We read the .newsrc file. Note that if there if a
2232 ;; .newsrc.eld file exists, it has already been read, and
2233 ;; the `gnus-newsrc-hashtb' has been created. While reading
2234 ;; the .newsrc file, Gnus will only use the information it
2235 ;; can find there for changing the data already read -
2236 ;; i. e., reading the .newsrc file will not trash the data
2237 ;; already read (except for read articles).
2238 (save-excursion
2239 (gnus-message 5 "Reading %s..." newsrc-file)
2240 (set-buffer (nnheader-find-file-noselect newsrc-file))
16409b0b 2241 (buffer-disable-undo)
eec82323
LMI
2242 (gnus-newsrc-to-gnus-format)
2243 (kill-buffer (current-buffer))
2244 (gnus-message 5 "Reading %s...done" newsrc-file)))
2245
2246 ;; Convert old to new.
2247 (gnus-convert-old-newsrc))))
2248
2249(defun gnus-convert-old-newsrc ()
54506618 2250 "Convert old newsrc formats into the current format, if needed."
eec82323 2251 (let ((fcv (and gnus-newsrc-file-version
86713405
MB
2252 (gnus-continuum-version gnus-newsrc-file-version)))
2253 (gcv (gnus-continuum-version)))
54506618
MB
2254 (when fcv
2255 ;; A newsrc file was loaded.
2256 (let (prompt-displayed
2257 (converters
2258 (sort
2259 (mapcar (lambda (date-func)
2260 (cons (gnus-continuum-version (car date-func))
2261 date-func))
2262 ;; This is a list of converters that must be run
2263 ;; to bring the newsrc file up to the current
2264 ;; version. If you create an incompatibility
2265 ;; with older versions, you should create an
2266 ;; entry here. The entry should consist of the
2267 ;; current gnus version (hardcoded so that it
2268 ;; doesn't change with each release) and the
2269 ;; function that must be applied to convert the
2270 ;; previous version into the current version.
c825ac9d 2271 '(("September Gnus v0.1" nil
54506618
MB
2272 gnus-convert-old-ticks)
2273 ("Oort Gnus v0.08" "legacy-gnus-agent"
2274 gnus-agent-convert-to-compressed-agentview)
c825ac9d 2275 ("Gnus v5.10.7" "legacy-gnus-agent"
54506618 2276 gnus-agent-unlist-expire-days)
c825ac9d 2277 ("Gnus v5.10.7" "legacy-gnus-agent"
54506618
MB
2278 gnus-agent-unhook-expire-days)))
2279 #'car-less-than-car)))
2280 ;; Skip converters older than the file version
2281 (while (and converters (>= fcv (caar converters)))
2282 (pop converters))
2283
2284 ;; Perform converters to bring older version up to date.
2285 (when (and converters (< fcv (caar converters)))
d5ddd795 2286 (while (and converters (< fcv (caar converters))
86713405 2287 (<= (caar converters) gcv))
54506618
MB
2288 (let* ((converter-spec (pop converters))
2289 (convert-to (nth 1 converter-spec))
2290 (load-from (nth 2 converter-spec))
2291 (func (nth 3 converter-spec)))
2292 (when (and load-from
2293 (not (fboundp func)))
2294 (load load-from t))
54506618
MB
2295 (or prompt-displayed
2296 (not (gnus-convert-converter-needs-prompt func))
2297 (while (let (c
2298 (cursor-in-echo-area t)
2299 (echo-keystrokes 0))
2300 (message "Convert gnus from version '%s' to '%s'? (n/y/?)"
2301 gnus-newsrc-file-version gnus-version)
2302 (setq c (read-char-exclusive))
2303
2304 (cond ((or (eq c ?n) (eq c ?N))
2305 (error "Can not start gnus without converting"))
2306 ((or (eq c ?y) (eq c ?Y))
2307 (setq prompt-displayed t)
2308 nil)
2309 ((eq c ?\?)
2310 (message "This conversion is irreversible. \
2311 To be safe, you should backup your files before proceeding.")
2312 (sit-for 5)
2313 t)
2314 (t
2315 (gnus-message 3 "Ignoring unexpected input")
2316 (sit-for 3)
2317 t)))))
2318
2319 (funcall func convert-to)))
bf247b6e 2320 (gnus-dribble-enter
54506618
MB
2321 (format ";Converted gnus from version '%s' to '%s'."
2322 gnus-newsrc-file-version gnus-version)))))))
2323
2324(defun gnus-convert-mark-converter-prompt (converter no-prompt)
2325 "Indicate whether CONVERTER requires gnus-convert-old-newsrc to
2326 display the conversion prompt. NO-PROMPT may be nil (prompt),
2327 t (no prompt), or any form that can be called as a function.
2328 The form should return either t or nil."
2329 (put converter 'gnus-convert-no-prompt no-prompt))
2330
2331(defun gnus-convert-converter-needs-prompt (converter)
2332 (let ((no-prompt (get converter 'gnus-convert-no-prompt)))
2333 (not (if (memq no-prompt '(t nil))
2334 no-prompt
2335 (funcall no-prompt)))))
2336
2337(defun gnus-convert-old-ticks (converting-to)
eec82323
LMI
2338 (let ((newsrc (cdr gnus-newsrc-alist))
2339 marks info dormant ticked)
2340 (while (setq info (pop newsrc))
2341 (when (setq marks (gnus-info-marks info))
2342 (setq dormant (cdr (assq 'dormant marks))
2343 ticked (cdr (assq 'tick marks)))
2344 (when (or dormant ticked)
2345 (gnus-info-set-read
2346 info
2347 (gnus-add-to-range
2348 (gnus-info-read info)
2349 (nconc (gnus-uncompress-range dormant)
2350 (gnus-uncompress-range ticked)))))))))
2351
23f87bed
MB
2352(defun gnus-load (file)
2353 "Load FILE, but in such a way that read errors can be reported."
2354 (with-temp-buffer
2355 (insert-file-contents file)
2356 (while (not (eobp))
2357 (condition-case type
2358 (let ((form (read (current-buffer))))
2359 (eval form))
2360 (error
2361 (unless (eq (car type) 'end-of-file)
af5370ad 2362 (let ((errmsg (format "Error in %s line %d" file
80d06753 2363 (count-lines (point-min) (point)))))
23f87bed 2364 (ding)
af5370ad
RS
2365 (unless (gnus-yes-or-no-p (concat errmsg "; continue? "))
2366 (error "%s" errmsg)))))))))
23f87bed 2367
eec82323
LMI
2368(defun gnus-read-newsrc-el-file (file)
2369 (let ((ding-file (concat file "d")))
23f87bed
MB
2370 (when (file-exists-p ding-file)
2371 ;; We always, always read the .eld file.
2372 (gnus-message 5 "Reading %s..." ding-file)
2373 (let (gnus-newsrc-assoc)
a37d67dd 2374 (gnus-load ding-file)
23f87bed
MB
2375 ;; Older versions of `gnus-format-specs' are no longer valid
2376 ;; in Oort Gnus 0.01.
2377 (let ((version
2378 (and gnus-newsrc-file-version
2379 (gnus-continuum-version gnus-newsrc-file-version))))
2380 (when (or (not version)
2381 (< version 5.090009))
2382 (setq gnus-format-specs gnus-default-format-specs)))
2383 (when gnus-newsrc-assoc
2384 (setq gnus-newsrc-alist gnus-newsrc-assoc))))
01c52d31
MB
2385 (dolist (elem gnus-newsrc-alist)
2386 (setcar elem (mm-string-as-unibyte (car elem))))
eec82323
LMI
2387 (gnus-make-hashtable-from-newsrc-alist)
2388 (when (file-newer-than-file-p file ding-file)
2389 ;; Old format quick file
2390 (gnus-message 5 "Reading %s..." file)
2391 ;; The .el file is newer than the .eld file, so we read that one
2392 ;; as well.
23f87bed
MB
2393 (gnus-read-old-newsrc-el-file file)))
2394 (gnus-run-hooks 'gnus-read-newsrc-el-hook))
eec82323
LMI
2395
2396;; Parse the old-style quick startup file
2397(defun gnus-read-old-newsrc-el-file (file)
2398 (let (newsrc killed marked group m info)
2399 (prog1
2400 (let ((gnus-killed-assoc nil)
2401 gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2402 (prog1
2403 (ignore-errors
2404 (load file t t t))
2405 (setq newsrc gnus-newsrc-assoc
2406 killed gnus-killed-assoc
2407 marked gnus-marked-assoc)))
2408 (setq gnus-newsrc-alist nil)
2409 (while (setq group (pop newsrc))
2410 (if (setq info (gnus-get-info (car group)))
2411 (progn
2412 (gnus-info-set-read info (cddr group))
2413 (gnus-info-set-level
2414 info (if (nth 1 group) gnus-level-default-subscribed
2415 gnus-level-default-unsubscribed))
2416 (push info gnus-newsrc-alist))
2417 (push (setq info
2418 (list (car group)
2419 (if (nth 1 group) gnus-level-default-subscribed
2420 gnus-level-default-unsubscribed)
2421 (cddr group)))
2422 gnus-newsrc-alist))
2423 ;; Copy marks into info.
2424 (when (setq m (assoc (car group) marked))
2425 (unless (nthcdr 3 info)
2426 (nconc info (list nil)))
2427 (gnus-info-set-marks
2428 info (list (cons 'tick (gnus-compress-sequence
2429 (sort (cdr m) '<) t))))))
2430 (setq newsrc killed)
2431 (while newsrc
2432 (setcar newsrc (caar newsrc))
2433 (setq newsrc (cdr newsrc)))
2434 (setq gnus-killed-list killed))
2435 ;; The .el file version of this variable does not begin with
2436 ;; "options", while the .eld version does, so we just add it if it
2437 ;; isn't there.
2438 (when
2439 gnus-newsrc-options
2440 (when (not (string-match "^ *options" gnus-newsrc-options))
2441 (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2442 (when (not (string-match "\n$" gnus-newsrc-options))
2443 (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2444 ;; Finally, if we read some options lines, we parse them.
2445 (unless (string= gnus-newsrc-options "")
2446 (gnus-newsrc-parse-options gnus-newsrc-options)))
2447
2448 (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2449 (gnus-make-hashtable-from-newsrc-alist)))
2450
2451(defun gnus-make-newsrc-file (file)
2452 "Make server dependent file name by catenating FILE and server host name."
2453 (let* ((file (expand-file-name file nil))
2454 (real-file (concat file "-" (nth 1 gnus-select-method))))
2455 (if (or (file-exists-p real-file)
2456 (file-exists-p (concat real-file ".el"))
2457 (file-exists-p (concat real-file ".eld")))
6748645f
LMI
2458 real-file
2459 file)))
eec82323
LMI
2460
2461(defun gnus-newsrc-to-gnus-format ()
2462 (setq gnus-newsrc-options "")
2463 (setq gnus-newsrc-options-n nil)
2464
2465 (unless gnus-active-hashtb
2466 (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2467 (let ((buf (current-buffer))
2468 (already-read (> (length gnus-newsrc-alist) 1))
2469 group subscribed options-symbol newsrc Options-symbol
2470 symbol reads num1)
2471 (goto-char (point-min))
2472 ;; We intern the symbol `options' in the active hashtb so that we
2473 ;; can `eq' against it later.
2474 (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2475 (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2476
2477 (while (not (eobp))
2478 ;; We first read the first word on the line by narrowing and
2479 ;; then reading into `gnus-active-hashtb'. Most groups will
2480 ;; already exist in that hashtb, so this will save some string
2481 ;; space.
2482 (narrow-to-region
2483 (point)
2484 (progn (skip-chars-forward "^ \t!:\n") (point)))
2485 (goto-char (point-min))
2486 (setq symbol
2487 (and (/= (point-min) (point-max))
2488 (let ((obarray gnus-active-hashtb)) (read buf))))
2489 (widen)
2490 ;; Now, the symbol we have read is either `options' or a group
2491 ;; name. If it is an options line, we just add it to a string.
2492 (cond
2493 ((or (eq symbol options-symbol)
2494 (eq symbol Options-symbol))
2495 (setq gnus-newsrc-options
2496 ;; This concating is quite inefficient, but since our
2497 ;; thorough studies show that approx 99.37% of all
2498 ;; .newsrc files only contain a single options line, we
2499 ;; don't give a damn, frankly, my dear.
2500 (concat gnus-newsrc-options
2501 (buffer-substring
01c52d31 2502 (point-at-bol)
eec82323
LMI
2503 ;; Options may continue on the next line.
2504 (or (and (re-search-forward "^[^ \t]" nil 'move)
01c52d31 2505 (point-at-bol))
eec82323
LMI
2506 (point)))))
2507 (forward-line -1))
2508 (symbol
2509 ;; Group names can be just numbers.
2510 (when (numberp symbol)
2511 (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2512 (unless (boundp symbol)
2513 (set symbol nil))
2514 ;; It was a group name.
16409b0b 2515 (setq subscribed (eq (char-after) ?:)
eec82323
LMI
2516 group (symbol-name symbol)
2517 reads nil)
2518 (if (eolp)
2519 ;; If the line ends here, this is clearly a buggy line, so
23f87bed 2520 ;; we put point a the beginning of line and let the cond
eec82323
LMI
2521 ;; below do the error handling.
2522 (beginning-of-line)
2523 ;; We skip to the beginning of the ranges.
2524 (skip-chars-forward "!: \t"))
2525 ;; We are now at the beginning of the list of read articles.
2526 ;; We read them range by range.
2527 (while
2528 (cond
2529 ((looking-at "[0-9]+")
2530 ;; We narrow and read a number instead of buffer-substring/
e9bd5782 2531 ;; string-to-number because it's faster. narrow/widen is
eec82323
LMI
2532 ;; faster than save-restriction/narrow, and save-restriction
2533 ;; produces a garbage object.
2534 (setq num1 (progn
2535 (narrow-to-region (match-beginning 0) (match-end 0))
2536 (read buf)))
2537 (widen)
2538 ;; If the next character is a dash, then this is a range.
16409b0b 2539 (if (eq (char-after) ?-)
eec82323
LMI
2540 (progn
2541 ;; We read the upper bound of the range.
2542 (forward-char 1)
2543 (if (not (looking-at "[0-9]+"))
2544 ;; This is a buggy line, by we pretend that
2545 ;; it's kinda OK. Perhaps the user should be
2546 ;; dinged?
2547 (push num1 reads)
2548 (push
2549 (cons num1
2550 (progn
2551 (narrow-to-region (match-beginning 0)
2552 (match-end 0))
2553 (read buf)))
2554 reads)
2555 (widen)))
2556 ;; It was just a simple number, so we add it to the
2557 ;; list of ranges.
2558 (push num1 reads))
2559 ;; If the next char in ?\n, then we have reached the end
2560 ;; of the line and return nil.
16409b0b
GM
2561 (not (eq (char-after) ?\n)))
2562 ((eq (char-after) ?\n)
eec82323
LMI
2563 ;; End of line, so we end.
2564 nil)
2565 (t
2566 ;; Not numbers and not eol, so this might be a buggy
2567 ;; line...
2568 (unless (eobp)
2569 ;; If it was eob instead of ?\n, we allow it.
2570 ;; The line was buggy.
2571 (setq group nil)
2572 (gnus-error 3.1 "Mangled line: %s"
01c52d31
MB
2573 (buffer-substring (point-at-bol)
2574 (point-at-eol))))
eec82323 2575 nil))
16409b0b 2576 ;; Skip past ", ". Spaces are invalid in these ranges, but
eec82323
LMI
2577 ;; we allow them, because it's a common mistake to put a
2578 ;; space after the comma.
2579 (skip-chars-forward ", "))
2580
2581 ;; We have already read .newsrc.eld, so we gently update the
2582 ;; data in the hash table with the information we have just
2583 ;; read.
2584 (when group
2585 (let ((info (gnus-get-info group))
2586 level)
2587 (if info
2588 ;; There is an entry for this file in the alist.
2589 (progn
2590 (gnus-info-set-read info (nreverse reads))
2591 ;; We update the level very gently. In fact, we
2592 ;; only change it if there's been a status change
2593 ;; from subscribed to unsubscribed, or vice versa.
2594 (setq level (gnus-info-level info))
2595 (cond ((and (<= level gnus-level-subscribed)
2596 (not subscribed))
2597 (setq level (if reads
2598 gnus-level-default-unsubscribed
2599 (1+ gnus-level-default-unsubscribed))))
2600 ((and (> level gnus-level-subscribed) subscribed)
2601 (setq level gnus-level-default-subscribed)))
2602 (gnus-info-set-level info level))
2603 ;; This is a new group.
2604 (setq info (list group
2605 (if subscribed
2606 gnus-level-default-subscribed
2607 (if reads
2608 (1+ gnus-level-subscribed)
2609 gnus-level-default-unsubscribed))
2610 (nreverse reads))))
2611 (push info newsrc)))))
2612 (forward-line 1))
2613
2614 (setq newsrc (nreverse newsrc))
2615
2616 (if (not already-read)
2617 ()
2618 ;; We now have two newsrc lists - `newsrc', which is what we
2619 ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2620 ;; what we've read from .newsrc.eld. We have to merge these
2621 ;; lists. We do this by "attaching" any (foreign) groups in the
2622 ;; gnus-newsrc-alist to the (native) group that precedes them.
2623 (let ((rc (cdr gnus-newsrc-alist))
2624 (prev gnus-newsrc-alist)
2625 entry mentry)
2626 (while rc
2627 (or (null (nth 4 (car rc))) ; It's a native group.
2628 (assoc (caar rc) newsrc) ; It's already in the alist.
2629 (if (setq entry (assoc (caar prev) newsrc))
2630 (setcdr (setq mentry (memq entry newsrc))
2631 (cons (car rc) (cdr mentry)))
2632 (push (car rc) newsrc)))
2633 (setq prev rc
2634 rc (cdr rc)))))
2635
2636 (setq gnus-newsrc-alist newsrc)
2637 ;; We make the newsrc hashtb.
2638 (gnus-make-hashtable-from-newsrc-alist)
2639
2640 ;; Finally, if we read some options lines, we parse them.
2641 (unless (string= gnus-newsrc-options "")
2642 (gnus-newsrc-parse-options gnus-newsrc-options))))
2643
2644;; Parse options lines to find "options -n !all rec.all" and stuff.
2645;; The return value will be a list on the form
2646;; ((regexp1 . ignore)
2647;; (regexp2 . subscribe)...)
2648;; When handling new newsgroups, groups that match a `ignore' regexp
2649;; will be ignored, and groups that match a `subscribe' regexp will be
2650;; subscribed. A line like
2651;; options -n !all rec.all
2652;; will lead to a list that looks like
2653;; (("^rec\\..+" . subscribe)
2654;; ("^.+" . ignore))
2655;; So all "rec.*" groups will be subscribed, while all the other
2656;; groups will be ignored. Note that "options -n !all rec.all" is very
2657;; different from "options -n rec.all !all".
2658(defun gnus-newsrc-parse-options (options)
2659 (let (out eol)
2660 (save-excursion
2661 (gnus-set-work-buffer)
2662 (insert (regexp-quote options))
2663 ;; First we treat all continuation lines.
2664 (goto-char (point-min))
2665 (while (re-search-forward "\n[ \t]+" nil t)
2666 (replace-match " " t t))
2667 ;; Then we transform all "all"s into ".+"s.
2668 (goto-char (point-min))
2669 (while (re-search-forward "\\ball\\b" nil t)
2670 (replace-match ".+" t t))
2671 (goto-char (point-min))
2672 ;; We remove all other options than the "-n" ones.
2673 (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2674 (replace-match " ")
2675 (forward-char -1))
2676 (goto-char (point-min))
2677
2678 ;; We are only interested in "options -n" lines - we
2679 ;; ignore the other option lines.
2680 (while (re-search-forward "[ \t]-n" nil t)
2681 (setq eol
2682 (or (save-excursion
01c52d31 2683 (and (re-search-forward "[ \t]-n" (point-at-eol) t)
eec82323 2684 (- (point) 2)))
01c52d31 2685 (point-at-eol)))
eec82323
LMI
2686 ;; Search for all "words"...
2687 (while (re-search-forward "[^ \t,\n]+" eol t)
16409b0b 2688 (if (eq (char-after (match-beginning 0)) ?!)
eec82323
LMI
2689 ;; If the word begins with a bang (!), this is a "not"
2690 ;; spec. We put this spec (minus the bang) and the
2691 ;; symbol `ignore' into the list.
2692 (push (cons (concat
2693 "^" (buffer-substring
2694 (1+ (match-beginning 0))
6748645f
LMI
2695 (match-end 0))
2696 "\\($\\|\\.\\)")
eec82323
LMI
2697 'ignore)
2698 out)
2699 ;; There was no bang, so this is a "yes" spec.
6748645f 2700 (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
eec82323
LMI
2701 'subscribe)
2702 out))))
2703
2704 (setq gnus-newsrc-options-n out))))
2705
23f87bed
MB
2706(eval-and-compile
2707 (defalias 'gnus-long-file-names
2708 (if (fboundp 'msdos-long-file-names)
2709 'msdos-long-file-names
2710 (lambda () t))))
2711
eec82323
LMI
2712(defun gnus-save-newsrc-file (&optional force)
2713 "Save .newsrc file."
2714 ;; Note: We cannot save .newsrc file if all newsgroups are removed
2715 ;; from the variable gnus-newsrc-alist.
2716 (when (and (or gnus-newsrc-alist gnus-killed-list)
2717 gnus-current-startup-file)
54506618
MB
2718 ;; Save agent range limits for the currently active method.
2719 (when gnus-agent
2720 (gnus-agent-save-local force))
2721
eec82323
LMI
2722 (save-excursion
2723 (if (and (or gnus-use-dribble-file gnus-slave)
2724 (not force)
2725 (or (not gnus-dribble-buffer)
2726 (not (buffer-name gnus-dribble-buffer))
20a673b2 2727 (zerop (with-current-buffer gnus-dribble-buffer
eec82323
LMI
2728 (buffer-size)))))
2729 (gnus-message 4 "(No changes need to be saved)")
6748645f 2730 (gnus-run-hooks 'gnus-save-newsrc-hook)
eec82323
LMI
2731 (if gnus-slave
2732 (gnus-slave-save-newsrc)
2733 ;; Save .newsrc.
2734 (when gnus-save-newsrc-file
2735 (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2736 (gnus-gnus-to-newsrc-format)
2737 (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
54506618 2738
eec82323 2739 ;; Save .newsrc.eld.
6748645f 2740 (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
eec82323 2741 (make-local-variable 'version-control)
23f87bed 2742 (setq version-control gnus-backup-startup-file)
eec82323
LMI
2743 (setq buffer-file-name
2744 (concat gnus-current-startup-file ".eld"))
2745 (setq default-directory (file-name-directory buffer-file-name))
16409b0b 2746 (buffer-disable-undo)
eec82323 2747 (erase-buffer)
23f87bed
MB
2748 (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2749
2750 (if gnus-save-startup-file-via-temp-buffer
2751 (let ((coding-system-for-write gnus-ding-file-coding-system)
2752 (standard-output (current-buffer)))
2753 (gnus-gnus-to-quick-newsrc-format)
2754 (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2755 (save-buffer))
2756 (let ((coding-system-for-write gnus-ding-file-coding-system)
2757 (version-control gnus-backup-startup-file)
2758 (startup-file (concat gnus-current-startup-file ".eld"))
2759 (working-dir (file-name-directory gnus-current-startup-file))
2760 working-file
2761 (i -1))
2762 ;; Generate the name of a non-existent file.
2763 (while (progn (setq working-file
2764 (format
2765 (if (and (eq system-type 'ms-dos)
2766 (not (gnus-long-file-names)))
2767 "%s#%d.tm#" ; MSDOS limits files to 8+3
7c2fb837 2768 "%s#tmp#%d")
23f87bed
MB
2769 working-dir (setq i (1+ i))))
2770 (file-exists-p working-file)))
2771
2772 (unwind-protect
2773 (progn
2774 (gnus-with-output-to-file working-file
2775 (gnus-gnus-to-quick-newsrc-format)
2776 (gnus-run-hooks 'gnus-save-quick-newsrc-hook))
2777
2778 ;; These bindings will mislead the current buffer
2779 ;; into thinking that it is visiting the startup
2780 ;; file.
2781 (let ((buffer-backed-up nil)
2782 (buffer-file-name startup-file)
2783 (file-precious-flag t)
2784 (setmodes (file-modes startup-file)))
2785 ;; Backup the current version of the startup file.
2786 (backup-buffer)
2787
2788 ;; Replace the existing startup file with the temp file.
2789 (rename-file working-file startup-file t)
01c52d31 2790 (gnus-set-file-modes startup-file setmodes)))
23f87bed
MB
2791 (condition-case nil
2792 (delete-file working-file)
2793 (file-error nil)))))
2794
2795 (gnus-kill-buffer (current-buffer))
eec82323
LMI
2796 (gnus-message
2797 5 "Saving %s.eld...done" gnus-current-startup-file))
2798 (gnus-dribble-delete-file)
2799 (gnus-group-set-mode-line)))))
2800
23f87bed
MB
2801(defun gnus-gnus-to-quick-newsrc-format (&optional minimal name &rest specific-variables)
2802 "Print Gnus variables such as `gnus-newsrc-alist' in Lisp format."
627e1fdf 2803 (princ (format ";; -*- mode:emacs-lisp; coding: %s; -*-\n"
a37d67dd 2804 gnus-ding-file-coding-system))
23f87bed
MB
2805 (if name
2806 (princ (format ";; %s\n" name))
2807 (princ ";; Gnus startup file.\n"))
16409b0b 2808
23f87bed
MB
2809 (unless minimal
2810 (princ "\
6748645f
LMI
2811;; Never delete this file -- if you want to force Gnus to read the
2812;; .newsrc file (if you have one), touch .newsrc instead.\n")
23f87bed
MB
2813 (princ "(setq gnus-newsrc-file-version ")
2814 (princ (gnus-prin1-to-string gnus-version))
2815 (princ ")\n"))
2816
2817 (let* ((print-quoted t)
2818 (print-readably t)
2819 (print-escape-multibyte nil)
2820 (print-escape-nonascii t)
2821 (print-length nil)
2822 (print-level nil)
7dafe00b 2823 (print-circle nil)
23f87bed
MB
2824 (print-escape-newlines t)
2825 (gnus-killed-list
eec82323
LMI
2826 (if (and gnus-save-killed-list
2827 (stringp gnus-save-killed-list))
2828 (gnus-strip-killed-list)
2829 gnus-killed-list))
2830 (variables
23f87bed
MB
2831 (or specific-variables
2832 (if gnus-save-killed-list gnus-variable-list
2833 ;; Remove the `gnus-killed-list' from the list of variables
2834 ;; to be saved, if required.
2835 (delq 'gnus-killed-list (copy-sequence gnus-variable-list)))))
eec82323
LMI
2836 ;; Peel off the "dummy" group.
2837 (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2838 variable)
2839 ;; Insert the variables into the file.
2840 (while variables
2841 (when (and (boundp (setq variable (pop variables)))
2842 (symbol-value variable))
01c52d31 2843 (princ "\n(setq ")
23f87bed
MB
2844 (princ (symbol-name variable))
2845 (princ " '")
2846 (prin1 (symbol-value variable))
2847 (princ ")\n")))))
eec82323
LMI
2848
2849(defun gnus-strip-killed-list ()
2850 "Return the killed list minus the groups that match `gnus-save-killed-list'."
2851 (let ((list gnus-killed-list)
2852 olist)
2853 (while list
6748645f 2854 (when (string-match gnus-save-killed-list (car list))
eec82323
LMI
2855 (push (car list) olist))
2856 (pop list))
2857 (nreverse olist)))
2858
2859(defun gnus-gnus-to-newsrc-format ()
2860 ;; Generate and save the .newsrc file.
20a673b2 2861 (with-current-buffer (create-file-buffer gnus-current-startup-file)
eec82323
LMI
2862 (let ((newsrc (cdr gnus-newsrc-alist))
2863 (standard-output (current-buffer))
2864 info ranges range method)
2865 (setq buffer-file-name gnus-current-startup-file)
2866 (setq default-directory (file-name-directory buffer-file-name))
16409b0b 2867 (buffer-disable-undo)
eec82323 2868 (erase-buffer)
01c52d31
MB
2869 ;; Use a unibyte buffer since group names are unibyte strings;
2870 ;; in particular, non-ASCII group names are the ones encoded by
2871 ;; a certain coding system.
2872 (mm-disable-multibyte)
eec82323
LMI
2873 ;; Write options.
2874 (when gnus-newsrc-options
2875 (insert gnus-newsrc-options))
2876 ;; Write subscribed and unsubscribed.
2877 (while (setq info (pop newsrc))
2878 ;; Don't write foreign groups to .newsrc.
2879 (when (or (null (setq method (gnus-info-method info)))
2880 (equal method "native")
2881 (inline (gnus-server-equal method gnus-select-method)))
2882 (insert (gnus-info-group info)
2883 (if (> (gnus-info-level info) gnus-level-subscribed)
2884 "!" ":"))
2885 (when (setq ranges (gnus-info-read info))
2886 (insert " ")
2887 (if (not (listp (cdr ranges)))
2888 (if (= (car ranges) (cdr ranges))
2889 (princ (car ranges))
2890 (princ (car ranges))
2891 (insert "-")
2892 (princ (cdr ranges)))
2893 (while (setq range (pop ranges))
2894 (if (or (atom range) (= (car range) (cdr range)))
2895 (princ (or (and (atom range) range) (car range)))
2896 (princ (car range))
2897 (insert "-")
2898 (princ (cdr range)))
2899 (when ranges
2900 (insert ",")))))
2901 (insert "\n")))
2902 (make-local-variable 'version-control)
2903 (setq version-control 'never)
2904 ;; It has been reported that sometime the modtime on the .newsrc
2905 ;; file seems to be off. We really do want to overwrite it, so
2906 ;; we clear the modtime here before saving. It's a bit odd,
2907 ;; though...
2908 ;; sometimes the modtime clear isn't sufficient. most brute force:
2909 ;; delete the silly thing entirely first. but this fails to provide
2910 ;; such niceties as .newsrc~ creation.
2911 (if gnus-modtime-botch
2912 (delete-file gnus-startup-file)
2913 (clear-visited-file-modtime))
6748645f 2914 (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
01c52d31
MB
2915 (let ((coding-system-for-write 'raw-text))
2916 (save-buffer))
eec82323
LMI
2917 (kill-buffer (current-buffer)))))
2918
2919\f
2920;;;
2921;;; Slave functions.
2922;;;
2923
6748645f
LMI
2924(defvar gnus-slave-mode nil)
2925
2926(defun gnus-slave-mode ()
2927 "Minor mode for slave Gnusae."
bbf52f1e
SM
2928 ;; FIXME: gnus-slave-mode appears to never be set (i.e. it'll always be nil):
2929 ;; Remove, or fix and use define-minor-mode.
01c52d31 2930 (add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
6748645f
LMI
2931 (gnus-run-hooks 'gnus-slave-mode-hook))
2932
eec82323 2933(defun gnus-slave-save-newsrc ()
20a673b2 2934 (with-current-buffer gnus-dribble-buffer
eec82323 2935 (let ((slave-name
3efe5554 2936 (mm-make-temp-file (concat gnus-current-startup-file "-slave-")))
eec82323
LMI
2937 (modes (ignore-errors
2938 (file-modes (concat gnus-current-startup-file ".eld")))))
2d2820a4 2939 (let ((coding-system-for-write gnus-ding-file-coding-system))
16409b0b 2940 (gnus-write-buffer slave-name))
eec82323 2941 (when modes
01c52d31 2942 (gnus-set-file-modes slave-name modes)))))
eec82323
LMI
2943
2944(defun gnus-master-read-slave-newsrc ()
2945 (let ((slave-files
2946 (directory-files
2947 (file-name-directory gnus-current-startup-file)
2948 t (concat
2949 "^" (regexp-quote
2950 (concat
2951 (file-name-nondirectory gnus-current-startup-file)
2952 "-slave-")))
2953 t))
2954 file)
2955 (if (not slave-files)
2956 () ; There are no slave files to read.
2957 (gnus-message 7 "Reading slave newsrcs...")
20a673b2 2958 (with-current-buffer (gnus-get-buffer-create " *gnus slave*")
eec82323
LMI
2959 (setq slave-files
2960 (sort (mapcar (lambda (file)
2961 (list (nth 5 (file-attributes file)) file))
2962 slave-files)
2963 (lambda (f1 f2)
2964 (or (< (caar f1) (caar f2))
2965 (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2966 (while slave-files
2967 (erase-buffer)
2968 (setq file (nth 1 (car slave-files)))
16409b0b 2969 (nnheader-insert-file-contents file)
eec82323
LMI
2970 (when (condition-case ()
2971 (progn
2972 (eval-buffer (current-buffer))
2973 t)
2974 (error
2975 (gnus-error 3.2 "Possible error in %s" file)
2976 nil))
2977 (unless gnus-slave ; Slaves shouldn't delete these files.
2978 (ignore-errors
2979 (delete-file file))))
2980 (setq slave-files (cdr slave-files))))
2981 (gnus-dribble-touch)
2982 (gnus-message 7 "Reading slave newsrcs...done"))))
2983
2984\f
2985;;;
2986;;; Group description.
2987;;;
2988
2989(defun gnus-read-all-descriptions-files ()
2990 (let ((methods (cons gnus-select-method
2991 (nconc
2992 (when (gnus-archive-server-wanted-p)
2993 (list "archive"))
2994 gnus-secondary-select-methods))))
2995 (while methods
2996 (gnus-read-descriptions-file (car methods))
2997 (setq methods (cdr methods)))
2998 t))
2999
3000(defun gnus-read-descriptions-file (&optional method)
3001 (let ((method (or method gnus-select-method))
3002 group)
3003 (when (stringp method)
3004 (setq method (gnus-server-to-method method)))
3005 ;; We create the hashtable whether we manage to read the desc file
3006 ;; to avoid trying to re-read after a failed read.
3007 (unless gnus-description-hashtb
3008 (setq gnus-description-hashtb
3009 (gnus-make-hashtable (length gnus-active-hashtb))))
3010 ;; Mark this method's desc file as read.
3011 (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
3012 gnus-description-hashtb)
3013
3014 (gnus-message 5 "Reading descriptions file via %s..." (car method))
3015 (cond
16409b0b
GM
3016 ((null (gnus-get-function method 'request-list-newsgroups t))
3017 t)
eec82323
LMI
3018 ((not (gnus-check-server method))
3019 (gnus-message 1 "Couldn't open server")
3020 nil)
3021 ((not (gnus-request-list-newsgroups method))
3022 (gnus-message 1 "Couldn't read newsgroups descriptions")
3023 nil)
3024 (t
3025 (save-excursion
bbf52f1e 3026 ;; FIXME: Shouldn't save-restriction be done after set-buffer?
eec82323
LMI
3027 (save-restriction
3028 (set-buffer nntp-server-buffer)
3029 (goto-char (point-min))
3030 (when (or (search-forward "\n.\n" nil t)
3031 (goto-char (point-max)))
3032 (beginning-of-line)
3033 (narrow-to-region (point-min) (point)))
3034 ;; If these are groups from a foreign select method, we insert the
3035 ;; group prefix in front of the group names.
3036 (and method (not (inline
3037 (gnus-server-equal
3038 (gnus-server-get-method nil method)
3039 (gnus-server-get-method
3040 nil gnus-select-method))))
3041 (let ((prefix (gnus-group-prefixed-name "" method)))
3042 (goto-char (point-min))
3043 (while (and (not (eobp))
3044 (progn (insert prefix)
3045 (zerop (forward-line 1)))))))
3046 (goto-char (point-min))
3047 (while (not (eobp))
3048 ;; If we get an error, we set group to 0, which is not a
3049 ;; symbol...
3050 (setq group
3051 (condition-case ()
3052 (let ((obarray gnus-description-hashtb))
3053 ;; Group is set to a symbol interned in this
3054 ;; hash table.
3055 (read nntp-server-buffer))
3056 (error 0)))
3057 (skip-chars-forward " \t")
3058 ;; ... which leads to this line being effectively ignored.
3059 (when (symbolp group)
23f87bed
MB
3060 (let* ((str (buffer-substring
3061 (point) (progn (end-of-line) (point))))
3062 (name (symbol-name group))
3063 (charset
3064 (or (gnus-group-name-charset method name)
3065 (gnus-parameter-charset name)
3066 gnus-default-charset)))
3067 ;; Fixme: Don't decode in unibyte mode.
3068 (when (and str charset (featurep 'mule))
3069 (setq str (mm-decode-coding-string str charset)))
0791fc56 3070 (set group str)))
eec82323
LMI
3071 (forward-line 1))))
3072 (gnus-message 5 "Reading descriptions file...done")
3073 t))))
3074
3075(defun gnus-group-get-description (group)
3076 "Get the description of a group by sending XGTITLE to the server."
3077 (when (gnus-request-group-description group)
20a673b2 3078 (with-current-buffer nntp-server-buffer
eec82323
LMI
3079 (goto-char (point-min))
3080 (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
3081 (match-string 1)))))
3082
3083;;;###autoload
3084(defun gnus-declare-backend (name &rest abilities)
23f87bed 3085 "Declare back end NAME with ABILITIES as a Gnus back end."
eec82323
LMI
3086 (setq gnus-valid-select-methods
3087 (nconc gnus-valid-select-methods
16409b0b
GM
3088 (list (apply 'list name abilities))))
3089 (gnus-redefine-select-method-widget))
eec82323
LMI
3090
3091(defun gnus-set-default-directory ()
3092 "Set the default directory in the current buffer to `gnus-default-directory'.
3093If this variable is nil, don't do anything."
3094 (setq default-directory
3095 (if (and gnus-default-directory
3096 (file-exists-p gnus-default-directory))
3097 (file-name-as-directory (expand-file-name gnus-default-directory))
3098 default-directory)))
3099
0302b80e
SM
3100(defun gnus-display-time-event-handler ()
3101 (if (and (fboundp 'display-time-event-handler)
3102 (gnus-boundp 'display-time-timer))
3103 (display-time-event-handler)))
23f87bed 3104
01c52d31
MB
3105(defun gnus-check-reasonable-setup ()
3106 ;; Check whether nnml and nnfolder share a directory.
3107 (let ((display-warn
3108 (if (fboundp 'display-warning)
3109 'display-warning
3110 (lambda (type message)
3111 (if noninteractive
3112 (message "Warning (%s): %s" type message)
3113 (let (window)
3114 (with-current-buffer (get-buffer-create "*Warnings*")
3115 (goto-char (point-max))
3116 (unless (bolp)
3117 (insert "\n"))
3118 (insert (format "Warning (%s): %s\n" type message))
3119 (setq window (display-buffer (current-buffer)))
3120 (set-window-start
3121 window
3122 (prog2
3123 (forward-line (- 1 (window-height window)))
3124 (point)
3125 (goto-char (point-max))))))))))
3126 method active actives match)
3127 (dolist (server gnus-server-alist)
3128 (setq method (gnus-server-to-method server)
3129 active (intern (format "%s-active-file" (car method))))
3130 (when (and (member (car method) '(nnml nnfolder))
3131 (gnus-server-opened method)
3132 (boundp active))
3133 (when (setq match (assoc (symbol-value active) actives))
3134 (funcall display-warn 'gnus-server
3135 (format "%s and %s share the same active file %s"
3136 (car method)
3137 (cadr match)
3138 (car match))))
3139 (push (list (symbol-value active) method) actives)))))
23f87bed 3140
eec82323
LMI
3141(provide 'gnus-start)
3142
3143;;; gnus-start.el ends here