1 ;;; gnus-agent.el --- unplugged support for Gnus
2 ;; Copyright (C) 1997,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
32 (require 'gnus-score))
34 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
35 "Where the Gnus agent will store its files."
39 (defcustom gnus-agent-plugged-hook nil
40 "Hook run when plugging into the network."
44 (defcustom gnus-agent-unplugged-hook nil
45 "Hook run when unplugging from the network."
49 (defcustom gnus-agent-handle-level gnus-level-subscribed
50 "Groups on levels higher than this variable will be ignored by the Agent."
54 (defcustom gnus-agent-expire-days 7
55 "Read articles older than this will be expired."
59 (defcustom gnus-agent-expire-all nil
60 "If non-nil, also expire unread, ticked and dormant articles.
61 If nil, only read articles will be expired."
65 (defcustom gnus-agent-group-mode-hook nil
66 "Hook run in Agent group minor modes."
70 (defcustom gnus-agent-summary-mode-hook nil
71 "Hook run in Agent summary minor modes."
75 (defcustom gnus-agent-server-mode-hook nil
76 "Hook run in Agent summary minor modes."
80 (defcustom gnus-agent-confirmation-function 'y-or-n-p
81 "Function to confirm when error happens."
85 ;;; Internal variables
87 (defvar gnus-agent-history-buffers nil)
88 (defvar gnus-agent-buffer-alist nil)
89 (defvar gnus-agent-article-alist nil)
90 (defvar gnus-agent-group-alist nil)
91 (defvar gnus-agent-covered-methods nil)
92 (defvar gnus-category-alist nil)
93 (defvar gnus-agent-current-history nil)
94 (defvar gnus-agent-overview-buffer nil)
95 (defvar gnus-category-predicate-cache nil)
96 (defvar gnus-category-group-cache nil)
97 (defvar gnus-agent-spam-hashtb nil)
98 (defvar gnus-agent-file-name nil)
99 (defvar gnus-agent-send-mail-function nil)
100 (defvar gnus-agent-file-coding-system 'raw-text)
102 (defconst gnus-agent-scoreable-headers
103 '("subject" "from" "date" "message-id" "references" "chars" "lines" "xref")
104 "Headers that are considered when scoring articles for download via the Agent.")
107 (defvar gnus-headers)
114 (defun gnus-open-agent ()
116 (gnus-agent-read-servers)
118 (gnus-agent-create-buffer)
119 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
120 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
121 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
123 (defun gnus-agent-create-buffer ()
124 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
126 (setq gnus-agent-overview-buffer
127 (gnus-get-buffer-create " *Gnus agent overview*"))
128 (with-current-buffer gnus-agent-overview-buffer
129 (mm-enable-multibyte))
132 (gnus-add-shutdown 'gnus-close-agent 'gnus)
134 (defun gnus-close-agent ()
135 (setq gnus-agent-covered-methods nil
136 gnus-category-predicate-cache nil
137 gnus-category-group-cache nil
138 gnus-agent-spam-hashtb nil)
139 (gnus-kill-buffer gnus-agent-overview-buffer))
142 ;;; Utility functions
145 (defun gnus-agent-read-file (file)
146 "Load FILE and do a `read' there."
149 (nnheader-insert-file-contents file)
150 (goto-char (point-min))
151 (read (current-buffer)))))
153 (defsubst gnus-agent-method ()
154 (concat (symbol-name (car gnus-command-method)) "/"
155 (if (equal (cadr gnus-command-method) "")
157 (cadr gnus-command-method))))
159 (defsubst gnus-agent-directory ()
160 "Path of the Gnus agent directory."
161 (nnheader-concat gnus-agent-directory
162 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
164 (defun gnus-agent-lib-file (file)
165 "The full path of the Gnus agent library FILE."
166 (concat (gnus-agent-directory) "agent.lib/" file))
168 ;;; Fetching setup functions.
170 (defun gnus-agent-start-fetch ()
171 "Initialize data structures for efficient fetching."
172 (gnus-agent-open-history)
173 (setq gnus-agent-current-history (gnus-agent-history-buffer))
174 (gnus-agent-create-buffer))
176 (defun gnus-agent-stop-fetch ()
177 "Save all data structures and clean up."
178 (gnus-agent-save-history)
179 (gnus-agent-close-history)
180 (setq gnus-agent-spam-hashtb nil)
182 (set-buffer nntp-server-buffer)
185 (defmacro gnus-agent-with-fetch (&rest forms)
189 (gnus-agent-start-fetch)
191 (gnus-agent-stop-fetch)))
193 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
194 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
200 (defvar gnus-agent-mode-hook nil
201 "Hook run when installing agent mode.")
203 (defvar gnus-agent-mode nil)
204 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
206 (defun gnus-agent-mode ()
207 "Minor mode for providing a agent support in Gnus buffers."
208 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
209 (symbol-name major-mode))
210 (match-string 1 (symbol-name major-mode))))
211 (mode (intern (format "gnus-agent-%s-mode" buffer))))
212 (set (make-local-variable 'gnus-agent-mode) t)
214 (set (make-local-variable mode) t)
216 (when (gnus-visual-p 'agent-menu 'menu)
217 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
218 (unless (assq 'gnus-agent-mode minor-mode-alist)
219 (push gnus-agent-mode-status minor-mode-alist))
220 (unless (assq mode minor-mode-map-alist)
221 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
223 minor-mode-map-alist))
224 (when (eq major-mode 'gnus-group-mode)
225 (gnus-agent-toggle-plugged gnus-plugged))
226 (gnus-run-hooks 'gnus-agent-mode-hook
227 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
229 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
230 (gnus-define-keys gnus-agent-group-mode-map
231 "Ju" gnus-agent-fetch-groups
232 "Jc" gnus-enter-category-buffer
233 "Jj" gnus-agent-toggle-plugged
234 "Js" gnus-agent-fetch-session
235 "JY" gnus-agent-synchronize
236 "JS" gnus-group-send-drafts
237 "Ja" gnus-agent-add-group
238 "Jr" gnus-agent-remove-group)
240 (defun gnus-agent-group-make-menu-bar ()
241 (unless (boundp 'gnus-agent-group-menu)
243 gnus-agent-group-menu gnus-agent-group-mode-map ""
245 ["Toggle plugged" gnus-agent-toggle-plugged t]
246 ["List categories" gnus-enter-category-buffer t]
247 ["Send drafts" gnus-group-send-drafts gnus-plugged]
249 ["All" gnus-agent-fetch-session gnus-plugged]
250 ["Group" gnus-agent-fetch-group gnus-plugged])))))
252 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
253 (gnus-define-keys gnus-agent-summary-mode-map
254 "Jj" gnus-agent-toggle-plugged
255 "J#" gnus-agent-mark-article
256 "J\M-#" gnus-agent-unmark-article
257 "@" gnus-agent-toggle-mark
258 "Jc" gnus-agent-catchup)
260 (defun gnus-agent-summary-make-menu-bar ()
261 (unless (boundp 'gnus-agent-summary-menu)
263 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
265 ["Toggle plugged" gnus-agent-toggle-plugged t]
266 ["Mark as downloadable" gnus-agent-mark-article t]
267 ["Unmark as downloadable" gnus-agent-unmark-article t]
268 ["Toggle mark" gnus-agent-toggle-mark t]
269 ["Catchup undownloaded" gnus-agent-catchup t]))))
271 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
272 (gnus-define-keys gnus-agent-server-mode-map
273 "Jj" gnus-agent-toggle-plugged
274 "Ja" gnus-agent-add-server
275 "Jr" gnus-agent-remove-server)
277 (defun gnus-agent-server-make-menu-bar ()
278 (unless (boundp 'gnus-agent-server-menu)
280 gnus-agent-server-menu gnus-agent-server-mode-map ""
282 ["Toggle plugged" gnus-agent-toggle-plugged t]
283 ["Add" gnus-agent-add-server t]
284 ["Remove" gnus-agent-remove-server t]))))
286 (defun gnus-agent-toggle-plugged (plugged)
287 "Toggle whether Gnus is unplugged or not."
288 (interactive (list (not gnus-plugged)))
291 (setq gnus-plugged plugged)
292 (gnus-run-hooks 'gnus-agent-plugged-hook)
293 (setcar (cdr gnus-agent-mode-status) " Plugged"))
294 (gnus-agent-close-connections)
295 (setq gnus-plugged plugged)
296 (gnus-run-hooks 'gnus-agent-unplugged-hook)
297 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
298 (set-buffer-modified-p t))
300 (defun gnus-agent-close-connections ()
301 "Close all methods covered by the Gnus agent."
302 (let ((methods gnus-agent-covered-methods))
304 (gnus-close-server (pop methods)))))
307 (defun gnus-unplugged ()
308 "Start Gnus unplugged."
310 (setq gnus-plugged nil)
314 (defun gnus-plugged ()
315 "Start Gnus plugged."
317 (setq gnus-plugged t)
321 (defun gnus-agentize ()
322 "Allow Gnus to be an offline newsreader.
323 The normal usage of this command is to put the following as the
324 last form in your `.gnus.el' file:
328 This will modify the `gnus-before-startup-hook', `gnus-post-method',
329 and `message-send-mail-function' variables, and install the Gnus
330 agent minor mode in all Gnus buffers."
333 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
334 (unless gnus-agent-send-mail-function
335 (setq gnus-agent-send-mail-function message-send-mail-function
336 message-send-mail-function 'gnus-agent-send-mail))
337 (unless gnus-agent-covered-methods
338 (setq gnus-agent-covered-methods (list gnus-select-method))))
340 (defun gnus-agent-queue-setup ()
341 "Make sure the queue group exists."
342 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
343 (gnus-request-create-group "queue" '(nndraft ""))
344 (let ((gnus-level-default-subscribed 1))
345 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
346 (gnus-group-set-parameter
347 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
349 (defun gnus-agent-send-mail ()
351 (funcall gnus-agent-send-mail-function)
352 (goto-char (point-min))
354 (concat "^" (regexp-quote mail-header-separator) "\n"))
356 (gnus-agent-insert-meta-information 'mail)
357 (gnus-request-accept-article "nndraft:queue" nil t t)))
359 (defun gnus-agent-insert-meta-information (type &optional method)
360 "Insert meta-information into the message that says how it's to be posted.
361 TYPE can be either `mail' or `news'. If the latter METHOD can
364 (message-remove-header gnus-agent-meta-information-header)
365 (goto-char (point-min))
366 (insert gnus-agent-meta-information-header ": "
367 (symbol-name type) " " (format "%S" method)
370 (while (search-backward "\n" nil t)
371 (replace-match "\\n" t t))))
374 ;;; Group mode commands
377 (defun gnus-agent-fetch-groups (n)
378 "Put all new articles in the current groups into the Agent."
381 (error "Groups can't be fetched when Gnus is unplugged"))
382 (gnus-group-iterate n 'gnus-agent-fetch-group))
384 (defun gnus-agent-fetch-group (group)
385 "Put all new articles in GROUP into the Agent."
386 (interactive (list (gnus-group-group-name)))
388 (error "Groups can't be fetched when Gnus is unplugged"))
390 (error "No group on the current line"))
391 (let ((gnus-command-method (gnus-find-method-for-group group)))
392 (gnus-agent-with-fetch
393 (gnus-agent-fetch-group-1 group gnus-command-method)
394 (gnus-message 5 "Fetching %s...done" group))))
396 (defun gnus-agent-add-group (category arg)
397 "Add the current group to an agent category."
403 (mapcar (lambda (cat) (list (symbol-name (car cat))))
407 (let ((cat (assq category gnus-category-alist))
409 (gnus-group-iterate arg
411 (when (cadddr (setq c (gnus-group-category group)))
412 (setf (cadddr c) (delete group (cadddr c))))
413 (push group groups)))
414 (setf (cadddr cat) (nconc (cadddr cat) groups))
415 (gnus-category-write)))
417 (defun gnus-agent-remove-group (arg)
418 "Remove the current group from its agent category, if any."
421 (gnus-group-iterate arg
423 (when (cadddr (setq c (gnus-group-category group)))
424 (setf (cadddr c) (delete group (cadddr c))))))
425 (gnus-category-write)))
427 (defun gnus-agent-synchronize ()
428 "Synchronize local, unplugged, data with backend.
429 Currently sends flag setting requests, if any."
432 (dolist (gnus-command-method gnus-agent-covered-methods)
433 (when (file-exists-p (gnus-agent-lib-file "flags"))
434 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
436 (insert-file-contents (gnus-agent-lib-file "flags"))
437 (if (null (gnus-check-server gnus-command-method))
438 (message "Couldn't open server %s" (nth 1 gnus-command-method))
440 (if (null (eval (read (current-buffer))))
441 (progn (forward-line)
443 (write-file (gnus-agent-lib-file "flags"))
444 (error "Couldn't set flags from file %s"
445 (gnus-agent-lib-file "flags"))))
446 (write-file (gnus-agent-lib-file "flags")))))))
449 ;;; Server mode commands
452 (defun gnus-agent-add-server (server)
453 "Enroll SERVER in the agent program."
454 (interactive (list (gnus-server-server-name)))
456 (error "No server on the current line"))
457 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
458 (when (member method gnus-agent-covered-methods)
459 (error "Server already in the agent program"))
460 (push method gnus-agent-covered-methods)
461 (gnus-agent-write-servers)
462 (message "Entered %s into the Agent" server)))
464 (defun gnus-agent-remove-server (server)
465 "Remove SERVER from the agent program."
466 (interactive (list (gnus-server-server-name)))
468 (error "No server on the current line"))
469 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
470 (unless (member method gnus-agent-covered-methods)
471 (error "Server not in the agent program"))
472 (setq gnus-agent-covered-methods
473 (delete method gnus-agent-covered-methods))
474 (gnus-agent-write-servers)
475 (message "Removed %s from the agent" server)))
477 (defun gnus-agent-read-servers ()
478 "Read the alist of covered servers."
479 (setq gnus-agent-covered-methods
480 (gnus-agent-read-file
481 (nnheader-concat gnus-agent-directory "lib/servers"))))
483 (defun gnus-agent-write-servers ()
484 "Write the alist of covered servers."
485 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
486 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
487 (prin1 gnus-agent-covered-methods (current-buffer))))
493 (defun gnus-agent-mark-article (n &optional unmark)
494 "Mark the next N articles as downloadable.
495 If N is negative, mark backward instead. If UNMARK is non-nil, remove
496 the mark instead. The difference between N and the actual number of
497 articles marked is returned."
499 (let ((backward (< n 0))
504 (gnus-summary-set-agent-mark
505 (gnus-summary-article-number) unmark)
506 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
509 (gnus-message 7 "No more articles"))
510 (gnus-summary-recenter)
511 (gnus-summary-position-point)
514 (defun gnus-agent-unmark-article (n)
515 "Remove the downloadable mark from the next N articles.
516 If N is negative, unmark backward instead. The difference between N and
517 the actual number of articles unmarked is returned."
519 (gnus-agent-mark-article n t))
521 (defun gnus-agent-toggle-mark (n)
522 "Toggle the downloadable mark from the next N articles.
523 If N is negative, toggle backward instead. The difference between N and
524 the actual number of articles toggled is returned."
526 (gnus-agent-mark-article n 'toggle))
528 (defun gnus-summary-set-agent-mark (article &optional unmark)
529 "Mark ARTICLE as downloadable."
530 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
531 (memq article gnus-newsgroup-downloadable)
535 (setq gnus-newsgroup-downloadable
536 (delq article gnus-newsgroup-downloadable))
537 (push article gnus-newsgroup-undownloaded))
538 (setq gnus-newsgroup-undownloaded
539 (delq article gnus-newsgroup-undownloaded))
540 (push article gnus-newsgroup-downloadable))
541 (gnus-summary-update-mark
542 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
545 (defun gnus-agent-get-undownloaded-list ()
546 "Mark all unfetched articles as read."
547 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
548 (when (and (not gnus-plugged)
549 (gnus-agent-method-p gnus-command-method))
550 (gnus-agent-load-alist gnus-newsgroup-name)
551 ;; First mark all undownloaded articles as undownloaded.
552 (let ((articles (append gnus-newsgroup-unreads
553 gnus-newsgroup-marked
554 gnus-newsgroup-dormant))
556 (while (setq article (pop articles))
557 (unless (or (cdr (assq article gnus-agent-article-alist))
558 (memq article gnus-newsgroup-downloadable)
559 (memq article gnus-newsgroup-cached))
560 (push article gnus-newsgroup-undownloaded))))
561 ;; Then mark downloaded downloadable as not-downloadable,
562 ;; if you get my drift.
563 (let ((articles gnus-newsgroup-downloadable)
565 (while (setq article (pop articles))
566 (when (cdr (assq article gnus-agent-article-alist))
567 (setq gnus-newsgroup-downloadable
568 (delq article gnus-newsgroup-downloadable))))))))
570 (defun gnus-agent-catchup ()
571 "Mark all undownloaded articles as read."
574 (while gnus-newsgroup-undownloaded
575 (gnus-summary-mark-article
576 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
577 (gnus-summary-position-point))
580 ;;; Internal functions
583 (defun gnus-agent-save-active (method)
584 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
586 (defun gnus-agent-save-active-1 (method function)
587 (when (gnus-agent-method-p method)
588 (let* ((gnus-command-method method)
589 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
590 (file (gnus-agent-lib-file "active")))
591 (funcall function nil new)
592 (gnus-agent-write-active file new)
594 (insert-file-contents-literally file))))
596 (defun gnus-agent-write-active (file new)
597 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
598 (file (gnus-agent-lib-file "active"))
600 (when (file-exists-p file)
602 (insert-file-contents file)
603 (gnus-active-to-gnus-format nil orig))
606 (when (and sym (boundp sym))
607 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
608 (setq elem (symbol-value osym)))
609 (setcdr elem (cdr (symbol-value sym)))
610 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
612 (gnus-make-directory (file-name-directory file))
613 (let ((coding-system-for-write gnus-agent-file-coding-system))
614 (gnus-write-active-file file orig))))
616 (defun gnus-agent-save-groups (method)
617 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
619 (defun gnus-agent-save-group-info (method group active)
620 (when (gnus-agent-method-p method)
621 (let* ((gnus-command-method method)
622 (file (gnus-agent-lib-file "active")))
623 (gnus-make-directory (file-name-directory file))
625 (when (file-exists-p file)
626 (nnheader-insert-file-contents file))
627 (goto-char (point-min))
628 (when (re-search-forward
629 (concat "^" (regexp-quote group) " ") nil t)
631 (insert (format "%S %d %d y\n" (intern group) (cdr active)
633 (goto-char (point-max))
634 (while (search-backward "\\." nil t)
637 (defun gnus-agent-group-path (group)
638 "Translate GROUP into a path."
639 (if nnmail-use-long-file-names
640 (gnus-group-real-name group)
641 (nnheader-translate-file-chars
642 (nnheader-replace-chars-in-string
643 (nnheader-replace-duplicate-chars-in-string
644 (nnheader-replace-chars-in-string
645 (gnus-group-real-name group)
652 (defun gnus-agent-method-p (method)
653 "Say whether METHOD is covered by the agent."
654 (member method gnus-agent-covered-methods))
656 (defun gnus-agent-get-function (method)
657 (if (and (not gnus-plugged)
658 (gnus-agent-method-p method))
664 ;;; History functions
666 (defun gnus-agent-history-buffer ()
667 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
669 (defun gnus-agent-open-history ()
671 (push (cons (gnus-agent-method)
672 (set-buffer (gnus-get-buffer-create
673 (format " *Gnus agent %s history*"
674 (gnus-agent-method)))))
675 gnus-agent-history-buffers)
678 (let ((file (gnus-agent-lib-file "history")))
679 (when (file-exists-p file)
681 (set (make-local-variable 'gnus-agent-file-name) file))))
683 (defun gnus-agent-save-history ()
685 (set-buffer gnus-agent-current-history)
686 (gnus-make-directory (file-name-directory gnus-agent-file-name))
687 (let ((coding-system-for-write gnus-agent-file-coding-system))
688 (write-region (1+ (point-min)) (point-max)
689 gnus-agent-file-name nil 'silent))))
691 (defun gnus-agent-close-history ()
692 (when (gnus-buffer-live-p gnus-agent-current-history)
693 (kill-buffer gnus-agent-current-history)
694 (setq gnus-agent-history-buffers
695 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
696 gnus-agent-history-buffers))))
698 (defun gnus-agent-enter-history (id group-arts date)
700 (set-buffer gnus-agent-current-history)
701 (goto-char (point-max))
702 (insert id "\t" (number-to-string date) "\t")
704 (insert (caar group-arts) " " (number-to-string (cdr (pop group-arts)))
708 (defun gnus-agent-article-in-history-p (id)
710 (set-buffer (gnus-agent-history-buffer))
711 (goto-char (point-min))
712 (search-forward (concat "\n" id "\t") nil t)))
714 (defun gnus-agent-history-path (id)
716 (set-buffer (gnus-agent-history-buffer))
717 (goto-char (point-min))
718 (when (search-forward (concat "\n" id "\t") nil t)
719 (let ((method (gnus-agent-method)))
721 (while (not (numberp (setq group (read (current-buffer)))))
722 (push (concat method "/" group) paths))
723 (nreverse paths))))))
729 (defun gnus-agent-fetch-articles (group articles)
730 "Fetch ARTICLES from GROUP and put them into the Agent."
732 ;; Prune off articles that we have already fetched.
734 (cdr (assq (car articles) gnus-agent-article-alist)))
736 (let ((arts articles))
738 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
739 (setcdr arts (cddr arts))
740 (setq arts (cdr arts)))))
743 (gnus-agent-directory)
744 (gnus-agent-group-path group) "/"))
745 (date (time-to-days (current-time)))
748 (gnus-make-directory dir)
749 (gnus-message 7 "Fetching articles for %s..." group)
750 ;; Fetch the articles from the backend.
751 (if (gnus-check-backend-function 'retrieve-articles group)
752 (setq pos (gnus-retrieve-articles articles group))
755 (while (setq article (pop articles))
756 (when (gnus-request-article article group)
757 (goto-char (point-max))
758 (push (cons article (point)) pos)
759 (insert-buffer-substring nntp-server-buffer)))
760 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
761 (setq pos (nreverse pos)))))
762 ;; Then save these articles into the Agent.
764 (set-buffer nntp-server-buffer)
766 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
767 (goto-char (point-min))
768 (when (search-forward "\n\n" nil t)
769 (when (search-backward "\nXrefs: " nil t)
770 ;; Handle crossposting.
771 (skip-chars-forward "^ ")
772 (skip-chars-forward " ")
774 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
775 (push (cons (buffer-substring (match-beginning 1)
777 (buffer-substring (match-beginning 2)
780 (goto-char (match-end 0)))
781 (gnus-agent-crosspost crosses (caar pos))))
782 (goto-char (point-min))
783 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
784 (setq id "No-Message-ID-in-article")
785 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
786 (let ((coding-system-for-write
787 gnus-agent-file-coding-system))
788 (write-region (point-min) (point-max)
789 (concat dir (number-to-string (caar pos)))
791 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
793 (gnus-agent-enter-history
794 id (or crosses (list (cons group (caar pos)))) date)
797 (gnus-agent-save-alist group)))))
799 (defun gnus-agent-crosspost (crosses article)
800 (let (gnus-agent-article-alist group alist beg end)
802 (set-buffer gnus-agent-overview-buffer)
803 (when (nnheader-find-nov-line article)
806 (setq end (progn (forward-line 1) (point)))))
808 (setq group (caar crosses))
809 (unless (setq alist (assoc group gnus-agent-group-alist))
810 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
811 gnus-agent-group-alist))
812 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
814 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
816 (when (= (point-max) (point-min))
817 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
819 (nnheader-insert-file-contents
820 (gnus-agent-article-name ".overview" group))))
821 (nnheader-find-nov-line (string-to-number (cdar crosses)))
822 (insert (string-to-number (cdar crosses)))
823 (insert-buffer-substring gnus-agent-overview-buffer beg end))
826 (defun gnus-agent-flush-cache ()
828 (while gnus-agent-buffer-alist
829 (set-buffer (cdar gnus-agent-buffer-alist))
830 (let ((coding-system-for-write
831 gnus-agent-file-coding-system))
832 (write-region (point-min) (point-max)
833 (gnus-agent-article-name ".overview"
834 (caar gnus-agent-buffer-alist))
836 (pop gnus-agent-buffer-alist))
837 (while gnus-agent-group-alist
838 (with-temp-file (caar gnus-agent-group-alist)
839 (princ (cdar gnus-agent-group-alist))
841 (pop gnus-agent-group-alist))))
843 (defun gnus-agent-fetch-headers (group &optional force)
844 (let ((articles (gnus-list-of-unread-articles group))
845 (gnus-decode-encoded-word-function 'identity)
846 (file (gnus-agent-article-name ".overview" group)))
847 ;; add article with marks to list of article headers we want to fetch
848 (dolist (arts (gnus-info-marks (gnus-get-info group)))
849 (setq articles (union (gnus-uncompress-sequence (cdr arts))
851 (setq articles (sort articles '<))
852 ;; remove known articles
853 (when (gnus-agent-load-alist group)
854 (setq articles (gnus-sorted-intersection
856 (gnus-uncompress-range
857 (cons (1+ (caar (last gnus-agent-article-alist)))
858 (cdr (gnus-active group)))))))
860 (gnus-make-directory (nnheader-translate-file-chars
861 (file-name-directory file)))
863 (gnus-message 7 "Fetching headers for %s..." group)
865 (set-buffer nntp-server-buffer)
866 (unless (eq 'nov (gnus-retrieve-headers articles group))
867 (nnvirtual-convert-headers))
868 ;; Save these headers for later processing.
869 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
870 (when (file-exists-p file)
871 (gnus-agent-braid-nov group articles file))
872 (let ((coding-system-for-write
873 gnus-agent-file-coding-system))
874 (write-region (point-min) (point-max) file nil 'silent))
875 (gnus-agent-save-alist group articles nil)
876 (gnus-agent-enter-history
877 "last-header-fetched-for-session"
878 (list (cons group (nth (- (length articles) 1) articles)))
879 (time-to-days (current-time)))
882 (defsubst gnus-agent-copy-nov-line (article)
884 (set-buffer gnus-agent-overview-buffer)
886 (if (eq article (read (current-buffer)))
887 (setq e (progn (forward-line 1) (point)))
891 (set-buffer nntp-server-buffer)
892 (insert-buffer-substring gnus-agent-overview-buffer b e)))
894 (defun gnus-agent-braid-nov (group articles file)
895 (set-buffer gnus-agent-overview-buffer)
896 (goto-char (point-min))
897 (set-buffer nntp-server-buffer)
899 (nnheader-insert-file-contents file)
900 (goto-char (point-max))
901 (if (or (= (point-min) (point-max))
904 (< (read (current-buffer)) (car articles))))
905 ;; We have only headers that are after the older headers,
906 ;; so we just append them.
908 (goto-char (point-max))
909 (insert-buffer-substring gnus-agent-overview-buffer))
910 ;; We do it the hard way.
911 (nnheader-find-nov-line (car articles))
912 (gnus-agent-copy-nov-line (car articles))
916 (while (and (not (eobp))
917 (< (read (current-buffer)) (car articles)))
921 (gnus-agent-copy-nov-line (car articles))
922 (setq articles (cdr articles))))
925 (set-buffer gnus-agent-overview-buffer)
928 (set-buffer nntp-server-buffer)
929 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
931 (defun gnus-agent-load-alist (group &optional dir)
932 "Load the article-state alist for GROUP."
933 (setq gnus-agent-article-alist
934 (gnus-agent-read-file
936 (concat dir ".agentview")
937 (gnus-agent-article-name ".agentview" group)))))
939 (defun gnus-agent-save-alist (group &optional articles state dir)
940 "Save the article-state alist for GROUP."
941 (with-temp-file (if dir
942 (concat dir ".agentview")
943 (gnus-agent-article-name ".agentview" group))
944 (princ (setq gnus-agent-article-alist
945 (nconc gnus-agent-article-alist
946 (mapcar (lambda (article) (cons article state))
951 (defun gnus-agent-article-name (article group)
952 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
953 (if (stringp article) article (string-to-number article))))
955 (defun gnus-agent-batch-confirmation (msg)
956 "Show error message and return t."
961 (defun gnus-agent-batch-fetch ()
962 "Start Gnus and fetch session."
965 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
966 (gnus-agent-fetch-session))
969 (defun gnus-agent-fetch-session ()
970 "Fetch all articles and headers that are eligible for fetching."
972 (unless gnus-agent-covered-methods
973 (error "No servers are covered by the Gnus agent"))
975 (error "Can't fetch articles while Gnus is unplugged"))
976 (let ((methods gnus-agent-covered-methods)
977 groups group gnus-command-method)
982 (setq gnus-command-method (car methods))
983 (when (or (gnus-server-opened gnus-command-method)
984 (gnus-open-server gnus-command-method))
985 (setq groups (gnus-groups-from-server (car methods)))
986 (gnus-agent-with-fetch
987 (while (setq group (pop groups))
988 (when (<= (gnus-group-level group) gnus-agent-handle-level)
989 (gnus-agent-fetch-group-1 group gnus-command-method))))))
991 (unless (funcall gnus-agent-confirmation-function
992 (format "Error (%s). Continue? " err))
993 (error "Cannot fetch articles into the Gnus agent."))))
995 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
997 (defun gnus-agent-fetch-group-1 (group method)
999 (let ((gnus-command-method method)
1000 (gnus-newsgroup-name group)
1001 gnus-newsgroup-dependencies gnus-newsgroup-headers
1002 gnus-newsgroup-scored gnus-headers gnus-score
1003 gnus-use-cache articles arts
1004 category predicate info marks score-param)
1005 (unless (gnus-check-group group)
1006 (error "Can't open server for %s" group))
1008 (when (and (or (gnus-active group) (gnus-activate-group group))
1009 (setq articles (gnus-agent-fetch-headers group))
1011 ;; Parse them and see which articles we want to fetch.
1012 (setq gnus-newsgroup-dependencies
1013 (make-vector (length articles) 0))
1014 ;; No need to call `gnus-get-newsgroup-headers-xover' with
1015 ;; the entire .overview for group as we still have the just
1016 ;; downloaded headers in `gnus-agent-overview-buffer'.
1017 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1018 (setq gnus-newsgroup-headers
1019 (gnus-get-newsgroup-headers-xover articles nil nil
1021 ;; `gnus-agent-overview-buffer' may be killed for
1022 ;; timeout reason. If so, recreate it.
1023 (gnus-agent-create-buffer)))
1024 (setq category (gnus-group-category group))
1027 (or (gnus-group-find-parameter group 'agent-predicate t)
1029 ;; Do we want to download everything, or nothing?
1030 (if (or (eq (caaddr predicate) 'gnus-agent-true)
1031 (eq (caaddr predicate) 'gnus-agent-false))
1033 (setq arts (symbol-value
1034 (cadr (assoc (caaddr predicate)
1035 '((gnus-agent-true articles)
1036 (gnus-agent-false nil))))))
1037 ;; No, we need to decide what we want.
1041 (gnus-group-find-parameter group 'agent-score t)
1044 (require 'gnus-score)
1045 (if (eq score-method 'file)
1047 (gnus-score-load-files
1048 (gnus-all-score-files group)))
1050 (while (setq list (car entries))
1051 (push (car list) score-file)
1052 (setq list (cdr list))
1054 (when (member (caar list)
1055 gnus-agent-scoreable-headers)
1056 (push (car list) score-file))
1057 (setq list (cdr list)))
1059 (append score-param (list (nreverse score-file)))
1060 score-file nil entries (cdr entries)))
1062 (if (stringp (car score-method))
1064 (list (list score-method)))))))
1066 (gnus-score-headers score-param))
1068 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1070 (or (cdr (assq (mail-header-number gnus-headers)
1071 gnus-newsgroup-scored))
1072 gnus-summary-default-score))
1073 (when (funcall predicate)
1074 (push (mail-header-number gnus-headers)
1076 ;; Fetch the articles.
1078 (gnus-agent-fetch-articles group arts)))
1079 ;; Perhaps we have some additional articles to fetch.
1080 (setq arts (assq 'download (gnus-info-marks
1081 (setq info (gnus-get-info group)))))
1083 (gnus-agent-fetch-articles
1084 group (gnus-uncompress-range (cdr arts)))
1085 (setq marks (delq arts (gnus-info-marks info)))
1086 (gnus-info-set-marks info marks)
1088 (concat "(gnus-group-set-info '"
1089 (gnus-prin1-to-string info)
1093 ;;; Agent Category Mode
1096 (defvar gnus-category-mode-hook nil
1097 "Hook run in `gnus-category-mode' buffers.")
1099 (defvar gnus-category-line-format " %(%20c%): %g\n"
1100 "Format of category lines.")
1102 (defvar gnus-category-mode-line-format "Gnus: %%b"
1103 "The format specification for the category mode line.")
1105 (defvar gnus-agent-short-article 100
1106 "Articles that have fewer lines than this are short.")
1108 (defvar gnus-agent-long-article 200
1109 "Articles that have more lines than this are long.")
1111 (defvar gnus-agent-low-score 0
1112 "Articles that have a score lower than this have a low score.")
1114 (defvar gnus-agent-high-score 0
1115 "Articles that have a score higher than this have a high score.")
1118 ;;; Internal variables.
1120 (defvar gnus-category-buffer "*Agent Category*")
1122 (defvar gnus-category-line-format-alist
1123 `((?c gnus-tmp-name ?s)
1124 (?g gnus-tmp-groups ?d)))
1126 (defvar gnus-category-mode-line-format-alist
1127 `((?u user-defined ?s)))
1129 (defvar gnus-category-line-format-spec nil)
1130 (defvar gnus-category-mode-line-format-spec nil)
1132 (defvar gnus-category-mode-map nil)
1133 (put 'gnus-category-mode 'mode-class 'special)
1135 (unless gnus-category-mode-map
1136 (setq gnus-category-mode-map (make-sparse-keymap))
1137 (suppress-keymap gnus-category-mode-map)
1139 (gnus-define-keys gnus-category-mode-map
1140 "q" gnus-category-exit
1141 "k" gnus-category-kill
1142 "c" gnus-category-copy
1143 "a" gnus-category-add
1144 "p" gnus-category-edit-predicate
1145 "g" gnus-category-edit-groups
1146 "s" gnus-category-edit-score
1147 "l" gnus-category-list
1149 "\C-c\C-i" gnus-info-find-node
1150 "\C-c\C-b" gnus-bug))
1152 (defvar gnus-category-menu-hook nil
1153 "*Hook run after the creation of the menu.")
1155 (defun gnus-category-make-menu-bar ()
1156 (gnus-turn-off-edit-menu 'category)
1157 (unless (boundp 'gnus-category-menu)
1159 gnus-category-menu gnus-category-mode-map ""
1161 ["Add" gnus-category-add t]
1162 ["Kill" gnus-category-kill t]
1163 ["Copy" gnus-category-copy t]
1164 ["Edit predicate" gnus-category-edit-predicate t]
1165 ["Edit score" gnus-category-edit-score t]
1166 ["Edit groups" gnus-category-edit-groups t]
1167 ["Exit" gnus-category-exit t]))
1169 (gnus-run-hooks 'gnus-category-menu-hook)))
1171 (defun gnus-category-mode ()
1172 "Major mode for listing and editing agent categories.
1174 All normal editing commands are switched off.
1175 \\<gnus-category-mode-map>
1176 For more in-depth information on this mode, read the manual
1177 (`\\[gnus-info-find-node]').
1179 The following commands are available:
1181 \\{gnus-category-mode-map}"
1183 (when (gnus-visual-p 'category-menu 'menu)
1184 (gnus-category-make-menu-bar))
1185 (kill-all-local-variables)
1186 (gnus-simplify-mode-line)
1187 (setq major-mode 'gnus-category-mode)
1188 (setq mode-name "Category")
1189 (gnus-set-default-directory)
1190 (setq mode-line-process nil)
1191 (use-local-map gnus-category-mode-map)
1192 (buffer-disable-undo)
1193 (setq truncate-lines t)
1194 (setq buffer-read-only t)
1195 (gnus-run-hooks 'gnus-category-mode-hook))
1197 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1199 (defun gnus-category-insert-line (category)
1200 (let* ((gnus-tmp-name (car category))
1201 (gnus-tmp-groups (length (cadddr category))))
1203 (gnus-add-text-properties
1207 (eval gnus-category-line-format-spec))
1208 (list 'gnus-category gnus-tmp-name))))
1210 (defun gnus-enter-category-buffer ()
1211 "Go to the Category buffer."
1213 (gnus-category-setup-buffer)
1214 (gnus-configure-windows 'category)
1215 (gnus-category-prepare))
1217 (defun gnus-category-setup-buffer ()
1218 (unless (get-buffer gnus-category-buffer)
1220 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1221 (gnus-category-mode))))
1223 (defun gnus-category-prepare ()
1224 (gnus-set-format 'category-mode)
1225 (gnus-set-format 'category t)
1226 (let ((alist gnus-category-alist)
1227 (buffer-read-only nil))
1230 (gnus-category-insert-line (pop alist)))
1231 (goto-char (point-min))
1232 (gnus-category-position-point)))
1234 (defun gnus-category-name ()
1235 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1236 (error "No category on the current line")))
1238 (defun gnus-category-read ()
1239 "Read the category alist."
1240 (setq gnus-category-alist
1241 (or (gnus-agent-read-file
1242 (nnheader-concat gnus-agent-directory "lib/categories"))
1243 (list (list 'default 'short nil nil)))))
1245 (defun gnus-category-write ()
1246 "Write the category alist."
1247 (setq gnus-category-predicate-cache nil
1248 gnus-category-group-cache nil)
1249 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1250 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1251 (prin1 gnus-category-alist (current-buffer))))
1253 (defun gnus-category-edit-predicate (category)
1254 "Edit the predicate for CATEGORY."
1255 (interactive (list (gnus-category-name)))
1256 (let ((info (assq category gnus-category-alist)))
1258 (cadr info) (format "Editing the predicate for category %s" category)
1260 `(lambda (predicate)
1261 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1262 (gnus-category-write)
1263 (gnus-category-list))))))
1265 (defun gnus-category-edit-score (category)
1266 "Edit the score expression for CATEGORY."
1267 (interactive (list (gnus-category-name)))
1268 (let ((info (assq category gnus-category-alist)))
1271 (format "Editing the score expression for category %s" category)
1274 (setcar (cddr (assq ',category gnus-category-alist)) groups)
1275 (gnus-category-write)
1276 (gnus-category-list))))))
1278 (defun gnus-category-edit-groups (category)
1279 "Edit the group list for CATEGORY."
1280 (interactive (list (gnus-category-name)))
1281 (let ((info (assq category gnus-category-alist)))
1283 (cadddr info) (format "Editing the group list for category %s" category)
1286 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1287 (gnus-category-write)
1288 (gnus-category-list))))))
1290 (defun gnus-category-kill (category)
1291 "Kill the current category."
1292 (interactive (list (gnus-category-name)))
1293 (let ((info (assq category gnus-category-alist))
1294 (buffer-read-only nil))
1296 (gnus-category-write)
1297 (setq gnus-category-alist (delq info gnus-category-alist))))
1299 (defun gnus-category-copy (category to)
1300 "Copy the current category."
1301 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1302 (let ((info (assq category gnus-category-alist)))
1303 (push (list to (gnus-copy-sequence (cadr info))
1304 (gnus-copy-sequence (caddr info)) nil)
1305 gnus-category-alist)
1306 (gnus-category-write)
1307 (gnus-category-list)))
1309 (defun gnus-category-add (category)
1310 "Create a new category."
1311 (interactive "SCategory name: ")
1312 (when (assq category gnus-category-alist)
1313 (error "Category %s already exists" category))
1314 (push (list category 'false nil nil)
1315 gnus-category-alist)
1316 (gnus-category-write)
1317 (gnus-category-list))
1319 (defun gnus-category-list ()
1320 "List all categories."
1322 (gnus-category-prepare))
1324 (defun gnus-category-exit ()
1325 "Return to the group buffer."
1327 (kill-buffer (current-buffer))
1328 (gnus-configure-windows 'group t))
1330 ;; To avoid having 8-bit characters in the source file.
1331 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1333 (defvar gnus-category-predicate-alist
1334 '((spam . gnus-agent-spam-p)
1335 (short . gnus-agent-short-p)
1336 (long . gnus-agent-long-p)
1337 (low . gnus-agent-low-scored-p)
1338 (high . gnus-agent-high-scored-p)
1339 (true . gnus-agent-true)
1340 (false . gnus-agent-false))
1341 "Mapping from short score predicate symbols to predicate functions.")
1343 (defun gnus-agent-spam-p ()
1344 "Say whether an article is spam or not."
1345 (unless gnus-agent-spam-hashtb
1346 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1347 (if (not (equal (mail-header-references gnus-headers) ""))
1349 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1351 (gnus-gethash string gnus-agent-spam-hashtb)
1352 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1354 (defun gnus-agent-short-p ()
1355 "Say whether an article is short or not."
1356 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1358 (defun gnus-agent-long-p ()
1359 "Say whether an article is long or not."
1360 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1362 (defun gnus-agent-low-scored-p ()
1363 "Say whether an article has a low score or not."
1364 (< gnus-score gnus-agent-low-score))
1366 (defun gnus-agent-high-scored-p ()
1367 "Say whether an article has a high score or not."
1368 (> gnus-score gnus-agent-high-score))
1370 (defun gnus-category-make-function (cat)
1371 "Make a function from category CAT."
1372 `(lambda () ,(gnus-category-make-function-1 cat)))
1374 (defun gnus-agent-true ()
1378 (defun gnus-agent-false ()
1382 (defun gnus-category-make-function-1 (cat)
1383 "Make a function from category CAT."
1385 ;; Functions are just returned as is.
1387 (gnus-functionp cat))
1388 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1390 ;; More complex category.
1393 ((memq (car cat) '(& and))
1395 ((memq (car cat) '(| or))
1397 ((memq (car cat) gnus-category-not)
1399 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1401 (error "Unknown category type: %s" cat))))
1403 (defun gnus-get-predicate (predicate)
1404 "Return the predicate for CATEGORY."
1405 (or (cdr (assoc predicate gnus-category-predicate-cache))
1406 (cdar (push (cons predicate
1407 (gnus-category-make-function predicate))
1408 gnus-category-predicate-cache))))
1410 (defun gnus-group-category (group)
1411 "Return the category GROUP belongs to."
1412 (unless gnus-category-group-cache
1413 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1414 (let ((cs gnus-category-alist)
1416 (while (setq cat (pop cs))
1417 (setq groups (cadddr cat))
1419 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1420 (or (gnus-gethash group gnus-category-group-cache)
1421 (assq 'default gnus-category-alist)))
1423 (defun gnus-agent-expire ()
1424 "Expire all old articles."
1426 (let ((methods gnus-agent-covered-methods)
1427 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1428 gnus-command-method sym group articles
1429 history overview file histories elem art nov-file low info
1430 unreads marked article orig lowest highest)
1432 (setq overview (gnus-get-buffer-create " *expire overview*"))
1433 (while (setq gnus-command-method (pop methods))
1434 (when (file-exists-p (gnus-agent-lib-file "active"))
1436 (insert-file-contents (gnus-agent-lib-file "active"))
1437 (gnus-active-to-gnus-format
1439 (setq orig (gnus-make-hashtable
1440 (count-lines (point-min) (point-max))))))
1441 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1442 (gnus-agent-open-history)
1444 (setq gnus-agent-current-history
1445 (setq history (gnus-agent-history-buffer))))
1446 (goto-char (point-min))
1447 (when (> (buffer-size) 1)
1448 (goto-char (point-min))
1450 (skip-chars-forward "^\t")
1451 (if (> (read (current-buffer)) day)
1452 ;; New article; we don't expire it.
1454 ;; Old article. Schedule it for possible nuking.
1456 (setq sym (let ((obarray expiry-hashtb))
1457 (read (current-buffer))))
1459 (set sym (cons (cons (read (current-buffer)) (point))
1460 (symbol-value sym)))
1461 (set sym (list (cons (read (current-buffer)) (point)))))
1462 (skip-chars-forward " "))
1464 ;; We now have all articles that can possibly be expired.
1467 (setq group (symbol-name sym)
1468 articles (sort (symbol-value sym) 'car-less-than-car)
1469 low (car (gnus-active group))
1470 info (gnus-get-info group)
1471 unreads (ignore-errors
1472 (gnus-list-of-unread-articles group))
1474 (gnus-uncompress-range
1475 (cdr (assq 'tick (gnus-info-marks info))))
1476 (gnus-uncompress-range
1478 (gnus-info-marks info)))))
1479 nov-file (gnus-agent-article-name ".overview" group)
1482 (gnus-agent-load-alist group)
1483 (gnus-message 5 "Expiring articles in %s" group)
1484 (set-buffer overview)
1486 (when (file-exists-p nov-file)
1487 (nnheader-insert-file-contents nov-file))
1488 (goto-char (point-min))
1490 (while (setq elem (pop articles))
1491 (setq article (car elem))
1492 (when (or (null low)
1494 gnus-agent-expire-all
1495 (and (not (memq article unreads))
1496 (not (memq article marked))))
1497 ;; Find and nuke the NOV line.
1498 (while (and (not (eobp))
1500 (setq art (read (current-buffer)))))
1502 (if (and (numberp art)
1504 (gnus-agent-article-name
1505 (number-to-string art) group)))
1511 ;; Remove old NOV lines that have no articles.
1512 (gnus-delete-line)))
1517 ;; Nuke the article.
1518 (when (file-exists-p
1519 (setq file (gnus-agent-article-name
1520 (number-to-string article)
1523 ;; Schedule the history line for nuking.
1524 (push (cdr elem) histories)))
1525 (gnus-make-directory (file-name-directory nov-file))
1526 (let ((coding-system-for-write
1527 gnus-agent-file-coding-system))
1528 (write-region (point-min) (point-max) nov-file nil 'silent))
1529 ;; Delete the unwanted entries in the alist.
1530 (setq gnus-agent-article-alist
1531 (sort gnus-agent-article-alist 'car-less-than-car))
1532 (let* ((alist gnus-agent-article-alist)
1533 (prev (cons nil alist))
1537 (<= (caar alist) article))
1538 (if (or (not (cdar alist))
1540 (gnus-agent-article-name
1545 (push (caar alist) expired)
1546 (setcdr prev (setq alist (cdr alist))))
1548 alist (cdr alist))))
1549 (setq gnus-agent-article-alist (cdr first))
1550 (gnus-agent-save-alist group)
1551 ;; Mark all articles up to the first article
1552 ;; in `gnus-article-alist' as read.
1553 (when (and info (caar gnus-agent-article-alist))
1554 (setcar (nthcdr 2 info)
1557 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1558 ;; Maybe everything has been expired from `gnus-article-alist'
1559 ;; and so the above marking as read could not be conducted,
1560 ;; or there are expired article within the range of the alist.
1563 (or (not (caar gnus-agent-article-alist))
1565 (caar gnus-agent-article-alist))))
1566 (setcar (nthcdr 2 info)
1569 (nreverse expired))))
1571 (concat "(gnus-group-set-info '"
1572 (gnus-prin1-to-string info)
1575 (if (gnus-gethash group orig)
1576 (setcar (gnus-gethash group orig) lowest)
1577 (gnus-sethash group (cons lowest highest) orig))))
1579 (set-buffer history)
1580 (setq histories (nreverse (sort histories '<)))
1582 (goto-char (pop histories))
1584 (gnus-agent-save-history)
1585 (gnus-agent-close-history)
1586 (gnus-write-active-file
1587 (gnus-agent-lib-file "active") orig))
1588 (gnus-message 4 "Expiry...done")))))))
1591 (defun gnus-agent-batch ()
1593 (let ((init-file-user "")
1594 (gnus-always-read-dribble-file t))
1596 (gnus-group-send-drafts)
1597 (gnus-agent-fetch-session))
1599 (provide 'gnus-agent)
1601 ;;; gnus-agent.el ends here