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