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