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