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