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