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