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