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