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