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