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