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