1 ;;; gnus-agent.el --- unplugged support for Semi-gnus
2 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
29 (eval-when-compile (require 'gnus-clfns))
38 (if (featurep 'xemacs)
41 (require 'gnus-group))
44 (autoload 'gnus-server-update-server "gnus-srvr"))
46 (defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
47 "Where the Gnus agent will store its files."
51 (defcustom gnus-agent-plugged-hook nil
52 "Hook run when plugging into the network."
56 (defcustom gnus-agent-unplugged-hook nil
57 "Hook run when unplugging from the network."
61 (defcustom gnus-agent-fetched-hook nil
62 "Hook run after finishing fetching articles."
66 (defcustom gnus-agent-handle-level gnus-level-subscribed
67 "Groups on levels higher than this variable will be ignored by the Agent."
71 (defcustom gnus-agent-expire-days 7
72 "Read articles older than this will be expired.
73 This can also be a list of regexp/day pairs. The regexps will
74 be matched against group names."
78 (defcustom gnus-agent-expire-all nil
79 "If non-nil, also expire unread, ticked and dormant articles.
80 If nil, only read articles will be expired."
84 (defcustom gnus-agent-group-mode-hook nil
85 "Hook run in Agent group minor modes."
89 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
90 (when (featurep 'xemacs)
91 (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))
93 (defcustom gnus-agent-summary-mode-hook nil
94 "Hook run in Agent summary minor modes."
98 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
99 (when (featurep 'xemacs)
100 (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))
102 (defcustom gnus-agent-server-mode-hook nil
103 "Hook run in Agent summary minor modes."
107 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
108 (when (featurep 'xemacs)
109 (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))
111 (defcustom gnus-agent-confirmation-function 'y-or-n-p
112 "Function to confirm when error happens."
117 (defcustom gnus-agent-large-newsgroup nil
118 "*The number of articles which indicates a large newsgroup.
119 If the number of unread articles exceeds it, The number of articles to be
120 fetched will be limited to it. If not a positive integer, never consider it."
122 :type '(choice (const nil)
123 (integer :tag "Number")))
125 (defcustom gnus-agent-synchronize-flags 'ask
126 "Indicate if flags are synchronized when you plug in.
127 If this is `ask' the hook will query the user."
129 :type '(choice (const :tag "Always" t)
130 (const :tag "Never" nil)
131 (const :tag "Ask" ask))
134 (defcustom gnus-agent-go-online 'ask
135 "Indicate if offline servers go online when you plug in.
136 If this is `ask' the hook will query the user."
138 :type '(choice (const :tag "Always" t)
139 (const :tag "Never" nil)
140 (const :tag "Ask" ask))
143 (defcustom gnus-agent-mark-unread-after-downloaded t
144 "Indicate whether to mark articles unread after downloaded."
149 (defcustom gnus-agent-download-marks '(download)
150 "Marks for downloading."
152 :type '(repeat (symbol :tag "Mark"))
155 ;;; Internal variables
157 (defvar gnus-agent-history-buffers nil)
158 (defvar gnus-agent-buffer-alist nil)
159 (defvar gnus-agent-article-alist nil)
160 (defvar gnus-agent-group-alist nil)
161 (defvar gnus-category-alist nil)
162 (defvar gnus-agent-current-history nil)
163 (defvar gnus-agent-overview-buffer nil)
164 (defvar gnus-category-predicate-cache nil)
165 (defvar gnus-category-group-cache nil)
166 (defvar gnus-agent-spam-hashtb nil)
167 (defvar gnus-agent-file-name nil)
168 (defvar gnus-agent-send-mail-function nil)
169 (defvar gnus-agent-file-coding-system 'raw-text)
170 (defvar gnus-agent-file-loading-cache nil)
172 (defvar gnus-agent-auto-agentize-methods '(nntp nnimap)
173 "Initially, all servers from these methods are agentized.
174 The user may remove or add servers using the Server buffer. See Info
175 node `(gnus)Server Buffer'.")
178 (defvar gnus-headers)
185 (defun gnus-open-agent ()
187 (gnus-agent-read-servers)
189 (gnus-agent-create-buffer)
190 (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
191 (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
192 (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))
194 (defun gnus-agent-create-buffer ()
195 (if (gnus-buffer-live-p gnus-agent-overview-buffer)
197 (setq gnus-agent-overview-buffer
198 (gnus-get-buffer-create " *Gnus agent overview*"))
199 (with-current-buffer gnus-agent-overview-buffer
200 (set-buffer-multibyte t))
203 (gnus-add-shutdown 'gnus-close-agent 'gnus)
205 (defun gnus-close-agent ()
206 (setq gnus-agent-covered-methods nil
207 gnus-category-predicate-cache nil
208 gnus-category-group-cache nil
209 gnus-agent-spam-hashtb nil)
210 (gnus-kill-buffer gnus-agent-overview-buffer))
213 ;;; Utility functions
216 (defun gnus-agent-read-file (file)
217 "Load FILE and do a `read' there."
220 (nnheader-insert-file-contents file)
221 (goto-char (point-min))
222 (read (current-buffer)))))
224 (defsubst gnus-agent-method ()
225 (concat (symbol-name (car gnus-command-method)) "/"
226 (if (equal (cadr gnus-command-method) "")
228 (cadr gnus-command-method))))
230 (defsubst gnus-agent-directory ()
231 "Path of the Gnus agent directory."
232 (nnheader-concat gnus-agent-directory
233 (nnheader-translate-file-chars (gnus-agent-method)) "/"))
235 (defun gnus-agent-lib-file (file)
236 "The full path of the Gnus agent library FILE."
237 (expand-file-name file
238 (file-name-as-directory
239 (expand-file-name "agent.lib" (gnus-agent-directory)))))
241 ;;; Fetching setup functions.
243 (defun gnus-agent-start-fetch ()
244 "Initialize data structures for efficient fetching."
245 (gnus-agent-open-history)
246 (setq gnus-agent-current-history (gnus-agent-history-buffer))
247 (gnus-agent-create-buffer))
249 (defun gnus-agent-stop-fetch ()
250 "Save all data structures and clean up."
251 (gnus-agent-save-history)
252 (gnus-agent-close-history)
253 (setq gnus-agent-spam-hashtb nil)
255 (set-buffer nntp-server-buffer)
258 (defmacro gnus-agent-with-fetch (&rest forms)
261 (let ((gnus-agent-fetching t))
262 (gnus-agent-start-fetch)
264 (gnus-agent-stop-fetch)))
266 (put 'gnus-agent-with-fetch 'lisp-indent-function 0)
267 (put 'gnus-agent-with-fetch 'edebug-form-spec '(body))
273 (defvar gnus-agent-mode-hook nil
274 "Hook run when installing agent mode.")
276 (defvar gnus-agent-mode nil)
277 (defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))
279 (defun gnus-agent-mode ()
280 "Minor mode for providing a agent support in Gnus buffers."
281 (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
282 (symbol-name major-mode))
283 (match-string 1 (symbol-name major-mode))))
284 (mode (intern (format "gnus-agent-%s-mode" buffer))))
285 (set (make-local-variable 'gnus-agent-mode) t)
287 (set (make-local-variable mode) t)
289 (when (gnus-visual-p 'agent-menu 'menu)
290 (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
291 (unless (assq 'gnus-agent-mode minor-mode-alist)
292 (push gnus-agent-mode-status minor-mode-alist))
293 (unless (assq mode minor-mode-map-alist)
294 (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
296 minor-mode-map-alist))
297 (when (eq major-mode 'gnus-group-mode)
298 (gnus-agent-toggle-plugged gnus-plugged))
299 (gnus-run-hooks 'gnus-agent-mode-hook
300 (intern (format "gnus-agent-%s-mode-hook" buffer)))))
302 (defvar gnus-agent-group-mode-map (make-sparse-keymap))
303 (gnus-define-keys gnus-agent-group-mode-map
304 "Ju" gnus-agent-fetch-groups
305 "Jc" gnus-enter-category-buffer
306 "Jj" gnus-agent-toggle-plugged
307 "Js" gnus-agent-fetch-session
308 "JY" gnus-agent-synchronize-flags
309 "JS" gnus-group-send-queue
310 "Ja" gnus-agent-add-group
311 "Jr" gnus-agent-remove-group
312 "Jo" gnus-agent-toggle-group-plugged)
314 (defun gnus-agent-group-make-menu-bar ()
315 (unless (boundp 'gnus-agent-group-menu)
317 gnus-agent-group-menu gnus-agent-group-mode-map ""
319 ["Toggle plugged" gnus-agent-toggle-plugged t]
320 ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
321 ["List categories" gnus-enter-category-buffer t]
322 ["Send queue" gnus-group-send-queue gnus-plugged]
324 ["All" gnus-agent-fetch-session gnus-plugged]
325 ["Group" gnus-agent-fetch-group gnus-plugged])))))
327 (defvar gnus-agent-summary-mode-map (make-sparse-keymap))
328 (gnus-define-keys gnus-agent-summary-mode-map
329 "Jj" gnus-agent-toggle-plugged
330 "Ju" gnus-agent-summary-fetch-group
331 "J#" gnus-agent-mark-article
332 "J\M-#" gnus-agent-unmark-article
333 "@" gnus-agent-toggle-mark
334 "Jc" gnus-agent-catchup)
336 (defun gnus-agent-summary-make-menu-bar ()
337 (unless (boundp 'gnus-agent-summary-menu)
339 gnus-agent-summary-menu gnus-agent-summary-mode-map ""
341 ["Toggle plugged" gnus-agent-toggle-plugged t]
342 ["Mark as downloadable" gnus-agent-mark-article t]
343 ["Unmark as downloadable" gnus-agent-unmark-article t]
344 ["Toggle mark" gnus-agent-toggle-mark t]
345 ["Fetch downloadable" gnus-agent-summary-fetch-group t]
346 ["Catchup undownloaded" gnus-agent-catchup t]))))
348 (defvar gnus-agent-server-mode-map (make-sparse-keymap))
349 (gnus-define-keys gnus-agent-server-mode-map
350 "Jj" gnus-agent-toggle-plugged
351 "Ja" gnus-agent-add-server
352 "Jr" gnus-agent-remove-server)
354 (defun gnus-agent-server-make-menu-bar ()
355 (unless (boundp 'gnus-agent-server-menu)
357 gnus-agent-server-menu gnus-agent-server-mode-map ""
359 ["Toggle plugged" gnus-agent-toggle-plugged t]
360 ["Add" gnus-agent-add-server t]
361 ["Remove" gnus-agent-remove-server t]))))
363 (defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
364 (if (and (fboundp 'propertize)
365 (fboundp 'make-mode-line-mouse-map))
366 (propertize string 'local-map
367 (make-mode-line-mouse-map mouse-button mouse-func))
370 (defun gnus-agent-toggle-plugged (plugged)
371 "Toggle whether Gnus is unplugged or not."
372 (interactive (list (not gnus-plugged)))
375 (setq gnus-plugged plugged)
376 (gnus-run-hooks 'gnus-agent-plugged-hook)
377 (setcar (cdr gnus-agent-mode-status)
378 (gnus-agent-make-mode-line-string " Plugged"
380 'gnus-agent-toggle-plugged))
381 (gnus-agent-go-online gnus-agent-go-online)
382 (gnus-agent-possibly-synchronize-flags))
383 (gnus-agent-close-connections)
384 (setq gnus-plugged plugged)
385 (gnus-run-hooks 'gnus-agent-unplugged-hook)
386 (setcar (cdr gnus-agent-mode-status)
387 (gnus-agent-make-mode-line-string " Unplugged"
389 'gnus-agent-toggle-plugged)))
390 (force-mode-line-update))
392 (defun gnus-agent-close-connections ()
393 "Close all methods covered by the Gnus agent."
394 (let ((methods gnus-agent-covered-methods))
396 (gnus-close-server (pop methods)))))
399 (defun gnus-unplugged ()
400 "Start Gnus unplugged."
402 (setq gnus-plugged nil)
406 (defun gnus-plugged ()
407 "Start Gnus plugged."
409 (setq gnus-plugged t)
413 (defun gnus-slave-unplugged (&optional arg)
414 "Read news as a slave unplugged."
416 (setq gnus-plugged nil)
417 (gnus arg nil 'slave))
420 (defun gnus-agentize ()
421 "Allow Gnus to be an offline newsreader.
422 The normal usage of this command is to put the following as the
423 last form in your `.gnus.el' file:
427 This will modify the `gnus-setup-news-hook', and
428 `message-send-mail-real-function' variables, and install the Gnus agent
429 minor mode in all Gnus buffers."
432 (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
433 (unless gnus-agent-send-mail-function
434 (setq gnus-agent-send-mail-function (or
435 message-send-mail-real-function
436 message-send-mail-function)
437 message-send-mail-real-function 'gnus-agent-send-mail))
438 (unless gnus-agent-covered-methods
441 (if (memq (car (gnus-server-to-method server))
442 gnus-agent-auto-agentize-methods)
443 (setq gnus-agent-covered-methods
444 (cons (gnus-server-to-method server)
445 gnus-agent-covered-methods ))))
446 (append (list gnus-select-method) gnus-secondary-select-methods))))
448 (defun gnus-agent-queue-setup ()
449 "Make sure the queue group exists."
450 (unless (gnus-gethash "nndraft:queue" gnus-newsrc-hashtb)
451 (gnus-request-create-group "queue" '(nndraft ""))
452 (let ((gnus-level-default-subscribed 1))
453 (gnus-subscribe-group "nndraft:queue" nil '(nndraft "")))
454 (gnus-group-set-parameter
455 "nndraft:queue" 'gnus-dummy '((gnus-draft-mode)))))
457 (defun gnus-agent-send-mail ()
459 (funcall gnus-agent-send-mail-function)
460 (goto-char (point-min))
462 (concat "^" (regexp-quote mail-header-separator) "\n"))
464 (gnus-agent-insert-meta-information 'mail)
465 (gnus-request-accept-article "nndraft:queue" nil t t)))
467 (defun gnus-agent-insert-meta-information (type &optional method)
468 "Insert meta-information into the message that says how it's to be posted.
469 TYPE can be either `mail' or `news'. If the latter, then METHOD can
472 (message-remove-header gnus-agent-meta-information-header)
473 (goto-char (point-min))
474 (insert gnus-agent-meta-information-header ": "
475 (symbol-name type) " " (format "%S" method)
478 (while (search-backward "\n" nil t)
479 (replace-match "\\n" t t))))
481 (defun gnus-agent-restore-gcc ()
482 "Restore GCC field from saved header."
484 (goto-char (point-min))
485 (while (re-search-forward (concat gnus-agent-gcc-header ":") nil t)
486 (replace-match "Gcc:" 'fixedcase))))
488 (defun gnus-agent-any-covered-gcc ()
490 (message-narrow-to-headers)
491 (let* ((gcc (mail-fetch-field "gcc" nil t))
493 (mapcar 'gnus-inews-group-method
494 (message-unquote-tokens
495 (message-tokenize-header
498 (while (and (not covered) methods)
499 (setq covered (gnus-agent-method-p (car methods))
500 methods (cdr methods)))
504 (defun gnus-agent-possibly-save-gcc ()
505 "Save GCC if Gnus is unplugged."
506 (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
508 (goto-char (point-min))
509 (let ((case-fold-search t))
510 (while (re-search-forward "^gcc:" nil t)
511 (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))
513 (defun gnus-agent-possibly-do-gcc ()
514 "Do GCC if Gnus is plugged."
515 (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
516 (gnus-inews-do-gcc)))
519 ;;; Group mode commands
522 (defun gnus-agent-fetch-groups (n)
523 "Put all new articles in the current groups into the Agent."
526 (error "Groups can't be fetched when Gnus is unplugged"))
527 (gnus-group-iterate n 'gnus-agent-fetch-group))
529 (defun gnus-agent-fetch-group (group)
530 "Put all new articles in GROUP into the Agent."
531 (interactive (list (gnus-group-group-name)))
532 (let ((state gnus-plugged))
536 (error "No group on the current line"))
538 (gnus-agent-toggle-plugged gnus-plugged))
539 (let ((gnus-command-method (gnus-find-method-for-group group)))
540 (gnus-agent-with-fetch
541 (gnus-agent-fetch-group-1 group gnus-command-method)
542 (gnus-message 5 "Fetching %s...done" group))))
543 (when (and (not state)
545 (gnus-agent-toggle-plugged gnus-plugged)))))
547 (defun gnus-agent-add-group (category arg)
548 "Add the current group to an agent category."
554 (mapcar (lambda (cat) (list (symbol-name (car cat))))
558 (let ((cat (assq category gnus-category-alist))
560 (gnus-group-iterate arg
562 (when (cadddr (setq c (gnus-group-category group)))
563 (setf (cadddr c) (delete group (cadddr c))))
564 (push group groups)))
565 (setf (cadddr cat) (nconc (cadddr cat) groups))
566 (gnus-category-write)))
568 (defun gnus-agent-remove-group (arg)
569 "Remove the current group from its agent category, if any."
572 (gnus-group-iterate arg
574 (when (cadddr (setq c (gnus-group-category group)))
575 (setf (cadddr c) (delete group (cadddr c))))))
576 (gnus-category-write)))
578 (defun gnus-agent-synchronize-flags ()
579 "Synchronize unplugged flags with servers."
582 (dolist (gnus-command-method gnus-agent-covered-methods)
583 (when (file-exists-p (gnus-agent-lib-file "flags"))
584 (gnus-agent-synchronize-flags-server gnus-command-method)))))
586 (defun gnus-agent-possibly-synchronize-flags ()
587 "Synchronize flags according to `gnus-agent-synchronize-flags'."
590 (dolist (gnus-command-method gnus-agent-covered-methods)
591 (when (file-exists-p (gnus-agent-lib-file "flags"))
592 (gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))
594 (defun gnus-agent-synchronize-flags-server (method)
595 "Synchronize flags set when unplugged for server."
596 (let ((gnus-command-method method))
597 (when (file-exists-p (gnus-agent-lib-file "flags"))
598 (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
600 (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
601 (if (null (gnus-check-server gnus-command-method))
602 (message "Couldn't open server %s" (nth 1 gnus-command-method))
604 (if (null (eval (read (current-buffer))))
605 (progn (forward-line)
607 (write-file (gnus-agent-lib-file "flags"))
608 (error "Couldn't set flags from file %s"
609 (gnus-agent-lib-file "flags"))))
610 (delete-file (gnus-agent-lib-file "flags")))
613 (defun gnus-agent-possibly-synchronize-flags-server (method)
614 "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
615 (when (or (and gnus-agent-synchronize-flags
616 (not (eq gnus-agent-synchronize-flags 'ask)))
617 (and (eq gnus-agent-synchronize-flags 'ask)
618 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
620 (gnus-agent-synchronize-flags-server method)))
623 ;;; Server mode commands
626 (defun gnus-agent-add-server (server)
627 "Enroll SERVER in the agent program."
628 (interactive (list (gnus-server-server-name)))
630 (error "No server on the current line"))
631 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
632 (when (gnus-agent-method-p method)
633 (error "Server already in the agent program"))
634 (push method gnus-agent-covered-methods)
635 (gnus-server-update-server server)
636 (gnus-agent-write-servers)
637 (message "Entered %s into the Agent" server)))
639 (defun gnus-agent-remove-server (server)
640 "Remove SERVER from the agent program."
641 (interactive (list (gnus-server-server-name)))
643 (error "No server on the current line"))
644 (let ((method (gnus-server-get-method nil (gnus-server-server-name))))
645 (unless (gnus-agent-method-p method)
646 (error "Server not in the agent program"))
647 (setq gnus-agent-covered-methods
648 (delete method gnus-agent-covered-methods))
649 (gnus-server-update-server server)
650 (gnus-agent-write-servers)
651 (message "Removed %s from the agent" server)))
653 (defun gnus-agent-read-servers ()
654 "Read the alist of covered servers."
656 (let ((server (gnus-server-get-method
660 (push server gnus-agent-covered-methods)
661 (message "Ignoring disappeared server `%s'" m)
663 (gnus-agent-read-file
664 (nnheader-concat gnus-agent-directory "lib/servers"))))
666 (defun gnus-agent-write-servers ()
667 "Write the alist of covered servers."
668 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
669 (let ((coding-system-for-write nnheader-file-coding-system)
670 (output-coding-system nnheader-file-coding-system)
671 (file-name-coding-system nnmail-pathname-coding-system)
672 (pathname-coding-system nnmail-pathname-coding-system))
673 (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
674 (prin1 (mapcar 'gnus-method-simplify gnus-agent-covered-methods)
681 (defun gnus-agent-mark-article (n &optional unmark)
682 "Mark the next N articles as downloadable.
683 If N is negative, mark backward instead. If UNMARK is non-nil, remove
684 the mark instead. The difference between N and the actual number of
685 articles marked is returned."
687 (let ((backward (< n 0))
692 (gnus-summary-set-agent-mark
693 (gnus-summary-article-number) unmark)
694 (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
697 (gnus-message 7 "No more articles"))
698 (gnus-summary-recenter)
699 (gnus-summary-position-point)
702 (defun gnus-agent-unmark-article (n)
703 "Remove the downloadable mark from the next N articles.
704 If N is negative, unmark backward instead. The difference between N and
705 the actual number of articles unmarked is returned."
707 (gnus-agent-mark-article n t))
709 (defun gnus-agent-toggle-mark (n)
710 "Toggle the downloadable mark from the next N articles.
711 If N is negative, toggle backward instead. The difference between N and
712 the actual number of articles toggled is returned."
714 (gnus-agent-mark-article n 'toggle))
716 (defun gnus-summary-set-agent-mark (article &optional unmark)
717 "Mark ARTICLE as downloadable."
718 (let ((unmark (if (and (not (null unmark)) (not (eq t unmark)))
719 (memq article gnus-newsgroup-downloadable)
723 (setq gnus-newsgroup-downloadable
724 (delq article gnus-newsgroup-downloadable))
725 (push article gnus-newsgroup-undownloaded))
726 (setq gnus-newsgroup-undownloaded
727 (delq article gnus-newsgroup-undownloaded))
728 (setq gnus-newsgroup-downloadable
729 (gnus-add-to-sorted-list gnus-newsgroup-downloadable article)))
730 (gnus-summary-update-mark
731 (if unmark gnus-undownloaded-mark gnus-downloadable-mark)
734 (defun gnus-agent-get-undownloaded-list ()
735 "Mark all unfetched articles as read."
736 (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
737 (when (and (not (gnus-online gnus-command-method))
738 (gnus-agent-method-p gnus-command-method))
739 (gnus-agent-load-alist gnus-newsgroup-name)
740 ;; First mark all undownloaded articles as undownloaded.
741 (let ((articles (mapcar (lambda (header) (mail-header-number header))
742 gnus-newsgroup-headers))
743 (agent-articles gnus-agent-article-alist)
745 (while (setq article (pop articles))
746 (while (and agent-articles
747 (< (caar agent-articles) article))
748 (setq agent-articles (cdr agent-articles)))
749 (when (or (not (cdar agent-articles))
750 (not (= (caar agent-articles) article)))
751 (push article candidates)))
752 (dolist (article candidates)
753 (unless (or (memq article gnus-newsgroup-downloadable)
754 (memq article gnus-newsgroup-cached))
755 (push article gnus-newsgroup-undownloaded))))
756 ;; Then mark downloaded downloadable as not-downloadable,
757 ;; if you get my drift.
758 (dolist (article gnus-newsgroup-downloadable)
759 (when (cdr (assq article gnus-agent-article-alist))
760 (setq gnus-newsgroup-downloadable
761 (delq article gnus-newsgroup-downloadable)))))))
763 (defun gnus-agent-catchup ()
764 "Mark all undownloaded articles as read."
767 (while gnus-newsgroup-undownloaded
768 (gnus-summary-mark-article
769 (pop gnus-newsgroup-undownloaded) gnus-catchup-mark)))
770 (gnus-summary-position-point))
772 (defun gnus-agent-summary-fetch-group (&optional all)
773 "Fetch the downloadable articles in the group.
774 Optional arg ALL, if non-nil, means to fetch all articles."
777 (if all gnus-newsgroup-articles
778 gnus-newsgroup-downloadable))
779 (gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
780 (state gnus-plugged))
784 (gnus-agent-toggle-plugged t))
786 (error "No articles to download"))
787 (gnus-agent-with-fetch
788 (gnus-agent-fetch-articles gnus-newsgroup-name articles))
790 (dolist (article articles)
791 (setq gnus-newsgroup-downloadable
792 (delq article gnus-newsgroup-downloadable))
793 (if gnus-agent-mark-unread-after-downloaded
794 (gnus-summary-mark-article article gnus-unread-mark)))))
795 (when (and (not state)
797 (gnus-agent-toggle-plugged nil)))))
799 (defun gnus-agent-fetch-selected-article ()
800 "Fetch the current article as it is selected.
801 This can be added to `gnus-select-article-hook' or
802 `gnus-mark-article-hook'."
803 (let ((gnus-command-method gnus-current-select-method))
804 (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
805 (gnus-agent-fetch-articles
807 (list gnus-current-article)))))
810 ;;; Internal functions
813 (defun gnus-agent-save-active (method)
814 (gnus-agent-save-active-1 method 'gnus-active-to-gnus-format))
816 (defun gnus-agent-save-active-1 (method function)
817 (when (gnus-agent-method-p method)
818 (let* ((gnus-command-method method)
819 (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
820 (file (gnus-agent-lib-file "active")))
821 (funcall function nil new)
822 (gnus-agent-write-active file new)
824 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
827 (defun gnus-agent-write-active (file new)
828 (let ((orig (gnus-make-hashtable (count-lines (point-min) (point-max))))
829 (file (gnus-agent-lib-file "active"))
831 (when (file-exists-p file)
833 (insert-file-contents-as-coding-system gnus-agent-file-coding-system
835 (gnus-active-to-gnus-format nil orig))
838 (when (and sym (boundp sym))
839 (if (and (boundp (setq osym (intern (symbol-name sym) orig)))
840 (setq elem (symbol-value osym)))
842 (if (and (integerp (car (symbol-value sym)))
843 (> (car elem) (car (symbol-value sym))))
844 (setcar elem (car (symbol-value sym))))
845 (if (integerp (cdr (symbol-value sym)))
846 (setcdr elem (cdr (symbol-value sym)))))
847 (set (intern (symbol-name sym) orig) (symbol-value sym)))))
849 (gnus-make-directory (file-name-directory file))
850 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
851 ;; The hashtable contains real names of groups, no more prefix
852 ;; removing, so set `full' to `t'.
853 (gnus-write-active-file file orig t))))
855 (defun gnus-agent-save-groups (method)
856 (gnus-agent-save-active-1 method 'gnus-groups-to-gnus-format))
858 (defun gnus-agent-save-group-info (method group active)
859 (when (gnus-agent-method-p method)
860 (let* ((gnus-command-method method)
861 (coding-system-for-write nnheader-file-coding-system)
862 (output-coding-system nnheader-file-coding-system)
863 (file-name-coding-system nnmail-pathname-coding-system)
864 (pathname-coding-system nnmail-pathname-coding-system)
865 (file (gnus-agent-lib-file "active"))
867 (gnus-make-directory (file-name-directory file))
869 (when (file-exists-p file)
870 (nnheader-insert-file-contents file))
871 (goto-char (point-min))
872 (when (re-search-forward
873 (concat "^" (regexp-quote group) " ") nil t)
875 (read (current-buffer)) ;; max
876 (setq oactive-min (read (current-buffer)))) ;; min
878 (insert (format "%S %d %d y\n" (intern group)
880 (or oactive-min (car active))))
881 (goto-char (point-max))
882 (while (search-backward "\\." nil t)
885 (defun gnus-agent-group-path (group)
886 "Translate GROUP into a path."
887 (if nnmail-use-long-file-names
888 (gnus-group-real-name group)
889 (nnheader-translate-file-chars
890 (nnheader-replace-chars-in-string
891 (nnheader-replace-duplicate-chars-in-string
892 (nnheader-replace-chars-in-string
893 (gnus-group-real-name group)
900 (defun gnus-agent-get-function (method)
901 (if (gnus-online method)
906 ;;; History functions
908 (defun gnus-agent-history-buffer ()
909 (cdr (assoc (gnus-agent-method) gnus-agent-history-buffers)))
911 (defun gnus-agent-open-history ()
913 (push (cons (gnus-agent-method)
914 (set-buffer (gnus-get-buffer-create
915 (format " *Gnus agent %s history*"
916 (gnus-agent-method)))))
917 gnus-agent-history-buffers)
920 (let ((file (gnus-agent-lib-file "history")))
921 (when (file-exists-p file)
922 (nnheader-insert-file-contents file))
923 (set (make-local-variable 'gnus-agent-file-name) file))))
925 (defun gnus-agent-save-history ()
927 (set-buffer gnus-agent-current-history)
928 (gnus-make-directory (file-name-directory gnus-agent-file-name))
929 (write-region-as-coding-system
930 gnus-agent-file-coding-system
931 (1+ (point-min)) (point-max) gnus-agent-file-name nil 'silent)))
933 (defun gnus-agent-close-history ()
934 (when (gnus-buffer-live-p gnus-agent-current-history)
935 (kill-buffer gnus-agent-current-history)
936 (setq gnus-agent-history-buffers
937 (delq (assoc (gnus-agent-method) gnus-agent-history-buffers)
938 gnus-agent-history-buffers))))
940 (defun gnus-agent-enter-history (id group-arts date)
942 (set-buffer gnus-agent-current-history)
943 (goto-char (point-max))
945 (insert id "\t" (number-to-string date) "\t")
947 (insert (format "%S" (intern (caar group-arts)))
948 " " (number-to-string (cdr (pop group-arts)))
951 (while (search-backward "\\." p t)
954 (defun gnus-agent-article-in-history-p (id)
956 (set-buffer (gnus-agent-history-buffer))
957 (goto-char (point-min))
958 (search-forward (concat "\n" id "\t") nil t)))
960 (defun gnus-agent-history-path (id)
962 (set-buffer (gnus-agent-history-buffer))
963 (goto-char (point-min))
964 (when (search-forward (concat "\n" id "\t") nil t)
965 (let ((method (gnus-agent-method)))
967 (while (not (numberp (setq group (read (current-buffer)))))
968 (push (concat method "/" group) paths))
969 (nreverse paths))))))
975 (defun gnus-agent-fetch-articles (group articles)
976 "Fetch ARTICLES from GROUP and put them into the Agent."
978 ;; Prune off articles that we have already fetched.
980 (cdr (assq (car articles) gnus-agent-article-alist)))
982 (let ((arts articles))
984 (if (cdr (assq (cadr arts) gnus-agent-article-alist))
985 (setcdr arts (cddr arts))
986 (setq arts (cdr arts)))))
989 (gnus-agent-directory)
990 (gnus-agent-group-path group) "/"))
991 (date (time-to-days (current-time)))
994 (gnus-make-directory dir)
995 (gnus-message 7 "Fetching articles for %s..." group)
996 ;; Fetch the articles from the backend.
997 (if (gnus-check-backend-function 'retrieve-articles group)
998 (setq pos (gnus-retrieve-articles articles group))
1001 (while (setq article (pop articles))
1003 (gnus-backlog-request-article group article
1005 (gnus-request-article article group))
1006 (goto-char (point-max))
1007 (push (cons article (point)) pos)
1008 (insert-buffer-substring nntp-server-buffer)))
1009 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
1010 (setq pos (nreverse pos)))))
1011 ;; Then save these articles into the Agent.
1013 (set-buffer nntp-server-buffer)
1015 (narrow-to-region (cdar pos) (or (cdadr pos) (point-max)))
1016 (goto-char (point-min))
1017 (unless (eobp) ;; Don't save empty articles.
1018 (when (search-forward "\n\n" nil t)
1019 (when (search-backward "\nXrefs: " nil t)
1020 ;; Handle cross posting.
1021 (skip-chars-forward "^ ")
1022 (skip-chars-forward " ")
1024 (while (looking-at "\\([^: \n]+\\):\\([0-9]+\\) +")
1025 (push (cons (buffer-substring (match-beginning 1)
1027 (buffer-substring (match-beginning 2)
1030 (goto-char (match-end 0)))
1031 (gnus-agent-crosspost crosses (caar pos))))
1032 (goto-char (point-min))
1033 (if (not (re-search-forward
1034 "^Message-ID: *<\\([^>\n]+\\)>" nil t))
1035 (setq id "No-Message-ID-in-article")
1036 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
1037 (write-region-as-coding-system
1038 gnus-agent-file-coding-system (point-min) (point-max)
1039 (concat dir (number-to-string (caar pos))) nil 'silent)
1040 (when (setq elem (assq (caar pos) gnus-agent-article-alist))
1042 (gnus-agent-enter-history
1043 id (or crosses (list (cons group (caar pos)))) date))
1046 (gnus-agent-save-alist group)))))
1048 (defun gnus-agent-crosspost (crosses article)
1049 (let (gnus-agent-article-alist group alist beg end)
1051 (set-buffer gnus-agent-overview-buffer)
1052 (when (nnheader-find-nov-line article)
1055 (setq end (progn (forward-line 1) (point)))))
1057 (setq group (caar crosses))
1058 (unless (setq alist (assoc group gnus-agent-group-alist))
1059 (push (setq alist (list group (gnus-agent-load-alist (caar crosses))))
1060 gnus-agent-group-alist))
1061 (setcdr alist (cons (cons (cdar crosses) t) (cdr alist)))
1063 (set-buffer (gnus-get-buffer-create (format " *Gnus agent overview %s*"
1065 (when (= (point-max) (point-min))
1066 (push (cons group (current-buffer)) gnus-agent-buffer-alist)
1068 (nnheader-insert-file-contents
1069 (gnus-agent-article-name ".overview" group))))
1070 (nnheader-find-nov-line (string-to-number (cdar crosses)))
1071 (insert (string-to-number (cdar crosses)))
1072 (insert-buffer-substring gnus-agent-overview-buffer beg end))
1075 (defun gnus-agent-flush-cache ()
1077 (while gnus-agent-buffer-alist
1078 (set-buffer (cdar gnus-agent-buffer-alist))
1079 (write-region-as-coding-system
1080 gnus-agent-file-coding-system
1081 (point-min) (point-max)
1082 (gnus-agent-article-name ".overview"
1083 (caar gnus-agent-buffer-alist))
1085 (pop gnus-agent-buffer-alist))
1086 (while gnus-agent-group-alist
1087 (with-temp-file (caar gnus-agent-group-alist)
1088 (princ (cdar gnus-agent-group-alist))
1090 (pop gnus-agent-group-alist))))
1092 (defun gnus-agent-fetch-headers (group &optional force)
1093 (let* ((articles (gnus-list-of-unread-articles group))
1094 (len (length articles))
1095 (gnus-decode-encoded-word-function 'identity)
1096 (file (gnus-agent-article-name ".overview" group))
1098 ;; Check the number of articles is not too large.
1099 (when (and (integerp gnus-agent-large-newsgroup)
1100 (< 0 gnus-agent-large-newsgroup))
1101 (and (< 0 (setq i (- len gnus-agent-large-newsgroup)))
1102 (setq articles (nthcdr i articles))))
1103 ;; add article with marks to list of article headers we want to fetch.
1104 (dolist (arts (gnus-info-marks (gnus-get-info group)))
1105 (unless (memq (car arts) '(seen recent))
1106 (setq articles (gnus-range-add articles (cdr arts)))))
1107 (setq articles (sort (gnus-uncompress-sequence articles) '<))
1108 ;; Remove known articles.
1109 (when (gnus-agent-load-alist group)
1110 (setq articles (gnus-list-range-intersection
1113 (cons (1+ (caar (last gnus-agent-article-alist)))
1114 (cdr (gnus-active group)))))))
1116 (gnus-make-directory (nnheader-translate-file-chars
1117 (file-name-directory file) t))
1119 (gnus-message 7 "Fetching headers for %s..." group)
1121 (set-buffer nntp-server-buffer)
1122 (unless (eq 'nov (gnus-retrieve-headers articles group))
1123 (nnvirtual-convert-headers))
1124 ;; Save these headers for later processing.
1125 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
1126 (when (file-exists-p file)
1127 (gnus-agent-braid-nov group articles file))
1128 (write-region-as-coding-system
1129 gnus-agent-file-coding-system
1130 (point-min) (point-max) file nil 'silent)
1131 (gnus-agent-save-alist group articles nil)
1132 (gnus-agent-enter-history
1133 "last-header-fetched-for-session"
1134 (list (cons group (nth (- (length articles) 1) articles)))
1135 (time-to-days (current-time)))
1138 (defsubst gnus-agent-copy-nov-line (article)
1140 (set-buffer gnus-agent-overview-buffer)
1141 (while (and (not (eobp))
1142 (< (setq art (read (current-buffer))) article))
1146 (not (eq article art)))
1147 (set-buffer nntp-server-buffer)
1149 (setq e (progn (forward-line 1) (point)))
1150 (set-buffer nntp-server-buffer)
1151 (insert-buffer-substring gnus-agent-overview-buffer b e))))
1153 (defun gnus-agent-braid-nov (group articles file)
1155 (set-buffer gnus-agent-overview-buffer)
1156 (goto-char (point-min))
1157 (set-buffer nntp-server-buffer)
1159 (nnheader-insert-file-contents file)
1160 (goto-char (point-max))
1161 (unless (or (= (point-min) (point-max))
1164 (< (setq last (read (current-buffer))) (car articles))))
1165 ;; We do it the hard way.
1166 (nnheader-find-nov-line (car articles))
1167 (gnus-agent-copy-nov-line (pop articles))
1168 (while (and articles
1170 (while (and (not (eobp))
1171 (< (read (current-buffer)) (car articles)))
1175 (gnus-agent-copy-nov-line (pop articles)))))
1176 ;; Copy the rest lines
1177 (set-buffer nntp-server-buffer)
1178 (goto-char (point-max))
1181 (set-buffer gnus-agent-overview-buffer)
1182 (while (and (not (eobp))
1183 (<= (read (current-buffer)) last))
1186 (setq start (point))
1187 (set-buffer nntp-server-buffer))
1188 (insert-buffer-substring gnus-agent-overview-buffer start))))
1190 (defun gnus-agent-load-alist (group &optional dir)
1191 "Load the article-state alist for GROUP."
1193 (setq gnus-agent-article-alist
1194 (gnus-cache-file-contents
1196 (expand-file-name ".agentview" dir)
1197 (gnus-agent-article-name ".agentview" group))
1198 'gnus-agent-file-loading-cache
1199 'gnus-agent-read-file))))
1201 (defun gnus-agent-save-alist (group &optional articles state dir)
1202 "Save the article-state alist for GROUP."
1203 (let* ((file-name-coding-system nnmail-pathname-coding-system)
1204 (pathname-coding-system nnmail-pathname-coding-system)
1205 (prev (cons nil gnus-agent-article-alist))
1207 print-level print-length item article)
1208 (while (setq article (pop articles))
1209 (while (and (cdr prev)
1210 (< (caadr prev) article))
1211 (setq prev (cdr prev)))
1214 (setcdr prev (list (cons article state))))
1215 ((> (caadr prev) article)
1216 (setcdr prev (cons (cons article state) (cdr prev))))
1217 ((= (caadr prev) article)
1218 (setcdr (cadr prev) state)))
1219 (setq prev (cdr prev)))
1220 (setq gnus-agent-article-alist (cdr all))
1221 (with-temp-file (if dir
1222 (expand-file-name ".agentview" dir)
1223 (gnus-agent-article-name ".agentview" group))
1224 (princ gnus-agent-article-alist (current-buffer))
1227 (defun gnus-agent-article-name (article group)
1228 (expand-file-name (if (stringp article) article (string-to-number article))
1229 (file-name-as-directory
1230 (expand-file-name (gnus-agent-group-path group)
1231 (gnus-agent-directory)))))
1234 (defun gnus-agent-batch-fetch ()
1235 "Start Gnus and fetch session."
1238 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1239 (gnus-agent-fetch-session))
1242 (defun gnus-agent-fetch-session ()
1243 "Fetch all articles and headers that are eligible for fetching."
1245 (unless gnus-agent-covered-methods
1246 (error "No servers are covered by the Gnus agent"))
1247 (unless gnus-plugged
1248 (error "Can't fetch articles while Gnus is unplugged"))
1249 (let ((methods gnus-agent-covered-methods)
1250 groups group gnus-command-method)
1255 (setq gnus-command-method (car methods))
1256 (when (and (or (gnus-server-opened gnus-command-method)
1257 (gnus-open-server gnus-command-method))
1258 (gnus-online gnus-command-method))
1259 (setq groups (gnus-groups-from-server (car methods)))
1260 (gnus-agent-with-fetch
1261 (while (setq group (pop groups))
1262 (when (<= (gnus-group-level group) gnus-agent-handle-level)
1263 (gnus-agent-fetch-group-1 group gnus-command-method))))))
1265 (unless (funcall gnus-agent-confirmation-function
1266 (format "Error (%s). Continue? " (cadr err)))
1267 (error "Cannot fetch articles into the Gnus agent")))
1269 (unless (funcall gnus-agent-confirmation-function
1270 (format "Quit fetching session (%s). Continue? "
1272 (signal 'quit "Cannot fetch articles into the Gnus agent"))))
1274 (run-hooks 'gnus-agent-fetch-hook)
1275 (gnus-message 6 "Finished fetching articles into the Gnus agent"))))
1277 (defun gnus-agent-fetch-group-1 (group method)
1279 (let ((gnus-command-method method)
1280 (gnus-newsgroup-name group)
1281 gnus-newsgroup-dependencies gnus-newsgroup-headers
1282 gnus-newsgroup-scored gnus-headers gnus-score
1283 gnus-use-cache articles arts
1284 category predicate info marks score-param
1285 (gnus-summary-expunge-below gnus-summary-expunge-below)
1286 (gnus-summary-mark-below gnus-summary-mark-below)
1287 (gnus-orphan-score gnus-orphan-score)
1288 ;; Maybe some other gnus-summary local variables should also
1291 (unless (gnus-check-group group)
1292 (error "Can't open server for %s" group))
1294 (when (and (or (gnus-active group)
1295 (gnus-activate-group group))
1296 (setq articles (gnus-agent-fetch-headers group))
1297 (let ((nntp-server-buffer gnus-agent-overview-buffer))
1298 ;; Parse them and see which articles we want to fetch.
1299 (setq gnus-newsgroup-dependencies
1300 (make-vector (length articles) 0))
1301 (setq gnus-newsgroup-headers
1302 (gnus-get-newsgroup-headers-xover articles nil nil
1304 ;; `gnus-agent-overview-buffer' may be killed for
1305 ;; timeout reason. If so, recreate it.
1306 (gnus-agent-create-buffer)))
1307 (setq category (gnus-group-category group))
1310 (or (gnus-group-find-parameter group 'agent-predicate t)
1312 (if (memq predicate '(gnus-agent-true gnus-agent-false))
1313 ;; Simple implementation
1314 (setq arts (and (eq predicate 'gnus-agent-true) articles))
1317 (or (gnus-group-get-parameter group 'agent-score t)
1319 ;; Translate score-param into real one
1322 ((eq score-param 'file)
1323 (setq score-param (gnus-all-score-files group)))
1324 ((stringp (car score-param)))
1326 (setq score-param (list (list score-param)))))
1328 (gnus-score-headers score-param))
1329 (while (setq gnus-headers (pop gnus-newsgroup-headers))
1331 (or (cdr (assq (mail-header-number gnus-headers)
1332 gnus-newsgroup-scored))
1333 gnus-summary-default-score))
1334 (when (funcall predicate)
1335 (push (mail-header-number gnus-headers)
1337 ;; Fetch the articles.
1339 (gnus-agent-fetch-articles group arts)))
1340 ;; Perhaps we have some additional articles to fetch.
1341 (dolist (mark gnus-agent-download-marks)
1342 (setq arts (assq mark (gnus-info-marks
1343 (setq info (gnus-get-info group)))))
1345 (gnus-message 8 "Agent is downloading marked articles...")
1346 (gnus-agent-fetch-articles
1347 group (gnus-uncompress-range (cdr arts)))
1348 (when (eq mark 'download)
1349 (setq marks (delq arts (gnus-info-marks info)))
1350 (gnus-info-set-marks info marks)
1352 (concat "(gnus-group-set-info '"
1353 (gnus-prin1-to-string info)
1357 ;;; Agent Category Mode
1360 (defvar gnus-category-mode-hook nil
1361 "Hook run in `gnus-category-mode' buffers.")
1363 (defvar gnus-category-line-format " %(%20c%): %g\n"
1364 "Format of category lines.
1366 Valid specifiers include:
1367 %c Topic name (string)
1368 %g The number of groups in the topic (integer)
1370 General format specifiers can also be used. See Info node
1371 `(gnus)Formatting Variables'.")
1373 (defvar gnus-category-mode-line-format "Gnus: %%b"
1374 "The format specification for the category mode line.")
1376 (defvar gnus-agent-short-article 100
1377 "Articles that have fewer lines than this are short.")
1379 (defvar gnus-agent-long-article 200
1380 "Articles that have more lines than this are long.")
1382 (defvar gnus-agent-low-score 0
1383 "Articles that have a score lower than this have a low score.")
1385 (defvar gnus-agent-high-score 0
1386 "Articles that have a score higher than this have a high score.")
1389 ;;; Internal variables.
1391 (defvar gnus-category-buffer "*Agent Category*")
1393 (defvar gnus-category-line-format-alist
1394 `((?c gnus-tmp-name ?s)
1395 (?g gnus-tmp-groups ?d)))
1397 (defvar gnus-category-mode-line-format-alist
1398 `((?u user-defined ?s)))
1400 (defvar gnus-category-line-format-spec nil)
1401 (defvar gnus-category-mode-line-format-spec nil)
1403 (defvar gnus-category-mode-map nil)
1404 (put 'gnus-category-mode 'mode-class 'special)
1406 (unless gnus-category-mode-map
1407 (setq gnus-category-mode-map (make-sparse-keymap))
1408 (suppress-keymap gnus-category-mode-map)
1410 (gnus-define-keys gnus-category-mode-map
1411 "q" gnus-category-exit
1412 "k" gnus-category-kill
1413 "c" gnus-category-copy
1414 "a" gnus-category-add
1415 "p" gnus-category-edit-predicate
1416 "g" gnus-category-edit-groups
1417 "s" gnus-category-edit-score
1418 "l" gnus-category-list
1420 "\C-c\C-i" gnus-info-find-node
1421 "\C-c\C-b" gnus-bug))
1423 (defvar gnus-category-menu-hook nil
1424 "*Hook run after the creation of the menu.")
1426 (defun gnus-category-make-menu-bar ()
1427 (gnus-turn-off-edit-menu 'category)
1428 (unless (boundp 'gnus-category-menu)
1430 gnus-category-menu gnus-category-mode-map ""
1432 ["Add" gnus-category-add t]
1433 ["Kill" gnus-category-kill t]
1434 ["Copy" gnus-category-copy t]
1435 ["Edit predicate" gnus-category-edit-predicate t]
1436 ["Edit score" gnus-category-edit-score t]
1437 ["Edit groups" gnus-category-edit-groups t]
1438 ["Exit" gnus-category-exit t]))
1440 (gnus-run-hooks 'gnus-category-menu-hook)))
1442 (defun gnus-category-mode ()
1443 "Major mode for listing and editing agent categories.
1445 All normal editing commands are switched off.
1446 \\<gnus-category-mode-map>
1447 For more in-depth information on this mode, read the manual
1448 (`\\[gnus-info-find-node]').
1450 The following commands are available:
1452 \\{gnus-category-mode-map}"
1454 (when (gnus-visual-p 'category-menu 'menu)
1455 (gnus-category-make-menu-bar))
1456 (kill-all-local-variables)
1457 (gnus-simplify-mode-line)
1458 (setq major-mode 'gnus-category-mode)
1459 (setq mode-name "Category")
1460 (gnus-set-default-directory)
1461 (setq mode-line-process nil)
1462 (use-local-map gnus-category-mode-map)
1463 (buffer-disable-undo)
1464 (setq truncate-lines t)
1465 (setq buffer-read-only t)
1466 (gnus-run-hooks 'gnus-category-mode-hook))
1468 (defalias 'gnus-category-position-point 'gnus-goto-colon)
1470 (defun gnus-category-insert-line (category)
1471 (let* ((gnus-tmp-name (format "%s" (car category)))
1472 (gnus-tmp-groups (length (cadddr category))))
1474 (gnus-add-text-properties
1478 (eval gnus-category-line-format-spec))
1479 (list 'gnus-category gnus-tmp-name))))
1481 (defun gnus-enter-category-buffer ()
1482 "Go to the Category buffer."
1484 (gnus-category-setup-buffer)
1485 (gnus-configure-windows 'category)
1486 (gnus-category-prepare))
1488 (defun gnus-category-setup-buffer ()
1489 (unless (get-buffer gnus-category-buffer)
1491 (set-buffer (gnus-get-buffer-create gnus-category-buffer))
1492 (gnus-category-mode))))
1494 (defun gnus-category-prepare ()
1495 (gnus-set-format 'category-mode)
1496 (gnus-set-format 'category t)
1497 (let ((alist gnus-category-alist)
1498 (buffer-read-only nil))
1501 (gnus-category-insert-line (pop alist)))
1502 (goto-char (point-min))
1503 (gnus-category-position-point)))
1505 (defun gnus-category-name ()
1506 (or (intern (get-text-property (gnus-point-at-bol) 'gnus-category))
1507 (error "No category on the current line")))
1509 (defun gnus-category-read ()
1510 "Read the category alist."
1511 (setq gnus-category-alist
1512 (or (gnus-agent-read-file
1513 (nnheader-concat gnus-agent-directory "lib/categories"))
1514 (list (list 'default 'short nil nil)))))
1516 (defun gnus-category-write ()
1517 "Write the category alist."
1518 (setq gnus-category-predicate-cache nil
1519 gnus-category-group-cache nil)
1520 (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
1521 (with-temp-file (nnheader-concat gnus-agent-directory "lib/categories")
1522 (prin1 gnus-category-alist (current-buffer))))
1524 (defun gnus-category-edit-predicate (category)
1525 "Edit the predicate for CATEGORY."
1526 (interactive (list (gnus-category-name)))
1527 (let ((info (assq category gnus-category-alist)))
1529 (cadr info) (format "Editing the predicate for category %s" category)
1530 `(lambda (predicate)
1531 (setcar (cdr (assq ',category gnus-category-alist)) predicate)
1532 (gnus-category-write)
1533 (gnus-category-list)))))
1535 (defun gnus-category-edit-score (category)
1536 "Edit the score expression for CATEGORY."
1537 (interactive (list (gnus-category-name)))
1538 (let ((info (assq category gnus-category-alist)))
1541 (format "Editing the score expression for category %s" category)
1543 (setcar (nthcdr 2 (assq ',category gnus-category-alist)) groups)
1544 (gnus-category-write)
1545 (gnus-category-list)))))
1547 (defun gnus-category-edit-groups (category)
1548 "Edit the group list for CATEGORY."
1549 (interactive (list (gnus-category-name)))
1550 (let ((info (assq category gnus-category-alist)))
1552 (cadddr info) (format "Editing the group list for category %s" category)
1554 (setcar (nthcdr 3 (assq ',category gnus-category-alist)) groups)
1555 (gnus-category-write)
1556 (gnus-category-list)))))
1558 (defun gnus-category-kill (category)
1559 "Kill the current category."
1560 (interactive (list (gnus-category-name)))
1561 (let ((info (assq category gnus-category-alist))
1562 (buffer-read-only nil))
1564 (setq gnus-category-alist (delq info gnus-category-alist))
1565 (gnus-category-write)))
1567 (defun gnus-category-copy (category to)
1568 "Copy the current category."
1569 (interactive (list (gnus-category-name) (intern (read-string "New name: "))))
1570 (let ((info (assq category gnus-category-alist)))
1571 (push (list to (gnus-copy-sequence (cadr info))
1572 (gnus-copy-sequence (caddr info)) nil)
1573 gnus-category-alist)
1574 (gnus-category-write)
1575 (gnus-category-list)))
1577 (defun gnus-category-add (category)
1578 "Create a new category."
1579 (interactive "SCategory name: ")
1580 (when (assq category gnus-category-alist)
1581 (error "Category %s already exists" category))
1582 (push (list category 'false nil nil)
1583 gnus-category-alist)
1584 (gnus-category-write)
1585 (gnus-category-list))
1587 (defun gnus-category-list ()
1588 "List all categories."
1590 (gnus-category-prepare))
1592 (defun gnus-category-exit ()
1593 "Return to the group buffer."
1595 (kill-buffer (current-buffer))
1596 (gnus-configure-windows 'group t))
1598 ;; To avoid having 8-bit characters in the source file.
1599 (defvar gnus-category-not (list '! 'not (intern (format "%c" 172))))
1601 (defvar gnus-category-predicate-alist
1602 '((spam . gnus-agent-spam-p)
1603 (short . gnus-agent-short-p)
1604 (long . gnus-agent-long-p)
1605 (low . gnus-agent-low-scored-p)
1606 (high . gnus-agent-high-scored-p)
1607 (true . gnus-agent-true)
1608 (false . gnus-agent-false))
1609 "Mapping from short score predicate symbols to predicate functions.")
1611 (defun gnus-agent-spam-p ()
1612 "Say whether an article is spam or not."
1613 (unless gnus-agent-spam-hashtb
1614 (setq gnus-agent-spam-hashtb (gnus-make-hashtable 1000)))
1615 (if (not (equal (mail-header-references gnus-headers) ""))
1617 (let ((string (gnus-simplify-subject (mail-header-subject gnus-headers))))
1619 (gnus-gethash string gnus-agent-spam-hashtb)
1620 (gnus-sethash string t gnus-agent-spam-hashtb)))))
1622 (defun gnus-agent-short-p ()
1623 "Say whether an article is short or not."
1624 (< (mail-header-lines gnus-headers) gnus-agent-short-article))
1626 (defun gnus-agent-long-p ()
1627 "Say whether an article is long or not."
1628 (> (mail-header-lines gnus-headers) gnus-agent-long-article))
1630 (defun gnus-agent-low-scored-p ()
1631 "Say whether an article has a low score or not."
1632 (< gnus-score gnus-agent-low-score))
1634 (defun gnus-agent-high-scored-p ()
1635 "Say whether an article has a high score or not."
1636 (> gnus-score gnus-agent-high-score))
1638 (defun gnus-category-make-function (cat)
1639 "Make a function from category CAT."
1640 (let ((func (gnus-category-make-function-1 cat)))
1641 (if (and (= (length func) 1)
1642 (symbolp (car func)))
1644 (gnus-byte-compile `(lambda () ,func)))))
1646 (defun gnus-agent-true ()
1650 (defun gnus-agent-false ()
1654 (defun gnus-category-make-function-1 (cat)
1655 "Make a function from category CAT."
1657 ;; Functions are just returned as is.
1659 (gnus-functionp cat))
1660 `(,(or (cdr (assq cat gnus-category-predicate-alist))
1662 ;; More complex category.
1665 ((memq (car cat) '(& and))
1667 ((memq (car cat) '(| or))
1669 ((memq (car cat) gnus-category-not)
1671 ,@(mapcar 'gnus-category-make-function-1 (cdr cat))))
1673 (error "Unknown category type: %s" cat))))
1675 (defun gnus-get-predicate (predicate)
1676 "Return the predicate for CATEGORY."
1677 (or (cdr (assoc predicate gnus-category-predicate-cache))
1678 (let ((func (gnus-category-make-function predicate)))
1679 (setq gnus-category-predicate-cache
1680 (nconc gnus-category-predicate-cache
1681 (list (cons predicate func))))
1684 (defun gnus-group-category (group)
1685 "Return the category GROUP belongs to."
1686 (unless gnus-category-group-cache
1687 (setq gnus-category-group-cache (gnus-make-hashtable 1000))
1688 (let ((cs gnus-category-alist)
1690 (while (setq cat (pop cs))
1691 (setq groups (cadddr cat))
1693 (gnus-sethash (pop groups) cat gnus-category-group-cache)))))
1694 (or (gnus-gethash group gnus-category-group-cache)
1695 (assq 'default gnus-category-alist)))
1697 (defun gnus-agent-expire (&optional articles group force)
1698 "Expire all old articles.
1699 If you want to force expiring of certain articles, this function can
1700 take ARTICLES, GROUP and FORCE parameters as well. Setting ARTICLES
1701 and GROUP without FORCE is not supported."
1703 (let ((methods (if group
1704 (list (gnus-find-method-for-group group))
1705 gnus-agent-covered-methods))
1706 (day (if (numberp gnus-agent-expire-days)
1707 (- (time-to-days (current-time)) gnus-agent-expire-days)
1709 (current-day (time-to-days (current-time)))
1710 gnus-command-method sym arts pos
1711 history overview file histories elem art nov-file low info
1712 unreads marked article orig lowest highest found days)
1714 (setq overview (gnus-get-buffer-create " *expire overview*"))
1715 (while (setq gnus-command-method (pop methods))
1716 (when (file-exists-p (gnus-agent-lib-file "active"))
1718 (let ((nnheader-file-coding-system gnus-agent-file-coding-system))
1719 (nnheader-insert-file-contents (gnus-agent-lib-file "active")))
1720 (gnus-active-to-gnus-format
1722 (setq orig (gnus-make-hashtable
1723 (count-lines (point-min) (point-max))))))
1724 (let ((expiry-hashtb (gnus-make-hashtable 1023)))
1725 (gnus-agent-open-history)
1727 (setq gnus-agent-current-history
1728 (setq history (gnus-agent-history-buffer))))
1729 (goto-char (point-min))
1730 (if (and articles group force) ;; point usless without art+group
1731 (while (setq article (pop articles))
1732 ;; try to find history entries for articles
1733 (goto-char (point-min))
1734 (if (re-search-forward
1735 (concat "^[^\t]*\t[^\t]*\t\(.* ?\)"
1736 (format "%S" (gnus-group-prefixed-name
1737 group gnus-command-method))
1739 (number-to-string article)
1744 (setq sym (let ((obarray expiry-hashtb) s)
1747 (set sym (cons (cons article pos)
1748 (symbol-value sym)))
1749 (set sym (list (cons article pos)))))
1750 ;; go through history file to find eligble articles
1751 (when (> (buffer-size) 1)
1752 (goto-char (point-min))
1754 (skip-chars-forward "^\t")
1755 (if (let ((fetch-date (read (current-buffer))))
1756 (if (numberp fetch-date)
1757 ;; We now have the arrival day, so we see
1758 ;; whether it's old enough to be expired.
1761 (skip-chars-forward "\t")
1763 days gnus-agent-expire-days)
1764 (while (and (not found)
1766 (when (looking-at (caar days))
1767 (setq found (cadar days)))
1769 (> fetch-date (- current-day found)))
1770 ;; History file is corrupted.
1773 (format "File %s is corrupted!"
1774 (gnus-agent-lib-file "history")))
1778 ;; New article; we don't expire it.
1780 ;; Old article. Schedule it for possible nuking.
1782 (setq sym (let ((obarray expiry-hashtb) s)
1783 (setq s (read (current-buffer)))
1784 (if (stringp s) (intern s) s)))
1786 (set sym (cons (cons (read (current-buffer)) (point))
1787 (symbol-value sym)))
1788 (set sym (list (cons (read (current-buffer))
1790 (skip-chars-forward " "))
1791 (forward-line 1)))))
1792 ;; We now have all articles that can possibly be expired.
1795 (setq group (symbol-name sym)
1796 arts (sort (symbol-value sym) 'car-less-than-car)
1797 low (car (gnus-active group))
1798 info (gnus-get-info group)
1799 unreads (ignore-errors
1800 (gnus-list-of-unread-articles group))
1802 (gnus-uncompress-range
1803 (cdr (assq 'tick (gnus-info-marks info))))
1804 (gnus-uncompress-range
1806 (gnus-info-marks info)))))
1807 nov-file (gnus-agent-article-name ".overview" group)
1810 (gnus-agent-load-alist group)
1811 (gnus-message 5 "Expiring articles in %s" group)
1812 (set-buffer overview)
1814 (when (file-exists-p nov-file)
1815 (nnheader-insert-file-contents nov-file))
1816 (goto-char (point-min))
1818 (while (setq elem (pop arts))
1819 (setq article (car elem))
1820 (when (or (null low)
1822 gnus-agent-expire-all
1823 (and (not (memq article unreads))
1824 (not (memq article marked)))
1826 ;; Find and nuke the NOV line.
1827 (while (and (not (eobp))
1829 (setq art (read (current-buffer)))))
1831 (if (and (numberp art)
1833 (gnus-agent-article-name
1834 (number-to-string art) group)))
1840 ;; Remove old NOV lines that have no articles.
1841 (gnus-delete-line)))
1846 ;; Nuke the article.
1847 (when (file-exists-p
1848 (setq file (gnus-agent-article-name
1849 (number-to-string article)
1852 ;; Schedule the history line for nuking.
1854 (push (cdr elem) histories))))
1855 (gnus-make-directory (file-name-directory nov-file))
1856 (write-region-as-coding-system
1857 gnus-agent-file-coding-system
1858 (point-min) (point-max) nov-file nil 'silent)
1859 ;; Delete the unwanted entries in the alist.
1860 (setq gnus-agent-article-alist
1861 (sort gnus-agent-article-alist 'car-less-than-car))
1862 (let* ((alist gnus-agent-article-alist)
1863 (prev (cons nil alist))
1867 (<= (caar alist) article))
1868 (if (or (not (cdar alist))
1870 (gnus-agent-article-name
1875 (push (caar alist) expired)
1876 (setcdr prev (setq alist (cdr alist))))
1878 alist (cdr alist))))
1879 (setq gnus-agent-article-alist (cdr first))
1880 (gnus-agent-save-alist group)
1881 ;; Mark all articles up to the first article
1882 ;; in `gnus-agent-article-alist' as read.
1883 (when (and info (caar gnus-agent-article-alist))
1884 (setcar (nthcdr 2 info)
1887 (cons 1 (- (caar gnus-agent-article-alist) 1)))))
1888 ;; Maybe everything has been expired from
1889 ;; `gnus-agent-article-alist' and so the above marking as
1890 ;; read could not be conducted, or there are
1891 ;; expired article within the range of the alist.
1894 (or (not (caar gnus-agent-article-alist))
1896 (caar gnus-agent-article-alist))))
1897 (setcar (nthcdr 2 info)
1900 (nreverse expired))))
1902 (concat "(gnus-group-set-info '"
1903 (gnus-prin1-to-string info)
1906 (if (gnus-gethash group orig)
1907 (setcar (gnus-gethash group orig) lowest)
1908 (gnus-sethash group (cons lowest highest) orig))))
1910 (set-buffer history)
1911 (setq histories (nreverse (sort histories '<)))
1913 (goto-char (pop histories))
1915 (gnus-agent-save-history)
1916 (gnus-agent-close-history)
1917 (gnus-write-active-file
1918 (gnus-agent-lib-file "active") orig))
1919 (gnus-message 4 "Expiry...done"))))))
1922 (defun gnus-agent-batch ()
1923 "Start Gnus, send queue and fetch session."
1925 (let ((init-file-user "")
1926 (gnus-always-read-dribble-file t))
1928 (let ((gnus-agent-confirmation-function 'gnus-agent-batch-confirmation))
1929 (gnus-group-send-queue)
1930 (gnus-agent-fetch-session)))
1932 (defun gnus-agent-retrieve-headers (articles group &optional fetch-old)
1934 (gnus-agent-create-buffer)
1935 (let ((gnus-decode-encoded-word-function 'identity)
1936 (file (gnus-agent-article-name ".overview" group))
1937 cached-articles uncached-articles)
1938 (gnus-make-directory (nnheader-translate-file-chars
1939 (file-name-directory file) t))
1940 (when (file-exists-p file)
1941 (with-current-buffer gnus-agent-overview-buffer
1943 (let ((nnheader-file-coding-system
1944 gnus-agent-file-coding-system))
1945 (nnheader-insert-nov-file file (car articles)))
1946 (nnheader-find-nov-line (car articles))
1948 (when (looking-at "[0-9]")
1949 (push (read (current-buffer)) cached-articles))
1951 (setq cached-articles (nreverse cached-articles))))
1952 (if (setq uncached-articles
1953 (gnus-sorted-difference articles cached-articles))
1955 (set-buffer nntp-server-buffer)
1957 (let (gnus-agent-cache)
1959 (gnus-retrieve-headers
1960 uncached-articles group fetch-old))
1961 (nnvirtual-convert-headers)))
1962 (set-buffer gnus-agent-overview-buffer)
1964 (set-buffer nntp-server-buffer)
1965 (copy-to-buffer gnus-agent-overview-buffer (point-min) (point-max))
1966 (when (and uncached-articles (file-exists-p file))
1967 (gnus-agent-braid-nov group uncached-articles file))
1968 (set-buffer nntp-server-buffer)
1969 (write-region-as-coding-system gnus-agent-file-coding-system
1970 (point-min) (point-max)
1972 (gnus-agent-load-alist group)
1973 (gnus-agent-save-alist group uncached-articles nil)
1974 (gnus-agent-open-history)
1975 (setq gnus-agent-current-history (gnus-agent-history-buffer))
1976 (gnus-agent-enter-history
1977 "last-header-fetched-for-session"
1978 (list (cons group (nth (- (length articles) 1) articles)))
1979 (time-to-days (current-time)))
1980 (gnus-agent-save-history))
1981 (set-buffer nntp-server-buffer)
1983 (insert-buffer-substring gnus-agent-overview-buffer)))
1985 (not (numberp fetch-old)))
1986 t ; Don't remove anything.
1987 (nnheader-nov-delete-outside-range
1988 (if fetch-old (max 1 (- (car articles) fetch-old))
1990 (car (last articles)))
1994 (defun gnus-agent-request-article (article group)
1995 "Retrieve ARTICLE in GROUP from the agent cache."
1996 (let* ((gnus-command-method (gnus-find-method-for-group group))
1998 (gnus-agent-directory)
1999 (gnus-agent-group-path group) "/"
2000 (number-to-string article)))
2001 (buffer-read-only nil))
2002 (when (and (file-exists-p file)
2003 (> (nth 7 (file-attributes file)) 0))
2005 (gnus-kill-all-overlays)
2006 (insert-file-contents-as-coding-system gnus-cache-coding-system file)
2009 (defun gnus-agent-regenerate-group (group &optional clean)
2011 (let ((dir (concat (gnus-agent-directory)
2012 (gnus-agent-group-path group) "/"))
2013 (file (gnus-agent-article-name ".overview" group))
2014 n point arts alist header new-alist changed)
2015 (when (file-exists-p dir)
2017 (sort (mapcar (lambda (name) (string-to-int name))
2018 (directory-files dir nil "^[0-9]+$" t))
2020 (gnus-make-directory (nnheader-translate-file-chars
2021 (file-name-directory file) t))
2022 (mm-with-unibyte-buffer
2023 (if (file-exists-p file)
2024 (let ((nnheader-file-coding-system
2025 gnus-agent-file-coding-system))
2026 (nnheader-insert-file-contents file)))
2027 (goto-char (point-min))
2029 (while (not (or (eobp) (looking-at "[0-9]")))
2030 (setq point (point))
2032 (delete-region point (point)))
2034 (setq n (read (current-buffer)))
2035 (when (and arts (> n (car arts)))
2037 (while (and arts (> n (car arts)))
2038 (message "Regenerating NOV %s %d..." group (car arts))
2039 (mm-with-unibyte-buffer
2040 (nnheader-insert-file-contents
2041 (concat dir (number-to-string (car arts))))
2042 (goto-char (point-min))
2043 (if (search-forward "\n\n" nil t)
2044 (delete-region (point) (point-max))
2045 (goto-char (point-max)))
2046 (setq header (nnheader-parse-head t)))
2047 (mail-header-set-number header (car arts))
2048 (nnheader-insert-nov header)
2050 (push (cons (car arts) t) alist)
2052 (if (and arts (= n (car arts)))
2054 (push (cons n t) alist)
2056 (push (cons n nil) alist))
2059 (write-region-as-coding-system gnus-agent-file-coding-system
2060 (point-min) (point-max)
2062 (setq gnus-agent-article-alist nil)
2064 (gnus-agent-load-alist group))
2065 (setq alist (sort alist 'car-less-than-car))
2066 (setq gnus-agent-article-alist (sort gnus-agent-article-alist
2067 'car-less-than-car))
2068 (while (and alist gnus-agent-article-alist)
2070 ((< (caar alist) (caar gnus-agent-article-alist))
2071 (push (pop alist) new-alist))
2072 ((> (caar alist) (caar gnus-agent-article-alist))
2073 (push (list (car (pop gnus-agent-article-alist))) new-alist))
2075 (pop gnus-agent-article-alist)
2076 (while (and gnus-agent-article-alist
2077 (= (caar alist) (caar gnus-agent-article-alist)))
2078 (pop gnus-agent-article-alist))
2079 (push (pop alist) new-alist))))
2081 (push (pop alist) new-alist))
2082 (while gnus-agent-article-alist
2083 (push (list (car (pop gnus-agent-article-alist))) new-alist))
2084 (setq gnus-agent-article-alist (nreverse new-alist))
2085 (gnus-agent-save-alist group)))
2087 (defun gnus-agent-regenerate-history (group article)
2088 (let ((file (concat (gnus-agent-directory)
2089 (gnus-agent-group-path group) "/"
2090 (number-to-string article))) id)
2091 (mm-with-unibyte-buffer
2092 (nnheader-insert-file-contents file)
2093 (message-narrow-to-head)
2094 (goto-char (point-min))
2095 (if (not (re-search-forward "^Message-ID: *<\\([^>\n]+\\)>" nil t))
2096 (setq id "No-Message-ID-in-article")
2097 (setq id (buffer-substring (match-beginning 1) (match-end 1))))
2098 (gnus-agent-enter-history
2099 id (list (cons group article))
2100 (time-to-days (nth 5 (file-attributes file)))))))
2103 (defun gnus-agent-regenerate (&optional clean)
2104 "Regenerate all agent covered files.
2105 If CLEAN, don't read existing active and agentview files."
2107 (message "Regenerating Gnus agent files...")
2108 (dolist (gnus-command-method gnus-agent-covered-methods)
2109 (let ((active-file (gnus-agent-lib-file "active"))
2110 history-hashtb active-hashtb active-changed
2111 history-changed point)
2112 (gnus-make-directory (file-name-directory active-file))
2114 (setq active-hashtb (gnus-make-hashtable 1000))
2115 (mm-with-unibyte-buffer
2116 (if (file-exists-p active-file)
2117 (let ((nnheader-file-coding-system
2118 gnus-agent-file-coding-system))
2119 (nnheader-insert-file-contents active-file))
2120 (setq active-changed t))
2121 (gnus-active-to-gnus-format
2122 nil (setq active-hashtb
2123 (gnus-make-hashtable
2124 (count-lines (point-min) (point-max)))))))
2125 (gnus-agent-open-history)
2126 (setq history-hashtb (gnus-make-hashtable 1000))
2127 (with-current-buffer
2128 (setq gnus-agent-current-history (gnus-agent-history-buffer))
2129 (goto-char (point-min))
2133 "\\([^\t\n]+\\)\t[0-9]+\t\\([^ \n]+\\) \\([0-9]+\\)")
2135 (unless (string= (match-string 1)
2136 "last-header-fetched-for-session")
2137 (gnus-sethash (match-string 2)
2138 (cons (string-to-number (match-string 3))
2139 (gnus-gethash-safe (match-string 2)
2143 (setq point (point))
2145 (delete-region point (point))
2146 (setq history-changed t))))
2147 (dolist (group (gnus-groups-from-server gnus-command-method))
2148 (gnus-agent-regenerate-group group clean)
2149 (let ((min (or (caar gnus-agent-article-alist) 1))
2150 (max (or (caar (last gnus-agent-article-alist)) 0))
2151 (active (gnus-gethash-safe (gnus-group-real-name group)
2155 (setq active (cons min max)
2157 (gnus-sethash group active active-hashtb))
2158 (when (> (car active) min)
2160 (setq active-changed t))
2161 (when (< (cdr active) max)
2163 (setq active-changed t))))
2164 (let ((arts (sort (gnus-gethash-safe group history-hashtb) '<))
2166 (gnus-sethash group arts history-hashtb)
2167 (while (and arts gnus-agent-article-alist)
2169 ((> (car arts) (caar gnus-agent-article-alist))
2170 (when (cdar gnus-agent-article-alist)
2171 (gnus-agent-regenerate-history
2172 group (caar gnus-agent-article-alist))
2173 (setq history-changed t))
2174 (setq n (car (pop gnus-agent-article-alist)))
2175 (while (and gnus-agent-article-alist
2176 (= n (caar gnus-agent-article-alist)))
2177 (pop gnus-agent-article-alist)))
2178 ((< (car arts) (caar gnus-agent-article-alist))
2180 (while (and arts (= n (car arts)))
2183 (setq n (car (pop gnus-agent-article-alist)))
2184 (while (and gnus-agent-article-alist
2185 (= n (caar gnus-agent-article-alist)))
2186 (pop gnus-agent-article-alist))
2188 (while (and arts (= n (car arts)))
2190 (while gnus-agent-article-alist
2191 (when (cdar gnus-agent-article-alist)
2192 (gnus-agent-regenerate-history
2193 group (caar gnus-agent-article-alist))
2194 (setq history-changed t))
2195 (pop gnus-agent-article-alist))))
2196 (when history-changed
2197 (message "Regenerate the history file of %s:%s"
2198 (car gnus-command-method)
2199 (cadr gnus-command-method))
2200 (gnus-agent-save-history))
2201 (gnus-agent-close-history)
2202 (when active-changed
2203 (message "Regenerate %s" active-file)
2204 (let ((nnmail-active-file-coding-system gnus-agent-file-coding-system))
2205 (gnus-write-active-file active-file active-hashtb)))))
2206 (message "Regenerating Gnus agent files...done"))
2208 (defun gnus-agent-go-online (&optional force)
2209 "Switch servers into online status."
2210 (interactive (list t))
2211 (dolist (server gnus-opened-servers)
2212 (when (eq (nth 1 server) 'offline)
2213 (if (if (eq force 'ask)
2215 (format "Switch %s:%s into online status? "
2216 (caar server) (cadar server)))
2218 (setcar (nthcdr 1 server) 'close)))))
2220 (defun gnus-agent-toggle-group-plugged (group)
2221 "Toggle the status of the server of the current group."
2222 (interactive (list (gnus-group-group-name)))
2223 (let* ((method (gnus-find-method-for-group group))
2224 (status (cadr (assoc method gnus-opened-servers))))
2225 (if (eq status 'offline)
2226 (gnus-server-set-status method 'closed)
2227 (gnus-close-server method)
2228 (gnus-server-set-status method 'offline))
2229 (message "Turn %s:%s from %s to %s." (car method) (cadr method)
2230 (if (eq status 'offline) 'offline 'online)
2231 (if (eq status 'offline) 'online 'offline))))
2233 (provide 'gnus-agent)
2235 ;;; gnus-agent.el ends here