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