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