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