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