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