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