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