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