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