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