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