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