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