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