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