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