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