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