62cbfddbeb788621552968c95e82c750e9d64c9e
[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-clear-system ()
629   "Clear all variables and buffers."
630   ;; Clear gnus variables.
631   (gnus-clear-quick-file-variables)
632   ;; Clear other internal variables.
633   (setq gnus-list-of-killed-groups nil
634         gnus-have-read-active-file nil
635         gnus-newsrc-alist nil
636         gnus-newsrc-hashtb nil
637         gnus-killed-list nil
638         gnus-zombie-list nil
639         gnus-killed-hashtb nil
640         gnus-active-hashtb nil
641         gnus-moderated-hashtb nil
642         gnus-description-hashtb nil
643         gnus-current-headers nil
644         gnus-thread-indent-array nil
645         gnus-newsgroup-headers nil
646         gnus-newsgroup-name nil
647         gnus-server-alist nil
648         gnus-group-list-mode nil
649         gnus-opened-servers nil
650         gnus-group-mark-positions nil
651         gnus-newsgroup-data nil
652         gnus-newsgroup-unreads nil
653         nnoo-state-alist nil
654         gnus-current-select-method nil
655         nnmail-split-history nil
656         gnus-ephemeral-servers nil)
657   (gnus-shutdown 'gnus)
658   ;; Kill the startup file.
659   (and gnus-current-startup-file
660        (get-file-buffer gnus-current-startup-file)
661        (kill-buffer (get-file-buffer gnus-current-startup-file)))
662   ;; Clear the dribble buffer.
663   (gnus-dribble-clear)
664   ;; Kill global KILL file buffer.
665   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
666     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
667   (gnus-kill-buffer nntp-server-buffer)
668   ;; Kill Gnus buffers.
669   (let ((buffers (gnus-buffers)))
670     (when buffers
671       (mapcar 'kill-buffer buffers)))
672   ;; Remove Gnus frames.
673   (gnus-kill-gnus-frames))
674
675 (defun gnus-no-server-1 (&optional arg slave)
676   "Read network news.
677 If ARG is a positive number, Gnus will use that as the
678 startup level.  If ARG is nil, Gnus will be started at level 2.
679 If ARG is non-nil and not a positive number, Gnus will
680 prompt the user for the name of an NNTP server to use.
681 As opposed to `gnus', this command will not connect to the local server."
682   (interactive "P")
683   (let ((val (or arg (1- gnus-level-default-subscribed))))
684     (gnus val t slave)
685     (make-local-variable 'gnus-group-use-permanent-levels)
686     (setq gnus-group-use-permanent-levels val)))
687
688 (defun gnus-1 (&optional arg dont-connect slave)
689   "Read network news.
690 If ARG is non-nil and a positive number, Gnus will use that as the
691 startup level.  If ARG is non-nil and not a positive number, Gnus will
692 prompt the user for the name of an NNTP server to use."
693   (interactive "P")
694
695   (if (gnus-alive-p)
696       (progn
697         (switch-to-buffer gnus-group-buffer)
698         (gnus-group-get-new-news
699          (and (numberp arg)
700               (> arg 0)
701               (max (car gnus-group-list-mode) arg))))
702
703     (gnus-clear-system)
704     (gnus-splash)
705     (gnus-run-hooks 'gnus-before-startup-hook)
706     (nnheader-init-server-buffer)
707     (setq gnus-slave slave)
708     (gnus-read-init-file)
709
710     (when gnus-simple-splash
711       (setq gnus-simple-splash nil)
712       (cond
713        ((featurep 'xemacs)
714         (gnus-xmas-splash))
715        ((and window-system
716              (= (frame-height) (1+ (window-height))))
717         (gnus-x-splash))))
718
719     (let ((level (and (numberp arg) (> arg 0) arg))
720           did-connect)
721       (unwind-protect
722           (progn
723             (unless dont-connect
724               (setq did-connect
725                     (gnus-start-news-server (and arg (not level))))))
726         (if (and (not dont-connect)
727                  (not did-connect))
728             (gnus-group-quit)
729           (gnus-run-hooks 'gnus-startup-hook)
730           ;; NNTP server is successfully open.
731
732           ;; Find the current startup file name.
733           (setq gnus-current-startup-file
734                 (gnus-make-newsrc-file gnus-startup-file))
735
736           ;; Read the dribble file.
737           (when (or gnus-slave gnus-use-dribble-file)
738             (gnus-dribble-read-file))
739
740           ;; Allow using GroupLens predictions.
741           (when gnus-use-grouplens
742             (bbb-login)
743             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
744
745           ;; Do the actual startup.
746           (gnus-setup-news nil level dont-connect)
747           (gnus-run-hooks 'gnus-setup-news-hook)
748           (gnus-start-draft-setup)
749           ;; Generate the group buffer.
750           (gnus-group-list-groups level)
751           (gnus-group-first-unread-group)
752           (gnus-configure-windows 'group)
753           (gnus-group-set-mode-line)
754           ;; For reading Info.
755           (set-language-info "Japanese" 'gnus-info "gnus-ja")
756           (gnus-run-hooks 'gnus-started-hook))))))
757
758 (defun gnus-start-draft-setup ()
759   "Make sure the draft group exists."
760   (gnus-request-create-group "drafts" '(nndraft ""))
761   (unless (gnus-gethash "nndraft:drafts" gnus-newsrc-hashtb)
762     (let ((gnus-level-default-subscribed 1))
763       (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
764     (gnus-group-set-parameter
765      "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
766
767 ;;;###autoload
768 (defun gnus-unload ()
769   "Unload all Gnus features.
770 \(For some value of `all' or `Gnus'.)  Currently, features whose names
771 have prefixes `gnus-', `nn', `mm-' or `rfc' are unloaded.  Use
772 cautiously -- unloading may cause trouble."
773   (interactive)
774   (dolist (feature features)
775     (if (string-match "^\\(gnus-\\|nn\\|mm-\\|rfc\\)" (symbol-name feature))
776         (unload-feature feature 'force))))
777
778 \f
779 ;;;
780 ;;; Dribble file
781 ;;;
782
783 (defvar gnus-dribble-ignore nil)
784 (defvar gnus-dribble-eval-file nil)
785
786 (defun gnus-dribble-file-name ()
787   "Return the dribble file for the current .newsrc."
788   (concat
789    (if gnus-dribble-directory
790        (concat (file-name-as-directory gnus-dribble-directory)
791                (file-name-nondirectory gnus-current-startup-file))
792      gnus-current-startup-file)
793    "-dribble"))
794
795 (defun gnus-dribble-enter (string)
796   "Enter STRING into the dribble buffer."
797   (when (and (not gnus-dribble-ignore)
798              gnus-dribble-buffer
799              (buffer-name gnus-dribble-buffer))
800     (let ((obuf (current-buffer)))
801       (set-buffer gnus-dribble-buffer)
802       (goto-char (point-max))
803       (insert string "\n")
804       (set-window-point (get-buffer-window (current-buffer)) (point-max))
805       (bury-buffer gnus-dribble-buffer)
806       (save-excursion
807         (set-buffer gnus-group-buffer)
808         (gnus-group-set-mode-line))
809       (set-buffer obuf))))
810
811 (defun gnus-dribble-touch ()
812   "Touch the dribble buffer."
813   (gnus-dribble-enter ""))
814
815 (defun gnus-dribble-read-file ()
816   "Read the dribble file from disk."
817   (let ((dribble-file (gnus-dribble-file-name)))
818     (save-excursion
819       (set-buffer (setq gnus-dribble-buffer
820                         (gnus-get-buffer-create
821                          (file-name-nondirectory dribble-file))))
822       (erase-buffer)
823       (setq buffer-file-name dribble-file)
824       (auto-save-mode t)
825       (buffer-disable-undo)
826       (bury-buffer (current-buffer))
827       (set-buffer-modified-p nil)
828       (let ((auto (make-auto-save-file-name))
829             (gnus-dribble-ignore t)
830             modes)
831         (when (or (file-exists-p auto) (file-exists-p dribble-file))
832           ;; Load whichever file is newest -- the auto save file
833           ;; or the "real" file.
834           (if (file-newer-than-file-p auto dribble-file)
835               (nnheader-insert-file-contents auto)
836             (nnheader-insert-file-contents dribble-file))
837           (unless (zerop (buffer-size))
838             (set-buffer-modified-p t))
839           ;; Set the file modes to reflect the .newsrc file modes.
840           (save-buffer)
841           (when (and (file-exists-p gnus-current-startup-file)
842                      (file-exists-p dribble-file)
843                      (setq modes (file-modes gnus-current-startup-file)))
844             (set-file-modes dribble-file modes))
845           ;; Possibly eval the file later.
846           (when (or gnus-always-read-dribble-file
847                     (gnus-y-or-n-p
848                      "Gnus auto-save file exists.  Do you want to read it? "))
849             (setq gnus-dribble-eval-file t)))))))
850
851 (defun gnus-dribble-eval-file ()
852   (when gnus-dribble-eval-file
853     (setq gnus-dribble-eval-file nil)
854     (save-excursion
855       (let ((gnus-dribble-ignore t))
856         (set-buffer gnus-dribble-buffer)
857         (eval-buffer (current-buffer))))))
858
859 (defun gnus-dribble-delete-file ()
860   (when (file-exists-p (gnus-dribble-file-name))
861     (delete-file (gnus-dribble-file-name)))
862   (when gnus-dribble-buffer
863     (save-excursion
864       (set-buffer gnus-dribble-buffer)
865       (let ((auto (make-auto-save-file-name)))
866         (when (file-exists-p auto)
867           (delete-file auto))
868         (erase-buffer)
869         (set-buffer-modified-p nil)))))
870
871 (defun gnus-dribble-save ()
872   (when (and gnus-dribble-buffer
873              (buffer-name gnus-dribble-buffer))
874     (save-excursion
875       (set-buffer gnus-dribble-buffer)
876       (save-buffer))))
877
878 (defun gnus-dribble-clear ()
879   (when (gnus-buffer-exists-p gnus-dribble-buffer)
880     (save-excursion
881       (set-buffer gnus-dribble-buffer)
882       (erase-buffer)
883       (set-buffer-modified-p nil)
884       (setq buffer-saved-size (buffer-size)))))
885
886 \f
887 ;;;
888 ;;; Active & Newsrc File Handling
889 ;;;
890
891 (defun gnus-setup-news (&optional rawfile level dont-connect)
892   "Setup news information.
893 If RAWFILE is non-nil, the .newsrc file will also be read.
894 If LEVEL is non-nil, the news will be set up at level LEVEL."
895   (require 'nnmail)
896   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
897         ;; Binding this variable will inhibit multiple fetchings
898         ;; of the same mail source.
899         (nnmail-fetched-sources (list t)))
900
901     (when init
902       ;; Clear some variables to re-initialize news information.
903       (setq gnus-newsrc-alist nil
904             gnus-active-hashtb nil)
905       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
906       (gnus-read-newsrc-file rawfile))
907
908     ;; Make sure the archive server is available to all and sundry.
909     (when gnus-message-archive-method
910       (setq gnus-server-alist (delq (assoc "archive" gnus-server-alist)
911                                     gnus-server-alist))
912       (push (cons "archive" gnus-message-archive-method)
913             gnus-server-alist))
914
915     ;; If we don't read the complete active file, we fill in the
916     ;; hashtb here.
917     (when (or (null gnus-read-active-file)
918               (eq gnus-read-active-file 'some))
919       (gnus-update-active-hashtb-from-killed))
920
921     ;; Read the active file and create `gnus-active-hashtb'.
922     ;; If `gnus-read-active-file' is nil, then we just create an empty
923     ;; hash table.  The partial filling out of the hash table will be
924     ;; done in `gnus-get-unread-articles'.
925     (and gnus-read-active-file
926          (not level)
927          (gnus-read-active-file nil dont-connect))
928
929     (unless gnus-active-hashtb
930       (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
931
932     ;; Initialize the cache.
933     (when gnus-use-cache
934       (gnus-cache-open))
935
936     ;; Possibly eval the dribble file.
937     (and init
938          (or gnus-use-dribble-file gnus-slave)
939          (gnus-dribble-eval-file))
940
941     ;; Slave Gnusii should then clear the dribble buffer.
942     (when (and init gnus-slave)
943       (gnus-dribble-clear))
944
945     (gnus-update-format-specifications)
946
947     ;; See whether we need to read the description file.
948     (when (and (boundp 'gnus-group-line-format)
949                (stringp gnus-group-line-format)
950                (let ((case-fold-search nil))
951                  (string-match "%[-,0-9]*D" gnus-group-line-format))
952                (not gnus-description-hashtb)
953                (not dont-connect)
954                gnus-read-active-file)
955       (gnus-read-all-descriptions-files))
956
957     ;; Find new newsgroups and treat them.
958     (when (and init gnus-check-new-newsgroups (not level)
959                (gnus-check-server gnus-select-method)
960                (not gnus-slave)
961                gnus-plugged)
962       (gnus-find-new-newsgroups))
963
964     ;; We might read in new NoCeM messages here.
965     (when (and gnus-use-nocem
966                (not level)
967                (not dont-connect))
968       (gnus-nocem-scan-groups))
969
970     ;; Read any slave files.
971     (gnus-master-read-slave-newsrc)
972
973     ;; Find the number of unread articles in each non-dead group.
974     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
975       (gnus-get-unread-articles level))
976
977     (when (and init gnus-check-bogus-newsgroups
978                gnus-read-active-file (not level)
979                (gnus-server-opened gnus-select-method))
980       (gnus-check-bogus-newsgroups))))
981
982 (defun gnus-call-subscribe-functions (method group)
983   "Call METHOD to subscribe GROUP.
984 If no function returns `non-nil', call `gnus-subscribe-zombies'."
985   (unless (cond
986            ((gnus-functionp method)
987             (funcall method group))
988            ((listp method)
989             (catch 'found
990               (dolist (func method)
991                 (if (funcall func group)
992                     (throw 'found t)))
993               nil))
994            (t nil))
995     (gnus-subscribe-zombies group)))
996
997 (defun gnus-find-new-newsgroups (&optional arg)
998   "Search for new newsgroups and add them.
999 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
1000 The `-n' option line from .newsrc is respected.
1001
1002 With 1 C-u, use the `ask-server' method to query the server for new
1003 groups.
1004 With 2 C-u's, use most complete method possible to query the server
1005 for new groups, and subscribe the new groups as zombies."
1006   (interactive "p")
1007   (let* ((gnus-subscribe-newsgroup-method
1008           gnus-subscribe-newsgroup-method)
1009          (check (cond
1010                  ((or (and (= (or arg 1) 4)
1011                            (not (listp gnus-check-new-newsgroups)))
1012                       (null gnus-read-active-file)
1013                       (eq gnus-read-active-file 'some))
1014                   'ask-server)
1015                  ((= (or arg 1) 16)
1016                   (setq gnus-subscribe-newsgroup-method
1017                         'gnus-subscribe-zombies)
1018                   t)
1019                  (t gnus-check-new-newsgroups))))
1020     (unless (gnus-check-first-time-used)
1021       (if (or (consp check)
1022               (eq check 'ask-server))
1023           ;; Ask the server for new groups.
1024           (gnus-ask-server-for-new-groups)
1025         ;; Go through the active hashtb and look for new groups.
1026         (let ((groups 0)
1027               group new-newsgroups)
1028           (gnus-message 5 "Looking for new newsgroups...")
1029           (unless gnus-have-read-active-file
1030             (gnus-read-active-file))
1031           (setq gnus-newsrc-last-checked-date (current-time-string))
1032           (unless gnus-killed-hashtb
1033             (gnus-make-hashtable-from-killed))
1034           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
1035           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
1036           (mapatoms
1037            (lambda (sym)
1038              (if (or (null (setq group (symbol-name sym)))
1039                      (not (boundp sym))
1040                      (null (symbol-value sym))
1041                      (gnus-gethash group gnus-killed-hashtb)
1042                      (gnus-gethash group gnus-newsrc-hashtb))
1043                  ()
1044                (let ((do-sub (gnus-matches-options-n group)))
1045                  (cond
1046                   ((eq do-sub 'subscribe)
1047                    (setq groups (1+ groups))
1048                    (gnus-sethash group group gnus-killed-hashtb)
1049                    (gnus-call-subscribe-functions
1050                     gnus-subscribe-options-newsgroup-method group))
1051                   ((eq do-sub 'ignore)
1052                    nil)
1053                   (t
1054                    (setq groups (1+ groups))
1055                    (gnus-sethash group group gnus-killed-hashtb)
1056                    (if gnus-subscribe-hierarchical-interactive
1057                        (push group new-newsgroups)
1058                      (gnus-call-subscribe-functions
1059                       gnus-subscribe-newsgroup-method group)))))))
1060            gnus-active-hashtb)
1061           (when new-newsgroups
1062             (gnus-subscribe-hierarchical-interactive new-newsgroups))
1063           (if (> groups 0)
1064               (gnus-message 5 "%d new newsgroup%s arrived."
1065                             groups (if (> groups 1) "s have" " has"))
1066             (gnus-message 5 "No new newsgroups.")))))))
1067
1068 (defun gnus-matches-options-n (group)
1069   ;; Returns `subscribe' if the group is to be unconditionally
1070   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
1071   ;; no match for the group.
1072
1073   ;; First we check the two user variables.
1074   (cond
1075    ((and gnus-options-subscribe
1076          (string-match gnus-options-subscribe group))
1077     'subscribe)
1078    ((and gnus-auto-subscribed-groups
1079          (string-match gnus-auto-subscribed-groups group))
1080     'subscribe)
1081    ((and gnus-options-not-subscribe
1082          (string-match gnus-options-not-subscribe group))
1083     'ignore)
1084    ;; Then we go through the list that was retrieved from the .newsrc
1085    ;; file.  This list has elements on the form
1086    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
1087    ;; is in the reverse order of the options line) is returned.
1088    (t
1089     (let ((regs gnus-newsrc-options-n))
1090       (while (and regs
1091                   (not (string-match (caar regs) group)))
1092         (setq regs (cdr regs)))
1093       (and regs (cdar regs))))))
1094
1095 (defun gnus-ask-server-for-new-groups ()
1096   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
1097          (methods (cons gnus-select-method
1098                         (nconc
1099                          (when (gnus-archive-server-wanted-p)
1100                            (list "archive"))
1101                          (append
1102                           (and (consp gnus-check-new-newsgroups)
1103                                gnus-check-new-newsgroups)
1104                           gnus-secondary-select-methods))))
1105          (groups 0)
1106          (new-date (current-time-string))
1107          group new-newsgroups got-new method hashtb
1108          gnus-override-subscribe-method)
1109     (unless gnus-killed-hashtb
1110       (gnus-make-hashtable-from-killed))
1111     ;; Go through both primary and secondary select methods and
1112     ;; request new newsgroups.
1113     (while (setq method (gnus-server-get-method nil (pop methods)))
1114       (setq new-newsgroups nil
1115             gnus-override-subscribe-method method)
1116       (when (and (gnus-check-server method)
1117                  (gnus-request-newgroups date method))
1118         (save-excursion
1119           (setq got-new t
1120                 hashtb (gnus-make-hashtable 100))
1121           (set-buffer nntp-server-buffer)
1122           ;; Enter all the new groups into a hashtable.
1123           (gnus-active-to-gnus-format method hashtb 'ignore))
1124         ;; Now all new groups from `method' are in `hashtb'.
1125         (mapatoms
1126          (lambda (group-sym)
1127            (if (or (null (setq group (symbol-name group-sym)))
1128                    (not (boundp group-sym))
1129                    (null (symbol-value group-sym))
1130                    (gnus-gethash group gnus-newsrc-hashtb)
1131                    (member group gnus-zombie-list)
1132                    (member group gnus-killed-list))
1133                ;; The group is already known.
1134                ()
1135              ;; Make this group active.
1136              (when (symbol-value group-sym)
1137                (gnus-set-active group (symbol-value group-sym)))
1138              ;; Check whether we want it or not.
1139              (let ((do-sub (gnus-matches-options-n group)))
1140                (cond
1141                 ((eq do-sub 'subscribe)
1142                  (incf groups)
1143                  (gnus-sethash group group gnus-killed-hashtb)
1144                  (gnus-call-subscribe-functions
1145                   gnus-subscribe-options-newsgroup-method group))
1146                 ((eq do-sub 'ignore)
1147                  nil)
1148                 (t
1149                  (incf groups)
1150                  (gnus-sethash group group gnus-killed-hashtb)
1151                  (if gnus-subscribe-hierarchical-interactive
1152                      (push group new-newsgroups)
1153                    (gnus-call-subscribe-functions
1154                     gnus-subscribe-newsgroup-method group)))))))
1155          hashtb))
1156       (when new-newsgroups
1157         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
1158     (if (> groups 0)
1159         (gnus-message 5 "%d new newsgroup%s arrived"
1160                       groups (if (> groups 1) "s have" " has"))
1161       (gnus-message 5 "No new newsgroups"))
1162     (when got-new
1163       (setq gnus-newsrc-last-checked-date new-date))
1164     got-new))
1165
1166 (defun gnus-check-first-time-used ()
1167   (catch 'ended
1168     ;; First check if any of the following files exist.  If they do,
1169     ;; it's not the first time the user has used Gnus.
1170     (dolist (file (list gnus-current-startup-file
1171                         (concat gnus-current-startup-file ".el")
1172                         (concat gnus-current-startup-file ".eld")
1173                         gnus-startup-file
1174                         (concat gnus-startup-file ".el")
1175                         (concat gnus-startup-file ".eld")))
1176       (when (file-exists-p file)
1177         (throw 'ended nil)))
1178     (gnus-message 6 "First time user; subscribing you to default groups")
1179     (unless (gnus-read-active-file-p)
1180       (let ((gnus-read-active-file t))
1181         (gnus-read-active-file)))
1182     (setq gnus-newsrc-last-checked-date (current-time-string))
1183     ;; Subscribe to the default newsgroups.
1184     (let ((groups (or gnus-default-subscribed-newsgroups
1185                       gnus-backup-default-subscribed-newsgroups))
1186           group)
1187       (when (eq groups t)
1188         ;; If t, we subscribe (or not) all groups as if they were new.
1189         (mapatoms
1190          (lambda (sym)
1191            (when (setq group (symbol-name sym))
1192              (let ((do-sub (gnus-matches-options-n group)))
1193                (cond
1194                 ((eq do-sub 'subscribe)
1195                  (gnus-sethash group group gnus-killed-hashtb)
1196                  (gnus-call-subscribe-functions
1197                   gnus-subscribe-options-newsgroup-method group))
1198                 ((eq do-sub 'ignore)
1199                  nil)
1200                 (t
1201                  (push group gnus-killed-list))))))
1202          gnus-active-hashtb)
1203         (dolist (group groups)
1204           ;; Only subscribe the default groups that are activated.
1205           (when (gnus-active group)
1206             (gnus-group-change-level
1207              group gnus-level-default-subscribed gnus-level-killed)))
1208         (save-excursion
1209           (set-buffer gnus-group-buffer)
1210           (gnus-group-make-help-group))
1211         (when gnus-novice-user
1212           (gnus-message 7 "`A k' to list killed groups"))))))
1213
1214 (defun gnus-subscribe-group (group &optional previous method)
1215   "Subcribe GROUP and put it after PREVIOUS."
1216   (gnus-group-change-level
1217    (if method
1218        (list t group gnus-level-default-subscribed nil nil method)
1219      group)
1220    gnus-level-default-subscribed gnus-level-killed previous t)
1221   t)
1222
1223 ;; `gnus-group-change-level' is the fundamental function for changing
1224 ;; subscription levels of newsgroups.  This might mean just changing
1225 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1226 ;; again, which subscribes/unsubscribes a group, which is equally
1227 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
1228 ;; from 8-9 to 1-7 means that you remove the group from the list of
1229 ;; killed (or zombie) groups and add them to the (kinda) subscribed
1230 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
1231 ;; which is trivial.
1232 ;; ENTRY can either be a string (newsgroup name) or a list (if
1233 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1234 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1235 ;; entries.
1236 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1237 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
1238 ;; after.
1239 (defun gnus-group-change-level (entry level &optional oldlevel
1240                                       previous fromkilled)
1241   (let (group info active num)
1242     ;; Glean what info we can from the arguments
1243     (if (consp entry)
1244         (if fromkilled (setq group (nth 1 entry))
1245           (setq group (car (nth 2 entry))))
1246       (setq group entry))
1247     (when (and (stringp entry)
1248                oldlevel
1249                (< oldlevel gnus-level-zombie))
1250       (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
1251     (if (and (not oldlevel)
1252              (consp entry))
1253         (setq oldlevel (gnus-info-level (nth 2 entry)))
1254       (setq oldlevel (or oldlevel gnus-level-killed)))
1255     (when (stringp previous)
1256       (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
1257
1258     (if (and (>= oldlevel gnus-level-zombie)
1259              (gnus-gethash group gnus-newsrc-hashtb))
1260         ;; We are trying to subscribe a group that is already
1261         ;; subscribed.
1262         ()                              ; Do nothing.
1263
1264       (unless (gnus-ephemeral-group-p group)
1265         (gnus-dribble-enter
1266          (format "(gnus-group-change-level %S %S %S %S %S)"
1267                  group level oldlevel (car (nth 2 previous)) fromkilled)))
1268
1269       ;; Then we remove the newgroup from any old structures, if needed.
1270       ;; If the group was killed, we remove it from the killed or zombie
1271       ;; list.  If not, and it is in fact going to be killed, we remove
1272       ;; it from the newsrc hash table and assoc.
1273       (cond
1274        ((>= oldlevel gnus-level-zombie)
1275         ;; oldlevel could be wrong.
1276         (setq gnus-zombie-list (delete group gnus-zombie-list))
1277         (setq gnus-killed-list (delete group gnus-killed-list)))
1278        (t
1279         (when (and (>= level gnus-level-zombie)
1280                    entry)
1281           (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1282           (when (nth 3 entry)
1283             (setcdr (gnus-gethash (car (nth 3 entry))
1284                                   gnus-newsrc-hashtb)
1285                     (cdr entry)))
1286           (setcdr (cdr entry) (cdddr entry)))))
1287
1288       ;; Finally we enter (if needed) the list where it is supposed to
1289       ;; go, and change the subscription level.  If it is to be killed,
1290       ;; we enter it into the killed or zombie list.
1291       (cond
1292        ((>= level gnus-level-zombie)
1293         ;; Remove from the hash table.
1294         (gnus-sethash group nil gnus-newsrc-hashtb)
1295         ;; We do not enter foreign groups into the list of dead
1296         ;; groups.
1297         (unless (gnus-group-foreign-p group)
1298           (if (= level gnus-level-zombie)
1299               (push group gnus-zombie-list)
1300             (if (= oldlevel gnus-level-killed)
1301                 ;; Remove from active hashtb.
1302                 (unintern group gnus-active-hashtb)
1303               ;; Don't add it into killed-list if it was killed.
1304               (push group gnus-killed-list)))))
1305        (t
1306         ;; If the list is to be entered into the newsrc assoc, and
1307         ;; it was killed, we have to create an entry in the newsrc
1308         ;; hashtb format and fix the pointers in the newsrc assoc.
1309         (if (< oldlevel gnus-level-zombie)
1310             ;; It was alive, and it is going to stay alive, so we
1311             ;; just change the level and don't change any pointers or
1312             ;; hash table entries.
1313             (setcar (cdaddr entry) level)
1314           (if (listp entry)
1315               (setq info (cdr entry)
1316                     num (car entry))
1317             (setq active (gnus-active group))
1318             (setq num
1319                   (if active (- (1+ (cdr active)) (car active)) t))
1320             ;; Shorten the select method if possible, if we need to
1321             ;; store it at all (native groups).
1322             (let ((method (gnus-method-simplify
1323                            (or gnus-override-subscribe-method
1324                                (gnus-group-method group)))))
1325               (if method
1326                   (setq info (list group level nil nil method))
1327                 (setq info (list group level nil)))))
1328           (unless previous
1329             (setq previous
1330                   (let ((p gnus-newsrc-alist))
1331                     (while (cddr p)
1332                       (setq p (cdr p)))
1333                     p)))
1334           (setq entry (cons info (cddr previous)))
1335           (if (cdr previous)
1336               (progn
1337                 (setcdr (cdr previous) entry)
1338                 (gnus-sethash group (cons num (cdr previous))
1339                               gnus-newsrc-hashtb))
1340             (setcdr previous entry)
1341             (gnus-sethash group (cons num previous)
1342                           gnus-newsrc-hashtb))
1343           (when (cdr entry)
1344             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry))
1345           (gnus-dribble-enter
1346            (format
1347             "(gnus-group-set-info '%S)" info)))))
1348       (when gnus-group-change-level-function
1349         (funcall gnus-group-change-level-function
1350                  group level oldlevel previous)))))
1351
1352 (defun gnus-kill-newsgroup (newsgroup)
1353   "Obsolete function.  Kills a newsgroup."
1354   (gnus-group-change-level
1355    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
1356
1357 (defun gnus-check-bogus-newsgroups (&optional confirm)
1358   "Remove bogus newsgroups.
1359 If CONFIRM is non-nil, the user has to confirm the deletion of every
1360 newsgroup."
1361   (let ((newsrc (cdr gnus-newsrc-alist))
1362         bogus group entry info)
1363     (gnus-message 5 "Checking bogus newsgroups...")
1364     (unless (gnus-read-active-file-p)
1365       (gnus-read-active-file t))
1366     (when (gnus-read-active-file-p)
1367       ;; Find all bogus newsgroup that are subscribed.
1368       (while newsrc
1369         (setq info (pop newsrc)
1370               group (gnus-info-group info))
1371         (unless (or (gnus-active group) ; Active
1372                     (gnus-info-method info)) ; Foreign
1373           ;; Found a bogus newsgroup.
1374           (push group bogus)))
1375       (if confirm
1376           (map-y-or-n-p
1377            "Remove bogus group %s? "
1378            (lambda (group)
1379              ;; Remove all bogus subscribed groups by first killing them, and
1380              ;; then removing them from the list of killed groups.
1381              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1382                (gnus-group-change-level entry gnus-level-killed)
1383                (setq gnus-killed-list (delete group gnus-killed-list))))
1384            bogus '("group" "groups" "remove"))
1385         (while (setq group (pop bogus))
1386           ;; Remove all bogus subscribed groups by first killing them, and
1387           ;; then removing them from the list of killed groups.
1388           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1389             (gnus-group-change-level entry gnus-level-killed)
1390             (setq gnus-killed-list (delete group gnus-killed-list)))))
1391       ;; Then we remove all bogus groups from the list of killed and
1392       ;; zombie groups.  They are removed without confirmation.
1393       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1394             killed)
1395         (while dead-lists
1396           (setq killed (symbol-value (car dead-lists)))
1397           (while killed
1398             (unless (gnus-active (setq group (pop killed)))
1399               ;; The group is bogus.
1400               ;; !!!Slow as hell.
1401               (set (car dead-lists)
1402                    (delete group (symbol-value (car dead-lists))))))
1403           (setq dead-lists (cdr dead-lists))))
1404       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1405       (gnus-message 5 "Checking bogus newsgroups...done"))))
1406
1407 (defun gnus-check-duplicate-killed-groups ()
1408   "Remove duplicates from the list of killed groups."
1409   (interactive)
1410   (let ((killed gnus-killed-list))
1411     (while killed
1412       (gnus-message 9 "%d" (length killed))
1413       (setcdr killed (delete (car killed) (cdr killed)))
1414       (setq killed (cdr killed)))))
1415
1416 ;; We want to inline a function from gnus-cache, so we cheat here:
1417 (eval-when-compile
1418   (defvar gnus-cache-active-hashtb)
1419   (defun gnus-cache-possibly-alter-active (group active)
1420     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1421     (when gnus-cache-active-hashtb
1422       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1423         (when cache-active
1424           (when (< (car cache-active) (car active))
1425             (setcar active (car cache-active)))
1426           (when (> (cdr cache-active) (cdr active))
1427             (setcdr active (cdr cache-active))))))))
1428
1429 (defun gnus-activate-group (group &optional scan dont-check method)
1430   ;; Check whether a group has been activated or not.
1431   ;; If SCAN, request a scan of that group as well.
1432   (let ((method (or method (inline (gnus-find-method-for-group group))))
1433         active)
1434     (and (inline (gnus-check-server method))
1435          ;; We escape all bugs and quit here to make it possible to
1436          ;; continue if a group is so out-there that it reports bugs
1437          ;; and stuff.
1438          (progn
1439            (and scan
1440                 (gnus-check-backend-function 'request-scan (car method))
1441                 (gnus-request-scan group method))
1442            t)
1443          (condition-case ()
1444              (inline (gnus-request-group group dont-check method))
1445            ;;(error nil)
1446            (quit
1447             (message "Quit activating %s" group)
1448             nil))
1449          (setq active (gnus-parse-active))
1450          ;; If there are no articles in the group, the GROUP
1451          ;; command may have responded with the `(0 . 0)'.  We
1452          ;; ignore this if we already have an active entry
1453          ;; for the group.
1454          (if (and (zerop (car active))
1455                   (zerop (cdr active))
1456                   (gnus-active group))
1457              (gnus-active group)
1458            (gnus-set-active group active)
1459            ;; Return the new active info.
1460            active))))
1461
1462 (defun gnus-get-unread-articles-in-group (info active &optional update)
1463   (when active
1464     ;; Allow the backend to update the info in the group.
1465     (when (and update
1466                (gnus-request-update-info
1467                 info (inline (gnus-find-method-for-group
1468                               (gnus-info-group info)))))
1469       (gnus-activate-group (gnus-info-group info) nil t))
1470
1471     (let* ((range (gnus-info-read info))
1472            (num 0))
1473       ;; If a cache is present, we may have to alter the active info.
1474       (when (and gnus-use-cache info)
1475         (inline (gnus-cache-possibly-alter-active
1476                  (gnus-info-group info) active)))
1477       ;; Modify the list of read articles according to what articles
1478       ;; are available; then tally the unread articles and add the
1479       ;; number to the group hash table entry.
1480       (cond
1481        ((zerop (cdr active))
1482         (setq num 0))
1483        ((not range)
1484         (setq num (- (1+ (cdr active)) (car active))))
1485        ((not (listp (cdr range)))
1486         ;; Fix a single (num . num) range according to the
1487         ;; active hash table.
1488         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1489         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1490         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1491         ;; Compute number of unread articles.
1492         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1493        (t
1494         ;; The read list is a list of ranges.  Fix them according to
1495         ;; the active hash table.
1496         ;; First peel off any elements that are below the lower
1497         ;; active limit.
1498         (while (and (cdr range)
1499                     (>= (car active)
1500                         (or (and (atom (cadr range)) (cadr range))
1501                             (caadr range))))
1502           (if (numberp (car range))
1503               (setcar range
1504                       (cons (car range)
1505                             (or (and (numberp (cadr range))
1506                                      (cadr range))
1507                                 (cdadr range))))
1508             (setcdr (car range)
1509                     (or (and (numberp (nth 1 range)) (nth 1 range))
1510                         (cdadr range))))
1511           (setcdr range (cddr range)))
1512         ;; Adjust the first element to be the same as the lower limit.
1513         (when (and (not (atom (car range)))
1514                    (< (cdar range) (car active)))
1515           (setcdr (car range) (1- (car active))))
1516         ;; Then we want to peel off any elements that are higher
1517         ;; than the upper active limit.
1518         (let ((srange range))
1519           ;; Go past all valid elements.
1520           (while (and (cdr srange)
1521                       (<= (or (and (atom (cadr srange))
1522                                    (cadr srange))
1523                               (caadr srange))
1524                           (cdr active)))
1525             (setq srange (cdr srange)))
1526           (when (cdr srange)
1527             ;; Nuke all remaining invalid elements.
1528             (setcdr srange nil))
1529
1530           ;; Adjust the final element.
1531           (when (and (not (atom (car srange)))
1532                      (> (cdar srange) (cdr active)))
1533             (setcdr (car srange) (cdr active))))
1534         ;; Compute the number of unread articles.
1535         (while range
1536           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1537                                       (cdar range)))
1538                               (or (and (atom (car range)) (car range))
1539                                   (caar range)))))
1540           (setq range (cdr range)))
1541         (setq num (max 0 (- (cdr active) num)))))
1542       ;; Set the number of unread articles.
1543       (when info
1544         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1545       num)))
1546
1547 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1548 ;; and compute how many unread articles there are in each group.
1549 (defun gnus-get-unread-articles (&optional level)
1550   (let* ((newsrc (cdr gnus-newsrc-alist))
1551          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1552          (foreign-level
1553           (min
1554            (cond ((and gnus-activate-foreign-newsgroups
1555                        (not (numberp gnus-activate-foreign-newsgroups)))
1556                   (1+ gnus-level-subscribed))
1557                  ((numberp gnus-activate-foreign-newsgroups)
1558                   gnus-activate-foreign-newsgroups)
1559                  (t 0))
1560            level))
1561          scanned-methods info group active method retrievegroups)
1562     (gnus-message 5 "Checking new news...")
1563
1564     (while newsrc
1565       (setq active (gnus-active (setq group (gnus-info-group
1566                                              (setq info (pop newsrc))))))
1567
1568       ;; Check newsgroups.  If the user doesn't want to check them, or
1569       ;; they can't be checked (for instance, if the news server can't
1570       ;; be reached) we just set the number of unread articles in this
1571       ;; newsgroup to t.  This means that Gnus thinks that there are
1572       ;; unread articles, but it has no idea how many.
1573
1574       ;; To be more explicit:
1575       ;; >0 for an active group with messages
1576       ;; 0 for an active group with no unread messages
1577       ;; nil for non-foreign groups that the user has requested not be checked
1578       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1579       ;;   be checked, for one reason or other.
1580       (if (and (setq method (gnus-info-method info))
1581                (not (inline
1582                       (gnus-server-equal
1583                        gnus-select-method
1584                        (setq method (gnus-server-get-method nil method)))))
1585                (not (gnus-secondary-method-p method)))
1586           ;; These groups are foreign.  Check the level.
1587           (when (and (<= (gnus-info-level info) foreign-level)
1588                      (setq active (gnus-activate-group group 'scan)))
1589             ;; Let the Gnus agent save the active file.
1590             (when (and gnus-agent gnus-plugged active)
1591               (gnus-agent-save-group-info
1592                method (gnus-group-real-name group) active))
1593             (unless (inline (gnus-virtual-group-p group))
1594               (inline (gnus-close-group group)))
1595             (when (fboundp (intern (concat (symbol-name (car method))
1596                                            "-request-update-info")))
1597               (inline (gnus-request-update-info info method))))
1598         ;; These groups are native or secondary.
1599         (cond
1600          ;; We don't want these groups.
1601          ((> (gnus-info-level info) level)
1602           (setq active 'ignore))
1603          ;; Activate groups.
1604          ((not gnus-read-active-file)
1605           (if (gnus-check-backend-function 'retrieve-groups group)
1606               ;; if server support gnus-retrieve-groups we push
1607               ;; the group onto retrievegroups for later checking
1608               (if (assoc method retrievegroups)
1609                   (setcdr (assoc method retrievegroups)
1610                           (cons group (cdr (assoc method retrievegroups))))
1611                 (push (list method group) retrievegroups))
1612             ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1613             ;; on the group, so we must perform a scan for every group
1614             ;; if the users has any directory mail sources.
1615             ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1616             ;; for it scan all spool files even when the groups are
1617             ;; not required.
1618             (if (and
1619                  (or nnmail-scan-directory-mail-source-once
1620                      (null (assq 'directory
1621                                  (or mail-sources
1622                                      (if (listp nnmail-spool-file)
1623                                          nnmail-spool-file
1624                                        (list nnmail-spool-file))))))
1625                  (member method scanned-methods))
1626                 (setq active (gnus-activate-group group))
1627               (setq active (gnus-activate-group group 'scan))
1628               (push method scanned-methods))
1629             (when active
1630               (gnus-close-group group))))))
1631
1632       ;; Get the number of unread articles in the group.
1633       (cond
1634        ((eq active 'ignore)
1635         ;; Don't do anything.
1636         )
1637        (active
1638         (inline (gnus-get-unread-articles-in-group info active t)))
1639        (t
1640         ;; The group couldn't be reached, so we nix out the number of
1641         ;; unread articles and stuff.
1642         (gnus-set-active group nil)
1643         (let ((tmp (gnus-gethash group gnus-newsrc-hashtb)))
1644           (if tmp (setcar tmp t))))))
1645
1646     ;; iterate through groups on methods which support gnus-retrieve-groups
1647     ;; and fetch a partial active file and use it to find new news.
1648     (while retrievegroups
1649       (let* ((mg (pop retrievegroups))
1650              (method (or (car mg) gnus-select-method))
1651              (groups (cdr mg)))
1652         (when (gnus-check-server method)
1653           ;; Request that the backend scan its incoming messages.
1654           (when (gnus-check-backend-function 'request-scan (car method))
1655             (gnus-request-scan nil method))
1656           (gnus-read-active-file-2 (mapcar (lambda (group)
1657                                              (gnus-group-real-name group))
1658                                            groups) method)
1659           (dolist (group groups)
1660             (cond
1661              ((setq active (gnus-active (gnus-info-group
1662                                          (setq info (gnus-get-info group)))))
1663               (inline (gnus-get-unread-articles-in-group info active t)))
1664              (t
1665               ;; The group couldn't be reached, so we nix out the number of
1666               ;; unread articles and stuff.
1667               (gnus-set-active group nil)
1668               (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))))))
1669
1670     (gnus-message 5 "Checking new news...done")))
1671
1672 ;; Create a hash table out of the newsrc alist.  The `car's of the
1673 ;; alist elements are used as keys.
1674 (defun gnus-make-hashtable-from-newsrc-alist ()
1675   (let ((alist gnus-newsrc-alist)
1676         (ohashtb gnus-newsrc-hashtb)
1677         prev)
1678     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1679     (setq alist
1680           (setq prev (setq gnus-newsrc-alist
1681                            (if (equal (caar gnus-newsrc-alist)
1682                                       "dummy.group")
1683                                gnus-newsrc-alist
1684                              (cons (list "dummy.group" 0 nil) alist)))))
1685     (while alist
1686       (gnus-sethash
1687        (caar alist)
1688        ;; Preserve number of unread articles in groups.
1689        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1690              prev)
1691        gnus-newsrc-hashtb)
1692       (setq prev alist
1693             alist (cdr alist)))))
1694
1695 (defun gnus-make-hashtable-from-killed ()
1696   "Create a hash table from the killed and zombie lists."
1697   (let ((lists '(gnus-killed-list gnus-zombie-list))
1698         list)
1699     (setq gnus-killed-hashtb
1700           (gnus-make-hashtable
1701            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1702     (while lists
1703       (setq list (symbol-value (pop lists)))
1704       (while list
1705         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1706
1707 (defun gnus-parse-active ()
1708   "Parse active info in the nntp server buffer."
1709   (save-excursion
1710     (set-buffer nntp-server-buffer)
1711     (goto-char (point-min))
1712     ;; Parse the result we got from `gnus-request-group'.
1713     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1714       (goto-char (match-beginning 1))
1715       (cons (read (current-buffer))
1716             (read (current-buffer))))))
1717
1718 (defun gnus-make-articles-unread (group articles)
1719   "Mark ARTICLES in GROUP as unread."
1720   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1721                           (gnus-gethash (gnus-group-real-name group)
1722                                         gnus-newsrc-hashtb))))
1723          (ranges (gnus-info-read info))
1724          news article)
1725     (while articles
1726       (when (gnus-member-of-range
1727              (setq article (pop articles)) ranges)
1728         (push article news)))
1729     (when news
1730       (gnus-info-set-read
1731        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1732       (gnus-group-update-group group t))))
1733
1734 ;; Enter all dead groups into the hashtb.
1735 (defun gnus-update-active-hashtb-from-killed ()
1736   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1737         (lists (list gnus-killed-list gnus-zombie-list))
1738         killed)
1739     (while lists
1740       (setq killed (car lists))
1741       (while killed
1742         (gnus-sethash (car killed) nil hashtb)
1743         (setq killed (cdr killed)))
1744       (setq lists (cdr lists)))))
1745
1746 (defun gnus-get-killed-groups ()
1747   "Go through the active hashtb and mark all unknown groups as killed."
1748   ;; First make sure active file has been read.
1749   (unless (gnus-read-active-file-p)
1750     (let ((gnus-read-active-file t))
1751       (gnus-read-active-file)))
1752   (unless gnus-killed-hashtb
1753     (gnus-make-hashtable-from-killed))
1754   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1755   (mapatoms
1756    (lambda (sym)
1757      (let ((groups 0)
1758            (group (symbol-name sym)))
1759        (if (or (null group)
1760                (gnus-gethash group gnus-killed-hashtb)
1761                (gnus-gethash group gnus-newsrc-hashtb))
1762            ()
1763          (let ((do-sub (gnus-matches-options-n group)))
1764            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1765                ()
1766              (setq groups (1+ groups))
1767              (push group gnus-killed-list)
1768              (gnus-sethash group group gnus-killed-hashtb))))))
1769    gnus-active-hashtb)
1770   (gnus-dribble-touch))
1771
1772 ;; Get the active file(s) from the backend(s).
1773 (defun gnus-read-active-file (&optional force not-native)
1774   (gnus-group-set-mode-line)
1775   (let ((methods
1776          (mapcar
1777           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1778           (append
1779            (if (and (not not-native)
1780                     (gnus-check-server gnus-select-method))
1781                ;; The native server is available.
1782                (cons gnus-select-method gnus-secondary-select-methods)
1783              ;; The native server is down, so we just do the
1784              ;; secondary ones.
1785              gnus-secondary-select-methods)
1786            ;; Also read from the archive server.
1787            (when (gnus-archive-server-wanted-p)
1788              (list "archive")))))
1789         method)
1790     (setq gnus-have-read-active-file nil)
1791     (save-excursion
1792       (set-buffer nntp-server-buffer)
1793       (while (setq method (pop methods))
1794         ;; Only do each method once, in case the methods appear more
1795         ;; than once in this list.
1796         (unless (member method methods)
1797           (condition-case ()
1798               (gnus-read-active-file-1 method force)
1799             ;; We catch C-g so that we can continue past servers
1800             ;; that do not respond.
1801             (quit
1802              (message "Quit reading the active file")
1803              nil)))))))
1804
1805 (defun gnus-read-active-file-1 (method force)
1806   (let (where mesg)
1807     (setq where (nth 1 method)
1808           mesg (format "Reading active file%s via %s..."
1809                        (if (and where (not (zerop (length where))))
1810                            (concat " from " where) "")
1811                        (car method)))
1812     (gnus-message 5 mesg)
1813     (when (gnus-check-server method)
1814       ;; Request that the backend scan its incoming messages.
1815       (when (gnus-check-backend-function 'request-scan (car method))
1816         (gnus-request-scan nil method))
1817       (cond
1818        ((and (eq gnus-read-active-file 'some)
1819              (gnus-check-backend-function 'retrieve-groups (car method))
1820              (not force))
1821         (let ((newsrc (cdr gnus-newsrc-alist))
1822               (gmethod (gnus-server-get-method nil method))
1823               groups info)
1824           (while (setq info (pop newsrc))
1825             (when (inline
1826                     (gnus-server-equal
1827                      (inline
1828                        (gnus-find-method-for-group
1829                         (gnus-info-group info) info))
1830                      gmethod))
1831               (push (gnus-group-real-name (gnus-info-group info))
1832                     groups)))
1833           (gnus-read-active-file-2 groups method)))
1834        ((null method)
1835         t)
1836        (t
1837         (if (not (gnus-request-list method))
1838             (unless (equal method gnus-message-archive-method)
1839               (gnus-error 1 "Cannot read active file from %s server"
1840                           (car method)))
1841           (gnus-message 5 mesg)
1842           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1843           ;; We mark this active file as read.
1844           (push method gnus-have-read-active-file)
1845           (gnus-message 5 "%sdone" mesg)))))))
1846
1847 (defun gnus-read-active-file-2 (groups method)
1848   "Read an active file for GROUPS in METHOD using gnus-retrieve-groups."
1849   (when groups
1850     (save-excursion
1851       (set-buffer nntp-server-buffer)
1852       (gnus-check-server method)
1853       (let ((list-type (gnus-retrieve-groups groups method)))
1854         (cond ((not list-type)
1855                (gnus-error
1856                 1.2 "Cannot read partial active file from %s server."
1857                 (car method)))
1858               ((eq list-type 'active)
1859                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
1860               (t
1861                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
1862
1863 ;; Read an active file and place the results in `gnus-active-hashtb'.
1864 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
1865                                              real-active)
1866   (unless method
1867     (setq method gnus-select-method))
1868   (let ((cur (current-buffer))
1869         (hashtb (or hashtb
1870                     (if (and gnus-active-hashtb
1871                              (not (equal method gnus-select-method)))
1872                         gnus-active-hashtb
1873                       (setq gnus-active-hashtb
1874                             (if (equal method gnus-select-method)
1875                                 (gnus-make-hashtable
1876                                  (count-lines (point-min) (point-max)))
1877                               (gnus-make-hashtable 4096)))))))
1878     ;; Delete unnecessary lines.
1879     (goto-char (point-min))
1880     (cond
1881      ((string= gnus-ignored-newsgroups "")
1882       (delete-matching-lines "^to\\."))
1883      (t
1884       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
1885
1886     (goto-char (point-min))
1887     (unless (re-search-forward "[\\\"]" nil t)
1888       ;; Make the group names readable as a lisp expression even if they
1889       ;; contain special characters.
1890       (goto-char (point-max))
1891       (while (re-search-backward "[][';?()#]" nil t)
1892         (insert ?\\)))
1893
1894     ;; Let the Gnus agent save the active file.
1895     (when (and gnus-agent real-active gnus-plugged)
1896       (gnus-agent-save-active method))
1897
1898     ;; If these are groups from a foreign select method, we insert the
1899     ;; group prefix in front of the group names.
1900     (when (not (gnus-server-equal
1901                 (gnus-server-get-method nil method)
1902                 (gnus-server-get-method nil gnus-select-method)))
1903       (let ((prefix (gnus-group-prefixed-name "" method)))
1904         (goto-char (point-min))
1905         (while (and (not (eobp))
1906                     (progn
1907                       (when (= (following-char) ?\")
1908                         (forward-char 1))
1909                       (insert prefix)
1910                       (zerop (forward-line 1)))))))
1911     ;; Store the active file in a hash table.
1912     (goto-char (point-min))
1913     (let (group max min)
1914       (while (not (eobp))
1915         (condition-case err
1916             (progn
1917               (narrow-to-region (point) (gnus-point-at-eol))
1918               ;; group gets set to a symbol interned in the hash table
1919               ;; (what a hack!!) - jwz
1920               (setq group (let ((obarray hashtb)) (read cur)))
1921               ;; ### The extended group name scheme makes
1922               ;; the previous optimization strategy sort of pointless...
1923               (when (stringp group)
1924                 (setq group (intern group hashtb)))
1925               (if (and (numberp (setq max (read cur)))
1926                        (numberp (setq min (read cur)))
1927                        (progn
1928                          (skip-chars-forward " \t")
1929                          (not
1930                           (or (eq (char-after) ?=)
1931                               (eq (char-after) ?x)
1932                               (eq (char-after) ?j)))))
1933                   (progn
1934                     (set group (cons min max))
1935                     ;; if group is moderated, stick in moderation table
1936                     (when (eq (char-after) ?m)
1937                       (unless gnus-moderated-hashtb
1938                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
1939                       (gnus-sethash (symbol-name group) t
1940                                     gnus-moderated-hashtb)))
1941                 (set group nil)))
1942           (error
1943            (and group
1944                 (symbolp group)
1945                 (set group nil))
1946            (unless ignore-errors
1947              (gnus-message 3 "Warning - invalid active: %s"
1948                            (buffer-substring
1949                             (gnus-point-at-bol) (gnus-point-at-eol))))))
1950         (widen)
1951         (forward-line 1)))))
1952
1953 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
1954   ;; Parse a "groups" active file.
1955   (let ((cur (current-buffer))
1956         (hashtb (or hashtb
1957                     (if (and method gnus-active-hashtb)
1958                         gnus-active-hashtb
1959                       (setq gnus-active-hashtb
1960                             (gnus-make-hashtable
1961                              (count-lines (point-min) (point-max)))))))
1962         (prefix (and method
1963                      (not (gnus-server-equal
1964                            (gnus-server-get-method nil method)
1965                            (gnus-server-get-method nil gnus-select-method)))
1966                      (gnus-group-prefixed-name "" method))))
1967
1968     ;; Let the Gnus agent save the active file.
1969     (if (and gnus-agent
1970              real-active
1971              gnus-plugged
1972              (gnus-agent-method-p method))
1973         (progn
1974           (gnus-agent-save-groups method)
1975           (gnus-active-to-gnus-format method hashtb nil real-active))
1976
1977       (goto-char (point-min))
1978       ;; We split this into to separate loops, one with the prefix
1979       ;; and one without to speed the reading up somewhat.
1980       (if prefix
1981           (let (min max opoint group)
1982             (while (not (eobp))
1983               (condition-case ()
1984                   (progn
1985                     (read cur) (read cur)
1986                     (setq min (read cur)
1987                           max (read cur)
1988                           opoint (point))
1989                     (skip-chars-forward " \t")
1990                     (insert prefix)
1991                     (goto-char opoint)
1992                     (set (let ((obarray hashtb)) (read cur))
1993                          (cons min max)))
1994                 (error (and group (symbolp group) (set group nil))))
1995               (forward-line 1)))
1996         (let (min max group)
1997           (while (not (eobp))
1998             (condition-case ()
1999                 (when (eq (char-after) ?2)
2000                   (read cur) (read cur)
2001                   (setq min (read cur)
2002                         max (read cur))
2003                   (set (setq group (let ((obarray hashtb)) (read cur)))
2004                        (cons min max)))
2005               (error (and group (symbolp group) (set group nil))))
2006             (forward-line 1)))))))
2007
2008 (defun gnus-read-newsrc-file (&optional force)
2009   "Read startup file.
2010 If FORCE is non-nil, the .newsrc file is read."
2011   ;; Reset variables that might be defined in the .newsrc.eld file.
2012   (gnus-clear-quick-file-variables)
2013   (let* ((newsrc-file gnus-current-startup-file)
2014          (quick-file (concat newsrc-file ".el")))
2015     (save-excursion
2016       ;; We always load the .newsrc.eld file.  If always contains
2017       ;; much information that can not be gotten from the .newsrc
2018       ;; file (ticked articles, killed groups, foreign methods, etc.)
2019       (gnus-read-newsrc-el-file quick-file)
2020
2021       (when (and gnus-read-newsrc-file
2022                  (file-exists-p gnus-current-startup-file)
2023                  (or force
2024                      (and (file-newer-than-file-p newsrc-file quick-file)
2025                           (file-newer-than-file-p newsrc-file
2026                                                   (concat quick-file "d")))
2027                      (not gnus-newsrc-alist)))
2028         ;; We read the .newsrc file.  Note that if there if a
2029         ;; .newsrc.eld file exists, it has already been read, and
2030         ;; the `gnus-newsrc-hashtb' has been created.  While reading
2031         ;; the .newsrc file, Gnus will only use the information it
2032         ;; can find there for changing the data already read -
2033         ;; i. e., reading the .newsrc file will not trash the data
2034         ;; already read (except for read articles).
2035         (save-excursion
2036           (gnus-message 5 "Reading %s..." newsrc-file)
2037           (set-buffer (nnheader-find-file-noselect newsrc-file))
2038           (buffer-disable-undo)
2039           (gnus-newsrc-to-gnus-format)
2040           (kill-buffer (current-buffer))
2041           (gnus-message 5 "Reading %s...done" newsrc-file))))))
2042
2043 (defun gnus-read-newsrc-el-file (file)
2044   (let ((ding-file (concat file "d")))
2045     ;; We always, always read the .eld file.
2046     (gnus-message 5 "Reading %s..." ding-file)
2047     (let (gnus-newsrc-assoc)
2048       (when (file-exists-p ding-file)
2049         (with-temp-buffer
2050           (condition-case nil
2051               (progn
2052                 (insert-file-contents-as-coding-system
2053                  gnus-startup-file-coding-system ding-file)
2054                 (eval-region (point-min) (point-max)))
2055             (error
2056              (ding)
2057              (or (not (or (zerop (buffer-size))
2058                           (eq 'binary gnus-startup-file-coding-system)
2059                           (gnus-re-read-newsrc-el-file ding-file)))
2060                  (gnus-yes-or-no-p
2061                   (format "Error in %s; continue? " ding-file))
2062                  (error "Error in %s" ding-file)))))
2063         (when gnus-newsrc-assoc
2064           (setq gnus-newsrc-alist gnus-newsrc-assoc))))
2065     (gnus-make-hashtable-from-newsrc-alist)
2066     (when (file-newer-than-file-p file ding-file)
2067       ;; Old format quick file
2068       (gnus-message 5 "Reading %s..." file)
2069       ;; The .el file is newer than the .eld file, so we read that one
2070       ;; as well.
2071       (gnus-read-old-newsrc-el-file file)))
2072   (when (and gnus-product-directory
2073              (file-directory-p gnus-product-directory))
2074     (let ((list gnus-product-variable-file-list))
2075       (while list
2076         (apply 'gnus-product-read-variable-file-1 (car list))
2077         (setq list (cdr list))))))
2078
2079 (defun gnus-re-read-newsrc-el-file (file)
2080   "Attempt to re-read .newsrc.eld file.  Returns `nil' if successful.
2081 The backup file \".newsrc.eld_\" will be created before re-reading."
2082   (message "Error in %s; retrying..." file)
2083   (if (and
2084        (condition-case nil
2085            (let ((backup (concat file "_")))
2086              (copy-file file backup 'ok-if-already-exists 'keep-time)
2087              (message " (The backup file %s has been created)" backup)
2088              t)
2089          (error nil))
2090        (progn
2091          (insert-file-contents-as-binary file nil nil nil 'replace)
2092          (when (re-search-forward
2093                 "^[\t ]*([\t\n\r ]*setq[\t\n\r ]+gnus-format-specs" nil t)
2094            (delete-region (goto-char (match-beginning 0)) (forward-list 1))
2095            (decode-coding-region (point-min) (point-max)
2096                                  gnus-startup-file-coding-system)
2097            (condition-case nil
2098                (progn
2099                  (eval-region (point-min) (point-max))
2100                  t)
2101              (error nil)))))
2102       (prog1
2103           nil
2104         (message "Error in %s; retrying...done" file))
2105     (message "Error in %s; retrying...failed" file)
2106     t))
2107
2108 (defun gnus-product-read-variable-file-1 (file checking-methods coding
2109                                                &rest variables)
2110   (let (error gnus-product-file-version method file-ver)
2111     (when (or
2112            (condition-case err
2113                (let ((coding-system-for-read coding)
2114                      (input-coding-system coding))
2115                  (load (expand-file-name file gnus-product-directory) t nil t)
2116                  nil)
2117              (error
2118               (message "%s" err)
2119               (setq error t)))
2120            (and (assq 'emacs-version checking-methods)
2121                 (not (string= emacs-version
2122                               (cdr (assq 'emacs-version
2123                                          gnus-product-file-version)))))
2124            (and (setq method (assq 'product-version checking-methods))
2125                 (or (not (setq file-ver
2126                                (cdr (assq 'product-version
2127                                           gnus-product-file-version))))
2128                     (< (product-version-compare file-ver (cadr method)) 0))))
2129       (unless error
2130         (message "\"%s\" seems to have mismatched contents, updating..."
2131                  file))
2132       (while variables
2133         (set (car variables) nil)
2134         (gnus-product-variable-touch (car variables))
2135         (setq variables (cdr variables))))))
2136
2137 ;; Parse the old-style quick startup file
2138 (defun gnus-read-old-newsrc-el-file (file)
2139   (let (newsrc killed marked group m info)
2140     (prog1
2141         (let ((gnus-killed-assoc nil)
2142               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2143           (prog1
2144               (ignore-errors
2145                 (load file t t t))
2146             (setq newsrc gnus-newsrc-assoc
2147                   killed gnus-killed-assoc
2148                   marked gnus-marked-assoc)))
2149       (setq gnus-newsrc-alist nil)
2150       (while (setq group (pop newsrc))
2151         (if (setq info (gnus-get-info (car group)))
2152             (progn
2153               (gnus-info-set-read info (cddr group))
2154               (gnus-info-set-level
2155                info (if (nth 1 group) gnus-level-default-subscribed
2156                       gnus-level-default-unsubscribed))
2157               (push info gnus-newsrc-alist))
2158           (push (setq info
2159                       (list (car group)
2160                             (if (nth 1 group) gnus-level-default-subscribed
2161                               gnus-level-default-unsubscribed)
2162                             (cddr group)))
2163                 gnus-newsrc-alist))
2164         ;; Copy marks into info.
2165         (when (setq m (assoc (car group) marked))
2166           (unless (nthcdr 3 info)
2167             (nconc info (list nil)))
2168           (gnus-info-set-marks
2169            info (list (cons 'tick (gnus-compress-sequence
2170                                    (sort (cdr m) '<) t))))))
2171       (setq newsrc killed)
2172       (while newsrc
2173         (setcar newsrc (caar newsrc))
2174         (setq newsrc (cdr newsrc)))
2175       (setq gnus-killed-list killed))
2176     ;; The .el file version of this variable does not begin with
2177     ;; "options", while the .eld version does, so we just add it if it
2178     ;; isn't there.
2179     (when
2180         gnus-newsrc-options
2181       (when (not (string-match "^ *options" gnus-newsrc-options))
2182         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2183       (when (not (string-match "\n$" gnus-newsrc-options))
2184         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2185       ;; Finally, if we read some options lines, we parse them.
2186       (unless (string= gnus-newsrc-options "")
2187         (gnus-newsrc-parse-options gnus-newsrc-options)))
2188
2189     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2190     (gnus-make-hashtable-from-newsrc-alist)))
2191
2192 (defun gnus-make-newsrc-file (file)
2193   "Make server dependent file name by catenating FILE and server host name."
2194   (let* ((file (expand-file-name file nil))
2195          (real-file (concat file "-" (nth 1 gnus-select-method))))
2196     (if (or (file-exists-p real-file)
2197             (file-exists-p (concat real-file ".el"))
2198             (file-exists-p (concat real-file ".eld")))
2199         real-file
2200       file)))
2201
2202 (defun gnus-newsrc-to-gnus-format ()
2203   (setq gnus-newsrc-options "")
2204   (setq gnus-newsrc-options-n nil)
2205
2206   (unless gnus-active-hashtb
2207     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2208   (let ((buf (current-buffer))
2209         (already-read (> (length gnus-newsrc-alist) 1))
2210         group subscribed options-symbol newsrc Options-symbol
2211         symbol reads num1)
2212     (goto-char (point-min))
2213     ;; We intern the symbol `options' in the active hashtb so that we
2214     ;; can `eq' against it later.
2215     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2216     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2217
2218     (while (not (eobp))
2219       ;; We first read the first word on the line by narrowing and
2220       ;; then reading into `gnus-active-hashtb'.  Most groups will
2221       ;; already exist in that hashtb, so this will save some string
2222       ;; space.
2223       (narrow-to-region
2224        (point)
2225        (progn (skip-chars-forward "^ \t!:\n") (point)))
2226       (goto-char (point-min))
2227       (setq symbol
2228             (and (/= (point-min) (point-max))
2229                  (let ((obarray gnus-active-hashtb)) (read buf))))
2230       (widen)
2231       ;; Now, the symbol we have read is either `options' or a group
2232       ;; name.  If it is an options line, we just add it to a string.
2233       (cond
2234        ((or (eq symbol options-symbol)
2235             (eq symbol Options-symbol))
2236         (setq gnus-newsrc-options
2237               ;; This concating is quite inefficient, but since our
2238               ;; thorough studies show that approx 99.37% of all
2239               ;; .newsrc files only contain a single options line, we
2240               ;; don't give a damn, frankly, my dear.
2241               (concat gnus-newsrc-options
2242                       (buffer-substring
2243                        (gnus-point-at-bol)
2244                        ;; Options may continue on the next line.
2245                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2246                                 (progn (beginning-of-line) (point)))
2247                            (point)))))
2248         (forward-line -1))
2249        (symbol
2250         ;; Group names can be just numbers.
2251         (when (numberp symbol)
2252           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2253         (unless (boundp symbol)
2254           (set symbol nil))
2255         ;; It was a group name.
2256         (setq subscribed (eq (char-after) ?:)
2257               group (symbol-name symbol)
2258               reads nil)
2259         (if (eolp)
2260             ;; If the line ends here, this is clearly a buggy line, so
2261             ;; we put point a the beginning of line and let the cond
2262             ;; below do the error handling.
2263             (beginning-of-line)
2264           ;; We skip to the beginning of the ranges.
2265           (skip-chars-forward "!: \t"))
2266         ;; We are now at the beginning of the list of read articles.
2267         ;; We read them range by range.
2268         (while
2269             (cond
2270              ((looking-at "[0-9]+")
2271               ;; We narrow and read a number instead of buffer-substring/
2272               ;; string-to-int because it's faster.  narrow/widen is
2273               ;; faster than save-restriction/narrow, and save-restriction
2274               ;; produces a garbage object.
2275               (setq num1 (progn
2276                            (narrow-to-region (match-beginning 0) (match-end 0))
2277                            (read buf)))
2278               (widen)
2279               ;; If the next character is a dash, then this is a range.
2280               (if (eq (char-after) ?-)
2281                   (progn
2282                     ;; We read the upper bound of the range.
2283                     (forward-char 1)
2284                     (if (not (looking-at "[0-9]+"))
2285                         ;; This is a buggy line, by we pretend that
2286                         ;; it's kinda OK.  Perhaps the user should be
2287                         ;; dinged?
2288                         (push num1 reads)
2289                       (push
2290                        (cons num1
2291                              (progn
2292                                (narrow-to-region (match-beginning 0)
2293                                                  (match-end 0))
2294                                (read buf)))
2295                        reads)
2296                       (widen)))
2297                 ;; It was just a simple number, so we add it to the
2298                 ;; list of ranges.
2299                 (push num1 reads))
2300               ;; If the next char in ?\n, then we have reached the end
2301               ;; of the line and return nil.
2302               (not (eq (char-after) ?\n)))
2303              ((eq (char-after) ?\n)
2304               ;; End of line, so we end.
2305               nil)
2306              (t
2307               ;; Not numbers and not eol, so this might be a buggy
2308               ;; line...
2309               (unless (eobp)
2310                 ;; If it was eob instead of ?\n, we allow it.
2311                 ;; The line was buggy.
2312                 (setq group nil)
2313                 (gnus-error 3.1 "Mangled line: %s"
2314                             (buffer-substring (gnus-point-at-bol)
2315                                               (gnus-point-at-eol))))
2316               nil))
2317           ;; Skip past ", ".  Spaces are invalid in these ranges, but
2318           ;; we allow them, because it's a common mistake to put a
2319           ;; space after the comma.
2320           (skip-chars-forward ", "))
2321
2322         ;; We have already read .newsrc.eld, so we gently update the
2323         ;; data in the hash table with the information we have just
2324         ;; read.
2325         (when group
2326           (let ((info (gnus-get-info group))
2327                 level)
2328             (if info
2329                 ;; There is an entry for this file in the alist.
2330                 (progn
2331                   (gnus-info-set-read info (nreverse reads))
2332                   ;; We update the level very gently.  In fact, we
2333                   ;; only change it if there's been a status change
2334                   ;; from subscribed to unsubscribed, or vice versa.
2335                   (setq level (gnus-info-level info))
2336                   (cond ((and (<= level gnus-level-subscribed)
2337                               (not subscribed))
2338                          (setq level (if reads
2339                                          gnus-level-default-unsubscribed
2340                                        (1+ gnus-level-default-unsubscribed))))
2341                         ((and (> level gnus-level-subscribed) subscribed)
2342                          (setq level gnus-level-default-subscribed)))
2343                   (gnus-info-set-level info level))
2344               ;; This is a new group.
2345               (setq info (list group
2346                                (if subscribed
2347                                    gnus-level-default-subscribed
2348                                  (if reads
2349                                      (1+ gnus-level-subscribed)
2350                                    gnus-level-default-unsubscribed))
2351                                (nreverse reads))))
2352             (push info newsrc)))))
2353       (forward-line 1))
2354
2355     (setq newsrc (nreverse newsrc))
2356
2357     (if (not already-read)
2358         ()
2359       ;; We now have two newsrc lists - `newsrc', which is what we
2360       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2361       ;; what we've read from .newsrc.eld.  We have to merge these
2362       ;; lists.  We do this by "attaching" any (foreign) groups in the
2363       ;; gnus-newsrc-alist to the (native) group that precedes them.
2364       (let ((rc (cdr gnus-newsrc-alist))
2365             (prev gnus-newsrc-alist)
2366             entry mentry)
2367         (while rc
2368           (or (null (nth 4 (car rc)))   ; It's a native group.
2369               (assoc (caar rc) newsrc)  ; It's already in the alist.
2370               (if (setq entry (assoc (caar prev) newsrc))
2371                   (setcdr (setq mentry (memq entry newsrc))
2372                           (cons (car rc) (cdr mentry)))
2373                 (push (car rc) newsrc)))
2374           (setq prev rc
2375                 rc (cdr rc)))))
2376
2377     (setq gnus-newsrc-alist newsrc)
2378     ;; We make the newsrc hashtb.
2379     (gnus-make-hashtable-from-newsrc-alist)
2380
2381     ;; Finally, if we read some options lines, we parse them.
2382     (unless (string= gnus-newsrc-options "")
2383       (gnus-newsrc-parse-options gnus-newsrc-options))))
2384
2385 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2386 ;; The return value will be a list on the form
2387 ;; ((regexp1 . ignore)
2388 ;;  (regexp2 . subscribe)...)
2389 ;; When handling new newsgroups, groups that match a `ignore' regexp
2390 ;; will be ignored, and groups that match a `subscribe' regexp will be
2391 ;; subscribed.  A line like
2392 ;; options -n !all rec.all
2393 ;; will lead to a list that looks like
2394 ;; (("^rec\\..+" . subscribe)
2395 ;;  ("^.+" . ignore))
2396 ;; So all "rec.*" groups will be subscribed, while all the other
2397 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2398 ;; different from "options -n rec.all !all".
2399 (defun gnus-newsrc-parse-options (options)
2400   (let (out eol)
2401     (save-excursion
2402       (gnus-set-work-buffer)
2403       (insert (regexp-quote options))
2404       ;; First we treat all continuation lines.
2405       (goto-char (point-min))
2406       (while (re-search-forward "\n[ \t]+" nil t)
2407         (replace-match " " t t))
2408       ;; Then we transform all "all"s into ".+"s.
2409       (goto-char (point-min))
2410       (while (re-search-forward "\\ball\\b" nil t)
2411         (replace-match ".+" t t))
2412       (goto-char (point-min))
2413       ;; We remove all other options than the "-n" ones.
2414       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2415         (replace-match " ")
2416         (forward-char -1))
2417       (goto-char (point-min))
2418
2419       ;; We are only interested in "options -n" lines - we
2420       ;; ignore the other option lines.
2421       (while (re-search-forward "[ \t]-n" nil t)
2422         (setq eol
2423               (or (save-excursion
2424                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
2425                          (- (point) 2)))
2426                   (gnus-point-at-eol)))
2427         ;; Search for all "words"...
2428         (while (re-search-forward "[^ \t,\n]+" eol t)
2429           (if (eq (char-after (match-beginning 0)) ?!)
2430               ;; If the word begins with a bang (!), this is a "not"
2431               ;; spec.  We put this spec (minus the bang) and the
2432               ;; symbol `ignore' into the list.
2433               (push (cons (concat
2434                            "^" (buffer-substring
2435                                 (1+ (match-beginning 0))
2436                                 (match-end 0))
2437                            "\\($\\|\\.\\)")
2438                           'ignore)
2439                     out)
2440             ;; There was no bang, so this is a "yes" spec.
2441             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2442                         'subscribe)
2443                   out))))
2444
2445       (setq gnus-newsrc-options-n out))))
2446
2447 (defun gnus-save-newsrc-file (&optional force)
2448   "Save .newsrc file."
2449   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2450   ;; from the variable gnus-newsrc-alist.
2451   (when (and (or gnus-newsrc-alist gnus-killed-list)
2452              gnus-current-startup-file)
2453     (save-excursion
2454       (if (and (or gnus-use-dribble-file gnus-slave)
2455                (not force)
2456                (or (not gnus-dribble-buffer)
2457                    (not (buffer-name gnus-dribble-buffer))
2458                    (zerop (save-excursion
2459                             (set-buffer gnus-dribble-buffer)
2460                             (buffer-size)))))
2461           (gnus-message 4 "(No changes need to be saved)")
2462         (gnus-run-hooks 'gnus-save-newsrc-hook)
2463         (if gnus-slave
2464             (gnus-slave-save-newsrc)
2465           ;; Save .newsrc.
2466           (when gnus-save-newsrc-file
2467             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2468             (gnus-gnus-to-newsrc-format)
2469             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2470           ;; Save .newsrc.eld.
2471           (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
2472           (make-local-variable 'version-control)
2473           (setq version-control 'never)
2474           (setq buffer-file-name
2475                 (concat gnus-current-startup-file ".eld"))
2476           (setq default-directory (file-name-directory buffer-file-name))
2477           (buffer-disable-undo)
2478           (erase-buffer)
2479           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2480           (gnus-gnus-to-quick-newsrc-format)
2481           (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2482           (save-buffer-as-coding-system gnus-startup-file-coding-system)
2483           (kill-buffer (current-buffer))
2484           (gnus-message
2485            5 "Saving %s.eld...done" gnus-current-startup-file))
2486         (gnus-dribble-delete-file)
2487         (gnus-group-set-mode-line))))
2488   (when gnus-product-directory
2489     (gnus-product-save-variable-file)))
2490
2491 ;; Call the function above at C-x C-c.
2492 (defadvice save-buffers-kill-emacs (before save-gnus-newsrc-file-maybe
2493                                            activate preactivate)
2494   "Save .newsrc and .newsrc.eld when Emacs is killed."
2495   (when (gnus-alive-p)
2496     (gnus-run-hooks 'gnus-exit-gnus-hook)
2497     (gnus-offer-save-summaries)
2498     (gnus-save-newsrc-file)))
2499
2500 (defun gnus-gnus-to-quick-newsrc-format ()
2501   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
2502   (let ((print-quoted t)
2503         (print-escape-newlines t))
2504
2505     (insert ";; -*- emacs-lisp -*-\n")
2506     (insert ";; Gnus startup file.\n")
2507     (insert "\
2508 ;; Never delete this file -- if you want to force Gnus to read the
2509 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2510     (insert "(setq gnus-newsrc-file-version "
2511             (prin1-to-string gnus-version) ")\n")
2512     (let* ((gnus-killed-list
2513             (if (and gnus-save-killed-list
2514                      (stringp gnus-save-killed-list))
2515                 (gnus-strip-killed-list)
2516               gnus-killed-list))
2517            (variables
2518             (if gnus-save-killed-list gnus-variable-list
2519               ;; Remove the `gnus-killed-list' from the list of variables
2520               ;; to be saved, if required.
2521               (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
2522            ;; Peel off the "dummy" group.
2523            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2524            variable)
2525       ;; Insert the variables into the file.
2526       (while variables
2527         (when (and (boundp (setq variable (pop variables)))
2528                    (symbol-value variable))
2529           (insert "(setq " (symbol-name variable) " '")
2530           (gnus-prin1 (symbol-value variable))
2531           (insert ")\n"))))))
2532
2533 (defun gnus-product-variable-touch (&rest variables)
2534   (while variables
2535     (put (pop variables) 'gnus-product-variable 'dirty)))
2536
2537 (defun gnus-product-variables-dirty-p (variables)
2538   (catch 'done
2539     (while variables
2540       (when (eq (get (car variables) 'gnus-product-variable) 'dirty)
2541         (throw 'done t))
2542       (setq variables (cdr variables)))))
2543
2544 (defun gnus-product-save-variable-file (&optional force)
2545   "Save all product variables to files, when need to be saved."
2546   (let ((list gnus-product-variable-file-list))
2547     (gnus-make-directory gnus-product-directory)
2548     (while list
2549       (apply 'gnus-product-save-variable-file-1 force (car list))
2550       (setq list (cdr list)))))
2551
2552 (defun gnus-product-save-variable-file-1 (force file checking-methods coding
2553                                                 &rest variables)
2554   "Save a product variable file, when need to be saved."
2555   (when (or force
2556             (gnus-product-variables-dirty-p variables))
2557     (let ((product (product-find 'gnus-vers)))
2558       (set-buffer (gnus-get-buffer-create " *gnus-product*"))
2559       (make-local-variable 'version-control)
2560       (setq version-control 'never)
2561       (setq file (expand-file-name file gnus-product-directory)
2562             buffer-file-name file
2563             default-directory (file-name-directory file))
2564       (buffer-disable-undo)
2565       (erase-buffer)
2566       (gnus-message 5 "Saving %s..." file)
2567       (apply 'gnus-product-quick-file-format product checking-methods coding
2568              variables)
2569       (save-buffer-as-coding-system coding)
2570       (kill-buffer (current-buffer))
2571       (while variables
2572         (put (car variables) 'gnus-product-variable nil)
2573         (setq variables (cdr variables)))
2574       (gnus-message
2575        5 "Saving %s...done" file))))
2576
2577 (defun gnus-product-quick-file-format (product checking-methods
2578                                                coding &rest variables)
2579   "Insert gnus product depend variables in lisp format."
2580   (let ((print-quoted t)
2581         (print-escape-newlines t)
2582         variable param)
2583     (insert (format ";; -*- Mode: emacs-lisp; coding: %s -*-\n" coding))
2584     (insert (format ";; %s startup file.\n" (product-name product)))
2585     (when (setq param (cdr (assq 'product-version checking-methods)))
2586       (insert "(or (>= (product-version-compare "
2587               "(product-version (product-find 'gnus-vers))\n"
2588               "\t\t\t\t '" (apply 'prin1-to-string param) ")\n"
2589               "\t0)\n"
2590               "    (error \"This file was created by later version of "
2591               "gnus.\"))\n"))
2592     (insert "(setq gnus-product-file-version \n"
2593             "      '((product-version . "
2594             (prin1-to-string (product-version product)) ")\n"
2595             "\t(emacs-version . " (prin1-to-string emacs-version) ")))\n")
2596     (while variables
2597       (when (and (boundp (setq variable (pop variables)))
2598                  (symbol-value variable))
2599           (insert "(setq " (symbol-name variable) " '")
2600           (gnus-prin1 (symbol-value variable))
2601           (insert ")\n")))))
2602
2603 (defun gnus-strip-killed-list ()
2604   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2605   (let ((list gnus-killed-list)
2606         olist)
2607     (while list
2608       (when (string-match gnus-save-killed-list (car list))
2609         (push (car list) olist))
2610       (pop list))
2611     (nreverse olist)))
2612
2613 (defun gnus-gnus-to-newsrc-format ()
2614   ;; Generate and save the .newsrc file.
2615   (save-excursion
2616     (set-buffer (create-file-buffer gnus-current-startup-file))
2617     (let ((newsrc (cdr gnus-newsrc-alist))
2618           (standard-output (current-buffer))
2619           info ranges range method)
2620       (setq buffer-file-name gnus-current-startup-file)
2621       (setq default-directory (file-name-directory buffer-file-name))
2622       (buffer-disable-undo)
2623       (erase-buffer)
2624       ;; Write options.
2625       (when gnus-newsrc-options
2626         (insert gnus-newsrc-options))
2627       ;; Write subscribed and unsubscribed.
2628       (while (setq info (pop newsrc))
2629         ;; Don't write foreign groups to .newsrc.
2630         (when (or (null (setq method (gnus-info-method info)))
2631                   (equal method "native")
2632                   (inline (gnus-server-equal method gnus-select-method)))
2633           (insert (gnus-info-group info)
2634                   (if (> (gnus-info-level info) gnus-level-subscribed)
2635                       "!" ":"))
2636           (when (setq ranges (gnus-info-read info))
2637             (insert " ")
2638             (if (not (listp (cdr ranges)))
2639                 (if (= (car ranges) (cdr ranges))
2640                     (princ (car ranges))
2641                   (princ (car ranges))
2642                   (insert "-")
2643                   (princ (cdr ranges)))
2644               (while (setq range (pop ranges))
2645                 (if (or (atom range) (= (car range) (cdr range)))
2646                     (princ (or (and (atom range) range) (car range)))
2647                   (princ (car range))
2648                   (insert "-")
2649                   (princ (cdr range)))
2650                 (when ranges
2651                   (insert ",")))))
2652           (insert "\n")))
2653       (make-local-variable 'version-control)
2654       (setq version-control 'never)
2655       ;; It has been reported that sometime the modtime on the .newsrc
2656       ;; file seems to be off.  We really do want to overwrite it, so
2657       ;; we clear the modtime here before saving.  It's a bit odd,
2658       ;; though...
2659       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2660       ;; delete the silly thing entirely first.  but this fails to provide
2661       ;; such niceties as .newsrc~ creation.
2662       (if gnus-modtime-botch
2663           (delete-file gnus-startup-file)
2664         (clear-visited-file-modtime))
2665       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2666       (save-buffer)
2667       (kill-buffer (current-buffer)))))
2668
2669 \f
2670 ;;;
2671 ;;; Slave functions.
2672 ;;;
2673
2674 (defvar gnus-slave-mode nil)
2675
2676 (defun gnus-slave-mode ()
2677   "Minor mode for slave Gnusae."
2678   (gnus-add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
2679   (gnus-run-hooks 'gnus-slave-mode-hook))
2680
2681 (defun gnus-slave-save-newsrc ()
2682   (save-excursion
2683     (set-buffer gnus-dribble-buffer)
2684     (let ((slave-name
2685            (make-temp-name (concat gnus-current-startup-file "-slave-")))
2686           (modes (ignore-errors
2687                    (file-modes (concat gnus-current-startup-file ".eld")))))
2688       (gnus-write-buffer-as-coding-system
2689        gnus-startup-file-coding-system slave-name)
2690       (when modes
2691         (set-file-modes slave-name modes)))))
2692
2693 (defun gnus-master-read-slave-newsrc ()
2694   (let ((slave-files
2695          (directory-files
2696           (file-name-directory gnus-current-startup-file)
2697           t (concat
2698              "^" (regexp-quote
2699                   (concat
2700                    (file-name-nondirectory gnus-current-startup-file)
2701                    "-slave-")))
2702           t))
2703         file)
2704     (if (not slave-files)
2705         ()                              ; There are no slave files to read.
2706       (gnus-message 7 "Reading slave newsrcs...")
2707       (save-excursion
2708         (set-buffer (gnus-get-buffer-create " *gnus slave*"))
2709         (setq slave-files
2710               (sort (mapcar (lambda (file)
2711                               (list (nth 5 (file-attributes file)) file))
2712                             slave-files)
2713                     (lambda (f1 f2)
2714                       (or (< (caar f1) (caar f2))
2715                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2716         (while slave-files
2717           (erase-buffer)
2718           (setq file (nth 1 (car slave-files)))
2719           (nnheader-insert-file-contents file)
2720           (when (condition-case ()
2721                     (progn
2722                       (eval-buffer (current-buffer))
2723                       t)
2724                   (error
2725                    (gnus-error 3.2 "Possible error in %s" file)
2726                    nil))
2727             (unless gnus-slave          ; Slaves shouldn't delete these files.
2728               (ignore-errors
2729                 (delete-file file))))
2730           (setq slave-files (cdr slave-files))))
2731       (gnus-dribble-touch)
2732       (gnus-message 7 "Reading slave newsrcs...done"))))
2733
2734 \f
2735 ;;;
2736 ;;; Group description.
2737 ;;;
2738
2739 (defun gnus-read-all-descriptions-files ()
2740   (let ((methods (cons gnus-select-method
2741                        (nconc
2742                         (when (gnus-archive-server-wanted-p)
2743                           (list "archive"))
2744                         gnus-secondary-select-methods))))
2745     (while methods
2746       (gnus-read-descriptions-file (car methods))
2747       (setq methods (cdr methods)))
2748     t))
2749
2750 (defun gnus-read-descriptions-file (&optional method)
2751   (let ((method (or method gnus-select-method))
2752         group)
2753     (when (stringp method)
2754       (setq method (gnus-server-to-method method)))
2755     ;; We create the hashtable whether we manage to read the desc file
2756     ;; to avoid trying to re-read after a failed read.
2757     (unless gnus-description-hashtb
2758       (setq gnus-description-hashtb
2759             (gnus-make-hashtable (length gnus-active-hashtb))))
2760     ;; Mark this method's desc file as read.
2761     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
2762                   gnus-description-hashtb)
2763
2764     (gnus-message 5 "Reading descriptions file via %s..." (car method))
2765     (cond
2766      ((null (gnus-get-function method 'request-list-newsgroups t))
2767       t)
2768      ((not (gnus-check-server method))
2769       (gnus-message 1 "Couldn't open server")
2770       nil)
2771      ((not (gnus-request-list-newsgroups method))
2772       (gnus-message 1 "Couldn't read newsgroups descriptions")
2773       nil)
2774      (t
2775       (save-excursion
2776         (save-restriction
2777           (set-buffer nntp-server-buffer)
2778           (goto-char (point-min))
2779           (when (or (search-forward "\n.\n" nil t)
2780                     (goto-char (point-max)))
2781             (beginning-of-line)
2782             (narrow-to-region (point-min) (point)))
2783           ;; If these are groups from a foreign select method, we insert the
2784           ;; group prefix in front of the group names.
2785           (and method (not (inline
2786                              (gnus-server-equal
2787                               (gnus-server-get-method nil method)
2788                               (gnus-server-get-method
2789                                nil gnus-select-method))))
2790                (let ((prefix (gnus-group-prefixed-name "" method)))
2791                  (goto-char (point-min))
2792                  (while (and (not (eobp))
2793                              (progn (insert prefix)
2794                                     (zerop (forward-line 1)))))))
2795           (goto-char (point-min))
2796           (while (not (eobp))
2797             ;; If we get an error, we set group to 0, which is not a
2798             ;; symbol...
2799             (setq group
2800                   (condition-case ()
2801                       (let ((obarray gnus-description-hashtb))
2802                         ;; Group is set to a symbol interned in this
2803                         ;; hash table.
2804                         (read nntp-server-buffer))
2805                     (error 0)))
2806             (skip-chars-forward " \t")
2807             ;; ...  which leads to this line being effectively ignored.
2808             (when (symbolp group)
2809               (let* ((str (buffer-substring
2810                            (point) (progn (end-of-line) (point))))
2811                      (name (symbol-name group))
2812                      (charset
2813                       (or (gnus-group-name-charset method name)
2814                           (gnus-parameter-charset name))))
2815                 (when (and str charset (featurep 'mule))
2816                   (setq str (decode-coding-string str charset)))
2817                 (set group str)))
2818             (forward-line 1))))
2819       (gnus-message 5 "Reading descriptions file...done")
2820       t))))
2821
2822 (defun gnus-group-get-description (group)
2823   "Get the description of a group by sending XGTITLE to the server."
2824   (when (gnus-request-group-description group)
2825     (save-excursion
2826       (set-buffer nntp-server-buffer)
2827       (goto-char (point-min))
2828       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
2829         (match-string 1)))))
2830
2831 ;;;###autoload
2832 (defun gnus-declare-backend (name &rest abilities)
2833   "Declare backend NAME with ABILITIES as a Gnus backend."
2834   (setq gnus-valid-select-methods
2835         (nconc gnus-valid-select-methods
2836                (list (apply 'list name abilities))))
2837   (gnus-redefine-select-method-widget))
2838
2839 (defun gnus-set-default-directory ()
2840   "Set the default directory in the current buffer to `gnus-default-directory'.
2841 If this variable is nil, don't do anything."
2842   (setq default-directory
2843         (if (and gnus-default-directory
2844                  (file-exists-p gnus-default-directory))
2845             (file-name-as-directory (expand-file-name gnus-default-directory))
2846           default-directory)))
2847
2848 (defun gnus-display-time-event-handler ()
2849   "Like `display-time-event-handler', but test `display-time-timer'."
2850   (when (gnus-boundp 'display-time-timer)
2851     (display-time-event-handler)))
2852
2853 (provide 'gnus-start)
2854
2855 ;;; gnus-start.el ends here