gnus-start.el 115 KB
Newer Older
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1
;;; gnus-start.el --- startup functions for Gnus
2

3
;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
4

5
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
6 7 8 9
;; Keywords: news

;; This file is part of GNU Emacs.

10
;; GNU Emacs is free software: you can redistribute it and/or modify
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
14 15 16

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
18 19 20
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
22 23 24 25 26 27 28 29 30 31 32

;;; Commentary:

;;; Code:

(require 'gnus)
(require 'gnus-win)
(require 'gnus-int)
(require 'gnus-spec)
(require 'gnus-range)
(require 'gnus-util)
33 34
(autoload 'message-make-date "message")
(autoload 'gnus-agent-read-servers-validate "gnus-agent")
35
(autoload 'gnus-agent-save-local "gnus-agent")
36
(autoload 'gnus-agent-possibly-alter-active "gnus-agent")
37

38
(eval-when-compile
Dan Nicolaescu's avatar
Dan Nicolaescu committed
39
  (require 'cl))
40

Dan Nicolaescu's avatar
Dan Nicolaescu committed
41 42 43
(defvar gnus-agent-covered-methods)
(defvar gnus-agent-file-loading-local)
(defvar gnus-agent-file-loading-cache)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
44 45 46 47 48 49 50

(defcustom gnus-startup-file (nnheader-concat gnus-home-directory ".newsrc")
  "Your `.newsrc' file.
`.newsrc-SERVER' will be used instead if that exists."
  :group 'gnus-start
  :type 'file)

51
(defcustom gnus-backup-startup-file 'never
Miles Bader's avatar
Miles Bader committed
52
  "Control use of version numbers for backups of `gnus-startup-file'.
53 54
This variable takes the same values as the `version-control'
variable."
55
  :version "22.1"
56 57 58 59 60 61 62 63 64 65
  :group 'gnus-start
  :type '(choice (const :tag "Never" never)
		 (const :tag "If existing" nil)
		 (other :tag "Always" t)))

(defcustom gnus-save-startup-file-via-temp-buffer t
  "Whether to write the startup file contents to a buffer then save
the buffer or write directly to the file.  The buffer is faster
because all of the contents are written at once.  The direct write
uses considerably less memory."
66
  :version "22.1"
67 68 69 70
  :group 'gnus-start
  :type '(choice (const :tag "Write via buffer" t)
                 (const :tag "Write directly to file" nil)))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
71
(defcustom gnus-init-file (nnheader-concat gnus-home-directory ".gnus")
72 73
  "Your Gnus Emacs-Lisp startup file name.
If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
74 75 76 77
  :group 'gnus-start
  :type 'file)

(defcustom gnus-site-init-file
78
  (condition-case nil
79 80 81
      (concat (file-name-directory
	       (directory-file-name installation-directory))
	      "site-lisp/gnus-init")
82
    (error nil))
83
  "The site-wide Gnus Emacs-Lisp startup file name, or nil if none.
84
If a file with the `.el' or `.elc' suffixes exists, it will be read instead."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
85
  :group 'gnus-start
86
  :type '(choice file (const nil)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

(defcustom gnus-use-dribble-file t
  "*Non-nil means that Gnus will use a dribble file to store user updates.
If Emacs should crash without saving the .newsrc files, complete
information can be restored from the dribble file."
  :group 'gnus-dribble-file
  :type 'boolean)

(defcustom gnus-dribble-directory nil
  "*The directory where dribble files will be saved.
If this variable is nil, the directory where the .newsrc files are
saved will be used."
  :group 'gnus-dribble-file
  :type '(choice directory (const nil)))

102
(defcustom gnus-check-new-newsgroups 'ask-server
103
  "*Non-nil means that Gnus will run `gnus-find-new-newsgroups' at startup.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
104 105 106 107
This normally finds new newsgroups by comparing the active groups the
servers have already reported with those Gnus already knows, either alive
or killed.

108
When any of the following are true, `gnus-find-new-newsgroups' will instead
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
109 110 111 112 113
ask the servers (primary, secondary, and archive servers) to list new
groups since the last time it checked:
  1. This variable is `ask-server'.
  2. This variable is a list of select methods (see below).
  3. `gnus-read-active-file' is nil or `some'.
114
  4. A prefix argument is given to `gnus-find-new-newsgroups' interactively.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

Thus, if this variable is `ask-server' or a list of select methods or
`gnus-read-active-file' is nil or `some', then the killed list is no
longer necessary, so you could safely set `gnus-save-killed-list' to nil.

This variable can be a list of select methods which Gnus will query with
the `ask-server' method in addition to the primary, secondary, and archive
servers.

Eg.
  (setq gnus-check-new-newsgroups
	'((nntp \"some.server\") (nntp \"other.server\")))

If this variable is nil, then you have to tell Gnus explicitly to
check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups]."
  :group 'gnus-start
  :type '(choice (const :tag "no" nil)
		 (const :tag "by brute force" t)
		 (const :tag "ask servers" ask-server)
		 (repeat :menu-tag "ask additional servers"
			 :tag "ask additional servers"
			 :value ((nntp ""))
			 (sexp :format "%v"))))

(defcustom gnus-check-bogus-newsgroups nil
  "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
If this variable is nil, then you have to tell Gnus explicitly to
check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups]."
  :group 'gnus-start-server
  :type 'boolean)

146
(defcustom gnus-read-active-file 'some
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  "*Non-nil means that Gnus will read the entire active file at startup.
If this variable is nil, Gnus will only know about the groups in your
`.newsrc' file.

If this variable is `some', Gnus will try to only read the relevant
parts of the active file from the server.  Not all servers support
this, and it might be quite slow with other servers, but this should
generally be faster than both the t and nil value.

If you set this variable to nil or `some', you probably still want to
be told about new newsgroups that arrive.  To do that, set
`gnus-check-new-newsgroups' to `ask-server'.  This may not work
properly with all servers."
  :group 'gnus-start-server
  :type '(choice (const nil)
		 (const some)
		 (const t)))

165 166
(defconst gnus-level-subscribed 5
  "Groups with levels less than or equal to this variable are subscribed.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
167

168 169
(defconst gnus-level-unsubscribed 7
  "Groups with levels less than or equal to this variable are unsubscribed.
Miles Bader's avatar
Miles Bader committed
170 171 172 173 174

Groups with levels less than `gnus-level-subscribed', which
should be less than this variable, are subscribed.  Groups with
levels from `gnus-level-subscribed' (exclusive) upto this
variable (inclusive) are unsubscribed.  See also
175
`gnus-level-zombie', `gnus-level-killed' and the Info node `(gnus)Group
Miles Bader's avatar
Miles Bader committed
176
Levels' for details.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
177

178 179
(defconst gnus-level-zombie 8
  "Groups with this level are zombie groups.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
180

181 182
(defconst gnus-level-killed 9
  "Groups with this level are killed.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

(defcustom gnus-level-default-subscribed 3
  "*New subscribed groups will be subscribed at this level."
  :group 'gnus-group-levels
  :type 'integer)

(defcustom gnus-level-default-unsubscribed 6
  "*New unsubscribed groups will be unsubscribed at this level."
  :group 'gnus-group-levels
  :type 'integer)

(defcustom gnus-activate-level (1+ gnus-level-subscribed)
  "*Groups higher than this level won't be activated on startup.
Setting this variable to something low might save lots of time when
you have many groups that you aren't interested in."
  :group 'gnus-group-levels
  :type 'integer)

(defcustom gnus-activate-foreign-newsgroups 4
  "*If nil, Gnus will not check foreign newsgroups at startup.
If it is non-nil, it should be a number between one and nine.  Foreign
newsgroups that have a level lower or equal to this number will be
activated on startup.  For instance, if you want to active all
subscribed newsgroups, but not the rest, you'd set this variable to
`gnus-level-subscribed'.

If you subscribe to lots of newsgroups from different servers, startup
might take a while.  By setting this variable to nil, you'll save time,
but you won't be told how many unread articles there are in the
groups."
  :group 'gnus-group-levels
214 215
  :type '(choice integer
		 (const :tag "none" nil)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
216

217 218 219 220 221 222 223
(defcustom gnus-read-newsrc-file t
  "*Non-nil means that Gnus will read the `.newsrc' file.
Gnus always reads its own startup file, which is called
\".newsrc.eld\".  The file called \".newsrc\" is in a format that can
be readily understood by other newsreaders.  If you don't plan on
using other newsreaders, set this variable to nil to save some time on
entry."
224
  :version "21.1"
225 226 227
  :group 'gnus-newsrc
  :type 'boolean)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
(defcustom gnus-save-newsrc-file t
  "*Non-nil means that Gnus will save the `.newsrc' file.
Gnus always saves its own startup file, which is called
\".newsrc.eld\".  The file called \".newsrc\" is in a format that can
be readily understood by other newsreaders.  If you don't plan on
using other newsreaders, set this variable to nil to save some time on
exit."
  :group 'gnus-newsrc
  :type 'boolean)

(defcustom gnus-save-killed-list t
  "*If non-nil, save the list of killed groups to the startup file.
If you set this variable to nil, you'll save both time (when starting
and quitting) and space (both memory and disk), but it will also mean
that Gnus has no record of which groups are new and which are old, so
the automatic new newsgroups subscription methods become meaningless.

You should always set `gnus-check-new-newsgroups' to `ask-server' or
nil if you set this variable to nil.

This variable can also be a regexp.  In that case, all groups that do
not match this regexp will be removed before saving the list."
  :group 'gnus-newsrc
251 252 253 254 255
  :type '(radio (sexp :format "Non-nil\n"
		      :match (lambda (widget value)
			       (and value (not (stringp value))))
		      :value t)
		(const nil)
256
		regexp))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
257 258

(defcustom gnus-ignored-newsgroups
259 260
  (mapconcat 'identity
	     '("^to\\."			; not "real" groups
261
	       "^[0-9. \t]+\\( \\|$\\)"	; all digits in name
262
	       "^[\"][\"#'()]"	; bogus characters
263 264
	       )
	     "\\|")
265
  "*A regexp to match uninteresting newsgroups in the active file.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
266 267 268 269 270 271 272
Any lines in the active file matching this regular expression are
removed from the newsgroup list before anything else is done to it,
thus making them effectively non-existent."
  :group 'gnus-group-new
  :type 'regexp)

(defcustom gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
273
  "*Function(s) called with a group name when new group is detected.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
274 275 276 277 278 279
A few pre-made functions are supplied: `gnus-subscribe-randomly'
inserts new groups at the beginning of the list of groups;
`gnus-subscribe-alphabetically' inserts new groups in strict
alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
for your decision; `gnus-subscribe-killed' kills all new groups;
280 281 282
`gnus-subscribe-zombies' will make all new groups into zombies;
`gnus-subscribe-topics' will enter groups into the topics that
claim them."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
283 284 285 286 287 288 289
  :group 'gnus-group-new
  :type '(radio (function-item gnus-subscribe-randomly)
		(function-item gnus-subscribe-alphabetically)
		(function-item gnus-subscribe-hierarchically)
		(function-item gnus-subscribe-interactively)
		(function-item gnus-subscribe-killed)
		(function-item gnus-subscribe-zombies)
290
		(function-item gnus-subscribe-topics)
291 292 293 294 295 296
		function
		(repeat function)))

(defcustom gnus-subscribe-newsgroup-hooks nil
  "*Hooks run after you subscribe to a new group.
The hooks will be called with new group's name as argument."
297
  :version "22.1"
298 299
  :group 'gnus-group-new
  :type 'hook)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
300 301 302

(defcustom gnus-subscribe-options-newsgroup-method
  'gnus-subscribe-alphabetically
303
  "*Function(s) called to subscribe newsgroups mentioned on \"options -n\" lines.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
304 305 306 307 308 309
If, for instance, you want to subscribe to all newsgroups in the
\"no\" and \"alt\" hierarchies, you'd put the following in your
.newsrc file:

options -n no.all alt.all

Miles Bader's avatar
Miles Bader committed
310 311
Gnus will then subscribe all new newsgroups in these hierarchies
with the subscription method in this variable."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
312 313 314 315 316 317 318
  :group 'gnus-group-new
  :type '(radio (function-item gnus-subscribe-randomly)
		(function-item gnus-subscribe-alphabetically)
		(function-item gnus-subscribe-hierarchically)
		(function-item gnus-subscribe-interactively)
		(function-item gnus-subscribe-killed)
		(function-item gnus-subscribe-zombies)
319 320 321
		(function-item gnus-subscribe-topics)
		function
		(repeat function)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
322 323 324 325 326 327 328 329 330 331 332 333 334

(defcustom gnus-subscribe-hierarchical-interactive nil
  "*If non-nil, Gnus will offer to subscribe hierarchically.
When a new hierarchy appears, Gnus will ask the user:

'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):

If the user pressed `d', Gnus will descend the hierarchy, `y' will
subscribe to all newsgroups in the hierarchy and `s' will skip this
hierarchy in its entirety."
  :group 'gnus-group-new
  :type 'boolean)

335 336 337 338 339 340 341 342 343
(defcustom gnus-auto-subscribed-categories '(mail post-mail)
  "*New groups from methods of these categories will be subscribed automatically.
Note that this variable only deals with new groups.  It has no
effect whatsoever on old groups.  The default is to automatically
subscribe all groups from mail-like backends."
  :version "24.1"
  :group 'gnus-group-new
  :type '(repeat symbol))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
344
(defcustom gnus-auto-subscribed-groups
345
  "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl\\|^nnmaildir\\|^nnimap"
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
346 347 348 349 350 351 352 353 354 355 356 357
  "*All new groups that match this regexp will be subscribed automatically.
Note that this variable only deals with new groups.  It has no effect
whatsoever on old groups.

New groups that match this regexp will not be handled by
`gnus-subscribe-newsgroup-method'.  Instead, they will
be subscribed using `gnus-subscribe-options-newsgroup-method'."
  :group 'gnus-group-new
  :type 'regexp)

(defcustom gnus-options-subscribe nil
  "*All new groups matching this regexp will be subscribed unconditionally.
358
Note that this variable deals only with new newsgroups.  This variable
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
359 360 361 362 363 364 365 366 367 368 369
does not affect old newsgroups.

New groups that match this regexp will not be handled by
`gnus-subscribe-newsgroup-method'.  Instead, they will
be subscribed using `gnus-subscribe-options-newsgroup-method'."
  :group 'gnus-group-new
  :type '(choice regexp
		 (const :tag "none" nil)))

(defcustom gnus-options-not-subscribe nil
  "*All new groups matching this regexp will be ignored.
370
Note that this variable deals only with new newsgroups.  This variable
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
does not affect old (already subscribed) newsgroups."
  :group 'gnus-group-new
  :type '(choice regexp
		 (const :tag "none" nil)))

(defcustom gnus-modtime-botch nil
  "*Non-nil means .newsrc should be deleted prior to save.
Its use is due to the bogus appearance that .newsrc was modified on
disc."
  :group 'gnus-newsrc
  :type 'boolean)

(defcustom gnus-check-bogus-groups-hook nil
  "A hook run after removing bogus groups."
  :group 'gnus-start-server
  :type 'hook)

(defcustom gnus-startup-hook nil
  "A hook called at startup.
This hook is called after Gnus is connected to the NNTP server."
  :group 'gnus-start
  :type 'hook)

394
(defcustom gnus-before-startup-hook nil
Miles Bader's avatar
Miles Bader committed
395
  "A hook called before startup.
396 397 398 399
This hook is called as the first thing when Gnus is started."
  :group 'gnus-start
  :type 'hook)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
400 401 402 403 404
(defcustom gnus-started-hook nil
  "A hook called as the last thing after startup."
  :group 'gnus-start
  :type 'hook)

405
(defcustom gnus-setup-news-hook nil
406 407 408 409
  "A hook after reading the .newsrc file, but before generating the buffer."
  :group 'gnus-start
  :type 'hook)

410 411
(defcustom gnus-get-top-new-news-hook nil
  "A hook run just before Gnus checks for new news globally."
412
  :version "22.1"
413 414 415
  :group 'gnus-group-new
  :type 'hook)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
416 417 418 419 420 421
(defcustom gnus-get-new-news-hook nil
  "A hook run just before Gnus checks for new news."
  :group 'gnus-group-new
  :type 'hook)

(defcustom gnus-after-getting-new-news-hook
422
  '(gnus-display-time-event-handler)
423
  "*A hook run after Gnus checks for new news when Gnus is already running."
424
  :version "24.1"
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
425 426 427
  :group 'gnus-group-new
  :type 'hook)

428 429 430 431 432
(defcustom gnus-read-newsrc-el-hook nil
  "A hook called after reading the newsrc.eld? file."
  :group 'gnus-newsrc
  :type 'hook)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
(defcustom gnus-save-newsrc-hook nil
  "A hook called before saving any of the newsrc files."
  :group 'gnus-newsrc
  :type 'hook)

(defcustom gnus-save-quick-newsrc-hook nil
  "A hook called just before saving the quick newsrc file.
Can be used to turn version control on or off."
  :group 'gnus-newsrc
  :type 'hook)

(defcustom gnus-save-standard-newsrc-hook nil
  "A hook called just before saving the standard newsrc file.
Can be used to turn version control on or off."
  :group 'gnus-newsrc
  :type 'hook)

450 451 452 453 454 455
(defcustom gnus-group-mode-hook nil
  "Hook for Gnus group mode."
  :group 'gnus-group-various
  :options '(gnus-topic-mode)
  :type 'hook)

456
(defcustom gnus-always-read-dribble-file nil
457
  "Unconditionally read the dribble file."
458 459 460
  :group 'gnus-newsrc
  :type 'boolean)

461 462
;;; Internal variables

463 464
;; Fixme: deal with old emacs-mule when mm-universal-coding-system is
;; utf-8-emacs.
465 466
(defvar gnus-ding-file-coding-system mm-universal-coding-system
  "Coding system for ding file.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

(defvar gnus-newsrc-file-version nil)
(defvar gnus-override-subscribe-method nil)
(defvar gnus-dribble-buffer nil)
(defvar gnus-newsrc-options nil
  "Options line in the .newsrc file.")

(defvar gnus-newsrc-options-n nil
  "List of regexps representing groups to be subscribed/ignored unconditionally.")

(defvar gnus-newsrc-last-checked-date nil
  "Date Gnus last asked server for new newsgroups.")

(defvar gnus-current-startup-file nil
  "Startup file for the current host.")

;; Byte-compiler warning.
(defvar gnus-group-line-format)

;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
(defvar gnus-init-inhibit nil)
(defun gnus-read-init-file (&optional inhibit-next)
  ;; Don't load .gnus if the -q option was used.
  (when init-file-user
    (if gnus-init-inhibit
	(setq gnus-init-inhibit nil)
      (setq gnus-init-inhibit inhibit-next)
494 495 496 497 498 499 500 501
      (dolist (file (list gnus-site-init-file gnus-init-file))
	(when (and file
		   (locate-library file))
	  (if (or debug-on-error debug-on-quit)
	      (load file nil t)
	    (condition-case var
		(load file nil t)
	      (error
502
	       (error "Error in %s: %s" file (cadr var))))))))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
503 504 505 506 507

;; For subscribing new newsgroup

(defun gnus-subscribe-hierarchical-interactive (groups)
  (let ((groups (sort groups 'string<))
508
	prefixes prefix start ans group starts real-group)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
509 510 511
    (while groups
      (setq prefixes (list "^"))
      (while (and groups prefixes)
512 513
	(while (not (string-match (car prefixes)
				  (gnus-group-real-name (car groups))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
514 515 516
	  (setq prefixes (cdr prefixes)))
	(setq prefix (car prefixes))
	(setq start (1- (length prefix)))
517 518
	(if (and (string-match "[^\\.]\\." (gnus-group-real-name (car groups))
			       start)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
519 520
		 (cdr groups)
		 (setq prefix
521 522 523 524
		       (concat "^" (substring
				    (gnus-group-real-name (car groups))
				    0 (match-end 0))))
		 (string-match prefix (gnus-group-real-name (cadr groups))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
525 526 527 528
	    (progn
	      (push prefix prefixes)
	      (message "Descend hierarchy %s? ([y]nsq): "
		       (substring prefix 1 (1- (length prefix))))
529 530
	      (while (not (memq (setq ans (read-char-exclusive))
				'(?y ?\n ?\r ?n ?s ?q)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
531 532 533 534 535
		(ding)
		(message "Descend hierarchy %s? ([y]nsq): "
			 (substring prefix 1 (1- (length prefix)))))
	      (cond ((= ans ?n)
		     (while (and groups
536 537 538
				 (setq group (car groups)
				       real-group (gnus-group-real-name group))
				 (string-match prefix real-group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
539 540 541 542 543 544
		       (push group gnus-killed-list)
		       (gnus-sethash group group gnus-killed-hashtb)
		       (setq groups (cdr groups)))
		     (setq starts (cdr starts)))
		    ((= ans ?s)
		     (while (and groups
545 546 547
				 (setq group (car groups)
				       real-group (gnus-group-real-name group))
				 (string-match prefix real-group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
548 549 550 551 552 553 554 555 556 557 558 559
		       (gnus-sethash group group gnus-killed-hashtb)
		       (gnus-subscribe-alphabetically (car groups))
		       (setq groups (cdr groups)))
		     (setq starts (cdr starts)))
		    ((= ans ?q)
		     (while groups
		       (setq group (car groups))
		       (push group gnus-killed-list)
		       (gnus-sethash group group gnus-killed-hashtb)
		       (setq groups (cdr groups))))
		    (t nil)))
	  (message "Subscribe %s? ([n]yq)" (car groups))
560 561
	  (while (not (memq (setq ans (read-char-exclusive))
			    '(?y ?\n ?\r ?q ?n)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	    (ding)
	    (message "Subscribe %s? ([n]yq)" (car groups)))
	  (setq group (car groups))
	  (cond ((= ans ?y)
		 (gnus-subscribe-alphabetically (car groups))
		 (gnus-sethash group group gnus-killed-hashtb))
		((= ans ?q)
		 (while groups
		   (setq group (car groups))
		   (push group gnus-killed-list)
		   (gnus-sethash group group gnus-killed-hashtb)
		   (setq groups (cdr groups))))
		(t
		 (push group gnus-killed-list)
		 (gnus-sethash group group gnus-killed-hashtb)))
	  (setq groups (cdr groups)))))))

(defun gnus-subscribe-randomly (newsgroup)
  "Subscribe new NEWSGROUP by making it the first newsgroup."
  (gnus-subscribe-newsgroup newsgroup))

(defun gnus-subscribe-alphabetically (newgroup)
584
  "Subscribe new NEWGROUP and insert it in alphabetical order."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
585 586 587 588 589 590 591 592 593
  (let ((groups (cdr gnus-newsrc-alist))
	before)
    (while (and (not before) groups)
      (if (string< newgroup (caar groups))
	  (setq before (caar groups))
	(setq groups (cdr groups))))
    (gnus-subscribe-newsgroup newgroup before)))

(defun gnus-subscribe-hierarchically (newgroup)
594
  "Subscribe new NEWGROUP and insert it in hierarchical newsgroup order."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
595
  ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
596
  (with-current-buffer (nnheader-find-file-noselect gnus-current-startup-file)
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    (prog1
	(let ((groupkey newgroup) before)
	  (while (and (not before) groupkey)
	    (goto-char (point-min))
	    (let ((groupkey-re
		   (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
	      (while (and (re-search-forward groupkey-re nil t)
			  (progn
			    (setq before (match-string 1))
			    (string< before newgroup)))))
	    ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
	    (setq groupkey
		  (when (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
		    (substring groupkey (match-beginning 1) (match-end 1)))))
	  (gnus-subscribe-newsgroup newgroup before))
      (kill-buffer (current-buffer)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
613 614 615 616 617

(defun gnus-subscribe-interactively (group)
  "Subscribe the new GROUP interactively.
It is inserted in hierarchical newsgroup order if subscribed.  If not,
it is killed."
618
  (if (gnus-y-or-n-p (format "Subscribe new newsgroup %s? " group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
619 620 621 622 623 624 625 626 627 628 629 630 631
      (gnus-subscribe-hierarchically group)
    (push group gnus-killed-list)))

(defun gnus-subscribe-zombies (group)
  "Make the new GROUP into a zombie group."
  (push group gnus-zombie-list))

(defun gnus-subscribe-killed (group)
  "Make the new GROUP a killed group."
  (push group gnus-killed-list))

(defun gnus-subscribe-newsgroup (newsgroup &optional next)
  "Subscribe new NEWSGROUP.
632
If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
633 634 635 636 637 638
the first newsgroup."
  (save-excursion
    (goto-char (point-min))
    ;; We subscribe the group by changing its level to `subscribed'.
    (gnus-group-change-level
     newsgroup gnus-level-default-subscribed
639
     gnus-level-killed (gnus-group-entry (or next "dummy.group")))
640
    (gnus-request-update-group-status newsgroup 'subscribe)
641 642 643
    (gnus-message 5 "Subscribe newsgroup: %s" newsgroup)
    (run-hook-with-args 'gnus-subscribe-newsgroup-hooks newsgroup)
    t))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
644 645 646 647 648 649 650 651

(defun gnus-read-active-file-p ()
  "Say whether the active file has been read from `gnus-select-method'."
  (memq gnus-select-method gnus-have-read-active-file))

;;; General various misc type functions.

;; Silence byte-compiler.
Dan Nicolaescu's avatar
Dan Nicolaescu committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665
(defvar gnus-current-headers)
(defvar gnus-thread-indent-array)
(defvar gnus-newsgroup-name)
(defvar gnus-newsgroup-headers)
(defvar gnus-group-list-mode)
(defvar gnus-group-mark-positions)
(defvar gnus-newsgroup-data)
(defvar gnus-newsgroup-unreads)
(defvar nnoo-state-alist)
(defvar gnus-current-select-method)
(defvar mail-sources)
(defvar nnmail-scan-directory-mail-source-once)
(defvar nnmail-split-history)
(defvar nnmail-spool-file)
666 667 668 669 670 671

(defun gnus-close-all-servers ()
  "Close all servers."
  (interactive)
  (dolist (server gnus-opened-servers)
    (gnus-close-server (car server))))
672

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
673 674 675
(defun gnus-clear-system ()
  "Clear all variables and buffers."
  ;; Clear Gnus variables.
676
  (let ((variables (remove 'gnus-format-specs gnus-variable-list)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
677 678 679 680 681 682
    (while variables
      (set (car variables) nil)
      (setq variables (cdr variables))))
  ;; Clear other internal variables.
  (setq gnus-list-of-killed-groups nil
	gnus-have-read-active-file nil
683
        gnus-agent-covered-methods nil
684 685
        gnus-agent-file-loading-local nil
        gnus-agent-file-loading-cache nil
686
        gnus-server-method-cache nil
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	gnus-newsrc-alist nil
	gnus-newsrc-hashtb nil
	gnus-killed-list nil
	gnus-zombie-list nil
	gnus-killed-hashtb nil
	gnus-active-hashtb nil
	gnus-moderated-hashtb nil
	gnus-description-hashtb nil
	gnus-current-headers nil
	gnus-thread-indent-array nil
	gnus-newsgroup-headers nil
	gnus-newsgroup-name nil
	gnus-server-alist nil
	gnus-group-list-mode nil
	gnus-opened-servers nil
	gnus-group-mark-positions nil
	gnus-newsgroup-data nil
	gnus-newsgroup-unreads nil
	nnoo-state-alist nil
706
	gnus-current-select-method nil
707
	nnmail-split-history nil
708
	gnus-extended-servers nil
709
	gnus-ephemeral-servers nil)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
710 711 712 713 714 715 716 717 718 719 720 721
  (gnus-shutdown 'gnus)
  ;; Kill the startup file.
  (and gnus-current-startup-file
       (get-file-buffer gnus-current-startup-file)
       (kill-buffer (get-file-buffer gnus-current-startup-file)))
  ;; Clear the dribble buffer.
  (gnus-dribble-clear)
  ;; Kill global KILL file buffer.
  (when (get-file-buffer (gnus-newsgroup-kill-file nil))
    (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
  (gnus-kill-buffer nntp-server-buffer)
  ;; Kill Gnus buffers.
722 723
  (dolist (buffer (gnus-buffers))
    (gnus-kill-buffer buffer))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
724 725 726 727 728
  ;; Remove Gnus frames.
  (gnus-kill-gnus-frames))

(defun gnus-no-server-1 (&optional arg slave)
  "Read network news.
729 730 731 732 733 734
If ARG is a positive number, Gnus will use that as the startup
level.  If ARG is nil, Gnus will be started at level 2
\(`gnus-level-default-subscribed' minus one).  If ARG is non-nil
and not a positive number, Gnus will prompt the user for the name
of an NNTP server to use.  As opposed to \\[gnus], this command
will not connect to the local server."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
735 736 737 738 739 740 741 742 743
  (interactive "P")
  (let ((val (or arg (1- gnus-level-default-subscribed))))
    (gnus val t slave)
    (make-local-variable 'gnus-group-use-permanent-levels)
    (setq gnus-group-use-permanent-levels val)))

(defun gnus-1 (&optional arg dont-connect slave)
  "Read network news.
If ARG is non-nil and a positive number, Gnus will use that as the
744
startup level.  If ARG is non-nil and not a positive number, Gnus will
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
745 746 747
prompt the user for the name of an NNTP server to use."
  (interactive "P")

748
  (if (gnus-alive-p)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
749 750 751 752 753 754 755 756
      (progn
	(switch-to-buffer gnus-group-buffer)
	(gnus-group-get-new-news
	 (and (numberp arg)
	      (> arg 0)
	      (max (car gnus-group-list-mode) arg))))

    (gnus-clear-system)
757 758
    (gnus-splash)
    (gnus-run-hooks 'gnus-before-startup-hook)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
759 760
    (nnheader-init-server-buffer)
    (setq gnus-slave slave)
761
    (gnus-read-init-file)
762 763 764 765

    ;; Add "native" to gnus-predefined-server-alist just to have a
    ;; name for the native select method.
    (when gnus-select-method
766 767
      (add-to-list 'gnus-predefined-server-alist
		   (cons "native" gnus-select-method)))
768

769 770
    (if gnus-agent
	(gnus-agentize))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
771 772 773 774 775 776 777 778 779 780

    (let ((level (and (numberp arg) (> arg 0) arg))
	  did-connect)
      (unwind-protect
	  (progn
	    (unless dont-connect
	      (setq did-connect
		    (gnus-start-news-server (and arg (not level))))))
	(if (and (not dont-connect)
		 (not did-connect))
781
	    ;; Couldn't connect to the server, so bail out.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
782
	    (gnus-group-quit)
783
	  (gnus-run-hooks 'gnus-startup-hook)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
784 785 786 787 788 789 790 791 792 793
	  ;; Find the current startup file name.
	  (setq gnus-current-startup-file
		(gnus-make-newsrc-file gnus-startup-file))

	  ;; Read the dribble file.
	  (when (or gnus-slave gnus-use-dribble-file)
	    (gnus-dribble-read-file))

	  ;; Do the actual startup.
	  (gnus-setup-news nil level dont-connect)
794
	  (gnus-run-hooks 'gnus-setup-news-hook)
795 796
	  (when gnus-agent
	    (gnus-request-create-group "queue" '(nndraft "")))
797
	  (gnus-start-draft-setup)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
798 799 800 801 802
	  ;; Generate the group buffer.
	  (gnus-group-list-groups level)
	  (gnus-group-first-unread-group)
	  (gnus-configure-windows 'group)
	  (gnus-group-set-mode-line)
803 804 805 806
	  (gnus-run-hooks 'gnus-started-hook))))))

(defun gnus-start-draft-setup ()
  "Make sure the draft group exists."
807
  (interactive)
808
  (gnus-request-create-group "drafts" '(nndraft ""))
809
  (unless (gnus-group-entry "nndraft:drafts")
810
    (let ((gnus-level-default-subscribed 1))
811 812
      (gnus-subscribe-group "nndraft:drafts" nil '(nndraft "")))
    (setcar (gnus-group-entry "nndraft:drafts") 0))
813 814
  (unless (equal (gnus-group-get-parameter "nndraft:drafts" 'gnus-dummy t)
		 '((gnus-draft-mode)))
815 816
    (gnus-group-set-parameter
     "nndraft:drafts" 'gnus-dummy '((gnus-draft-mode)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834


;;;
;;; Dribble file
;;;

(defvar gnus-dribble-ignore nil)
(defvar gnus-dribble-eval-file nil)

(defun gnus-dribble-file-name ()
  "Return the dribble file for the current .newsrc."
  (concat
   (if gnus-dribble-directory
       (concat (file-name-as-directory gnus-dribble-directory)
	       (file-name-nondirectory gnus-current-startup-file))
     gnus-current-startup-file)
   "-dribble"))

835 836 837
(defun gnus-dribble-enter (string &optional regexp)
  "Enter STRING into the dribble buffer.
If REGEXP is given, lines that match it will be deleted."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
838 839 840 841 842
  (when (and (not gnus-dribble-ignore)
	     gnus-dribble-buffer
	     (buffer-name gnus-dribble-buffer))
    (let ((obuf (current-buffer)))
      (set-buffer gnus-dribble-buffer)
843 844 845 846 847 848 849 850
      (when regexp
	(goto-char (point-min))
	(let (end)
	  (while (re-search-forward regexp nil t)
	    (unless (bolp) (forward-line 1))
	    (setq end (point))
	    (goto-char (match-beginning 0))
	    (delete-region (point-at-bol) end))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
851 852
      (goto-char (point-max))
      (insert string "\n")
853 854 855 856 857
      ;; This has been commented by Josh Huber <huber@alum.wpi.edu>
      ;; It causes problems with both XEmacs and Emacs 21, and doesn't
      ;; seem to be of much value. (FIXME: remove this after we make sure
      ;; it's not needed).
      ;; (set-window-point (get-buffer-window (current-buffer)) (point-max))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
858
      (bury-buffer gnus-dribble-buffer)
859
      (with-current-buffer gnus-group-buffer
860
	(gnus-group-set-mode-line))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
861 862 863 864 865 866 867 868 869
      (set-buffer obuf))))

(defun gnus-dribble-touch ()
  "Touch the dribble buffer."
  (gnus-dribble-enter ""))

(defun gnus-dribble-read-file ()
  "Read the dribble file from disk."
  (let ((dribble-file (gnus-dribble-file-name)))
870 871
    (unless (file-exists-p (file-name-directory dribble-file))
      (make-directory (file-name-directory dribble-file) t))
872 873 874
    (with-current-buffer (setq gnus-dribble-buffer
			       (gnus-get-buffer-create
				(file-name-nondirectory dribble-file)))
875
      (set (make-local-variable 'file-precious-flag) t)
876
      (setq buffer-save-without-query t)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
877 878 879
      (erase-buffer)
      (setq buffer-file-name dribble-file)
      (auto-save-mode t)
880
      (buffer-disable-undo)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
881 882 883 884
      (bury-buffer (current-buffer))
      (set-buffer-modified-p nil)
      (let ((auto (make-auto-save-file-name))
	    (gnus-dribble-ignore t)
885
	    (purpose nil)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
886 887 888 889 890 891 892 893 894 895 896 897
	    modes)
	(when (or (file-exists-p auto) (file-exists-p dribble-file))
	  ;; Load whichever file is newest -- the auto save file
	  ;; or the "real" file.
	  (if (file-newer-than-file-p auto dribble-file)
	      (nnheader-insert-file-contents auto)
	    (nnheader-insert-file-contents dribble-file))
	  (unless (zerop (buffer-size))
	    (set-buffer-modified-p t))
	  ;; Set the file modes to reflect the .newsrc file modes.
	  (save-buffer)
	  (when (and (file-exists-p gnus-current-startup-file)
898
		     (file-exists-p dribble-file)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
899
		     (setq modes (file-modes gnus-current-startup-file)))
900
	    (gnus-set-file-modes dribble-file modes))
901 902 903
	  (goto-char (point-min))
	  (when (search-forward "Gnus was exited on purpose" nil t)
	    (setq purpose t))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
904
	  ;; Possibly eval the file later.
905 906
	  (when (or gnus-always-read-dribble-file
		    (gnus-y-or-n-p
907 908 909
		     (if purpose
			 "Gnus exited on purpose without saving; read auto-save file anyway? "
		     "Gnus auto-save file exists.  Do you want to read it? ")))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923
	    (setq gnus-dribble-eval-file t)))))))

(defun gnus-dribble-eval-file ()
  (when gnus-dribble-eval-file
    (setq gnus-dribble-eval-file nil)
    (save-excursion
      (let ((gnus-dribble-ignore t))
	(set-buffer gnus-dribble-buffer)
	(eval-buffer (current-buffer))))))

(defun gnus-dribble-delete-file ()
  (when (file-exists-p (gnus-dribble-file-name))
    (delete-file (gnus-dribble-file-name)))
  (when gnus-dribble-buffer
924
    (with-current-buffer gnus-dribble-buffer
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
925 926 927 928 929 930 931 932 933
      (let ((auto (make-auto-save-file-name)))
	(when (file-exists-p auto)
	  (delete-file auto))
	(erase-buffer)
	(set-buffer-modified-p nil)))))

(defun gnus-dribble-save ()
  (when (and gnus-dribble-buffer
	     (buffer-name gnus-dribble-buffer))
934
    (with-current-buffer gnus-dribble-buffer
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
935 936 937 938
      (save-buffer))))

(defun gnus-dribble-clear ()
  (when (gnus-buffer-exists-p gnus-dribble-buffer)
939
    (with-current-buffer gnus-dribble-buffer
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
940 941 942 943 944 945 946 947 948 949 950 951 952
      (erase-buffer)
      (set-buffer-modified-p nil)
      (setq buffer-saved-size (buffer-size)))))


;;;
;;; Active & Newsrc File Handling
;;;

(defun gnus-setup-news (&optional rawfile level dont-connect)
  "Setup news information.
If RAWFILE is non-nil, the .newsrc file will also be read.
If LEVEL is non-nil, the news will be set up at level LEVEL."
953 954 955 956 957
  (require 'nnmail)
  (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile))))
	;; Binding this variable will inhibit multiple fetchings
	;; of the same mail source.
	(nnmail-fetched-sources (list t)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
958 959 960 961 962 963 964 965

    (when init
      ;; Clear some variables to re-initialize news information.
      (setq gnus-newsrc-alist nil
	    gnus-active-hashtb nil)
      ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
      (gnus-read-newsrc-file rawfile))

966
    ;; Make sure the archive server is available to all and sundry.
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
    (let ((method (or (and (stringp gnus-message-archive-method)
			   (gnus-server-to-method
			    gnus-message-archive-method))
		      gnus-message-archive-method)))
      ;; Check whether the archive method is writable.
      (unless (or (not method)
		  (stringp method)
		  (memq 'respool (assoc (format "%s" (car method))
					gnus-valid-select-methods)))
	(setq method "archive")) ;; The default.
      (when (stringp method)
	(setq method `(nnfolder
		       ,method
		       (nnfolder-directory
			,(nnheader-concat message-directory method))
		       (nnfolder-active-file
			,(nnheader-concat message-directory
					  (concat method "/active")))
		       (nnfolder-get-new-mail nil)
		       (nnfolder-inhibit-expiry t))))
      (if (assoc "archive" gnus-server-alist)
	  (when gnus-update-message-archive-method
	    (if method
		(setcdr (assoc "archive" gnus-server-alist) method)
	      (setq gnus-server-alist (delq (assoc "archive" gnus-server-alist)
					    gnus-server-alist))))
	(when method
	  (push (cons "archive" method) gnus-server-alist))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

    ;; If we don't read the complete active file, we fill in the
    ;; hashtb here.
    (when (or (null gnus-read-active-file)
	      (eq gnus-read-active-file 'some))
      (gnus-update-active-hashtb-from-killed))
    (unless gnus-active-hashtb
      (setq gnus-active-hashtb (gnus-make-hashtable 4096)))
    ;; Initialize the cache.
    (when gnus-use-cache
      (gnus-cache-open))

    ;; Possibly eval the dribble file.
    (and init
	 (or gnus-use-dribble-file gnus-slave)
	 (gnus-dribble-eval-file))

    ;; Slave Gnusii should then clear the dribble buffer.
    (when (and init gnus-slave)
      (gnus-dribble-clear))

    (gnus-update-format-specifications)

    ;; See whether we need to read the description file.
    (when (and (boundp 'gnus-group-line-format)
1020
	       (stringp gnus-group-line-format)
1021 1022
	       (let ((case-fold-search nil))
		 (string-match "%[-,0-9]*D" gnus-group-line-format))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1023 1024 1025 1026 1027 1028 1029 1030
	       (not gnus-description-hashtb)
	       (not dont-connect)
	       gnus-read-active-file)
      (gnus-read-all-descriptions-files))

    ;; Find new newsgroups and treat them.
    (when (and init gnus-check-new-newsgroups (not level)
	       (gnus-check-server gnus-select-method)
1031 1032
	       (not gnus-slave)
	       gnus-plugged)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1033 1034
      (gnus-find-new-newsgroups))

1035 1036 1037 1038 1039 1040
    ;; Check and remove bogus newsgroups.
    (when (and init gnus-check-bogus-newsgroups
	       gnus-read-active-file (not level)
	       (gnus-server-opened gnus-select-method))
      (gnus-check-bogus-newsgroups))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1041 1042 1043 1044 1045
    ;; Read any slave files.
    (gnus-master-read-slave-newsrc)

    ;; Find the number of unread articles in each non-dead group.
    (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
1046
      (gnus-get-unread-articles level dont-connect))))
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

(defun gnus-call-subscribe-functions (method group)
  "Call METHOD to subscribe GROUP.
If no function returns `non-nil', call `gnus-subscribe-zombies'."
  (unless (cond
	   ((functionp method)
	    (funcall method group))
	   ((listp method)
	    (catch 'found
	      (dolist (func method)
		(if (funcall func group)
		    (throw 'found t)))
	      nil))
	   (t nil))
    (gnus-subscribe-zombies group)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1062 1063 1064

(defun gnus-find-new-newsgroups (&optional arg)
  "Search for new newsgroups and add them.
1065
Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method'.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1066
The `-n' option line from .newsrc is respected.
1067 1068 1069 1070 1071 1072 1073 1074 1075

With 1 C-u, use the `ask-server' method to query the server for new
groups.
With 2 C-u's, use most complete method possible to query the server
for new groups, and subscribe the new groups as zombies."
  (interactive "p")
  (let* ((gnus-subscribe-newsgroup-method
	  gnus-subscribe-newsgroup-method)
	 (check (cond
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
		 ((or (and (= (or arg 1) 4)
			   (not (listp gnus-check-new-newsgroups)))
		      (null gnus-read-active-file)
		      (eq gnus-read-active-file 'some))
		  'ask-server)
		 ((= (or arg 1) 16)
		  (setq gnus-subscribe-newsgroup-method
			'gnus-subscribe-zombies)
		  t)
		 (t gnus-check-new-newsgroups))))
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
    (if (or (consp check)
            (eq check 'ask-server))
        ;; Ask the server for new groups.
        (gnus-ask-server-for-new-groups)
      ;; Go through the active hashtb and look for new groups.
      (let ((groups 0)
            group new-newsgroups)
        (gnus-message 5 "Looking for new newsgroups...")
        (unless gnus-have-read-active-file
          (gnus-read-active-file))
        (setq gnus-newsrc-last-checked-date (message-make-date))
        (unless gnus-killed-hashtb
          (gnus-make-hashtable-from-killed))
        ;; Go though every newsgroup in `gnus-active-hashtb' and compare
        ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
        (mapatoms
         (lambda (sym)
           (if (or (null (setq group (symbol-name sym)))
                   (not (boundp sym))
                   (null (symbol-value sym))
                   (gnus-gethash group gnus-killed-hashtb)
                   (gnus-gethash group gnus-newsrc-hashtb))
               ()
             (let ((do-sub (gnus-matches-options-n group)))
               (cond
                ((eq do-sub 'subscribe)
                 (setq groups (1+ groups))
                 (gnus-sethash group group gnus-killed-hashtb)
                 (gnus-call-subscribe-functions
                  gnus-subscribe-options-newsgroup-method group))
                ((eq do-sub 'ignore)
                 nil)
                (t
                 (setq groups (1+ groups))
                 (gnus-sethash group group gnus-killed-hashtb)
                 (if gnus-subscribe-hierarchical-interactive
                     (push group new-newsgroups)
                   (gnus-call-subscribe-functions
                    gnus-subscribe-newsgroup-method group)))))))
         gnus-active-hashtb)
        (when new-newsgroups
          (gnus-subscribe-hierarchical-interactive new-newsgroups))
        (if (> groups 0)
            (gnus-message 5 "%d new newsgroup%s arrived."
                          groups (if (> groups 1) "s have" " has"))
1131 1132
          (gnus-message 5 "No new newsgroups."))
	groups))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

(defun gnus-matches-options-n (group)
  ;; Returns `subscribe' if the group is to be unconditionally
  ;; subscribed, `ignore' if it is to be ignored, and nil if there is
  ;; no match for the group.

  ;; First we check the two user variables.
  (cond
   ((and gnus-options-subscribe
	 (string-match gnus-options-subscribe group))
    'subscribe)
1144 1145 1146 1147 1148 1149
   ((let ((do-subscribe nil))
      (dolist (category gnus-auto-subscribed-categories)
	(when (gnus-member-of-valid category group)
	  (setq do-subscribe t)))
      do-subscribe)
    'subscribe)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
   ((and gnus-auto-subscribed-groups
	 (string-match gnus-auto-subscribed-groups group))
    'subscribe)
   ((and gnus-options-not-subscribe
	 (string-match gnus-options-not-subscribe group))
    'ignore)
   ;; Then we go through the list that was retrieved from the .newsrc
   ;; file.  This list has elements on the form
   ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
   ;; is in the reverse order of the options line) is returned.
   (t
    (let ((regs gnus-newsrc-options-n))
      (while (and regs
		  (not (string-match (caar regs) group)))
	(setq regs (cdr regs)))
      (and regs (cdar regs))))))

(defun gnus-ask-server-for-new-groups ()
1168 1169
  (let* ((new-date (message-make-date))
	 (date (or gnus-newsrc-last-checked-date new-date))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	 (methods (cons gnus-select-method
			(nconc
			 (when (gnus-archive-server-wanted-p)
			   (list "archive"))
			 (append
			  (and (consp gnus-check-new-newsgroups)
			       gnus-check-new-newsgroups)
			  gnus-secondary-select-methods))))
	 (groups 0)
	 group new-newsgroups got-new method hashtb
	 gnus-override-subscribe-method)
1181 1182
    (unless gnus-killed-hashtb
      (gnus-make-hashtable-from-killed))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1183 1184 1185
    ;; Go through both primary and secondary select methods and
    ;; request new newsgroups.
    (while (setq method (gnus-server-get-method nil (pop methods)))
1186 1187
      (setq new-newsgroups nil
	    gnus-override-subscribe-method method)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1188 1189 1190
      (when (and (gnus-check-server method)
		 (gnus-request-newgroups date method))
	(save-excursion
1191 1192
	  (setq got-new t
		hashtb (gnus-make-hashtable 100))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
	  (set-buffer nntp-server-buffer)
	  ;; Enter all the new groups into a hashtable.
	  (gnus-active-to-gnus-format method hashtb 'ignore))
	;; Now all new groups from `method' are in `hashtb'.
	(mapatoms
	 (lambda (group-sym)
	   (if (or (null (setq group (symbol-name group-sym)))
		   (not (boundp group-sym))
		   (null (symbol-value group-sym))
		   (gnus-gethash group gnus-newsrc-hashtb)
		   (member group gnus-zombie-list)
		   (member group gnus-killed-list))
	       ;; The group is already known.
	       ()
	     ;; Make this group active.
	     (when (symbol-value group-sym)
	       (gnus-set-active group (symbol-value group-sym)))
	     ;; Check whether we want it or not.
	     (let ((do-sub (gnus-matches-options-n group)))
	       (cond
		((eq do-sub 'subscribe)
		 (incf groups)
		 (gnus-sethash group group gnus-killed-hashtb)
1216 1217
		 (gnus-call-subscribe-functions
		  gnus-subscribe-options-newsgroup-method group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1218 1219 1220 1221 1222 1223 1224
		((eq do-sub 'ignore)
		 nil)
		(t
		 (incf groups)
		 (gnus-sethash group group gnus-killed-hashtb)
		 (if gnus-subscribe-hierarchical-interactive
		     (push group new-newsgroups)
1225 1226
		   (gnus-call-subscribe-functions
		    gnus-subscribe-newsgroup-method group)))))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1227 1228 1229
	 hashtb))
      (when new-newsgroups
	(gnus-subscribe-hierarchical-interactive new-newsgroups)))
1230 1231 1232 1233
    (if (> groups 0)
	(gnus-message 5 "%d new newsgroup%s arrived"
		      groups (if (> groups 1) "s have" " has"))
      (gnus-message 5 "No new newsgroups"))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1234 1235
    (when got-new
      (setq gnus-newsrc-last-checked-date new-date))
1236
    new-newsgroups))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1237

1238
(defun gnus-subscribe-group (group &optional previous method)
1239
  "Subscribe GROUP and put it after PREVIOUS."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1240 1241 1242 1243
  (gnus-group-change-level
   (if method
       (list t group gnus-level-default-subscribed nil nil method)
     group)
1244 1245
   gnus-level-default-subscribed gnus-level-killed previous t)
  t)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

;; `gnus-group-change-level' is the fundamental function for changing
;; subscription levels of newsgroups.  This might mean just changing
;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
;; again, which subscribes/unsubscribes a group, which is equally
;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
;; from 8-9 to 1-7 means that you remove the group from the list of
;; killed (or zombie) groups and add them to the (kinda) subscribed
;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
;; which is trivial.
;; ENTRY can either be a string (newsgroup name) or a list (if
;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
;; entries.
;; LEVEL is the new level of the group, OLDLEVEL is the old level and
;; PREVIOUS is the group (in hashtb entry format) to insert this group
;; after.
(defun gnus-group-change-level (entry level &optional oldlevel
				      previous fromkilled)
  (let (group info active num)
    ;; Glean what info we can from the arguments
    (if (consp entry)
	(if fromkilled (setq group (nth 1 entry))
	  (setq group (car (nth 2 entry))))
      (setq group entry))
    (when (and (stringp entry)
	       oldlevel
	       (< oldlevel gnus-level-zombie))
1274
      (setq entry (gnus-group-entry entry)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1275 1276 1277
    (if (and (not oldlevel)
	     (consp entry))
	(setq oldlevel (gnus-info-level (nth 2 entry)))
1278
      (setq oldlevel (or oldlevel gnus-level-killed)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1279
    (when (stringp previous)
1280
      (setq previous (gnus-group-entry previous)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1281 1282

    (if (and (>= oldlevel gnus-level-zombie)
1283
	     (gnus-group-entry group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	;; We are trying to subscribe a group that is already
	;; subscribed.
	()				; Do nothing.

      (unless (gnus-ephemeral-group-p group)
	(gnus-dribble-enter
	 (format "(gnus-group-change-level %S %S %S %S %S)"
		 group level oldlevel (car (nth 2 previous)) fromkilled)))

      ;; Then we remove the newgroup from any old structures, if needed.
      ;; If the group was killed, we remove it from the killed or zombie
      ;; list.  If not, and it is in fact going to be killed, we remove
      ;; it from the newsrc hash table and assoc.
      (cond
       ((>= oldlevel gnus-level-zombie)
1299 1300 1301
	;; oldlevel could be wrong.
	(setq gnus-zombie-list (delete group gnus-zombie-list))
	(setq gnus-killed-list (delete group gnus-killed-list)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1302 1303 1304 1305 1306
       (t
	(when (and (>= level gnus-level-zombie)
		   entry)
	  (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
	  (when (nth 3 entry)
1307
	    (setcdr (gnus-group-entry (car (nth 3 entry)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
		    (cdr entry)))
	  (setcdr (cdr entry) (cdddr entry)))))

      ;; Finally we enter (if needed) the list where it is supposed to
      ;; go, and change the subscription level.  If it is to be killed,
      ;; we enter it into the killed or zombie list.
      (cond
       ((>= level gnus-level-zombie)
	;; Remove from the hash table.
	(gnus-sethash group nil gnus-newsrc-hashtb)
1318 1319 1320 1321 1322 1323 1324
	(if (= level gnus-level-zombie)
	    (push group gnus-zombie-list)
	  (if (= oldlevel gnus-level-killed)
	      ;; Remove from active hashtb.
	      (unintern group gnus-active-hashtb)
	    ;; Don't add it into killed-list if it was killed.
	    (push group gnus-killed-list))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
       (t
	;; If the list is to be entered into the newsrc assoc, and
	;; it was killed, we have to create an entry in the newsrc
	;; hashtb format and fix the pointers in the newsrc assoc.
	(if (< oldlevel gnus-level-zombie)
	    ;; It was alive, and it is going to stay alive, so we
	    ;; just change the level and don't change any pointers or
	    ;; hash table entries.
	    (setcar (cdaddr entry) level)
	  (if (listp entry)
	      (setq info (cdr entry)
		    num (car entry))
	    (setq active (gnus-active group))
	    (setq num
		  (if active (- (1+ (cdr active)) (car active)) t))
1340 1341 1342 1343 1344 1345 1346 1347
	    ;; Shorten the select method if possible, if we need to
	    ;; store it at all (native groups).
	    (let ((method (gnus-method-simplify
			   (or gnus-override-subscribe-method
			       (gnus-group-method group)))))
	      (if method
		  (setq info (list group level nil nil method))
		(setq info (list group level nil)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
	  (unless previous
	    (setq previous
		  (let ((p gnus-newsrc-alist))
		    (while (cddr p)
		      (setq p (cdr p)))
		    p)))
	  (setq entry (cons info (cddr previous)))
	  (if (cdr previous)
	      (progn
		(setcdr (cdr previous) entry)
		(gnus-sethash group (cons num (cdr previous))
			      gnus-newsrc-hashtb))
	    (setcdr previous entry)
	    (gnus-sethash group (cons num previous)
			  gnus-newsrc-hashtb))
	  (when (cdr entry)
1364
	    (setcdr (gnus-group-entry (caadr entry)) entry))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1365
	  (gnus-dribble-enter
1366 1367
	   (format "(gnus-group-set-info '%S)" info)
	   (concat "^(gnus-group-set-info '(\"" (regexp-quote group) "\"")))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1368
      (when gnus-group-change-level-function
1369 1370
	(funcall gnus-group-change-level-function
		 group level oldlevel previous)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1371 1372 1373 1374

(defun gnus-kill-newsgroup (newsgroup)
  "Obsolete function.  Kills a newsgroup."
  (gnus-group-change-level
1375
   (gnus-group-entry newsgroup) gnus-level-killed))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

(defun gnus-check-bogus-newsgroups (&optional confirm)
  "Remove bogus newsgroups.
If CONFIRM is non-nil, the user has to confirm the deletion of every
newsgroup."
  (let ((newsrc (cdr gnus-newsrc-alist))
	bogus group entry info)
    (gnus-message 5 "Checking bogus newsgroups...")
    (unless (gnus-read-active-file-p)
      (gnus-read-active-file t))
    (when (gnus-read-active-file-p)
      ;; Find all bogus newsgroup that are subscribed.
      (while newsrc
	(setq info (pop newsrc)
	      group (gnus-info-group info))
	(unless (or (gnus-active group)	; Active
1392 1393 1394
		    (and (gnus-info-method info)
			 (not (gnus-secondary-method-p
			       (gnus-info-method info))))) ; Foreign
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1395 1396 1397 1398
	  ;; Found a bogus newsgroup.
	  (push group bogus)))
      (if confirm
	  (map-y-or-n-p
1399
	   (format "Remove bogus group %%s (of %d groups)? " (length bogus))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1400 1401 1402
	   (lambda (group)
	     ;; Remove all bogus subscribed groups by first killing them, and
	     ;; then removing them from the list of killed groups.
1403
	     (when (setq entry (gnus-group-entry group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1404 1405 1406 1407 1408 1409
	       (gnus-group-change-level entry gnus-level-killed)
	       (setq gnus-killed-list (delete group gnus-killed-list))))
	   bogus '("group" "groups" "remove"))
	(while (setq group (pop bogus))
	  ;; Remove all bogus subscribed groups by first killing them, and
	  ;; then removing them from the list of killed groups.
1410
	  (when (setq entry (gnus-group-entry group))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1411 1412