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