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