1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
34 (if (featurep 'xemacs)
40 (autoload 'gnus-server-update-server "gnus-srvr"))
42 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
43 "Where the Gnus agent will store its files."
47 (defcustom gnus-agent-plugged-hook nil
48 "Hook run when plugging into the network."
52 (defcustom gnus-agent-unplugged-hook nil
53 "Hook run when unplugging from the network."
57 (defcustom gnus-agent-fetched-hook nil
58 "Hook run after finishing fetching articles."
62 (defcustom gnus-agent-handle-level gnus-level-subscribed
63 "Groups on levels higher than this variable will be ignored by the Agent."
67 (defcustom gnus-agent-expire-days 7
68 "Read articles older than this will be expired.
69 This can also be a list of regexp/day pairs. The regexps will
70 be matched against group names."
74 (defcustom gnus-agent-expire-all nil
75 "If non-nil, also expire unread, ticked and dormant articles.
76 If nil, only read articles will be expired."
80 (defcustom gnus-agent-group-mode-hook nil
81 "Hook run in Agent group minor modes."
85 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
86 (when (featurep 'xemacs)
87 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
89 (defcustom gnus-agent-summary-mode-hook nil
90 "Hook run in Agent summary minor modes."
94 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
95 (when (featurep 'xemacs)
96 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
98 (defcustom gnus-agent-server-mode-hook nil
99 "Hook run in Agent summary minor modes."
103 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
104 (when (featurep 'xemacs)
105 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
107 (defcustom gnus-agent-confirmation-function 'y-or-n-p
108 "Function to confirm when error happens."
113 (defcustom gnus-agent-synchronize-flags 'ask
114 "Indicate if flags are synchronized when you plug in.
115 If this is `ask' the hook will query the user."
117 :type '(choice (const :tag "Always" t)
118 (const :tag "Never" nil)
119 (const :tag "Ask" ask))
122 (defcustom gnus-agent-go-online 'ask
123 "Indicate if offline servers go online when you plug in.
124 If this is `ask' the hook will query the user."
126 :type '(choice (const :tag "Always" t)
127 (const :tag "Never" nil)
128 (const :tag "Ask" ask))
131 (defcustom gnus-agent-mark-unread-after-downloaded t
132 "Indicate whether to mark articles unread after downloaded."
137 (defcustom gnus-agent-download-marks '(download)
138 "Marks for downloading."
140 :type '(repeat (symbol :tag "Mark"))
143 (defcustom gnus-agent-consider-all-articles nil
144 "If non-nil, consider also the read articles for downloading."
149 ;;; Internal variables
151 (defvar gnus-agent-history-buffers nil)
152 (defvar gnus-agent-buffer-alist nil)
153 (defvar gnus-agent-article-alist nil)
154 (defvar gnus-agent-fetched-headers nil)
155 (defvar gnus-agent-group-alist nil)
156 (defvar gnus-category-alist nil)
157 (defvar gnus-agent-current-history nil)
158 (defvar gnus-agent-overview-buffer nil)
159 (defvar gnus-category-predicate-cache nil)
160 (defvar gnus-category-group-cache nil)
161 (defvar gnus-agent-spam-hashtb nil)
162 (defvar gnus-agent-file-name nil)
163 (defvar gnus-agent-send-mail-function nil)
164 (defvar gnus-agent-file-coding-system 'raw-text)
165 (defvar gnus-agent-file-loading-cache nil)
166 (defvar gnus-agent-file-header-cache nil)
168 (defvar gnus-agent-auto-agentize-methods '(nntp nnimap)
169 "Initially, all servers from these methods are agentized.
170 The user may remove or add servers using the Server buffer. See Info
171 node `(gnus)Server Buffer'.")
174 (defvar gnus-headers)
181 (defun gnus-open-agent ()
183 (gnus-agent-read-servers)
185 (gnus-agent-create-buffer)
186 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
187 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
188 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
190 (defun gnus-agent-create-buffer ()
191 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
193 (setq gnus-agent-overview-buffer
194 (gnus-get-buffer-create " *Gnus agent overview*"))
195 (with-current-buffer gnus-agent-overview-buffer
196 (mm-enable-multibyte))
199 (gnus-add-shutdown 'gnus-close-agent 'gnus)
201 (defun gnus-close-agent ()
202 (setq gnus-agent-covered-methods nil
203 gnus-category-predicate-cache nil
204 gnus-category-group-cache nil
205 gnus-agent-spam-hashtb nil)
206 (gnus-kill-buffer gnus-agent-overview-buffer))
209 ;;; Utility functions
212 (defun gnus-agent-read-file (file)
213 "Load FILE and do a `read' there."
216 (nnheader-insert-file-contents file)
217 (goto-char (point-min))
218 (read (current-buffer)))))
220 (defsubst gnus-agent-method ()
221 (concat (symbol-name (car gnus-command-method)) "/"
222 (if (equal (cadr gnus-command-method) "")
224 (cadr gnus-command-method))))
226 (defsubst gnus-agent-directory ()
227 "Path of the Gnus agent directory."
228 (nnheader-concat gnus-agent-directory
229 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
231 (defun gnus-agent-lib-file (file)
232 "The full path of the Gnus agent library FILE."
233 (expand-file-name file
234 (file-name-as-directory
235 (expand-file-name "agent.lib" (gnus-agent-directory)))))
237 ;;; Fetching setup functions.
239 (defun gnus-agent-start-fetch ()
240 "Initialize data structures for efficient fetching."
241 (gnus-agent-open-history)
242 (setq gnus-agent-current-history (gnus-agent-history-buffer))
243 (gnus-agent-create-buffer))
245 (defun gnus-agent-stop-fetch ()
246 "Save all data structures and clean up."
247 (gnus-agent-save-history)
248 (gnus-agent-close-history)
249 (setq gnus-agent-spam-hashtb nil)
251 (set-buffer nntp-server-buffer)
254 (defmacro gnus-agent-with-fetch (&rest forms)
257 (let ((gnus-agent-fetching t))
258 (gnus-agent-start-fetch)
260 (gnus-agent-stop-fetch)))
262 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
263 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
269 (defvar gnus-agent-mode-hook nil
270 "Hook run when installing agent mode.")
272 (defvar gnus-agent-mode nil)
273 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
275 (defun gnus-agent-mode ()
276 "Minor mode for providing a agent support in Gnus buffers."
277 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
278 (symbol-name major-mode))
279 (match-string 1 (symbol-name major-mode))))
280 (mode (intern (format "gnus-agent-%s-mode" buffer))))
281 (set (make-local-variable 'gnus-agent-mode) t)
283 (set (make-local-variable mode) t)
285 (when (gnus-visual-p 'agent-menu 'menu)
286 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
287 (unless (assq 'gnus-agent-mode minor-mode-alist)
288 (push gnus-agent-mode-status minor-mode-alist))
289 (unless (assq mode minor-mode-map-alist)
290 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
292 minor-mode-map-alist))
293 (when (eq major-mode 'gnus-group-mode)
294 (gnus-agent-toggle-plugged gnus-plugged))
295 (gnus-run-hooks 'gnus-agent-mode-hook
296 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
298 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
299 (gnus-define-keys gnus-agent-group-mode-map
300 "Ju" gnus-agent-fetch-groups
301 "Jc" gnus-enter-category-buffer
302 "Jj" gnus-agent-toggle-plugged
303 "Js" gnus-agent-fetch-session
304 "JY" gnus-agent-synchronize-flags
305 "JS" gnus-group-send-queue
306 "Ja" gnus-agent-add-group
307 "Jr" gnus-agent-remove-group
308 "Jo" gnus-agent-toggle-group-plugged)
310 (defun gnus-agent-group-make-menu-bar ()
311 (unless (boundp 'gnus-agent-group-menu)
313 gnus-agent-group-menu gnus-agent-group-mode-map ""
315 ["Toggle plugged" gnus-agent-toggle-plugged t]
316 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
317 ["List categories" gnus-enter-category-buffer t]
318 ["Send queue" gnus-group-send-queue gnus-plugged]
320 ["All" gnus-agent-fetch-session gnus-plugged]
321 ["Group" gnus-agent-fetch-group gnus-plugged])))))
323 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
324 (gnus-define-keys gnus-agent-summary-mode-map
325 "Jj" gnus-agent-toggle-plugged
326 "Ju" gnus-agent-summary-fetch-group
327 "J#" gnus-agent-mark-article
328 "J\M-#" gnus-agent-unmark-article
329 "@" gnus-agent-toggle-mark
330 "Jc" gnus-agent-catchup)
332 (defun gnus-agent-summary-make-menu-bar ()
333 (unless (boundp 'gnus-agent-summary-menu)
335 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
337 ["Toggle plugged" gnus-agent-toggle-plugged t]
338 ["Mark as downloadable" gnus-agent-mark-article t]
339 ["Unmark as downloadable" gnus-agent-unmark-article t]
340 ["Toggle mark" gnus-agent-toggle-mark t]
341 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
342 ["Catchup undownloaded" gnus-agent-catchup t]))))
344 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
345 (gnus-define-keys gnus-agent-server-mode-map
346 "Jj" gnus-agent-toggle-plugged
347 "Ja" gnus-agent-add-server
348 "Jr" gnus-agent-remove-server)
350 (defun gnus-agent-server-make-menu-bar ()
351 (unless (boundp 'gnus-agent-server-menu)
353 gnus-agent-server-menu gnus-agent-server-mode-map ""
355 ["Toggle plugged" gnus-agent-toggle-plugged t]
356 ["Add" gnus-agent-add-server t]
357 ["Remove" gnus-agent-remove-server t]))))
359 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
360 (if (and (fboundp 'propertize)
361 (fboundp 'make-mode-line-mouse-map))
362 (propertize string 'local-map
363 (make-mode-line-mouse-map mouse-button mouse-func))
366 (defun gnus-agent-toggle-plugged (plugged)
367 "Toggle whether Gnus is unplugged or not."
368 (interactive (list (not gnus-plugged)))
371 (setq gnus-plugged plugged)
372 (gnus-run-hooks 'gnus-agent-plugged-hook)
373 (setcar (cdr gnus-agent-mode-status)
374 (gnus-agent-make-mode-line-string " Plugged"
376 'gnus-agent-toggle-plugged))
377 (gnus-agent-go-online gnus-agent-go-online)
378 (gnus-agent-possibly-synchronize-flags))
379 (gnus-agent-close-connections)
380 (setq gnus-plugged plugged)
381 (gnus-run-hooks 'gnus-agent-unplugged-hook)
382 (setcar (cdr gnus-agent-mode-status)
383 (gnus-agent-make-mode-line-string " Unplugged"
385 'gnus-agent-toggle-plugged)))
386 (set-buffer-modified-p t))
388 (defun gnus-agent-close-connections ()
389 "Close all methods covered by the Gnus agent."
390 (let ((methods gnus-agent-covered-methods))
392 (gnus-close-server (pop methods)))))
395 (defun gnus-unplugged ()
396 "Start Gnus unplugged."
398 (setq gnus-plugged nil)
402 (defun gnus-plugged ()
403 "Start Gnus plugged."
405 (setq gnus-plugged t)
409 (defun gnus-slave-unplugged (&optional arg)
410 "Read news as a slave unplugged."
412 (setq gnus-plugged nil)
413 (gnus arg nil 'slave))
416 (defun gnus-agentize ()
417 "Allow Gnus to be an offline newsreader.
418 The normal usage of this command is to put the following as the
419 last form in your `.gnus.el' file:
423 This will modify the `gnus-setup-news-hook', and
424 `message-send-mail-real-function' variables, and install the Gnus agent
425 minor mode in all Gnus buffers."
428 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
429 (unless gnus-agent-send-mail-function
430 (setq gnus-agent-send-mail-function (or
431 message-send-mail-real-function
432 message-send-mail-function)
433 message-send-mail-real-function 'gnus-agent-send-mail))
434 (unless gnus-agent-covered-methods
437 (if (memq (car (gnus-server-to-method server))
438 gnus-agent-auto-agentize-methods)
439 (setq gnus-agent-covered-methods
440 (cons (gnus-server-to-method server)
441 gnus-agent-covered-methods ))))
442 (append (list gnus-select-method) gnus-secondary-select-methods))))
444 (defun gnus-agent-queue-setup ()
445 "Make sure the queue group exists."
446 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
447 (gnus-request-create-group "queue" '(nndraft ""))
448 (let ((gnus-level-default-subscribed 1))
449 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
450 (gnus-group-set-parameter
451 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
453 (defun gnus-agent-send-mail ()
455 (funcall gnus-agent-send-mail-function)
456 (goto-char (point-min))
458 (concat "^" (regexp-quote mail-header-separator) "\n"))
460 (gnus-agent-insert-meta-information 'mail)
461 (gnus-request-accept-article "nndraft:queue" nil t t)))
463 (defun gnus-agent-insert-meta-information (type &optional method)
464 "Insert meta-information into the message that says how it's to be posted.
465 TYPE can be either `mail' or `news'. If the latter, then METHOD can
468 (message-remove-header gnus-agent-meta-information-header)
469 (goto-char (point-min))
470 (insert gnus-agent-meta-information-header ": "
471 (symbol-name type) " " (format "%S" method)
474 (while (search-backward "\n" nil t)
475 (replace-match "\\n" t t))))
477 (defun gnus-agent-restore-gcc ()
478 "Restore GCC field from saved header."
480 (goto-char (point-min))
481 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
482 (replace-match "Gcc:" 'fixedcase))))
484 (defun gnus-agent-any-covered-gcc ()
486 (message-narrow-to-headers)
487 (let* ((gcc (mail-fetch-field "gcc" nil t))
489 (mapcar 'gnus-inews-group-method
490 (message-unquote-tokens
491 (message-tokenize-header
494 (while (and (not covered) methods)
495 (setq covered (gnus-agent-method-p (car methods))
496 methods (cdr methods)))
500 (defun gnus-agent-possibly-save-gcc ()
501 "Save GCC if Gnus is unplugged."
502 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
504 (goto-char (point-min))
505 (let ((case-fold-search t))
506 (while (re-search-forward "^gcc:" nil t)
507 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
509 (defun gnus-agent-possibly-do-gcc ()
510 "Do GCC if Gnus is plugged."
511 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
512 (gnus-inews-do-gcc)))
515 ;;; Group mode commands
518 (defun gnus-agent-fetch-groups (n)
519 "Put all new articles in the current groups into the Agent."
522 (error "Groups can't be fetched when Gnus is unplugged"))
523 (gnus-group-iterate n 'gnus-agent-fetch-group))
525 (defun gnus-agent-fetch-group (group)
526 "Put all new articles in GROUP into the Agent."
527 (interactive (list (gnus-group-group-name)))
528 (let ((state gnus-plugged))
532 (error "No group on the current line"))
534 (gnus-agent-toggle-plugged gnus-plugged))
535 (let ((gnus-command-method (gnus-find-method-for-group group)))
536 (gnus-agent-with-fetch
537 (gnus-agent-fetch-group-1 group gnus-command-method)
538 (gnus-message 5 "Fetching %s...done" group))))
539 (when (and (not state)
541 (gnus-agent-toggle-plugged gnus-plugged)))))
543 (defun gnus-agent-add-group (category arg)
544 "Add the current group to an agent category."
550 (mapcar (lambda (cat) (list (symbol-name (car cat))))
554 (let ((cat (assq category gnus-category-alist))
556 (gnus-group-iterate arg
558 (when (cadddr (setq c (gnus-group-category group)))
559 (setf (cadddr c) (delete group (cadddr c))))
560 (push group groups)))
561 (setf (cadddr cat) (nconc (cadddr cat) groups))
562 (gnus-category-write)))
564 (defun gnus-agent-remove-group (arg)
565 "Remove the current group from its agent category, if any."
568 (gnus-group-iterate arg
570 (when (cadddr (setq c (gnus-group-category group)))
571 (setf (cadddr c) (delete group (cadddr c))))))
572 (gnus-category-write)))
574 (defun gnus-agent-synchronize-flags ()
575 "Synchronize unplugged flags with servers."
578 (dolist (gnus-command-method gnus-agent-covered-methods)
579 (when (file-exists-p (gnus-agent-lib-file "flags"))
580 (gnus-agent-synchronize-flags-server gnus-command-method)))))
582 (defun gnus-agent-possibly-synchronize-flags ()
583 "Synchronize flags according to `gnus-agent-synchronize-flags'."
586 (dolist (gnus-command-method gnus-agent-covered-methods)
587 (when (file-exists-p (gnus-agent-lib-file "flags"))
588 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
590 (defun gnus-agent-synchronize-flags-server (method)
591 "Synchronize flags set when unplugged for server."
592 (let ((gnus-command-method method))
593 (when (file-exists-p (gnus-agent-lib-file "flags"))
594 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
596 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
597 (if (null (gnus-check-server gnus-command-method))
598 (message "Couldn't open server %s" (nth 1 gnus-command-method))
600 (if (null (eval (read (current-buffer))))
601 (progn (forward-line)
603 (write-file (gnus-agent-lib-file "flags"))
604 (error "Couldn't set flags from file %s"
605 (gnus-agent-lib-file "flags"))))
606 (delete-file (gnus-agent-lib-file "flags")))
609 (defun gnus-agent-possibly-synchronize-flags-server (method)
610 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
611 (when (or (and gnus-agent-synchronize-flags
612 (not (eq gnus-agent-synchronize-flags 'ask)))
613 (and (eq gnus-agent-synchronize-flags 'ask)
614 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
616 (gnus-agent-synchronize-flags-server method)))
619 ;;; Server mode commands
622 (defun gnus-agent-add-server (server)
623 "Enroll SERVER in the agent program."
624 (interactive (list (gnus-server-server-name)))
626 (error "No server on the current line"))
627 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
628 (when (gnus-agent-method-p method)
629 (error "Server already in the agent program"))
630 (push method gnus-agent-covered-methods)
631 (gnus-server-update-server server)
632 (gnus-agent-write-servers)
633 (message "Entered %s into the Agent" server)))
635 (defun gnus-agent-remove-server (server)
636 "Remove SERVER from the agent program."
637 (interactive (list (gnus-server-server-name)))
639 (error "No server on the current line"))
640 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
641 (unless (gnus-agent-method-p method)
642 (error "Server not in the agent program"))
643 (setq gnus-agent-covered-methods
644 (delete method gnus-agent-covered-methods))
645 (gnus-server-update-server server)
646 (gnus-agent-write-servers)
647 (message "Removed %s from the agent" server)))
649 (defun gnus-agent-read-servers ()
650 "Read the alist of covered servers."
652 (let ((server (gnus-server-get-method
656 (push server gnus-agent-covered-methods)
657 (message "Ignoring disappeared server `%s'" m)
659 (gnus-agent-read-file
660 (nnheader-concat gnus-agent-directory "lib/servers"))))
662 (defun gnus-agent-write-servers ()
663 "Write the alist of covered servers."
664 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
665 (let ((coding-system-for-write nnheader-file-coding-system)
666 (file-name-coding-system nnmail-pathname-coding-system))
667 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
668 (prin1 (mapcar 'gnus-method-simplify gnus-agent-covered-methods)
675 (defun gnus-agent-mark-article (n &optional unmark)
676 "Mark the next N articles as downloadable.
677 If N is negative, mark backward instead. If UNMARK is non-nil, remove
678 the mark instead. The difference between N and the actual number of
679 articles marked is returned."
681 (let ((backward (< n 0))
686 (gnus-summary-set-agent-mark
687 (gnus-summary-article-number) unmark)
688 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
691 (gnus-message 7 "No more articles"))
692 (gnus-summary-recenter)
693 (gnus-summary-position-point)
696 (defun gnus-agent-unmark-article (n)
697 "Remove the downloadable mark from the next N articles.
698 If N is negative, unmark backward instead. The difference between N and
699 the actual number of articles unmarked is returned."
701 (gnus-agent-mark-article n t))
703 (defun gnus-agent-toggle-mark (n)
704 "Toggle the downloadable mark from the next N articles.
705 If N is negative, toggle backward instead. The difference between N and
706 the actual number of articles toggled is returned."
708 (gnus-agent-mark-article n 'toggle))
710 (defun gnus-summary-set-agent-mark (article &optional unmark)
711 "Mark ARTICLE as downloadable."
712 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
713 (memq article gnus-newsgroup-downloadable)
717 (setq gnus-newsgroup-downloadable
718 (delq article gnus-newsgroup-downloadable))
719 (push article gnus-newsgroup-undownloaded))
720 (setq gnus-newsgroup-undownloaded
721 (delq article gnus-newsgroup-undownloaded))
722 (setq gnus-newsgroup-downloadable
723 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article)))
724 (gnus-summary-update-mark
725 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
728 (defun gnus-agent-get-undownloaded-list ()
729 "Mark all unfetched articles as read."
730 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
731 (when (and (not (gnus-online gnus-command-method))
732 (gnus-agent-method-p gnus-command-method))
733 (gnus-agent-load-alist gnus-newsgroup-name)
734 ;; First mark all undownloaded articles as undownloaded.
735 ;; CCC kaig: Maybe change here to consider all headers.
736 (let ((articles (mapcar (lambda (header) (mail-header-number header))
737 gnus-newsgroup-headers))
738 (agent-articles gnus-agent-article-alist)
740 (while (setq article (pop articles))
741 (while (and agent-articles
742 (< (caar agent-articles) article))
743 (setq agent-articles (cdr agent-articles)))
744 (when (or (not (cdar agent-articles))
745 (not (= (caar agent-articles) article)))
746 (push article candidates)))
747 (dolist (article candidates)
748 (unless (or (memq article gnus-newsgroup-downloadable)
749 (memq article gnus-newsgroup-cached))
750 (push article gnus-newsgroup-undownloaded))))
751 ;; Then mark downloaded downloadable as not-downloadable,
752 ;; if you get my drift.
753 (dolist (article gnus-newsgroup-downloadable)
754 (when (cdr (assq article gnus-agent-article-alist))
755 (setq gnus-newsgroup-downloadable
756 (delq article gnus-newsgroup-downloadable)))))))
758 (defun gnus-agent-catchup ()
759 "Mark all undownloaded articles as read."
762 (while gnus-newsgroup-undownloaded
763 (gnus-summary-mark-article
764 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
765 (gnus-summary-position-point))
767 (defun gnus-agent-summary-fetch-group (&optional all)
768 "Fetch the downloadable articles in the group.
769 Optional arg ALL, if non-nil, means to fetch all articles."
772 (if all gnus-newsgroup-articles
773 gnus-newsgroup-downloadable))
774 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
775 (state gnus-plugged))
779 (gnus-agent-toggle-plugged t))
781 (error "No articles to download"))
782 (gnus-agent-with-fetch
783 (gnus-agent-fetch-articles gnus-newsgroup-name articles))
785 (dolist (article articles)
786 (setq gnus-newsgroup-downloadable
787 (delq article gnus-newsgroup-downloadable))
788 (if gnus-agent-mark-unread-after-downloaded
789 (gnus-summary-mark-article article gnus-unread-mark)))))
790 (when (and (not state)
792 (gnus-agent-toggle-plugged nil)))))
794 (defun gnus-agent-fetch-selected-article ()
795 "Fetch the current article as it is selected.
796 This can be added to `gnus-select-article-hook' or
797 `gnus-mark-article-hook'."
798 (let ((gnus-command-method gnus-current-select-method))
799 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
800 (let ((gnus-agent-current-history
801 (gnus-agent-history-buffer)))
802 (unless (and gnus-agent-current-history
803 (buffer-live-p gnus-agent-current-history))
804 (gnus-agent-open-history)
805 (setq gnus-agent-current-history
806 (gnus-agent-history-buffer)))
807 (gnus-agent-fetch-articles
809 (list gnus-current-article))))))
812 ;;; Internal functions
815 (defun gnus-agent-save-active (method)
816 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
818 (defun gnus-agent-save-active-1 (method function)
819 (when (gnus-agent-method-p method)
820 (let* ((gnus-command-method method)
821 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
822 (file (gnus-agent-lib-file "active")))
823 (funcall function nil new)
824 (gnus-agent-write-active file new)
826 (nnheader-insert-file-contents file))))
828 (defun gnus-agent-write-active (file new)
829 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
830 (file (gnus-agent-lib-file "active"))
832 (when (file-exists-p file)
834 (nnheader-insert-file-contents file)
835 (gnus-active-to-gnus-format nil orig))
838 (when (and sym (boundp sym))
839 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
840 (setq elem (symbol-value osym)))
842 (if (and (integerp (car (symbol-value sym)))
843 (> (car elem) (car (symbol-value sym))))
844 (setcar elem (car (symbol-value sym))))
845 (if (integerp (cdr (symbol-value sym)))
846 (setcdr elem (cdr (symbol-value sym)))))
847 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
849 (gnus-make-directory (file-name-directory file))
850 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
851 ;; The hashtable contains real names of groups, no more prefix
852 ;; removing, so set `full' to `t'.
853 (gnus-write-active-file file orig t))))
855 (defun gnus-agent-save-groups (method)
856 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
858 (defun gnus-agent-save-group-info (method group active)
859 (when (gnus-agent-method-p method)
860 (let* ((gnus-command-method method)
861 (coding-system-for-write nnheader-file-coding-system)
862 (file-name-coding-system nnmail-pathname-coding-system)
863 (file (gnus-agent-lib-file "active"))
865 (gnus-make-directory (file-name-directory file))
867 ;; Emacs got problem to match non-ASCII group in multibyte buffer.
868 (mm-disable-multibyte)
869 (when (file-exists-p file)
870 (nnheader-insert-file-contents file))
871 (goto-char (point-min))
872 (when (re-search-forward
873 (concat "^" (regexp-quote group) " ") nil t)
875 (read (current-buffer)) ;; max
876 (setq oactive-min (read (current-buffer)))) ;; min
878 (insert (format "%S %d %d y\n" (intern group)
880 (or oactive-min (car active))))
881 (goto-char (point-max))
882 (while (search-backward "\\." nil t)
885 (defun gnus-agent-group-path (group)
886 "Translate GROUP into a path."
887 (if nnmail-use-long-file-names
888 (gnus-group-real-name group)
889 (nnheader-translate-file-chars
890 (nnheader-replace-chars-in-string
891 (nnheader-replace-duplicate-chars-in-string
892 (nnheader-replace-chars-in-string
893 (gnus-group-real-name group)
900 (defun gnus-agent-get-function (method)
901 (if (gnus-online method)
906 ;;; History functions
908 (defun gnus-agent-history-buffer ()
909 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
911 (defun gnus-agent-open-history ()
913 (push (cons (gnus-agent-method)
914 (set-buffer (gnus-get-buffer-create
915 (format " *Gnus agent %s history*"
916 (gnus-agent-method)))))
917 gnus-agent-history-buffers)
918 (mm-disable-multibyte) ;; everything is binary
921 (let ((file (gnus-agent-lib-file "history")))
922 (when (file-exists-p file)
923 (nnheader-insert-file-contents file))
924 (set (make-local-variable 'gnus-agent-file-name) file))))
926 (defun gnus-agent-save-history ()
928 (set-buffer gnus-agent-current-history)
929 (gnus-make-directory (file-name-directory gnus-agent-file-name))
930 (let ((coding-system-for-write gnus-agent-file-coding-system))
931 (write-region (1+ (point-min)) (point-max)
932 gnus-agent-file-name nil 'silent))))
934 (defun gnus-agent-close-history ()
935 (when (gnus-buffer-live-p gnus-agent-current-history)
936 (kill-buffer gnus-agent-current-history)
937 (setq gnus-agent-history-buffers
938 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
939 gnus-agent-history-buffers))))
941 (defun gnus-agent-enter-history (id group-arts date)
943 (set-buffer gnus-agent-current-history)
944 (goto-char (point-max))
946 (insert id "\t" (number-to-string date) "\t")
948 (insert (format "%S" (intern (caar group-arts)))
949 " " (number-to-string (cdr (pop group-arts)))
952 (while (search-backward "\\." p t)
955 (defun gnus-agent-article-in-history-p (id)
957 (set-buffer (gnus-agent-history-buffer))
958 (goto-char (point-min))
959 (search-forward (concat "\n" id "\t") nil t)))
961 (defun gnus-agent-history-path (id)
963 (set-buffer (gnus-agent-history-buffer))
964 (goto-char (point-min))
965 (when (search-forward (concat "\n" id "\t") nil t)
966 (let ((method (gnus-agent-method)))
968 (while (not (numberp (setq group (read (current-buffer)))))
969 (push (concat method "/" group) paths))
970 (nreverse paths))))))
976 (defun gnus-agent-fetch-articles (group articles)
977 "Fetch ARTICLES from GROUP and put them into the Agent."
979 ;; Prune off articles that we have already fetched.
981 (cdr (assq (car articles) gnus-agent-article-alist)))
983 (let ((arts articles))
985 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
986 (setcdr arts (cddr arts))
987 (setq arts (cdr arts)))))
990 (gnus-agent-directory)
991 (gnus-agent-group-path group) "/"))
992 (date (time-to-days (current-time)))
995 (gnus-make-directory dir)
996 (gnus-message 7 "Fetching articles for %s..." group)
997 ;; Fetch the articles from the backend.
998 (if (gnus-check-backend-function 'retrieve-articles group)
999 (setq pos (gnus-retrieve-articles articles group))
1002 (while (setq article (pop articles))
1003 (gnus-message 10 "Fetching article %s for %s..."
1006 (gnus-backlog-request-article group article
1008 (gnus-request-article article group))
1009 (goto-char (point-max))
1010 (push (cons article (point)) pos)
1011 (insert-buffer-substring nntp-server-buffer)))
1012 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
1013 (setq pos (nreverse pos)))))
1014 ;; Then save these articles into the Agent.
1016 (set-buffer nntp-server-buffer)
1018 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1019 (goto-char (point-min))
1020 (unless (eobp) ;; Don't save empty articles.
1021 (when (search-forward "\n\n" nil t)
1022 (when (search-backward "\nXrefs: " nil t)
1023 ;; Handle cross posting.
1024 (skip-chars-forward "^ ")
1025 (skip-chars-forward " ")
1027 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
1028 (push (cons (buffer-substring (match-beginning 1)
1030 (buffer-substring (match-beginning 2)
1033 (goto-char (match-end 0)))
1034 (gnus-agent-crosspost crosses (caar pos))))
1035 (goto-char (point-min))
1036 (if (not (re-search-forward
1037 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1038 (setq id "No-Message-ID-in-article")
1039 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
1040 (let ((coding-system-for-write
1041 gnus-agent-file-coding-system))
1042 (write-region (point-min) (point-max)
1043 (concat dir (number-to-string (caar pos)))
1045 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
1047 (gnus-agent-enter-history
1048 id (or crosses (list (cons group (caar pos)))) date))
1051 (gnus-agent-save-alist group)))))
1053 (defun gnus-agent-crosspost (crosses article)
1054 (let (gnus-agent-article-alist group alist beg end)
1056 (set-buffer gnus-agent-overview-buffer)
1057 (when (nnheader-find-nov-line article)
1060 (setq end (progn (forward-line 1) (point)))))
1062 (setq group (caar crosses))
1063 (unless (setq alist (assoc group gnus-agent-group-alist))
1064 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1065 gnus-agent-group-alist))
1066 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
1068 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1070 (when (= (point-max) (point-min))
1071 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1073 (nnheader-insert-file-contents
1074 (gnus-agent-article-name ".overview" group))))
1075 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1076 (insert (string-to-number (cdar crosses)))
1077 (insert-buffer-substring gnus-agent-overview-buffer beg end))
1080 (defun gnus-agent-flush-cache ()
1082 (while gnus-agent-buffer-alist
1083 (set-buffer (cdar gnus-agent-buffer-alist))
1084 (let ((coding-system-for-write
1085 gnus-agent-file-coding-system))
1086 (write-region (point-min) (point-max)
1087 (gnus-agent-article-name ".overview"
1088 (caar gnus-agent-buffer-alist))
1090 (pop gnus-agent-buffer-alist))
1091 (while gnus-agent-group-alist
1092 (with-temp-file (caar gnus-agent-group-alist)
1093 (princ (cdar gnus-agent-group-alist))
1095 (pop gnus-agent-group-alist))))
1097 (defun gnus-agent-fetch-headers (group &optional force)
1099 (if (and gnus-agent-consider-all-articles
1100 ;; Do not fetch all headers if the predicate
1101 ;; implies that we only consider unread articles.
1102 (not (gnus-predicate-implies-unread
1103 (or (gnus-group-find-parameter
1104 group 'agent-predicate t)
1105 (cadr (gnus-group-category group))))))
1106 (gnus-uncompress-range (gnus-active group))
1107 (gnus-list-of-unread-articles group)))
1108 (gnus-decode-encoded-word-function 'identity)
1109 (file (gnus-agent-article-name ".overview" group))
1111 ;; Add articles with marks to the list of article headers we want to
1112 ;; fetch. Don't fetch articles solely on the basis of a recent or seen
1113 ;; mark, but do fetch recent or seen articles if they have other, more
1114 ;; interesting marks. (We have to fetch articles with boring marks
1115 ;; because otherwise the agent will remove their marks.)
1116 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1117 (unless (memq (car arts) '(seen recent))
1118 (setq articles (gnus-range-add articles (cdr arts)))))
1119 (setq articles (sort (gnus-uncompress-sequence articles) '<))
1120 ;; Note which headers are fetched, and don't fetch those again.
1121 (gnus-agent-load-fetched-headers group)
1122 (let ((new-fetched (gnus-range-add gnus-agent-fetched-headers
1124 (new-articles (gnus-list-range-difference
1125 articles gnus-agent-fetched-headers)))
1126 (gnus-agent-save-fetched-headers group new-fetched)
1127 (setq articles new-articles))
1128 ;; Remove known articles.
1129 (when (gnus-agent-load-alist group)
1130 ;; Remove articles marked as downloaded.
1132 (gnus-sorted-difference
1135 (mapcar (lambda (x) (when (cdr x) (car x)))
1136 gnus-agent-article-alist))))
1137 (let ((low (1+ (caar (last gnus-agent-article-alist))))
1138 (high (cdr (gnus-active group))))
1139 ;; I suspect a deeper problem here and I suspect that low
1140 ;; should never be greater than high. But for the time being
1141 ;; we just work around the problem and abstain from frobbing
1142 ;; the article list in that case. If anyone knows how to
1143 ;; properly deal with it, please holler. -- kai
1145 (setq articles (gnus-list-range-intersection
1146 articles (list (cons low high)))))))
1148 (gnus-make-directory (nnheader-translate-file-chars
1149 (file-name-directory file) t))
1151 (gnus-message 7 "Fetching headers for %s..." group)
1153 (set-buffer nntp-server-buffer)
1154 (unless (eq 'nov (gnus-retrieve-headers articles group))
1155 (nnvirtual-convert-headers))
1156 ;; Save these headers for later processing.
1157 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
1158 (when (file-exists-p file)
1159 (gnus-agent-braid-nov group articles file))
1160 (let ((coding-system-for-write
1161 gnus-agent-file-coding-system))
1162 (write-region (point-min) (point-max) file nil 'silent))
1163 (gnus-agent-save-alist group articles nil)
1164 (gnus-agent-enter-history
1165 "last-header-fetched-for-session"
1166 (list (cons group (nth (- (length articles) 1) articles)))
1167 (time-to-days (current-time)))
1170 (defsubst gnus-agent-copy-nov-line (article)
1172 (set-buffer gnus-agent-overview-buffer)
1173 (while (and (not (eobp))
1174 (< (setq art (read (current-buffer))) article))
1178 (not (eq article art)))
1179 (set-buffer nntp-server-buffer)
1181 (setq e (progn (forward-line 1) (point)))
1182 (set-buffer nntp-server-buffer)
1183 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1185 (defun gnus-agent-braid-nov (group articles file)
1187 (set-buffer gnus-agent-overview-buffer)
1188 (goto-char (point-min))
1189 (set-buffer nntp-server-buffer)
1191 (nnheader-insert-file-contents file)
1192 (goto-char (point-max))
1193 (unless (or (= (point-min) (point-max))
1196 (< (setq last (read (current-buffer))) (car articles))))
1197 ;; We do it the hard way.
1198 (nnheader-find-nov-line (car articles))
1199 (gnus-agent-copy-nov-line (pop articles))
1200 (while (and articles
1202 (while (and (not (eobp))
1203 (< (read (current-buffer)) (car articles)))
1207 (gnus-agent-copy-nov-line (pop articles)))))
1208 ;; Copy the rest lines
1209 (set-buffer nntp-server-buffer)
1210 (goto-char (point-max))
1213 (set-buffer gnus-agent-overview-buffer)
1214 (while (and (not (eobp))
1215 (<= (read (current-buffer)) last))
1218 (setq start (point))
1219 (set-buffer nntp-server-buffer))
1220 (insert-buffer-substring gnus-agent-overview-buffer start))))
1222 (defun gnus-agent-load-alist (group &optional dir)
1223 "Load the article-state alist for GROUP."
1225 (setq gnus-agent-article-alist
1226 (gnus-cache-file-contents
1228 (expand-file-name ".agentview" dir)
1229 (gnus-agent-article-name ".agentview" group))
1230 'gnus-agent-file-loading-cache
1231 'gnus-agent-read-file))))
1233 (defun gnus-agent-save-alist (group &optional articles state dir)
1234 "Save the article-state alist for GROUP."
1235 (let* ((file-name-coding-system nnmail-pathname-coding-system)
1236 (prev (cons nil gnus-agent-article-alist))
1238 print-level print-length item article)
1239 (while (setq article (pop articles))
1240 (while (and (cdr prev)
1241 (< (caadr prev) article))
1242 (setq prev (cdr prev)))
1245 (setcdr prev (list (cons article state))))
1246 ((> (caadr prev) article)
1247 (setcdr prev (cons (cons article state) (cdr prev))))
1248 ((= (caadr prev) article)
1249 (setcdr (cadr prev) state)))
1250 (setq prev (cdr prev)))
1251 (setq gnus-agent-article-alist (cdr all))
1252 (with-temp-file (if dir
1253 (expand-file-name ".agentview" dir)
1254 (gnus-agent-article-name ".agentview" group))
1255 (princ gnus-agent-article-alist (current-buffer))
1258 (defun gnus-agent-load-fetched-headers (group)
1259 "Load ranges of fetched headers for GROUP."
1260 (setq gnus-agent-fetched-headers
1261 (gnus-cache-file-contents
1262 (gnus-agent-article-name ".fetched" group)
1263 'gnus-agent-file-header-cache
1264 'gnus-agent-read-file)))
1266 (defun gnus-agent-save-fetched-headers (group range)
1267 "Save ranges of fetched headers for GROUP.
1268 This range includes nonexisting articles."
1269 (let ((file-name-coding-system nnmail-pathname-coding-system)
1270 print-level print-length)
1271 (setq gnus-agent-fetched-headers range)
1272 (with-temp-file (gnus-agent-article-name ".fetched" group)
1273 (princ gnus-agent-fetched-headers (current-buffer))
1276 (defun gnus-agent-article-name (article group)
1277 (expand-file-name (if (stringp article) article (string-to-number article))
1278 (file-name-as-directory
1279 (expand-file-name (gnus-agent-group-path group)
1280 (gnus-agent-directory)))))
1282 (defun gnus-agent-batch-confirmation (msg)
1283 "Show error message and return t."
1284 (gnus-message 1 msg)
1288 (defun gnus-agent-batch-fetch ()
1289 "Start Gnus and fetch session."
1292 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1293 (gnus-agent-fetch-session))
1296 (defun gnus-agent-fetch-session ()
1297 "Fetch all articles and headers that are eligible for fetching."
1299 (unless gnus-agent-covered-methods
1300 (error "No servers are covered by the Gnus agent"))
1301 (unless gnus-plugged
1302 (error "Can't fetch articles while Gnus is unplugged"))
1303 (let ((methods gnus-agent-covered-methods)
1304 groups group gnus-command-method)
1309 (setq gnus-command-method (car methods))
1310 (when (and (or (gnus-server-opened gnus-command-method)
1311 (gnus-open-server gnus-command-method))
1312 (gnus-online gnus-command-method))
1313 (setq groups (gnus-groups-from-server (car methods)))
1314 (gnus-agent-with-fetch
1315 (while (setq group (pop groups))
1316 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1317 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1319 (unless (funcall gnus-agent-confirmation-function
1320 (format "Error (%s). Continue? " (cadr err)))
1321 (error "Cannot fetch articles into the Gnus agent")))
1323 (unless (funcall gnus-agent-confirmation-function
1324 (format "Quit fetching session (%s). Continue? "
1326 (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1328 (run-hooks 'gnus-agent-fetch-hook)
1329 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1331 (defun gnus-agent-fetch-group-1 (group method)
1333 (let ((gnus-command-method method)
1334 (gnus-newsgroup-name group)
1335 gnus-newsgroup-dependencies gnus-newsgroup-headers
1336 gnus-newsgroup-scored gnus-headers gnus-score
1337 gnus-use-cache articles arts
1338 category predicate info marks score-param
1339 (gnus-summary-expunge-below gnus-summary-expunge-below)
1340 (gnus-summary-mark-below gnus-summary-mark-below)
1341 (gnus-orphan-score gnus-orphan-score)
1342 ;; Maybe some other gnus-summary local variables should also
1345 (unless (gnus-check-group group)
1346 (error "Can't open server for %s" group))
1348 (when (and (or (gnus-active group)
1349 (gnus-activate-group group))
1350 (setq articles (gnus-agent-fetch-headers group))
1351 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1352 ;; Parse them and see which articles we want to fetch.
1353 (setq gnus-newsgroup-dependencies
1354 (make-vector (length articles) 0))
1355 (setq gnus-newsgroup-headers
1356 (gnus-get-newsgroup-headers-xover articles nil nil
1358 ;; Some articles may not exist, so update `articles'
1359 ;; from what was actually found. -- kai
1361 (mapcar (lambda (x) (aref x 0))
1362 gnus-newsgroup-headers))
1363 ;; `gnus-agent-overview-buffer' may be killed for
1364 ;; timeout reason. If so, recreate it.
1365 (gnus-agent-create-buffer)))
1366 (setq category (gnus-group-category group))
1369 (or (gnus-group-find-parameter group 'agent-predicate t)
1371 (if (memq predicate '(gnus-agent-true gnus-agent-false))
1372 ;; Simple implementation
1373 (setq arts (and (eq predicate 'gnus-agent-true) articles))
1376 (or (gnus-group-get-parameter group 'agent-score t)
1378 ;; Translate score-param into real one
1381 ((eq score-param 'file)
1382 (setq score-param (gnus-all-score-files group)))
1383 ((stringp (car score-param)))
1385 (setq score-param (list (list score-param)))))
1387 (gnus-score-headers score-param))
1388 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1390 (or (cdr (assq (mail-header-number gnus-headers)
1391 gnus-newsgroup-scored))
1392 gnus-summary-default-score))
1393 (when (funcall predicate)
1394 (push (mail-header-number gnus-headers)
1396 ;; Fetch the articles.
1398 (gnus-agent-fetch-articles group arts)))
1399 ;; Perhaps we have some additional articles to fetch.
1400 (dolist (mark gnus-agent-download-marks)
1401 (setq arts (assq mark (gnus-info-marks
1402 (setq info (gnus-get-info group)))))
1404 (gnus-message 8 "Agent is downloading marked articles...")
1405 (gnus-agent-fetch-articles
1406 group (gnus-uncompress-range (cdr arts)))
1407 (when (eq mark 'download)
1408 (setq marks (delq arts (gnus-info-marks info)))
1409 (gnus-info-set-marks info marks)
1411 (concat "(gnus-group-set-info '"
1412 (gnus-prin1-to-string info)
1416 ;;; Agent Category Mode
1419 (defvar gnus-category-mode-hook nil
1420 "Hook run in `gnus-category-mode' buffers.")
1422 (defvar gnus-category-line-format " %(%20c%): %g\n"
1423 "Format of category lines.
1425 Valid specifiers include:
1426 %c Topic name (string)
1427 %g The number of groups in the topic (integer)
1429 General format specifiers can also be used. See Info node
1430 `(gnus)Formatting Variables'.")
1432 (defvar gnus-category-mode-line-format "Gnus: %%b"
1433 "The format specification for the category mode line.")
1435 (defvar gnus-agent-short-article 100
1436 "Articles that have fewer lines than this are short.")
1438 (defvar gnus-agent-long-article 200
1439 "Articles that have more lines than this are long.")
1441 (defvar gnus-agent-low-score 0
1442 "Articles that have a score lower than this have a low score.")
1444 (defvar gnus-agent-high-score 0
1445 "Articles that have a score higher than this have a high score.")
1448 ;;; Internal variables.
1450 (defvar gnus-category-buffer "*Agent Category*")
1452 (defvar gnus-category-line-format-alist
1453 `((?c gnus-tmp-name ?s)
1454 (?g gnus-tmp-groups ?d)))
1456 (defvar gnus-category-mode-line-format-alist
1457 `((?u user-defined ?s)))
1459 (defvar gnus-category-line-format-spec nil)
1460 (defvar gnus-category-mode-line-format-spec nil)
1462 (defvar gnus-category-mode-map nil)
1463 (put 'gnus-category-mode 'mode-class 'special)
1465 (unless gnus-category-mode-map
1466 (setq gnus-category-mode-map (make-sparse-keymap))
1467 (suppress-keymap gnus-category-mode-map)
1469 (gnus-define-keys gnus-category-mode-map
1470 "q" gnus-category-exit
1471 "k" gnus-category-kill
1472 "c" gnus-category-copy
1473 "a" gnus-category-add
1474 "p" gnus-category-edit-predicate
1475 "g" gnus-category-edit-groups
1476 "s" gnus-category-edit-score
1477 "l" gnus-category-list
1479 "\C-c\C-i" gnus-info-find-node
1480 "\C-c\C-b" gnus-bug))
1482 (defvar gnus-category-menu-hook nil
1483 "*Hook run after the creation of the menu.")
1485 (defun gnus-category-make-menu-bar ()
1486 (gnus-turn-off-edit-menu 'category)
1487 (unless (boundp 'gnus-category-menu)
1489 gnus-category-menu gnus-category-mode-map ""
1491 ["Add" gnus-category-add t]
1492 ["Kill" gnus-category-kill t]
1493 ["Copy" gnus-category-copy t]
1494 ["Edit predicate" gnus-category-edit-predicate t]
1495 ["Edit score" gnus-category-edit-score t]
1496 ["Edit groups" gnus-category-edit-groups t]
1497 ["Exit" gnus-category-exit t]))
1499 (gnus-run-hooks 'gnus-category-menu-hook)))
1501 (defun gnus-category-mode ()
1502 "Major mode for listing and editing agent categories.
1504 All normal editing commands are switched off.
1505 \\<gnus-category-mode-map>
1506 For more in-depth information on this mode, read the manual
1507 (`\\[gnus-info-find-node]').
1509 The following commands are available:
1511 \\{gnus-category-mode-map}"
1513 (when (gnus-visual-p 'category-menu 'menu)
1514 (gnus-category-make-menu-bar))
1515 (kill-all-local-variables)
1516 (gnus-simplify-mode-line)
1517 (setq major-mode 'gnus-category-mode)
1518 (setq mode-name "Category")
1519 (gnus-set-default-directory)
1520 (setq mode-line-process nil)
1521 (use-local-map gnus-category-mode-map)
1522 (buffer-disable-undo)
1523 (setq truncate-lines t)
1524 (setq buffer-read-only t)
1525 (gnus-run-hooks 'gnus-category-mode-hook))
1527 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1529 (defun gnus-category-insert-line (category)
1530 (let* ((gnus-tmp-name (format "%s" (car category)))
1531 (gnus-tmp-groups (length (cadddr category))))
1533 (gnus-add-text-properties
1537 (eval gnus-category-line-format-spec))
1538 (list 'gnus-category gnus-tmp-name))))
1540 (defun gnus-enter-category-buffer ()
1541 "Go to the Category buffer."
1543 (gnus-category-setup-buffer)
1544 (gnus-configure-windows 'category)
1545 (gnus-category-prepare))
1547 (defun gnus-category-setup-buffer ()
1548 (unless (get-buffer gnus-category-buffer)
1550 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1551 (gnus-category-mode))))
1553 (defun gnus-category-prepare ()
1554 (gnus-set-format 'category-mode)
1555 (gnus-set-format 'category t)
1556 (let ((alist gnus-category-alist)
1557 (buffer-read-only nil))
1560 (gnus-category-insert-line (pop alist)))
1561 (goto-char (point-min))
1562 (gnus-category-position-point)))
1564 (defun gnus-category-name ()
1565 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
1566 (error "No category on the current line")))
1568 (defun gnus-category-read ()
1569 "Read the category alist."
1570 (setq gnus-category-alist
1571 (or (gnus-agent-read-file
1572 (nnheader-concat gnus-agent-directory "lib/categories"))
1573 (list (list 'default 'short nil nil)))))
1575 (defun gnus-category-write ()
1576 "Write the category alist."
1577 (setq gnus-category-predicate-cache nil
1578 gnus-category-group-cache nil)
1579 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1580 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1581 (prin1 gnus-category-alist (current-buffer))))
1583 (defun gnus-category-edit-predicate (category)
1584 "Edit the predicate for CATEGORY."
1585 (interactive (list (gnus-category-name)))
1586 (let ((info (assq category gnus-category-alist)))
1588 (cadr info) (format "Editing the predicate for category %s" category)
1589 `(lambda (predicate)
1590 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1591 (gnus-category-write)
1592 (gnus-category-list)))))
1594 (defun gnus-category-edit-score (category)
1595 "Edit the score expression for CATEGORY."
1596 (interactive (list (gnus-category-name)))
1597 (let ((info (assq category gnus-category-alist)))
1600 (format "Editing the score expression for category %s" category)
1602 (setcar (cddr (assq ',category gnus-category-alist)) groups)
1603 (gnus-category-write)
1604 (gnus-category-list)))))
1606 (defun gnus-category-edit-groups (category)
1607 "Edit the group list for CATEGORY."
1608 (interactive (list (gnus-category-name)))
1609 (let ((info (assq category gnus-category-alist)))
1611 (cadddr info) (format "Editing the group list for category %s" category)
1613 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1614 (gnus-category-write)
1615 (gnus-category-list)))))
1617 (defun gnus-category-kill (category)
1618 "Kill the current category."
1619 (interactive (list (gnus-category-name)))
1620 (let ((info (assq category gnus-category-alist))
1621 (buffer-read-only nil))
1623 (setq gnus-category-alist (delq info gnus-category-alist))
1624 (gnus-category-write)))
1626 (defun gnus-category-copy (category to)
1627 "Copy the current category."
1628 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1629 (let ((info (assq category gnus-category-alist)))
1630 (push (list to (gnus-copy-sequence (cadr info))
1631 (gnus-copy-sequence (caddr info)) nil)
1632 gnus-category-alist)
1633 (gnus-category-write)
1634 (gnus-category-list)))
1636 (defun gnus-category-add (category)
1637 "Create a new category."
1638 (interactive "SCategory name: ")
1639 (when (assq category gnus-category-alist)
1640 (error "Category %s already exists" category))
1641 (push (list category 'false nil nil)
1642 gnus-category-alist)
1643 (gnus-category-write)
1644 (gnus-category-list))
1646 (defun gnus-category-list ()
1647 "List all categories."
1649 (gnus-category-prepare))
1651 (defun gnus-category-exit ()
1652 "Return to the group buffer."
1654 (kill-buffer (current-buffer))
1655 (gnus-configure-windows 'group t))
1657 ;; To avoid having 8-bit characters in the source file.
1658 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1660 (defvar gnus-category-predicate-alist
1661 '((spam . gnus-agent-spam-p)
1662 (short . gnus-agent-short-p)
1663 (long . gnus-agent-long-p)
1664 (low . gnus-agent-low-scored-p)
1665 (high . gnus-agent-high-scored-p)
1666 (read . gnus-agent-read-p)
1667 (true . gnus-agent-true)
1668 (false . gnus-agent-false))
1669 "Mapping from short score predicate symbols to predicate functions.")
1671 (defun gnus-agent-spam-p ()
1672 "Say whether an article is spam or not."
1673 (unless gnus-agent-spam-hashtb
1674 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1675 (if (not (equal (mail-header-references gnus-headers) ""))
1677 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1679 (gnus-gethash string gnus-agent-spam-hashtb)
1680 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1682 (defun gnus-agent-short-p ()
1683 "Say whether an article is short or not."
1684 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1686 (defun gnus-agent-long-p ()
1687 "Say whether an article is long or not."
1688 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1690 (defun gnus-agent-low-scored-p ()
1691 "Say whether an article has a low score or not."
1692 (< gnus-score gnus-agent-low-score))
1694 (defun gnus-agent-high-scored-p ()
1695 "Say whether an article has a high score or not."
1696 (> gnus-score gnus-agent-high-score))
1698 (defun gnus-agent-read-p ()
1699 "Say whether an article is read or not."
1700 (gnus-member-of-range (mail-header-number gnus-headers)
1701 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
1703 (defun gnus-category-make-function (cat)
1704 "Make a function from category CAT."
1705 (let ((func (gnus-category-make-function-1 cat)))
1706 (if (and (= (length func) 1)
1707 (symbolp (car func)))
1709 (gnus-byte-compile `(lambda () ,func)))))
1711 (defun gnus-agent-true ()
1715 (defun gnus-agent-false ()
1719 (defun gnus-category-make-function-1 (cat)
1720 "Make a function from category CAT."
1722 ;; Functions are just returned as is.
1724 (gnus-functionp cat))
1725 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1727 ;; More complex category.
1730 ((memq (car cat) '(& and))
1732 ((memq (car cat) '(| or))
1734 ((memq (car cat) gnus-category-not)
1736 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1738 (error "Unknown category type: %s" cat))))
1740 (defun gnus-get-predicate (predicate)
1741 "Return the predicate for CATEGORY."
1742 (or (cdr (assoc predicate gnus-category-predicate-cache))
1743 (let ((func (gnus-category-make-function predicate)))
1744 (setq gnus-category-predicate-cache
1745 (nconc gnus-category-predicate-cache
1746 (list (cons predicate func))))
1749 (defun gnus-predicate-implies-unread (predicate)
1750 "Say whether PREDICATE implies unread articles only.
1751 It is okay to miss some cases, but there must be no false positives.
1752 That is, if this function returns true, then indeed the predicate must
1753 return only unread articles."
1754 ;; Todo: make this work in more cases.
1755 (equal predicate '(not read)))
1757 (defun gnus-group-category (group)
1758 "Return the category GROUP belongs to."
1759 (unless gnus-category-group-cache
1760 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1761 (let ((cs gnus-category-alist)
1763 (while (setq cat (pop cs))
1764 (setq groups (cadddr cat))
1766 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1767 (or (gnus-gethash group gnus-category-group-cache)
1768 (assq 'default gnus-category-alist)))
1770 (defun gnus-agent-expire (&optional articles group force)
1771 "Expire all old articles.
1772 If you want to force expiring of certain articles, this function can
1773 take ARTICLES, GROUP and FORCE parameters as well. Setting ARTICLES
1774 and GROUP without FORCE is not supported."
1776 (let ((methods (if group
1777 (list (gnus-find-method-for-group group))
1778 gnus-agent-covered-methods))
1779 (day (if (numberp gnus-agent-expire-days)
1780 (- (time-to-days (current-time)) gnus-agent-expire-days)
1782 (current-day (time-to-days (current-time)))
1783 gnus-command-method sym arts pos
1784 history overview file histories elem art nov-file low info
1785 unreads marked article orig lowest highest found days)
1787 (setq overview (gnus-get-buffer-create " *expire overview*"))
1788 (while (setq gnus-command-method (pop methods))
1789 (when (file-exists-p (gnus-agent-lib-file "active"))
1791 (nnheader-insert-file-contents (gnus-agent-lib-file "active"))
1792 (gnus-active-to-gnus-format
1794 (setq orig (gnus-make-hashtable
1795 (count-lines (point-min) (point-max))))))
1796 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1797 (gnus-agent-open-history)
1799 (setq gnus-agent-current-history
1800 (setq history (gnus-agent-history-buffer))))
1801 (goto-char (point-min))
1802 (if (and articles group force) ;; point usless without art+group
1803 (while (setq article (pop articles))
1804 ;; try to find history entries for articles
1805 (goto-char (point-min))
1806 (if (re-search-forward
1807 (concat "^[^\t]*\t[^\t]*\t\(.* ?\)"
1808 (format "%S" (gnus-group-prefixed-name
1809 group gnus-command-method))
1811 (number-to-string article)
1816 (setq sym (let ((obarray expiry-hashtb) s)
1819 (set sym (cons (cons article pos)
1820 (symbol-value sym)))
1821 (set sym (list (cons article pos)))))
1822 ;; go through history file to find eligble articles
1823 (when (> (buffer-size) 1)
1824 (goto-char (point-min))
1826 (skip-chars-forward "^\t")
1827 (if (let ((fetch-date (read (current-buffer))))
1828 (if (numberp fetch-date)
1829 ;; We now have the arrival day, so we see
1830 ;; whether it's old enough to be expired.
1833 (skip-chars-forward "\t")
1835 days gnus-agent-expire-days)
1836 (while (and (not found)
1838 (when (looking-at (caar days))
1839 (setq found (cadar days)))
1841 (> fetch-date (- current-day found)))
1842 ;; History file is corrupted.
1845 (format "File %s is corrupted!"
1846 (gnus-agent-lib-file "history")))
1850 ;; New article; we don't expire it.
1852 ;; Old article. Schedule it for possible nuking.
1854 (setq sym (let ((obarray expiry-hashtb) s)
1855 (setq s (read (current-buffer)))
1856 (if (stringp s) (intern s) s)))
1858 (set sym (cons (cons (read (current-buffer)) (point))
1859 (symbol-value sym)))
1860 (set sym (list (cons (read (current-buffer))
1862 (skip-chars-forward " "))
1863 (forward-line 1)))))
1864 ;; We now have all articles that can possibly be expired.
1867 (setq group (symbol-name sym)
1868 arts (sort (symbol-value sym) 'car-less-than-car)
1869 low (car (gnus-active group))
1870 info (gnus-get-info group)
1871 unreads (ignore-errors
1872 (gnus-list-of-unread-articles group))
1874 (gnus-uncompress-range
1875 (cdr (assq 'tick (gnus-info-marks info))))
1876 (gnus-uncompress-range
1878 (gnus-info-marks info)))))
1879 nov-file (gnus-agent-article-name ".overview" group)
1882 (gnus-agent-load-alist group)
1883 (gnus-message 5 "Expiring articles in %s" group)
1884 (set-buffer overview)
1886 (when (file-exists-p nov-file)
1887 (nnheader-insert-file-contents nov-file))
1888 (goto-char (point-min))
1890 (while (setq elem (pop arts))
1891 (setq article (car elem))
1892 (when (or (null low)
1894 gnus-agent-expire-all
1895 (and (not (memq article unreads))
1896 (not (memq article marked)))
1898 ;; Find and nuke the NOV line.
1899 (while (and (not (eobp))
1901 (setq art (read (current-buffer)))))
1903 (if (and (numberp art)
1905 (gnus-agent-article-name
1906 (number-to-string art) group)))
1912 ;; Remove old NOV lines that have no articles.
1913 (gnus-delete-line)))
1918 ;; Nuke the article.
1919 (when (file-exists-p
1920 (setq file (gnus-agent-article-name
1921 (number-to-string article)
1924 ;; Schedule the history line for nuking.
1926 (push (cdr elem) histories))))
1927 (gnus-make-directory (file-name-directory nov-file))
1928 (let ((coding-system-for-write
1929 gnus-agent-file-coding-system))
1930 (write-region (point-min) (point-max) nov-file nil 'silent))
1931 ;; Delete the unwanted entries in the alist.
1932 (setq gnus-agent-article-alist
1933 (sort gnus-agent-article-alist 'car-less-than-car))
1934 (let* ((alist gnus-agent-article-alist)
1935 (prev (cons nil alist))
1939 (<= (caar alist) article))
1940 (if (or (not (cdar alist))
1942 (gnus-agent-article-name
1947 (push (caar alist) expired)
1948 (setcdr prev (setq alist (cdr alist))))
1950 alist (cdr alist))))
1951 (setq gnus-agent-article-alist (cdr first))
1952 (gnus-agent-save-alist group)
1953 ;; Mark all articles up to the first article
1954 ;; in `gnus-agent-article-alist' as read.
1955 (when (and info (caar gnus-agent-article-alist))
1956 (setcar (nthcdr 2 info)
1959 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1960 ;; Maybe everything has been expired from
1961 ;; `gnus-agent-article-alist' and so the above marking as
1962 ;; read could not be conducted, or there are
1963 ;; expired article within the range of the alist.
1966 (or (not (caar gnus-agent-article-alist))
1968 (caar gnus-agent-article-alist))))
1969 (setcar (nthcdr 2 info)
1972 (nreverse expired))))
1974 (concat "(gnus-group-set-info '"
1975 (gnus-prin1-to-string info)
1978 (if (gnus-gethash group orig)
1979 (setcar (gnus-gethash group orig) lowest)
1980 (gnus-sethash group (cons lowest highest) orig))))
1982 (set-buffer history)
1983 (setq histories (nreverse (sort histories '<)))
1985 (goto-char (pop histories))
1987 (gnus-agent-save-history)
1988 (gnus-agent-close-history)
1989 (gnus-write-active-file
1990 (gnus-agent-lib-file "active") orig))
1991 (gnus-message 4 "Expiry...done"))))))
1994 (defun gnus-agent-batch ()
1995 "Start Gnus, send queue and fetch session."
1997 (let ((init-file-user "")
1998 (gnus-always-read-dribble-file t))
2000 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2001 (gnus-group-send-queue)
2002 (gnus-agent-fetch-session)))
2004 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
2006 (gnus-agent-create-buffer)
2007 (let ((gnus-decode-encoded-word-function 'identity)
2008 (file (gnus-agent-article-name ".overview" group))
2009 cached-articles uncached-articles)
2010 (gnus-make-directory (nnheader-translate-file-chars
2011 (file-name-directory file) t))
2012 (when (file-exists-p file)
2013 (with-current-buffer gnus-agent-overview-buffer
2015 (let ((nnheader-file-coding-system
2016 gnus-agent-file-coding-system))
2017 (nnheader-insert-nov-file file (car articles)))
2018 (nnheader-find-nov-line (car articles))
2020 (when (looking-at "[0-9]")
2021 (push (read (current-buffer)) cached-articles))
2023 (setq cached-articles (nreverse cached-articles))))
2024 (if (setq uncached-articles
2025 (gnus-sorted-difference articles cached-articles))
2027 (set-buffer nntp-server-buffer)
2029 (let (gnus-agent-cache)
2031 (gnus-retrieve-headers
2032 uncached-articles group fetch-old))
2033 (nnvirtual-convert-headers)))
2034 (set-buffer gnus-agent-overview-buffer)
2036 (set-buffer nntp-server-buffer)
2037 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
2038 (when (and uncached-articles (file-exists-p file))
2039 (gnus-agent-braid-nov group uncached-articles file))
2040 (set-buffer nntp-server-buffer)
2041 (let ((coding-system-for-write
2042 gnus-agent-file-coding-system))
2043 (write-region (point-min) (point-max) file nil 'silent))
2044 (gnus-agent-load-alist group)
2045 (gnus-agent-save-alist group uncached-articles nil)
2046 (gnus-agent-open-history)
2047 (setq gnus-agent-current-history (gnus-agent-history-buffer))
2048 (gnus-agent-enter-history
2049 "last-header-fetched-for-session"
2050 (list (cons group (nth (- (length articles) 1) articles)))
2051 (time-to-days (current-time)))
2052 (gnus-agent-save-history))
2053 (set-buffer nntp-server-buffer)
2055 (insert-buffer-substring gnus-agent-overview-buffer)))
2057 (not (numberp fetch-old)))
2058 t ; Don't remove anything.
2059 (nnheader-nov-delete-outside-range
2060 (if fetch-old (max 1 (- (car articles) fetch-old))
2062 (car (last articles)))
2066 (defun gnus-agent-request-article (article group)
2067 "Retrieve ARTICLE in GROUP from the agent cache."
2068 (let* ((gnus-command-method (gnus-find-method-for-group group))
2070 (gnus-agent-directory)
2071 (gnus-agent-group-path group) "/"
2072 (number-to-string article)))
2073 (buffer-read-only nil))
2074 (when (and (file-exists-p file)
2075 (> (nth 7 (file-attributes file)) 0))
2077 (gnus-kill-all-overlays)
2078 (let ((coding-system-for-read gnus-cache-coding-system))
2079 (insert-file-contents file))
2082 (defun gnus-agent-regenerate-group (group &optional clean)
2084 (let ((dir (concat (gnus-agent-directory)
2085 (gnus-agent-group-path group) "/"))
2086 (file (gnus-agent-article-name ".overview" group))
2087 n point arts alist header new-alist changed)
2088 (when (file-exists-p dir)
2090 (sort (mapcar (lambda (name) (string-to-int name))
2091 (directory-files dir nil "^[0-9]+$" t))
2093 (gnus-make-directory (nnheader-translate-file-chars
2094 (file-name-directory file) t))
2095 (mm-with-unibyte-buffer
2096 (if (file-exists-p file)
2097 (let ((nnheader-file-coding-system
2098 gnus-agent-file-coding-system))
2099 (nnheader-insert-file-contents file)))
2100 (goto-char (point-min))
2102 (while (not (or (eobp) (looking-at "[0-9]")))
2103 (setq point (point))
2105 (delete-region point (point)))
2107 (setq n (read (current-buffer)))
2108 (when (and arts (> n (car arts)))
2110 (while (and arts (> n (car arts)))
2111 (message "Regenerating NOV %s %d..." group (car arts))
2112 (mm-with-unibyte-buffer
2113 (nnheader-insert-file-contents
2114 (concat dir (number-to-string (car arts))))
2115 (nnheader-remove-body)
2116 (setq header (nnheader-parse-naked-head)))
2117 (mail-header-set-number header (car arts))
2118 (nnheader-insert-nov header)
2120 (push (cons (car arts) t) alist)
2122 (if (and arts (= n (car arts)))
2124 (push (cons n t) alist)
2126 (push (cons n nil) alist))
2129 (let ((coding-system-for-write gnus-agent-file-coding-system))
2130 (write-region (point-min) (point-max) file nil 'silent))))
2131 (setq gnus-agent-article-alist nil)
2133 (gnus-agent-load-alist group))
2134 (setq alist (sort alist 'car-less-than-car))
2135 (setq gnus-agent-article-alist (sort gnus-agent-article-alist
2136 'car-less-than-car))
2137 (while (and alist gnus-agent-article-alist)
2139 ((< (caar alist) (caar gnus-agent-article-alist))
2140 (push (pop alist) new-alist))
2141 ((> (caar alist) (caar gnus-agent-article-alist))
2142 (push (list (car (pop gnus-agent-article-alist))) new-alist))
2144 (pop gnus-agent-article-alist)
2145 (while (and gnus-agent-article-alist
2146 (= (caar alist) (caar gnus-agent-article-alist)))
2147 (pop gnus-agent-article-alist))
2148 (push (pop alist) new-alist))))
2150 (push (pop alist) new-alist))
2151 (while gnus-agent-article-alist
2152 (push (list (car (pop gnus-agent-article-alist))) new-alist))
2153 (setq gnus-agent-article-alist (nreverse new-alist))
2154 (gnus-agent-save-alist group)))
2156 (defun gnus-agent-regenerate-history (group article)
2157 (let ((file (concat (gnus-agent-directory)
2158 (gnus-agent-group-path group) "/"
2159 (number-to-string article))) id)
2160 (mm-with-unibyte-buffer
2161 (nnheader-insert-file-contents file)
2162 (message-narrow-to-head)
2163 (goto-char (point-min))
2164 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
2165 (setq id "No-Message-ID-in-article")
2166 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
2167 (gnus-agent-enter-history
2168 id (list (cons group article))
2169 (time-to-days (nth 5 (file-attributes file)))))))
2172 (defun gnus-agent-regenerate (&optional clean)
2173 "Regenerate all agent covered files.
2174 If CLEAN, don't read existing active and agentview files."
2176 (message "Regenerating Gnus agent files...")
2177 (dolist (gnus-command-method gnus-agent-covered-methods)
2178 (let ((active-file (gnus-agent-lib-file "active"))
2179 history-hashtb active-hashtb active-changed
2180 history-changed point)
2181 (gnus-make-directory (file-name-directory active-file))
2183 (setq active-hashtb (gnus-make-hashtable 1000))
2184 (mm-with-unibyte-buffer
2185 (if (file-exists-p active-file)
2186 (let ((nnheader-file-coding-system
2187 gnus-agent-file-coding-system))
2188 (nnheader-insert-file-contents active-file))
2189 (setq active-changed t))
2190 (gnus-active-to-gnus-format
2191 nil (setq active-hashtb
2192 (gnus-make-hashtable
2193 (count-lines (point-min) (point-max)))))))
2194 (gnus-agent-open-history)
2195 (setq history-hashtb (gnus-make-hashtable 1000))
2196 (with-current-buffer
2197 (setq gnus-agent-current-history (gnus-agent-history-buffer))
2198 (goto-char (point-min))
2202 "\\([^\t\n]+\\)\t[0-9]+\t\\([^ \n]+\\) \\([0-9]+\\)")
2204 (unless (string= (match-string 1)
2205 "last-header-fetched-for-session")
2206 (gnus-sethash (match-string 2)
2207 (cons (string-to-number (match-string 3))
2208 (gnus-gethash-safe (match-string 2)
2212 (setq point (point))
2214 (delete-region point (point))
2215 (setq history-changed t))))
2216 (dolist (group (gnus-groups-from-server gnus-command-method))
2217 (gnus-agent-regenerate-group group clean)
2218 (let ((min (or (caar gnus-agent-article-alist) 1))
2219 (max (or (caar (last gnus-agent-article-alist)) 0))
2220 (active (gnus-gethash-safe (gnus-group-real-name group)
2224 (setq active (cons min max)
2226 (gnus-sethash group active active-hashtb))
2227 (when (> (car active) min)
2229 (setq active-changed t))
2230 (when (< (cdr active) max)
2232 (setq active-changed t))))
2233 (let ((arts (sort (gnus-gethash-safe group history-hashtb) '<))
2235 (gnus-sethash group arts history-hashtb)
2236 (while (and arts gnus-agent-article-alist)
2238 ((> (car arts) (caar gnus-agent-article-alist))
2239 (when (cdar gnus-agent-article-alist)
2240 (gnus-agent-regenerate-history
2241 group (caar gnus-agent-article-alist))
2242 (setq history-changed t))
2243 (setq n (car (pop gnus-agent-article-alist)))
2244 (while (and gnus-agent-article-alist
2245 (= n (caar gnus-agent-article-alist)))
2246 (pop gnus-agent-article-alist)))
2247 ((< (car arts) (caar gnus-agent-article-alist))
2249 (while (and arts (= n (car arts)))
2252 (setq n (car (pop gnus-agent-article-alist)))
2253 (while (and gnus-agent-article-alist
2254 (= n (caar gnus-agent-article-alist)))
2255 (pop gnus-agent-article-alist))
2257 (while (and arts (= n (car arts)))
2259 (while gnus-agent-article-alist
2260 (when (cdar gnus-agent-article-alist)
2261 (gnus-agent-regenerate-history
2262 group (caar gnus-agent-article-alist))
2263 (setq history-changed t))
2264 (pop gnus-agent-article-alist))))
2265 (when history-changed
2266 (message "Regenerate the history file of %s:%s"
2267 (car gnus-command-method)
2268 (cadr gnus-command-method))
2269 (gnus-agent-save-history))
2270 (gnus-agent-close-history)
2271 (when active-changed
2272 (message "Regenerate %s" active-file)
2273 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
2274 (gnus-write-active-file active-file active-hashtb)))))
2275 (message "Regenerating Gnus agent files...done"))
2277 (defun gnus-agent-go-online (&optional force)
2278 "Switch servers into online status."
2279 (interactive (list t))
2280 (dolist (server gnus-opened-servers)
2281 (when (eq (nth 1 server) 'offline)
2282 (if (if (eq force 'ask)
2284 (format "Switch %s:%s into online status? "
2285 (caar server) (cadar server)))
2287 (setcar (nthcdr 1 server) 'close)))))
2289 (defun gnus-agent-toggle-group-plugged (group)
2290 "Toggle the status of the server of the current group."
2291 (interactive (list (gnus-group-group-name)))
2292 (let* ((method (gnus-find-method-for-group group))
2293 (status (cadr (assoc method gnus-opened-servers))))
2294 (if (eq status 'offline)
2295 (gnus-server-set-status method 'closed)
2296 (gnus-close-server method)
2297 (gnus-server-set-status method 'offline))
2298 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
2299 (if (eq status 'offline) 'offline 'online)
2300 (if (eq status 'offline) 'online 'offline))))
2302 (provide 'gnus-agent)
2304 ;;; gnus-agent.el ends here