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