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