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