gnus-agent.el 151 KB
Newer Older
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1
;;; gnus-agent.el --- unplugged support for Gnus
2 3 4

;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
;;   2005 Free Software Foundation, Inc.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
21 22
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
23 24 25 26 27 28 29

;;; Commentary:

;;; Code:

(require 'gnus)
(require 'gnus-cache)
30
(require 'nnmail)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
31 32
(require 'nnvirtual)
(require 'gnus-sum)
33
(require 'gnus-score)
34 35
(require 'gnus-srvr)
(require 'gnus-util)
36
(eval-when-compile
37 38 39 40
  (if (featurep 'xemacs)
      (require 'itimer)
    (require 'timer))
  (require 'cl))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
41

42 43 44 45 46
(eval-and-compile
  (autoload 'gnus-server-update-server "gnus-srvr")
  (autoload 'gnus-agent-customize-category "gnus-cus")
)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
(defcustom gnus-agent-directory (nnheader-concat gnus-directory "agent/")
  "Where the Gnus agent will store its files."
  :group 'gnus-agent
  :type 'directory)

(defcustom gnus-agent-plugged-hook nil
  "Hook run when plugging into the network."
  :group 'gnus-agent
  :type 'hook)

(defcustom gnus-agent-unplugged-hook nil
  "Hook run when unplugging from the network."
  :group 'gnus-agent
  :type 'hook)

62 63
(defcustom gnus-agent-fetched-hook nil
  "Hook run when finished fetching articles."
64
  :version "22.1"
65 66 67
  :group 'gnus-agent
  :type 'hook)

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
68 69 70 71 72 73
(defcustom gnus-agent-handle-level gnus-level-subscribed
  "Groups on levels higher than this variable will be ignored by the Agent."
  :group 'gnus-agent
  :type 'integer)

(defcustom gnus-agent-expire-days 7
74 75
  "Read articles older than this will be expired.
If you wish to disable Agent expiring, see `gnus-agent-enable-expiration'."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
76
  :group 'gnus-agent
77
  :type '(number :tag "days"))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
78 79 80 81 82 83 84 85 86 87 88 89

(defcustom gnus-agent-expire-all nil
  "If non-nil, also expire unread, ticked and dormant articles.
If nil, only read articles will be expired."
  :group 'gnus-agent
  :type 'boolean)

(defcustom gnus-agent-group-mode-hook nil
  "Hook run in Agent group minor modes."
  :group 'gnus-agent
  :type 'hook)

90 91 92 93
;; Extracted from gnus-xmas-redefine in order to preserve user settings
(when (featurep 'xemacs)
  (add-hook 'gnus-agent-group-mode-hook 'gnus-xmas-agent-group-menu-add))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
94 95 96 97 98
(defcustom gnus-agent-summary-mode-hook nil
  "Hook run in Agent summary minor modes."
  :group 'gnus-agent
  :type 'hook)

99 100 101 102
;; Extracted from gnus-xmas-redefine in order to preserve user settings
(when (featurep 'xemacs)
  (add-hook 'gnus-agent-summary-mode-hook 'gnus-xmas-agent-summary-menu-add))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
103 104 105 106 107
(defcustom gnus-agent-server-mode-hook nil
  "Hook run in Agent summary minor modes."
  :group 'gnus-agent
  :type 'hook)

108 109 110 111
;; Extracted from gnus-xmas-redefine in order to preserve user settings
(when (featurep 'xemacs)
  (add-hook 'gnus-agent-server-mode-hook 'gnus-xmas-agent-server-menu-add))

112 113
(defcustom gnus-agent-confirmation-function 'y-or-n-p
  "Function to confirm when error happens."
114
  :version "21.1"
115 116
  :group 'gnus-agent
  :type 'function)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
117

118
(defcustom gnus-agent-synchronize-flags t
119 120
  "Indicate if flags are synchronized when you plug in.
If this is `ask' the hook will query the user."
121 122
  ;; If the default switches to something else than nil, then the function
  ;; should be fixed not be exceedingly slow.  See 2005-09-20 ChangeLog entry.
123
  :version "21.1"
124 125 126 127 128
  :type '(choice (const :tag "Always" t)
		 (const :tag "Never" nil)
		 (const :tag "Ask" ask))
  :group 'gnus-agent)

129 130 131
(defcustom gnus-agent-go-online 'ask
  "Indicate if offline servers go online when you plug in.
If this is `ask' the hook will query the user."
132
  :version "21.3"
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  :type '(choice (const :tag "Always" t)
		 (const :tag "Never" nil)
		 (const :tag "Ask" ask))
  :group 'gnus-agent)

(defcustom gnus-agent-mark-unread-after-downloaded t
  "Indicate whether to mark articles unread after downloaded."
  :version "21.1"
  :type 'boolean
  :group 'gnus-agent)

(defcustom gnus-agent-download-marks '(download)
  "Marks for downloading."
  :version "21.1"
  :type '(repeat (symbol :tag "Mark"))
  :group 'gnus-agent)

(defcustom gnus-agent-consider-all-articles nil
  "When non-nil, the agent will let the agent predicate decide
whether articles need to be downloaded or not, for all articles.  When
nil, the default, the agent will only let the predicate decide
whether unread articles are downloaded or not.  If you enable this,
groups with large active ranges may open slower and you may also want
to look into the agent expiry settings to block the expiration of
read articles as they would just be downloaded again."
158
  :version "22.1"
159 160 161 162 163 164 165
  :type 'boolean
  :group 'gnus-agent)

(defcustom gnus-agent-max-fetch-size 10000000 ;; 10 Mb
  "Chunk size for `gnus-agent-fetch-session'.
The function will split its article fetches into chunks smaller than
this limit."
166
  :version "22.1"
167 168 169 170 171 172 173 174 175 176
  :group 'gnus-agent
  :type 'integer)

(defcustom gnus-agent-enable-expiration 'ENABLE
  "The default expiration state for each group.
When set to ENABLE, the default, `gnus-agent-expire' will expire old
contents from a group's local storage.  This value may be overridden
to disable expiration in specific categories, topics, and groups.  Of
course, you could change gnus-agent-enable-expiration to DISABLE then
enable expiration per categories, topics, and groups."
177
  :version "22.1"
178 179 180 181 182 183 184 185 186
  :group 'gnus-agent
  :type '(radio (const :format "Enable " ENABLE)
                (const :format "Disable " DISABLE)))

(defcustom gnus-agent-expire-unagentized-dirs t
  "*Whether expiration should expire in unagentized directories.
Have gnus-agent-expire scan the directories under
\(gnus-agent-directory) for groups that are no longer agentized.
When found, offer to remove them."
187
  :version "22.1"
188 189 190 191 192 193 194
  :type 'boolean
  :group 'gnus-agent)

(defcustom gnus-agent-auto-agentize-methods '(nntp nnimap)
  "Initially, all servers from these methods are agentized.
The user may remove or add servers using the Server buffer.
See Info node `(gnus)Server Buffer'."
195
  :version "22.1"
196 197 198 199 200 201 202
  :type '(repeat symbol)
  :group 'gnus-agent)

(defcustom gnus-agent-queue-mail t
  "Whether and when outgoing mail should be queued by the agent.
When `always', always queue outgoing mail.  When nil, never
queue.  Otherwise, queue if and only if unplugged."
203
  :version "22.1"
204 205 206 207 208 209 210 211
  :group 'gnus-agent
  :type '(radio (const :format "Always" always)
		(const :format "Never" nil)
		(const :format "When plugged" t)))

(defcustom gnus-agent-prompt-send-queue nil
  "If non-nil, `gnus-group-send-queue' will prompt if called when
unplugged."
212
  :version "22.1"
213 214 215
  :group 'gnus-agent
  :type 'boolean)

216 217 218 219 220 221 222 223 224 225 226
(defcustom gnus-agent-article-alist-save-format 1
  "Indicates whether to use compression(2), verses no
  compression(1), when writing agentview files.  The compressed
  files do save space but load times are 6-7 times higher.  A
  group must be opened then closed for the agentview to be
  updated using the new format."
  :version "22.1"
  :group 'gnus-agent
  :type '(radio (const :format "Compressed" 2)
		(const :format "Uncompressed" 1)))

227
;;; Internal variables
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
228 229 230

(defvar gnus-agent-history-buffers nil)
(defvar gnus-agent-buffer-alist nil)
231
(defvar gnus-agent-article-alist nil
232
  "An assoc list identifying the articles whose headers have been fetched.
233 234 235 236 237
If successfully fetched, these headers will be stored in the group's overview
file.  The key of each assoc pair is the article ID, the value of each assoc
pair is a flag indicating whether the identified article has been downloaded
\(gnus-agent-fetch-articles sets the value to the day of the download).
NOTES:
238
1) The last element of this list can not be expired as some
239 240 241
   routines (for example, get-agent-fetch-headers) use the last
   value to track which articles have had their headers retrieved.
2) The function `gnus-agent-regenerate' may destructively modify the value.")
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
242 243 244 245 246 247 248 249 250
(defvar gnus-agent-group-alist nil)
(defvar gnus-category-alist nil)
(defvar gnus-agent-current-history nil)
(defvar gnus-agent-overview-buffer nil)
(defvar gnus-category-predicate-cache nil)
(defvar gnus-category-group-cache nil)
(defvar gnus-agent-spam-hashtb nil)
(defvar gnus-agent-file-name nil)
(defvar gnus-agent-send-mail-function nil)
251
(defvar gnus-agent-file-coding-system 'raw-text)
252
(defvar gnus-agent-file-loading-cache nil)
253

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
254 255 256 257 258 259 260 261 262 263 264 265
;; Dynamic variables
(defvar gnus-headers)
(defvar gnus-score)

;;;
;;; Setup
;;;

(defun gnus-open-agent ()
  (setq gnus-agent t)
  (gnus-agent-read-servers)
  (gnus-category-read)
266
  (gnus-agent-create-buffer)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
267 268 269 270
  (add-hook 'gnus-group-mode-hook 'gnus-agent-mode)
  (add-hook 'gnus-summary-mode-hook 'gnus-agent-mode)
  (add-hook 'gnus-server-mode-hook 'gnus-agent-mode))

271 272 273 274 275 276 277 278 279
(defun gnus-agent-create-buffer ()
  (if (gnus-buffer-live-p gnus-agent-overview-buffer)
      t
    (setq gnus-agent-overview-buffer
	  (gnus-get-buffer-create " *Gnus agent overview*"))
    (with-current-buffer gnus-agent-overview-buffer
      (mm-enable-multibyte))
    nil))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
280 281 282
(gnus-add-shutdown 'gnus-close-agent 'gnus)

(defun gnus-close-agent ()
283
  (setq gnus-category-predicate-cache nil
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
284 285 286 287 288 289 290 291 292 293
	gnus-category-group-cache nil
	gnus-agent-spam-hashtb nil)
  (gnus-kill-buffer gnus-agent-overview-buffer))

;;;
;;; Utility functions
;;;

(defun gnus-agent-read-file (file)
  "Load FILE and do a `read' there."
294
  (with-temp-buffer
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
295 296 297 298 299 300 301 302 303 304 305 306
    (ignore-errors
      (nnheader-insert-file-contents file)
      (goto-char (point-min))
      (read (current-buffer)))))

(defsubst gnus-agent-method ()
  (concat (symbol-name (car gnus-command-method)) "/"
	  (if (equal (cadr gnus-command-method) "")
	      "unnamed"
	    (cadr gnus-command-method))))

(defsubst gnus-agent-directory ()
307
  "The name of the Gnus agent directory."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
308 309 310 311
  (nnheader-concat gnus-agent-directory
		   (nnheader-translate-file-chars (gnus-agent-method)) "/"))

(defun gnus-agent-lib-file (file)
312
  "The full name of the Gnus agent library FILE."
313 314 315
  (expand-file-name file
		    (file-name-as-directory
		     (expand-file-name "agent.lib" (gnus-agent-directory)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
(defun gnus-agent-cat-set-property (category property value)
  (if value
      (setcdr (or (assq property category)
              (let ((cell (cons property nil)))
                    (setcdr category (cons cell (cdr category)))
                    cell)) value)
    (let ((category category))
      (while (cond ((eq property (caadr category))
                    (setcdr category (cddr category))
                    nil)
                   (t
                    (setq category (cdr category)))))))
  category)

(eval-when-compile
  (defmacro gnus-agent-cat-defaccessor (name prop-name)
    "Define accessor and setter methods for manipulating a list of the form
\(NAME (PROPERTY1 VALUE1) ... (PROPERTY_N VALUE_N)).
Given the call (gnus-agent-cat-defaccessor func PROPERTY1), the list may be
manipulated as follows:
  (func LIST): Returns VALUE1
  (setf (func LIST) NEW_VALUE1): Replaces VALUE1 with NEW_VALUE1."
    `(progn (defmacro ,name (category)
              (list (quote cdr) (list (quote assq)
                                      (quote (quote ,prop-name)) category)))

            (define-setf-method ,name (category)
              (let* ((--category--temp-- (make-symbol "--category--"))
                     (--value--temp-- (make-symbol "--value--")))
                (list (list --category--temp--) ; temporary-variables
                      (list category)   ; value-forms
                      (list --value--temp--) ; store-variables
                      (let* ((category --category--temp--) ; store-form
                             (value --value--temp--))
                        (list (quote gnus-agent-cat-set-property)
                              category
                              (quote (quote ,prop-name))
                              value))
                      (list (quote ,name) --category--temp--) ; access-form
                      )))))
  )

(defmacro gnus-agent-cat-name (category)
  `(car ,category))

(gnus-agent-cat-defaccessor
 gnus-agent-cat-days-until-old             agent-days-until-old)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-enable-expiration          agent-enable-expiration)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-groups                     agent-groups)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-high-score                 agent-high-score)
(gnus-agent-cat-defaccessor
371
 gnus-agent-cat-length-when-long           agent-long-article)
372
(gnus-agent-cat-defaccessor
373
 gnus-agent-cat-length-when-short          agent-short-article)
374 375 376 377 378
(gnus-agent-cat-defaccessor
 gnus-agent-cat-low-score                  agent-low-score)
(gnus-agent-cat-defaccessor
 gnus-agent-cat-predicate                  agent-predicate)
(gnus-agent-cat-defaccessor
379
 gnus-agent-cat-score-file                 agent-score)
380
(gnus-agent-cat-defaccessor
381
 gnus-agent-cat-enable-undownloaded-faces  agent-enable-undownloaded-faces)
382

383 384 385 386 387

;; This form is equivalent to defsetf except that it calls make-symbol
;; whereas defsetf calls gensym (Using gensym creates a run-time
;; dependency on the CL library).

388
(eval-and-compile
389 390 391 392 393 394 395 396 397 398 399
  (define-setf-method gnus-agent-cat-groups (category)
    (let* ((--category--temp-- (make-symbol "--category--"))
	   (--groups--temp-- (make-symbol "--groups--")))
      (list (list --category--temp--)
	    (list category)
	    (list --groups--temp--)
	    (let* ((category --category--temp--)
		   (groups --groups--temp--))
	      (list (quote gnus-agent-set-cat-groups) category groups))
	    (list (quote gnus-agent-cat-groups) --category--temp--))))
  )
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

(defun gnus-agent-set-cat-groups (category groups)
  (unless (eq groups 'ignore)
    (let ((new-g groups)
          (old-g (gnus-agent-cat-groups category)))
      (cond ((eq new-g old-g)
             ;; gnus-agent-add-group is fiddling with the group
             ;; list. Still, Im done.
             nil
             )
            ((eq new-g (cdr old-g))
             ;; gnus-agent-add-group is fiddling with the group list
             (setcdr (or (assq 'agent-groups category)
                         (let ((cell (cons 'agent-groups nil)))
                           (setcdr category (cons cell (cdr category)))
                           cell)) new-g))
            (t
             (let ((groups groups))
               (while groups
                 (let* ((group        (pop groups))
                        (old-category (gnus-group-category group)))
                   (if (eq category old-category)
                       nil
                     (setf (gnus-agent-cat-groups old-category)
                           (delete group (gnus-agent-cat-groups
                                          old-category))))))
               ;; Purge cache as preceeding loop invalidated it.
               (setq gnus-category-group-cache nil))

             (setcdr (or (assq 'agent-groups category)
                         (let ((cell (cons 'agent-groups nil)))
                           (setcdr category (cons cell (cdr category)))
                           cell)) groups))))))

(defsubst gnus-agent-cat-make (name &optional default-agent-predicate)
  (list name `(agent-predicate . ,(or default-agent-predicate 'false))))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
437 438 439 440
;;; Fetching setup functions.

(defun gnus-agent-start-fetch ()
  "Initialize data structures for efficient fetching."
441
  (gnus-agent-create-buffer))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
442 443 444 445 446 447 448 449 450 451 452

(defun gnus-agent-stop-fetch ()
  "Save all data structures and clean up."
  (setq gnus-agent-spam-hashtb nil)
  (save-excursion
    (set-buffer nntp-server-buffer)
    (widen)))

(defmacro gnus-agent-with-fetch (&rest forms)
  "Do FORMS safely."
  `(unwind-protect
453
       (let ((gnus-agent-fetching t))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
454 455 456 457 458 459 460
	 (gnus-agent-start-fetch)
	 ,@forms)
     (gnus-agent-stop-fetch)))

(put 'gnus-agent-with-fetch 'lisp-indent-function 0)
(put 'gnus-agent-with-fetch 'edebug-form-spec '(body))

461 462 463 464 465 466 467
(defmacro gnus-agent-append-to-list (tail value)
  `(setq ,tail (setcdr ,tail (cons ,value nil))))

(defmacro gnus-agent-message (level &rest args)
  `(if (<= ,level gnus-verbose)
       (message ,@args)))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
;;;
;;; Mode infestation
;;;

(defvar gnus-agent-mode-hook nil
  "Hook run when installing agent mode.")

(defvar gnus-agent-mode nil)
(defvar gnus-agent-mode-status '(gnus-agent-mode " Plugged"))

(defun gnus-agent-mode ()
  "Minor mode for providing a agent support in Gnus buffers."
  (let* ((buffer (progn (string-match "^gnus-\\(.*\\)-mode$"
				      (symbol-name major-mode))
			(match-string 1 (symbol-name major-mode))))
	 (mode (intern (format "gnus-agent-%s-mode" buffer))))
    (set (make-local-variable 'gnus-agent-mode) t)
    (set mode nil)
    (set (make-local-variable mode) t)
    ;; Set up the menu.
    (when (gnus-visual-p 'agent-menu 'menu)
      (funcall (intern (format "gnus-agent-%s-make-menu-bar" buffer))))
    (unless (assq 'gnus-agent-mode minor-mode-alist)
      (push gnus-agent-mode-status minor-mode-alist))
    (unless (assq mode minor-mode-map-alist)
      (push (cons mode (symbol-value (intern (format "gnus-agent-%s-mode-map"
						     buffer))))
	    minor-mode-map-alist))
    (when (eq major-mode 'gnus-group-mode)
497 498 499 500 501 502 503
      (let ((init-plugged gnus-plugged)
            (gnus-agent-go-online nil))
        ;; g-a-t-p does nothing when gnus-plugged isn't changed.
        ;; Therefore, make certain that the current value does not
        ;; match the desired initial value.
        (setq gnus-plugged :unknown)
        (gnus-agent-toggle-plugged init-plugged)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
504 505 506 507 508 509 510 511 512
    (gnus-run-hooks 'gnus-agent-mode-hook
		    (intern (format "gnus-agent-%s-mode-hook" buffer)))))

(defvar gnus-agent-group-mode-map (make-sparse-keymap))
(gnus-define-keys gnus-agent-group-mode-map
  "Ju" gnus-agent-fetch-groups
  "Jc" gnus-enter-category-buffer
  "Jj" gnus-agent-toggle-plugged
  "Js" gnus-agent-fetch-session
513
  "JY" gnus-agent-synchronize-flags
514
  "JS" gnus-group-send-queue
515
  "Ja" gnus-agent-add-group
516 517
  "Jr" gnus-agent-remove-group
  "Jo" gnus-agent-toggle-group-plugged)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
518 519 520 521 522 523 524

(defun gnus-agent-group-make-menu-bar ()
  (unless (boundp 'gnus-agent-group-menu)
    (easy-menu-define
     gnus-agent-group-menu gnus-agent-group-mode-map ""
     '("Agent"
       ["Toggle plugged" gnus-agent-toggle-plugged t]
525
       ["Toggle group plugged" gnus-agent-toggle-group-plugged t]
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
526
       ["List categories" gnus-enter-category-buffer t]
527 528 529
       ["Add (current) group to category" gnus-agent-add-group t]
       ["Remove (current) group from category" gnus-agent-remove-group t]
       ["Send queue" gnus-group-send-queue gnus-plugged]
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
530 531
       ("Fetch"
	["All" gnus-agent-fetch-session gnus-plugged]
532 533 534
	["Group" gnus-agent-fetch-group gnus-plugged])
       ["Synchronize flags" gnus-agent-synchronize-flags t]
       ))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
535 536 537 538

(defvar gnus-agent-summary-mode-map (make-sparse-keymap))
(gnus-define-keys gnus-agent-summary-mode-map
  "Jj" gnus-agent-toggle-plugged
539 540 541
  "Ju" gnus-agent-summary-fetch-group
  "JS" gnus-agent-fetch-group
  "Js" gnus-agent-summary-fetch-series
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555
  "J#" gnus-agent-mark-article
  "J\M-#" gnus-agent-unmark-article
  "@" gnus-agent-toggle-mark
  "Jc" gnus-agent-catchup)

(defun gnus-agent-summary-make-menu-bar ()
  (unless (boundp 'gnus-agent-summary-menu)
    (easy-menu-define
     gnus-agent-summary-menu gnus-agent-summary-mode-map ""
     '("Agent"
       ["Toggle plugged" gnus-agent-toggle-plugged t]
       ["Mark as downloadable" gnus-agent-mark-article t]
       ["Unmark as downloadable" gnus-agent-unmark-article t]
       ["Toggle mark" gnus-agent-toggle-mark t]
556
       ["Fetch downloadable" gnus-agent-summary-fetch-group t]
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
       ["Catchup undownloaded" gnus-agent-catchup t]))))

(defvar gnus-agent-server-mode-map (make-sparse-keymap))
(gnus-define-keys gnus-agent-server-mode-map
  "Jj" gnus-agent-toggle-plugged
  "Ja" gnus-agent-add-server
  "Jr" gnus-agent-remove-server)

(defun gnus-agent-server-make-menu-bar ()
  (unless (boundp 'gnus-agent-server-menu)
    (easy-menu-define
     gnus-agent-server-menu gnus-agent-server-mode-map ""
     '("Agent"
       ["Toggle plugged" gnus-agent-toggle-plugged t]
       ["Add" gnus-agent-add-server t]
       ["Remove" gnus-agent-remove-server t]))))

574 575 576 577
(defun gnus-agent-make-mode-line-string (string mouse-button mouse-func)
  (if (and (fboundp 'propertize)
	   (fboundp 'make-mode-line-mouse-map))
      (propertize string 'local-map
578 579
		  (make-mode-line-mouse-map mouse-button mouse-func)
		  'mouse-face 'mode-line-highlight)
580 581 582
    string))

(defun gnus-agent-toggle-plugged (set-to)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
583 584
  "Toggle whether Gnus is unplugged or not."
  (interactive (list (not gnus-plugged)))
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
  (cond ((eq set-to gnus-plugged)
         nil)
        (set-to
         (setq gnus-plugged set-to)
         (gnus-run-hooks 'gnus-agent-plugged-hook)
         (setcar (cdr gnus-agent-mode-status)
                 (gnus-agent-make-mode-line-string " Plugged"
                                                   'mouse-2
                                                   'gnus-agent-toggle-plugged))
         (gnus-agent-go-online gnus-agent-go-online)
         (gnus-agent-possibly-synchronize-flags))
        (t
         (gnus-agent-close-connections)
         (setq gnus-plugged set-to)
         (gnus-run-hooks 'gnus-agent-unplugged-hook)
         (setcar (cdr gnus-agent-mode-status)
                 (gnus-agent-make-mode-line-string " Unplugged"
                                                   'mouse-2
                                                   'gnus-agent-toggle-plugged))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
604 605
  (set-buffer-modified-p t))

606 607 608 609 610 611 612 613 614 615
(defmacro gnus-agent-while-plugged (&rest body)
  `(let ((original-gnus-plugged gnus-plugged))
    (unwind-protect
        (progn (gnus-agent-toggle-plugged t)
               ,@body)
      (gnus-agent-toggle-plugged original-gnus-plugged))))

(put 'gnus-agent-while-plugged 'lisp-indent-function 0)
(put 'gnus-agent-while-plugged 'edebug-form-spec '(body))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
616 617
(defun gnus-agent-close-connections ()
  "Close all methods covered by the Gnus agent."
618
  (let ((methods (gnus-agent-covered-methods)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    (while methods
      (gnus-close-server (pop methods)))))

;;;###autoload
(defun gnus-unplugged ()
  "Start Gnus unplugged."
  (interactive)
  (setq gnus-plugged nil)
  (gnus))

;;;###autoload
(defun gnus-plugged ()
  "Start Gnus plugged."
  (interactive)
  (setq gnus-plugged t)
  (gnus))

636 637 638 639 640 641 642
;;;###autoload
(defun gnus-slave-unplugged (&optional arg)
  "Read news as a slave unplugged."
  (interactive "P")
  (setq gnus-plugged nil)
  (gnus arg nil 'slave))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
643 644 645 646
;;;###autoload
(defun gnus-agentize ()
  "Allow Gnus to be an offline newsreader.

647 648 649
The gnus-agentize function is now called internally by gnus when
gnus-agent is set.  If you wish to avoid calling gnus-agentize,
customize gnus-agent to nil.
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
650

651 652 653
This will modify the `gnus-setup-news-hook', and
`message-send-mail-real-function' variables, and install the Gnus agent
minor mode in all Gnus buffers."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
654 655 656 657
  (interactive)
  (gnus-open-agent)
  (add-hook 'gnus-setup-news-hook 'gnus-agent-queue-setup)
  (unless gnus-agent-send-mail-function
658 659
    (setq gnus-agent-send-mail-function
	  (or message-send-mail-real-function
660
	      (function (lambda () (funcall message-send-mail-function))))
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
	  message-send-mail-real-function 'gnus-agent-send-mail))

  ;; If the servers file doesn't exist, auto-agentize some servers and
  ;; save the servers file so this auto-agentizing isn't invoked
  ;; again.
  (unless (file-exists-p (nnheader-concat gnus-agent-directory "lib/servers"))
    (gnus-message 3 "First time agent user, agentizing remote groups...")
    (mapc
     (lambda (server-or-method)
       (let ((method (gnus-server-to-method server-or-method)))
	 (when (memq (car method)
		     gnus-agent-auto-agentize-methods)
	   (push (gnus-method-to-server method)
		 gnus-agent-covered-methods)
	   (setq gnus-agent-method-p-cache nil))))
     (cons gnus-select-method gnus-secondary-select-methods))
    (gnus-agent-write-servers)))

(defun gnus-agent-queue-setup (&optional group-name)
  "Make sure the queue group exists.
Optional arg GROUP-NAME allows to specify another group."
  (unless (gnus-gethash (format "nndraft:%s" (or group-name "queue"))
			gnus-newsrc-hashtb)
    (gnus-request-create-group (or group-name "queue") '(nndraft ""))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
685
    (let ((gnus-level-default-subscribed 1))
686 687
      (gnus-subscribe-group (format "nndraft:%s" (or group-name "queue"))
			    nil '(nndraft "")))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
688
    (gnus-group-set-parameter
689 690
     (format "nndraft:%s" (or group-name "queue"))
     'gnus-dummy '((gnus-draft-mode)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
691 692

(defun gnus-agent-send-mail ()
693 694
  (if (or (not gnus-agent-queue-mail)
	  (and gnus-plugged (not (eq gnus-agent-queue-mail 'always))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
695 696 697 698 699 700
      (funcall gnus-agent-send-mail-function)
    (goto-char (point-min))
    (re-search-forward
     (concat "^" (regexp-quote mail-header-separator) "\n"))
    (replace-match "\n")
    (gnus-agent-insert-meta-information 'mail)
701
    (gnus-request-accept-article "nndraft:queue" nil t t)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
702 703 704

(defun gnus-agent-insert-meta-information (type &optional method)
  "Insert meta-information into the message that says how it's to be posted.
705
TYPE can be either `mail' or `news'.  If the latter, then METHOD can
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
706 707 708 709 710 711 712 713 714 715 716
be a select method."
  (save-excursion
    (message-remove-header gnus-agent-meta-information-header)
    (goto-char (point-min))
    (insert gnus-agent-meta-information-header ": "
	    (symbol-name type) " " (format "%S" method)
	    "\n")
    (forward-char -1)
    (while (search-backward "\n" nil t)
      (replace-match "\\n" t t))))

717 718 719 720
(defun gnus-agent-restore-gcc ()
  "Restore GCC field from saved header."
  (save-excursion
    (goto-char (point-min))
721 722
    (while (re-search-forward
	    (concat "^" (regexp-quote gnus-agent-gcc-header) ":") nil t)
723 724 725 726 727 728
      (replace-match "Gcc:" 'fixedcase))))

(defun gnus-agent-any-covered-gcc ()
  (save-restriction
    (message-narrow-to-headers)
    (let* ((gcc (mail-fetch-field "gcc" nil t))
729
	   (methods (and gcc
730 731
			 (mapcar 'gnus-inews-group-method
				 (message-unquote-tokens
732
				  (message-tokenize-header
733 734 735
				   gcc " ,")))))
	   covered)
      (while (and (not covered) methods)
736
	(setq covered (gnus-agent-method-p (car methods))
737 738 739
	      methods (cdr methods)))
      covered)))

740
;;;###autoload
741 742 743 744 745 746 747 748 749 750 751 752 753 754
(defun gnus-agent-possibly-save-gcc ()
  "Save GCC if Gnus is unplugged."
  (when (and (not gnus-plugged) (gnus-agent-any-covered-gcc))
    (save-excursion
      (goto-char (point-min))
      (let ((case-fold-search t))
	(while (re-search-forward "^gcc:" nil t)
	  (replace-match (concat gnus-agent-gcc-header ":") 'fixedcase))))))

(defun gnus-agent-possibly-do-gcc ()
  "Do GCC if Gnus is plugged."
  (when (or gnus-plugged (not (gnus-agent-any-covered-gcc)))
    (gnus-inews-do-gcc)))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
755 756 757 758 759 760 761
;;;
;;; Group mode commands
;;;

(defun gnus-agent-fetch-groups (n)
  "Put all new articles in the current groups into the Agent."
  (interactive "P")
762 763
  (unless gnus-plugged
    (error "Groups can't be fetched when Gnus is unplugged"))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
764 765
  (gnus-group-iterate n 'gnus-agent-fetch-group))

766
(defun gnus-agent-fetch-group (&optional group)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
767 768
  "Put all new articles in GROUP into the Agent."
  (interactive (list (gnus-group-group-name)))
769
  (setq group (or group gnus-newsgroup-name))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
770 771
  (unless group
    (error "No group on the current line"))
772 773 774 775 776 777

  (gnus-agent-while-plugged
    (let ((gnus-command-method (gnus-find-method-for-group group)))
      (gnus-agent-with-fetch
        (gnus-agent-fetch-group-1 group gnus-command-method)
        (gnus-message 5 "Fetching %s...done" group)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

(defun gnus-agent-add-group (category arg)
  "Add the current group to an agent category."
  (interactive
   (list
    (intern
     (completing-read
      "Add to category: "
      (mapcar (lambda (cat) (list (symbol-name (car cat))))
	      gnus-category-alist)
      nil t))
    current-prefix-arg))
  (let ((cat (assq category gnus-category-alist))
	c groups)
    (gnus-group-iterate arg
      (lambda (group)
794 795 796
	(when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
	  (setf (gnus-agent-cat-groups c)
                (delete group (gnus-agent-cat-groups c))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
797
	(push group groups)))
798 799
    (setf (gnus-agent-cat-groups cat)
          (nconc (gnus-agent-cat-groups cat) groups))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
800 801
    (gnus-category-write)))

802 803 804 805 806 807
(defun gnus-agent-remove-group (arg)
  "Remove the current group from its agent category, if any."
  (interactive "P")
  (let (c)
    (gnus-group-iterate arg
      (lambda (group)
808 809 810
	(when (gnus-agent-cat-groups (setq c (gnus-group-category group)))
	  (setf (gnus-agent-cat-groups c)
                (delete group (gnus-agent-cat-groups c))))))
811 812
    (gnus-category-write)))

813 814 815 816
(defun gnus-agent-synchronize-flags ()
  "Synchronize unplugged flags with servers."
  (interactive)
  (save-excursion
817
    (dolist (gnus-command-method (gnus-agent-covered-methods))
818 819 820 821 822
      (when (file-exists-p (gnus-agent-lib-file "flags"))
	(gnus-agent-synchronize-flags-server gnus-command-method)))))

(defun gnus-agent-possibly-synchronize-flags ()
  "Synchronize flags according to `gnus-agent-synchronize-flags'."
823 824
  (interactive)
  (save-excursion
825
    (dolist (gnus-command-method (gnus-agent-covered-methods))
826 827
      (when (and (file-exists-p (gnus-agent-lib-file "flags"))
		 (not (eq (gnus-server-status gnus-command-method) 'offline)))
828 829 830 831
	(gnus-agent-possibly-synchronize-flags-server gnus-command-method)))))

(defun gnus-agent-synchronize-flags-server (method)
  "Synchronize flags set when unplugged for server."
832 833
  (let ((gnus-command-method method)
	(gnus-agent nil))
834 835 836 837
    (when (file-exists-p (gnus-agent-lib-file "flags"))
      (set-buffer (get-buffer-create " *Gnus Agent flag synchronize*"))
      (erase-buffer)
      (nnheader-insert-file-contents (gnus-agent-lib-file "flags"))
838
      (cond ((null gnus-plugged)
839 840
	     (gnus-message
	      1 "You must be plugged to synchronize flags with server %s"
841 842
	      (nth 1 gnus-command-method)))
	    ((null (gnus-check-server gnus-command-method))
843
	     (gnus-message
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	      1 "Couldn't open server %s" (nth 1 gnus-command-method)))
	    (t
	     (condition-case err
		 (while t
		   (let ((bgn (point)))
		     (eval (read (current-buffer)))
		     (delete-region bgn (point))))
	       (end-of-file
		(delete-file (gnus-agent-lib-file "flags")))
	       (error
		(let ((file (gnus-agent-lib-file "flags")))
		  (write-region (point-min) (point-max)
				(gnus-agent-lib-file "flags") nil 'silent)
		  (error "Couldn't set flags from file %s due to %s"
			 file (error-message-string err)))))))
859 860 861 862 863 864 865
      (kill-buffer nil))))

(defun gnus-agent-possibly-synchronize-flags-server (method)
  "Synchronize flags for server according to `gnus-agent-synchronize-flags'."
  (when (or (and gnus-agent-synchronize-flags
		 (not (eq gnus-agent-synchronize-flags 'ask)))
	    (and (eq gnus-agent-synchronize-flags 'ask)
866
		 (gnus-y-or-n-p (format "Synchronize flags on server `%s'? "
867 868
					(cadr method)))))
    (gnus-agent-synchronize-flags-server method)))
869

870 871
;;;###autoload
(defun gnus-agent-rename-group (old-group new-group)
872 873 874 875 876
  "Rename fully-qualified OLD-GROUP as NEW-GROUP.
Always updates the agent, even when disabled, as the old agent
files would corrupt gnus when the agent was next enabled.
Depends upon the caller to determine whether group renaming is
supported."
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
  (let* ((old-command-method (gnus-find-method-for-group old-group))
	 (old-path           (directory-file-name
			      (let (gnus-command-method old-command-method)
				(gnus-agent-group-pathname old-group))))
	 (new-command-method (gnus-find-method-for-group new-group))
	 (new-path           (directory-file-name
			      (let (gnus-command-method new-command-method)
				(gnus-agent-group-pathname new-group)))))
    (gnus-rename-file old-path new-path t)

    (let* ((old-real-group (gnus-group-real-name old-group))
	   (new-real-group (gnus-group-real-name new-group))
	   (old-active (gnus-agent-get-group-info old-command-method old-real-group)))
      (gnus-agent-save-group-info old-command-method old-real-group nil)
      (gnus-agent-save-group-info new-command-method new-real-group old-active)

893
      (let ((old-local (gnus-agent-get-local old-group
894 895 896 897 898 899 900 901 902 903
					     old-real-group old-command-method)))
	(gnus-agent-set-local old-group
			      nil nil
			      old-real-group old-command-method)
	(gnus-agent-set-local new-group
			      (car old-local) (cdr old-local)
			      new-real-group new-command-method)))))

;;;###autoload
(defun gnus-agent-delete-group (group)
904 905 906 907 908
  "Delete fully-qualified GROUP.
Always updates the agent, even when disabled, as the old agent
files would corrupt gnus when the agent was next enabled.
Depends upon the caller to determine whether group deletion is
supported."
909 910 911 912
  (let* ((command-method (gnus-find-method-for-group group))
	 (path           (directory-file-name
			  (let (gnus-command-method command-method)
			    (gnus-agent-group-pathname group)))))
913
    (gnus-delete-directory path)
914 915 916 917

    (let* ((real-group (gnus-group-real-name group)))
      (gnus-agent-save-group-info command-method real-group nil)

918
      (let ((local (gnus-agent-get-local group
919 920 921 922 923
					 real-group command-method)))
	(gnus-agent-set-local group
			      nil nil
			      real-group command-method)))))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
924 925 926 927
;;;
;;; Server mode commands
;;;

928
(defun gnus-agent-add-server ()
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
929
  "Enroll SERVER in the agent program."
930 931 932 933 934 935 936 937 938
  (interactive)
  (let* ((server       (gnus-server-server-name))
         (named-server (gnus-server-named-server))
         (method       (and server
                            (gnus-server-get-method nil server))))
    (unless server
      (error "No server on the current line"))

    (when (gnus-agent-method-p method)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
939
      (error "Server already in the agent program"))
940 941 942 943 944

    (push named-server gnus-agent-covered-methods)

    (setq gnus-agent-method-p-cache nil)
    (gnus-server-update-server server)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
945
    (gnus-agent-write-servers)
946
    (gnus-message 1 "Entered %s into the Agent" server)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
947

948
(defun gnus-agent-remove-server ()
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
949
  "Remove SERVER from the agent program."
950 951 952 953 954 955 956
  (interactive)
  (let* ((server       (gnus-server-server-name))
         (named-server (gnus-server-named-server)))
    (unless server
      (error "No server on the current line"))

    (unless (member named-server gnus-agent-covered-methods)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
957
      (error "Server not in the agent program"))
958

959
    (setq gnus-agent-covered-methods
960 961 962 963
          (delete named-server gnus-agent-covered-methods)
          gnus-agent-method-p-cache nil)

    (gnus-server-update-server server)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
964
    (gnus-agent-write-servers)
965
    (gnus-message 1 "Removed %s from the agent" server)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
966 967 968

(defun gnus-agent-read-servers ()
  "Read the alist of covered servers."
969
  (setq gnus-agent-covered-methods
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
        (gnus-agent-read-file
         (nnheader-concat gnus-agent-directory "lib/servers"))
        gnus-agent-method-p-cache nil)

  ;; I am called so early in start-up that I can not validate server
  ;; names.  When that is the case, I skip the validation.  That is
  ;; alright as the gnus startup code calls the validate methods
  ;; directly.
  (if gnus-server-alist
      (gnus-agent-read-servers-validate)))

(defun gnus-agent-read-servers-validate ()
  (mapcar (lambda (server-or-method)
            (let* ((server (if (stringp server-or-method)
                               server-or-method
                             (gnus-method-to-server server-or-method)))
                   (method (gnus-server-to-method server)))
              (if method
                  (unless (member server gnus-agent-covered-methods)
                    (push server gnus-agent-covered-methods)
                    (setq gnus-agent-method-p-cache nil))
                (gnus-message 1 "Ignoring disappeared server `%s'" server))))
          (prog1 gnus-agent-covered-methods
            (setq gnus-agent-covered-methods nil))))

(defun gnus-agent-read-servers-validate-native (native-method)
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
996
  (setq gnus-agent-covered-methods
997 998 999 1000 1001
        (mapcar (lambda (method)
                  (if (or (not method)
                          (equal method native-method))
                      "native"
                    method)) gnus-agent-covered-methods)))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1002 1003 1004

(defun gnus-agent-write-servers ()
  "Write the alist of covered servers."
1005 1006 1007 1008
  (gnus-make-directory (nnheader-concat gnus-agent-directory "lib"))
  (let ((coding-system-for-write nnheader-file-coding-system)
	(file-name-coding-system nnmail-pathname-coding-system))
    (with-temp-file (nnheader-concat gnus-agent-directory "lib/servers")
1009 1010
      (prin1 gnus-agent-covered-methods
	     (current-buffer)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

;;;
;;; Summary commands
;;;

(defun gnus-agent-mark-article (n &optional unmark)
  "Mark the next N articles as downloadable.
If N is negative, mark backward instead.  If UNMARK is non-nil, remove
the mark instead.  The difference between N and the actual number of
articles marked is returned."
  (interactive "p")
  (let ((backward (< n 0))
	(n (abs n)))
    (while (and
	    (> n 0)
	    (progn
	      (gnus-summary-set-agent-mark
	       (gnus-summary-article-number) unmark)
	      (zerop (gnus-summary-next-subject (if backward -1 1) nil t))))
      (setq n (1- n)))
    (when (/= 0 n)
      (gnus-message 7 "No more articles"))
    (gnus-summary-recenter)
    (gnus-summary-position-point)
    n))

(defun gnus-agent-unmark-article (n)
  "Remove the downloadable mark from the next N articles.
If N is negative, unmark backward instead.  The difference between N and
the actual number of articles unmarked is returned."
  (interactive "p")
  (gnus-agent-mark-article n t))

(defun gnus-agent-toggle-mark (n)
  "Toggle the downloadable mark from the next N articles.
If N is negative, toggle backward instead.  The difference between N and
the actual number of articles toggled is returned."
  (interactive "p")
  (gnus-agent-mark-article n 'toggle))

(defun gnus-summary-set-agent-mark (article &optional unmark)
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  "Mark ARTICLE as downloadable.  If UNMARK is nil, article is marked.
When UNMARK is t, the article is unmarked.  For any other value, the
article's mark is toggled."
  (let ((unmark (cond ((eq nil unmark)
		       nil)
		      ((eq t unmark)
		       t)
		      (t
		       (memq article gnus-newsgroup-downloadable)))))
    (when (gnus-summary-goto-subject article nil t)
      (gnus-summary-update-mark
       (if unmark
           (progn
             (setq gnus-newsgroup-downloadable
                   (delq article gnus-newsgroup-downloadable))
             (gnus-article-mark article))
	 (setq gnus-newsgroup-downloadable
	       (gnus-add-to-sorted-list gnus-newsgroup-downloadable article))
	 gnus-downloadable-mark)
       'unread))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1072

1073
;;;###autoload
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1074
(defun gnus-agent-get-undownloaded-list ()
1075
  "Construct list of articles that have not been downloaded."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1076
  (let ((gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name)))
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    (when (set (make-local-variable 'gnus-newsgroup-agentized)
               (gnus-agent-method-p gnus-command-method))
      (let* ((alist (gnus-agent-load-alist gnus-newsgroup-name))
             (headers (sort (mapcar (lambda (h)
                                      (mail-header-number h))
                                    gnus-newsgroup-headers) '<))
             (cached (and gnus-use-cache gnus-newsgroup-cached))
             (undownloaded (list nil))
             (tail-undownloaded undownloaded)
             (unfetched (list nil))
             (tail-unfetched unfetched))
	(while (and alist headers)
	  (let ((a (caar alist))
		(h (car headers)))
	    (cond ((< a h)
		   ;; Ignore IDs in the alist that are not being
		   ;; displayed in the summary.
		   (setq alist (cdr alist)))
		  ((> a h)
                   ;; Headers that are not in the alist should be
                   ;; fictious (see nnagent-retrieve-headers); they
                   ;; imply that this article isn't in the agent.
		   (gnus-agent-append-to-list tail-undownloaded h)
		   (gnus-agent-append-to-list tail-unfetched    h)
1101
                   (setq headers (cdr headers)))
1102 1103 1104 1105 1106 1107 1108 1109
		  ((cdar alist)
		   (setq alist (cdr alist))
		   (setq headers (cdr headers))
		   nil                  ; ignore already downloaded
		   )
		  (t
		   (setq alist (cdr alist))
		   (setq headers (cdr headers))
1110

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
                   ;; This article isn't in the agent.  Check to see
                   ;; if it is in the cache.  If it is, it's been
                   ;; downloaded.
                   (while (and cached (< (car cached) a))
                     (setq cached (cdr cached)))
                   (unless (equal a (car cached))
                     (gnus-agent-append-to-list tail-undownloaded a))))))

	(while headers
          (let ((num (pop headers)))
            (gnus-agent-append-to-list tail-undownloaded num)
            (gnus-agent-append-to-list tail-unfetched    num)))

	(setq gnus-newsgroup-undownloaded (cdr undownloaded)
              gnus-newsgroup-unfetched    (cdr unfetched))))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1126 1127

(defun gnus-agent-catchup ()
1128 1129 1130
  "Mark as read all unhandled articles.
An article is unhandled if it is neither cached, nor downloaded, nor
downloadable."
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1131 1132
  (interactive)
  (save-excursion
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    (let ((articles gnus-newsgroup-undownloaded))
      (when (or gnus-newsgroup-downloadable
                gnus-newsgroup-cached)
        (setq articles (gnus-sorted-ndifference
			(gnus-sorted-ndifference
			 (gnus-copy-sequence articles)
			 gnus-newsgroup-downloadable)
			gnus-newsgroup-cached)))

      (while articles
        (gnus-summary-mark-article
         (pop articles) gnus-catchup-mark)))
    (gnus-summary-position-point)))

(defun gnus-agent-summary-fetch-series ()
  (interactive)
  (when gnus-newsgroup-processable
    (setq gnus-newsgroup-downloadable
          (let* ((dl gnus-newsgroup-downloadable)
1152 1153 1154
		 (processable (sort (gnus-copy-sequence gnus-newsgroup-processable) '<))
                 (gnus-newsgroup-downloadable processable))
	    (gnus-agent-summary-fetch-group)
1155

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
            ;; For each article that I processed that is no longer
            ;; undownloaded, remove its processable mark.

	    (mapc #'gnus-summary-remove-process-mark 
		  (gnus-sorted-ndifference gnus-newsgroup-processable gnus-newsgroup-undownloaded))

            ;; The preceeding call to (gnus-agent-summary-fetch-group)
            ;; updated the temporary gnus-newsgroup-downloadable to
            ;; remove each article successfully fetched.  Now, I
            ;; update the real gnus-newsgroup-downloadable to only
            ;; include undownloaded articles.
	    (gnus-sorted-ndifference dl (gnus-sorted-ndifference processable gnus-newsgroup-undownloaded))))))
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

(defun gnus-agent-summary-fetch-group (&optional all)
  "Fetch the downloadable articles in the group.
Optional arg ALL, if non-nil, means to fetch all articles."
  (interactive "P")
  (let ((articles
	 (if all gnus-newsgroup-articles
	   gnus-newsgroup-downloadable))
	(gnus-command-method (gnus-find-method-for-group gnus-newsgroup-name))
        fetched-articles)
    (gnus-agent-while-plugged
      (unless articles
        (error "No articles to download"))
      (gnus-agent-with-fetch
        (setq gnus-newsgroup-undownloaded
              (gnus-sorted-ndifference
               gnus-newsgroup-undownloaded
               (setq fetched-articles
                     (gnus-agent-fetch-articles
                      gnus-newsgroup-name articles)))))
      (save-excursion
        (dolist (article articles)
1190
          (let ((was-marked-downloadable
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
                 (memq article gnus-newsgroup-downloadable)))
            (cond (gnus-agent-mark-unread-after-downloaded
                   (setq gnus-newsgroup-downloadable
                         (delq article gnus-newsgroup-downloadable))

                   (gnus-summary-mark-article article gnus-unread-mark))
                  (was-marked-downloadable
                   (gnus-summary-set-agent-mark article t)))
            (when (gnus-summary-goto-subject article nil t)
              (gnus-summary-update-download-mark article))))))
    fetched-articles))

(defun gnus-agent-fetch-selected-article ()
  "Fetch the current article as it is selected.
This can be added to `gnus-select-article-hook' or
`gnus-mark-article-hook'."
  (let ((gnus-command-method gnus-current-select-method))
    (when (and gnus-plugged (gnus-agent-method-p gnus-command-method))
      (when (gnus-agent-fetch-articles
             gnus-newsgroup-name
	     (list gnus-current-article))
	(setq gnus-newsgroup-undownloaded
	      (delq gnus-current-article gnus-newsgroup-undownloaded))
        (gnus-summary-update-download-mark gnus-current-article)))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1215 1216 1217 1218 1219

;;;
;;; Internal functions
;;;

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
(defun gnus-agent-synchronize-group-flags (group actions server)
"Update a plugged group by performing the indicated actions."
  (let* ((gnus-command-method (gnus-server-to-method server))
	 (info
	  ;; This initializer is required as gnus-request-set-mark
	  ;; calls gnus-group-real-name to strip off the host name
	  ;; before calling the backend.  Now that the backend is
	  ;; trying to call gnus-request-set-mark, I have to
	  ;; reconstruct the original group name.
	  (or (gnus-get-info group)
1230 1231
	      (gnus-get-info
	       (setq group (gnus-group-full-name
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
			    group gnus-command-method))))))
    (gnus-request-set-mark group actions)

    (when info
      (dolist (action actions)
	(let ((range (nth 0 action))
	      (what  (nth 1 action))
	      (marks (nth 2 action)))
	  (dolist (mark marks)
	    (cond ((eq mark 'read)
1242
		   (gnus-info-set-read
1243 1244 1245 1246 1247 1248
		    info
		    (funcall (if (eq what 'add)
				 'gnus-range-add
			       'gnus-remove-from-range)
			     (gnus-info-read info)
			     range))
1249
		   (gnus-get-unread-articles-in-group
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
		    info
		    (gnus-active (gnus-info-group info))))
		  ((memq mark '(tick))
		   (let ((info-marks (assoc mark (gnus-info-marks info))))
		     (unless info-marks
		       (gnus-info-set-marks info (cons (setq info-marks (list mark)) (gnus-info-marks info))))
		     (setcdr info-marks (funcall (if (eq what 'add)
				  'gnus-range-add
				'gnus-remove-from-range)
			      (cdr info-marks)
1260 1261 1262 1263 1264 1265 1266
			      range))))))))

      ;;Marks can be synchronized at any time by simply toggling from
      ;;unplugged to plugged.  If that is what is happening right now, make
      ;;sure that the group buffer is up to date.
          (when (gnus-buffer-live-p gnus-group-buffer)
            (gnus-group-update-group group t)))
1267 1268
    nil))

Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1269 1270 1271
(defun gnus-agent-save-active (method)
  (when (gnus-agent-method-p method)
    (let* ((gnus-command-method method)
1272
	   (new (gnus-make-hashtable (count-lines (point-min) (point-max))))
Lars Magne Ingebrigtsen's avatar
Lars Magne Ingebrigtsen committed
1273
	   (file (gnus-agent-lib-file "active")))
1274
      (gnus-active-to-gnus-format nil new)
1275 1276 1277 1278 1279
      (gnus-agent-write-active file new)
      (erase-buffer)