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