Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-start.el
1 ;;; gnus-start.el --- startup functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30 (eval-when-compile (require 'static))
31
32 (require 'gnus)
33 (require 'gnus-win)
34 (require 'gnus-int)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-util)
38 (require 'message)
39
40 (defcustom gnus-startup-file (nnheader-concat gnus-home-directory ".newsrc")
41   "Your `.newsrc' file.
42 `.newsrc-SERVER' will be used instead if that exists."
43   :group 'gnus-start
44   :type 'file)
45
46 (defcustom gnus-product-directory
47   (nnheader-concat gnus-directory (concat "." gnus-product-name))
48   "Product depend data files directory."
49   :group 'gnus-start
50   :type '(choice directory (const nil)))
51
52 (defcustom gnus-init-file (nnheader-concat gnus-home-directory ".gnus")
53   "Your Gnus Emacs-Lisp startup file name.
54 If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
55   :group 'gnus-start
56   :type 'file)
57
58 (defcustom gnus-site-init-file
59   (condition-case nil
60       (concat (file-name-directory
61                (directory-file-name installation-directory))
62               "site-lisp/gnus-init")
63     (error nil))
64   "The site-wide Gnus Emacs-Lisp startup file name, or nil if none.
65 If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
66   :group 'gnus-start
67   :type '(choice file (const nil)))
68
69 (defcustom gnus-default-subscribed-newsgroups nil
70   "List of newsgroups to subscribe, when a user runs Gnus the first time.
71 The value should be a list of strings.
72 If it is t, Gnus will not do anything special the first time it is
73 started; it'll just use the normal newsgroups subscription methods."
74   :group 'gnus-start
75   :type '(choice (repeat string) (const :tag "Nothing special" t)))
76
77 (defcustom gnus-use-dribble-file t
78   "*Non-nil means that Gnus will use a dribble file to store user updates.
79 If Emacs should crash without saving the .newsrc files, complete
80 information can be restored from the dribble file."
81   :group 'gnus-dribble-file
82   :type 'boolean)
83
84 (defcustom gnus-dribble-directory nil
85   "*The directory where dribble files will be saved.
86 If this variable is nil, the directory where the .newsrc files are
87 saved will be used."
88   :group 'gnus-dribble-file
89   :type '(choice directory (const nil)))
90
91 (defcustom gnus-check-new-newsgroups 'ask-server
92   "*Non-nil means that Gnus will run `gnus-find-new-newsgroups' at startup.
93 This normally finds new newsgroups by comparing the active groups the
94 servers have already reported with those Gnus already knows, either alive
95 or killed.
96
97 When any of the following are true, `gnus-find-new-newsgroups' will instead
98 ask the servers (primary, secondary, and archive servers) to list new
99 groups since the last time it checked:
100   1. This variable is `ask-server'.
101   2. This variable is a list of select methods (see below).
102   3. `gnus-read-active-file' is nil or `some'.
103   4. A prefix argument is given to `gnus-find-new-newsgroups' interactively.
104
105 Thus, if this variable is `ask-server' or a list of select methods or
106 `gnus-read-active-file' is nil or `some', then the killed list is no
107 longer necessary, so you could safely set `gnus-save-killed-list' to nil.
108
109 This variable can be a list of select methods which Gnus will query with
110 the `ask-server' method in addition to the primary, secondary, and archive
111 servers.
112
113 Eg.
114   (setq gnus-check-new-newsgroups
115         '((nntp \"some.server\") (nntp \"other.server\")))
116
117 If this variable is nil, then you have to tell Gnus explicitly to
118 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups]."
119   :group 'gnus-start
120   :type '(choice (const :tag "no" nil)
121                  (const :tag "by brute force" t)
122                  (const :tag "ask servers" ask-server)
123                  (repeat :menu-tag "ask additional servers"
124                          :tag "ask additional servers"
125                          :value ((nntp ""))
126                          (sexp :format "%v"))))
127
128 (defcustom gnus-check-bogus-newsgroups nil
129   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
130 If this variable is nil, then you have to tell Gnus explicitly to
131 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups]."
132   :group 'gnus-start-server
133   :type 'boolean)
134
135 (defcustom gnus-read-active-file 'some
136   "*Non-nil means that Gnus will read the entire active file at startup.
137 If this variable is nil, Gnus will only know about the groups in your
138 `.newsrc' file.
139
140 If this variable is `some', Gnus will try to only read the relevant
141 parts of the active file from the server.  Not all servers support
142 this, and it might be quite slow with other servers, but this should
143 generally be faster than both the t and nil value.
144
145 If you set this variable to nil or `some', you probably still want to
146 be told about new newsgroups that arrive.  To do that, set
147 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
148 properly with all servers."
149   :group 'gnus-start-server
150   :type '(choice (const nil)
151                  (const some)
152                  (const t)))
153
154 (defconst gnus-level-subscribed 5
155   "Groups with levels less than or equal to this variable are subscribed.")
156
157 (defconst gnus-level-unsubscribed 7
158   "Groups with levels less than or equal to this variable are unsubscribed.
159 Groups with levels less than `gnus-level-subscribed', which should be
160 less than this variable, are subscribed.")
161
162 (defconst gnus-level-zombie 8
163   "Groups with this level are zombie groups.")
164
165 (defconst gnus-level-killed 9
166   "Groups with this level are killed.")
167
168 (defcustom gnus-level-default-subscribed 3
169   "*New subscribed groups will be subscribed at this level."
170   :group 'gnus-group-levels
171   :type 'integer)
172
173 (defcustom gnus-level-default-unsubscribed 6
174   "*New unsubscribed groups will be unsubscribed at this level."
175   :group 'gnus-group-levels
176   :type 'integer)
177
178 (defcustom gnus-activate-level (1+ gnus-level-subscribed)
179   "*Groups higher than this level won't be activated on startup.
180 Setting this variable to something low might save lots of time when
181 you have many groups that you aren't interested in."
182   :group 'gnus-group-levels
183   :type 'integer)
184
185 (defcustom gnus-activate-foreign-newsgroups 4
186   "*If nil, Gnus will not check foreign newsgroups at startup.
187 If it is non-nil, it should be a number between one and nine.  Foreign
188 newsgroups that have a level lower or equal to this number will be
189 activated on startup.  For instance, if you want to active all
190 subscribed newsgroups, but not the rest, you'd set this variable to
191 `gnus-level-subscribed'.
192
193 If you subscribe to lots of newsgroups from different servers, startup
194 might take a while.  By setting this variable to nil, you'll save time,
195 but you won't be told how many unread articles there are in the
196 groups."
197   :group 'gnus-group-levels
198   :type '(choice integer
199                  (const :tag "none" nil)))
200
201 (defcustom gnus-read-newsrc-file t
202   "*Non-nil means that Gnus will read the `.newsrc' file.
203 Gnus always reads its own startup file, which is called
204 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
205 be readily understood by other newsreaders.  If you don't plan on
206 using other newsreaders, set this variable to nil to save some time on
207 entry."
208   :version "21.1"
209   :group 'gnus-newsrc
210   :type 'boolean)
211
212 (defcustom gnus-save-newsrc-file t
213   "*Non-nil means that Gnus will save the `.newsrc' file.
214 Gnus always saves its own startup file, which is called
215 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
216 be readily understood by other newsreaders.  If you don't plan on
217 using other newsreaders, set this variable to nil to save some time on
218 exit."
219   :group 'gnus-newsrc
220   :type 'boolean)
221
222 (defcustom gnus-save-killed-list t
223   "*If non-nil, save the list of killed groups to the startup file.
224 If you set this variable to nil, you'll save both time (when starting
225 and quitting) and space (both memory and disk), but it will also mean
226 that Gnus has no record of which groups are new and which are old, so
227 the automatic new newsgroups subscription methods become meaningless.
228
229 You should always set `gnus-check-new-newsgroups' to `ask-server' or
230 nil if you set this variable to nil.
231
232 This variable can also be a regexp.  In that case, all groups that do
233 not match this regexp will be removed before saving the list."
234   :group 'gnus-newsrc
235   :type 'boolean)
236
237 (defcustom gnus-ignored-newsgroups
238   (mapconcat 'identity
239              '("^to\\."                 ; not "real" groups
240                "^[0-9. \t]+ "           ; all digits in name
241                "^[\"][]\"[#'()]"        ; bogus characters
242                )
243              "\\|")
244   "*A regexp to match uninteresting newsgroups in the active file.
245 Any lines in the active file matching this regular expression are
246 removed from the newsgroup list before anything else is done to it,
247 thus making them effectively non-existent."
248   :group 'gnus-group-new
249   :type 'regexp)
250
251 (defcustom gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
252   "*Function called with a group name when new group is detected.
253 A few pre-made functions are supplied: `gnus-subscribe-randomly'
254 inserts new groups at the beginning of the list of groups;
255 `gnus-subscribe-alphabetically' inserts new groups in strict
256 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
257 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
258 for your decision; `gnus-subscribe-killed' kills all new groups;
259 `gnus-subscribe-zombies' will make all new groups into zombies;
260 `gnus-subscribe-topics' will enter groups into the topics that
261 claim them."
262   :group 'gnus-group-new
263   :type '(radio (function-item gnus-subscribe-randomly)
264                 (function-item gnus-subscribe-alphabetically)
265                 (function-item gnus-subscribe-hierarchically)
266                 (function-item gnus-subscribe-interactively)
267                 (function-item gnus-subscribe-killed)
268                 (function-item gnus-subscribe-zombies)
269                 (function-item gnus-subscribe-topics)
270                 function))
271
272 (defcustom gnus-subscribe-options-newsgroup-method
273   'gnus-subscribe-alphabetically
274   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
275 If, for instance, you want to subscribe to all newsgroups in the
276 \"no\" and \"alt\" hierarchies, you'd put the following in your
277 .newsrc file:
278
279 options -n no.all alt.all
280
281 Gnus will the subscribe all new newsgroups in these hierarchies with
282 the subscription method in this variable."
283   :group 'gnus-group-new
284   :type '(radio (function-item gnus-subscribe-randomly)
285                 (function-item gnus-subscribe-alphabetically)
286                 (function-item gnus-subscribe-hierarchically)
287                 (function-item gnus-subscribe-interactively)
288                 (function-item gnus-subscribe-killed)
289                 (function-item gnus-subscribe-zombies)
290                 function))
291
292 (defcustom gnus-subscribe-hierarchical-interactive nil
293   "*If non-nil, Gnus will offer to subscribe hierarchically.
294 When a new hierarchy appears, Gnus will ask the user:
295
296 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
297
298 If the user pressed `d', Gnus will descend the hierarchy, `y' will
299 subscribe to all newsgroups in the hierarchy and `s' will skip this
300 hierarchy in its entirety."
301   :group 'gnus-group-new
302   :type 'boolean)
303
304 (defcustom gnus-auto-subscribed-groups
305   "nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
306   "*All new groups that match this regexp will be subscribed automatically.
307 Note that this variable only deals with new groups.  It has no effect
308 whatsoever on old groups.
309
310 New groups that match this regexp will not be handled by
311 `gnus-subscribe-newsgroup-method'.  Instead, they will
312 be subscribed using `gnus-subscribe-options-newsgroup-method'."
313   :group 'gnus-group-new
314   :type 'regexp)
315
316 (defcustom gnus-options-subscribe nil
317   "*All new groups matching this regexp will be subscribed unconditionally.
318 Note that this variable deals only with new newsgroups.  This variable
319 does not affect old newsgroups.
320
321 New groups that match this regexp will not be handled by
322 `gnus-subscribe-newsgroup-method'.  Instead, they will
323 be subscribed using `gnus-subscribe-options-newsgroup-method'."
324   :group 'gnus-group-new
325   :type '(choice regexp
326                  (const :tag "none" nil)))
327
328 (defcustom gnus-options-not-subscribe nil
329   "*All new groups matching this regexp will be ignored.
330 Note that this variable deals only with new newsgroups.  This variable
331 does not affect old (already subscribed) newsgroups."
332   :group 'gnus-group-new
333   :type '(choice regexp
334                  (const :tag "none" nil)))
335
336 (defcustom gnus-modtime-botch nil
337   "*Non-nil means .newsrc should be deleted prior to save.
338 Its use is due to the bogus appearance that .newsrc was modified on
339 disc."
340   :group 'gnus-newsrc
341   :type 'boolean)
342
343 (defcustom gnus-check-bogus-groups-hook nil
344   "A hook run after removing bogus groups."
345   :group 'gnus-start-server
346   :type 'hook)
347
348 (defcustom gnus-startup-hook nil
349   "A hook called at startup.
350 This hook is called after Gnus is connected to the NNTP server."
351   :group 'gnus-start
352   :type 'hook)
353
354 (defcustom gnus-before-startup-hook nil
355   "A hook called at before startup.
356 This hook is called as the first thing when Gnus is started."
357   :group 'gnus-start
358   :type 'hook)
359
360 (defcustom gnus-started-hook nil
361   "A hook called as the last thing after startup."
362   :group 'gnus-start
363   :type 'hook)
364
365 (defcustom gnus-setup-news-hook nil
366   "A hook after reading the .newsrc file, but before generating the buffer."
367   :group 'gnus-start
368   :type 'hook)
369
370 (defcustom gnus-get-new-news-hook nil
371   "A hook run just before Gnus checks for new news."
372   :group 'gnus-group-new
373   :type 'hook)
374
375 (defcustom gnus-after-getting-new-news-hook
376   '(gnus-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        ((featurep 'xemacs)
703         (gnus-xmas-splash))
704        ((and window-system
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             (if (= oldlevel gnus-level-killed)
1269                 ;; Remove from active hashtb.
1270                 (unintern group gnus-active-hashtb)
1271               ;; Don't add it into killed-list if it was killed.
1272               (push group gnus-killed-list)))))
1273        (t
1274         ;; If the list is to be entered into the newsrc assoc, and
1275         ;; it was killed, we have to create an entry in the newsrc
1276         ;; hashtb format and fix the pointers in the newsrc assoc.
1277         (if (< oldlevel gnus-level-zombie)
1278             ;; It was alive, and it is going to stay alive, so we
1279             ;; just change the level and don't change any pointers or
1280             ;; hash table entries.
1281             (setcar (cdaddr entry) level)
1282           (if (listp entry)
1283               (setq info (cdr entry)
1284                     num (car entry))
1285             (setq active (gnus-active group))
1286             (setq num
1287                   (if active (- (1+ (cdr active)) (car active)) t))
1288             ;; Shorten the select method if possible, if we need to
1289             ;; store it at all (native groups).
1290             (let ((method (gnus-method-simplify
1291                            (or gnus-override-subscribe-method
1292                                (gnus-group-method group)))))
1293               (if method
1294                   (setq info (list group level nil nil method))
1295                 (setq info (list group level nil)))))
1296           (unless previous
1297             (setq previous
1298                   (let ((p gnus-newsrc-alist))
1299                     (while (cddr p)
1300                       (setq p (cdr p)))
1301                     p)))
1302           (setq entry (cons info (cddr previous)))
1303           (if (cdr previous)
1304               (progn
1305                 (setcdr (cdr previous) entry)
1306                 (gnus-sethash group (cons num (cdr previous))
1307                               gnus-newsrc-hashtb))
1308             (setcdr previous entry)
1309             (gnus-sethash group (cons num previous)
1310                           gnus-newsrc-hashtb))
1311           (when (cdr entry)
1312             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry))
1313           (gnus-dribble-enter
1314            (format
1315             "(gnus-group-set-info '%S)" info)))))
1316       (when gnus-group-change-level-function
1317         (funcall gnus-group-change-level-function
1318                  group level oldlevel previous)))))
1319
1320 (defun gnus-kill-newsgroup (newsgroup)
1321   "Obsolete function.  Kills a newsgroup."
1322   (gnus-group-change-level
1323    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
1324
1325 (defun gnus-check-bogus-newsgroups (&optional confirm)
1326   "Remove bogus newsgroups.
1327 If CONFIRM is non-nil, the user has to confirm the deletion of every
1328 newsgroup."
1329   (let ((newsrc (cdr gnus-newsrc-alist))
1330         bogus group entry info)
1331     (gnus-message 5 "Checking bogus newsgroups...")
1332     (unless (gnus-read-active-file-p)
1333       (gnus-read-active-file t))
1334     (when (gnus-read-active-file-p)
1335       ;; Find all bogus newsgroup that are subscribed.
1336       (while newsrc
1337         (setq info (pop newsrc)
1338               group (gnus-info-group info))
1339         (unless (or (gnus-active group) ; Active
1340                     (gnus-info-method info)) ; Foreign
1341           ;; Found a bogus newsgroup.
1342           (push group bogus)))
1343       (if confirm
1344           (map-y-or-n-p
1345            "Remove bogus group %s? "
1346            (lambda (group)
1347              ;; Remove all bogus subscribed groups by first killing them, and
1348              ;; then removing them from the list of killed groups.
1349              (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1350                (gnus-group-change-level entry gnus-level-killed)
1351                (setq gnus-killed-list (delete group gnus-killed-list))))
1352            bogus '("group" "groups" "remove"))
1353         (while (setq group (pop bogus))
1354           ;; Remove all bogus subscribed groups by first killing them, and
1355           ;; then removing them from the list of killed groups.
1356           (when (setq entry (gnus-gethash group gnus-newsrc-hashtb))
1357             (gnus-group-change-level entry gnus-level-killed)
1358             (setq gnus-killed-list (delete group gnus-killed-list)))))
1359       ;; Then we remove all bogus groups from the list of killed and
1360       ;; zombie groups.  They are removed without confirmation.
1361       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1362             killed)
1363         (while dead-lists
1364           (setq killed (symbol-value (car dead-lists)))
1365           (while killed
1366             (unless (gnus-active (setq group (pop killed)))
1367               ;; The group is bogus.
1368               ;; !!!Slow as hell.
1369               (set (car dead-lists)
1370                    (delete group (symbol-value (car dead-lists))))))
1371           (setq dead-lists (cdr dead-lists))))
1372       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1373       (gnus-message 5 "Checking bogus newsgroups...done"))))
1374
1375 (defun gnus-check-duplicate-killed-groups ()
1376   "Remove duplicates from the list of killed groups."
1377   (interactive)
1378   (let ((killed gnus-killed-list))
1379     (while killed
1380       (gnus-message 9 "%d" (length killed))
1381       (setcdr killed (delete (car killed) (cdr killed)))
1382       (setq killed (cdr killed)))))
1383
1384 ;; We want to inline a function from gnus-cache, so we cheat here:
1385 (eval-when-compile
1386   (defvar gnus-cache-active-hashtb)
1387   (defun gnus-cache-possibly-alter-active (group active)
1388     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1389     (when gnus-cache-active-hashtb
1390       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1391         (when cache-active
1392           (when (< (car cache-active) (car active))
1393             (setcar active (car cache-active)))
1394           (when (> (cdr cache-active) (cdr active))
1395             (setcdr active (cdr cache-active))))))))
1396
1397 (defun gnus-activate-group (group &optional scan dont-check method)
1398   ;; Check whether a group has been activated or not.
1399   ;; If SCAN, request a scan of that group as well.
1400   (let ((method (or method (inline (gnus-find-method-for-group group))))
1401         active)
1402     (and (inline (gnus-check-server method))
1403          ;; We escape all bugs and quit here to make it possible to
1404          ;; continue if a group is so out-there that it reports bugs
1405          ;; and stuff.
1406          (progn
1407            (and scan
1408                 (gnus-check-backend-function 'request-scan (car method))
1409                 (gnus-request-scan group method))
1410            t)
1411          (condition-case ()
1412              (inline (gnus-request-group group dont-check method))
1413            ;;(error nil)
1414            (quit
1415             (message "Quit activating %s" group)
1416             nil))
1417          (setq active (gnus-parse-active))
1418          ;; If there are no articles in the group, the GROUP
1419          ;; command may have responded with the `(0 . 0)'.  We
1420          ;; ignore this if we already have an active entry
1421          ;; for the group.
1422          (if (and (zerop (car active))
1423                   (zerop (cdr active))
1424                   (gnus-active group))
1425              (gnus-active group)
1426            (gnus-set-active group active)
1427            ;; Return the new active info.
1428            active))))
1429
1430 (defun gnus-get-unread-articles-in-group (info active &optional update)
1431   (when active
1432     ;; Allow the backend to update the info in the group.
1433     (when (and update
1434                (gnus-request-update-info
1435                 info (inline (gnus-find-method-for-group
1436                               (gnus-info-group info)))))
1437       (gnus-activate-group (gnus-info-group info) nil t))
1438
1439     (let* ((range (gnus-info-read info))
1440            (num 0))
1441       ;; If a cache is present, we may have to alter the active info.
1442       (when (and gnus-use-cache info)
1443         (inline (gnus-cache-possibly-alter-active
1444                  (gnus-info-group info) active)))
1445       ;; Modify the list of read articles according to what articles
1446       ;; are available; then tally the unread articles and add the
1447       ;; number to the group hash table entry.
1448       (cond
1449        ((zerop (cdr active))
1450         (setq num 0))
1451        ((not range)
1452         (setq num (- (1+ (cdr active)) (car active))))
1453        ((not (listp (cdr range)))
1454         ;; Fix a single (num . num) range according to the
1455         ;; active hash table.
1456         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1457         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1458         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1459         ;; Compute number of unread articles.
1460         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1461        (t
1462         ;; The read list is a list of ranges.  Fix them according to
1463         ;; the active hash table.
1464         ;; First peel off any elements that are below the lower
1465         ;; active limit.
1466         (while (and (cdr range)
1467                     (>= (car active)
1468                         (or (and (atom (cadr range)) (cadr range))
1469                             (caadr range))))
1470           (if (numberp (car range))
1471               (setcar range
1472                       (cons (car range)
1473                             (or (and (numberp (cadr range))
1474                                      (cadr range))
1475                                 (cdadr range))))
1476             (setcdr (car range)
1477                     (or (and (numberp (nth 1 range)) (nth 1 range))
1478                         (cdadr range))))
1479           (setcdr range (cddr range)))
1480         ;; Adjust the first element to be the same as the lower limit.
1481         (when (and (not (atom (car range)))
1482                    (< (cdar range) (car active)))
1483           (setcdr (car range) (1- (car active))))
1484         ;; Then we want to peel off any elements that are higher
1485         ;; than the upper active limit.
1486         (let ((srange range))
1487           ;; Go past all valid elements.
1488           (while (and (cdr srange)
1489                       (<= (or (and (atom (cadr srange))
1490                                    (cadr srange))
1491                               (caadr srange))
1492                           (cdr active)))
1493             (setq srange (cdr srange)))
1494           (when (cdr srange)
1495             ;; Nuke all remaining invalid elements.
1496             (setcdr srange nil))
1497
1498           ;; Adjust the final element.
1499           (when (and (not (atom (car srange)))
1500                      (> (cdar srange) (cdr active)))
1501             (setcdr (car srange) (cdr active))))
1502         ;; Compute the number of unread articles.
1503         (while range
1504           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1505                                       (cdar range)))
1506                               (or (and (atom (car range)) (car range))
1507                                   (caar range)))))
1508           (setq range (cdr range)))
1509         (setq num (max 0 (- (cdr active) num)))))
1510       ;; Set the number of unread articles.
1511       (when info
1512         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
1513       num)))
1514
1515 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1516 ;; and compute how many unread articles there are in each group.
1517 (defun gnus-get-unread-articles (&optional level)
1518   (let* ((newsrc (cdr gnus-newsrc-alist))
1519          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
1520          (foreign-level
1521           (min
1522            (cond ((and gnus-activate-foreign-newsgroups
1523                        (not (numberp gnus-activate-foreign-newsgroups)))
1524                   (1+ gnus-level-subscribed))
1525                  ((numberp gnus-activate-foreign-newsgroups)
1526                   gnus-activate-foreign-newsgroups)
1527                  (t 0))
1528            level))
1529          scanned-methods info group active method retrievegroups)
1530     (gnus-message 5 "Checking new news...")
1531
1532     (while newsrc
1533       (setq active (gnus-active (setq group (gnus-info-group
1534                                              (setq info (pop newsrc))))))
1535
1536       ;; Check newsgroups.  If the user doesn't want to check them, or
1537       ;; they can't be checked (for instance, if the news server can't
1538       ;; be reached) we just set the number of unread articles in this
1539       ;; newsgroup to t.  This means that Gnus thinks that there are
1540       ;; unread articles, but it has no idea how many.
1541
1542       ;; To be more explicit:
1543       ;; >0 for an active group with messages
1544       ;; 0 for an active group with no unread messages
1545       ;; nil for non-foreign groups that the user has requested not be checked
1546       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1547       ;;   be checked, for one reason or other.
1548       (if (and (setq method (gnus-info-method info))
1549                (not (inline
1550                       (gnus-server-equal
1551                        gnus-select-method
1552                        (setq method (gnus-server-get-method nil method)))))
1553                (not (gnus-secondary-method-p method)))
1554           ;; These groups are foreign.  Check the level.
1555           (when (and (<= (gnus-info-level info) foreign-level)
1556                      (setq active (gnus-activate-group group 'scan)))
1557             ;; Let the Gnus agent save the active file.
1558             (when (and gnus-agent gnus-plugged active)
1559               (gnus-agent-save-group-info
1560                method (gnus-group-real-name group) active))
1561             (unless (inline (gnus-virtual-group-p group))
1562               (inline (gnus-close-group group)))
1563             (when (fboundp (intern (concat (symbol-name (car method))
1564                                            "-request-update-info")))
1565               (inline (gnus-request-update-info info method))))
1566         ;; These groups are native or secondary.
1567         (cond
1568          ;; We don't want these groups.
1569          ((> (gnus-info-level info) level)
1570           (setq active 'ignore))
1571          ;; Activate groups.
1572          ((not gnus-read-active-file)
1573           (if (gnus-check-backend-function 'retrieve-groups group)
1574               ;; if server support gnus-retrieve-groups we push
1575               ;; the group onto retrievegroups for later checking
1576               (if (assoc method retrievegroups)
1577                   (setcdr (assoc method retrievegroups)
1578                           (cons group (cdr (assoc method retrievegroups))))
1579                 (push (list method group) retrievegroups))
1580             ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1581             ;; on the group, so we must perform a scan for every group
1582             ;; if the users has any directory mail sources.
1583             ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1584             ;; for it scan all spool files even when the groups are
1585             ;; not required.
1586             (if (and
1587                  (or nnmail-scan-directory-mail-source-once
1588                      (null (assq 'directory
1589                                  (or mail-sources
1590                                      (if (listp nnmail-spool-file)
1591                                          nnmail-spool-file
1592                                        (list nnmail-spool-file))))))
1593                  (member method scanned-methods))
1594                 (setq active (gnus-activate-group group))
1595               (setq active (gnus-activate-group group 'scan))
1596               (push method scanned-methods))
1597             (when active
1598               (gnus-close-group group))))))
1599
1600       ;; Get the number of unread articles in the group.
1601       (cond
1602        ((eq active 'ignore)
1603         ;; Don't do anything.
1604         )
1605        (active
1606         (inline (gnus-get-unread-articles-in-group info active t)))
1607        (t
1608         ;; The group couldn't be reached, so we nix out the number of
1609         ;; unread articles and stuff.
1610         (gnus-set-active group nil)
1611         (let ((tmp (gnus-gethash group gnus-newsrc-hashtb)))
1612           (if tmp (setcar tmp t))))))
1613
1614     ;; iterate through groups on methods which support gnus-retrieve-groups
1615     ;; and fetch a partial active file and use it to find new news.
1616     (while retrievegroups
1617       (let* ((mg (pop retrievegroups))
1618              (method (or (car mg) gnus-select-method))
1619              (groups (cdr mg)))
1620         (when (gnus-check-server method)
1621           ;; Request that the backend scan its incoming messages.
1622           (when (gnus-check-backend-function 'request-scan (car method))
1623             (gnus-request-scan nil method))
1624           (gnus-read-active-file-2 (mapcar (lambda (group)
1625                                              (gnus-group-real-name group))
1626                                            groups) method)
1627           (dolist (group groups)
1628             (cond
1629              ((setq active (gnus-active (gnus-info-group
1630                                          (setq info (gnus-get-info group)))))
1631               (inline (gnus-get-unread-articles-in-group info active t)))
1632              (t
1633               ;; The group couldn't be reached, so we nix out the number of
1634               ;; unread articles and stuff.
1635               (gnus-set-active group nil)
1636               (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))))))
1637
1638     (gnus-message 5 "Checking new news...done")))
1639
1640 ;; Create a hash table out of the newsrc alist.  The `car's of the
1641 ;; alist elements are used as keys.
1642 (defun gnus-make-hashtable-from-newsrc-alist ()
1643   (let ((alist gnus-newsrc-alist)
1644         (ohashtb gnus-newsrc-hashtb)
1645         prev)
1646     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1647     (setq alist
1648           (setq prev (setq gnus-newsrc-alist
1649                            (if (equal (caar gnus-newsrc-alist)
1650                                       "dummy.group")
1651                                gnus-newsrc-alist
1652                              (cons (list "dummy.group" 0 nil) alist)))))
1653     (while alist
1654       (gnus-sethash
1655        (caar alist)
1656        ;; Preserve number of unread articles in groups.
1657        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
1658              prev)
1659        gnus-newsrc-hashtb)
1660       (setq prev alist
1661             alist (cdr alist)))))
1662
1663 (defun gnus-make-hashtable-from-killed ()
1664   "Create a hash table from the killed and zombie lists."
1665   (let ((lists '(gnus-killed-list gnus-zombie-list))
1666         list)
1667     (setq gnus-killed-hashtb
1668           (gnus-make-hashtable
1669            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1670     (while lists
1671       (setq list (symbol-value (pop lists)))
1672       (while list
1673         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1674
1675 (defun gnus-parse-active ()
1676   "Parse active info in the nntp server buffer."
1677   (save-excursion
1678     (set-buffer nntp-server-buffer)
1679     (goto-char (point-min))
1680     ;; Parse the result we got from `gnus-request-group'.
1681     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1682       (goto-char (match-beginning 1))
1683       (cons (read (current-buffer))
1684             (read (current-buffer))))))
1685
1686 (defun gnus-make-articles-unread (group articles)
1687   "Mark ARTICLES in GROUP as unread."
1688   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
1689                           (gnus-gethash (gnus-group-real-name group)
1690                                         gnus-newsrc-hashtb))))
1691          (ranges (gnus-info-read info))
1692          news article)
1693     (while articles
1694       (when (gnus-member-of-range
1695              (setq article (pop articles)) ranges)
1696         (push article news)))
1697     (when news
1698       (gnus-info-set-read
1699        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1700       (gnus-group-update-group group t))))
1701
1702 ;; Enter all dead groups into the hashtb.
1703 (defun gnus-update-active-hashtb-from-killed ()
1704   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1705         (lists (list gnus-killed-list gnus-zombie-list))
1706         killed)
1707     (while lists
1708       (setq killed (car lists))
1709       (while killed
1710         (gnus-sethash (car killed) nil hashtb)
1711         (setq killed (cdr killed)))
1712       (setq lists (cdr lists)))))
1713
1714 (defun gnus-get-killed-groups ()
1715   "Go through the active hashtb and mark all unknown groups as killed."
1716   ;; First make sure active file has been read.
1717   (unless (gnus-read-active-file-p)
1718     (let ((gnus-read-active-file t))
1719       (gnus-read-active-file)))
1720   (unless gnus-killed-hashtb
1721     (gnus-make-hashtable-from-killed))
1722   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1723   (mapatoms
1724    (lambda (sym)
1725      (let ((groups 0)
1726            (group (symbol-name sym)))
1727        (if (or (null group)
1728                (gnus-gethash group gnus-killed-hashtb)
1729                (gnus-gethash group gnus-newsrc-hashtb))
1730            ()
1731          (let ((do-sub (gnus-matches-options-n group)))
1732            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1733                ()
1734              (setq groups (1+ groups))
1735              (push group gnus-killed-list)
1736              (gnus-sethash group group gnus-killed-hashtb))))))
1737    gnus-active-hashtb)
1738   (gnus-dribble-touch))
1739
1740 ;; Get the active file(s) from the backend(s).
1741 (defun gnus-read-active-file (&optional force not-native)
1742   (gnus-group-set-mode-line)
1743   (let ((methods
1744          (mapcar
1745           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
1746           (append
1747            (if (and (not not-native)
1748                     (gnus-check-server gnus-select-method))
1749                ;; The native server is available.
1750                (cons gnus-select-method gnus-secondary-select-methods)
1751              ;; The native server is down, so we just do the
1752              ;; secondary ones.
1753              gnus-secondary-select-methods)
1754            ;; Also read from the archive server.
1755            (when (gnus-archive-server-wanted-p)
1756              (list "archive")))))
1757         method)
1758     (setq gnus-have-read-active-file nil)
1759     (save-excursion
1760       (set-buffer nntp-server-buffer)
1761       (while (setq method (pop methods))
1762         ;; Only do each method once, in case the methods appear more
1763         ;; than once in this list.
1764         (unless (member method methods)
1765           (condition-case ()
1766               (gnus-read-active-file-1 method force)
1767             ;; We catch C-g so that we can continue past servers
1768             ;; that do not respond.
1769             (quit
1770              (message "Quit reading the active file")
1771              nil)))))))
1772
1773 (defun gnus-read-active-file-1 (method force)
1774   (let (where mesg)
1775     (setq where (nth 1 method)
1776           mesg (format "Reading active file%s via %s..."
1777                        (if (and where (not (zerop (length where))))
1778                            (concat " from " where) "")
1779                        (car method)))
1780     (gnus-message 5 mesg)
1781     (when (gnus-check-server method)
1782       ;; Request that the backend scan its incoming messages.
1783       (when (gnus-check-backend-function 'request-scan (car method))
1784         (gnus-request-scan nil method))
1785       (cond
1786        ((and (eq gnus-read-active-file 'some)
1787              (gnus-check-backend-function 'retrieve-groups (car method))
1788              (not force))
1789         (let ((newsrc (cdr gnus-newsrc-alist))
1790               (gmethod (gnus-server-get-method nil method))
1791               groups info)
1792           (while (setq info (pop newsrc))
1793             (when (inline
1794                     (gnus-server-equal
1795                      (inline
1796                        (gnus-find-method-for-group
1797                         (gnus-info-group info) info))
1798                      gmethod))
1799               (push (gnus-group-real-name (gnus-info-group info))
1800                     groups)))
1801           (gnus-read-active-file-2 groups method)))
1802        ((null method)
1803         t)
1804        (t
1805         (if (not (gnus-request-list method))
1806             (unless (equal method gnus-message-archive-method)
1807               (gnus-error 1 "Cannot read active file from %s server"
1808                           (car method)))
1809           (gnus-message 5 mesg)
1810           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
1811           ;; We mark this active file as read.
1812           (push method gnus-have-read-active-file)
1813           (gnus-message 5 "%sdone" mesg)))))))
1814
1815 (defun gnus-read-active-file-2 (groups method)
1816   "Read an active file for GROUPS in METHOD using gnus-retrieve-groups."
1817   (when groups
1818     (save-excursion
1819       (set-buffer nntp-server-buffer)
1820       (gnus-check-server method)
1821       (let ((list-type (gnus-retrieve-groups groups method)))
1822         (cond ((not list-type)
1823                (gnus-error
1824                 1.2 "Cannot read partial active file from %s server."
1825                 (car method)))
1826               ((eq list-type 'active)
1827                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
1828               (t
1829                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
1830
1831 ;; Read an active file and place the results in `gnus-active-hashtb'.
1832 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
1833                                              real-active)
1834   (unless method
1835     (setq method gnus-select-method))
1836   (let ((cur (current-buffer))
1837         (hashtb (or hashtb
1838                     (if (and gnus-active-hashtb
1839                              (not (equal method gnus-select-method)))
1840                         gnus-active-hashtb
1841                       (setq gnus-active-hashtb
1842                             (if (equal method gnus-select-method)
1843                                 (gnus-make-hashtable
1844                                  (count-lines (point-min) (point-max)))
1845                               (gnus-make-hashtable 4096)))))))
1846     ;; Delete unnecessary lines.
1847     (goto-char (point-min))
1848     (cond
1849      ((string= gnus-ignored-newsgroups "")
1850       (delete-matching-lines "^to\\."))
1851      (t
1852       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
1853
1854     (goto-char (point-min))
1855     (unless (re-search-forward "[\\\"]" nil t)
1856       ;; Make the group names readable as a lisp expression even if they
1857       ;; contain special characters.
1858       (goto-char (point-max))
1859       (while (re-search-backward "[][';?()#]" nil t)
1860         (insert ?\\)))
1861
1862     ;; Let the Gnus agent save the active file.
1863     (when (and gnus-agent real-active gnus-plugged)
1864       (gnus-agent-save-active method))
1865
1866     ;; If these are groups from a foreign select method, we insert the
1867     ;; group prefix in front of the group names.
1868     (when (not (gnus-server-equal
1869                 (gnus-server-get-method nil method)
1870                 (gnus-server-get-method nil gnus-select-method)))
1871       (let ((prefix (gnus-group-prefixed-name "" method)))
1872         (goto-char (point-min))
1873         (while (and (not (eobp))
1874                     (progn
1875                       (when (= (following-char) ?\")
1876                         (forward-char 1))
1877                       (insert prefix)
1878                       (zerop (forward-line 1)))))))
1879     ;; Store the active file in a hash table.
1880     (goto-char (point-min))
1881     (let (group max min)
1882       (while (not (eobp))
1883         (condition-case err
1884             (progn
1885               (narrow-to-region (point) (gnus-point-at-eol))
1886               ;; group gets set to a symbol interned in the hash table
1887               ;; (what a hack!!) - jwz
1888               (setq group (let ((obarray hashtb)) (read cur)))
1889               ;; ### The extended group name scheme makes
1890               ;; the previous optimization strategy sort of pointless...
1891               (when (stringp group)
1892                 (setq group (intern group hashtb)))
1893               (if (and (numberp (setq max (read cur)))
1894                        (numberp (setq min (read cur)))
1895                        (progn
1896                          (skip-chars-forward " \t")
1897                          (not
1898                           (or (eq (char-after) ?=)
1899                               (eq (char-after) ?x)
1900                               (eq (char-after) ?j)))))
1901                   (progn
1902                     (set group (cons min max))
1903                     ;; if group is moderated, stick in moderation table
1904                     (when (eq (char-after) ?m)
1905                       (unless gnus-moderated-hashtb
1906                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
1907                       (gnus-sethash (symbol-name group) t
1908                                     gnus-moderated-hashtb)))
1909                 (set group nil)))
1910           (error
1911            (and group
1912                 (symbolp group)
1913                 (set group nil))
1914            (unless ignore-errors
1915              (gnus-message 3 "Warning - invalid active: %s"
1916                            (buffer-substring
1917                             (gnus-point-at-bol) (gnus-point-at-eol))))))
1918         (widen)
1919         (forward-line 1)))))
1920
1921 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
1922   ;; Parse a "groups" active file.
1923   (let ((cur (current-buffer))
1924         (hashtb (or hashtb
1925                     (if (and method gnus-active-hashtb)
1926                         gnus-active-hashtb
1927                       (setq gnus-active-hashtb
1928                             (gnus-make-hashtable
1929                              (count-lines (point-min) (point-max)))))))
1930         (prefix (and method
1931                      (not (gnus-server-equal
1932                            (gnus-server-get-method nil method)
1933                            (gnus-server-get-method nil gnus-select-method)))
1934                      (gnus-group-prefixed-name "" method))))
1935
1936     ;; Let the Gnus agent save the active file.
1937     (if (and gnus-agent
1938              real-active
1939              gnus-plugged
1940              (gnus-agent-method-p method))
1941         (progn
1942           (gnus-agent-save-groups method)
1943           (gnus-active-to-gnus-format method hashtb nil real-active))
1944
1945       (goto-char (point-min))
1946       ;; We split this into to separate loops, one with the prefix
1947       ;; and one without to speed the reading up somewhat.
1948       (if prefix
1949           (let (min max opoint group)
1950             (while (not (eobp))
1951               (condition-case ()
1952                   (progn
1953                     (read cur) (read cur)
1954                     (setq min (read cur)
1955                           max (read cur)
1956                           opoint (point))
1957                     (skip-chars-forward " \t")
1958                     (insert prefix)
1959                     (goto-char opoint)
1960                     (set (let ((obarray hashtb)) (read cur))
1961                          (cons min max)))
1962                 (error (and group (symbolp group) (set group nil))))
1963               (forward-line 1)))
1964         (let (min max group)
1965           (while (not (eobp))
1966             (condition-case ()
1967                 (when (eq (char-after) ?2)
1968                   (read cur) (read cur)
1969                   (setq min (read cur)
1970                         max (read cur))
1971                   (set (setq group (let ((obarray hashtb)) (read cur)))
1972                        (cons min max)))
1973               (error (and group (symbolp group) (set group nil))))
1974             (forward-line 1)))))))
1975
1976 (defun gnus-read-newsrc-file (&optional force)
1977   "Read startup file.
1978 If FORCE is non-nil, the .newsrc file is read."
1979   ;; Reset variables that might be defined in the .newsrc.eld file.
1980   (gnus-clear-quick-file-variables)
1981   (let* ((newsrc-file gnus-current-startup-file)
1982          (quick-file (concat newsrc-file ".el")))
1983     (save-excursion
1984       ;; We always load the .newsrc.eld file.  If always contains
1985       ;; much information that can not be gotten from the .newsrc
1986       ;; file (ticked articles, killed groups, foreign methods, etc.)
1987       (gnus-read-newsrc-el-file quick-file)
1988
1989       (when (and gnus-read-newsrc-file
1990                  (file-exists-p gnus-current-startup-file)
1991                  (or force
1992                      (and (file-newer-than-file-p newsrc-file quick-file)
1993                           (file-newer-than-file-p newsrc-file
1994                                                   (concat quick-file "d")))
1995                      (not gnus-newsrc-alist)))
1996         ;; We read the .newsrc file.  Note that if there if a
1997         ;; .newsrc.eld file exists, it has already been read, and
1998         ;; the `gnus-newsrc-hashtb' has been created.  While reading
1999         ;; the .newsrc file, Gnus will only use the information it
2000         ;; can find there for changing the data already read -
2001         ;; i. e., reading the .newsrc file will not trash the data
2002         ;; already read (except for read articles).
2003         (save-excursion
2004           (gnus-message 5 "Reading %s..." newsrc-file)
2005           (set-buffer (nnheader-find-file-noselect newsrc-file))
2006           (buffer-disable-undo)
2007           (gnus-newsrc-to-gnus-format)
2008           (kill-buffer (current-buffer))
2009           (gnus-message 5 "Reading %s...done" newsrc-file))))))
2010
2011 (defun gnus-read-newsrc-el-file (file)
2012   (let ((ding-file (concat file "d")))
2013     ;; We always, always read the .eld file.
2014     (gnus-message 5 "Reading %s..." ding-file)
2015     (let (gnus-newsrc-assoc)
2016       (when (file-exists-p ding-file)
2017         (with-temp-buffer
2018           (condition-case nil
2019               (progn
2020                 (insert-file-contents-as-coding-system
2021                  gnus-startup-file-coding-system ding-file)
2022                 (eval-region (point-min) (point-max)))
2023             (error
2024              (ding)
2025              (or (not (or (zerop (buffer-size))
2026                           (eq 'binary gnus-startup-file-coding-system)
2027                           (gnus-re-read-newsrc-el-file ding-file)))
2028                  (gnus-yes-or-no-p
2029                   (format "Error in %s; continue? " ding-file))
2030                  (error "Error in %s" ding-file)))))
2031         (when gnus-newsrc-assoc
2032           (setq gnus-newsrc-alist gnus-newsrc-assoc))))
2033     (gnus-make-hashtable-from-newsrc-alist)
2034     (when (file-newer-than-file-p file ding-file)
2035       ;; Old format quick file
2036       (gnus-message 5 "Reading %s..." file)
2037       ;; The .el file is newer than the .eld file, so we read that one
2038       ;; as well.
2039       (gnus-read-old-newsrc-el-file file)))
2040   (when (and gnus-product-directory
2041              (file-directory-p gnus-product-directory))
2042     (let ((list gnus-product-variable-file-list))
2043       (while list
2044         (apply 'gnus-product-read-variable-file-1 (car list))
2045         (setq list (cdr list))))))
2046
2047 (defun gnus-re-read-newsrc-el-file (file)
2048   "Attempt to re-read .newsrc.eld file.  Returns `nil' if successful.
2049 The backup file \".newsrc.eld_\" will be created before re-reading."
2050   (message "Error in %s; retrying..." file)
2051   (if (and
2052        (condition-case nil
2053            (let ((backup (concat file "_")))
2054              (copy-file file backup 'ok-if-already-exists 'keep-time)
2055              (message " (The backup file %s has been created)" backup)
2056              t)
2057          (error nil))
2058        (progn
2059          (insert-file-contents-as-binary file nil nil nil 'replace)
2060          (when (re-search-forward
2061                 "^[\t ]*([\t\n\r ]*setq[\t\n\r ]+gnus-format-specs" nil t)
2062            (delete-region (goto-char (match-beginning 0)) (forward-list 1))
2063            (decode-coding-region (point-min) (point-max)
2064                                  gnus-startup-file-coding-system)
2065            (condition-case nil
2066                (progn
2067                  (eval-region (point-min) (point-max))
2068                  t)
2069              (error nil)))))
2070       (prog1
2071           nil
2072         (message "Error in %s; retrying...done" file))
2073     (message "Error in %s; retrying...failed" file)
2074     t))
2075
2076 (defun gnus-product-read-variable-file-1 (file checking-methods coding
2077                                                &rest variables)
2078   (let (error gnus-product-file-version method file-ver)
2079     (when (or
2080            (condition-case err
2081                (let ((coding-system-for-read coding)
2082                      (input-coding-system coding))
2083                  (load (expand-file-name file gnus-product-directory) t nil t)
2084                  nil)
2085              (error
2086               (message "%s" err)
2087               (setq error t)))
2088            (and (assq 'emacs-version checking-methods)
2089                 (not (string= emacs-version
2090                               (cdr (assq 'emacs-version
2091                                          gnus-product-file-version)))))
2092            (and (setq method (assq 'product-version checking-methods))
2093                 (or (not (setq file-ver
2094                                (cdr (assq 'product-version
2095                                           gnus-product-file-version))))
2096                     (< (product-version-compare file-ver (cadr method)) 0))))
2097       (unless error
2098         (message "\"%s\" seems to have mismatched contents, updating..."
2099                  file))
2100       (while variables
2101         (set (car variables) nil)
2102         (gnus-product-variable-touch (car variables))
2103         (setq variables (cdr variables))))))
2104
2105 ;; Parse the old-style quick startup file
2106 (defun gnus-read-old-newsrc-el-file (file)
2107   (let (newsrc killed marked group m info)
2108     (prog1
2109         (let ((gnus-killed-assoc nil)
2110               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2111           (prog1
2112               (ignore-errors
2113                 (load file t t t))
2114             (setq newsrc gnus-newsrc-assoc
2115                   killed gnus-killed-assoc
2116                   marked gnus-marked-assoc)))
2117       (setq gnus-newsrc-alist nil)
2118       (while (setq group (pop newsrc))
2119         (if (setq info (gnus-get-info (car group)))
2120             (progn
2121               (gnus-info-set-read info (cddr group))
2122               (gnus-info-set-level
2123                info (if (nth 1 group) gnus-level-default-subscribed
2124                       gnus-level-default-unsubscribed))
2125               (push info gnus-newsrc-alist))
2126           (push (setq info
2127                       (list (car group)
2128                             (if (nth 1 group) gnus-level-default-subscribed
2129                               gnus-level-default-unsubscribed)
2130                             (cddr group)))
2131                 gnus-newsrc-alist))
2132         ;; Copy marks into info.
2133         (when (setq m (assoc (car group) marked))
2134           (unless (nthcdr 3 info)
2135             (nconc info (list nil)))
2136           (gnus-info-set-marks
2137            info (list (cons 'tick (gnus-compress-sequence
2138                                    (sort (cdr m) '<) t))))))
2139       (setq newsrc killed)
2140       (while newsrc
2141         (setcar newsrc (caar newsrc))
2142         (setq newsrc (cdr newsrc)))
2143       (setq gnus-killed-list killed))
2144     ;; The .el file version of this variable does not begin with
2145     ;; "options", while the .eld version does, so we just add it if it
2146     ;; isn't there.
2147     (when
2148         gnus-newsrc-options
2149       (when (not (string-match "^ *options" gnus-newsrc-options))
2150         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2151       (when (not (string-match "\n$" gnus-newsrc-options))
2152         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2153       ;; Finally, if we read some options lines, we parse them.
2154       (unless (string= gnus-newsrc-options "")
2155         (gnus-newsrc-parse-options gnus-newsrc-options)))
2156
2157     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2158     (gnus-make-hashtable-from-newsrc-alist)))
2159
2160 (defun gnus-make-newsrc-file (file)
2161   "Make server dependent file name by catenating FILE and server host name."
2162   (let* ((file (expand-file-name file nil))
2163          (real-file (concat file "-" (nth 1 gnus-select-method))))
2164     (if (or (file-exists-p real-file)
2165             (file-exists-p (concat real-file ".el"))
2166             (file-exists-p (concat real-file ".eld")))
2167         real-file
2168       file)))
2169
2170 (defun gnus-newsrc-to-gnus-format ()
2171   (setq gnus-newsrc-options "")
2172   (setq gnus-newsrc-options-n nil)
2173
2174   (unless gnus-active-hashtb
2175     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2176   (let ((buf (current-buffer))
2177         (already-read (> (length gnus-newsrc-alist) 1))
2178         group subscribed options-symbol newsrc Options-symbol
2179         symbol reads num1)
2180     (goto-char (point-min))
2181     ;; We intern the symbol `options' in the active hashtb so that we
2182     ;; can `eq' against it later.
2183     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2184     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2185
2186     (while (not (eobp))
2187       ;; We first read the first word on the line by narrowing and
2188       ;; then reading into `gnus-active-hashtb'.  Most groups will
2189       ;; already exist in that hashtb, so this will save some string
2190       ;; space.
2191       (narrow-to-region
2192        (point)
2193        (progn (skip-chars-forward "^ \t!:\n") (point)))
2194       (goto-char (point-min))
2195       (setq symbol
2196             (and (/= (point-min) (point-max))
2197                  (let ((obarray gnus-active-hashtb)) (read buf))))
2198       (widen)
2199       ;; Now, the symbol we have read is either `options' or a group
2200       ;; name.  If it is an options line, we just add it to a string.
2201       (cond
2202        ((or (eq symbol options-symbol)
2203             (eq symbol Options-symbol))
2204         (setq gnus-newsrc-options
2205               ;; This concating is quite inefficient, but since our
2206               ;; thorough studies show that approx 99.37% of all
2207               ;; .newsrc files only contain a single options line, we
2208               ;; don't give a damn, frankly, my dear.
2209               (concat gnus-newsrc-options
2210                       (buffer-substring
2211                        (gnus-point-at-bol)
2212                        ;; Options may continue on the next line.
2213                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2214                                 (progn (beginning-of-line) (point)))
2215                            (point)))))
2216         (forward-line -1))
2217        (symbol
2218         ;; Group names can be just numbers.
2219         (when (numberp symbol)
2220           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2221         (unless (boundp symbol)
2222           (set symbol nil))
2223         ;; It was a group name.
2224         (setq subscribed (eq (char-after) ?:)
2225               group (symbol-name symbol)
2226               reads nil)
2227         (if (eolp)
2228             ;; If the line ends here, this is clearly a buggy line, so
2229             ;; we put point a the beginning of line and let the cond
2230             ;; below do the error handling.
2231             (beginning-of-line)
2232           ;; We skip to the beginning of the ranges.
2233           (skip-chars-forward "!: \t"))
2234         ;; We are now at the beginning of the list of read articles.
2235         ;; We read them range by range.
2236         (while
2237             (cond
2238              ((looking-at "[0-9]+")
2239               ;; We narrow and read a number instead of buffer-substring/
2240               ;; string-to-int because it's faster.  narrow/widen is
2241               ;; faster than save-restriction/narrow, and save-restriction
2242               ;; produces a garbage object.
2243               (setq num1 (progn
2244                            (narrow-to-region (match-beginning 0) (match-end 0))
2245                            (read buf)))
2246               (widen)
2247               ;; If the next character is a dash, then this is a range.
2248               (if (eq (char-after) ?-)
2249                   (progn
2250                     ;; We read the upper bound of the range.
2251                     (forward-char 1)
2252                     (if (not (looking-at "[0-9]+"))
2253                         ;; This is a buggy line, by we pretend that
2254                         ;; it's kinda OK.  Perhaps the user should be
2255                         ;; dinged?
2256                         (push num1 reads)
2257                       (push
2258                        (cons num1
2259                              (progn
2260                                (narrow-to-region (match-beginning 0)
2261                                                  (match-end 0))
2262                                (read buf)))
2263                        reads)
2264                       (widen)))
2265                 ;; It was just a simple number, so we add it to the
2266                 ;; list of ranges.
2267                 (push num1 reads))
2268               ;; If the next char in ?\n, then we have reached the end
2269               ;; of the line and return nil.
2270               (not (eq (char-after) ?\n)))
2271              ((eq (char-after) ?\n)
2272               ;; End of line, so we end.
2273               nil)
2274              (t
2275               ;; Not numbers and not eol, so this might be a buggy
2276               ;; line...
2277               (unless (eobp)
2278                 ;; If it was eob instead of ?\n, we allow it.
2279                 ;; The line was buggy.
2280                 (setq group nil)
2281                 (gnus-error 3.1 "Mangled line: %s"
2282                             (buffer-substring (gnus-point-at-bol)
2283                                               (gnus-point-at-eol))))
2284               nil))
2285           ;; Skip past ", ".  Spaces are invalid in these ranges, but
2286           ;; we allow them, because it's a common mistake to put a
2287           ;; space after the comma.
2288           (skip-chars-forward ", "))
2289
2290         ;; We have already read .newsrc.eld, so we gently update the
2291         ;; data in the hash table with the information we have just
2292         ;; read.
2293         (when group
2294           (let ((info (gnus-get-info group))
2295                 level)
2296             (if info
2297                 ;; There is an entry for this file in the alist.
2298                 (progn
2299                   (gnus-info-set-read info (nreverse reads))
2300                   ;; We update the level very gently.  In fact, we
2301                   ;; only change it if there's been a status change
2302                   ;; from subscribed to unsubscribed, or vice versa.
2303                   (setq level (gnus-info-level info))
2304                   (cond ((and (<= level gnus-level-subscribed)
2305                               (not subscribed))
2306                          (setq level (if reads
2307                                          gnus-level-default-unsubscribed
2308                                        (1+ gnus-level-default-unsubscribed))))
2309                         ((and (> level gnus-level-subscribed) subscribed)
2310                          (setq level gnus-level-default-subscribed)))
2311                   (gnus-info-set-level info level))
2312               ;; This is a new group.
2313               (setq info (list group
2314                                (if subscribed
2315                                    gnus-level-default-subscribed
2316                                  (if reads
2317                                      (1+ gnus-level-subscribed)
2318                                    gnus-level-default-unsubscribed))
2319                                (nreverse reads))))
2320             (push info newsrc)))))
2321       (forward-line 1))
2322
2323     (setq newsrc (nreverse newsrc))
2324
2325     (if (not already-read)
2326         ()
2327       ;; We now have two newsrc lists - `newsrc', which is what we
2328       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2329       ;; what we've read from .newsrc.eld.  We have to merge these
2330       ;; lists.  We do this by "attaching" any (foreign) groups in the
2331       ;; gnus-newsrc-alist to the (native) group that precedes them.
2332       (let ((rc (cdr gnus-newsrc-alist))
2333             (prev gnus-newsrc-alist)
2334             entry mentry)
2335         (while rc
2336           (or (null (nth 4 (car rc)))   ; It's a native group.
2337               (assoc (caar rc) newsrc)  ; It's already in the alist.
2338               (if (setq entry (assoc (caar prev) newsrc))
2339                   (setcdr (setq mentry (memq entry newsrc))
2340                           (cons (car rc) (cdr mentry)))
2341                 (push (car rc) newsrc)))
2342           (setq prev rc
2343                 rc (cdr rc)))))
2344
2345     (setq gnus-newsrc-alist newsrc)
2346     ;; We make the newsrc hashtb.
2347     (gnus-make-hashtable-from-newsrc-alist)
2348
2349     ;; Finally, if we read some options lines, we parse them.
2350     (unless (string= gnus-newsrc-options "")
2351       (gnus-newsrc-parse-options gnus-newsrc-options))))
2352
2353 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2354 ;; The return value will be a list on the form
2355 ;; ((regexp1 . ignore)
2356 ;;  (regexp2 . subscribe)...)
2357 ;; When handling new newsgroups, groups that match a `ignore' regexp
2358 ;; will be ignored, and groups that match a `subscribe' regexp will be
2359 ;; subscribed.  A line like
2360 ;; options -n !all rec.all
2361 ;; will lead to a list that looks like
2362 ;; (("^rec\\..+" . subscribe)
2363 ;;  ("^.+" . ignore))
2364 ;; So all "rec.*" groups will be subscribed, while all the other
2365 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2366 ;; different from "options -n rec.all !all".
2367 (defun gnus-newsrc-parse-options (options)
2368   (let (out eol)
2369     (save-excursion
2370       (gnus-set-work-buffer)
2371       (insert (regexp-quote options))
2372       ;; First we treat all continuation lines.
2373       (goto-char (point-min))
2374       (while (re-search-forward "\n[ \t]+" nil t)
2375         (replace-match " " t t))
2376       ;; Then we transform all "all"s into ".+"s.
2377       (goto-char (point-min))
2378       (while (re-search-forward "\\ball\\b" nil t)
2379         (replace-match ".+" t t))
2380       (goto-char (point-min))
2381       ;; We remove all other options than the "-n" ones.
2382       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2383         (replace-match " ")
2384         (forward-char -1))
2385       (goto-char (point-min))
2386
2387       ;; We are only interested in "options -n" lines - we
2388       ;; ignore the other option lines.
2389       (while (re-search-forward "[ \t]-n" nil t)
2390         (setq eol
2391               (or (save-excursion
2392                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
2393                          (- (point) 2)))
2394                   (gnus-point-at-eol)))
2395         ;; Search for all "words"...
2396         (while (re-search-forward "[^ \t,\n]+" eol t)
2397           (if (eq (char-after (match-beginning 0)) ?!)
2398               ;; If the word begins with a bang (!), this is a "not"
2399               ;; spec.  We put this spec (minus the bang) and the
2400               ;; symbol `ignore' into the list.
2401               (push (cons (concat
2402                            "^" (buffer-substring
2403                                 (1+ (match-beginning 0))
2404                                 (match-end 0))
2405                            "\\($\\|\\.\\)")
2406                           'ignore)
2407                     out)
2408             ;; There was no bang, so this is a "yes" spec.
2409             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2410                         'subscribe)
2411                   out))))
2412
2413       (setq gnus-newsrc-options-n out))))
2414
2415 (defun gnus-save-newsrc-file (&optional force)
2416   "Save .newsrc file."
2417   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2418   ;; from the variable gnus-newsrc-alist.
2419   (when (and (or gnus-newsrc-alist gnus-killed-list)
2420              gnus-current-startup-file)
2421     (save-excursion
2422       (if (and (or gnus-use-dribble-file gnus-slave)
2423                (not force)
2424                (or (not gnus-dribble-buffer)
2425                    (not (buffer-name gnus-dribble-buffer))
2426                    (zerop (save-excursion
2427                             (set-buffer gnus-dribble-buffer)
2428                             (buffer-size)))))
2429           (gnus-message 4 "(No changes need to be saved)")
2430         (gnus-run-hooks 'gnus-save-newsrc-hook)
2431         (if gnus-slave
2432             (gnus-slave-save-newsrc)
2433           ;; Save .newsrc.
2434           (when gnus-save-newsrc-file
2435             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2436             (gnus-gnus-to-newsrc-format)
2437             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2438           ;; Save .newsrc.eld.
2439           (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
2440           (make-local-variable 'version-control)
2441           (setq version-control 'never)
2442           (setq buffer-file-name
2443                 (concat gnus-current-startup-file ".eld"))
2444           (setq default-directory (file-name-directory buffer-file-name))
2445           (buffer-disable-undo)
2446           (erase-buffer)
2447           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2448           (gnus-gnus-to-quick-newsrc-format)
2449           (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2450           (save-buffer-as-coding-system gnus-startup-file-coding-system)
2451           (kill-buffer (current-buffer))
2452           (gnus-message
2453            5 "Saving %s.eld...done" gnus-current-startup-file))
2454         (gnus-dribble-delete-file)
2455         (gnus-group-set-mode-line))))
2456   (when gnus-product-directory
2457     (gnus-product-save-variable-file)))
2458
2459 ;; Call the function above at C-x C-c.
2460 (defadvice save-buffers-kill-emacs (before save-gnus-newsrc-file-maybe
2461                                            activate preactivate)
2462   "Save .newsrc and .newsrc.eld when Emacs is killed."
2463   (when (gnus-alive-p)
2464     (gnus-run-hooks 'gnus-exit-gnus-hook)
2465     (gnus-offer-save-summaries)
2466     (gnus-save-newsrc-file)))
2467
2468 (defun gnus-gnus-to-quick-newsrc-format ()
2469   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
2470   (let ((print-quoted t)
2471         (print-escape-newlines t))
2472
2473     (insert ";; -*- emacs-lisp -*-\n")
2474     (insert ";; Gnus startup file.\n")
2475     (insert "\
2476 ;; Never delete this file -- if you want to force Gnus to read the
2477 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2478     (insert "(setq gnus-newsrc-file-version "
2479             (prin1-to-string gnus-version) ")\n")
2480     (let* ((gnus-killed-list
2481             (if (and gnus-save-killed-list
2482                      (stringp gnus-save-killed-list))
2483                 (gnus-strip-killed-list)
2484               gnus-killed-list))
2485            (variables
2486             (if gnus-save-killed-list gnus-variable-list
2487               ;; Remove the `gnus-killed-list' from the list of variables
2488               ;; to be saved, if required.
2489               (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
2490            ;; Peel off the "dummy" group.
2491            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2492            variable)
2493       ;; Insert the variables into the file.
2494       (while variables
2495         (when (and (boundp (setq variable (pop variables)))
2496                    (symbol-value variable))
2497           (insert "(setq " (symbol-name variable) " '")
2498           (gnus-prin1 (symbol-value variable))
2499           (insert ")\n"))))))
2500
2501 (defun gnus-product-variable-touch (&rest variables)
2502   (while variables
2503     (put (pop variables) 'gnus-product-variable 'dirty)))
2504
2505 (defun gnus-product-variables-dirty-p (variables)
2506   (catch 'done
2507     (while variables
2508       (when (eq (get (car variables) 'gnus-product-variable) 'dirty)
2509         (throw 'done t))
2510       (setq variables (cdr variables)))))
2511
2512 (defun gnus-product-save-variable-file (&optional force)
2513   "Save all product variables to files, when need to be saved."
2514   (let ((list gnus-product-variable-file-list))
2515     (gnus-make-directory gnus-product-directory)
2516     (while list
2517       (apply 'gnus-product-save-variable-file-1 force (car list))
2518       (setq list (cdr list)))))
2519
2520 (defun gnus-product-save-variable-file-1 (force file checking-methods coding
2521                                                 &rest variables)
2522   "Save a product variable file, when need to be saved."
2523   (when (or force
2524             (gnus-product-variables-dirty-p variables))
2525     (let ((product (product-find 'gnus-vers)))
2526       (set-buffer (gnus-get-buffer-create " *gnus-product*"))
2527       (make-local-variable 'version-control)
2528       (setq version-control 'never)
2529       (setq file (expand-file-name file gnus-product-directory)
2530             buffer-file-name file
2531             default-directory (file-name-directory file))
2532       (buffer-disable-undo)
2533       (erase-buffer)
2534       (gnus-message 5 "Saving %s..." file)
2535       (apply 'gnus-product-quick-file-format product checking-methods coding
2536              variables)
2537       (save-buffer-as-coding-system coding)
2538       (kill-buffer (current-buffer))
2539       (while variables
2540         (put (car variables) 'gnus-product-variable nil)
2541         (setq variables (cdr variables)))
2542       (gnus-message
2543        5 "Saving %s...done" file))))
2544
2545 (defun gnus-product-quick-file-format (product checking-methods
2546                                                coding &rest variables)
2547   "Insert gnus product depend variables in lisp format."
2548   (let ((print-quoted t)
2549         (print-escape-newlines t)
2550         variable param)
2551     (insert (format ";; -*- Mode: emacs-lisp; coding: %s -*-\n" coding))
2552     (insert (format ";; %s startup file.\n" (product-name product)))
2553     (when (setq param (cdr (assq 'product-version checking-methods)))
2554       (insert "(or (>= (product-version-compare "
2555               "(product-version (product-find 'gnus-vers))\n"
2556               "\t\t\t\t '" (apply 'prin1-to-string param) ")\n"
2557               "\t0)\n"
2558               "    (error \"This file was created by later version of "
2559               "gnus.\"))\n"))
2560     (insert "(setq gnus-product-file-version \n"
2561             "      '((product-version . "
2562             (prin1-to-string (product-version product)) ")\n"
2563             "\t(emacs-version . " (prin1-to-string emacs-version) ")))\n")
2564     (while variables
2565       (when (and (boundp (setq variable (pop variables)))
2566                  (symbol-value variable))
2567           (insert "(setq " (symbol-name variable) " '")
2568           (gnus-prin1 (symbol-value variable))
2569           (insert ")\n")))))
2570
2571 (defun gnus-strip-killed-list ()
2572   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2573   (let ((list gnus-killed-list)
2574         olist)
2575     (while list
2576       (when (string-match gnus-save-killed-list (car list))
2577         (push (car list) olist))
2578       (pop list))
2579     (nreverse olist)))
2580
2581 (defun gnus-gnus-to-newsrc-format ()
2582   ;; Generate and save the .newsrc file.
2583   (save-excursion
2584     (set-buffer (create-file-buffer gnus-current-startup-file))
2585     (let ((newsrc (cdr gnus-newsrc-alist))
2586           (standard-output (current-buffer))
2587           info ranges range method)
2588       (setq buffer-file-name gnus-current-startup-file)
2589       (setq default-directory (file-name-directory buffer-file-name))
2590       (buffer-disable-undo)
2591       (erase-buffer)
2592       ;; Write options.
2593       (when gnus-newsrc-options
2594         (insert gnus-newsrc-options))
2595       ;; Write subscribed and unsubscribed.
2596       (while (setq info (pop newsrc))
2597         ;; Don't write foreign groups to .newsrc.
2598         (when (or (null (setq method (gnus-info-method info)))
2599                   (equal method "native")
2600                   (inline (gnus-server-equal method gnus-select-method)))
2601           (insert (gnus-info-group info)
2602                   (if (> (gnus-info-level info) gnus-level-subscribed)
2603                       "!" ":"))
2604           (when (setq ranges (gnus-info-read info))
2605             (insert " ")
2606             (if (not (listp (cdr ranges)))
2607                 (if (= (car ranges) (cdr ranges))
2608                     (princ (car ranges))
2609                   (princ (car ranges))
2610                   (insert "-")
2611                   (princ (cdr ranges)))
2612               (while (setq range (pop ranges))
2613                 (if (or (atom range) (= (car range) (cdr range)))
2614                     (princ (or (and (atom range) range) (car range)))
2615                   (princ (car range))
2616                   (insert "-")
2617                   (princ (cdr range)))
2618                 (when ranges
2619                   (insert ",")))))
2620           (insert "\n")))
2621       (make-local-variable 'version-control)
2622       (setq version-control 'never)
2623       ;; It has been reported that sometime the modtime on the .newsrc
2624       ;; file seems to be off.  We really do want to overwrite it, so
2625       ;; we clear the modtime here before saving.  It's a bit odd,
2626       ;; though...
2627       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2628       ;; delete the silly thing entirely first.  but this fails to provide
2629       ;; such niceties as .newsrc~ creation.
2630       (if gnus-modtime-botch
2631           (delete-file gnus-startup-file)
2632         (clear-visited-file-modtime))
2633       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2634       (save-buffer)
2635       (kill-buffer (current-buffer)))))
2636
2637 \f
2638 ;;;
2639 ;;; Slave functions.
2640 ;;;
2641
2642 (defvar gnus-slave-mode nil)
2643
2644 (defun gnus-slave-mode ()
2645   "Minor mode for slave Gnusae."
2646   (gnus-add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
2647   (gnus-run-hooks 'gnus-slave-mode-hook))
2648
2649 (defun gnus-slave-save-newsrc ()
2650   (save-excursion
2651     (set-buffer gnus-dribble-buffer)
2652     (let ((slave-name
2653            (make-temp-name (concat gnus-current-startup-file "-slave-")))
2654           (modes (ignore-errors
2655                    (file-modes (concat gnus-current-startup-file ".eld")))))
2656       (gnus-write-buffer-as-coding-system
2657        gnus-startup-file-coding-system slave-name)
2658       (when modes
2659         (set-file-modes slave-name modes)))))
2660
2661 (defun gnus-master-read-slave-newsrc ()
2662   (let ((slave-files
2663          (directory-files
2664           (file-name-directory gnus-current-startup-file)
2665           t (concat
2666              "^" (regexp-quote
2667                   (concat
2668                    (file-name-nondirectory gnus-current-startup-file)
2669                    "-slave-")))
2670           t))
2671         file)
2672     (if (not slave-files)
2673         ()                              ; There are no slave files to read.
2674       (gnus-message 7 "Reading slave newsrcs...")
2675       (save-excursion
2676         (set-buffer (gnus-get-buffer-create " *gnus slave*"))
2677         (setq slave-files
2678               (sort (mapcar (lambda (file)
2679                               (list (nth 5 (file-attributes file)) file))
2680                             slave-files)
2681                     (lambda (f1 f2)
2682                       (or (< (caar f1) (caar f2))
2683                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
2684         (while slave-files
2685           (erase-buffer)
2686           (setq file (nth 1 (car slave-files)))
2687           (nnheader-insert-file-contents file)
2688           (when (condition-case ()
2689                     (progn
2690                       (eval-buffer (current-buffer))
2691                       t)
2692                   (error
2693                    (gnus-error 3.2 "Possible error in %s" file)
2694                    nil))
2695             (unless gnus-slave          ; Slaves shouldn't delete these files.
2696               (ignore-errors
2697                 (delete-file file))))
2698           (setq slave-files (cdr slave-files))))
2699       (gnus-dribble-touch)
2700       (gnus-message 7 "Reading slave newsrcs...done"))))
2701
2702 \f
2703 ;;;
2704 ;;; Group description.
2705 ;;;
2706
2707 (defun gnus-read-all-descriptions-files ()
2708   (let ((methods (cons gnus-select-method
2709                        (nconc
2710                         (when (gnus-archive-server-wanted-p)
2711                           (list "archive"))
2712                         gnus-secondary-select-methods))))
2713     (while methods
2714       (gnus-read-descriptions-file (car methods))
2715       (setq methods (cdr methods)))
2716     t))
2717
2718 (defun gnus-read-descriptions-file (&optional method)
2719   (let ((method (or method gnus-select-method))
2720         group)
2721     (when (stringp method)
2722       (setq method (gnus-server-to-method method)))
2723     ;; We create the hashtable whether we manage to read the desc file
2724     ;; to avoid trying to re-read after a failed read.
2725     (unless gnus-description-hashtb
2726       (setq gnus-description-hashtb
2727             (gnus-make-hashtable (length gnus-active-hashtb))))
2728     ;; Mark this method's desc file as read.
2729     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
2730                   gnus-description-hashtb)
2731
2732     (gnus-message 5 "Reading descriptions file via %s..." (car method))
2733     (cond
2734      ((null (gnus-get-function method 'request-list-newsgroups t))
2735       t)
2736      ((not (gnus-check-server method))
2737       (gnus-message 1 "Couldn't open server")
2738       nil)
2739      ((not (gnus-request-list-newsgroups method))
2740       (gnus-message 1 "Couldn't read newsgroups descriptions")
2741       nil)
2742      (t
2743       (save-excursion
2744         (save-restriction
2745           (set-buffer nntp-server-buffer)
2746           (goto-char (point-min))
2747           (when (or (search-forward "\n.\n" nil t)
2748                     (goto-char (point-max)))
2749             (beginning-of-line)
2750             (narrow-to-region (point-min) (point)))
2751           ;; If these are groups from a foreign select method, we insert the
2752           ;; group prefix in front of the group names.
2753           (and method (not (inline
2754                              (gnus-server-equal
2755                               (gnus-server-get-method nil method)
2756                               (gnus-server-get-method
2757                                nil gnus-select-method))))
2758                (let ((prefix (gnus-group-prefixed-name "" method)))
2759                  (goto-char (point-min))
2760                  (while (and (not (eobp))
2761                              (progn (insert prefix)
2762                                     (zerop (forward-line 1)))))))
2763           (goto-char (point-min))
2764           (while (not (eobp))
2765             ;; If we get an error, we set group to 0, which is not a
2766             ;; symbol...
2767             (setq group
2768                   (condition-case ()
2769                       (let ((obarray gnus-description-hashtb))
2770                         ;; Group is set to a symbol interned in this
2771                         ;; hash table.
2772                         (read nntp-server-buffer))
2773                     (error 0)))
2774             (skip-chars-forward " \t")
2775             ;; ...  which leads to this line being effectively ignored.
2776             (when (symbolp group)
2777               (let ((str (buffer-substring
2778                           (point) (progn (end-of-line) (point))))
2779                     (coding
2780                      (and (or (featurep 'xemacs)
2781                               (and (boundp 'enable-multibyte-characters)
2782                                    enable-multibyte-characters))
2783                           (fboundp 'gnus-mule-get-coding-system)
2784                           (gnus-mule-get-coding-system (symbol-name group)))))
2785                 (when coding
2786                   (setq str (decode-coding-string str (car coding))))
2787                 (set group str)))
2788             (forward-line 1))))
2789       (gnus-message 5 "Reading descriptions file...done")
2790       t))))
2791
2792 (defun gnus-group-get-description (group)
2793   "Get the description of a group by sending XGTITLE to the server."
2794   (when (gnus-request-group-description group)
2795     (save-excursion
2796       (set-buffer nntp-server-buffer)
2797       (goto-char (point-min))
2798       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
2799         (match-string 1)))))
2800
2801 ;;;###autoload
2802 (defun gnus-declare-backend (name &rest abilities)
2803   "Declare backend NAME with ABILITIES as a Gnus backend."
2804   (setq gnus-valid-select-methods
2805         (nconc gnus-valid-select-methods
2806                (list (apply 'list name abilities))))
2807   (gnus-redefine-select-method-widget))
2808
2809 (defun gnus-set-default-directory ()
2810   "Set the default directory in the current buffer to `gnus-default-directory'.
2811 If this variable is nil, don't do anything."
2812   (setq default-directory
2813         (if (and gnus-default-directory
2814                  (file-exists-p gnus-default-directory))
2815             (file-name-as-directory (expand-file-name gnus-default-directory))
2816           default-directory)))
2817
2818 (defun gnus-display-time-event-handler ()
2819   "Like `display-time-event-handler', but test `display-time-timer'."
2820   (when (gnus-boundp 'display-time-timer)
2821     (display-time-event-handler)))
2822
2823 (provide 'gnus-start)
2824
2825 ;;; gnus-start.el ends here