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