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