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