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