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