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