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