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)
1223 "Load the article-state alist for GROUP."
1224 (setq gnus-agent-article-alist
1225 (gnus-cache-file-contents
1226 (gnus-agent-article-name ".agentview" group)
1227 'gnus-agent-file-loading-cache
1228 'gnus-agent-read-file)))
1230 ;; Why do we have to create the directory for the .fetched files (see
1231 ;; function gnus-agent-save-fetched-headers below) but not for the
1232 ;; .agentview files?
1233 (defun gnus-agent-save-alist (group &optional articles state)
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 (gnus-agent-article-name ".agentview" group)
1253 (princ gnus-agent-article-alist (current-buffer))
1256 (defun gnus-agent-load-fetched-headers (group)
1257 "Load ranges of fetched headers for GROUP."
1258 (setq gnus-agent-fetched-headers
1259 (gnus-cache-file-contents
1260 (gnus-agent-article-name ".fetched" group)
1261 'gnus-agent-file-header-cache
1262 'gnus-agent-read-file)))
1264 (defun gnus-agent-save-fetched-headers (group range)
1265 "Save ranges of fetched headers for GROUP.
1266 This range includes nonexisting articles."
1267 (let ((file-name-coding-system nnmail-pathname-coding-system)
1268 (fetched-file (gnus-agent-article-name ".fetched" group))
1269 print-level print-length)
1270 (setq gnus-agent-fetched-headers range)
1271 (unless (file-exists-p (file-name-directory fetched-file))
1272 (make-directory (file-name-directory fetched-file) t))
1273 (with-temp-file fetched-file
1274 (princ gnus-agent-fetched-headers (current-buffer))
1277 (defun gnus-agent-article-name (article group)
1278 (expand-file-name (if (stringp article) article (string-to-number article))
1279 (file-name-as-directory
1280 (expand-file-name (gnus-agent-group-path group)
1281 (gnus-agent-directory)))))
1283 (defun gnus-agent-batch-confirmation (msg)
1284 "Show error message and return t."
1285 (gnus-message 1 msg)
1289 (defun gnus-agent-batch-fetch ()
1290 "Start Gnus and fetch session."
1293 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1294 (gnus-agent-fetch-session))
1297 (defun gnus-agent-fetch-session ()
1298 "Fetch all articles and headers that are eligible for fetching."
1300 (unless gnus-agent-covered-methods
1301 (error "No servers are covered by the Gnus agent"))
1302 (unless gnus-plugged
1303 (error "Can't fetch articles while Gnus is unplugged"))
1304 (let ((methods gnus-agent-covered-methods)
1305 groups group gnus-command-method)
1310 (setq gnus-command-method (car methods))
1311 (when (and (or (gnus-server-opened gnus-command-method)
1312 (gnus-open-server gnus-command-method))
1313 (gnus-online gnus-command-method))
1314 (setq groups (gnus-groups-from-server (car methods)))
1315 (gnus-agent-with-fetch
1316 (while (setq group (pop groups))
1317 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1318 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1320 (unless (funcall gnus-agent-confirmation-function
1321 (format "Error (%s). Continue? " (cadr err)))
1322 (error "Cannot fetch articles into the Gnus agent")))
1324 (unless (funcall gnus-agent-confirmation-function
1325 (format "Quit fetching session (%s). Continue? "
1327 (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1329 (run-hooks 'gnus-agent-fetch-hook)
1330 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1332 (defun gnus-agent-fetch-group-1 (group method)
1334 (let ((gnus-command-method method)
1335 (gnus-newsgroup-name group)
1336 gnus-newsgroup-dependencies gnus-newsgroup-headers
1337 gnus-newsgroup-scored gnus-headers gnus-score
1338 gnus-use-cache articles arts
1339 category predicate info marks score-param
1340 (gnus-summary-expunge-below gnus-summary-expunge-below)
1341 (gnus-summary-mark-below gnus-summary-mark-below)
1342 (gnus-orphan-score gnus-orphan-score)
1343 ;; Maybe some other gnus-summary local variables should also
1346 (unless (gnus-check-group group)
1347 (error "Can't open server for %s" group))
1349 (when (and (or (gnus-active group)
1350 (gnus-activate-group group))
1351 (setq articles (gnus-agent-fetch-headers group))
1352 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1353 ;; Parse them and see which articles we want to fetch.
1354 (setq gnus-newsgroup-dependencies
1355 (make-vector (length articles) 0))
1356 (setq gnus-newsgroup-headers
1357 (gnus-get-newsgroup-headers-xover articles nil nil
1359 ;; Some articles may not exist, so update `articles'
1360 ;; from what was actually found. -- kai
1362 (mapcar (lambda (x) (aref x 0))
1363 gnus-newsgroup-headers))
1364 ;; `gnus-agent-overview-buffer' may be killed for
1365 ;; timeout reason. If so, recreate it.
1366 (gnus-agent-create-buffer)))
1367 (setq category (gnus-group-category group))
1370 (or (gnus-group-find-parameter group 'agent-predicate t)
1372 (if (memq predicate '(gnus-agent-true gnus-agent-false))
1373 ;; Simple implementation
1374 (setq arts (and (eq predicate 'gnus-agent-true) articles))
1377 (or (gnus-group-get-parameter group 'agent-score t)
1379 ;; Translate score-param into real one
1382 ((eq score-param 'file)
1383 (setq score-param (gnus-all-score-files group)))
1384 ((stringp (car score-param)))
1386 (setq score-param (list (list score-param)))))
1388 (gnus-score-headers score-param))
1389 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1391 (or (cdr (assq (mail-header-number gnus-headers)
1392 gnus-newsgroup-scored))
1393 gnus-summary-default-score))
1394 (when (funcall predicate)
1395 (push (mail-header-number gnus-headers)
1397 ;; Fetch the articles.
1399 (gnus-agent-fetch-articles group arts)))
1400 ;; Perhaps we have some additional articles to fetch.
1401 (dolist (mark gnus-agent-download-marks)
1402 (setq arts (assq mark (gnus-info-marks
1403 (setq info (gnus-get-info group)))))
1405 (gnus-message 8 "Agent is downloading marked articles...")
1406 (gnus-agent-fetch-articles
1407 group (gnus-uncompress-range (cdr arts)))
1408 (when (eq mark 'download)
1409 (setq marks (delq arts (gnus-info-marks info)))
1410 (gnus-info-set-marks info marks)
1412 (concat "(gnus-group-set-info '"
1413 (gnus-prin1-to-string info)
1417 ;;; Agent Category Mode
1420 (defvar gnus-category-mode-hook nil
1421 "Hook run in `gnus-category-mode' buffers.")
1423 (defvar gnus-category-line-format " %(%20c%): %g\n"
1424 "Format of category lines.
1426 Valid specifiers include:
1427 %c Topic name (string)
1428 %g The number of groups in the topic (integer)
1430 General format specifiers can also be used. See Info node
1431 `(gnus)Formatting Variables'.")
1433 (defvar gnus-category-mode-line-format "Gnus: %%b"
1434 "The format specification for the category mode line.")
1436 (defvar gnus-agent-short-article 100
1437 "Articles that have fewer lines than this are short.")
1439 (defvar gnus-agent-long-article 200
1440 "Articles that have more lines than this are long.")
1442 (defvar gnus-agent-low-score 0
1443 "Articles that have a score lower than this have a low score.")
1445 (defvar gnus-agent-high-score 0
1446 "Articles that have a score higher than this have a high score.")
1449 ;;; Internal variables.
1451 (defvar gnus-category-buffer "*Agent Category*")
1453 (defvar gnus-category-line-format-alist
1454 `((?c gnus-tmp-name ?s)
1455 (?g gnus-tmp-groups ?d)))
1457 (defvar gnus-category-mode-line-format-alist
1458 `((?u user-defined ?s)))
1460 (defvar gnus-category-line-format-spec nil)
1461 (defvar gnus-category-mode-line-format-spec nil)
1463 (defvar gnus-category-mode-map nil)
1464 (put 'gnus-category-mode 'mode-class 'special)
1466 (unless gnus-category-mode-map
1467 (setq gnus-category-mode-map (make-sparse-keymap))
1468 (suppress-keymap gnus-category-mode-map)
1470 (gnus-define-keys gnus-category-mode-map
1471 "q" gnus-category-exit
1472 "k" gnus-category-kill
1473 "c" gnus-category-copy
1474 "a" gnus-category-add
1475 "p" gnus-category-edit-predicate
1476 "g" gnus-category-edit-groups
1477 "s" gnus-category-edit-score
1478 "l" gnus-category-list
1480 "\C-c\C-i" gnus-info-find-node
1481 "\C-c\C-b" gnus-bug))
1483 (defvar gnus-category-menu-hook nil
1484 "*Hook run after the creation of the menu.")
1486 (defun gnus-category-make-menu-bar ()
1487 (gnus-turn-off-edit-menu 'category)
1488 (unless (boundp 'gnus-category-menu)
1490 gnus-category-menu gnus-category-mode-map ""
1492 ["Add" gnus-category-add t]
1493 ["Kill" gnus-category-kill t]
1494 ["Copy" gnus-category-copy t]
1495 ["Edit predicate" gnus-category-edit-predicate t]
1496 ["Edit score" gnus-category-edit-score t]
1497 ["Edit groups" gnus-category-edit-groups t]
1498 ["Exit" gnus-category-exit t]))
1500 (gnus-run-hooks 'gnus-category-menu-hook)))
1502 (defun gnus-category-mode ()
1503 "Major mode for listing and editing agent categories.
1505 All normal editing commands are switched off.
1506 \\<gnus-category-mode-map>
1507 For more in-depth information on this mode, read the manual
1508 (`\\[gnus-info-find-node]').
1510 The following commands are available:
1512 \\{gnus-category-mode-map}"
1514 (when (gnus-visual-p 'category-menu 'menu)
1515 (gnus-category-make-menu-bar))
1516 (kill-all-local-variables)
1517 (gnus-simplify-mode-line)
1518 (setq major-mode 'gnus-category-mode)
1519 (setq mode-name "Category")
1520 (gnus-set-default-directory)
1521 (setq mode-line-process nil)
1522 (use-local-map gnus-category-mode-map)
1523 (buffer-disable-undo)
1524 (setq truncate-lines t)
1525 (setq buffer-read-only t)
1526 (gnus-run-hooks 'gnus-category-mode-hook))
1528 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1530 (defun gnus-category-insert-line (category)
1531 (let* ((gnus-tmp-name (format "%s" (car category)))
1532 (gnus-tmp-groups (length (cadddr category))))
1534 (gnus-add-text-properties
1538 (eval gnus-category-line-format-spec))
1539 (list 'gnus-category gnus-tmp-name))))
1541 (defun gnus-enter-category-buffer ()
1542 "Go to the Category buffer."
1544 (gnus-category-setup-buffer)
1545 (gnus-configure-windows 'category)
1546 (gnus-category-prepare))
1548 (defun gnus-category-setup-buffer ()
1549 (unless (get-buffer gnus-category-buffer)
1551 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1552 (gnus-category-mode))))
1554 (defun gnus-category-prepare ()
1555 (gnus-set-format 'category-mode)
1556 (gnus-set-format 'category t)
1557 (let ((alist gnus-category-alist)
1558 (buffer-read-only nil))
1561 (gnus-category-insert-line (pop alist)))
1562 (goto-char (point-min))
1563 (gnus-category-position-point)))
1565 (defun gnus-category-name ()
1566 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
1567 (error "No category on the current line")))
1569 (defun gnus-category-read ()
1570 "Read the category alist."
1571 (setq gnus-category-alist
1572 (or (gnus-agent-read-file
1573 (nnheader-concat gnus-agent-directory "lib/categories"))
1574 (list (list 'default 'short nil nil)))))
1576 (defun gnus-category-write ()
1577 "Write the category alist."
1578 (setq gnus-category-predicate-cache nil
1579 gnus-category-group-cache nil)
1580 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1581 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1582 (prin1 gnus-category-alist (current-buffer))))
1584 (defun gnus-category-edit-predicate (category)
1585 "Edit the predicate for CATEGORY."
1586 (interactive (list (gnus-category-name)))
1587 (let ((info (assq category gnus-category-alist)))
1589 (cadr info) (format "Editing the predicate for category %s" category)
1590 `(lambda (predicate)
1591 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1592 (gnus-category-write)
1593 (gnus-category-list)))))
1595 (defun gnus-category-edit-score (category)
1596 "Edit the score expression for CATEGORY."
1597 (interactive (list (gnus-category-name)))
1598 (let ((info (assq category gnus-category-alist)))
1601 (format "Editing the score expression for category %s" category)
1603 (setcar (cddr (assq ',category gnus-category-alist)) groups)
1604 (gnus-category-write)
1605 (gnus-category-list)))))
1607 (defun gnus-category-edit-groups (category)
1608 "Edit the group list for CATEGORY."
1609 (interactive (list (gnus-category-name)))
1610 (let ((info (assq category gnus-category-alist)))
1612 (cadddr info) (format "Editing the group list for category %s" category)
1614 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1615 (gnus-category-write)
1616 (gnus-category-list)))))
1618 (defun gnus-category-kill (category)
1619 "Kill the current category."
1620 (interactive (list (gnus-category-name)))
1621 (let ((info (assq category gnus-category-alist))
1622 (buffer-read-only nil))
1624 (setq gnus-category-alist (delq info gnus-category-alist))
1625 (gnus-category-write)))
1627 (defun gnus-category-copy (category to)
1628 "Copy the current category."
1629 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1630 (let ((info (assq category gnus-category-alist)))
1631 (push (list to (gnus-copy-sequence (cadr info))
1632 (gnus-copy-sequence (caddr info)) nil)
1633 gnus-category-alist)
1634 (gnus-category-write)
1635 (gnus-category-list)))
1637 (defun gnus-category-add (category)
1638 "Create a new category."
1639 (interactive "SCategory name: ")
1640 (when (assq category gnus-category-alist)
1641 (error "Category %s already exists" category))
1642 (push (list category 'false nil nil)
1643 gnus-category-alist)
1644 (gnus-category-write)
1645 (gnus-category-list))
1647 (defun gnus-category-list ()
1648 "List all categories."
1650 (gnus-category-prepare))
1652 (defun gnus-category-exit ()
1653 "Return to the group buffer."
1655 (kill-buffer (current-buffer))
1656 (gnus-configure-windows 'group t))
1658 ;; To avoid having 8-bit characters in the source file.
1659 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1661 (defvar gnus-category-predicate-alist
1662 '((spam . gnus-agent-spam-p)
1663 (short . gnus-agent-short-p)
1664 (long . gnus-agent-long-p)
1665 (low . gnus-agent-low-scored-p)
1666 (high . gnus-agent-high-scored-p)
1667 (read . gnus-agent-read-p)
1668 (true . gnus-agent-true)
1669 (false . gnus-agent-false))
1670 "Mapping from short score predicate symbols to predicate functions.")
1672 (defun gnus-agent-spam-p ()
1673 "Say whether an article is spam or not."
1674 (unless gnus-agent-spam-hashtb
1675 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1676 (if (not (equal (mail-header-references gnus-headers) ""))
1678 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1680 (gnus-gethash string gnus-agent-spam-hashtb)
1681 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1683 (defun gnus-agent-short-p ()
1684 "Say whether an article is short or not."
1685 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1687 (defun gnus-agent-long-p ()
1688 "Say whether an article is long or not."
1689 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1691 (defun gnus-agent-low-scored-p ()
1692 "Say whether an article has a low score or not."
1693 (< gnus-score gnus-agent-low-score))
1695 (defun gnus-agent-high-scored-p ()
1696 "Say whether an article has a high score or not."
1697 (> gnus-score gnus-agent-high-score))
1699 (defun gnus-agent-read-p ()
1700 "Say whether an article is read or not."
1701 (gnus-member-of-range (mail-header-number gnus-headers)
1702 (gnus-info-read (gnus-get-info gnus-newsgroup-name))))
1704 (defun gnus-category-make-function (cat)
1705 "Make a function from category CAT."
1706 (let ((func (gnus-category-make-function-1 cat)))
1707 (if (and (= (length func) 1)
1708 (symbolp (car func)))
1710 (gnus-byte-compile `(lambda () ,func)))))
1712 (defun gnus-agent-true ()
1716 (defun gnus-agent-false ()
1720 (defun gnus-category-make-function-1 (cat)
1721 "Make a function from category CAT."
1723 ;; Functions are just returned as is.
1725 (gnus-functionp cat))
1726 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1728 ;; More complex category.
1731 ((memq (car cat) '(& and))
1733 ((memq (car cat) '(| or))
1735 ((memq (car cat) gnus-category-not)
1737 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1739 (error "Unknown category type: %s" cat))))
1741 (defun gnus-get-predicate (predicate)
1742 "Return the predicate for CATEGORY."
1743 (or (cdr (assoc predicate gnus-category-predicate-cache))
1744 (let ((func (gnus-category-make-function predicate)))
1745 (setq gnus-category-predicate-cache
1746 (nconc gnus-category-predicate-cache
1747 (list (cons predicate func))))
1750 (defun gnus-predicate-implies-unread (predicate)
1751 "Say whether PREDICATE implies unread articles only.
1752 It is okay to miss some cases, but there must be no false positives.
1753 That is, if this function returns true, then indeed the predicate must
1754 return only unread articles."
1755 ;; Todo: make this work in more cases.
1756 (equal predicate '(not read)))
1758 (defun gnus-group-category (group)
1759 "Return the category GROUP belongs to."
1760 (unless gnus-category-group-cache
1761 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1762 (let ((cs gnus-category-alist)
1764 (while (setq cat (pop cs))
1765 (setq groups (cadddr cat))
1767 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1768 (or (gnus-gethash group gnus-category-group-cache)
1769 (assq 'default gnus-category-alist)))
1771 (defun gnus-agent-expire (&optional articles group force)
1772 "Expire all old articles.
1773 If you want to force expiring of certain articles, this function can
1774 take ARTICLES, GROUP and FORCE parameters as well. Setting ARTICLES
1775 and GROUP without FORCE is not supported."
1777 (let ((methods (if group
1778 (list (gnus-find-method-for-group group))
1779 gnus-agent-covered-methods))
1780 (day (if (numberp gnus-agent-expire-days)
1781 (- (time-to-days (current-time)) gnus-agent-expire-days)
1783 (current-day (time-to-days (current-time)))
1784 gnus-command-method sym arts pos
1785 history overview file histories elem art nov-file low info
1786 unreads marked article orig lowest highest found days)
1788 (setq overview (gnus-get-buffer-create " *expire overview*"))
1789 (while (setq gnus-command-method (pop methods))
1790 (when (file-exists-p (gnus-agent-lib-file "active"))
1792 (nnheader-insert-file-contents (gnus-agent-lib-file "active"))
1793 (gnus-active-to-gnus-format
1795 (setq orig (gnus-make-hashtable
1796 (count-lines (point-min) (point-max))))))
1797 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1798 (gnus-agent-open-history)
1800 (setq gnus-agent-current-history
1801 (setq history (gnus-agent-history-buffer))))
1802 (goto-char (point-min))
1803 (if (and articles group force) ;; point usless without art+group
1804 (while (setq article (pop articles))
1805 ;; try to find history entries for articles
1806 (goto-char (point-min))
1807 (if (re-search-forward
1808 (concat "^[^\t]*\t[^\t]*\t\(.* ?\)"
1809 (format "%S" (gnus-group-prefixed-name
1810 group gnus-command-method))
1812 (number-to-string article)
1817 (setq sym (let ((obarray expiry-hashtb) s)
1820 (set sym (cons (cons article pos)
1821 (symbol-value sym)))
1822 (set sym (list (cons article pos)))))
1823 ;; go through history file to find eligble articles
1824 (when (> (buffer-size) 1)
1825 (goto-char (point-min))
1827 (skip-chars-forward "^\t")
1828 (if (let ((fetch-date (read (current-buffer))))
1829 (if (numberp fetch-date)
1830 ;; We now have the arrival day, so we see
1831 ;; whether it's old enough to be expired.
1834 (skip-chars-forward "\t")
1836 days gnus-agent-expire-days)
1837 (while (and (not found)
1839 (when (looking-at (caar days))
1840 (setq found (cadar days)))
1842 (> fetch-date (- current-day found)))
1843 ;; History file is corrupted.
1846 (format "File %s is corrupted!"
1847 (gnus-agent-lib-file "history")))
1851 ;; New article; we don't expire it.
1853 ;; Old article. Schedule it for possible nuking.
1855 (setq sym (let ((obarray expiry-hashtb) s)
1856 (setq s (read (current-buffer)))
1857 (if (stringp s) (intern s) s)))
1859 (set sym (cons (cons (read (current-buffer)) (point))
1860 (symbol-value sym)))
1861 (set sym (list (cons (read (current-buffer))
1863 (skip-chars-forward " "))
1864 (forward-line 1)))))
1865 ;; We now have all articles that can possibly be expired.
1868 (setq group (symbol-name sym)
1869 arts (sort (symbol-value sym) 'car-less-than-car)
1870 low (car (gnus-active group))
1871 info (gnus-get-info group)
1872 unreads (ignore-errors
1873 (gnus-list-of-unread-articles group))
1875 (gnus-uncompress-range
1876 (cdr (assq 'tick (gnus-info-marks info))))
1877 (gnus-uncompress-range
1879 (gnus-info-marks info)))))
1880 nov-file (gnus-agent-article-name ".overview" group)
1883 (gnus-agent-load-alist group)
1884 (gnus-message 5 "Expiring articles in %s" group)
1885 (set-buffer overview)
1887 (when (file-exists-p nov-file)
1888 (nnheader-insert-file-contents nov-file))
1889 (goto-char (point-min))
1891 (while (setq elem (pop arts))
1892 (setq article (car elem))
1893 (when (or (null low)
1895 gnus-agent-expire-all
1896 (and (not (memq article unreads))
1897 (not (memq article marked)))
1899 ;; Find and nuke the NOV line.
1900 (while (and (not (eobp))
1902 (setq art (read (current-buffer)))))
1904 (if (and (numberp art)
1906 (gnus-agent-article-name
1907 (number-to-string art) group)))
1913 ;; Remove old NOV lines that have no articles.
1914 (gnus-delete-line)))
1919 ;; Nuke the article.
1920 (when (file-exists-p
1921 (setq file (gnus-agent-article-name
1922 (number-to-string article)
1925 ;; Schedule the history line for nuking.
1927 (push (cdr elem) histories))))
1928 (gnus-make-directory (file-name-directory nov-file))
1929 (let ((coding-system-for-write
1930 gnus-agent-file-coding-system))
1931 (write-region (point-min) (point-max) nov-file nil 'silent))
1932 ;; Delete the unwanted entries in the alist.
1933 (setq gnus-agent-article-alist
1934 (sort gnus-agent-article-alist 'car-less-than-car))
1935 (let* ((alist gnus-agent-article-alist)
1936 (prev (cons nil alist))
1940 (<= (caar alist) article))
1941 (if (or (not (cdar alist))
1943 (gnus-agent-article-name
1948 (push (caar alist) expired)
1949 (setcdr prev (setq alist (cdr alist))))
1951 alist (cdr alist))))
1952 (setq gnus-agent-article-alist (cdr first))
1953 (gnus-agent-save-alist group)
1954 ;; Mark all articles up to the first article
1955 ;; in `gnus-agent-article-alist' as read.
1956 (when (and info (caar gnus-agent-article-alist))
1957 (setcar (nthcdr 2 info)
1960 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1961 ;; Maybe everything has been expired from
1962 ;; `gnus-agent-article-alist' and so the above marking as
1963 ;; read could not be conducted, or there are
1964 ;; expired article within the range of the alist.
1967 (or (not (caar gnus-agent-article-alist))
1969 (caar gnus-agent-article-alist))))
1970 (setcar (nthcdr 2 info)
1973 (nreverse expired))))
1975 (concat "(gnus-group-set-info '"
1976 (gnus-prin1-to-string info)
1979 (if (gnus-gethash group orig)
1980 (setcar (gnus-gethash group orig) lowest)
1981 (gnus-sethash group (cons lowest highest) orig))))
1983 (set-buffer history)
1984 (setq histories (nreverse (sort histories '<)))
1986 (goto-char (pop histories))
1988 (gnus-agent-save-history)
1989 (gnus-agent-close-history)
1990 (gnus-write-active-file
1991 (gnus-agent-lib-file "active") orig))
1992 (gnus-message 4 "Expiry...done"))))))
1995 (defun gnus-agent-batch ()
1996 "Start Gnus, send queue and fetch session."
1998 (let ((init-file-user "")
1999 (gnus-always-read-dribble-file t))
2001 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
2002 (gnus-group-send-queue)
2003 (gnus-agent-fetch-session)))
2005 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
2007 (gnus-agent-create-buffer)
2008 (let ((gnus-decode-encoded-word-function 'identity)
2009 (file (gnus-agent-article-name ".overview" group))
2010 cached-articles uncached-articles)
2011 (gnus-make-directory (nnheader-translate-file-chars
2012 (file-name-directory file) t))
2013 (when (file-exists-p file)
2014 (with-current-buffer gnus-agent-overview-buffer
2016 (let ((nnheader-file-coding-system
2017 gnus-agent-file-coding-system))
2018 (nnheader-insert-nov-file file (car articles)))
2019 (nnheader-find-nov-line (car articles))
2021 (when (looking-at "[0-9]")
2022 (push (read (current-buffer)) cached-articles))
2024 (setq cached-articles (nreverse cached-articles))))
2025 (if (setq uncached-articles
2026 (gnus-sorted-difference articles cached-articles))
2028 (set-buffer nntp-server-buffer)
2030 (let (gnus-agent-cache)
2032 (gnus-retrieve-headers
2033 uncached-articles group fetch-old))
2034 (nnvirtual-convert-headers)))
2035 (set-buffer gnus-agent-overview-buffer)
2037 (set-buffer nntp-server-buffer)
2038 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
2039 (when (and uncached-articles (file-exists-p file))
2040 (gnus-agent-braid-nov group uncached-articles file))
2041 (set-buffer nntp-server-buffer)
2042 (let ((coding-system-for-write
2043 gnus-agent-file-coding-system))
2044 (write-region (point-min) (point-max) file nil 'silent))
2045 (gnus-agent-load-alist group)
2046 (gnus-agent-save-alist group uncached-articles nil)
2047 (gnus-agent-open-history)
2048 (setq gnus-agent-current-history (gnus-agent-history-buffer))
2049 (gnus-agent-enter-history
2050 "last-header-fetched-for-session"
2051 (list (cons group (nth (- (length articles) 1) articles)))
2052 (time-to-days (current-time)))
2053 (gnus-agent-save-history))
2054 (set-buffer nntp-server-buffer)
2056 (insert-buffer-substring gnus-agent-overview-buffer)))
2058 (not (numberp fetch-old)))
2059 t ; Don't remove anything.
2060 (nnheader-nov-delete-outside-range
2061 (if fetch-old (max 1 (- (car articles) fetch-old))
2063 (car (last articles)))
2067 (defun gnus-agent-request-article (article group)
2068 "Retrieve ARTICLE in GROUP from the agent cache."
2069 (let* ((gnus-command-method (gnus-find-method-for-group group))
2071 (gnus-agent-directory)
2072 (gnus-agent-group-path group) "/"
2073 (number-to-string article)))
2074 (buffer-read-only nil))
2075 (when (and (file-exists-p file)
2076 (> (nth 7 (file-attributes file)) 0))
2078 (gnus-kill-all-overlays)
2079 (let ((coding-system-for-read gnus-cache-coding-system))
2080 (insert-file-contents file))
2083 (defun gnus-agent-regenerate-group (group &optional clean)
2085 (let ((dir (concat (gnus-agent-directory)
2086 (gnus-agent-group-path group) "/"))
2087 (file (gnus-agent-article-name ".overview" group))
2088 n point arts alist header new-alist changed)
2089 (when (file-exists-p dir)
2091 (sort (mapcar (lambda (name) (string-to-int name))
2092 (directory-files dir nil "^[0-9]+$" t))
2094 (gnus-make-directory (nnheader-translate-file-chars
2095 (file-name-directory file) t))
2096 (mm-with-unibyte-buffer
2097 (if (file-exists-p file)
2098 (let ((nnheader-file-coding-system
2099 gnus-agent-file-coding-system))
2100 (nnheader-insert-file-contents file)))
2101 (goto-char (point-min))
2103 (while (not (or (eobp) (looking-at "[0-9]")))
2104 (setq point (point))
2106 (delete-region point (point)))
2108 (setq n (read (current-buffer)))
2109 (when (and arts (> n (car arts)))
2111 (while (and arts (> n (car arts)))
2112 (message "Regenerating NOV %s %d..." group (car arts))
2113 (mm-with-unibyte-buffer
2114 (nnheader-insert-file-contents
2115 (concat dir (number-to-string (car arts))))
2116 (nnheader-remove-body)
2117 (setq header (nnheader-parse-naked-head)))
2118 (mail-header-set-number header (car arts))
2119 (nnheader-insert-nov header)
2121 (push (cons (car arts) t) alist)
2123 (if (and arts (= n (car arts)))
2125 (push (cons n t) alist)
2127 (push (cons n nil) alist))
2130 (let ((coding-system-for-write gnus-agent-file-coding-system))
2131 (write-region (point-min) (point-max) file nil 'silent))))
2132 (setq gnus-agent-article-alist nil)
2134 (gnus-agent-load-alist group))
2135 (setq alist (sort alist 'car-less-than-car))
2136 (setq gnus-agent-article-alist (sort gnus-agent-article-alist
2137 'car-less-than-car))
2138 (while (and alist gnus-agent-article-alist)
2140 ((< (caar alist) (caar gnus-agent-article-alist))
2141 (push (pop alist) new-alist))
2142 ((> (caar alist) (caar gnus-agent-article-alist))
2143 (push (list (car (pop gnus-agent-article-alist))) new-alist))
2145 (pop gnus-agent-article-alist)
2146 (while (and gnus-agent-article-alist
2147 (= (caar alist) (caar gnus-agent-article-alist)))
2148 (pop gnus-agent-article-alist))
2149 (push (pop alist) new-alist))))
2151 (push (pop alist) new-alist))
2152 (while gnus-agent-article-alist
2153 (push (list (car (pop gnus-agent-article-alist))) new-alist))
2154 (setq gnus-agent-article-alist (nreverse new-alist))
2155 (gnus-agent-save-alist group)))
2157 (defun gnus-agent-regenerate-history (group article)
2158 (let ((file (concat (gnus-agent-directory)
2159 (gnus-agent-group-path group) "/"
2160 (number-to-string article))) id)
2161 (mm-with-unibyte-buffer
2162 (nnheader-insert-file-contents file)
2163 (message-narrow-to-head)
2164 (goto-char (point-min))
2165 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
2166 (setq id "No-Message-ID-in-article")
2167 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
2168 (gnus-agent-enter-history
2169 id (list (cons group article))
2170 (time-to-days (nth 5 (file-attributes file)))))))
2173 (defun gnus-agent-regenerate (&optional clean)
2174 "Regenerate all agent covered files.
2175 If CLEAN, don't read existing active and agentview files."
2177 (message "Regenerating Gnus agent files...")
2178 (dolist (gnus-command-method gnus-agent-covered-methods)
2179 (let ((active-file (gnus-agent-lib-file "active"))
2180 history-hashtb active-hashtb active-changed
2181 history-changed point)
2182 (gnus-make-directory (file-name-directory active-file))
2184 (setq active-hashtb (gnus-make-hashtable 1000))
2185 (mm-with-unibyte-buffer
2186 (if (file-exists-p active-file)
2187 (let ((nnheader-file-coding-system
2188 gnus-agent-file-coding-system))
2189 (nnheader-insert-file-contents active-file))
2190 (setq active-changed t))
2191 (gnus-active-to-gnus-format
2192 nil (setq active-hashtb
2193 (gnus-make-hashtable
2194 (count-lines (point-min) (point-max)))))))
2195 (gnus-agent-open-history)
2196 (setq history-hashtb (gnus-make-hashtable 1000))
2197 (with-current-buffer
2198 (setq gnus-agent-current-history (gnus-agent-history-buffer))
2199 (goto-char (point-min))
2203 "\\([^\t\n]+\\)\t[0-9]+\t\\([^ \n]+\\) \\([0-9]+\\)")
2205 (unless (string= (match-string 1)
2206 "last-header-fetched-for-session")
2207 (gnus-sethash (match-string 2)
2208 (cons (string-to-number (match-string 3))
2209 (gnus-gethash-safe (match-string 2)
2213 (setq point (point))
2215 (delete-region point (point))
2216 (setq history-changed t))))
2217 (dolist (group (gnus-groups-from-server gnus-command-method))
2218 (gnus-agent-regenerate-group group clean)
2219 (let ((min (or (caar gnus-agent-article-alist) 1))
2220 (max (or (caar (last gnus-agent-article-alist)) 0))
2221 (active (gnus-gethash-safe (gnus-group-real-name group)
2225 (setq active (cons min max)
2227 (gnus-sethash group active active-hashtb))
2228 (when (> (car active) min)
2230 (setq active-changed t))
2231 (when (< (cdr active) max)
2233 (setq active-changed t))))
2234 (let ((arts (sort (gnus-gethash-safe group history-hashtb) '<))
2236 (gnus-sethash group arts history-hashtb)
2237 (while (and arts gnus-agent-article-alist)
2239 ((> (car arts) (caar gnus-agent-article-alist))
2240 (when (cdar gnus-agent-article-alist)
2241 (gnus-agent-regenerate-history
2242 group (caar gnus-agent-article-alist))
2243 (setq history-changed t))
2244 (setq n (car (pop gnus-agent-article-alist)))
2245 (while (and gnus-agent-article-alist
2246 (= n (caar gnus-agent-article-alist)))
2247 (pop gnus-agent-article-alist)))
2248 ((< (car arts) (caar gnus-agent-article-alist))
2250 (while (and arts (= n (car arts)))
2253 (setq n (car (pop gnus-agent-article-alist)))
2254 (while (and gnus-agent-article-alist
2255 (= n (caar gnus-agent-article-alist)))
2256 (pop gnus-agent-article-alist))
2258 (while (and arts (= n (car arts)))
2260 (while gnus-agent-article-alist
2261 (when (cdar gnus-agent-article-alist)
2262 (gnus-agent-regenerate-history
2263 group (caar gnus-agent-article-alist))
2264 (setq history-changed t))
2265 (pop gnus-agent-article-alist))))
2266 (when history-changed
2267 (message "Regenerate the history file of %s:%s"
2268 (car gnus-command-method)
2269 (cadr gnus-command-method))
2270 (gnus-agent-save-history))
2271 (gnus-agent-close-history)
2272 (when active-changed
2273 (message "Regenerate %s" active-file)
2274 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
2275 (gnus-write-active-file active-file active-hashtb)))))
2276 (message "Regenerating Gnus agent files...done"))
2278 (defun gnus-agent-go-online (&optional force)
2279 "Switch servers into online status."
2280 (interactive (list t))
2281 (dolist (server gnus-opened-servers)
2282 (when (eq (nth 1 server) 'offline)
2283 (if (if (eq force 'ask)
2285 (format "Switch %s:%s into online status? "
2286 (caar server) (cadar server)))
2288 (setcar (nthcdr 1 server) 'close)))))
2290 (defun gnus-agent-toggle-group-plugged (group)
2291 "Toggle the status of the server of the current group."
2292 (interactive (list (gnus-group-group-name)))
2293 (let* ((method (gnus-find-method-for-group group))
2294 (status (cadr (assoc method gnus-opened-servers))))
2295 (if (eq status 'offline)
2296 (gnus-server-set-status method 'closed)
2297 (gnus-close-server method)
2298 (gnus-server-set-status method 'offline))
2299 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
2300 (if (eq status 'offline) 'offline 'online)
2301 (if (eq status 'offline) 'online 'offline))))
2303 (provide 'gnus-agent)
2305 ;;; gnus-agent.el ends here