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