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