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