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