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