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