Merge from emacs--devo--0
[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 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 `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   (save-excursion
598     (set-buffer (nnheader-find-file-noselect gnus-current-startup-file))
599     (prog1
600         (let ((groupkey newgroup) before)
601           (while (and (not before) groupkey)
602             (goto-char (point-min))
603             (let ((groupkey-re
604                    (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
605               (while (and (re-search-forward groupkey-re nil t)
606                           (progn
607                             (setq before (match-string 1))
608                             (string< before newgroup)))))
609             ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
610             (setq groupkey
611                   (when (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
612                     (substring groupkey (match-beginning 1) (match-end 1)))))
613           (gnus-subscribe-newsgroup newgroup before))
614       (kill-buffer (current-buffer)))))
615
616 (defun gnus-subscribe-interactively (group)
617   "Subscribe the new GROUP interactively.
618 It is inserted in hierarchical newsgroup order if subscribed.  If not,
619 it is killed."
620   (if (gnus-y-or-n-p (format "Subscribe new newsgroup %s? " group))
621       (gnus-subscribe-hierarchically group)
622     (push group gnus-killed-list)))
623
624 (defun gnus-subscribe-zombies (group)
625   "Make the new GROUP into a zombie group."
626   (push group gnus-zombie-list))
627
628 (defun gnus-subscribe-killed (group)
629   "Make the new GROUP a killed group."
630   (push group gnus-killed-list))
631
632 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
633   "Subscribe new NEWSGROUP.
634 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
635 the first newsgroup."
636   (save-excursion
637     (goto-char (point-min))
638     ;; We subscribe the group by changing its level to `subscribed'.
639     (gnus-group-change-level
640      newsgroup gnus-level-default-subscribed
641      gnus-level-killed (gnus-group-entry (or next "dummy.group")))
642     (gnus-message 5 "Subscribe newsgroup: %s" newsgroup)
643     (run-hook-with-args 'gnus-subscribe-newsgroup-hooks newsgroup)
644     t))
645
646 (defun gnus-read-active-file-p ()
647   "Say whether the active file has been read from `gnus-select-method'."
648   (memq gnus-select-method gnus-have-read-active-file))
649
650 ;;; General various misc type functions.
651
652 ;; Silence byte-compiler.
653 (defvar gnus-current-headers)
654 (defvar gnus-thread-indent-array)
655 (defvar gnus-newsgroup-name)
656 (defvar gnus-newsgroup-headers)
657 (defvar gnus-group-list-mode)
658 (defvar gnus-group-mark-positions)
659 (defvar gnus-newsgroup-data)
660 (defvar gnus-newsgroup-unreads)
661 (defvar nnoo-state-alist)
662 (defvar gnus-current-select-method)
663 (defvar mail-sources)
664 (defvar nnmail-scan-directory-mail-source-once)
665 (defvar nnmail-split-history)
666 (defvar nnmail-spool-file)
667
668 (defun gnus-close-all-servers ()
669   "Close all servers."
670   (interactive)
671   (dolist (server gnus-opened-servers)
672     (gnus-close-server (car server))))
673
674 (defun gnus-clear-system ()
675   "Clear all variables and buffers."
676   ;; Clear Gnus variables.
677   (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
678     (while variables
679       (set (car variables) nil)
680       (setq variables (cdr variables))))
681   ;; Clear other internal variables.
682   (setq gnus-list-of-killed-groups nil
683         gnus-have-read-active-file nil
684         gnus-agent-covered-methods nil
685         gnus-agent-file-loading-local nil
686         gnus-agent-file-loading-cache nil
687         gnus-server-method-cache nil
688         gnus-newsrc-alist nil
689         gnus-newsrc-hashtb nil
690         gnus-killed-list nil
691         gnus-zombie-list nil
692         gnus-killed-hashtb nil
693         gnus-active-hashtb nil
694         gnus-moderated-hashtb nil
695         gnus-description-hashtb nil
696         gnus-current-headers nil
697         gnus-thread-indent-array nil
698         gnus-newsgroup-headers nil
699         gnus-newsgroup-name nil
700         gnus-server-alist nil
701         gnus-group-list-mode nil
702         gnus-opened-servers nil
703         gnus-group-mark-positions nil
704         gnus-newsgroup-data nil
705         gnus-newsgroup-unreads nil
706         nnoo-state-alist nil
707         gnus-current-select-method nil
708         nnmail-split-history nil
709         gnus-ephemeral-servers nil)
710   (gnus-shutdown 'gnus)
711   ;; Kill the startup file.
712   (and gnus-current-startup-file
713        (get-file-buffer gnus-current-startup-file)
714        (kill-buffer (get-file-buffer gnus-current-startup-file)))
715   ;; Clear the dribble buffer.
716   (gnus-dribble-clear)
717   ;; Kill global KILL file buffer.
718   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
719     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
720   (gnus-kill-buffer nntp-server-buffer)
721   ;; Kill Gnus buffers.
722   (dolist (buffer (gnus-buffers))
723     (gnus-kill-buffer buffer))
724   ;; Remove Gnus frames.
725   (gnus-kill-gnus-frames))
726
727 (defun gnus-no-server-1 (&optional arg slave)
728   "Read network news.
729 If ARG is a positive number, Gnus will use that as the startup
730 level.  If ARG is nil, Gnus will be started at level 2
731 \(`gnus-level-default-subscribed' minus one).  If ARG is non-nil
732 and not a positive number, Gnus will prompt the user for the name
733 of an NNTP server to use.  As opposed to \\[gnus], this command
734 will not connect to the local server."
735   (interactive "P")
736   (let ((val (or arg (1- gnus-level-default-subscribed))))
737     (gnus val t slave)
738     (make-local-variable 'gnus-group-use-permanent-levels)
739     (setq gnus-group-use-permanent-levels val)))
740
741 (defun gnus-1 (&optional arg dont-connect slave)
742   "Read network news.
743 If ARG is non-nil and a positive number, Gnus will use that as the
744 startup level.  If ARG is non-nil and not a positive number, Gnus will
745 prompt the user for the name of an NNTP server to use."
746   (interactive "P")
747
748   (if (gnus-alive-p)
749       (progn
750         (switch-to-buffer gnus-group-buffer)
751         (gnus-group-get-new-news
752          (and (numberp arg)
753               (> arg 0)
754               (max (car gnus-group-list-mode) arg))))
755
756     (gnus-clear-system)
757     (gnus-splash)
758     (gnus-run-hooks 'gnus-before-startup-hook)
759     (nnheader-init-server-buffer)
760     (setq gnus-slave slave)
761     (gnus-read-init-file)
762
763     ;; Add "native" to gnus-predefined-server-alist just to have a
764     ;; name for the native select method.
765     (when gnus-select-method
766       (push (cons "native" gnus-select-method)
767             gnus-predefined-server-alist))
768     
769     (if gnus-agent
770         (gnus-agentize))
771
772     (when gnus-simple-splash
773       (setq gnus-simple-splash nil)
774       (cond
775        ((featurep 'xemacs)
776         (gnus-xmas-splash))
777        (window-system
778         (gnus-x-splash))))
779
780     (let ((level (and (numberp arg) (> arg 0) arg))
781           did-connect)
782       (unwind-protect
783           (progn
784             (unless dont-connect
785               (setq did-connect
786                     (gnus-start-news-server (and arg (not level))))))
787         (if (and (not dont-connect)
788                  (not did-connect))
789             (gnus-group-quit)
790           (gnus-run-hooks 'gnus-startup-hook)
791           ;; NNTP server is successfully open.
792
793           ;; Find the current startup file name.
794           (setq gnus-current-startup-file
795                 (gnus-make-newsrc-file gnus-startup-file))
796
797           ;; Read the dribble file.
798           (when (or gnus-slave gnus-use-dribble-file)
799             (gnus-dribble-read-file))
800
801           ;; Do the actual startup.
802           (if gnus-agent
803               (gnus-request-create-group "queue" '(nndraft "")))
804           (gnus-request-create-group "drafts" '(nndraft ""))
805           (gnus-setup-news nil level dont-connect)
806           (gnus-run-hooks 'gnus-setup-news-hook)
807           (gnus-start-draft-setup)
808           ;; Generate the group buffer.
809           (gnus-group-list-groups level)
810           (gnus-group-first-unread-group)
811           (gnus-configure-windows 'group)
812           (gnus-group-set-mode-line)
813           (gnus-run-hooks 'gnus-started-hook))))))
814
815 (defun gnus-start-draft-setup ()
816   "Make sure the draft group exists."
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       (save-excursion
860         (set-buffer gnus-group-buffer)
861         (gnus-group-set-mode-line))
862       (set-buffer obuf))))
863
864 (defun gnus-dribble-touch ()
865   "Touch the dribble buffer."
866   (gnus-dribble-enter ""))
867
868 (defun gnus-dribble-read-file ()
869   "Read the dribble file from disk."
870   (let ((dribble-file (gnus-dribble-file-name)))
871     (save-excursion
872       (set-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     (save-excursion
924       (set-buffer gnus-dribble-buffer)
925       (let ((auto (make-auto-save-file-name)))
926         (when (file-exists-p auto)
927           (delete-file auto))
928         (erase-buffer)
929         (set-buffer-modified-p nil)))))
930
931 (defun gnus-dribble-save ()
932   (when (and gnus-dribble-buffer
933              (buffer-name gnus-dribble-buffer))
934     (save-excursion
935       (set-buffer gnus-dribble-buffer)
936       (save-buffer))))
937
938 (defun gnus-dribble-clear ()
939   (when (gnus-buffer-exists-p gnus-dribble-buffer)
940     (save-excursion
941       (set-buffer gnus-dribble-buffer)
942       (erase-buffer)
943       (set-buffer-modified-p nil)
944       (setq buffer-saved-size (buffer-size)))))
945
946 \f
947 ;;;
948 ;;; Active & Newsrc File Handling
949 ;;;
950
951 (defun gnus-setup-news (&optional rawfile level dont-connect)
952   "Setup news information.
953 If RAWFILE is non-nil, the .newsrc file will also be read.
954 If LEVEL is non-nil, the news will be set up at level LEVEL."
955   (require 'nnmail)
956   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
957         ;; Binding this variable will inhibit multiple fetchings
958         ;; of the same mail source.
959         (nnmail-fetched-sources (list t)))
960
961     (when init
962       ;; Clear some variables to re-initialize news information.
963       (setq gnus-newsrc-alist nil
964             gnus-active-hashtb nil)
965       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
966       (gnus-read-newsrc-file rawfile))
967
968     ;; Make sure the archive server is available to all and sundry.
969     (let ((method (or (and (stringp gnus-message-archive-method)
970                            (gnus-server-to-method
971                             gnus-message-archive-method))
972                       gnus-message-archive-method)))
973       ;; Check whether the archive method is writable.
974       (unless (or (not method)
975                   (stringp method)
976                   (memq 'respool (assoc (format "%s" (car method))
977                                         gnus-valid-select-methods)))
978         (setq method "archive")) ;; The default.
979       (when (stringp method)
980         (setq method `(nnfolder
981                        ,method
982                        (nnfolder-directory
983                         ,(nnheader-concat message-directory method))
984                        (nnfolder-active-file
985                         ,(nnheader-concat message-directory
986                                           (concat method "/active")))
987                        (nnfolder-get-new-mail nil)
988                        (nnfolder-inhibit-expiry t))))
989       (if (assoc "archive" gnus-server-alist)
990           (when gnus-update-message-archive-method
991             (if method
992                 (setcdr (assoc "archive" gnus-server-alist) method)
993               (setq gnus-server-alist (delq (assoc "archive" gnus-server-alist)
994                                             gnus-server-alist))))
995         (when method
996           (push (cons "archive" method) gnus-server-alist))))
997
998     ;; If we don't read the complete active file, we fill in the
999     ;; hashtb here.
1000     (when (or (null gnus-read-active-file)
1001               (eq gnus-read-active-file 'some))
1002       (gnus-update-active-hashtb-from-killed))
1003
1004     ;; Validate agent covered methods now that gnus-server-alist has
1005     ;; been initialized.
1006     ;; NOTE: This is here for one purpose only.  By validating the
1007     ;; agentized server's, it converts the old 5.10.3, and earlier,
1008     ;; format to the current format.  That enables the agent code
1009     ;; within gnus-read-active-file to function correctly.
1010     (if gnus-agent
1011         (gnus-agent-read-servers-validate))
1012
1013     ;; Read the active file and create `gnus-active-hashtb'.
1014     ;; If `gnus-read-active-file' is nil, then we just create an empty
1015     ;; hash table.  The partial filling out of the hash table will be
1016     ;; done in `gnus-get-unread-articles'.
1017     (and gnus-read-active-file
1018          (not level)
1019          (gnus-read-active-file nil dont-connect))
1020
1021     (unless gnus-active-hashtb
1022       (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1023
1024     ;; Initialize the cache.
1025     (when gnus-use-cache
1026       (gnus-cache-open))
1027
1028     ;; Possibly eval the dribble file.
1029     (and init
1030          (or gnus-use-dribble-file gnus-slave)
1031          (gnus-dribble-eval-file))
1032
1033     ;; Slave Gnusii should then clear the dribble buffer.
1034     (when (and init gnus-slave)
1035       (gnus-dribble-clear))
1036
1037     (gnus-update-format-specifications)
1038
1039     ;; See whether we need to read the description file.
1040     (when (and (boundp 'gnus-group-line-format)
1041                (stringp gnus-group-line-format)
1042                (let ((case-fold-search nil))
1043                  (string-match "%[-,0-9]*D" gnus-group-line-format))
1044                (not gnus-description-hashtb)
1045                (not dont-connect)
1046                gnus-read-active-file)
1047       (gnus-read-all-descriptions-files))
1048
1049     ;; Find new newsgroups and treat them.
1050     (when (and init gnus-check-new-newsgroups (not level)
1051                (gnus-check-server gnus-select-method)
1052                (not gnus-slave)
1053                gnus-plugged)
1054       (gnus-find-new-newsgroups))
1055
1056     ;; Check and remove bogus newsgroups.
1057     (when (and init gnus-check-bogus-newsgroups
1058                gnus-read-active-file (not level)
1059                (gnus-server-opened gnus-select-method))
1060       (gnus-check-bogus-newsgroups))
1061
1062     ;; We might read in new NoCeM messages here.
1063     (when (and (not dont-connect)
1064                gnus-use-nocem
1065                (or (and (numberp gnus-use-nocem)
1066                         (numberp level)
1067                         (>= level gnus-use-nocem))
1068                    (not level)))
1069       (gnus-nocem-scan-groups))
1070
1071     ;; Read any slave files.
1072     (gnus-master-read-slave-newsrc)
1073
1074     ;; Find the number of unread articles in each non-dead group.
1075     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
1076       (gnus-get-unread-articles level))))
1077
1078 (defun gnus-call-subscribe-functions (method group)
1079   "Call METHOD to subscribe GROUP.
1080 If no function returns `non-nil', call `gnus-subscribe-zombies'."
1081   (unless (cond
1082            ((functionp method)
1083             (funcall method group))
1084            ((listp method)
1085             (catch 'found
1086               (dolist (func method)
1087                 (if (funcall func group)
1088                     (throw 'found t)))
1089               nil))
1090            (t nil))
1091     (gnus-subscribe-zombies group)))
1092
1093 (defun gnus-find-new-newsgroups (&optional arg)
1094   "Search for new newsgroups and add them.
1095 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
1096 The `-n' option line from .newsrc is respected.
1097
1098 With 1 C-u, use the `ask-server' method to query the server for new
1099 groups.
1100 With 2 C-u's, use most complete method possible to query the server
1101 for new groups, and subscribe the new groups as zombies."
1102   (interactive "p")
1103   (let* ((gnus-subscribe-newsgroup-method
1104           gnus-subscribe-newsgroup-method)
1105          (check (cond
1106                  ((or (and (= (or arg 1) 4)
1107                            (not (listp gnus-check-new-newsgroups)))
1108                       (null gnus-read-active-file)
1109                       (eq gnus-read-active-file 'some))
1110                   'ask-server)
1111                  ((= (or arg 1) 16)
1112                   (setq gnus-subscribe-newsgroup-method
1113                         'gnus-subscribe-zombies)
1114                   t)
1115                  (t gnus-check-new-newsgroups))))
1116     (unless (gnus-check-first-time-used)
1117       (if (or (consp check)
1118               (eq check 'ask-server))
1119           ;; Ask the server for new groups.
1120           (gnus-ask-server-for-new-groups)
1121         ;; Go through the active hashtb and look for new groups.
1122         (let ((groups 0)
1123               group new-newsgroups)
1124           (gnus-message 5 "Looking for new newsgroups...")
1125           (unless gnus-have-read-active-file
1126             (gnus-read-active-file))
1127           (setq gnus-newsrc-last-checked-date (message-make-date))
1128           (unless gnus-killed-hashtb
1129             (gnus-make-hashtable-from-killed))
1130           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
1131           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
1132           (mapatoms
1133            (lambda (sym)
1134              (if (or (null (setq group (symbol-name sym)))
1135                      (not (boundp sym))
1136                      (null (symbol-value sym))
1137                      (gnus-gethash group gnus-killed-hashtb)
1138                      (gnus-gethash group gnus-newsrc-hashtb))
1139                  ()
1140                (let ((do-sub (gnus-matches-options-n group)))
1141                  (cond
1142                   ((eq do-sub 'subscribe)
1143                    (setq groups (1+ groups))
1144                    (gnus-sethash group group gnus-killed-hashtb)
1145                    (gnus-call-subscribe-functions
1146                     gnus-subscribe-options-newsgroup-method group))
1147                   ((eq do-sub 'ignore)
1148                    nil)
1149                   (t
1150                    (setq groups (1+ groups))
1151                    (gnus-sethash group group gnus-killed-hashtb)
1152                    (if gnus-subscribe-hierarchical-interactive
1153                        (push group new-newsgroups)
1154                      (gnus-call-subscribe-functions
1155                       gnus-subscribe-newsgroup-method group)))))))
1156            gnus-active-hashtb)
1157           (when new-newsgroups
1158             (gnus-subscribe-hierarchical-interactive new-newsgroups))
1159           (if (> groups 0)
1160               (gnus-message 5 "%d new newsgroup%s arrived."
1161                             groups (if (> groups 1) "s have" " has"))
1162             (gnus-message 5 "No new newsgroups.")))))))
1163
1164 (defun gnus-matches-options-n (group)
1165   ;; Returns `subscribe' if the group is to be unconditionally
1166   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
1167   ;; no match for the group.
1168
1169   ;; First we check the two user variables.
1170   (cond
1171    ((and gnus-options-subscribe
1172          (string-match gnus-options-subscribe group))
1173     'subscribe)
1174    ((and gnus-auto-subscribed-groups
1175          (string-match gnus-auto-subscribed-groups group))
1176     'subscribe)
1177    ((and gnus-options-not-subscribe
1178          (string-match gnus-options-not-subscribe group))
1179     'ignore)
1180    ;; Then we go through the list that was retrieved from the .newsrc
1181    ;; file.  This list has elements on the form
1182    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
1183    ;; is in the reverse order of the options line) is returned.
1184    (t
1185     (let ((regs gnus-newsrc-options-n))
1186       (while (and regs
1187                   (not (string-match (caar regs) group)))
1188         (setq regs (cdr regs)))
1189       (and regs (cdar regs))))))
1190
1191 (defun gnus-ask-server-for-new-groups ()
1192   (let* ((new-date (message-make-date))
1193          (date (or gnus-newsrc-last-checked-date new-date))
1194          (methods (cons gnus-select-method
1195                         (nconc
1196                          (when (gnus-archive-server-wanted-p)
1197                            (list "archive"))
1198                          (append
1199                           (and (consp gnus-check-new-newsgroups)
1200                                gnus-check-new-newsgroups)
1201                           gnus-secondary-select-methods))))
1202          (groups 0)
1203          group new-newsgroups got-new method hashtb
1204          gnus-override-subscribe-method)
1205     (unless gnus-killed-hashtb
1206       (gnus-make-hashtable-from-killed))
1207     ;; Go through both primary and secondary select methods and
1208     ;; request new newsgroups.
1209     (while (setq method (gnus-server-get-method nil (pop methods)))
1210       (setq new-newsgroups nil
1211             gnus-override-subscribe-method method)
1212       (when (and (gnus-check-server method)
1213                  (gnus-request-newgroups date method))
1214         (save-excursion
1215           (setq got-new t
1216                 hashtb (gnus-make-hashtable 100))
1217           (set-buffer nntp-server-buffer)
1218           ;; Enter all the new groups into a hashtable.
1219           (gnus-active-to-gnus-format method hashtb 'ignore))
1220         ;; Now all new groups from `method' are in `hashtb'.
1221         (mapatoms
1222          (lambda (group-sym)
1223            (if (or (null (setq group (symbol-name group-sym)))
1224                    (not (boundp group-sym))
1225                    (null (symbol-value group-sym))
1226                    (gnus-gethash group gnus-newsrc-hashtb)
1227                    (member group gnus-zombie-list)
1228                    (member group gnus-killed-list))
1229                ;; The group is already known.
1230                ()
1231              ;; Make this group active.
1232              (when (symbol-value group-sym)
1233                (gnus-set-active group (symbol-value group-sym)))
1234              ;; Check whether we want it or not.
1235              (let ((do-sub (gnus-matches-options-n group)))
1236                (cond
1237                 ((eq do-sub 'subscribe)
1238                  (incf groups)
1239                  (gnus-sethash group group gnus-killed-hashtb)
1240                  (gnus-call-subscribe-functions
1241                   gnus-subscribe-options-newsgroup-method group))
1242                 ((eq do-sub 'ignore)
1243                  nil)
1244                 (t
1245                  (incf groups)
1246                  (gnus-sethash group group gnus-killed-hashtb)
1247                  (if gnus-subscribe-hierarchical-interactive
1248                      (push group new-newsgroups)
1249                    (gnus-call-subscribe-functions
1250                     gnus-subscribe-newsgroup-method group)))))))
1251          hashtb))
1252       (when new-newsgroups
1253         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
1254     (if (> groups 0)
1255         (gnus-message 5 "%d new newsgroup%s arrived"
1256                       groups (if (> groups 1) "s have" " has"))
1257       (gnus-message 5 "No new newsgroups"))
1258     (when got-new
1259       (setq gnus-newsrc-last-checked-date new-date))
1260     got-new))
1261
1262 (defun gnus-check-first-time-used ()
1263   (catch 'ended
1264     ;; First check if any of the following files exist.  If they do,
1265     ;; it's not the first time the user has used Gnus.
1266     (dolist (file (list (concat gnus-current-startup-file ".el")
1267                         (concat gnus-current-startup-file ".eld")
1268                         (concat gnus-startup-file ".el")
1269                         (concat gnus-startup-file ".eld")))
1270       (when (file-exists-p file)
1271         (throw 'ended nil)))
1272     (gnus-message 6 "First time user; subscribing you to default groups")
1273     (unless (gnus-read-active-file-p)
1274       (let ((gnus-read-active-file t))
1275         (gnus-read-active-file)))
1276     (setq gnus-newsrc-last-checked-date (message-make-date))
1277     ;; Subscribe to the default newsgroups.
1278     (let ((groups (or gnus-default-subscribed-newsgroups
1279                       gnus-backup-default-subscribed-newsgroups))
1280           group)
1281       (if (eq groups t)
1282           ;; If t, we subscribe (or not) all groups as if they were new.
1283           (mapatoms
1284            (lambda (sym)
1285              (when (setq group (symbol-name sym))
1286                (let ((do-sub (gnus-matches-options-n group)))
1287                  (cond
1288                   ((eq do-sub 'subscribe)
1289                    (gnus-sethash group group gnus-killed-hashtb)
1290                    (gnus-call-subscribe-functions
1291                     gnus-subscribe-options-newsgroup-method group))
1292                   ((eq do-sub 'ignore)
1293                    nil)
1294                   (t
1295                    (push group gnus-killed-list))))))
1296            gnus-active-hashtb)
1297         (dolist (group groups)
1298           ;; Only subscribe the default groups that are activated.
1299           (when (gnus-active group)
1300             (gnus-group-change-level
1301              group gnus-level-default-subscribed gnus-level-killed)))
1302         (save-excursion
1303           (set-buffer gnus-group-buffer)
1304           ;; Don't error if the group already exists. This happens when a
1305           ;; first-time user types 'F'. -- didier
1306           (gnus-group-make-help-group t))
1307         (when gnus-novice-user
1308           (gnus-message 7 "`A k' to list killed groups"))))))
1309
1310 (defun gnus-subscribe-group (group &optional previous method)
1311   "Subscribe GROUP and put it after PREVIOUS."
1312   (gnus-group-change-level
1313    (if method
1314        (list t group gnus-level-default-subscribed nil nil method)
1315      group)
1316    gnus-level-default-subscribed gnus-level-killed previous t)
1317   t)
1318
1319 ;; `gnus-group-change-level' is the fundamental function for changing
1320 ;; subscription levels of newsgroups.  This might mean just changing
1321 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
1322 ;; again, which subscribes/unsubscribes a group, which is equally
1323 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
1324 ;; from 8-9 to 1-7 means that you remove the group from the list of
1325 ;; killed (or zombie) groups and add them to the (kinda) subscribed
1326 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
1327 ;; which is trivial.
1328 ;; ENTRY can either be a string (newsgroup name) or a list (if
1329 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
1330 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
1331 ;; entries.
1332 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
1333 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
1334 ;; after.
1335 (defun gnus-group-change-level (entry level &optional oldlevel
1336                                       previous fromkilled)
1337   (let (group info active num)
1338     ;; Glean what info we can from the arguments
1339     (if (consp entry)
1340         (if fromkilled (setq group (nth 1 entry))
1341           (setq group (car (nth 2 entry))))
1342       (setq group entry))
1343     (when (and (stringp entry)
1344                oldlevel
1345                (< oldlevel gnus-level-zombie))
1346       (setq entry (gnus-group-entry entry)))
1347     (if (and (not oldlevel)
1348              (consp entry))
1349         (setq oldlevel (gnus-info-level (nth 2 entry)))
1350       (setq oldlevel (or oldlevel gnus-level-killed)))
1351     (when (stringp previous)
1352       (setq previous (gnus-group-entry previous)))
1353
1354     (if (and (>= oldlevel gnus-level-zombie)
1355              (gnus-group-entry group))
1356         ;; We are trying to subscribe a group that is already
1357         ;; subscribed.
1358         ()                              ; Do nothing.
1359
1360       (unless (gnus-ephemeral-group-p group)
1361         (gnus-dribble-enter
1362          (format "(gnus-group-change-level %S %S %S %S %S)"
1363                  group level oldlevel (car (nth 2 previous)) fromkilled)))
1364
1365       ;; Then we remove the newgroup from any old structures, if needed.
1366       ;; If the group was killed, we remove it from the killed or zombie
1367       ;; list.  If not, and it is in fact going to be killed, we remove
1368       ;; it from the newsrc hash table and assoc.
1369       (cond
1370        ((>= oldlevel gnus-level-zombie)
1371         ;; oldlevel could be wrong.
1372         (setq gnus-zombie-list (delete group gnus-zombie-list))
1373         (setq gnus-killed-list (delete group gnus-killed-list)))
1374        (t
1375         (when (and (>= level gnus-level-zombie)
1376                    entry)
1377           (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
1378           (when (nth 3 entry)
1379             (setcdr (gnus-group-entry (car (nth 3 entry)))
1380                     (cdr entry)))
1381           (setcdr (cdr entry) (cdddr entry)))))
1382
1383       ;; Finally we enter (if needed) the list where it is supposed to
1384       ;; go, and change the subscription level.  If it is to be killed,
1385       ;; we enter it into the killed or zombie list.
1386       (cond
1387        ((>= level gnus-level-zombie)
1388         ;; Remove from the hash table.
1389         (gnus-sethash group nil gnus-newsrc-hashtb)
1390         ;; We do not enter foreign groups into the list of dead
1391         ;; groups.
1392         (unless (gnus-group-foreign-p group)
1393           (if (= level gnus-level-zombie)
1394               (push group gnus-zombie-list)
1395             (if (= oldlevel gnus-level-killed)
1396                 ;; Remove from active hashtb.
1397                 (unintern group gnus-active-hashtb)
1398               ;; Don't add it into killed-list if it was killed.
1399               (push group gnus-killed-list)))))
1400        (t
1401         ;; If the list is to be entered into the newsrc assoc, and
1402         ;; it was killed, we have to create an entry in the newsrc
1403         ;; hashtb format and fix the pointers in the newsrc assoc.
1404         (if (< oldlevel gnus-level-zombie)
1405             ;; It was alive, and it is going to stay alive, so we
1406             ;; just change the level and don't change any pointers or
1407             ;; hash table entries.
1408             (setcar (cdaddr entry) level)
1409           (if (listp entry)
1410               (setq info (cdr entry)
1411                     num (car entry))
1412             (setq active (gnus-active group))
1413             (setq num
1414                   (if active (- (1+ (cdr active)) (car active)) t))
1415             ;; Shorten the select method if possible, if we need to
1416             ;; store it at all (native groups).
1417             (let ((method (gnus-method-simplify
1418                            (or gnus-override-subscribe-method
1419                                (gnus-group-method group)))))
1420               (if method
1421                   (setq info (list group level nil nil method))
1422                 (setq info (list group level nil)))))
1423           (unless previous
1424             (setq previous
1425                   (let ((p gnus-newsrc-alist))
1426                     (while (cddr p)
1427                       (setq p (cdr p)))
1428                     p)))
1429           (setq entry (cons info (cddr previous)))
1430           (if (cdr previous)
1431               (progn
1432                 (setcdr (cdr previous) entry)
1433                 (gnus-sethash group (cons num (cdr previous))
1434                               gnus-newsrc-hashtb))
1435             (setcdr previous entry)
1436             (gnus-sethash group (cons num previous)
1437                           gnus-newsrc-hashtb))
1438           (when (cdr entry)
1439             (setcdr (gnus-group-entry (caadr entry)) entry))
1440           (gnus-dribble-enter
1441            (format
1442             "(gnus-group-set-info '%S)" info)))))
1443       (when gnus-group-change-level-function
1444         (funcall gnus-group-change-level-function
1445                  group level oldlevel previous)))))
1446
1447 (defun gnus-kill-newsgroup (newsgroup)
1448   "Obsolete function.  Kills a newsgroup."
1449   (gnus-group-change-level
1450    (gnus-group-entry newsgroup) gnus-level-killed))
1451
1452 (defun gnus-check-bogus-newsgroups (&optional confirm)
1453   "Remove bogus newsgroups.
1454 If CONFIRM is non-nil, the user has to confirm the deletion of every
1455 newsgroup."
1456   (let ((newsrc (cdr gnus-newsrc-alist))
1457         bogus group entry info)
1458     (gnus-message 5 "Checking bogus newsgroups...")
1459     (unless (gnus-read-active-file-p)
1460       (gnus-read-active-file t))
1461     (when (gnus-read-active-file-p)
1462       ;; Find all bogus newsgroup that are subscribed.
1463       (while newsrc
1464         (setq info (pop newsrc)
1465               group (gnus-info-group info))
1466         (unless (or (gnus-active group) ; Active
1467                     (and (gnus-info-method info)
1468                          (not (gnus-secondary-method-p
1469                                (gnus-info-method info))))) ; Foreign
1470           ;; Found a bogus newsgroup.
1471           (push group bogus)))
1472       (if confirm
1473           (map-y-or-n-p
1474            "Remove bogus group %s? "
1475            (lambda (group)
1476              ;; Remove all bogus subscribed groups by first killing them, and
1477              ;; then removing them from the list of killed groups.
1478              (when (setq entry (gnus-group-entry group))
1479                (gnus-group-change-level entry gnus-level-killed)
1480                (setq gnus-killed-list (delete group gnus-killed-list))))
1481            bogus '("group" "groups" "remove"))
1482         (while (setq group (pop bogus))
1483           ;; Remove all bogus subscribed groups by first killing them, and
1484           ;; then removing them from the list of killed groups.
1485           (when (setq entry (gnus-group-entry group))
1486             (gnus-group-change-level entry gnus-level-killed)
1487             (setq gnus-killed-list (delete group gnus-killed-list)))))
1488       ;; Then we remove all bogus groups from the list of killed and
1489       ;; zombie groups.  They are removed without confirmation.
1490       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
1491             killed)
1492         (while dead-lists
1493           (setq killed (symbol-value (car dead-lists)))
1494           (while killed
1495             (unless (gnus-active (setq group (pop killed)))
1496               ;; The group is bogus.
1497               ;; !!!Slow as hell.
1498               (set (car dead-lists)
1499                    (delete group (symbol-value (car dead-lists))))))
1500           (setq dead-lists (cdr dead-lists))))
1501       (gnus-run-hooks 'gnus-check-bogus-groups-hook)
1502       (gnus-message 5 "Checking bogus newsgroups...done"))))
1503
1504 (defun gnus-check-duplicate-killed-groups ()
1505   "Remove duplicates from the list of killed groups."
1506   (interactive)
1507   (let ((killed gnus-killed-list))
1508     (while killed
1509       (gnus-message 9 "%d" (length killed))
1510       (setcdr killed (delete (car killed) (cdr killed)))
1511       (setq killed (cdr killed)))))
1512
1513 ;; We want to inline a function from gnus-cache, so we cheat here:
1514 (defvar gnus-cache-active-hashtb)
1515 (eval-when-compile
1516   (defun gnus-cache-possibly-alter-active (group active)
1517     "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
1518     (when gnus-cache-active-hashtb
1519       (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
1520         (when cache-active
1521           (when (< (car cache-active) (car active))
1522             (setcar active (car cache-active)))
1523           (when (> (cdr cache-active) (cdr active))
1524             (setcdr active (cdr cache-active))))))))
1525
1526 (defun gnus-activate-group (group &optional scan dont-check method)
1527   "Check whether a group has been activated or not.
1528 If SCAN, request a scan of that group as well."
1529   (let ((method (or method (inline (gnus-find-method-for-group group))))
1530         active)
1531     (and (inline (gnus-check-server method))
1532          ;; We escape all bugs and quit here to make it possible to
1533          ;; continue if a group is so out-there that it reports bugs
1534          ;; and stuff.
1535          (progn
1536            (and scan
1537                 (gnus-check-backend-function 'request-scan (car method))
1538                 (gnus-request-scan group method))
1539            t)
1540          (if (or debug-on-error debug-on-quit)
1541              (inline (gnus-request-group group dont-check method))
1542            (condition-case nil
1543                (inline (gnus-request-group group dont-check method))
1544              ;;(error nil)
1545              (quit
1546               (message "Quit activating %s" group)
1547               nil)))
1548          (unless dont-check
1549            (setq active (gnus-parse-active))
1550            ;; If there are no articles in the group, the GROUP
1551            ;; command may have responded with the `(0 . 0)'.  We
1552            ;; ignore this if we already have an active entry
1553            ;; for the group.
1554            (if (and (zerop (or (car active) 0))
1555                     (zerop (or (cdr active) 0))
1556                     (gnus-active group))
1557                (gnus-active group)
1558
1559              ;; If a cache is present, we may have to alter the active info.
1560              (when gnus-use-cache
1561                (inline (gnus-cache-possibly-alter-active
1562                         group active)))
1563
1564              ;; If the agent is enabled, we may have to alter the active info.
1565              (when gnus-agent
1566                (gnus-agent-possibly-alter-active group active))
1567
1568              (gnus-set-active group active)
1569              ;; Return the new active info.
1570              active)))))
1571
1572 (defun gnus-get-unread-articles-in-group (info active &optional update)
1573   (when (and info active)
1574     ;; Allow the backend to update the info in the group.
1575     (when (and update
1576                (gnus-request-update-info
1577                 info (inline (gnus-find-method-for-group
1578                               (gnus-info-group info)))))
1579       (gnus-activate-group (gnus-info-group info) nil t))
1580
1581     (let* ((range (gnus-info-read info))
1582            (num 0))
1583
1584       ;; These checks are present in gnus-activate-group but skipped
1585       ;; due to setting dont-check in the preceeding call.
1586
1587       ;; If a cache is present, we may have to alter the active info.
1588       (when (and gnus-use-cache info)
1589         (inline (gnus-cache-possibly-alter-active
1590                  (gnus-info-group info) active)))
1591
1592       ;; If the agent is enabled, we may have to alter the active info.
1593       (when (and gnus-agent info)
1594         (gnus-agent-possibly-alter-active (gnus-info-group info) active info))
1595
1596       ;; Modify the list of read articles according to what articles
1597       ;; are available; then tally the unread articles and add the
1598       ;; number to the group hash table entry.
1599       (cond
1600        ((zerop (cdr active))
1601         (setq num 0))
1602        ((not range)
1603         (setq num (- (1+ (cdr active)) (car active))))
1604        ((not (listp (cdr range)))
1605         ;; Fix a single (num . num) range according to the
1606         ;; active hash table.
1607         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
1608         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
1609         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
1610         ;; Compute number of unread articles.
1611         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
1612        (t
1613         ;; The read list is a list of ranges.  Fix them according to
1614         ;; the active hash table.
1615         ;; First peel off any elements that are below the lower
1616         ;; active limit.
1617         (while (and (cdr range)
1618                     (>= (car active)
1619                         (or (and (atom (cadr range)) (cadr range))
1620                             (caadr range))))
1621           (if (numberp (car range))
1622               (setcar range
1623                       (cons (car range)
1624                             (or (and (numberp (cadr range))
1625                                      (cadr range))
1626                                 (cdadr range))))
1627             (setcdr (car range)
1628                     (or (and (numberp (nth 1 range)) (nth 1 range))
1629                         (cdadr range))))
1630           (setcdr range (cddr range)))
1631         ;; Adjust the first element to be the same as the lower limit.
1632         (when (and (not (atom (car range)))
1633                    (< (cdar range) (car active)))
1634           (setcdr (car range) (1- (car active))))
1635         ;; Then we want to peel off any elements that are higher
1636         ;; than the upper active limit.
1637         (let ((srange range))
1638           ;; Go past all valid elements.
1639           (while (and (cdr srange)
1640                       (<= (or (and (atom (cadr srange))
1641                                    (cadr srange))
1642                               (caadr srange))
1643                           (cdr active)))
1644             (setq srange (cdr srange)))
1645           (when (cdr srange)
1646             ;; Nuke all remaining invalid elements.
1647             (setcdr srange nil))
1648
1649           ;; Adjust the final element.
1650           (when (and (not (atom (car srange)))
1651                      (> (cdar srange) (cdr active)))
1652             (setcdr (car srange) (cdr active))))
1653         ;; Compute the number of unread articles.
1654         (while range
1655           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
1656                                       (cdar range)))
1657                               (or (and (atom (car range)) (car range))
1658                                   (caar range)))))
1659           (setq range (cdr range)))
1660         (setq num (max 0 (- (cdr active) num)))))
1661       ;; Set the number of unread articles.
1662       (when (and info
1663                  (gnus-group-entry (gnus-info-group info)))
1664         (setcar (gnus-group-entry (gnus-info-group info)) num))
1665       num)))
1666
1667 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
1668 ;; and compute how many unread articles there are in each group.
1669 (defun gnus-get-unread-articles (&optional level)
1670   (setq gnus-server-method-cache nil)
1671   (let* ((newsrc (cdr gnus-newsrc-alist))
1672          (alevel (or level gnus-activate-level (1+ gnus-level-subscribed)))
1673          (foreign-level
1674           (min
1675            (cond ((and gnus-activate-foreign-newsgroups
1676                        (not (numberp gnus-activate-foreign-newsgroups)))
1677                   (1+ gnus-level-subscribed))
1678                  ((numberp gnus-activate-foreign-newsgroups)
1679                   gnus-activate-foreign-newsgroups)
1680                  (t 0))
1681            alevel))
1682          (methods-cache nil)
1683          (type-cache nil)
1684          scanned-methods info group active method retrieve-groups cmethod
1685          method-type)
1686     (gnus-message 6 "Checking new news...")
1687
1688     (while newsrc
1689       (setq active (gnus-active (setq group (gnus-info-group
1690                                              (setq info (pop newsrc))))))
1691
1692       ;; Check newsgroups.  If the user doesn't want to check them, or
1693       ;; they can't be checked (for instance, if the news server can't
1694       ;; be reached) we just set the number of unread articles in this
1695       ;; newsgroup to t.  This means that Gnus thinks that there are
1696       ;; unread articles, but it has no idea how many.
1697
1698       ;; To be more explicit:
1699       ;; >0 for an active group with messages
1700       ;; 0 for an active group with no unread messages
1701       ;; nil for non-foreign groups that the user has requested not be checked
1702       ;; t for unchecked foreign groups or bogus groups, or groups that can't
1703       ;;   be checked, for one reason or other.
1704       (when (setq method (gnus-info-method info))
1705         (if (setq cmethod (assoc method methods-cache))
1706             (setq method (cdr cmethod))
1707           (setq cmethod (inline (gnus-server-get-method nil method)))
1708           (push (cons method cmethod) methods-cache)
1709           (setq method cmethod)))
1710       (when (and method
1711                  (not (setq method-type (cdr (assoc method type-cache)))))
1712         (setq method-type
1713               (cond
1714                ((gnus-secondary-method-p method)
1715                 'secondary)
1716                ((inline (gnus-server-equal gnus-select-method method))
1717                 'primary)
1718                (t
1719                 'foreign)))
1720         (push (cons method method-type) type-cache))
1721
1722       (cond ((and method (eq method-type 'foreign))
1723              ;; These groups are foreign.  Check the level.
1724              (if (<= (gnus-info-level info) foreign-level)
1725                  (when (setq active (gnus-activate-group group 'scan))
1726                    ;; Let the Gnus agent save the active file.
1727                    (when (and gnus-agent active (gnus-online method))
1728                      (gnus-agent-save-group-info
1729                       method (gnus-group-real-name group) active))
1730                    (unless (inline (gnus-virtual-group-p group))
1731                      (inline (gnus-close-group group)))
1732                    (when (fboundp (intern (concat (symbol-name (car method))
1733                                                   "-request-update-info")))
1734                      (inline (gnus-request-update-info info method))))
1735                (if (and level
1736                         ;; If `active' is nil that means the group has
1737                         ;; never been read, the group should be marked
1738                         ;; as having never been checked (see below).
1739                         active
1740                         (> (gnus-info-level info) level))
1741                    ;; Don't check groups of which levels are higher
1742                    ;; than the one that a user specified.
1743                    (setq active 'ignore))))
1744             ;; These groups are native or secondary.
1745             ((> (gnus-info-level info) alevel)
1746              ;; We don't want these groups.
1747              (setq active 'ignore))
1748             ;; Activate groups.
1749             ((not gnus-read-active-file)
1750              (if (gnus-check-backend-function 'retrieve-groups group)
1751                  ;; if server support gnus-retrieve-groups we push
1752                  ;; the group onto retrievegroups for later checking
1753                  (if (assoc method retrieve-groups)
1754                      (setcdr (assoc method retrieve-groups)
1755                              (cons group (cdr (assoc method retrieve-groups))))
1756                    (push (list method group) retrieve-groups))
1757                ;; hack: `nnmail-get-new-mail' changes the mail-source depending
1758                ;; on the group, so we must perform a scan for every group
1759                ;; if the users has any directory mail sources.
1760                ;; hack: if `nnmail-scan-directory-mail-source-once' is non-nil,
1761                ;; for it scan all spool files even when the groups are
1762                ;; not required.
1763                (if (and
1764                     (or nnmail-scan-directory-mail-source-once
1765                         (null (assq 'directory mail-sources)))
1766                     (member method scanned-methods))
1767                    (setq active (gnus-activate-group group))
1768                  (setq active (gnus-activate-group group 'scan))
1769                  (push method scanned-methods))
1770                (when active
1771                  (gnus-close-group group)))))
1772
1773       ;; Get the number of unread articles in the group.
1774       (cond
1775        ((eq active 'ignore)
1776         ;; Don't do anything.
1777         )
1778        (active
1779         (inline (gnus-get-unread-articles-in-group info active t)))
1780        (t
1781         ;; The group couldn't be reached, so we nix out the number of
1782         ;; unread articles and stuff.
1783         (gnus-set-active group nil)
1784         (let ((tmp (gnus-group-entry group)))
1785           (when tmp
1786             (setcar tmp t))))))
1787
1788     ;; iterate through groups on methods which support gnus-retrieve-groups
1789     ;; and fetch a partial active file and use it to find new news.
1790     (dolist (rg retrieve-groups)
1791       (let ((method (or (car rg) gnus-select-method))
1792             (groups (cdr rg)))
1793         (when (gnus-check-server method)
1794           ;; Request that the backend scan its incoming messages.
1795           (when (gnus-check-backend-function 'request-scan (car method))
1796             (gnus-request-scan nil method))
1797           (gnus-read-active-file-2
1798            (mapcar (lambda (group) (gnus-group-real-name group)) groups)
1799            method)
1800           (dolist (group groups)
1801             (cond
1802              ((setq active (gnus-active (gnus-info-group
1803                                          (setq info (gnus-get-info group)))))
1804               (inline (gnus-get-unread-articles-in-group info active t)))
1805              (t
1806               ;; The group couldn't be reached, so we nix out the number of
1807               ;; unread articles and stuff.
1808               (gnus-set-active group nil)
1809               (setcar (gnus-group-entry group) t)))))))
1810
1811     (gnus-message 6 "Checking new news...done")))
1812
1813 ;; Create a hash table out of the newsrc alist.  The `car's of the
1814 ;; alist elements are used as keys.
1815 (defun gnus-make-hashtable-from-newsrc-alist ()
1816   (let ((alist gnus-newsrc-alist)
1817         (ohashtb gnus-newsrc-hashtb)
1818         prev info method rest methods)
1819     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
1820     (setq alist
1821           (setq prev (setq gnus-newsrc-alist
1822                            (if (equal (caar gnus-newsrc-alist)
1823                                       "dummy.group")
1824                                gnus-newsrc-alist
1825                              (cons (list "dummy.group" 0 nil) alist)))))
1826     (while alist
1827       (setq info (car alist))
1828       ;; Make the same select-methods identical Lisp objects.
1829       (when (setq method (gnus-info-method info))
1830         (if (setq rest (member method methods))
1831             (gnus-info-set-method info (car rest))
1832           (push method methods)))
1833       (gnus-sethash
1834        (car info)
1835        ;; Preserve number of unread articles in groups.
1836        (cons (and ohashtb (car (gnus-gethash (car info) ohashtb)))
1837              prev)
1838        gnus-newsrc-hashtb)
1839       (setq prev alist
1840             alist (cdr alist)))
1841     ;; Make the same select-methods in `gnus-server-alist' identical
1842     ;; as well.
1843     (while methods
1844       (setq method (pop methods))
1845       (when (setq rest (rassoc method gnus-server-alist))
1846         (setcdr rest method)))))
1847
1848 (defun gnus-make-hashtable-from-killed ()
1849   "Create a hash table from the killed and zombie lists."
1850   (let ((lists '(gnus-killed-list gnus-zombie-list))
1851         list)
1852     (setq gnus-killed-hashtb
1853           (gnus-make-hashtable
1854            (+ (length gnus-killed-list) (length gnus-zombie-list))))
1855     (while lists
1856       (setq list (symbol-value (pop lists)))
1857       (while list
1858         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
1859
1860 (defun gnus-parse-active ()
1861   "Parse active info in the nntp server buffer."
1862   (save-excursion
1863     (set-buffer nntp-server-buffer)
1864     (goto-char (point-min))
1865     ;; Parse the result we got from `gnus-request-group'.
1866     (when (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
1867       (goto-char (match-beginning 1))
1868       (cons (read (current-buffer))
1869             (read (current-buffer))))))
1870
1871 (defun gnus-make-articles-unread (group articles)
1872   "Mark ARTICLES in GROUP as unread."
1873   (let* ((info (nth 2 (or (gnus-group-entry group)
1874                           (gnus-group-entry
1875                            (gnus-group-real-name group)))))
1876          (ranges (gnus-info-read info))
1877          news article)
1878     (while articles
1879       (when (gnus-member-of-range
1880              (setq article (pop articles)) ranges)
1881         (push article news)))
1882     (when news
1883       ;; Enter this list into the group info.
1884       (gnus-info-set-read
1885        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
1886
1887       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1888       (gnus-get-unread-articles-in-group info (gnus-active group))
1889
1890       ;; Insert the change into the group buffer and the dribble file.
1891       (gnus-group-update-group group t))))
1892
1893 (defun gnus-make-ascending-articles-unread (group articles)
1894   "Mark ascending ARTICLES in GROUP as unread."
1895   (let* ((entry (or (gnus-group-entry group)
1896                     (gnus-group-entry (gnus-group-real-name group))))
1897          (info (nth 2 entry))
1898          (ranges (gnus-info-read info))
1899          (r ranges)
1900          modified)
1901
1902     (while articles
1903       (let ((article (pop articles))) ; get the next article to remove from ranges
1904         (while (let ((range (car ranges))) ; note the current range
1905                  (if (atom range)       ; single value range
1906                      (cond ((not range)
1907                             ;; the articles extend past the end of the ranges
1908                             ;; OK - I'm done
1909                             (setq articles nil))
1910                            ((< range article)
1911                             ;; this range preceeds the article. Leave the range unmodified.
1912                             (pop ranges)
1913                             ranges)
1914                            ((= range article)
1915                             ;; this range exactly matches the article; REMOVE THE RANGE.
1916                             ;; NOTE: When the range being removed is the last range, the list is corrupted by inserting null at its end.
1917                             (setcar ranges (cadr ranges))
1918                             (setcdr ranges (cddr ranges))
1919                             (setq modified (if (car ranges) t 'remove-null))
1920                             nil))
1921                    (let ((min (car range))
1922                          (max (cdr range)))
1923                      ;; I have a min/max range to consider
1924                      (cond ((> min max) ; invalid range introduced by splitter
1925                             (setcar ranges (cadr ranges))
1926                             (setcdr ranges (cddr ranges))
1927                             (setq modified (if (car ranges) t 'remove-null))
1928                             ranges)
1929                            ((= min max)
1930                             ;; replace min/max range with a single-value range
1931                             (setcar ranges min)
1932                             ranges)
1933                            ((< max article)
1934                             ;; this range preceeds the article. Leave the range unmodified.
1935                             (pop ranges)
1936                             ranges)
1937                            ((< article min)
1938                             ;; this article preceeds the range.  Return null to move to the
1939                             ;; next article
1940                             nil)
1941                            (t
1942                             ;; this article splits the range into two parts
1943                             (setcdr ranges (cons (cons (1+ article) max) (cdr ranges)))
1944                             (setcdr range (1- article))
1945                             (setq modified t)
1946                             ranges))))))))
1947
1948     (when modified
1949       (when (eq modified 'remove-null)
1950         (setq r (delq nil r)))
1951       ;; Enter this list into the group info.
1952       (gnus-info-set-read info r)
1953
1954       ;; Set the number of unread articles in gnus-newsrc-hashtb.
1955       (gnus-get-unread-articles-in-group info (gnus-active group))
1956
1957       ;; Insert the change into the group buffer and the dribble file.
1958       (gnus-group-update-group group t))))
1959
1960 ;; Enter all dead groups into the hashtb.
1961 (defun gnus-update-active-hashtb-from-killed ()
1962   (let ((hashtb (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
1963         (lists (list gnus-killed-list gnus-zombie-list))
1964         killed)
1965     (while lists
1966       (setq killed (car lists))
1967       (while killed
1968         (gnus-sethash (mm-string-as-unibyte (car killed)) nil hashtb)
1969         (setq killed (cdr killed)))
1970       (setq lists (cdr lists)))))
1971
1972 (defun gnus-get-killed-groups ()
1973   "Go through the active hashtb and mark all unknown groups as killed."
1974   ;; First make sure active file has been read.
1975   (unless (gnus-read-active-file-p)
1976     (let ((gnus-read-active-file t))
1977       (gnus-read-active-file)))
1978   (unless gnus-killed-hashtb
1979     (gnus-make-hashtable-from-killed))
1980   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
1981   (mapatoms
1982    (lambda (sym)
1983      (let ((groups 0)
1984            (group (symbol-name sym)))
1985        (if (or (null group)
1986                (gnus-gethash group gnus-killed-hashtb)
1987                (gnus-gethash group gnus-newsrc-hashtb))
1988            ()
1989          (let ((do-sub (gnus-matches-options-n group)))
1990            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
1991                ()
1992              (setq groups (1+ groups))
1993              (push group gnus-killed-list)
1994              (gnus-sethash group group gnus-killed-hashtb))))))
1995    gnus-active-hashtb)
1996   (gnus-dribble-touch))
1997
1998 ;; Get the active file(s) from the backend(s).
1999 (defun gnus-read-active-file (&optional force not-native)
2000   (gnus-group-set-mode-line)
2001   (let ((methods
2002          (mapcar
2003           (lambda (m) (if (stringp m) (gnus-server-get-method nil m) m))
2004           (append
2005            (if (and (not not-native)
2006                     (gnus-check-server gnus-select-method))
2007                ;; The native server is available.
2008                (cons gnus-select-method gnus-secondary-select-methods)
2009              ;; The native server is down, so we just do the
2010              ;; secondary ones.
2011              gnus-secondary-select-methods)
2012            ;; Also read from the archive server.
2013            (when (gnus-archive-server-wanted-p)
2014              (list "archive")))))
2015         method)
2016     (setq gnus-have-read-active-file nil)
2017     (save-excursion
2018       (set-buffer nntp-server-buffer)
2019       (while (setq method (pop methods))
2020         ;; Only do each method once, in case the methods appear more
2021         ;; than once in this list.
2022         (unless (member method methods)
2023           (if (or debug-on-error debug-on-quit)
2024               (gnus-read-active-file-1 method force)
2025             (condition-case ()
2026                 (gnus-read-active-file-1 method force)
2027               ;; We catch C-g so that we can continue past servers
2028               ;; that do not respond.
2029               (quit
2030                (message "Quit reading the active file")
2031                nil))))))))
2032
2033 (defun gnus-read-active-file-1 (method force)
2034   (let (where mesg)
2035     (setq where (nth 1 method)
2036           mesg (format "Reading active file%s via %s..."
2037                        (if (and where (not (zerop (length where))))
2038                            (concat " from " where) "")
2039                        (car method)))
2040     (gnus-message 5 mesg)
2041     (when (gnus-check-server method)
2042       ;; Request that the backend scan its incoming messages.
2043       (when (gnus-check-backend-function 'request-scan (car method))
2044         (gnus-request-scan nil method))
2045       (cond
2046        ((and (eq gnus-read-active-file 'some)
2047              (gnus-check-backend-function 'retrieve-groups (car method))
2048              (not force))
2049         (let ((newsrc (cdr gnus-newsrc-alist))
2050               (gmethod (gnus-server-get-method nil method))
2051               groups info)
2052           (while (setq info (pop newsrc))
2053             (when (inline
2054                     (gnus-server-equal
2055                           (inline
2056                             (gnus-find-method-for-group
2057                                   (gnus-info-group info) info))
2058                           gmethod))
2059               (push (gnus-group-real-name (gnus-info-group info))
2060                     groups)))
2061           (gnus-read-active-file-2 groups method)))
2062        ((null method)
2063         t)
2064        (t
2065         (if (not (gnus-request-list method))
2066             (unless (equal method gnus-message-archive-method)
2067               (gnus-error 1 "Cannot read active file from %s server"
2068                           (car method)))
2069           (gnus-message 5 mesg)
2070           (gnus-active-to-gnus-format method gnus-active-hashtb nil t)
2071           ;; We mark this active file as read.
2072           (push method gnus-have-read-active-file)
2073           (gnus-message 5 "%sdone" mesg)))))))
2074
2075 (defun gnus-read-active-file-2 (groups method)
2076   "Read an active file for GROUPS in METHOD using `gnus-retrieve-groups'."
2077   (when groups
2078     (save-excursion
2079       (set-buffer nntp-server-buffer)
2080       (gnus-check-server method)
2081       (let ((list-type (gnus-retrieve-groups groups method)))
2082         (cond ((not list-type)
2083                (gnus-error
2084                 1.2 "Cannot read partial active file from %s server."
2085                 (car method)))
2086               ((eq list-type 'active)
2087                (gnus-active-to-gnus-format method gnus-active-hashtb nil t))
2088               (t
2089                (gnus-groups-to-gnus-format method gnus-active-hashtb t)))))))
2090
2091 ;; Read an active file and place the results in `gnus-active-hashtb'.
2092 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors
2093                                              real-active)
2094   (unless method
2095     (setq method gnus-select-method))
2096   (let ((cur (current-buffer))
2097         (hashtb (or hashtb
2098                     (if (and gnus-active-hashtb
2099                              (not (equal method gnus-select-method)))
2100                         gnus-active-hashtb
2101                       (setq gnus-active-hashtb
2102                             (if (equal method gnus-select-method)
2103                                 (gnus-make-hashtable
2104                                  (count-lines (point-min) (point-max)))
2105                               (gnus-make-hashtable 4096))))))
2106         group max min)
2107     ;; Delete unnecessary lines.
2108     (goto-char (point-min))
2109     (cond
2110      ((string= gnus-ignored-newsgroups "")
2111       (delete-matching-lines "^to\\."))
2112      (t
2113       (delete-matching-lines (concat "^to\\.\\|" gnus-ignored-newsgroups))))
2114
2115     (goto-char (point-min))
2116     (unless (re-search-forward "[\\\"]" nil t)
2117       ;; Make the group names readable as a lisp expression even if they
2118       ;; contain special characters.
2119       (goto-char (point-max))
2120       (while (re-search-backward "[][';?()#]" nil t)
2121         (insert ?\\)))
2122
2123     ;; Let the Gnus agent save the active file.
2124     (when (and gnus-agent real-active (gnus-online method))
2125       (gnus-agent-save-active method))
2126
2127     ;; If these are groups from a foreign select method, we insert the
2128     ;; group prefix in front of the group names.
2129     (when (not (gnus-server-equal
2130                 (gnus-server-get-method nil method)
2131                 (gnus-server-get-method nil gnus-select-method)))
2132       (let ((prefix (gnus-group-prefixed-name "" method)))
2133         (goto-char (point-min))
2134         (while (and (not (eobp))
2135                     (progn
2136                       (when (= (following-char) ?\")
2137                         (forward-char 1))
2138                       (insert prefix)
2139                       (zerop (forward-line 1)))))))
2140     ;; Store the active file in a hash table.
2141     ;; Use a unibyte buffer in order to make `read' read non-ASCII
2142     ;; group names (which have been encoded) as unibyte strings.
2143     (mm-with-unibyte-buffer
2144       (insert-buffer-substring cur)
2145       (setq cur (current-buffer))
2146       (goto-char (point-min))
2147       (while (not (eobp))
2148         (condition-case ()
2149             (progn
2150               (narrow-to-region (point) (point-at-eol))
2151               ;; group gets set to a symbol interned in the hash table
2152               ;; (what a hack!!) - jwz
2153               (setq group (let ((obarray hashtb)) (read cur)))
2154               ;; ### The extended group name scheme makes
2155               ;; the previous optimization strategy sort of pointless...
2156               (when (stringp group)
2157                 (setq group (intern group hashtb)))
2158               (if (and (numberp (setq max (read cur)))
2159                        (numberp (setq min (read cur)))
2160                        (progn
2161                          (skip-chars-forward " \t")
2162                          (not
2163                           (or (eq (char-after) ?=)
2164                               (eq (char-after) ?x)
2165                               (eq (char-after) ?j)))))
2166                   (progn
2167                     (set group (cons min max))
2168                     ;; if group is moderated, stick in moderation table
2169                     (when (eq (char-after) ?m)
2170                       (unless gnus-moderated-hashtb
2171                         (setq gnus-moderated-hashtb (gnus-make-hashtable)))
2172                       (gnus-sethash (symbol-name group) t
2173                                     gnus-moderated-hashtb)))
2174                 (set group nil)))
2175           (error
2176            (and group
2177                 (symbolp group)
2178                 (set group nil))
2179            (unless ignore-errors
2180              (gnus-message 3 "Warning - invalid active: %s"
2181                            (buffer-substring
2182                             (point-at-bol) (point-at-eol))))))
2183         (widen)
2184         (forward-line 1)))))
2185
2186 (defun gnus-groups-to-gnus-format (method &optional hashtb real-active)
2187   ;; Parse a "groups" active file.
2188   (let ((cur (current-buffer))
2189         (hashtb (or hashtb
2190                     (if (and method gnus-active-hashtb)
2191                         gnus-active-hashtb
2192                       (setq gnus-active-hashtb
2193                             (gnus-make-hashtable
2194                              (count-lines (point-min) (point-max)))))))
2195         (prefix (and method
2196                      (not (gnus-server-equal
2197                            (gnus-server-get-method nil method)
2198                            (gnus-server-get-method nil gnus-select-method)))
2199                      (gnus-group-prefixed-name "" method))))
2200
2201     ;; Let the Gnus agent save the active file.
2202     (if (and gnus-agent
2203              real-active
2204              (gnus-online method)
2205              (gnus-agent-method-p method))
2206         (progn
2207           (gnus-agent-save-active method)
2208           (gnus-active-to-gnus-format method hashtb nil real-active))
2209
2210       (goto-char (point-min))
2211       ;; We split this into to separate loops, one with the prefix
2212       ;; and one without to speed the reading up somewhat.
2213       (if prefix
2214           (let (min max opoint group)
2215             (while (not (eobp))
2216               (condition-case ()
2217                   (progn
2218                     (read cur) (read cur)
2219                     (setq min (read cur)
2220                           max (read cur)
2221                           opoint (point))
2222                     (skip-chars-forward " \t")
2223                     (insert prefix)
2224                     (goto-char opoint)
2225                     (set (let ((obarray hashtb)) (read cur))
2226                          (cons min max)))
2227                 (error (and group (symbolp group) (set group nil))))
2228               (forward-line 1)))
2229         (let (min max group)
2230           (while (not (eobp))
2231             (condition-case ()
2232                 (when (eq (char-after) ?2)
2233                   (read cur) (read cur)
2234                   (setq min (read cur)
2235                         max (read cur))
2236                   (set (setq group (let ((obarray hashtb)) (read cur)))
2237                        (cons min max)))
2238               (error (and group (symbolp group) (set group nil))))
2239             (forward-line 1)))))))
2240
2241 (defun gnus-read-newsrc-file (&optional force)
2242   "Read startup file.
2243 If FORCE is non-nil, the .newsrc file is read."
2244   ;; Reset variables that might be defined in the .newsrc.eld file.
2245   (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
2246     (while variables
2247       (set (car variables) nil)
2248       (setq variables (cdr variables))))
2249   (let* ((newsrc-file gnus-current-startup-file)
2250          (quick-file (concat newsrc-file ".el")))
2251     (save-excursion
2252       ;; We always load the .newsrc.eld file.  If always contains
2253       ;; much information that can not be gotten from the .newsrc
2254       ;; file (ticked articles, killed groups, foreign methods, etc.)
2255       (gnus-read-newsrc-el-file quick-file)
2256
2257       (when (and gnus-read-newsrc-file
2258                  (file-exists-p gnus-current-startup-file)
2259                  (or force
2260                      (and (file-newer-than-file-p newsrc-file quick-file)
2261                           (file-newer-than-file-p newsrc-file
2262                                                   (concat quick-file "d")))
2263                      (not gnus-newsrc-alist)))
2264         ;; We read the .newsrc file.  Note that if there if a
2265         ;; .newsrc.eld file exists, it has already been read, and
2266         ;; the `gnus-newsrc-hashtb' has been created.  While reading
2267         ;; the .newsrc file, Gnus will only use the information it
2268         ;; can find there for changing the data already read -
2269         ;; i. e., reading the .newsrc file will not trash the data
2270         ;; already read (except for read articles).
2271         (save-excursion
2272           (gnus-message 5 "Reading %s..." newsrc-file)
2273           (set-buffer (nnheader-find-file-noselect newsrc-file))
2274           (buffer-disable-undo)
2275           (gnus-newsrc-to-gnus-format)
2276           (kill-buffer (current-buffer))
2277           (gnus-message 5 "Reading %s...done" newsrc-file)))
2278
2279       ;; Convert old to new.
2280       (gnus-convert-old-newsrc))))
2281
2282 (defun gnus-convert-old-newsrc ()
2283   "Convert old newsrc formats into the current format, if needed."
2284   (let ((fcv (and gnus-newsrc-file-version
2285                   (gnus-continuum-version gnus-newsrc-file-version)))
2286         (gcv (gnus-continuum-version)))
2287     (when fcv
2288       ;; A newsrc file was loaded.
2289       (let (prompt-displayed
2290             (converters
2291              (sort
2292               (mapcar (lambda (date-func)
2293                         (cons (gnus-continuum-version (car date-func))
2294                               date-func))
2295                       ;; This is a list of converters that must be run
2296                       ;; to bring the newsrc file up to the current
2297                       ;; version.  If you create an incompatibility
2298                       ;; with older versions, you should create an
2299                       ;; entry here.  The entry should consist of the
2300                       ;; current gnus version (hardcoded so that it
2301                       ;; doesn't change with each release) and the
2302                       ;; function that must be applied to convert the
2303                       ;; previous version into the current version.
2304                       '(("September Gnus v0.1" nil
2305                          gnus-convert-old-ticks)
2306                         ("Oort Gnus v0.08"     "legacy-gnus-agent"
2307                          gnus-agent-convert-to-compressed-agentview)
2308                         ("Gnus v5.10.7"        "legacy-gnus-agent"
2309                          gnus-agent-unlist-expire-days)
2310                         ("Gnus v5.10.7"        "legacy-gnus-agent"
2311                          gnus-agent-unhook-expire-days)))
2312               #'car-less-than-car)))
2313         ;; Skip converters older than the file version
2314         (while (and converters (>= fcv (caar converters)))
2315           (pop converters))
2316
2317         ;; Perform converters to bring older version up to date.
2318         (when (and converters (< fcv (caar converters)))
2319           (while (and converters (< fcv (caar converters))
2320                       (<= (caar converters) gcv))
2321             (let* ((converter-spec  (pop converters))
2322                    (convert-to      (nth 1 converter-spec))
2323                    (load-from       (nth 2 converter-spec))
2324                    (func            (nth 3 converter-spec)))
2325               (when (and load-from
2326                          (not (fboundp func)))
2327                 (load load-from t))
2328               (or prompt-displayed
2329                   (not (gnus-convert-converter-needs-prompt func))
2330                   (while (let (c
2331                                (cursor-in-echo-area t)
2332                                (echo-keystrokes 0))
2333                            (message "Convert gnus from version '%s' to '%s'? (n/y/?)"
2334                                     gnus-newsrc-file-version gnus-version)
2335                            (setq c (read-char-exclusive))
2336
2337                            (cond ((or (eq c ?n) (eq c ?N))
2338                                   (error "Can not start gnus without converting"))
2339                                  ((or (eq c ?y) (eq c ?Y))
2340                                   (setq prompt-displayed t)
2341                                   nil)
2342                                  ((eq c ?\?)
2343                                   (message "This conversion is irreversible. \
2344  To be safe, you should backup your files before proceeding.")
2345                                   (sit-for 5)
2346                                   t)
2347                                  (t
2348                                   (gnus-message 3 "Ignoring unexpected input")
2349                                   (sit-for 3)
2350                                   t)))))
2351
2352               (funcall func convert-to)))
2353           (gnus-dribble-enter
2354            (format ";Converted gnus from version '%s' to '%s'."
2355                    gnus-newsrc-file-version gnus-version)))))))
2356
2357 (defun gnus-convert-mark-converter-prompt (converter no-prompt)
2358   "Indicate whether CONVERTER requires gnus-convert-old-newsrc to
2359   display the conversion prompt.  NO-PROMPT may be nil (prompt),
2360   t (no prompt), or any form that can be called as a function.
2361   The form should return either t or nil."
2362   (put converter 'gnus-convert-no-prompt no-prompt))
2363
2364 (defun gnus-convert-converter-needs-prompt (converter)
2365   (let ((no-prompt (get converter 'gnus-convert-no-prompt)))
2366     (not (if (memq no-prompt '(t nil))
2367              no-prompt
2368            (funcall no-prompt)))))
2369
2370 (defun gnus-convert-old-ticks (converting-to)
2371   (let ((newsrc (cdr gnus-newsrc-alist))
2372         marks info dormant ticked)
2373     (while (setq info (pop newsrc))
2374       (when (setq marks (gnus-info-marks info))
2375         (setq dormant (cdr (assq 'dormant marks))
2376               ticked (cdr (assq 'tick marks)))
2377         (when (or dormant ticked)
2378           (gnus-info-set-read
2379            info
2380            (gnus-add-to-range
2381             (gnus-info-read info)
2382             (nconc (gnus-uncompress-range dormant)
2383                    (gnus-uncompress-range ticked)))))))))
2384
2385 (defun gnus-load (file)
2386   "Load FILE, but in such a way that read errors can be reported."
2387   (with-temp-buffer
2388     (insert-file-contents file)
2389     (while (not (eobp))
2390       (condition-case type
2391           (let ((form (read (current-buffer))))
2392             (eval form))
2393         (error
2394          (unless (eq (car type) 'end-of-file)
2395            (let ((errmsg (format "Error in %s line %d" file
2396                                  (count-lines (point-min) (point)))))
2397              (ding)
2398              (unless (gnus-yes-or-no-p (concat errmsg "; continue? "))
2399                (error "%s" errmsg)))))))))
2400
2401 (defun gnus-read-newsrc-el-file (file)
2402   (let ((ding-file (concat file "d")))
2403     (when (file-exists-p ding-file)
2404       ;; We always, always read the .eld file.
2405       (gnus-message 5 "Reading %s..." ding-file)
2406       (let (gnus-newsrc-assoc)
2407         (gnus-load ding-file)
2408         ;; Older versions of `gnus-format-specs' are no longer valid
2409         ;; in Oort Gnus 0.01.
2410         (let ((version
2411                (and gnus-newsrc-file-version
2412                     (gnus-continuum-version gnus-newsrc-file-version))))
2413           (when (or (not version)
2414                     (< version 5.090009))
2415             (setq gnus-format-specs gnus-default-format-specs)))
2416         (when gnus-newsrc-assoc
2417           (setq gnus-newsrc-alist gnus-newsrc-assoc))))
2418     (dolist (elem gnus-newsrc-alist)
2419       (setcar elem (mm-string-as-unibyte (car elem))))
2420     (gnus-make-hashtable-from-newsrc-alist)
2421     (when (file-newer-than-file-p file ding-file)
2422       ;; Old format quick file
2423       (gnus-message 5 "Reading %s..." file)
2424       ;; The .el file is newer than the .eld file, so we read that one
2425       ;; as well.
2426       (gnus-read-old-newsrc-el-file file)))
2427   (gnus-run-hooks 'gnus-read-newsrc-el-hook))
2428
2429 ;; Parse the old-style quick startup file
2430 (defun gnus-read-old-newsrc-el-file (file)
2431   (let (newsrc killed marked group m info)
2432     (prog1
2433         (let ((gnus-killed-assoc nil)
2434               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
2435           (prog1
2436               (ignore-errors
2437                 (load file t t t))
2438             (setq newsrc gnus-newsrc-assoc
2439                   killed gnus-killed-assoc
2440                   marked gnus-marked-assoc)))
2441       (setq gnus-newsrc-alist nil)
2442       (while (setq group (pop newsrc))
2443         (if (setq info (gnus-get-info (car group)))
2444             (progn
2445               (gnus-info-set-read info (cddr group))
2446               (gnus-info-set-level
2447                info (if (nth 1 group) gnus-level-default-subscribed
2448                       gnus-level-default-unsubscribed))
2449               (push info gnus-newsrc-alist))
2450           (push (setq info
2451                       (list (car group)
2452                             (if (nth 1 group) gnus-level-default-subscribed
2453                               gnus-level-default-unsubscribed)
2454                             (cddr group)))
2455                 gnus-newsrc-alist))
2456         ;; Copy marks into info.
2457         (when (setq m (assoc (car group) marked))
2458           (unless (nthcdr 3 info)
2459             (nconc info (list nil)))
2460           (gnus-info-set-marks
2461            info (list (cons 'tick (gnus-compress-sequence
2462                                    (sort (cdr m) '<) t))))))
2463       (setq newsrc killed)
2464       (while newsrc
2465         (setcar newsrc (caar newsrc))
2466         (setq newsrc (cdr newsrc)))
2467       (setq gnus-killed-list killed))
2468     ;; The .el file version of this variable does not begin with
2469     ;; "options", while the .eld version does, so we just add it if it
2470     ;; isn't there.
2471     (when
2472         gnus-newsrc-options
2473       (when (not (string-match "^ *options" gnus-newsrc-options))
2474         (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
2475       (when (not (string-match "\n$" gnus-newsrc-options))
2476         (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
2477       ;; Finally, if we read some options lines, we parse them.
2478       (unless (string= gnus-newsrc-options "")
2479         (gnus-newsrc-parse-options gnus-newsrc-options)))
2480
2481     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
2482     (gnus-make-hashtable-from-newsrc-alist)))
2483
2484 (defun gnus-make-newsrc-file (file)
2485   "Make server dependent file name by catenating FILE and server host name."
2486   (let* ((file (expand-file-name file nil))
2487          (real-file (concat file "-" (nth 1 gnus-select-method))))
2488     (if (or (file-exists-p real-file)
2489             (file-exists-p (concat real-file ".el"))
2490             (file-exists-p (concat real-file ".eld")))
2491         real-file
2492       file)))
2493
2494 (defun gnus-newsrc-to-gnus-format ()
2495   (setq gnus-newsrc-options "")
2496   (setq gnus-newsrc-options-n nil)
2497
2498   (unless gnus-active-hashtb
2499     (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
2500   (let ((buf (current-buffer))
2501         (already-read (> (length gnus-newsrc-alist) 1))
2502         group subscribed options-symbol newsrc Options-symbol
2503         symbol reads num1)
2504     (goto-char (point-min))
2505     ;; We intern the symbol `options' in the active hashtb so that we
2506     ;; can `eq' against it later.
2507     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
2508     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
2509
2510     (while (not (eobp))
2511       ;; We first read the first word on the line by narrowing and
2512       ;; then reading into `gnus-active-hashtb'.  Most groups will
2513       ;; already exist in that hashtb, so this will save some string
2514       ;; space.
2515       (narrow-to-region
2516        (point)
2517        (progn (skip-chars-forward "^ \t!:\n") (point)))
2518       (goto-char (point-min))
2519       (setq symbol
2520             (and (/= (point-min) (point-max))
2521                  (let ((obarray gnus-active-hashtb)) (read buf))))
2522       (widen)
2523       ;; Now, the symbol we have read is either `options' or a group
2524       ;; name.  If it is an options line, we just add it to a string.
2525       (cond
2526        ((or (eq symbol options-symbol)
2527             (eq symbol Options-symbol))
2528         (setq gnus-newsrc-options
2529               ;; This concating is quite inefficient, but since our
2530               ;; thorough studies show that approx 99.37% of all
2531               ;; .newsrc files only contain a single options line, we
2532               ;; don't give a damn, frankly, my dear.
2533               (concat gnus-newsrc-options
2534                       (buffer-substring
2535                        (point-at-bol)
2536                        ;; Options may continue on the next line.
2537                        (or (and (re-search-forward "^[^ \t]" nil 'move)
2538                                 (point-at-bol))
2539                            (point)))))
2540         (forward-line -1))
2541        (symbol
2542         ;; Group names can be just numbers.
2543         (when (numberp symbol)
2544           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
2545         (unless (boundp symbol)
2546           (set symbol nil))
2547         ;; It was a group name.
2548         (setq subscribed (eq (char-after) ?:)
2549               group (symbol-name symbol)
2550               reads nil)
2551         (if (eolp)
2552             ;; If the line ends here, this is clearly a buggy line, so
2553             ;; we put point a the beginning of line and let the cond
2554             ;; below do the error handling.
2555             (beginning-of-line)
2556           ;; We skip to the beginning of the ranges.
2557           (skip-chars-forward "!: \t"))
2558         ;; We are now at the beginning of the list of read articles.
2559         ;; We read them range by range.
2560         (while
2561             (cond
2562              ((looking-at "[0-9]+")
2563               ;; We narrow and read a number instead of buffer-substring/
2564               ;; string-to-number because it's faster.  narrow/widen is
2565               ;; faster than save-restriction/narrow, and save-restriction
2566               ;; produces a garbage object.
2567               (setq num1 (progn
2568                            (narrow-to-region (match-beginning 0) (match-end 0))
2569                            (read buf)))
2570               (widen)
2571               ;; If the next character is a dash, then this is a range.
2572               (if (eq (char-after) ?-)
2573                   (progn
2574                     ;; We read the upper bound of the range.
2575                     (forward-char 1)
2576                     (if (not (looking-at "[0-9]+"))
2577                         ;; This is a buggy line, by we pretend that
2578                         ;; it's kinda OK.  Perhaps the user should be
2579                         ;; dinged?
2580                         (push num1 reads)
2581                       (push
2582                        (cons num1
2583                              (progn
2584                                (narrow-to-region (match-beginning 0)
2585                                                  (match-end 0))
2586                                (read buf)))
2587                        reads)
2588                       (widen)))
2589                 ;; It was just a simple number, so we add it to the
2590                 ;; list of ranges.
2591                 (push num1 reads))
2592               ;; If the next char in ?\n, then we have reached the end
2593               ;; of the line and return nil.
2594               (not (eq (char-after) ?\n)))
2595              ((eq (char-after) ?\n)
2596               ;; End of line, so we end.
2597               nil)
2598              (t
2599               ;; Not numbers and not eol, so this might be a buggy
2600               ;; line...
2601               (unless (eobp)
2602                 ;; If it was eob instead of ?\n, we allow it.
2603                 ;; The line was buggy.
2604                 (setq group nil)
2605                 (gnus-error 3.1 "Mangled line: %s"
2606                             (buffer-substring (point-at-bol)
2607                                               (point-at-eol))))
2608               nil))
2609           ;; Skip past ", ".  Spaces are invalid in these ranges, but
2610           ;; we allow them, because it's a common mistake to put a
2611           ;; space after the comma.
2612           (skip-chars-forward ", "))
2613
2614         ;; We have already read .newsrc.eld, so we gently update the
2615         ;; data in the hash table with the information we have just
2616         ;; read.
2617         (when group
2618           (let ((info (gnus-get-info group))
2619                 level)
2620             (if info
2621                 ;; There is an entry for this file in the alist.
2622                 (progn
2623                   (gnus-info-set-read info (nreverse reads))
2624                   ;; We update the level very gently.  In fact, we
2625                   ;; only change it if there's been a status change
2626                   ;; from subscribed to unsubscribed, or vice versa.
2627                   (setq level (gnus-info-level info))
2628                   (cond ((and (<= level gnus-level-subscribed)
2629                               (not subscribed))
2630                          (setq level (if reads
2631                                          gnus-level-default-unsubscribed
2632                                        (1+ gnus-level-default-unsubscribed))))
2633                         ((and (> level gnus-level-subscribed) subscribed)
2634                          (setq level gnus-level-default-subscribed)))
2635                   (gnus-info-set-level info level))
2636               ;; This is a new group.
2637               (setq info (list group
2638                                (if subscribed
2639                                    gnus-level-default-subscribed
2640                                  (if reads
2641                                      (1+ gnus-level-subscribed)
2642                                    gnus-level-default-unsubscribed))
2643                                (nreverse reads))))
2644             (push info newsrc)))))
2645       (forward-line 1))
2646
2647     (setq newsrc (nreverse newsrc))
2648
2649     (if (not already-read)
2650         ()
2651       ;; We now have two newsrc lists - `newsrc', which is what we
2652       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
2653       ;; what we've read from .newsrc.eld.  We have to merge these
2654       ;; lists.  We do this by "attaching" any (foreign) groups in the
2655       ;; gnus-newsrc-alist to the (native) group that precedes them.
2656       (let ((rc (cdr gnus-newsrc-alist))
2657             (prev gnus-newsrc-alist)
2658             entry mentry)
2659         (while rc
2660           (or (null (nth 4 (car rc)))   ; It's a native group.
2661               (assoc (caar rc) newsrc)  ; It's already in the alist.
2662               (if (setq entry (assoc (caar prev) newsrc))
2663                   (setcdr (setq mentry (memq entry newsrc))
2664                           (cons (car rc) (cdr mentry)))
2665                 (push (car rc) newsrc)))
2666           (setq prev rc
2667                 rc (cdr rc)))))
2668
2669     (setq gnus-newsrc-alist newsrc)
2670     ;; We make the newsrc hashtb.
2671     (gnus-make-hashtable-from-newsrc-alist)
2672
2673     ;; Finally, if we read some options lines, we parse them.
2674     (unless (string= gnus-newsrc-options "")
2675       (gnus-newsrc-parse-options gnus-newsrc-options))))
2676
2677 ;; Parse options lines to find "options -n !all rec.all" and stuff.
2678 ;; The return value will be a list on the form
2679 ;; ((regexp1 . ignore)
2680 ;;  (regexp2 . subscribe)...)
2681 ;; When handling new newsgroups, groups that match a `ignore' regexp
2682 ;; will be ignored, and groups that match a `subscribe' regexp will be
2683 ;; subscribed.  A line like
2684 ;; options -n !all rec.all
2685 ;; will lead to a list that looks like
2686 ;; (("^rec\\..+" . subscribe)
2687 ;;  ("^.+" . ignore))
2688 ;; So all "rec.*" groups will be subscribed, while all the other
2689 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
2690 ;; different from "options -n rec.all !all".
2691 (defun gnus-newsrc-parse-options (options)
2692   (let (out eol)
2693     (save-excursion
2694       (gnus-set-work-buffer)
2695       (insert (regexp-quote options))
2696       ;; First we treat all continuation lines.
2697       (goto-char (point-min))
2698       (while (re-search-forward "\n[ \t]+" nil t)
2699         (replace-match " " t t))
2700       ;; Then we transform all "all"s into ".+"s.
2701       (goto-char (point-min))
2702       (while (re-search-forward "\\ball\\b" nil t)
2703         (replace-match ".+" t t))
2704       (goto-char (point-min))
2705       ;; We remove all other options than the "-n" ones.
2706       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
2707         (replace-match " ")
2708         (forward-char -1))
2709       (goto-char (point-min))
2710
2711       ;; We are only interested in "options -n" lines - we
2712       ;; ignore the other option lines.
2713       (while (re-search-forward "[ \t]-n" nil t)
2714         (setq eol
2715               (or (save-excursion
2716                     (and (re-search-forward "[ \t]-n" (point-at-eol) t)
2717                          (- (point) 2)))
2718                   (point-at-eol)))
2719         ;; Search for all "words"...
2720         (while (re-search-forward "[^ \t,\n]+" eol t)
2721           (if (eq (char-after (match-beginning 0)) ?!)
2722               ;; If the word begins with a bang (!), this is a "not"
2723               ;; spec.  We put this spec (minus the bang) and the
2724               ;; symbol `ignore' into the list.
2725               (push (cons (concat
2726                            "^" (buffer-substring
2727                                 (1+ (match-beginning 0))
2728                                 (match-end 0))
2729                            "\\($\\|\\.\\)")
2730                           'ignore)
2731                     out)
2732             ;; There was no bang, so this is a "yes" spec.
2733             (push (cons (concat "^" (match-string 0) "\\($\\|\\.\\)")
2734                         'subscribe)
2735                   out))))
2736
2737       (setq gnus-newsrc-options-n out))))
2738
2739 (eval-and-compile
2740   (defalias 'gnus-long-file-names
2741     (if (fboundp 'msdos-long-file-names)
2742       'msdos-long-file-names
2743       (lambda () t))))
2744
2745 (defun gnus-save-newsrc-file (&optional force)
2746   "Save .newsrc file."
2747   ;; Note: We cannot save .newsrc file if all newsgroups are removed
2748   ;; from the variable gnus-newsrc-alist.
2749   (when (and (or gnus-newsrc-alist gnus-killed-list)
2750              gnus-current-startup-file)
2751     ;; Save agent range limits for the currently active method.
2752     (when gnus-agent
2753       (gnus-agent-save-local force))
2754
2755     (save-excursion
2756       (if (and (or gnus-use-dribble-file gnus-slave)
2757                (not force)
2758                (or (not gnus-dribble-buffer)
2759                    (not (buffer-name gnus-dribble-buffer))
2760                    (zerop (save-excursion
2761                             (set-buffer gnus-dribble-buffer)
2762                             (buffer-size)))))
2763           (gnus-message 4 "(No changes need to be saved)")
2764         (gnus-run-hooks 'gnus-save-newsrc-hook)
2765         (if gnus-slave
2766             (gnus-slave-save-newsrc)
2767           ;; Save .newsrc.
2768           (when gnus-save-newsrc-file
2769             (gnus-message 8 "Saving %s..." gnus-current-startup-file)
2770             (gnus-gnus-to-newsrc-format)
2771             (gnus-message 8 "Saving %s...done" gnus-current-startup-file))
2772
2773           ;; Save .newsrc.eld.
2774           (set-buffer (gnus-get-buffer-create " *Gnus-newsrc*"))
2775           (make-local-variable 'version-control)
2776           (setq version-control gnus-backup-startup-file)
2777           (setq buffer-file-name
2778                 (concat gnus-current-startup-file ".eld"))
2779           (setq default-directory (file-name-directory buffer-file-name))
2780           (buffer-disable-undo)
2781           (erase-buffer)
2782           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
2783
2784           (if gnus-save-startup-file-via-temp-buffer
2785               (let ((coding-system-for-write gnus-ding-file-coding-system)
2786                     (standard-output (current-buffer)))
2787                 (gnus-gnus-to-quick-newsrc-format)
2788                 (gnus-run-hooks 'gnus-save-quick-newsrc-hook)
2789                 (save-buffer))
2790             (let ((coding-system-for-write gnus-ding-file-coding-system)
2791                   (version-control gnus-backup-startup-file)
2792                   (startup-file (concat gnus-current-startup-file ".eld"))
2793                   (working-dir (file-name-directory gnus-current-startup-file))
2794                   working-file
2795                   (i -1))
2796               ;; Generate the name of a non-existent file.
2797               (while (progn (setq working-file
2798                                   (format
2799                                    (if (and (eq system-type 'ms-dos)
2800                                             (not (gnus-long-file-names)))
2801                                        "%s#%d.tm#" ; MSDOS limits files to 8+3
2802                                      "%s#tmp#%d")
2803                                    working-dir (setq i (1+ i))))
2804                             (file-exists-p working-file)))
2805
2806               (unwind-protect
2807                   (progn
2808                     (gnus-with-output-to-file working-file
2809                       (gnus-gnus-to-quick-newsrc-format)
2810                       (gnus-run-hooks 'gnus-save-quick-newsrc-hook))
2811
2812                     ;; These bindings will mislead the current buffer
2813                     ;; into thinking that it is visiting the startup
2814                     ;; file.
2815                     (let ((buffer-backed-up nil)
2816                           (buffer-file-name startup-file)
2817                           (file-precious-flag t)
2818                           (setmodes (file-modes startup-file)))
2819                       ;; Backup the current version of the startup file.
2820                       (backup-buffer)
2821
2822                       ;; Replace the existing startup file with the temp file.
2823                       (rename-file working-file startup-file t)
2824                       (gnus-set-file-modes startup-file setmodes)))
2825                 (condition-case nil
2826                     (delete-file working-file)
2827                   (file-error nil)))))
2828
2829           (gnus-kill-buffer (current-buffer))
2830           (gnus-message
2831            5 "Saving %s.eld...done" gnus-current-startup-file))
2832         (gnus-dribble-delete-file)
2833         (gnus-group-set-mode-line)))))
2834
2835 (defun gnus-gnus-to-quick-newsrc-format (&optional minimal name &rest specific-variables)
2836   "Print Gnus variables such as `gnus-newsrc-alist' in Lisp format."
2837     (princ (format ";; -*- mode:emacs-lisp; coding: %s; -*-\n"
2838                    gnus-ding-file-coding-system))
2839     (if name
2840         (princ (format ";; %s\n" name))
2841       (princ ";; Gnus startup file.\n"))
2842
2843     (unless minimal
2844       (princ "\
2845 ;; Never delete this file -- if you want to force Gnus to read the
2846 ;; .newsrc file (if you have one), touch .newsrc instead.\n")
2847       (princ "(setq gnus-newsrc-file-version ")
2848       (princ (gnus-prin1-to-string gnus-version))
2849       (princ ")\n"))
2850
2851     (let* ((print-quoted t)
2852            (print-readably t)
2853            (print-escape-multibyte nil)
2854            (print-escape-nonascii t)
2855            (print-length nil)
2856            (print-level nil)
2857            (print-circle nil)
2858            (print-escape-newlines t)
2859            (gnus-killed-list
2860             (if (and gnus-save-killed-list
2861                      (stringp gnus-save-killed-list))
2862                 (gnus-strip-killed-list)
2863               gnus-killed-list))
2864            (variables
2865             (or specific-variables
2866                 (if gnus-save-killed-list gnus-variable-list
2867                   ;; Remove the `gnus-killed-list' from the list of variables
2868                   ;; to be saved, if required.
2869                   (delq 'gnus-killed-list (copy-sequence gnus-variable-list)))))
2870            ;; Peel off the "dummy" group.
2871            (gnus-newsrc-alist (cdr gnus-newsrc-alist))
2872            variable)
2873       ;; Insert the variables into the file.
2874       (while variables
2875         (when (and (boundp (setq variable (pop variables)))
2876                    (symbol-value variable))
2877           (princ "\n(setq ")
2878           (princ (symbol-name variable))
2879           (princ " '")
2880           (prin1 (symbol-value variable))
2881           (princ ")\n")))))
2882
2883 (defun gnus-strip-killed-list ()
2884   "Return the killed list minus the groups that match `gnus-save-killed-list'."
2885   (let ((list gnus-killed-list)
2886         olist)
2887     (while list
2888       (when (string-match gnus-save-killed-list (car list))
2889         (push (car list) olist))
2890       (pop list))
2891     (nreverse olist)))
2892
2893 (defun gnus-gnus-to-newsrc-format ()
2894   ;; Generate and save the .newsrc file.
2895   (save-excursion
2896     (set-buffer (create-file-buffer gnus-current-startup-file))
2897     (let ((newsrc (cdr gnus-newsrc-alist))
2898           (standard-output (current-buffer))
2899           info ranges range method)
2900       (setq buffer-file-name gnus-current-startup-file)
2901       (setq default-directory (file-name-directory buffer-file-name))
2902       (buffer-disable-undo)
2903       (erase-buffer)
2904       ;; Use a unibyte buffer since group names are unibyte strings;
2905       ;; in particular, non-ASCII group names are the ones encoded by
2906       ;; a certain coding system.
2907       (mm-disable-multibyte)
2908       ;; Write options.
2909       (when gnus-newsrc-options
2910         (insert gnus-newsrc-options))
2911       ;; Write subscribed and unsubscribed.
2912       (while (setq info (pop newsrc))
2913         ;; Don't write foreign groups to .newsrc.
2914         (when (or (null (setq method (gnus-info-method info)))
2915                   (equal method "native")
2916                   (inline (gnus-server-equal method gnus-select-method)))
2917           (insert (gnus-info-group info)
2918                   (if (> (gnus-info-level info) gnus-level-subscribed)
2919                       "!" ":"))
2920           (when (setq ranges (gnus-info-read info))
2921             (insert " ")
2922             (if (not (listp (cdr ranges)))
2923                 (if (= (car ranges) (cdr ranges))
2924                     (princ (car ranges))
2925                   (princ (car ranges))
2926                   (insert "-")
2927                   (princ (cdr ranges)))
2928               (while (setq range (pop ranges))
2929                 (if (or (atom range) (= (car range) (cdr range)))
2930                     (princ (or (and (atom range) range) (car range)))
2931                   (princ (car range))
2932                   (insert "-")
2933                   (princ (cdr range)))
2934                 (when ranges
2935                   (insert ",")))))
2936           (insert "\n")))
2937       (make-local-variable 'version-control)
2938       (setq version-control 'never)
2939       ;; It has been reported that sometime the modtime on the .newsrc
2940       ;; file seems to be off.  We really do want to overwrite it, so
2941       ;; we clear the modtime here before saving.  It's a bit odd,
2942       ;; though...
2943       ;; sometimes the modtime clear isn't sufficient.  most brute force:
2944       ;; delete the silly thing entirely first.  but this fails to provide
2945       ;; such niceties as .newsrc~ creation.
2946       (if gnus-modtime-botch
2947           (delete-file gnus-startup-file)
2948         (clear-visited-file-modtime))
2949       (gnus-run-hooks 'gnus-save-standard-newsrc-hook)
2950       (let ((coding-system-for-write 'raw-text))
2951         (save-buffer))
2952       (kill-buffer (current-buffer)))))
2953
2954 \f
2955 ;;;
2956 ;;; Slave functions.
2957 ;;;
2958
2959 (defvar gnus-slave-mode nil)
2960
2961 (defun gnus-slave-mode ()
2962   "Minor mode for slave Gnusae."
2963   (add-minor-mode 'gnus-slave-mode " Slave" (make-sparse-keymap))
2964   (gnus-run-hooks 'gnus-slave-mode-hook))
2965
2966 (defun gnus-slave-save-newsrc ()
2967   (save-excursion
2968     (set-buffer gnus-dribble-buffer)
2969     (let ((slave-name
2970            (mm-make-temp-file (concat gnus-current-startup-file "-slave-")))
2971           (modes (ignore-errors
2972                    (file-modes (concat gnus-current-startup-file ".eld")))))
2973       (let ((coding-system-for-write gnus-ding-file-coding-system))
2974         (gnus-write-buffer slave-name))
2975       (when modes
2976         (gnus-set-file-modes slave-name modes)))))
2977
2978 (defun gnus-master-read-slave-newsrc ()
2979   (let ((slave-files
2980          (directory-files
2981           (file-name-directory gnus-current-startup-file)
2982           t (concat
2983              "^" (regexp-quote
2984                   (concat
2985                    (file-name-nondirectory gnus-current-startup-file)
2986                    "-slave-")))
2987           t))
2988         file)
2989     (if (not slave-files)
2990         ()                              ; There are no slave files to read.
2991       (gnus-message 7 "Reading slave newsrcs...")
2992       (save-excursion
2993         (set-buffer (gnus-get-buffer-create " *gnus slave*"))
2994         (setq slave-files
2995               (sort (mapcar (lambda (file)
2996                               (list (nth 5 (file-attributes file)) file))
2997                             slave-files)
2998                     (lambda (f1 f2)
2999                       (or (< (caar f1) (caar f2))
3000                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
3001         (while slave-files
3002           (erase-buffer)
3003           (setq file (nth 1 (car slave-files)))
3004           (nnheader-insert-file-contents file)
3005           (when (condition-case ()
3006                     (progn
3007                       (eval-buffer (current-buffer))
3008                       t)
3009                   (error
3010                    (gnus-error 3.2 "Possible error in %s" file)
3011                    nil))
3012             (unless gnus-slave          ; Slaves shouldn't delete these files.
3013               (ignore-errors
3014                 (delete-file file))))
3015           (setq slave-files (cdr slave-files))))
3016       (gnus-dribble-touch)
3017       (gnus-message 7 "Reading slave newsrcs...done"))))
3018
3019 \f
3020 ;;;
3021 ;;; Group description.
3022 ;;;
3023
3024 (defun gnus-read-all-descriptions-files ()
3025   (let ((methods (cons gnus-select-method
3026                        (nconc
3027                         (when (gnus-archive-server-wanted-p)
3028                           (list "archive"))
3029                         gnus-secondary-select-methods))))
3030     (while methods
3031       (gnus-read-descriptions-file (car methods))
3032       (setq methods (cdr methods)))
3033     t))
3034
3035 (defun gnus-read-descriptions-file (&optional method)
3036   (let ((method (or method gnus-select-method))
3037         group)
3038     (when (stringp method)
3039       (setq method (gnus-server-to-method method)))
3040     ;; We create the hashtable whether we manage to read the desc file
3041     ;; to avoid trying to re-read after a failed read.
3042     (unless gnus-description-hashtb
3043       (setq gnus-description-hashtb
3044             (gnus-make-hashtable (length gnus-active-hashtb))))
3045     ;; Mark this method's desc file as read.
3046     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
3047                   gnus-description-hashtb)
3048
3049     (gnus-message 5 "Reading descriptions file via %s..." (car method))
3050     (cond
3051      ((null (gnus-get-function method 'request-list-newsgroups t))
3052       t)
3053      ((not (gnus-check-server method))
3054       (gnus-message 1 "Couldn't open server")
3055       nil)
3056      ((not (gnus-request-list-newsgroups method))
3057       (gnus-message 1 "Couldn't read newsgroups descriptions")
3058       nil)
3059      (t
3060       (save-excursion
3061         (save-restriction
3062           (set-buffer nntp-server-buffer)
3063           (goto-char (point-min))
3064           (when (or (search-forward "\n.\n" nil t)
3065                     (goto-char (point-max)))
3066             (beginning-of-line)
3067             (narrow-to-region (point-min) (point)))
3068           ;; If these are groups from a foreign select method, we insert the
3069           ;; group prefix in front of the group names.
3070           (and method (not (inline
3071                              (gnus-server-equal
3072                               (gnus-server-get-method nil method)
3073                               (gnus-server-get-method
3074                                nil gnus-select-method))))
3075                (let ((prefix (gnus-group-prefixed-name "" method)))
3076                  (goto-char (point-min))
3077                  (while (and (not (eobp))
3078                              (progn (insert prefix)
3079                                     (zerop (forward-line 1)))))))
3080           (goto-char (point-min))
3081           (while (not (eobp))
3082             ;; If we get an error, we set group to 0, which is not a
3083             ;; symbol...
3084             (setq group
3085                   (condition-case ()
3086                       (let ((obarray gnus-description-hashtb))
3087                         ;; Group is set to a symbol interned in this
3088                         ;; hash table.
3089                         (read nntp-server-buffer))
3090                     (error 0)))
3091             (skip-chars-forward " \t")
3092             ;; ...  which leads to this line being effectively ignored.
3093             (when (symbolp group)
3094               (let* ((str (buffer-substring
3095                            (point) (progn (end-of-line) (point))))
3096                      (name (symbol-name group))
3097                      (charset
3098                       (or (gnus-group-name-charset method name)
3099                           (gnus-parameter-charset name)
3100                           gnus-default-charset)))
3101                 ;; Fixme: Don't decode in unibyte mode.
3102                 (when (and str charset (featurep 'mule))
3103                   (setq str (mm-decode-coding-string str charset)))
3104                 (set group str)))
3105             (forward-line 1))))
3106       (gnus-message 5 "Reading descriptions file...done")
3107       t))))
3108
3109 (defun gnus-group-get-description (group)
3110   "Get the description of a group by sending XGTITLE to the server."
3111   (when (gnus-request-group-description group)
3112     (save-excursion
3113       (set-buffer nntp-server-buffer)
3114       (goto-char (point-min))
3115       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
3116         (match-string 1)))))
3117
3118 ;;;###autoload
3119 (defun gnus-declare-backend (name &rest abilities)
3120   "Declare back end NAME with ABILITIES as a Gnus back end."
3121   (setq gnus-valid-select-methods
3122         (nconc gnus-valid-select-methods
3123                (list (apply 'list name abilities))))
3124   (gnus-redefine-select-method-widget))
3125
3126 (defun gnus-set-default-directory ()
3127   "Set the default directory in the current buffer to `gnus-default-directory'.
3128 If this variable is nil, don't do anything."
3129   (setq default-directory
3130         (if (and gnus-default-directory
3131                  (file-exists-p gnus-default-directory))
3132             (file-name-as-directory (expand-file-name gnus-default-directory))
3133           default-directory)))
3134
3135 (defun gnus-display-time-event-handler ()
3136   (if (and (fboundp 'display-time-event-handler)
3137            (gnus-boundp 'display-time-timer))
3138       (display-time-event-handler)))
3139
3140 ;;;###autoload
3141 (defun gnus-fixup-nnimap-unread-after-getting-new-news ()
3142   (let (server group info)
3143     (mapatoms
3144      (lambda (sym)
3145        (when (and (setq group (symbol-name sym))
3146                   (gnus-group-entry group)
3147                   (setq info (symbol-value sym)))
3148          (gnus-sethash group (cons (nth 2 info) (cdr (gnus-group-entry group)))
3149                        gnus-newsrc-hashtb)))
3150      (if (boundp 'nnimap-mailbox-info)
3151          (symbol-value 'nnimap-mailbox-info)
3152        (make-vector 1 0)))))
3153
3154 (defun gnus-check-reasonable-setup ()
3155   ;; Check whether nnml and nnfolder share a directory.
3156   (let ((display-warn
3157          (if (fboundp 'display-warning)
3158              'display-warning
3159            (lambda (type message)
3160              (if noninteractive
3161                  (message "Warning (%s): %s" type message)
3162                (let (window)
3163                  (with-current-buffer (get-buffer-create "*Warnings*")
3164                    (goto-char (point-max))
3165                    (unless (bolp)
3166                      (insert "\n"))
3167                    (insert (format "Warning (%s): %s\n" type message))
3168                    (setq window (display-buffer (current-buffer)))
3169                    (set-window-start
3170                     window
3171                     (prog2
3172                         (forward-line (- 1 (window-height window)))
3173                         (point)
3174                       (goto-char (point-max))))))))))
3175         method active actives match)
3176     (dolist (server gnus-server-alist)
3177       (setq method (gnus-server-to-method server)
3178             active (intern (format "%s-active-file" (car method))))
3179       (when (and (member (car method) '(nnml nnfolder))
3180                  (gnus-server-opened method)
3181                  (boundp active))
3182         (when (setq match (assoc (symbol-value active) actives))
3183           (funcall display-warn 'gnus-server
3184                    (format "%s and %s share the same active file %s"
3185                            (car method)
3186                            (cadr match)
3187                            (car match))))
3188         (push (list (symbol-value active) method) actives)))))
3189
3190 (provide 'gnus-start)
3191
3192 ;; arch-tag: f4584a22-b7b7-4853-abfc-a637329af5d2
3193 ;;; gnus-start.el ends here
3194
3195