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 (setq gnus-agent-overview-buffer
114 (gnus-get-buffer-create " *Gnus agent overview*"))
115 (with-current-buffer gnus-agent-overview-buffer
116 (mm-enable-multibyte))
117 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
118 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
119 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
121 (gnus-add-shutdown 'gnus-close-agent 'gnus)
123 (defun gnus-close-agent ()
124 (setq gnus-agent-covered-methods nil
125 gnus-category-predicate-cache nil
126 gnus-category-group-cache nil
127 gnus-agent-spam-hashtb nil)
128 (gnus-kill-buffer gnus-agent-overview-buffer))
131 ;;; Utility functions
134 (defun gnus-agent-read-file (file)
135 "Load FILE and do a `read' there."
138 (mm-insert-file-contents file)
139 (goto-char (point-min))
140 (read (current-buffer)))))
142 (defsubst gnus-agent-method ()
143 (concat (symbol-name (car gnus-command-method)) "/"
144 (if (equal (cadr gnus-command-method) "")
146 (cadr gnus-command-method))))
148 (defsubst gnus-agent-directory ()
149 "Path of the Gnus agent directory."
150 (nnheader-concat gnus-agent-directory
151 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
153 (defun gnus-agent-lib-file (file)
154 "The full path of the Gnus agent library FILE."
155 (concat (gnus-agent-directory) "agent.lib/" file))
157 ;;; Fetching setup functions.
159 (defun gnus-agent-start-fetch ()
160 "Initialize data structures for efficient fetching."
161 (gnus-agent-open-history)
162 (setq gnus-agent-current-history (gnus-agent-history-buffer)))
164 (defun gnus-agent-stop-fetch ()
165 "Save all data structures and clean up."
166 (gnus-agent-save-history)
167 (gnus-agent-close-history)
168 (setq gnus-agent-spam-hashtb nil)
170 (set-buffer nntp-server-buffer)
173 (defmacro gnus-agent-with-fetch (&rest forms)
177 (gnus-agent-start-fetch)
179 (gnus-agent-stop-fetch)))
181 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
182 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
188 (defvar gnus-agent-mode-hook nil
189 "Hook run when installing agent mode.")
191 (defvar gnus-agent-mode nil)
192 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
194 (defun gnus-agent-mode ()
195 "Minor mode for providing a agent support in Gnus buffers."
196 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
197 (symbol-name major-mode))
198 (match-string 1 (symbol-name major-mode))))
199 (mode (intern (format "gnus-agent-%s-mode" buffer))))
200 (set (make-local-variable 'gnus-agent-mode) t)
202 (set (make-local-variable mode) t)
204 (when (gnus-visual-p 'agent-menu 'menu)
205 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
206 (unless (assq 'gnus-agent-mode minor-mode-alist)
207 (push gnus-agent-mode-status minor-mode-alist))
208 (unless (assq mode minor-mode-map-alist)
209 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
211 minor-mode-map-alist))
212 (when (eq major-mode 'gnus-group-mode)
213 (gnus-agent-toggle-plugged gnus-plugged))
214 (gnus-run-hooks 'gnus-agent-mode-hook
215 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
217 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
218 (gnus-define-keys gnus-agent-group-mode-map
219 "Ju" gnus-agent-fetch-groups
220 "Jc" gnus-enter-category-buffer
221 "Jj" gnus-agent-toggle-plugged
222 "Js" gnus-agent-fetch-session
223 "JS" gnus-group-send-drafts
224 "Ja" gnus-agent-add-group
225 "Jr" gnus-agent-remove-group)
227 (defun gnus-agent-group-make-menu-bar ()
228 (unless (boundp 'gnus-agent-group-menu)
230 gnus-agent-group-menu gnus-agent-group-mode-map ""
232 ["Toggle plugged" gnus-agent-toggle-plugged t]
233 ["List categories" gnus-enter-category-buffer t]
234 ["Send drafts" gnus-group-send-drafts gnus-plugged]
236 ["All" gnus-agent-fetch-session gnus-plugged]
237 ["Group" gnus-agent-fetch-group gnus-plugged])))))
239 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
240 (gnus-define-keys gnus-agent-summary-mode-map
241 "Jj" gnus-agent-toggle-plugged
242 "J#" gnus-agent-mark-article
243 "J\M-#" gnus-agent-unmark-article
244 "@" gnus-agent-toggle-mark
245 "Jc" gnus-agent-catchup)
247 (defun gnus-agent-summary-make-menu-bar ()
248 (unless (boundp 'gnus-agent-summary-menu)
250 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
252 ["Toggle plugged" gnus-agent-toggle-plugged t]
253 ["Mark as downloadable" gnus-agent-mark-article t]
254 ["Unmark as downloadable" gnus-agent-unmark-article t]
255 ["Toggle mark" gnus-agent-toggle-mark t]
256 ["Catchup undownloaded" gnus-agent-catchup t]))))
258 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
259 (gnus-define-keys gnus-agent-server-mode-map
260 "Jj" gnus-agent-toggle-plugged
261 "Ja" gnus-agent-add-server
262 "Jr" gnus-agent-remove-server)
264 (defun gnus-agent-server-make-menu-bar ()
265 (unless (boundp 'gnus-agent-server-menu)
267 gnus-agent-server-menu gnus-agent-server-mode-map ""
269 ["Toggle plugged" gnus-agent-toggle-plugged t]
270 ["Add" gnus-agent-add-server t]
271 ["Remove" gnus-agent-remove-server t]))))
273 (defun gnus-agent-toggle-plugged (plugged)
274 "Toggle whether Gnus is unplugged or not."
275 (interactive (list (not gnus-plugged)))
278 (setq gnus-plugged plugged)
279 (gnus-run-hooks 'gnus-agent-plugged-hook)
280 (setcar (cdr gnus-agent-mode-status) " Plugged"))
281 (gnus-agent-close-connections)
282 (setq gnus-plugged plugged)
283 (gnus-run-hooks 'gnus-agent-unplugged-hook)
284 (setcar (cdr gnus-agent-mode-status) " Unplugged"))
285 (set-buffer-modified-p t))
287 (defun gnus-agent-close-connections ()
288 "Close all methods covered by the Gnus agent."
289 (let ((methods gnus-agent-covered-methods))
291 (gnus-close-server (pop methods)))))
294 (defun gnus-unplugged ()
295 "Start Gnus unplugged."
297 (setq gnus-plugged nil)
301 (defun gnus-plugged ()
302 "Start Gnus plugged."
304 (setq gnus-plugged t)
308 (defun gnus-agentize ()
309 "Allow Gnus to be an offline newsreader.
310 The normal usage of this command is to put the following as the
311 last form in your `.gnus.el' file:
315 This will modify the `gnus-before-startup-hook', `gnus-post-method',
316 and `message-send-mail-function' variables, and install the Gnus
317 agent minor mode in all Gnus buffers."
320 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
321 (unless gnus-agent-send-mail-function
322 (setq gnus-agent-send-mail-function message-send-mail-function
323 message-send-mail-function 'gnus-agent-send-mail))
324 (unless gnus-agent-covered-methods
325 (setq gnus-agent-covered-methods (list gnus-select-method))))
327 (defun gnus-agent-queue-setup ()
328 "Make sure the queue group exists."
329 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
330 (gnus-request-create-group "queue" '(nndraft ""))
331 (let ((gnus-level-default-subscribed 1))
332 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
333 (gnus-group-set-parameter
334 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
336 (defun gnus-agent-send-mail ()
338 (funcall gnus-agent-send-mail-function)
339 (goto-char (point-min))
341 (concat "^" (regexp-quote mail-header-separator) "\n"))
343 (gnus-agent-insert-meta-information 'mail)
344 (gnus-request-accept-article "nndraft:queue" nil t t)))
346 (defun gnus-agent-insert-meta-information (type &optional method)
347 "Insert meta-information into the message that says how it's to be posted.
348 TYPE can be either `mail' or `news'. If the latter METHOD can
351 (message-remove-header gnus-agent-meta-information-header)
352 (goto-char (point-min))
353 (insert gnus-agent-meta-information-header ": "
354 (symbol-name type) " " (format "%S" method)
357 (while (search-backward "\n" nil t)
358 (replace-match "\\n" t t))))
361 ;;; Group mode commands
364 (defun gnus-agent-fetch-groups (n)
365 "Put all new articles in the current groups into the Agent."
368 (error "Groups can't be fetched when Gnus is unplugged"))
369 (gnus-group-iterate n 'gnus-agent-fetch-group))
371 (defun gnus-agent-fetch-group (group)
372 "Put all new articles in GROUP into the Agent."
373 (interactive (list (gnus-group-group-name)))
375 (error "Groups can't be fetched when Gnus is unplugged"))
377 (error "No group on the current line"))
378 (let ((gnus-command-method (gnus-find-method-for-group group)))
379 (gnus-agent-with-fetch
380 (gnus-agent-fetch-group-1 group gnus-command-method)
381 (gnus-message 5 "Fetching %s...done" group))))
383 (defun gnus-agent-add-group (category arg)
384 "Add the current group to an agent category."
390 (mapcar (lambda (cat) (list (symbol-name (car cat))))
394 (let ((cat (assq category gnus-category-alist))
396 (gnus-group-iterate arg
398 (when (cadddr (setq c (gnus-group-category group)))
399 (setf (cadddr c) (delete group (cadddr c))))
400 (push group groups)))
401 (setf (cadddr cat) (nconc (cadddr cat) groups))
402 (gnus-category-write)))
404 (defun gnus-agent-remove-group (arg)
405 "Remove the current group from its agent category, if any."
408 (gnus-group-iterate arg
410 (when (cadddr (setq c (gnus-group-category group)))
411 (setf (cadddr c) (delete group (cadddr c))))))
412 (gnus-category-write)))
415 ;;; Server mode commands
418 (defun gnus-agent-add-server (server)
419 "Enroll SERVER in the agent program."
420 (interactive (list (gnus-server-server-name)))
422 (error "No server on the current line"))
423 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
424 (when (member method gnus-agent-covered-methods)
425 (error "Server already in the agent program"))
426 (push method gnus-agent-covered-methods)
427 (gnus-agent-write-servers)
428 (message "Entered %s into the Agent" server)))
430 (defun gnus-agent-remove-server (server)
431 "Remove SERVER from the agent program."
432 (interactive (list (gnus-server-server-name)))
434 (error "No server on the current line"))
435 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
436 (unless (member method gnus-agent-covered-methods)
437 (error "Server not in the agent program"))
438 (setq gnus-agent-covered-methods
439 (delete method gnus-agent-covered-methods))
440 (gnus-agent-write-servers)
441 (message "Removed %s from the agent" server)))
443 (defun gnus-agent-read-servers ()
444 "Read the alist of covered servers."
445 (setq gnus-agent-covered-methods
446 (gnus-agent-read-file
447 (nnheader-concat gnus-agent-directory "lib/servers"))))
449 (defun gnus-agent-write-servers ()
450 "Write the alist of covered servers."
451 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
452 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
453 (prin1 gnus-agent-covered-methods (current-buffer))))
459 (defun gnus-agent-mark-article (n &optional unmark)
460 "Mark the next N articles as downloadable.
461 If N is negative, mark backward instead. If UNMARK is non-nil, remove
462 the mark instead. The difference between N and the actual number of
463 articles marked is returned."
465 (let ((backward (< n 0))
470 (gnus-summary-set-agent-mark
471 (gnus-summary-article-number) unmark)
472 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
475 (gnus-message 7 "No more articles"))
476 (gnus-summary-recenter)
477 (gnus-summary-position-point)
480 (defun gnus-agent-unmark-article (n)
481 "Remove the downloadable mark from the next N articles.
482 If N is negative, unmark backward instead. The difference between N and
483 the actual number of articles unmarked is returned."
485 (gnus-agent-mark-article n t))
487 (defun gnus-agent-toggle-mark (n)
488 "Toggle the downloadable mark from the next N articles.
489 If N is negative, toggle backward instead. The difference between N and
490 the actual number of articles toggled is returned."
492 (gnus-agent-mark-article n 'toggle))
494 (defun gnus-summary-set-agent-mark (article &optional unmark)
495 "Mark ARTICLE as downloadable."
496 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
497 (memq article gnus-newsgroup-downloadable)
501 (setq gnus-newsgroup-downloadable
502 (delq article gnus-newsgroup-downloadable))
503 (push article gnus-newsgroup-undownloaded))
504 (setq gnus-newsgroup-undownloaded
505 (delq article gnus-newsgroup-undownloaded))
506 (push article gnus-newsgroup-downloadable))
507 (gnus-summary-update-mark
508 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
511 (defun gnus-agent-get-undownloaded-list ()
512 "Mark all unfetched articles as read."
513 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
514 (when (and (not gnus-plugged)
515 (gnus-agent-method-p gnus-command-method))
516 (gnus-agent-load-alist gnus-newsgroup-name)
517 ;; First mark all undownloaded articles as undownloaded.
518 (let ((articles (append gnus-newsgroup-unreads
519 gnus-newsgroup-marked
520 gnus-newsgroup-dormant))
522 (while (setq article (pop articles))
523 (unless (or (cdr (assq article gnus-agent-article-alist))
524 (memq article gnus-newsgroup-downloadable))
525 (push article gnus-newsgroup-undownloaded))))
526 ;; Then mark downloaded downloadable as not-downloadable,
527 ;; if you get my drift.
528 (let ((articles gnus-newsgroup-downloadable)
530 (while (setq article (pop articles))
531 (when (cdr (assq article gnus-agent-article-alist))
532 (setq gnus-newsgroup-downloadable
533 (delq article gnus-newsgroup-downloadable))))))))
535 (defun gnus-agent-catchup ()
536 "Mark all undownloaded articles as read."
539 (while gnus-newsgroup-undownloaded
540 (gnus-summary-mark-article
541 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
542 (gnus-summary-position-point))
545 ;;; Internal functions
548 (defun gnus-agent-save-active (method)
549 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
551 (defun gnus-agent-save-active-1 (method function)
552 (when (gnus-agent-method-p method)
553 (let* ((gnus-command-method method)
554 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
555 (file (gnus-agent-lib-file "active")))
556 (funcall function nil new)
557 (gnus-agent-write-active file new)
559 (insert-file-contents-literally file))))
561 (defun gnus-agent-write-active (file new)
562 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
563 (file (gnus-agent-lib-file "active"))
565 (when (file-exists-p file)
567 (insert-file-contents file)
568 (gnus-active-to-gnus-format nil orig))
571 (when (and sym (boundp sym))
572 (if (setq elem (symbol-value (intern (symbol-name sym) orig)))
573 (setcdr elem (cdr (symbol-value sym)))
574 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
576 (gnus-make-directory (file-name-directory file))
577 (let ((coding-system-for-write gnus-agent-file-coding-system))
578 (gnus-write-active-file file orig))))
580 (defun gnus-agent-save-groups (method)
581 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
583 (defun gnus-agent-save-group-info (method group active)
584 (when (gnus-agent-method-p method)
585 (let* ((gnus-command-method method)
586 (file (gnus-agent-lib-file "active")))
587 (gnus-make-directory (file-name-directory file))
589 (when (file-exists-p file)
590 (mm-insert-file-contents file))
591 (goto-char (point-min))
592 (when (re-search-forward
593 (concat "^" (regexp-quote group) " ") nil t)
595 (insert group " " (number-to-string (cdr active)) " "
596 (number-to-string (car active)) " y\n")))))
598 (defun gnus-agent-group-path (group)
599 "Translate GROUP into a path."
600 (if nnmail-use-long-file-names
601 (gnus-group-real-name group)
602 (nnheader-replace-chars-in-string
603 (nnheader-translate-file-chars (gnus-group-real-name group))
608 (defun gnus-agent-method-p (method)
609 "Say whether METHOD is covered by the agent."
610 (member method gnus-agent-covered-methods))
612 (defun gnus-agent-get-function (method)
613 (if (and (not gnus-plugged)
614 (gnus-agent-method-p method))
620 ;;; History functions
622 (defun gnus-agent-history-buffer ()
623 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
625 (defun gnus-agent-open-history ()
627 (push (cons (gnus-agent-method)
628 (set-buffer (gnus-get-buffer-create
629 (format " *Gnus agent %s history*"
630 (gnus-agent-method)))))
631 gnus-agent-history-buffers)
634 (let ((file (gnus-agent-lib-file "history")))
635 (when (file-exists-p file)
637 (set (make-local-variable 'gnus-agent-file-name) file))))
639 (defun gnus-agent-save-history ()
641 (set-buffer gnus-agent-current-history)
642 (gnus-make-directory (file-name-directory gnus-agent-file-name))
643 (let ((coding-system-for-write gnus-agent-file-coding-system))
644 (write-region (1+ (point-min)) (point-max)
645 gnus-agent-file-name nil 'silent))))
647 (defun gnus-agent-close-history ()
648 (when (gnus-buffer-live-p gnus-agent-current-history)
649 (kill-buffer gnus-agent-current-history)
650 (setq gnus-agent-history-buffers
651 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
652 gnus-agent-history-buffers))))
654 (defun gnus-agent-enter-history (id group-arts date)
656 (set-buffer gnus-agent-current-history)
657 (goto-char (point-max))
658 (insert id "\t" (number-to-string date) "\t")
660 (insert (caar group-arts) " " (number-to-string (cdr (pop group-arts)))
664 (defun gnus-agent-article-in-history-p (id)
666 (set-buffer (gnus-agent-history-buffer))
667 (goto-char (point-min))
668 (search-forward (concat "\n" id "\t") nil t)))
670 (defun gnus-agent-history-path (id)
672 (set-buffer (gnus-agent-history-buffer))
673 (goto-char (point-min))
674 (when (search-forward (concat "\n" id "\t") nil t)
675 (let ((method (gnus-agent-method)))
677 (while (not (numberp (setq group (read (current-buffer)))))
678 (push (concat method "/" group) paths))
679 (nreverse paths))))))
685 (defun gnus-agent-fetch-articles (group articles)
686 "Fetch ARTICLES from GROUP and put them into the Agent."
688 ;; Prune off articles that we have already fetched.
690 (cdr (assq (car articles) gnus-agent-article-alist)))
692 (let ((arts articles))
694 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
695 (setcdr arts (cddr arts))
696 (setq arts (cdr arts)))))
699 (gnus-agent-directory)
700 (gnus-agent-group-path group) "/"))
701 (date (time-to-days (current-time)))
704 (gnus-make-directory dir)
705 (gnus-message 7 "Fetching articles for %s..." group)
706 ;; Fetch the articles from the backend.
707 (if (gnus-check-backend-function 'retrieve-articles group)
708 (setq pos (gnus-retrieve-articles articles group))
711 (while (setq article (pop articles))
712 (when (gnus-request-article article group)
713 (goto-char (point-max))
714 (push (cons article (point)) pos)
715 (insert-buffer-substring nntp-server-buffer)))
716 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
717 (setq pos (nreverse pos)))))
718 ;; Then save these articles into the Agent.
720 (set-buffer nntp-server-buffer)
722 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
723 (goto-char (point-min))
724 (when (search-forward "\n\n" nil t)
725 (when (search-backward "\nXrefs: " nil t)
726 ;; Handle crossposting.
727 (skip-chars-forward "^ ")
728 (skip-chars-forward " ")
730 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
731 (push (cons (buffer-substring (match-beginning 1)
733 (buffer-substring (match-beginning 2)
736 (goto-char (match-end 0)))
737 (gnus-agent-crosspost crosses (caar pos))))
738 (goto-char (point-min))
739 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
740 (setq id "No-Message-ID-in-article")
741 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
742 (let ((coding-system-for-write
743 gnus-agent-file-coding-system))
744 (write-region (point-min) (point-max)
745 (concat dir (number-to-string (caar pos)))
747 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
749 (gnus-agent-enter-history
750 id (or crosses (list (cons group (caar pos)))) date)
753 (gnus-agent-save-alist group)))))
755 (defun gnus-agent-crosspost (crosses article)
756 (let (gnus-agent-article-alist group alist beg end)
758 (set-buffer gnus-agent-overview-buffer)
759 (when (nnheader-find-nov-line article)
762 (setq end (progn (forward-line 1) (point)))))
764 (setq group (caar crosses))
765 (unless (setq alist (assoc group gnus-agent-group-alist))
766 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
767 gnus-agent-group-alist))
768 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
770 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
772 (when (= (point-max) (point-min))
773 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
775 (mm-insert-file-contents
776 (gnus-agent-article-name ".overview" group))))
777 (nnheader-find-nov-line (string-to-number (cdar crosses)))
778 (insert (string-to-number (cdar crosses)))
779 (insert-buffer-substring gnus-agent-overview-buffer beg end))
782 (defun gnus-agent-flush-cache ()
784 (while gnus-agent-buffer-alist
785 (set-buffer (cdar gnus-agent-buffer-alist))
786 (let ((coding-system-for-write
787 gnus-agent-file-coding-system))
788 (write-region (point-min) (point-max)
789 (gnus-agent-article-name ".overview"
790 (caar gnus-agent-buffer-alist))
792 (pop gnus-agent-buffer-alist))
793 (while gnus-agent-group-alist
794 (with-temp-file (caar gnus-agent-group-alist)
795 (princ (cdar gnus-agent-group-alist))
797 (pop gnus-agent-group-alist))))
799 (defun gnus-agent-fetch-headers (group &optional force)
800 (let ((articles (gnus-list-of-unread-articles group))
801 (gnus-decode-encoded-word-function 'identity)
802 (file (gnus-agent-article-name ".overview" group)))
803 ;; add article with marks to list of article headers we want to fetch
804 (dolist (arts (gnus-info-marks (gnus-get-info group)))
805 (setq articles (union (gnus-uncompress-sequence (cdr arts))
807 (setq articles (sort articles '<))
808 ;; remove known articles
809 (when (gnus-agent-load-alist group)
810 (setq articles (gnus-sorted-intersection
812 (gnus-uncompress-range
813 (cons (1+ (caar (last gnus-agent-article-alist)))
814 (cdr (gnus-active group)))))))
816 (gnus-make-directory (nnheader-translate-file-chars
817 (file-name-directory file)))
819 (gnus-message 7 "Fetching headers for %s..." group)
821 (set-buffer nntp-server-buffer)
822 (unless (eq 'nov (gnus-retrieve-headers articles group))
823 (nnvirtual-convert-headers))
824 ;; Save these headers for later processing.
825 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
826 (when (file-exists-p file)
827 (gnus-agent-braid-nov group articles file))
828 (let ((coding-system-for-write
829 gnus-agent-file-coding-system))
830 (write-region (point-min) (point-max) file nil 'silent))
831 (gnus-agent-save-alist group articles nil)
832 (gnus-agent-enter-history
833 "last-header-fetched-for-session"
834 (list (cons group (nth (- (length articles) 1) articles)))
835 (time-to-days (current-time)))
838 (defsubst gnus-agent-copy-nov-line (article)
840 (set-buffer gnus-agent-overview-buffer)
842 (if (eq article (read (current-buffer)))
843 (setq e (progn (forward-line 1) (point)))
847 (set-buffer nntp-server-buffer)
848 (insert-buffer-substring gnus-agent-overview-buffer b e)))
850 (defun gnus-agent-braid-nov (group articles file)
851 (set-buffer gnus-agent-overview-buffer)
852 (goto-char (point-min))
853 (set-buffer nntp-server-buffer)
855 (mm-insert-file-contents file)
856 (goto-char (point-max))
857 (if (or (= (point-min) (point-max))
860 (< (read (current-buffer)) (car articles))))
861 ;; We have only headers that are after the older headers,
862 ;; so we just append them.
864 (goto-char (point-max))
865 (insert-buffer-substring gnus-agent-overview-buffer))
866 ;; We do it the hard way.
867 (nnheader-find-nov-line (car articles))
868 (gnus-agent-copy-nov-line (car articles))
872 (while (and (not (eobp))
873 (< (read (current-buffer)) (car articles)))
877 (gnus-agent-copy-nov-line (car articles))
878 (setq articles (cdr articles))))
881 (set-buffer gnus-agent-overview-buffer)
884 (set-buffer nntp-server-buffer)
885 (insert-buffer-substring gnus-agent-overview-buffer b e)))))
887 (defun gnus-agent-load-alist (group &optional dir)
888 "Load the article-state alist for GROUP."
889 (setq gnus-agent-article-alist
890 (gnus-agent-read-file
892 (concat dir ".agentview")
893 (gnus-agent-article-name ".agentview" group)))))
895 (defun gnus-agent-save-alist (group &optional articles state dir)
896 "Save the article-state alist for GROUP."
897 (with-temp-file (if dir
898 (concat dir ".agentview")
899 (gnus-agent-article-name ".agentview" group))
900 (princ (setq gnus-agent-article-alist
901 (nconc gnus-agent-article-alist
902 (mapcar (lambda (article) (cons article state))
907 (defun gnus-agent-article-name (article group)
908 (concat (gnus-agent-directory) (gnus-agent-group-path group) "/"
909 (if (stringp article) article (string-to-number article))))
912 (defun gnus-agent-batch-fetch ()
913 "Start Gnus and fetch session."
916 (gnus-agent-fetch-session)
919 (defun gnus-agent-fetch-session ()
920 "Fetch all articles and headers that are eligible for fetching."
922 (unless gnus-agent-covered-methods
923 (error "No servers are covered by the Gnus agent"))
925 (error "Can't fetch articles while Gnus is unplugged"))
926 (let ((methods gnus-agent-covered-methods)
927 groups group gnus-command-method)
930 (setq gnus-command-method (car methods))
931 (when (or (gnus-server-opened gnus-command-method)
932 (gnus-open-server gnus-command-method))
933 (setq groups (gnus-groups-from-server (car methods)))
934 (gnus-agent-with-fetch
935 (while (setq group (pop groups))
936 (when (<= (gnus-group-level group) gnus-agent-handle-level)
937 (gnus-agent-fetch-group-1 group gnus-command-method)))))
939 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
941 (defun gnus-agent-fetch-group-1 (group method)
943 (let ((gnus-command-method method)
944 (gnus-newsgroup-name group)
945 gnus-newsgroup-dependencies gnus-newsgroup-headers
946 gnus-newsgroup-scored gnus-headers gnus-score
947 gnus-use-cache articles arts
948 category predicate info marks score-param)
950 (when (and (or (gnus-active group) (gnus-activate-group group))
951 (setq articles (gnus-agent-fetch-headers group)))
952 ;; Parse them and see which articles we want to fetch.
953 (setq gnus-newsgroup-dependencies
954 (make-vector (length articles) 0))
955 ;; No need to call `gnus-get-newsgroup-headers-xover' with
956 ;; the entire .overview for group as we still have the just
957 ;; downloaded headers in `gnus-agent-overview-buffer'.
958 (let ((nntp-server-buffer gnus-agent-overview-buffer))
959 (setq gnus-newsgroup-headers
960 (gnus-get-newsgroup-headers-xover articles nil nil group)))
961 (setq category (gnus-group-category group))
964 (or (gnus-group-get-parameter group 'agent-predicate t)
966 ;; Do we want to download everything, or nothing?
967 (if (or (eq (caaddr predicate) 'gnus-agent-true)
968 (eq (caaddr predicate) 'gnus-agent-false))
970 (setq arts (symbol-value
971 (cadr (assoc (caaddr predicate)
972 '((gnus-agent-true articles)
973 (gnus-agent-false nil))))))
974 ;; No, we need to decide what we want.
978 (gnus-group-get-parameter group 'agent-score t)
981 (require 'gnus-score)
982 (if (eq score-method 'file)
984 (gnus-score-load-files
985 (gnus-all-score-files group)))
987 (while (setq list (car entries))
988 (push (car list) score-file)
989 (setq list (cdr list))
991 (when (member (caar list)
992 gnus-agent-scoreable-headers)
993 (push (car list) score-file))
994 (setq list (cdr list)))
996 (append score-param (list (nreverse score-file)))
997 score-file nil entries (cdr entries)))
999 (if (stringp (car score-method))
1001 (list (list score-method)))))))
1003 (gnus-score-headers score-param))
1005 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1007 (or (cdr (assq (mail-header-number gnus-headers)
1008 gnus-newsgroup-scored))
1009 gnus-summary-default-score))
1010 (when (funcall predicate)
1011 (push (mail-header-number gnus-headers)
1013 ;; Fetch the articles.
1015 (gnus-agent-fetch-articles group arts)))
1016 ;; Perhaps we have some additional articles to fetch.
1017 (setq arts (assq 'download (gnus-info-marks
1018 (setq info (gnus-get-info group)))))
1020 (gnus-agent-fetch-articles
1021 group (gnus-uncompress-range (cdr arts)))
1022 (setq marks (delq arts (gnus-info-marks info)))
1023 (gnus-info-set-marks info marks)
1025 (concat "(gnus-group-set-info '"
1026 (gnus-prin1-to-string info)
1030 ;;; Agent Category Mode
1033 (defvar gnus-category-mode-hook nil
1034 "Hook run in `gnus-category-mode' buffers.")
1036 (defvar gnus-category-line-format " %(%20c%): %g\n"
1037 "Format of category lines.")
1039 (defvar gnus-category-mode-line-format "Gnus: %%b"
1040 "The format specification for the category mode line.")
1042 (defvar gnus-agent-short-article 100
1043 "Articles that have fewer lines than this are short.")
1045 (defvar gnus-agent-long-article 200
1046 "Articles that have more lines than this are long.")
1048 (defvar gnus-agent-low-score 0
1049 "Articles that have a score lower than this have a low score.")
1051 (defvar gnus-agent-high-score 0
1052 "Articles that have a score higher than this have a high score.")
1055 ;;; Internal variables.
1057 (defvar gnus-category-buffer "*Agent Category*")
1059 (defvar gnus-category-line-format-alist
1060 `((?c gnus-tmp-name ?s)
1061 (?g gnus-tmp-groups ?d)))
1063 (defvar gnus-category-mode-line-format-alist
1064 `((?u user-defined ?s)))
1066 (defvar gnus-category-line-format-spec nil)
1067 (defvar gnus-category-mode-line-format-spec nil)
1069 (defvar gnus-category-mode-map nil)
1070 (put 'gnus-category-mode 'mode-class 'special)
1072 (unless gnus-category-mode-map
1073 (setq gnus-category-mode-map (make-sparse-keymap))
1074 (suppress-keymap gnus-category-mode-map)
1076 (gnus-define-keys gnus-category-mode-map
1077 "q" gnus-category-exit
1078 "k" gnus-category-kill
1079 "c" gnus-category-copy
1080 "a" gnus-category-add
1081 "p" gnus-category-edit-predicate
1082 "g" gnus-category-edit-groups
1083 "s" gnus-category-edit-score
1084 "l" gnus-category-list
1086 "\C-c\C-i" gnus-info-find-node
1087 "\C-c\C-b" gnus-bug))
1089 (defvar gnus-category-menu-hook nil
1090 "*Hook run after the creation of the menu.")
1092 (defun gnus-category-make-menu-bar ()
1093 (gnus-turn-off-edit-menu 'category)
1094 (unless (boundp 'gnus-category-menu)
1096 gnus-category-menu gnus-category-mode-map ""
1098 ["Add" gnus-category-add t]
1099 ["Kill" gnus-category-kill t]
1100 ["Copy" gnus-category-copy t]
1101 ["Edit predicate" gnus-category-edit-predicate t]
1102 ["Edit score" gnus-category-edit-score t]
1103 ["Edit groups" gnus-category-edit-groups t]
1104 ["Exit" gnus-category-exit t]))
1106 (gnus-run-hooks 'gnus-category-menu-hook)))
1108 (defun gnus-category-mode ()
1109 "Major mode for listing and editing agent categories.
1111 All normal editing commands are switched off.
1112 \\<gnus-category-mode-map>
1113 For more in-depth information on this mode, read the manual
1114 (`\\[gnus-info-find-node]').
1116 The following commands are available:
1118 \\{gnus-category-mode-map}"
1120 (when (gnus-visual-p 'category-menu 'menu)
1121 (gnus-category-make-menu-bar))
1122 (kill-all-local-variables)
1123 (gnus-simplify-mode-line)
1124 (setq major-mode 'gnus-category-mode)
1125 (setq mode-name "Category")
1126 (gnus-set-default-directory)
1127 (setq mode-line-process nil)
1128 (use-local-map gnus-category-mode-map)
1129 (buffer-disable-undo)
1130 (setq truncate-lines t)
1131 (setq buffer-read-only t)
1132 (gnus-run-hooks 'gnus-category-mode-hook))
1134 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1136 (defun gnus-category-insert-line (category)
1137 (let* ((gnus-tmp-name (car category))
1138 (gnus-tmp-groups (length (cadddr category))))
1140 (gnus-add-text-properties
1144 (eval gnus-category-line-format-spec))
1145 (list 'gnus-category gnus-tmp-name))))
1147 (defun gnus-enter-category-buffer ()
1148 "Go to the Category buffer."
1150 (gnus-category-setup-buffer)
1151 (gnus-configure-windows 'category)
1152 (gnus-category-prepare))
1154 (defun gnus-category-setup-buffer ()
1155 (unless (get-buffer gnus-category-buffer)
1157 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1158 (gnus-category-mode))))
1160 (defun gnus-category-prepare ()
1161 (gnus-set-format 'category-mode)
1162 (gnus-set-format 'category t)
1163 (let ((alist gnus-category-alist)
1164 (buffer-read-only nil))
1167 (gnus-category-insert-line (pop alist)))
1168 (goto-char (point-min))
1169 (gnus-category-position-point)))
1171 (defun gnus-category-name ()
1172 (or (get-text-property (gnus-point-at-bol) 'gnus-category)
1173 (error "No category on the current line")))
1175 (defun gnus-category-read ()
1176 "Read the category alist."
1177 (setq gnus-category-alist
1178 (or (gnus-agent-read-file
1179 (nnheader-concat gnus-agent-directory "lib/categories"))
1180 (list (list 'default 'short nil nil)))))
1182 (defun gnus-category-write ()
1183 "Write the category alist."
1184 (setq gnus-category-predicate-cache nil
1185 gnus-category-group-cache nil)
1186 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1187 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1188 (prin1 gnus-category-alist (current-buffer))))
1190 (defun gnus-category-edit-predicate (category)
1191 "Edit the predicate for CATEGORY."
1192 (interactive (list (gnus-category-name)))
1193 (let ((info (assq category gnus-category-alist)))
1195 (cadr info) (format "Editing the predicate for category %s" category)
1196 `(lambda (predicate)
1197 (setf (cadr (assq ',category gnus-category-alist)) predicate)
1198 (gnus-category-write)
1199 (gnus-category-list)))))
1201 (defun gnus-category-edit-score (category)
1202 "Edit the score expression for CATEGORY."
1203 (interactive (list (gnus-category-name)))
1204 (let ((info (assq category gnus-category-alist)))
1207 (format "Editing the score expression for category %s" category)
1209 (setf (caddr (assq ',category gnus-category-alist)) groups)
1210 (gnus-category-write)
1211 (gnus-category-list)))))
1213 (defun gnus-category-edit-groups (category)
1214 "Edit the group list for CATEGORY."
1215 (interactive (list (gnus-category-name)))
1216 (let ((info (assq category gnus-category-alist)))
1218 (cadddr info) (format "Editing the group list for category %s" category)
1220 (setf (cadddr (assq ',category gnus-category-alist)) groups)
1221 (gnus-category-write)
1222 (gnus-category-list)))))
1224 (defun gnus-category-kill (category)
1225 "Kill the current category."
1226 (interactive (list (gnus-category-name)))
1227 (let ((info (assq category gnus-category-alist))
1228 (buffer-read-only nil))
1230 (gnus-category-write)
1231 (setq gnus-category-alist (delq info gnus-category-alist))))
1233 (defun gnus-category-copy (category to)
1234 "Copy the current category."
1235 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1236 (let ((info (assq category gnus-category-alist)))
1237 (push (list to (gnus-copy-sequence (cadr info))
1238 (gnus-copy-sequence (caddr info)) nil)
1239 gnus-category-alist)
1240 (gnus-category-write)
1241 (gnus-category-list)))
1243 (defun gnus-category-add (category)
1244 "Create a new category."
1245 (interactive "SCategory name: ")
1246 (when (assq category gnus-category-alist)
1247 (error "Category %s already exists" category))
1248 (push (list category 'false nil nil)
1249 gnus-category-alist)
1250 (gnus-category-write)
1251 (gnus-category-list))
1253 (defun gnus-category-list ()
1254 "List all categories."
1256 (gnus-category-prepare))
1258 (defun gnus-category-exit ()
1259 "Return to the group buffer."
1261 (kill-buffer (current-buffer))
1262 (gnus-configure-windows 'group t))
1264 ;; To avoid having 8-bit characters in the source file.
1265 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1267 (defvar gnus-category-predicate-alist
1268 '((spam . gnus-agent-spam-p)
1269 (short . gnus-agent-short-p)
1270 (long . gnus-agent-long-p)
1271 (low . gnus-agent-low-scored-p)
1272 (high . gnus-agent-high-scored-p)
1273 (true . gnus-agent-true)
1274 (false . gnus-agent-false))
1275 "Mapping from short score predicate symbols to predicate functions.")
1277 (defun gnus-agent-spam-p ()
1278 "Say whether an article is spam or not."
1279 (unless gnus-agent-spam-hashtb
1280 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1281 (if (not (equal (mail-header-references gnus-headers) ""))
1283 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1285 (gnus-gethash string gnus-agent-spam-hashtb)
1286 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1288 (defun gnus-agent-short-p ()
1289 "Say whether an article is short or not."
1290 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1292 (defun gnus-agent-long-p ()
1293 "Say whether an article is long or not."
1294 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1296 (defun gnus-agent-low-scored-p ()
1297 "Say whether an article has a low score or not."
1298 (< gnus-score gnus-agent-low-score))
1300 (defun gnus-agent-high-scored-p ()
1301 "Say whether an article has a high score or not."
1302 (> gnus-score gnus-agent-high-score))
1304 (defun gnus-category-make-function (cat)
1305 "Make a function from category CAT."
1306 `(lambda () ,(gnus-category-make-function-1 cat)))
1308 (defun gnus-agent-true ()
1312 (defun gnus-agent-false ()
1316 (defun gnus-category-make-function-1 (cat)
1317 "Make a function from category CAT."
1319 ;; Functions are just returned as is.
1321 (gnus-functionp cat))
1322 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1324 ;; More complex category.
1327 ((memq (car cat) '(& and))
1329 ((memq (car cat) '(| or))
1331 ((memq (car cat) gnus-category-not)
1333 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1335 (error "Unknown category type: %s" cat))))
1337 (defun gnus-get-predicate (predicate)
1338 "Return the predicate for CATEGORY."
1339 (or (cdr (assoc predicate gnus-category-predicate-cache))
1340 (cdar (push (cons predicate
1341 (gnus-category-make-function predicate))
1342 gnus-category-predicate-cache))))
1344 (defun gnus-group-category (group)
1345 "Return the category GROUP belongs to."
1346 (unless gnus-category-group-cache
1347 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1348 (let ((cs gnus-category-alist)
1350 (while (setq cat (pop cs))
1351 (setq groups (cadddr cat))
1353 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1354 (or (gnus-gethash group gnus-category-group-cache)
1355 (assq 'default gnus-category-alist)))
1357 (defun gnus-agent-expire ()
1358 "Expire all old articles."
1360 (let ((methods gnus-agent-covered-methods)
1361 (day (- (time-to-days (current-time)) gnus-agent-expire-days))
1362 gnus-command-method sym group articles
1363 history overview file histories elem art nov-file low info
1364 unreads marked article orig lowest highest)
1367 (insert-file-contents file)
1368 (gnus-active-to-gnus-format
1369 nil (setq orig (gnus-make-hashtable
1370 (count-lines (point-min) (point-max))))))
1371 (setq overview (gnus-get-buffer-create " *expire overview*"))
1372 (while (setq gnus-command-method (pop methods))
1373 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1374 (gnus-agent-open-history)
1376 (setq gnus-agent-current-history
1377 (setq history (gnus-agent-history-buffer))))
1378 (goto-char (point-min))
1379 (when (> (buffer-size) 1)
1380 (goto-char (point-min))
1382 (skip-chars-forward "^\t")
1383 (if (> (read (current-buffer)) day)
1384 ;; New article; we don't expire it.
1386 ;; Old article. Schedule it for possible nuking.
1388 (setq sym (let ((obarray expiry-hashtb))
1389 (read (current-buffer))))
1391 (set sym (cons (cons (read (current-buffer)) (point))
1392 (symbol-value sym)))
1393 (set sym (list (cons (read (current-buffer)) (point)))))
1394 (skip-chars-forward " "))
1396 ;; We now have all articles that can possibly be expired.
1399 (setq group (symbol-name sym)
1400 articles (sort (symbol-value sym) 'car-less-than-car)
1401 low (car (gnus-active group))
1402 info (gnus-get-info group)
1403 unreads (ignore-errors (gnus-list-of-unread-articles group))
1404 marked (nconc (gnus-uncompress-range
1405 (cdr (assq 'tick (gnus-info-marks info))))
1406 (gnus-uncompress-range
1408 (gnus-info-marks info)))))
1409 nov-file (gnus-agent-article-name ".overview" group)
1412 (gnus-agent-load-alist group)
1413 (gnus-message 5 "Expiring articles in %s" group)
1414 (set-buffer overview)
1416 (when (file-exists-p nov-file)
1417 (mm-insert-file-contents nov-file))
1418 (goto-char (point-min))
1420 (while (setq elem (pop articles))
1421 (setq article (car elem))
1422 (when (or (null low)
1424 gnus-agent-expire-all
1425 (and (not (memq article unreads))
1426 (not (memq article marked))))
1427 ;; Find and nuke the NOV line.
1428 (while (and (not (eobp))
1430 (setq art (read (current-buffer)))))
1433 (gnus-agent-article-name
1434 (number-to-string art) group))
1440 ;; Remove old NOV lines that have no articles.
1441 (gnus-delete-line)))
1446 ;; Nuke the article.
1447 (when (file-exists-p (setq file (gnus-agent-article-name
1448 (number-to-string article)
1451 ;; Schedule the history line for nuking.
1452 (push (cdr elem) histories)))
1453 (gnus-make-directory (file-name-directory nov-file))
1454 (let ((coding-system-for-write
1455 gnus-agent-file-coding-system))
1456 (write-region (point-min) (point-max) nov-file nil 'silent))
1457 ;; Delete the unwanted entries in the alist.
1458 (setq gnus-agent-article-alist
1459 (sort gnus-agent-article-alist 'car-less-than-car))
1460 (let* ((alist gnus-agent-article-alist)
1461 (prev (cons nil alist))
1465 (<= (caar alist) article))
1466 (if (or (not (cdar alist))
1468 (gnus-agent-article-name
1473 (push (caar alist) expired)
1474 (setcdr prev (setq alist (cdr alist))))
1476 alist (cdr alist))))
1477 (setq gnus-agent-article-alist (cdr first))
1478 (gnus-agent-save-alist group)
1479 ;; Mark all articles up to the first article
1480 ;; in `gnus-article-alist' as read.
1481 (when (and info (caar gnus-agent-article-alist))
1482 (setcar (nthcdr 2 info)
1485 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1486 ;; Maybe everything has been expired from `gnus-article-alist'
1487 ;; and so the above marking as read could not be conducted,
1488 ;; or there are expired article within the range of the alist.
1491 (or (not (caar gnus-agent-article-alist))
1493 (caar gnus-agent-article-alist))))
1494 (setcar (nthcdr 2 info)
1497 (nreverse expired))))
1499 (concat "(gnus-group-set-info '"
1500 (gnus-prin1-to-string info)
1503 (if (gnus-gethash group orig)
1504 (setcar (gnus-gethash group orig) lowest)
1505 (gnus-sethash group (cons lowest highest) orig))))
1507 (set-buffer history)
1508 (setq histories (nreverse (sort histories '<)))
1510 (goto-char (pop histories))
1512 (gnus-agent-save-history)
1513 (gnus-agent-close-history)
1514 (gnus-write-active-file
1515 (gnus-agent-lib-file "active") orig))
1516 (gnus-message 4 "Expiry...done"))))))
1519 (defun gnus-agent-batch ()
1521 (let ((init-file-user "")
1522 (gnus-always-read-dribble-file t))
1524 (gnus-group-send-drafts)
1525 (gnus-agent-fetch-session))
1527 (provide 'gnus-agent)
1529 ;;; gnus-agent.el ends here