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