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