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 ;;; Internal variables
82 (defvar gnus-agent-history-buffers nil)
83 (defvar gnus-agent-buffer-alist nil)
84 (defvar gnus-agent-article-alist nil)
85 (defvar gnus-agent-group-alist nil)
86 (defvar gnus-agent-covered-methods nil)
87 (defvar gnus-category-alist nil)
88 (defvar gnus-agent-current-history nil)
89 (defvar gnus-agent-overview-buffer nil)
90 (defvar gnus-category-predicate-cache nil)
91 (defvar gnus-category-group-cache nil)
92 (defvar gnus-agent-spam-hashtb nil)
93 (defvar gnus-agent-file-name nil)
94 (defvar gnus-agent-send-mail-function nil)
95 (defvar gnus-agent-file-coding-system 'raw-text)
97 (defconst gnus-agent-scoreable-headers
98 '("subject" "from" "date" "message-id" "references" "chars" "lines" "xref")
99 "Headers that are considered when scoring articles for download via the Agent.")
102 (defvar gnus-headers)
109 (defun gnus-open-agent ()
111 (gnus-agent-read-servers)
113 (gnus-agent-create-buffer)
114 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
115 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
116 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
118 (defun gnus-agent-create-buffer ()
119 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
121 (setq gnus-agent-overview-buffer
122 (gnus-get-buffer-create " *Gnus agent overview*"))
123 (with-current-buffer gnus-agent-overview-buffer
124 (mm-enable-multibyte))
127 (gnus-add-shutdown 'gnus-close-agent 'gnus)
129 (defun gnus-close-agent ()
130 (setq gnus-agent-covered-methods nil
131 gnus-category-predicate-cache nil
132 gnus-category-group-cache nil
133 gnus-agent-spam-hashtb nil)
134 (gnus-kill-buffer gnus-agent-overview-buffer))
137 ;;; Utility functions
140 (defun gnus-agent-read-file (file)
141 "Load FILE and do a `read' there."
144 (nnheader-insert-file-contents file)
145 (goto-char (point-min))
146 (read (current-buffer)))))
148 (defsubst gnus-agent-method ()
149 (concat (symbol-name (car gnus-command-method)) "/"
150 (if (equal (cadr gnus-command-method) "")
152 (cadr gnus-command-method))))
154 (defsubst gnus-agent-directory ()
155 "Path of the Gnus agent directory."
156 (nnheader-concat gnus-agent-directory
157 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
159 (defun gnus-agent-lib-file (file)
160 "The full path of the Gnus agent library FILE."
161 (concat (gnus-agent-directory) "agent.lib/" file))
163 ;;; Fetching setup functions.
165 (defun gnus-agent-start-fetch ()
166 "Initialize data structures for efficient fetching."
167 (gnus-agent-open-history)
168 (setq gnus-agent-current-history (gnus-agent-history-buffer))
169 (gnus-agent-create-buffer))
171 (defun gnus-agent-stop-fetch ()
172 "Save all data structures and clean up."
173 (gnus-agent-save-history)
174 (gnus-agent-close-history)
175 (setq gnus-agent-spam-hashtb nil)
177 (set-buffer nntp-server-buffer)
180 (defmacro gnus-agent-with-fetch (&rest forms)
184 (gnus-agent-start-fetch)
186 (gnus-agent-stop-fetch)))
188 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
189 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
195 (defvar gnus-agent-mode-hook nil
196 "Hook run when installing agent mode.")
198 (defvar gnus-agent-mode nil)
199 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
201 (defun gnus-agent-mode ()
202 "Minor mode for providing a agent support in Gnus buffers."
203 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
204 (symbol-name major-mode))
205 (match-string 1 (symbol-name major-mode))))
206 (mode (intern (format "gnus-agent-%s-mode" buffer))))
207 (set (make-local-variable 'gnus-agent-mode) t)
209 (set (make-local-variable mode) t)
211 (when (gnus-visual-p 'agent-menu 'menu)
212 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
213 (unless (assq 'gnus-agent-mode minor-mode-alist)
214 (push gnus-agent-mode-status minor-mode-alist))
215 (unless (assq mode minor-mode-map-alist)
216 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
218 minor-mode-map-alist))
219 (when (eq major-mode 'gnus-group-mode)
220 (gnus-agent-toggle-plugged gnus-plugged))
221 (gnus-run-hooks 'gnus-agent-mode-hook
222 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
224 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
225 (gnus-define-keys gnus-agent-group-mode-map
226 "Ju" gnus-agent-fetch-groups
227 "Jc" gnus-enter-category-buffer
228 "Jj" gnus-agent-toggle-plugged
229 "Js" gnus-agent-fetch-session
230 "JY" gnus-agent-synchronize
231 "JS" gnus-group-send-drafts
232 "Ja" gnus-agent-add-group
233 "Jr" gnus-agent-remove-group)
235 (defun gnus-agent-group-make-menu-bar ()
236 (unless (boundp 'gnus-agent-group-menu)
238 gnus-agent-group-menu gnus-agent-group-mode-map ""
240 ["Toggle plugged" gnus-agent-toggle-plugged t]
241 ["List categories" gnus-enter-category-buffer t]
242 ["Send drafts" gnus-group-send-drafts gnus-plugged]
244 ["All" gnus-agent-fetch-session gnus-plugged]
245 ["Group" gnus-agent-fetch-group gnus-plugged])))))
247 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
248 (gnus-define-keys gnus-agent-summary-mode-map
249 "Jj" gnus-agent-toggle-plugged
250 "J#" gnus-agent-mark-article
251 "J\M-#" gnus-agent-unmark-article
252 "@" gnus-agent-toggle-mark
253 "Jc" gnus-agent-catchup)
255 (defun gnus-agent-summary-make-menu-bar ()
256 (unless (boundp 'gnus-agent-summary-menu)
258 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
260 ["Toggle plugged" gnus-agent-toggle-plugged t]
261 ["Mark as downloadable" gnus-agent-mark-article t]
262 ["Unmark as downloadable" gnus-agent-unmark-article t]
263 ["Toggle mark" gnus-agent-toggle-mark t]
264 ["Catchup undownloaded" gnus-agent-catchup t]))))
266 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
267 (gnus-define-keys gnus-agent-server-mode-map
268 "Jj" gnus-agent-toggle-plugged
269 "Ja" gnus-agent-add-server
270 "Jr" gnus-agent-remove-server)
272 (defun gnus-agent-server-make-menu-bar ()
273 (unless (boundp 'gnus-agent-server-menu)
275 gnus-agent-server-menu gnus-agent-server-mode-map ""
277 ["Toggle plugged" gnus-agent-toggle-plugged t]
278 ["Add" gnus-agent-add-server t]
279 ["Remove" gnus-agent-remove-server t]))))
281 (defun gnus-agent-toggle-plugged (plugged)
282 "Toggle whether Gnus is unplugged or not."
283 (interactive (list (not gnus-plugged)))
286 (setq gnus-plugged plugged)
287 (gnus-run-hooks 'gnus-agent-plugged-hook)
288 (setcar (cdr gnus-agent-mode-status) " Plugged"))
289 (gnus-agent-close-connections)
290 (setq gnus-plugged plugged)
291 (gnus-run-hooks 'gnus-agent-unplugged-hook)
292 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
293 (set-buffer-modified-p t))
295 (defun gnus-agent-close-connections ()
296 "Close all methods covered by the Gnus agent."
297 (let ((methods gnus-agent-covered-methods))
299 (gnus-close-server (pop methods)))))
302 (defun gnus-unplugged ()
303 "Start Gnus unplugged."
305 (setq gnus-plugged nil)
309 (defun gnus-plugged ()
310 "Start Gnus plugged."
312 (setq gnus-plugged t)
316 (defun gnus-agentize ()
317 "Allow Gnus to be an offline newsreader.
318 The normal usage of this command is to put the following as the
319 last form in your `.gnus.el' file:
323 This will modify the `gnus-before-startup-hook', `gnus-post-method',
324 and `message-send-mail-function' variables, and install the Gnus
325 agent minor mode in all Gnus buffers."
328 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
329 (unless gnus-agent-send-mail-function
330 (setq gnus-agent-send-mail-function message-send-mail-function
331 message-send-mail-function 'gnus-agent-send-mail))
332 (unless gnus-agent-covered-methods
333 (setq gnus-agent-covered-methods (list gnus-select-method))))
335 (defun gnus-agent-queue-setup ()
336 "Make sure the queue group exists."
337 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
338 (gnus-request-create-group "queue" '(nndraft ""))
339 (let ((gnus-level-default-subscribed 1))
340 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
341 (gnus-group-set-parameter
342 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
344 (defun gnus-agent-send-mail ()
346 (funcall gnus-agent-send-mail-function)
347 (goto-char (point-min))
349 (concat "^" (regexp-quote mail-header-separator) "\n"))
351 (gnus-agent-insert-meta-information 'mail)
352 (gnus-request-accept-article "nndraft:queue" nil t t)))
354 (defun gnus-agent-insert-meta-information (type &optional method)
355 "Insert meta-information into the message that says how it's to be posted.
356 TYPE can be either `mail' or `news'. If the latter METHOD can
359 (message-remove-header gnus-agent-meta-information-header)
360 (goto-char (point-min))
361 (insert gnus-agent-meta-information-header ": "
362 (symbol-name type) " " (format "%S" method)
365 (while (search-backward "\n" nil t)
366 (replace-match "\\n" t t))))
369 ;;; Group mode commands
372 (defun gnus-agent-fetch-groups (n)
373 "Put all new articles in the current groups into the Agent."
376 (error "Groups can't be fetched when Gnus is unplugged"))
377 (gnus-group-iterate n 'gnus-agent-fetch-group))
379 (defun gnus-agent-fetch-group (group)
380 "Put all new articles in GROUP into the Agent."
381 (interactive (list (gnus-group-group-name)))
383 (error "Groups can't be fetched when Gnus is unplugged"))
385 (error "No group on the current line"))
386 (let ((gnus-command-method (gnus-find-method-for-group group)))
387 (gnus-agent-with-fetch
388 (gnus-agent-fetch-group-1 group gnus-command-method)
389 (gnus-message 5 "Fetching %s...done" group))))
391 (defun gnus-agent-add-group (category arg)
392 "Add the current group to an agent category."
398 (mapcar (lambda (cat) (list (symbol-name (car cat))))
402 (let ((cat (assq category gnus-category-alist))
404 (gnus-group-iterate arg
406 (when (cadddr (setq c (gnus-group-category group)))
407 (setf (cadddr c) (delete group (cadddr c))))
408 (push group groups)))
409 (setf (cadddr cat) (nconc (cadddr cat) groups))
410 (gnus-category-write)))
412 (defun gnus-agent-remove-group (arg)
413 "Remove the current group from its agent category, if any."
416 (gnus-group-iterate arg
418 (when (cadddr (setq c (gnus-group-category group)))
419 (setf (cadddr c) (delete group (cadddr c))))))
420 (gnus-category-write)))
422 (defun gnus-agent-synchronize ()
423 "Synchronize local, unplugged, data with backend.
424 Currently sends flag setting requests, if any."
427 (dolist (gnus-command-method gnus-agent-covered-methods)
428 (when (file-exists-p (gnus-agent-lib-file "flags"))
429 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
431 (insert-file-contents (gnus-agent-lib-file "flags"))
432 (if (null (gnus-check-server gnus-command-method))
433 (message "Couldn't open server %s" (nth 1 gnus-command-method))
435 (if (null (eval (read (current-buffer))))
436 (progn (forward-line)
438 (write-file (gnus-agent-lib-file "flags"))
439 (error "Couldn't set flags from file %s"
440 (gnus-agent-lib-file "flags"))))
441 (write-file (gnus-agent-lib-file "flags")))))))
444 ;;; Server mode commands
447 (defun gnus-agent-add-server (server)
448 "Enroll SERVER in the agent program."
449 (interactive (list (gnus-server-server-name)))
451 (error "No server on the current line"))
452 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
453 (when (member method gnus-agent-covered-methods)
454 (error "Server already in the agent program"))
455 (push method gnus-agent-covered-methods)
456 (gnus-agent-write-servers)
457 (message "Entered %s into the Agent" server)))
459 (defun gnus-agent-remove-server (server)
460 "Remove SERVER from the agent program."
461 (interactive (list (gnus-server-server-name)))
463 (error "No server on the current line"))
464 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
465 (unless (member method gnus-agent-covered-methods)
466 (error "Server not in the agent program"))
467 (setq gnus-agent-covered-methods
468 (delete method gnus-agent-covered-methods))
469 (gnus-agent-write-servers)
470 (message "Removed %s from the agent" server)))
472 (defun gnus-agent-read-servers ()
473 "Read the alist of covered servers."
474 (setq gnus-agent-covered-methods
475 (gnus-agent-read-file
476 (nnheader-concat gnus-agent-directory "lib/servers"))))
478 (defun gnus-agent-write-servers ()
479 "Write the alist of covered servers."
480 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
481 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
482 (prin1 gnus-agent-covered-methods (current-buffer))))
488 (defun gnus-agent-mark-article (n &optional unmark)
489 "Mark the next N articles as downloadable.
490 If N is negative, mark backward instead. If UNMARK is non-nil, remove
491 the mark instead. The difference between N and the actual number of
492 articles marked is returned."
494 (let ((backward (< n 0))
499 (gnus-summary-set-agent-mark
500 (gnus-summary-article-number) unmark)
501 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
504 (gnus-message 7 "No more articles"))
505 (gnus-summary-recenter)
506 (gnus-summary-position-point)
509 (defun gnus-agent-unmark-article (n)
510 "Remove the downloadable mark from the next N articles.
511 If N is negative, unmark backward instead. The difference between N and
512 the actual number of articles unmarked is returned."
514 (gnus-agent-mark-article n t))
516 (defun gnus-agent-toggle-mark (n)
517 "Toggle the downloadable mark from the next N articles.
518 If N is negative, toggle backward instead. The difference between N and
519 the actual number of articles toggled is returned."
521 (gnus-agent-mark-article n 'toggle))
523 (defun gnus-summary-set-agent-mark (article &optional unmark)
524 "Mark ARTICLE as downloadable."
525 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
526 (memq article gnus-newsgroup-downloadable)
530 (setq gnus-newsgroup-downloadable
531 (delq article gnus-newsgroup-downloadable))
532 (push article gnus-newsgroup-undownloaded))
533 (setq gnus-newsgroup-undownloaded
534 (delq article gnus-newsgroup-undownloaded))
535 (push article gnus-newsgroup-downloadable))
536 (gnus-summary-update-mark
537 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
540 (defun gnus-agent-get-undownloaded-list ()
541 "Mark all unfetched articles as read."
542 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
543 (when (and (not gnus-plugged)
544 (gnus-agent-method-p gnus-command-method))
545 (gnus-agent-load-alist gnus-newsgroup-name)
546 ;; First mark all undownloaded articles as undownloaded.
547 (let ((articles (append gnus-newsgroup-unreads
548 gnus-newsgroup-marked
549 gnus-newsgroup-dormant))
551 (while (setq article (pop articles))
552 (unless (or (cdr (assq article gnus-agent-article-alist))
553 (memq article gnus-newsgroup-downloadable)
554 (memq article gnus-newsgroup-cached))
555 (push article gnus-newsgroup-undownloaded))))
556 ;; Then mark downloaded downloadable as not-downloadable,
557 ;; if you get my drift.
558 (let ((articles gnus-newsgroup-downloadable)
560 (while (setq article (pop articles))
561 (when (cdr (assq article gnus-agent-article-alist))
562 (setq gnus-newsgroup-downloadable
563 (delq article gnus-newsgroup-downloadable))))))))
565 (defun gnus-agent-catchup ()
566 "Mark all undownloaded articles as read."
569 (while gnus-newsgroup-undownloaded
570 (gnus-summary-mark-article
571 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
572 (gnus-summary-position-point))
575 ;;; Internal functions
578 (defun gnus-agent-save-active (method)
579 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
581 (defun gnus-agent-save-active-1 (method function)
582 (when (gnus-agent-method-p method)
583 (let* ((gnus-command-method method)
584 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
585 (file (gnus-agent-lib-file "active")))
586 (funcall function nil new)
587 (gnus-agent-write-active file new)
589 (insert-file-contents-literally file))))
591 (defun gnus-agent-write-active (file new)
592 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
593 (file (gnus-agent-lib-file "active"))
595 (when (file-exists-p file)
597 (insert-file-contents file)
598 (gnus-active-to-gnus-format nil orig))
601 (when (and sym (boundp sym))
602 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
603 (setq elem (symbol-value osym)))
604 (setcdr elem (cdr (symbol-value sym)))
605 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
607 (gnus-make-directory (file-name-directory file))
608 (let ((coding-system-for-write gnus-agent-file-coding-system))
609 (gnus-write-active-file file orig))))
611 (defun gnus-agent-save-groups (method)
612 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
614 (defun gnus-agent-save-group-info (method group active)
615 (when (gnus-agent-method-p method)
616 (let* ((gnus-command-method method)
617 (file (gnus-agent-lib-file "active")))
618 (gnus-make-directory (file-name-directory file))
620 (when (file-exists-p file)
621 (nnheader-insert-file-contents file))
622 (goto-char (point-min))
623 (when (re-search-forward
624 (concat "^" (regexp-quote group) " ") nil t)
626 (insert group " " (number-to-string (cdr active)) " "
627 (number-to-string (car active)) " y\n")))))
629 (defun gnus-agent-group-path (group)
630 "Translate GROUP into a path."
631 (if nnmail-use-long-file-names
632 (gnus-group-real-name group)
633 (nnheader-replace-chars-in-string
634 (nnheader-translate-file-chars (gnus-group-real-name group))
639 (defun gnus-agent-method-p (method)
640 "Say whether METHOD is covered by the agent."
641 (member method gnus-agent-covered-methods))
643 (defun gnus-agent-get-function (method)
644 (if (and (not gnus-plugged)
645 (gnus-agent-method-p method))
651 ;;; History functions
653 (defun gnus-agent-history-buffer ()
654 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
656 (defun gnus-agent-open-history ()
658 (push (cons (gnus-agent-method)
659 (set-buffer (gnus-get-buffer-create
660 (format " *Gnus agent %s history*"
661 (gnus-agent-method)))))
662 gnus-agent-history-buffers)
665 (let ((file (gnus-agent-lib-file "history")))
666 (when (file-exists-p file)
668 (set (make-local-variable 'gnus-agent-file-name) file))))
670 (defun gnus-agent-save-history ()
672 (set-buffer gnus-agent-current-history)
673 (gnus-make-directory (file-name-directory gnus-agent-file-name))
674 (let ((coding-system-for-write gnus-agent-file-coding-system))
675 (write-region (1+ (point-min)) (point-max)
676 gnus-agent-file-name nil 'silent))))
678 (defun gnus-agent-close-history ()
679 (when (gnus-buffer-live-p gnus-agent-current-history)
680 (kill-buffer gnus-agent-current-history)
681 (setq gnus-agent-history-buffers
682 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
683 gnus-agent-history-buffers))))
685 (defun gnus-agent-enter-history (id group-arts date)
687 (set-buffer gnus-agent-current-history)
688 (goto-char (point-max))
689 (insert id "\t" (number-to-string date) "\t")
691 (insert (caar group-arts) " " (number-to-string (cdr (pop group-arts)))
695 (defun gnus-agent-article-in-history-p (id)
697 (set-buffer (gnus-agent-history-buffer))
698 (goto-char (point-min))
699 (search-forward (concat "\n" id "\t") nil t)))
701 (defun gnus-agent-history-path (id)
703 (set-buffer (gnus-agent-history-buffer))
704 (goto-char (point-min))
705 (when (search-forward (concat "\n" id "\t") nil t)
706 (let ((method (gnus-agent-method)))
708 (while (not (numberp (setq group (read (current-buffer)))))
709 (push (concat method "/" group) paths))
710 (nreverse paths))))))
716 (defun gnus-agent-fetch-articles (group articles)
717 "Fetch ARTICLES from GROUP and put them into the Agent."
719 ;; Prune off articles that we have already fetched.
721 (cdr (assq (car articles) gnus-agent-article-alist)))
723 (let ((arts articles))
725 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
726 (setcdr arts (cddr arts))
727 (setq arts (cdr arts)))))
730 (gnus-agent-directory)
731 (gnus-agent-group-path group) "/"))
732 (date (time-to-days (current-time)))
735 (gnus-make-directory dir)
736 (gnus-message 7 "Fetching articles for %s..." group)
737 ;; Fetch the articles from the backend.
738 (if (gnus-check-backend-function 'retrieve-articles group)
739 (setq pos (gnus-retrieve-articles articles group))
742 (while (setq article (pop articles))
743 (when (gnus-request-article article group)
744 (goto-char (point-max))
745 (push (cons article (point)) pos)
746 (insert-buffer-substring nntp-server-buffer)))
747 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
748 (setq pos (nreverse pos)))))
749 ;; Then save these articles into the Agent.
751 (set-buffer nntp-server-buffer)
753 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
754 (goto-char (point-min))
755 (when (search-forward "\n\n" nil t)
756 (when (search-backward "\nXrefs: " nil t)
757 ;; Handle crossposting.
758 (skip-chars-forward "^ ")
759 (skip-chars-forward " ")
761 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
762 (push (cons (buffer-substring (match-beginning 1)
764 (buffer-substring (match-beginning 2)
767 (goto-char (match-end 0)))
768 (gnus-agent-crosspost crosses (caar pos))))
769 (goto-char (point-min))
770 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
771 (setq id "No-Message-ID-in-article")
772 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
773 (let ((coding-system-for-write
774 gnus-agent-file-coding-system))
775 (write-region (point-min) (point-max)
776 (concat dir (number-to-string (caar pos)))
778 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
780 (gnus-agent-enter-history
781 id (or crosses (list (cons group (caar pos)))) date)
784 (gnus-agent-save-alist group)))))
786 (defun gnus-agent-crosspost (crosses article)
787 (let (gnus-agent-article-alist group alist beg end)
789 (set-buffer gnus-agent-overview-buffer)
790 (when (nnheader-find-nov-line article)
793 (setq end (progn (forward-line 1) (point)))))
795 (setq group (caar crosses))
796 (unless (setq alist (assoc group gnus-agent-group-alist))
797 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
798 gnus-agent-group-alist))
799 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
801 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
803 (when (= (point-max) (point-min))
804 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
806 (nnheader-insert-file-contents
807 (gnus-agent-article-name ".overview" group))))
808 (nnheader-find-nov-line (string-to-number (cdar crosses)))
809 (insert (string-to-number (cdar crosses)))
810 (insert-buffer-substring gnus-agent-overview-buffer beg end))
813 (defun gnus-agent-flush-cache ()
815 (while gnus-agent-buffer-alist
816 (set-buffer (cdar gnus-agent-buffer-alist))
817 (let ((coding-system-for-write
818 gnus-agent-file-coding-system))
819 (write-region (point-min) (point-max)
820 (gnus-agent-article-name ".overview"
821 (caar gnus-agent-buffer-alist))
823 (pop gnus-agent-buffer-alist))
824 (while gnus-agent-group-alist
825 (with-temp-file (caar gnus-agent-group-alist)
826 (princ (cdar gnus-agent-group-alist))
828 (pop gnus-agent-group-alist))))
830 (defun gnus-agent-fetch-headers (group &optional force)
831 (let ((articles (gnus-list-of-unread-articles group))
832 (gnus-decode-encoded-word-function 'identity)
833 (file (gnus-agent-article-name ".overview" group)))
834 ;; add article with marks to list of article headers we want to fetch
835 (dolist (arts (gnus-info-marks (gnus-get-info group)))
836 (setq articles (union (gnus-uncompress-sequence (cdr arts))
838 (setq articles (sort articles '<))
839 ;; remove known articles
840 (when (gnus-agent-load-alist group)
841 (setq articles (gnus-sorted-intersection
843 (gnus-uncompress-range
844 (cons (1+ (caar (last gnus-agent-article-alist)))
845 (cdr (gnus-active group)))))))
847 (gnus-make-directory (nnheader-translate-file-chars
848 (file-name-directory file)))
850 (gnus-message 7 "Fetching headers for %s..." group)
852 (set-buffer nntp-server-buffer)
853 (unless (eq 'nov (gnus-retrieve-headers articles group))
854 (nnvirtual-convert-headers))
855 ;; Save these headers for later processing.
856 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
857 (when (file-exists-p file)
858 (gnus-agent-braid-nov group articles file))
859 (let ((coding-system-for-write
860 gnus-agent-file-coding-system))
861 (write-region (point-min) (point-max) file nil 'silent))
862 (gnus-agent-save-alist group articles nil)
863 (gnus-agent-enter-history
864 "last-header-fetched-for-session"
865 (list (cons group (nth (- (length articles) 1) articles)))
866 (time-to-days (current-time)))
869 (defsubst gnus-agent-copy-nov-line (article)
871 (set-buffer gnus-agent-overview-buffer)
873 (if (eq article (read (current-buffer)))
874 (setq e (progn (forward-line 1) (point)))
878 (set-buffer nntp-server-buffer)
879 (insert-buffer-substring gnus-agent-overview-buffer b e)))
881 (defun gnus-agent-braid-nov (group articles file)
882 (set-buffer gnus-agent-overview-buffer)
883 (goto-char (point-min))
884 (set-buffer nntp-server-buffer)
886 (nnheader-insert-file-contents file)
887 (goto-char (point-max))
888 (if (or (= (point-min) (point-max))
891 (< (read (current-buffer)) (car articles))))
892 ;; We have only headers that are after the older headers,
893 ;; so we just append them.
895 (goto-char (point-max))
896 (insert-buffer-substring gnus-agent-overview-buffer))
897 ;; We do it the hard way.
898 (nnheader-find-nov-line (car articles))
899 (gnus-agent-copy-nov-line (car articles))
903 (while (and (not (eobp))
904 (< (read (current-buffer)) (car articles)))
908 (gnus-agent-copy-nov-line (car articles))
909 (setq articles (cdr articles))))
912 (set-buffer gnus-agent-overview-buffer)
915 (set-buffer nntp-server-buffer)
916 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
918 (defun gnus-agent-load-alist (group &optional dir)
919 "Load the article-state alist for GROUP."
920 (setq gnus-agent-article-alist
921 (gnus-agent-read-file
923 (concat dir ".agentview")
924 (gnus-agent-article-name ".agentview" group)))))
926 (defun gnus-agent-save-alist (group &optional articles state dir)
927 "Save the article-state alist for GROUP."
928 (with-temp-file (if dir
929 (concat dir ".agentview")
930 (gnus-agent-article-name ".agentview" group))
931 (princ (setq gnus-agent-article-alist
932 (nconc gnus-agent-article-alist
933 (mapcar (lambda (article) (cons article state))
938 (defun gnus-agent-article-name (article group)
939 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
940 (if (stringp article) article (string-to-number article))))
943 (defun gnus-agent-batch-fetch ()
944 "Start Gnus and fetch session."
947 (gnus-agent-fetch-session)
950 (defun gnus-agent-fetch-session ()
951 "Fetch all articles and headers that are eligible for fetching."
953 (unless gnus-agent-covered-methods
954 (error "No servers are covered by the Gnus agent"))
956 (error "Can't fetch articles while Gnus is unplugged"))
957 (let ((methods gnus-agent-covered-methods)
958 groups group gnus-command-method)
961 (setq gnus-command-method (car methods))
962 (when (or (gnus-server-opened gnus-command-method)
963 (gnus-open-server gnus-command-method))
964 (setq groups (gnus-groups-from-server (car methods)))
965 (gnus-agent-with-fetch
966 (while (setq group (pop groups))
967 (when (<= (gnus-group-level group) gnus-agent-handle-level)
968 (gnus-agent-fetch-group-1 group gnus-command-method)))))
970 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
972 (defun gnus-agent-fetch-group-1 (group method)
974 (let ((gnus-command-method method)
975 (gnus-newsgroup-name group)
976 gnus-newsgroup-dependencies gnus-newsgroup-headers
977 gnus-newsgroup-scored gnus-headers gnus-score
978 gnus-use-cache articles arts
979 category predicate info marks score-param)
980 (unless (gnus-check-group group)
981 (error "Can't open server for %s" group))
983 (when (and (or (gnus-active group) (gnus-activate-group group))
984 (setq articles (gnus-agent-fetch-headers group))
986 ;; Parse them and see which articles we want to fetch.
987 (setq gnus-newsgroup-dependencies
988 (make-vector (length articles) 0))
989 ;; No need to call `gnus-get-newsgroup-headers-xover' with
990 ;; the entire .overview for group as we still have the just
991 ;; downloaded headers in `gnus-agent-overview-buffer'.
992 (let ((nntp-server-buffer gnus-agent-overview-buffer))
993 (setq gnus-newsgroup-headers
994 (gnus-get-newsgroup-headers-xover articles nil nil
996 ;; `gnus-agent-overview-buffer' may be killed for
997 ;; timeout reason. If so, recreate it.
998 (gnus-agent-create-buffer)))
999 (setq category (gnus-group-category group))
1002 (or (gnus-group-find-parameter group 'agent-predicate t)
1004 ;; Do we want to download everything, or nothing?
1005 (if (or (eq (caaddr predicate) 'gnus-agent-true)
1006 (eq (caaddr predicate) 'gnus-agent-false))
1008 (setq arts (symbol-value
1009 (cadr (assoc (caaddr predicate)
1010 '((gnus-agent-true articles)
1011 (gnus-agent-false nil))))))
1012 ;; No, we need to decide what we want.
1016 (gnus-group-find-parameter group 'agent-score t)
1019 (require 'gnus-score)
1020 (if (eq score-method 'file)
1022 (gnus-score-load-files
1023 (gnus-all-score-files group)))
1025 (while (setq list (car entries))
1026 (push (car list) score-file)
1027 (setq list (cdr list))
1029 (when (member (caar list)
1030 gnus-agent-scoreable-headers)
1031 (push (car list) score-file))
1032 (setq list (cdr list)))
1034 (append score-param (list (nreverse score-file)))
1035 score-file nil entries (cdr entries)))
1037 (if (stringp (car score-method))
1039 (list (list score-method)))))))
1041 (gnus-score-headers score-param))
1043 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1045 (or (cdr (assq (mail-header-number gnus-headers)
1046 gnus-newsgroup-scored))
1047 gnus-summary-default-score))
1048 (when (funcall predicate)
1049 (push (mail-header-number gnus-headers)
1051 ;; Fetch the articles.
1053 (gnus-agent-fetch-articles group arts)))
1054 ;; Perhaps we have some additional articles to fetch.
1055 (setq arts (assq 'download (gnus-info-marks
1056 (setq info (gnus-get-info group)))))
1058 (gnus-agent-fetch-articles
1059 group (gnus-uncompress-range (cdr arts)))
1060 (setq marks (delq arts (gnus-info-marks info)))
1061 (gnus-info-set-marks info marks)
1063 (concat "(gnus-group-set-info '"
1064 (gnus-prin1-to-string info)
1068 ;;; Agent Category Mode
1071 (defvar gnus-category-mode-hook nil
1072 "Hook run in `gnus-category-mode' buffers.")
1074 (defvar gnus-category-line-format " %(%20c%): %g\n"
1075 "Format of category lines.")
1077 (defvar gnus-category-mode-line-format "Gnus: %%b"
1078 "The format specification for the category mode line.")
1080 (defvar gnus-agent-short-article 100
1081 "Articles that have fewer lines than this are short.")
1083 (defvar gnus-agent-long-article 200
1084 "Articles that have more lines than this are long.")
1086 (defvar gnus-agent-low-score 0
1087 "Articles that have a score lower than this have a low score.")
1089 (defvar gnus-agent-high-score 0
1090 "Articles that have a score higher than this have a high score.")
1093 ;;; Internal variables.
1095 (defvar gnus-category-buffer "*Agent Category*")
1097 (defvar gnus-category-line-format-alist
1098 `((?c gnus-tmp-name ?s)
1099 (?g gnus-tmp-groups ?d)))
1101 (defvar gnus-category-mode-line-format-alist
1102 `((?u user-defined ?s)))
1104 (defvar gnus-category-line-format-spec nil)
1105 (defvar gnus-category-mode-line-format-spec nil)
1107 (defvar gnus-category-mode-map nil)
1108 (put 'gnus-category-mode 'mode-class 'special)
1110 (unless gnus-category-mode-map
1111 (setq gnus-category-mode-map (make-sparse-keymap))
1112 (suppress-keymap gnus-category-mode-map)
1114 (gnus-define-keys gnus-category-mode-map
1115 "q" gnus-category-exit
1116 "k" gnus-category-kill
1117 "c" gnus-category-copy
1118 "a" gnus-category-add
1119 "p" gnus-category-edit-predicate
1120 "g" gnus-category-edit-groups
1121 "s" gnus-category-edit-score
1122 "l" gnus-category-list
1124 "\C-c\C-i" gnus-info-find-node
1125 "\C-c\C-b" gnus-bug))
1127 (defvar gnus-category-menu-hook nil
1128 "*Hook run after the creation of the menu.")
1130 (defun gnus-category-make-menu-bar ()
1131 (gnus-turn-off-edit-menu 'category)
1132 (unless (boundp 'gnus-category-menu)
1134 gnus-category-menu gnus-category-mode-map ""
1136 ["Add" gnus-category-add t]
1137 ["Kill" gnus-category-kill t]
1138 ["Copy" gnus-category-copy t]
1139 ["Edit predicate" gnus-category-edit-predicate t]
1140 ["Edit score" gnus-category-edit-score t]
1141 ["Edit groups" gnus-category-edit-groups t]
1142 ["Exit" gnus-category-exit t]))
1144 (gnus-run-hooks 'gnus-category-menu-hook)))
1146 (defun gnus-category-mode ()
1147 "Major mode for listing and editing agent categories.
1149 All normal editing commands are switched off.
1150 \\<gnus-category-mode-map>
1151 For more in-depth information on this mode, read the manual
1152 (`\\[gnus-info-find-node]').
1154 The following commands are available:
1156 \\{gnus-category-mode-map}"
1158 (when (gnus-visual-p 'category-menu 'menu)
1159 (gnus-category-make-menu-bar))
1160 (kill-all-local-variables)
1161 (gnus-simplify-mode-line)
1162 (setq major-mode 'gnus-category-mode)
1163 (setq mode-name "Category")
1164 (gnus-set-default-directory)
1165 (setq mode-line-process nil)
1166 (use-local-map gnus-category-mode-map)
1167 (buffer-disable-undo)
1168 (setq truncate-lines t)
1169 (setq buffer-read-only t)
1170 (gnus-run-hooks 'gnus-category-mode-hook))
1172 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1174 (defun gnus-category-insert-line (category)
1175 (let* ((gnus-tmp-name (car category))
1176 (gnus-tmp-groups (length (cadddr category))))
1178 (gnus-add-text-properties
1182 (eval gnus-category-line-format-spec))
1183 (list 'gnus-category gnus-tmp-name))))
1185 (defun gnus-enter-category-buffer ()
1186 "Go to the Category buffer."
1188 (gnus-category-setup-buffer)
1189 (gnus-configure-windows 'category)
1190 (gnus-category-prepare))
1192 (defun gnus-category-setup-buffer ()
1193 (unless (get-buffer gnus-category-buffer)
1195 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1196 (gnus-category-mode))))
1198 (defun gnus-category-prepare ()
1199 (gnus-set-format 'category-mode)
1200 (gnus-set-format 'category t)
1201 (let ((alist gnus-category-alist)
1202 (buffer-read-only nil))
1205 (gnus-category-insert-line (pop alist)))
1206 (goto-char (point-min))
1207 (gnus-category-position-point)))
1209 (defun gnus-category-name ()
1210 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1211 (error "No category on the current line")))
1213 (defun gnus-category-read ()
1214 "Read the category alist."
1215 (setq gnus-category-alist
1216 (or (gnus-agent-read-file
1217 (nnheader-concat gnus-agent-directory "lib/categories"))
1218 (list (list 'default 'short nil nil)))))
1220 (defun gnus-category-write ()
1221 "Write the category alist."
1222 (setq gnus-category-predicate-cache nil
1223 gnus-category-group-cache nil)
1224 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1225 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1226 (prin1 gnus-category-alist (current-buffer))))
1228 (defun gnus-category-edit-predicate (category)
1229 "Edit the predicate for CATEGORY."
1230 (interactive (list (gnus-category-name)))
1231 (let ((info (assq category gnus-category-alist)))
1233 (cadr info) (format "Editing the predicate for category %s" category)
1234 `(lambda (predicate)
1235 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1236 (gnus-category-write)
1237 (gnus-category-list)))))
1239 (defun gnus-category-edit-score (category)
1240 "Edit the score expression for CATEGORY."
1241 (interactive (list (gnus-category-name)))
1242 (let ((info (assq category gnus-category-alist)))
1245 (format "Editing the score expression for category %s" category)
1247 (setf (caddr (assq ',category gnus-category-alist)) groups)
1248 (gnus-category-write)
1249 (gnus-category-list)))))
1251 (defun gnus-category-edit-groups (category)
1252 "Edit the group list for CATEGORY."
1253 (interactive (list (gnus-category-name)))
1254 (let ((info (assq category gnus-category-alist)))
1256 (cadddr info) (format "Editing the group list for category %s" category)
1258 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1259 (gnus-category-write)
1260 (gnus-category-list)))))
1262 (defun gnus-category-kill (category)
1263 "Kill the current category."
1264 (interactive (list (gnus-category-name)))
1265 (let ((info (assq category gnus-category-alist))
1266 (buffer-read-only nil))
1268 (gnus-category-write)
1269 (setq gnus-category-alist (delq info gnus-category-alist))))
1271 (defun gnus-category-copy (category to)
1272 "Copy the current category."
1273 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1274 (let ((info (assq category gnus-category-alist)))
1275 (push (list to (gnus-copy-sequence (cadr info))
1276 (gnus-copy-sequence (caddr info)) nil)
1277 gnus-category-alist)
1278 (gnus-category-write)
1279 (gnus-category-list)))
1281 (defun gnus-category-add (category)
1282 "Create a new category."
1283 (interactive "SCategory name: ")
1284 (when (assq category gnus-category-alist)
1285 (error "Category %s already exists" category))
1286 (push (list category 'false nil nil)
1287 gnus-category-alist)
1288 (gnus-category-write)
1289 (gnus-category-list))
1291 (defun gnus-category-list ()
1292 "List all categories."
1294 (gnus-category-prepare))
1296 (defun gnus-category-exit ()
1297 "Return to the group buffer."
1299 (kill-buffer (current-buffer))
1300 (gnus-configure-windows 'group t))
1302 ;; To avoid having 8-bit characters in the source file.
1303 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1305 (defvar gnus-category-predicate-alist
1306 '((spam . gnus-agent-spam-p)
1307 (short . gnus-agent-short-p)
1308 (long . gnus-agent-long-p)
1309 (low . gnus-agent-low-scored-p)
1310 (high . gnus-agent-high-scored-p)
1311 (true . gnus-agent-true)
1312 (false . gnus-agent-false))
1313 "Mapping from short score predicate symbols to predicate functions.")
1315 (defun gnus-agent-spam-p ()
1316 "Say whether an article is spam or not."
1317 (unless gnus-agent-spam-hashtb
1318 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1319 (if (not (equal (mail-header-references gnus-headers) ""))
1321 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1323 (gnus-gethash string gnus-agent-spam-hashtb)
1324 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1326 (defun gnus-agent-short-p ()
1327 "Say whether an article is short or not."
1328 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1330 (defun gnus-agent-long-p ()
1331 "Say whether an article is long or not."
1332 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1334 (defun gnus-agent-low-scored-p ()
1335 "Say whether an article has a low score or not."
1336 (< gnus-score gnus-agent-low-score))
1338 (defun gnus-agent-high-scored-p ()
1339 "Say whether an article has a high score or not."
1340 (> gnus-score gnus-agent-high-score))
1342 (defun gnus-category-make-function (cat)
1343 "Make a function from category CAT."
1344 `(lambda () ,(gnus-category-make-function-1 cat)))
1346 (defun gnus-agent-true ()
1350 (defun gnus-agent-false ()
1354 (defun gnus-category-make-function-1 (cat)
1355 "Make a function from category CAT."
1357 ;; Functions are just returned as is.
1359 (gnus-functionp cat))
1360 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1362 ;; More complex category.
1365 ((memq (car cat) '(& and))
1367 ((memq (car cat) '(| or))
1369 ((memq (car cat) gnus-category-not)
1371 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1373 (error "Unknown category type: %s" cat))))
1375 (defun gnus-get-predicate (predicate)
1376 "Return the predicate for CATEGORY."
1377 (or (cdr (assoc predicate gnus-category-predicate-cache))
1378 (cdar (push (cons predicate
1379 (gnus-category-make-function predicate))
1380 gnus-category-predicate-cache))))
1382 (defun gnus-group-category (group)
1383 "Return the category GROUP belongs to."
1384 (unless gnus-category-group-cache
1385 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1386 (let ((cs gnus-category-alist)
1388 (while (setq cat (pop cs))
1389 (setq groups (cadddr cat))
1391 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1392 (or (gnus-gethash group gnus-category-group-cache)
1393 (assq 'default gnus-category-alist)))
1395 (defun gnus-agent-expire ()
1396 "Expire all old articles."
1398 (let ((methods gnus-agent-covered-methods)
1399 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1400 gnus-command-method sym group articles
1401 history overview file histories elem art nov-file low info
1402 unreads marked article orig lowest highest)
1404 (setq overview (gnus-get-buffer-create " *expire overview*"))
1405 (while (setq gnus-command-method (pop methods))
1406 (when (file-exists-p (gnus-agent-lib-file "active"))
1408 (insert-file-contents (gnus-agent-lib-file "active"))
1409 (gnus-active-to-gnus-format
1411 (setq orig (gnus-make-hashtable
1412 (count-lines (point-min) (point-max))))))
1413 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1414 (gnus-agent-open-history)
1416 (setq gnus-agent-current-history
1417 (setq history (gnus-agent-history-buffer))))
1418 (goto-char (point-min))
1419 (when (> (buffer-size) 1)
1420 (goto-char (point-min))
1422 (skip-chars-forward "^\t")
1423 (if (> (read (current-buffer)) day)
1424 ;; New article; we don't expire it.
1426 ;; Old article. Schedule it for possible nuking.
1428 (setq sym (let ((obarray expiry-hashtb))
1429 (read (current-buffer))))
1431 (set sym (cons (cons (read (current-buffer)) (point))
1432 (symbol-value sym)))
1433 (set sym (list (cons (read (current-buffer)) (point)))))
1434 (skip-chars-forward " "))
1436 ;; We now have all articles that can possibly be expired.
1439 (setq group (symbol-name sym)
1440 articles (sort (symbol-value sym) 'car-less-than-car)
1441 low (car (gnus-active group))
1442 info (gnus-get-info group)
1443 unreads (ignore-errors
1444 (gnus-list-of-unread-articles group))
1446 (gnus-uncompress-range
1447 (cdr (assq 'tick (gnus-info-marks info))))
1448 (gnus-uncompress-range
1450 (gnus-info-marks info)))))
1451 nov-file (gnus-agent-article-name ".overview" group)
1454 (gnus-agent-load-alist group)
1455 (gnus-message 5 "Expiring articles in %s" group)
1456 (set-buffer overview)
1458 (when (file-exists-p nov-file)
1459 (nnheader-insert-file-contents nov-file))
1460 (goto-char (point-min))
1462 (while (setq elem (pop articles))
1463 (setq article (car elem))
1464 (when (or (null low)
1466 gnus-agent-expire-all
1467 (and (not (memq article unreads))
1468 (not (memq article marked))))
1469 ;; Find and nuke the NOV line.
1470 (while (and (not (eobp))
1472 (setq art (read (current-buffer)))))
1474 (if (and (numberp art)
1476 (gnus-agent-article-name
1477 (number-to-string art) group)))
1483 ;; Remove old NOV lines that have no articles.
1484 (gnus-delete-line)))
1489 ;; Nuke the article.
1490 (when (file-exists-p
1491 (setq file (gnus-agent-article-name
1492 (number-to-string article)
1495 ;; Schedule the history line for nuking.
1496 (push (cdr elem) histories)))
1497 (gnus-make-directory (file-name-directory nov-file))
1498 (let ((coding-system-for-write
1499 gnus-agent-file-coding-system))
1500 (write-region (point-min) (point-max) nov-file nil 'silent))
1501 ;; Delete the unwanted entries in the alist.
1502 (setq gnus-agent-article-alist
1503 (sort gnus-agent-article-alist 'car-less-than-car))
1504 (let* ((alist gnus-agent-article-alist)
1505 (prev (cons nil alist))
1509 (<= (caar alist) article))
1510 (if (or (not (cdar alist))
1512 (gnus-agent-article-name
1517 (push (caar alist) expired)
1518 (setcdr prev (setq alist (cdr alist))))
1520 alist (cdr alist))))
1521 (setq gnus-agent-article-alist (cdr first))
1522 (gnus-agent-save-alist group)
1523 ;; Mark all articles up to the first article
1524 ;; in `gnus-article-alist' as read.
1525 (when (and info (caar gnus-agent-article-alist))
1526 (setcar (nthcdr 2 info)
1529 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1530 ;; Maybe everything has been expired from `gnus-article-alist'
1531 ;; and so the above marking as read could not be conducted,
1532 ;; or there are expired article within the range of the alist.
1535 (or (not (caar gnus-agent-article-alist))
1537 (caar gnus-agent-article-alist))))
1538 (setcar (nthcdr 2 info)
1541 (nreverse expired))))
1543 (concat "(gnus-group-set-info '"
1544 (gnus-prin1-to-string info)
1547 (if (gnus-gethash group orig)
1548 (setcar (gnus-gethash group orig) lowest)
1549 (gnus-sethash group (cons lowest highest) orig))))
1551 (set-buffer history)
1552 (setq histories (nreverse (sort histories '<)))
1554 (goto-char (pop histories))
1556 (gnus-agent-save-history)
1557 (gnus-agent-close-history)
1558 (gnus-write-active-file
1559 (gnus-agent-lib-file "active") orig))
1560 (gnus-message 4 "Expiry...done")))))))
1563 (defun gnus-agent-batch ()
1565 (let ((init-file-user "")
1566 (gnus-always-read-dribble-file t))
1568 (gnus-group-send-drafts)
1569 (gnus-agent-fetch-session))
1571 (provide 'gnus-agent)
1573 ;;; gnus-agent.el ends here